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
27 * <?plugin CreateBib pagename||=whatever ?>
29 * @author: Lea Viljanen
32 class WikiPlugin_CreateBib
36 return _("CreateBib");
39 function getDescription() {
40 return _("Automatically create a Bibtex file from linked pages");
43 function getVersion() {
44 return preg_replace("/[Revision: $]/", '',
48 function getDefaultArguments() {
49 return array( 'pagename' => '[pagename]', // The page from which the BibTex file is generated
53 function preg_quote ($heading) {
54 return str_replace(array("/",".","?","*"),
55 array('\/','\.','\?','\*'), $heading);
59 // Have to include the $starttag and $endtag to the regexps...
60 function extractBibTeX (&$content, $starttag, $endtag)
66 for ($i=0; $i<count($content); $i++)
68 // $starttag shows when to start
69 if (preg_match('/^@/',$content[$i],$match)) {
72 // $endtag shows when to stop
73 else if (preg_match('/^\}/',$content[$i],$match)) {
77 $bib[] = $content[$i];
78 if ($stop) $start = false;
84 // Extract article links. Current markup is by * characters...
85 // Assume straight list
86 function extractArticles (&$content) {
88 for ($i=0; $i<count($content); $i++) {
89 // Should match "* [WikiPageName] whatever"
90 //if (preg_match('/^\s*\*\s+(\[.+\])/',$content[$i],$match))
91 if (preg_match('/^\s*\*\s+\[(.+)\]/',$content[$i],$match))
93 $articles[] = $match[1];
100 function dumpFile(&$thispage, $filename) {
101 include_once("lib/loadsave.php");
102 $mailified = MailifyPage($thispage);
104 $attrib = array('mtime' => $thispage->get('mtime'), 'is_ascii' => 1);
106 $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $filename);
107 $zip->addRegularFile( FilenameForPage($thispage->getName()),
108 $mailified, $attrib);
113 function run($dbi, $argstr, $request, $basepage) {
114 extract($this->getArgs($argstr, $request));
116 // Expand relative page names.
117 $page = new WikiPageName($pagename, $basepage);
118 $pagename = $page->name;
121 return $this->error(_("no page specified"));
124 // Get the links page contents
125 $page = $dbi->getPage($pagename);
126 $current = $page->getCurrentRevision();
127 $content = $current->getContent();
129 // Prepare the button to trigger dumping
130 $dump_url = $request->getURLtoSelf(array("file" => "tube.bib"));
132 $dump_button = $WikiTheme->makeButton("To File",
135 $html = HTML::div(array('class' => 'bib','align' => 'left'));
136 $html->pushContent($dump_button, ' ');
137 $list = HTML::pre(array('id'=>'biblist', 'class' => 'bib'));
139 // Let's find the subpages
140 if ($articles = $this->extractArticles($content)) {
141 foreach ($articles as $h) {
143 // Now let's get the bibtex information from that subpage
144 $subpage = $dbi->getPage($h);
145 $subversion = $subpage->getCurrentRevision();
146 $subcontent = $subversion->getContent();
148 $bib = $this->extractBibTeX($subcontent, "@", "}");
150 // ...and finally just push the bibtex data to page
151 $foo = implode("\n", $bib);
152 $bar = $foo . "\n\n";
153 $list->pushContent(HTML::raw($bar));
156 $html->pushContent($list);
158 if ($request->getArg('file')) {
159 // Yes, we want to dump this somewhere
160 // Get the contents of this page
161 $p = $dbi->getPage($pagename);
162 $c = $p->getCurrentRevision();
163 $pagedata = $c->getContent();
164 $this->dumpFile($pagedata, $request->getArg('file'));
171 // Based on CreateTOC
178 // c-hanging-comment-ender-p: nil
179 // indent-tabs-mode: nil