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 along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * CreateBib: Automatically create a BibTex file from page
28 * <<CreateBib pagename||=whatever >>
30 * @author: Lea Viljanen
33 class WikiPlugin_CreateBib
36 function getDescription()
38 return _("Automatically create a Bibtex file from linked pages.");
41 function getDefaultArguments()
43 return array('pagename' => '[pagename]', // The page from which the BibTex file is generated
47 // Have to include the $starttag and $endtag to the regexps...
48 function extractBibTeX(&$content, $starttag, $endtag)
54 for ($i = 0; $i < count($content); $i++) {
55 // $starttag shows when to start
56 if (preg_match('/^@/', $content[$i], $match)) {
58 } // $endtag shows when to stop
59 else if (preg_match('/^\}/', $content[$i], $match)) {
63 $bib[] = $content[$i];
64 if ($stop) $start = false;
70 // Extract article links. Current markup is by * characters...
71 // Assume straight list
72 function extractArticles(&$content)
75 for ($i = 0; $i < count($content); $i++) {
76 // Should match "* [WikiPageName] whatever"
77 //if (preg_match('/^\s*\*\s+(\[.+\])/',$content[$i],$match))
78 if (preg_match('/^\s*\*\s+\[(.+)\]/', $content[$i], $match)) {
79 $articles[] = $match[1];
85 function dumpFile(&$thispage, $filename)
87 include_once 'lib/loadsave.php';
88 $mailified = MailifyPage($thispage);
90 $attrib = array('mtime' => $thispage->get('mtime'), 'is_ascii' => 1);
92 $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $filename);
93 $zip->addRegularFile(FilenameForPage($thispage->getName()),
99 function run($dbi, $argstr, $request, $basepage)
101 extract($this->getArgs($argstr, $request));
103 // Expand relative page names.
104 $page = new WikiPageName($pagename, $basepage);
105 $pagename = $page->name;
108 return $this->error(sprintf(_("A required argument ā%sā is missing."), 'pagename'));
111 // Get the links page contents
112 $page = $dbi->getPage($pagename);
113 $current = $page->getCurrentRevision();
114 $content = $current->getContent();
116 // Prepare the button to trigger dumping
117 $dump_url = $request->getURLtoSelf(array("file" => "tube.bib"));
119 $dump_button = $WikiTheme->makeButton("To File",
122 $html = HTML::div(array('class' => 'bib', 'align' => 'left'));
123 $html->pushContent($dump_button, ' ');
124 $list = HTML::pre(array('id' => 'biblist', 'class' => 'bib'));
126 // Let's find the subpages
127 if ($articles = $this->extractArticles($content)) {
128 foreach ($articles as $h) {
130 // Now let's get the bibtex information from that subpage
131 $subpage = $dbi->getPage($h);
132 $subversion = $subpage->getCurrentRevision();
133 $subcontent = $subversion->getContent();
135 $bib = $this->extractBibTeX($subcontent, "@", "}");
137 // ...and finally just push the bibtex data to page
138 $foo = implode("\n", $bib);
139 $bar = $foo . "\n\n";
140 $list->pushContent(HTML::raw($bar));
143 $html->pushContent($list);
145 if ($request->getArg('file')) {
146 // Yes, we want to dump this somewhere
147 // Get the contents of this page
148 $p = $dbi->getPage($pagename);
149 return $this->dumpFile($p, $request->getArg('file'));
160 // c-hanging-comment-ender-p: nil
161 // indent-tabs-mode: nil