]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CreateBib.php
Remove $Log
[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 getVersion() {
44         return preg_replace("/[Revision: $]/", '',
45                             "\$Revision$");
46     }
47
48     function getDefaultArguments() {
49         return array( 'pagename'  => '[pagename]', // The page from which the BibTex file is generated
50                       );
51     }
52
53     function preg_quote ($heading) {
54         return str_replace(array("/",".","?","*"),
55                            array('\/','\.','\?','\*'), $heading);
56     }
57     
58
59     // Have to include the $starttag and $endtag to the regexps...
60     function extractBibTeX (&$content, $starttag, $endtag) 
61     {
62         $bib = array();
63
64         $start = false;
65         $stop = false;
66         for ($i=0; $i<count($content); $i++) 
67         {
68             // $starttag shows when to start
69             if (preg_match('/^@/',$content[$i],$match)) {
70                 $start = true;
71             }
72             // $endtag shows when to stop
73             else if (preg_match('/^\}/',$content[$i],$match)) {
74                 $stop = true;
75             }
76             if ($start) {
77                 $bib[] = $content[$i];
78                 if ($stop) $start = false;
79             }
80         }
81         return $bib;
82     }
83
84     // Extract article links. Current markup is by * characters...
85     // Assume straight list
86     function extractArticles (&$content) {
87         $articles = array();
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))
92             {
93                 $articles[] = $match[1];
94             }
95         }
96         return $articles;
97     }
98                 
99
100     function dumpFile(&$thispage, $filename) {
101       include_once("lib/loadsave.php");
102       $mailified = MailifyPage($thispage);
103
104       $attrib = array('mtime' => $thispage->get('mtime'), 'is_ascii' => 1);
105
106       $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $filename);
107       $zip->addRegularFile( FilenameForPage($thispage->getName()),
108                             $mailified, $attrib);
109       $zip->finish();
110
111     }
112
113     function run($dbi, $argstr, $request, $basepage) {
114         extract($this->getArgs($argstr, $request));
115         if ($pagename) {
116             // Expand relative page names.
117             $page = new WikiPageName($pagename, $basepage);
118             $pagename = $page->name;
119         }
120         if (!$pagename) {
121             return $this->error(_("no page specified"));
122         }
123
124         // Get the links page contents
125         $page = $dbi->getPage($pagename);
126         $current = $page->getCurrentRevision();
127         $content = $current->getContent();
128
129         // Prepare the button to trigger dumping
130         $dump_url = $request->getURLtoSelf(array("file" => "tube.bib"));
131         global $WikiTheme;
132         $dump_button = $WikiTheme->makeButton("To File", 
133                                           $dump_url , 'foo');
134
135         $html = HTML::div(array('class' => 'bib','align' => 'left'));
136         $html->pushContent($dump_button, ' ');
137         $list = HTML::pre(array('id'=>'biblist', 'class' => 'bib'));
138
139         // Let's find the subpages
140         if ($articles = $this->extractArticles($content)) {
141             foreach ($articles as $h) {
142
143                 // Now let's get the bibtex information from that subpage
144                 $subpage = $dbi->getPage($h);
145                 $subversion = $subpage->getCurrentRevision();
146                 $subcontent = $subversion->getContent();
147
148                 $bib = $this->extractBibTeX($subcontent, "@", "}");
149
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));
154             }
155         }
156         $html->pushContent($list);
157
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'));
165         }
166
167         return $html;
168     }
169 };
170
171 // Based on CreateTOC
172
173 // For emacs users
174 // Local Variables:
175 // mode: php
176 // tab-width: 8
177 // c-basic-offset: 4
178 // c-hanging-comment-ender-p: nil
179 // indent-tabs-mode: nil
180 // End:
181 ?>