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