]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CreateBib.php
rcs_id no longer makes sense with Subversion global version number
[SourceForge/phpwiki.git] / lib / plugin / CreateBib.php
1 <?php // -*-php-*-
2 // rcs_id('$Id$');
3 /*
4  * Copyright 2004 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 /**
24  * CreateBib:  Automatically create a BibTex file from page
25  *
26  * Usage:
27  *  <?plugin CreateBib pagename||=whatever ?>
28  *
29  * @author:  Lea Viljanen
30  */
31
32 class WikiPlugin_CreateBib
33 extends WikiPlugin
34 {
35     function getName() {
36         return _("CreateBib");
37     }
38
39     function getDescription() {
40         return _("Automatically create a Bibtex file from linked pages");
41     }
42
43     function getDefaultArguments() {
44         return array( 'pagename'  => '[pagename]', // The page from which the BibTex file is generated
45                       );
46     }
47
48     function preg_quote ($heading) {
49         return str_replace(array("/",".","?","*"),
50                                array('\/','\.','\?','\*'), $heading);
51     }
52
53
54     // Have to include the $starttag and $endtag to the regexps...
55     function extractBibTeX (&$content, $starttag, $endtag)
56     {
57         $bib = array();
58
59         $start = false;
60         $stop = false;
61         for ($i=0; $i<count($content); $i++)
62         {
63             // $starttag shows when to start
64             if (preg_match('/^@/',$content[$i],$match)) {
65                 $start = true;
66             }
67             // $endtag shows when to stop
68             else if (preg_match('/^\}/',$content[$i],$match)) {
69                 $stop = true;
70             }
71             if ($start) {
72                 $bib[] = $content[$i];
73                 if ($stop) $start = false;
74             }
75         }
76         return $bib;
77     }
78
79     // Extract article links. Current markup is by * characters...
80     // Assume straight list
81     function extractArticles (&$content) {
82         $articles = array();
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))
87             {
88                 $articles[] = $match[1];
89             }
90         }
91         return $articles;
92     }
93
94
95     function dumpFile(&$thispage, $filename) {
96       include_once("lib/loadsave.php");
97       $mailified = MailifyPage($thispage);
98
99       $attrib = array('mtime' => $thispage->get('mtime'), 'is_ascii' => 1);
100
101       $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $filename);
102       $zip->addRegularFile( FilenameForPage($thispage->getName()),
103                             $mailified, $attrib);
104       $zip->finish();
105
106     }
107
108     function run($dbi, $argstr, $request, $basepage) {
109         extract($this->getArgs($argstr, $request));
110         if ($pagename) {
111             // Expand relative page names.
112             $page = new WikiPageName($pagename, $basepage);
113             $pagename = $page->name;
114         }
115         if (!$pagename) {
116             return $this->error(_("no page specified"));
117         }
118
119         // Get the links page contents
120         $page = $dbi->getPage($pagename);
121         $current = $page->getCurrentRevision();
122         $content = $current->getContent();
123
124         // Prepare the button to trigger dumping
125         $dump_url = $request->getURLtoSelf(array("file" => "tube.bib"));
126         global $WikiTheme;
127         $dump_button = $WikiTheme->makeButton("To File",
128                                           $dump_url , 'foo');
129
130         $html = HTML::div(array('class' => 'bib','align' => 'left'));
131         $html->pushContent($dump_button, ' ');
132         $list = HTML::pre(array('id'=>'biblist', 'class' => 'bib'));
133
134         // Let's find the subpages
135         if ($articles = $this->extractArticles($content)) {
136             foreach ($articles as $h) {
137
138                 // Now let's get the bibtex information from that subpage
139                 $subpage = $dbi->getPage($h);
140                 $subversion = $subpage->getCurrentRevision();
141                 $subcontent = $subversion->getContent();
142
143                 $bib = $this->extractBibTeX($subcontent, "@", "}");
144
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));
149             }
150         }
151         $html->pushContent($list);
152
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'));
160         }
161
162         return $html;
163     }
164 };
165
166 // Based on CreateTOC
167
168 // For emacs users
169 // Local Variables:
170 // mode: php
171 // tab-width: 8
172 // c-basic-offset: 4
173 // c-hanging-comment-ender-p: nil
174 // indent-tabs-mode: nil
175 // End:
176 ?>