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