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 getDefaultArguments() {
44 return array( 'pagename' => '[pagename]', // The page from which the BibTex file is generated
48 function preg_quote ($heading) {
49 return str_replace(array("/",".","?","*"),
50 array('\/','\.','\?','\*'), $heading);
54 // Have to include the $starttag and $endtag to the regexps...
55 function extractBibTeX (&$content, $starttag, $endtag)
61 for ($i=0; $i<count($content); $i++)
63 // $starttag shows when to start
64 if (preg_match('/^@/',$content[$i],$match)) {
67 // $endtag shows when to stop
68 else if (preg_match('/^\}/',$content[$i],$match)) {
72 $bib[] = $content[$i];
73 if ($stop) $start = false;
79 // Extract article links. Current markup is by * characters...
80 // Assume straight list
81 function extractArticles (&$content) {
83 for ($i=0; $i<count($content); $i++) {
84 // Should match "* [WikiPageName] whatever"
85 //if (preg_match('/^\s*\*\s+(\[.+\])/',$content[$i],$match))
86 if (preg_match('/^\s*\*\s+\[(.+)\]/',$content[$i],$match))
88 $articles[] = $match[1];
95 function dumpFile(&$thispage, $filename) {
96 include_once("lib/loadsave.php");
97 $mailified = MailifyPage($thispage);
99 $attrib = array('mtime' => $thispage->get('mtime'), 'is_ascii' => 1);
101 $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $filename);
102 $zip->addRegularFile( FilenameForPage($thispage->getName()),
103 $mailified, $attrib);
108 function run($dbi, $argstr, $request, $basepage) {
109 extract($this->getArgs($argstr, $request));
111 // Expand relative page names.
112 $page = new WikiPageName($pagename, $basepage);
113 $pagename = $page->name;
116 return $this->error(_("no page specified"));
119 // Get the links page contents
120 $page = $dbi->getPage($pagename);
121 $current = $page->getCurrentRevision();
122 $content = $current->getContent();
124 // Prepare the button to trigger dumping
125 $dump_url = $request->getURLtoSelf(array("file" => "tube.bib"));
127 $dump_button = $WikiTheme->makeButton("To File",
130 $html = HTML::div(array('class' => 'bib','align' => 'left'));
131 $html->pushContent($dump_button, ' ');
132 $list = HTML::pre(array('id'=>'biblist', 'class' => 'bib'));
134 // Let's find the subpages
135 if ($articles = $this->extractArticles($content)) {
136 foreach ($articles as $h) {
138 // Now let's get the bibtex information from that subpage
139 $subpage = $dbi->getPage($h);
140 $subversion = $subpage->getCurrentRevision();
141 $subcontent = $subversion->getContent();
143 $bib = $this->extractBibTeX($subcontent, "@", "}");
145 // ...and finally just push the bibtex data to page
146 $foo = implode("\n", $bib);
147 $bar = $foo . "\n\n";
148 $list->pushContent(HTML::raw($bar));
151 $html->pushContent($list);
153 if ($request->getArg('file')) {
154 // Yes, we want to dump this somewhere
155 // Get the contents of this page
156 $p = $dbi->getPage($pagename);
157 $c = $p->getCurrentRevision();
158 $pagedata = $c->getContent();
159 $this->dumpFile($pagedata, $request->getArg('file'));
166 // Based on CreateTOC
173 // c-hanging-comment-ender-p: nil
174 // indent-tabs-mode: nil