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