File: CreateBib.php

package info (click to toggle)
phpwiki 1.3.12p3-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,956 kB
  • ctags: 21,608
  • sloc: php: 82,335; xml: 3,840; sh: 1,522; sql: 1,198; perl: 625; makefile: 562; awk: 28
file content (186 lines) | stat: -rw-r--r-- 5,745 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php // -*-php-*-
rcs_id('$Id: CreateBib.php,v 1.1 2006/03/07 21:02:36 rurban Exp $');
/*
 Copyright 2004 $ThePhpWikiProgrammingTeam

 This file is part of PhpWiki.

 PhpWiki is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 PhpWiki is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with PhpWiki; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * CreateBib:  Automatically create a BibTex file from page
 *
 * Usage:   
 *  <?plugin CreateBib pagename||=whatever ?>
 *                     
 * @author:  Lea Viljanen
 */

class WikiPlugin_CreateBib
extends WikiPlugin
{
    function getName() {
        return _("CreateBib");
    }

    function getDescription() {
        return _("Automatically create a Bibtex file from linked pages");
    }

    function getVersion() {
        return preg_replace("/[Revision: $]/", '',
                            "\$Revision: 1.1 $");
    }

    function getDefaultArguments() {
        return array( 'pagename'  => '[pagename]', // The page from which the BibTex file is generated
                      );
    }

    function preg_quote ($heading) {
        return str_replace(array("/",".","?","*"),
    		           array('\/','\.','\?','\*'), $heading);
    }
    

    // Have to include the $starttag and $endtag to the regexps...
    function extractBibTeX (&$content, $starttag, $endtag) 
    {
        $bib = array();

        $start = false;
        $stop = false;
        for ($i=0; $i<count($content); $i++) 
        {
            // $starttag shows when to start
            if (preg_match('/^@/',$content[$i],$match)) {
                $start = true;
            }
            // $endtag shows when to stop
            else if (preg_match('/^\}/',$content[$i],$match)) {
                $stop = true;
            }
            if ($start) {
                $bib[] = $content[$i];
                if ($stop) $start = false;
            }
        }
        return $bib;
    }

    // Extract article links. Current markup is by * characters...
    // Assume straight list
    function extractArticles (&$content) {
        $articles = array();
        for ($i=0; $i<count($content); $i++) {
            // Should match "* [WikiPageName] whatever"
            //if (preg_match('/^\s*\*\s+(\[.+\])/',$content[$i],$match)) 
            if (preg_match('/^\s*\*\s+\[(.+)\]/',$content[$i],$match))
            {
                $articles[] = $match[1];
            }
        }
        return $articles;
    }
                

    function dumpFile(&$thispage, $filename) {
      include_once("lib/loadsave.php");
      $mailified = MailifyPage($thispage);

      $attrib = array('mtime' => $thispage->get('mtime'), 'is_ascii' => 1);

      $zip = new ZipWriter("Created by PhpWiki " . PHPWIKI_VERSION, $filename);
      $zip->addRegularFile( FilenameForPage($thispage->getName()),
                            $mailified, $attrib);
      $zip->finish();

    }

    function run($dbi, $argstr, $request, $basepage) {
        extract($this->getArgs($argstr, $request));
        if ($pagename) {
            // Expand relative page names.
            $page = new WikiPageName($pagename, $basepage);
            $pagename = $page->name;
        }
        if (!$pagename) {
            return $this->error(_("no page specified"));
        }

        // Get the links page contents
        $page = $dbi->getPage($pagename);
        $current = $page->getCurrentRevision();
        $content = $current->getContent();

	// Prepare the button to trigger dumping
	$dump_url = $request->getURLtoSelf(array("file" => "tube.bib"));
        global $WikiTheme;
        $dump_button = $WikiTheme->makeButton("To File", 
					  $dump_url , 'foo');

        $html = HTML::div(array('class' => 'bib','align' => 'left'));
	$html->pushContent($dump_button, ' ');
        $list = HTML::pre(array('name'=>'biblist','id'=>'biblist',
				'class' => 'bib'));

        // Let's find the subpages
        if ($articles = $this->extractArticles($content)) {
            foreach ($articles as $h) {

                // Now let's get the bibtex information from that subpage
                $subpage = $dbi->getPage($h);
                $subversion = $subpage->getCurrentRevision();
                $subcontent = $subversion->getContent();

                $bib = $this->extractBibTeX($subcontent, "@", "}");

                // ...and finally just push the bibtex data to page
                $foo = implode("\n", $bib);
                $bar = $foo . "\n\n";
                $list->pushContent(HTML::raw($bar));
            }
        }
        $html->pushContent($list);

        if ($request->getArg('file')) {
            // Yes, we want to dump this somewhere
            // Get the contents of this page
            $p = $dbi->getPage($pagename);
            $c = $p->getCurrentRevision();
            $pagedata = $c->getContent();
            $this->dumpFile($pagedata, $request->getArg('file'));
	}

        return $html;
    }
};

// $Log: CreateBib.php,v $
// Revision 1.1  2006/03/07 21:02:36  rurban
// contributed by Lea Viljanen
//
// Based on CreateTOC

// For emacs users
// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>