4 * Copyright 2004 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
8 * PhpWiki is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * PhpWiki is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with PhpWiki; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * CreateBib: Automatically create a BibTex file from page
28 * <<CreateBib pagename||=whatever >>
30 * @author: Lea Viljanen
33 class WikiPlugin_CreateBib
37 return _("CreateBib");
40 function getDescription() {
41 return _("Automatically create a Bibtex file from linked pages");
44 function getDefaultArguments() {
45 return array( 'pagename' => '[pagename]', // The page from which the BibTex file is generated
49 function preg_quote ($heading) {
50 return str_replace(array("/",".","?","*"),
51 array('\/','\.','\?','\*'), $heading);
55 // Have to include the $starttag and $endtag to the regexps...
56 function extractBibTeX (&$content, $starttag, $endtag)
62 for ($i=0; $i<count($content); $i++)
64 // $starttag shows when to start
65 if (preg_match('/^@/',$content[$i],$match)) {
68 // $endtag shows when to stop
69 else if (preg_match('/^\}/',$content[$i],$match)) {
73 $bib[] = $content[$i];
74 if ($stop) $start = false;
80 // Extract article links. Current markup is by * characters...
81 // Assume straight list
82 function extractArticles (&$content) {
84 for ($i=0; $i<count($content); $i++) {
85 // Should match "* [WikiPageName] whatever"
86 //if (preg_match('/^\s*\*\s+(\[.+\])/',$content[$i],$match))
87 if (preg_match('/^\s*\*\s+\[(.+)\]/',$content[$i],$match))
89 $articles[] = $match[1];
96 function dumpFile(&$thispage, $filename) {
97 include_once("lib/loadsave.php");
98 $mailified = MailifyPage($thispage);
100 $attrib = array('mtime' => $thispage->get('mtime'), 'is_ascii' => 1);
102 $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $filename);
103 $zip->addRegularFile( FilenameForPage($thispage->getName()),
104 $mailified, $attrib);
109 function run($dbi, $argstr, $request, $basepage) {
110 extract($this->getArgs($argstr, $request));
112 // Expand relative page names.
113 $page = new WikiPageName($pagename, $basepage);
114 $pagename = $page->name;
117 return $this->error(_("no page specified"));
120 // Get the links page contents
121 $page = $dbi->getPage($pagename);
122 $current = $page->getCurrentRevision();
123 $content = $current->getContent();
125 // Prepare the button to trigger dumping
126 $dump_url = $request->getURLtoSelf(array("file" => "tube.bib"));
128 $dump_button = $WikiTheme->makeButton("To File",
131 $html = HTML::div(array('class' => 'bib','align' => 'left'));
132 $html->pushContent($dump_button, ' ');
133 $list = HTML::pre(array('id'=>'biblist', 'class' => 'bib'));
135 // Let's find the subpages
136 if ($articles = $this->extractArticles($content)) {
137 foreach ($articles as $h) {
139 // Now let's get the bibtex information from that subpage
140 $subpage = $dbi->getPage($h);
141 $subversion = $subpage->getCurrentRevision();
142 $subcontent = $subversion->getContent();
144 $bib = $this->extractBibTeX($subcontent, "@", "}");
146 // ...and finally just push the bibtex data to page
147 $foo = implode("\n", $bib);
148 $bar = $foo . "\n\n";
149 $list->pushContent(HTML::raw($bar));
152 $html->pushContent($list);
154 if ($request->getArg('file')) {
155 // Yes, we want to dump this somewhere
156 // Get the contents of this page
157 $p = $dbi->getPage($pagename);
158 $c = $p->getCurrentRevision();
159 $pagedata = $c->getContent();
160 $this->dumpFile($pagedata, $request->getArg('file'));
171 // c-hanging-comment-ender-p: nil
172 // indent-tabs-mode: nil