]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/CreateBib.php
contributed by Lea Viljanen
[SourceForge/phpwiki.git] / lib / plugin / CreateBib.php
1 <?php // -*-php-*-
2 rcs_id('$Id: CreateBib.php,v 1.1 2006-03-07 21:02:36 rurban Exp $');
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: 1.1 $");
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('name'=>'biblist','id'=>'biblist',
138                                 'class' => 'bib'));
139
140         // Let's find the subpages
141         if ($articles = $this->extractArticles($content)) {
142             foreach ($articles as $h) {
143
144                 // Now let's get the bibtex information from that subpage
145                 $subpage = $dbi->getPage($h);
146                 $subversion = $subpage->getCurrentRevision();
147                 $subcontent = $subversion->getContent();
148
149                 $bib = $this->extractBibTeX($subcontent, "@", "}");
150
151                 // ...and finally just push the bibtex data to page
152                 $foo = implode("\n", $bib);
153                 $bar = $foo . "\n\n";
154                 $list->pushContent(HTML::raw($bar));
155             }
156         }
157         $html->pushContent($list);
158
159         if ($request->getArg('file')) {
160             // Yes, we want to dump this somewhere
161             // Get the contents of this page
162             $p = $dbi->getPage($pagename);
163             $c = $p->getCurrentRevision();
164             $pagedata = $c->getContent();
165             $this->dumpFile($pagedata, $request->getArg('file'));
166         }
167
168         return $html;
169     }
170 };
171
172 // $Log: not supported by cvs2svn $
173 // Based on CreateTOC
174
175 // For emacs users
176 // Local Variables:
177 // mode: php
178 // tab-width: 8
179 // c-basic-offset: 4
180 // c-hanging-comment-ender-p: nil
181 // indent-tabs-mode: nil
182 // End:
183 ?>