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
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 // Have to include the $starttag and $endtag to the regexps...
50 function extractBibTeX (&$content, $starttag, $endtag)
56 for ($i=0; $i<count($content); $i++)
58 // $starttag shows when to start
59 if (preg_match('/^@/',$content[$i],$match)) {
62 // $endtag shows when to stop
63 else if (preg_match('/^\}/',$content[$i],$match)) {
67 $bib[] = $content[$i];
68 if ($stop) $start = false;
74 // Extract article links. Current markup is by * characters...
75 // Assume straight list
76 function extractArticles (&$content) {
78 for ($i=0; $i<count($content); $i++) {
79 // Should match "* [WikiPageName] whatever"
80 //if (preg_match('/^\s*\*\s+(\[.+\])/',$content[$i],$match))
81 if (preg_match('/^\s*\*\s+\[(.+)\]/',$content[$i],$match))
83 $articles[] = $match[1];
89 function dumpFile(&$thispage, $filename) {
90 include_once("lib/loadsave.php");
91 $mailified = MailifyPage($thispage);
93 $attrib = array('mtime' => $thispage->get('mtime'), 'is_ascii' => 1);
95 $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $filename);
96 $zip->addRegularFile( FilenameForPage($thispage->getName()),
102 function run($dbi, $argstr, $request, $basepage) {
103 extract($this->getArgs($argstr, $request));
105 // Expand relative page names.
106 $page = new WikiPageName($pagename, $basepage);
107 $pagename = $page->name;
110 return $this->error(sprintf(_("A required argument '%s' is missing."), 'pagename'));
113 // Get the links page contents
114 $page = $dbi->getPage($pagename);
115 $current = $page->getCurrentRevision();
116 $content = $current->getContent();
118 // Prepare the button to trigger dumping
119 $dump_url = $request->getURLtoSelf(array("file" => "tube.bib"));
121 $dump_button = $WikiTheme->makeButton("To File",
124 $html = HTML::div(array('class' => 'bib','align' => 'left'));
125 $html->pushContent($dump_button, ' ');
126 $list = HTML::pre(array('id'=>'biblist', 'class' => 'bib'));
128 // Let's find the subpages
129 if ($articles = $this->extractArticles($content)) {
130 foreach ($articles as $h) {
132 // Now let's get the bibtex information from that subpage
133 $subpage = $dbi->getPage($h);
134 $subversion = $subpage->getCurrentRevision();
135 $subcontent = $subversion->getContent();
137 $bib = $this->extractBibTeX($subcontent, "@", "}");
139 // ...and finally just push the bibtex data to page
140 $foo = implode("\n", $bib);
141 $bar = $foo . "\n\n";
142 $list->pushContent(HTML::raw($bar));
145 $html->pushContent($list);
147 if ($request->getArg('file')) {
148 // Yes, we want to dump this somewhere
149 // Get the contents of this page
150 $p = $dbi->getPage($pagename);
151 return $this->dumpFile($p, $request->getArg('file'));
162 // c-hanging-comment-ender-p: nil
163 // indent-tabs-mode: nil