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