]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageDump.php
var --> public
[SourceForge/phpwiki.git] / lib / plugin / PageDump.php
1 <?php
2
3 /*
4  * Copyright (C) 2003 $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  * PhpWikiPlugin for PhpWiki developers to generate single page dumps
25  * for checking into Subversion, or for users or the admin to produce a
26  * downloadable page dump of a single page.
27  *
28  * This plugin will also be useful to (semi-)automatically sync pages
29  * directly between two wikis. First the LoadFile function of
30  * PhpWikiAdministration needs to be updated to handle URLs again, and
31  * add loading capability from InterWiki addresses.
32  *
33  * Multiple revisions in one file handled by format=backup
34  *
35  * TODO: What about comments/summary field? quoted-printable?
36  *
37  * Usage:
38  *  Direct URL access:
39  *   http://...phpwiki/PageDump?page=HomePage?format=forsvn
40  *   http://...phpwiki/index.php?PageDump&page=HomePage
41  *   http://...phpwiki/index.php?PageDump&page=HomePage&download=1
42  *  Static:
43  *   <<PageDump page=HomePage>>
44  *  Dynamic form (put both on the page):
45  *   <<PageDump>>
46  *   <?plugin-form PageDump?>
47  *  Typical usage: as actionbar button
48  */
49
50 class WikiPlugin_PageDump
51     extends WikiPlugin
52 {
53     public $MessageId;
54
55     function getName()
56     {
57         return _("PageDump");
58     }
59
60     function getDescription()
61     {
62         return _("View a single page dump online.");
63     }
64
65     function getDefaultArguments()
66     {
67         return array('s' => false,
68             'page' => '[pagename]',
69             //'encoding' => 'binary', // 'binary', 'quoted-printable'
70             'format' => false, // 'normal', 'forsvn', 'forcvs', 'backup'
71             // display within WikiPage or give a downloadable
72             // raw pgsrc?
73             'download' => false);
74     }
75
76     function run($dbi, $argstr, &$request, $basepage)
77     {
78         extract($this->getArgs($argstr, $request));
79         // allow plugin-form
80         if (!empty($s)) {
81             $page = $s;
82         }
83         if (!$page) {
84             return '';
85         }
86         if (!$dbi->isWikiPage($page)) {
87             return $this->error(sprintf(_("Page ā€œ%sā€ does not exist."), $page));
88         }
89
90         // Check if user is allowed to get the Page.
91         if (!mayAccessPage('view', $page)) {
92             return $this->error(sprintf(_("Illegal access to page %s: no read access"),
93                 $page));
94         }
95
96         $p = $dbi->getPage($page);
97         include_once 'lib/loadsave.php';
98         $mailified = MailifyPage($p, ($format == 'backup') ? 99 : 1);
99
100         // fixup_headers massages the page dump headers depending on
101         // the 'format' argument, 'normal'(default) or 'forsvn'.
102         //
103         // Normal: add unique Message-Id, don't
104         // strip any fields from Content-Type.
105         //
106         // ForCVS: strip attributes from
107         // Content-Type field: "author", "version", "lastmodified",
108         // "author_id", "hits".
109
110         $this->pagename = $page;
111         $this->generateMessageId($mailified);
112         if (($format == 'forsvn') || ($format == 'forcvs'))
113             $this->fixup_headers_forsvn($mailified);
114         else // backup or normal
115             $this->fixup_headers($mailified);
116
117         if ($download) {
118             // TODO: we need a way to hook into the generated headers, to override
119             // Content-Type, Set-Cookie, Cache-control, ...
120             $request->discardOutput(); // Hijack the http request from PhpWiki.
121             ob_end_clean(); // clean up after hijacking $request
122             //while (@ob_end_flush()); //debugging
123             $filename = FilenameForPage($page);
124             Header("Content-disposition: attachment; filename=\""
125                 . $filename . "\"");
126             // We generate 3 Content-Type headers! first in loadsave,
127             // then here and the mimified string $mailified also has it!
128             // This one is correct and overwrites the others.
129             Header("Content-Type: application/octet-stream; name=\""
130                 . $filename . "\"; charset=\"" . 'UTF-8'
131                 . "\"");
132             $request->checkValidators();
133             // let $request provide last modified & etag
134             Header("Content-Id: <" . $this->MessageId . ">");
135             // be nice to http keepalive~s
136             Header("Content-Length: " . strlen($mailified));
137
138             // Here comes our prepared mime file
139             echo $mailified;
140             exit(); // noreturn! php exits.
141         }
142         // We are displaing inline preview in a WikiPage, so wrap the
143         // text if it is too long--unless quoted-printable (TODO).
144         $mailified = wordwrap($mailified, 70);
145
146         $dlsvn = Button(array( //'page' => $page,
147                 'action' => $this->getName(),
148                 'format' => 'forsvn',
149                 'download' => true),
150             _("Download for Subversion"),
151             $page);
152         $dl = Button(array( //'page' => $page,
153                 'action' => $this->getName(),
154                 'download' => true),
155             _("Download for backup"),
156             $page);
157         $dlall = Button(array( //'page' => $page,
158                 'action' => $this->getName(),
159                 'format' => 'backup',
160                 'download' => true),
161             _("Download all revisions for backup"),
162             $page);
163
164         $h2 = HTML::h2(fmt("Preview: Page dump of %s",
165             WikiLink($page, 'auto')));
166         global $WikiTheme;
167         if (!$Sep = $WikiTheme->getButtonSeparator())
168             $Sep = " ";
169
170         if ($format == 'forsvn') {
171             $desc = _("(formatted for PhpWiki developers as pgsrc template, not for backing up)");
172             $altpreviewbuttons = HTML(
173                 Button(array('action' => $this->getName()),
174                     _("Preview as normal format"),
175                     $page),
176                 $Sep,
177                 Button(array(
178                         'action' => $this->getName(),
179                         'format' => 'backup'),
180                     _("Preview as backup format"),
181                     $page));
182         } elseif ($format == 'backup') {
183             $desc = _("(formatted for backing up: all revisions)"); // all revisions
184             $altpreviewbuttons = HTML(
185                 Button(array('action' => $this->getName(),
186                         'format' => 'forsvn'),
187                     _("Preview as developer format"),
188                     $page),
189                 $Sep,
190                 Button(array(
191                         'action' => $this->getName(),
192                         'format' => ''),
193                     _("Preview as normal format"),
194                     $page));
195         } else {
196             $desc = _("(normal formatting: latest revision only)");
197             $altpreviewbuttons = HTML(
198                 Button(array('action' => $this->getName(),
199                         'format' => 'forsvn'),
200                     _("Preview as developer format"),
201                     $page),
202                 $Sep,
203                 Button(array(
204                         'action' => $this->getName(),
205                         'format' => 'backup'),
206                     _("Preview as backup format"),
207                     $page));
208         }
209         $warning = HTML(
210             _("Please use one of the downloadable versions rather than copying and pasting from the above preview.")
211                 . " " .
212                 _("The wordwrap of the preview doesn't take nested markup or list indentation into consideration!")
213                 . " ",
214             HTML::em(
215                 _("PhpWiki developers should manually inspect the downloaded file for nested markup before rewrapping with emacs and checking into Subversion.")
216             )
217         );
218
219         return HTML($h2, HTML::em($desc),
220             HTML::pre($mailified),
221             $altpreviewbuttons,
222             HTML::div(array('class' => 'error'),
223                 HTML::strong(_("Warning:")),
224                 " ", $warning),
225             $dl, $Sep, $dlall, $Sep, $dlsvn
226         );
227     }
228
229     // function handle_plugin_args_cruft(&$argstr, &$args) {
230     // }
231
232     function generateMessageId($mailified)
233     {
234         $array = explode("\n", $mailified);
235         // Extract lastmodifed from mailified document for Content-Id
236         // and/or Message-Id header, NOT from DB (page could have been
237         // edited by someone else since we started).
238         $m1 = preg_grep("/^\s+lastmodified\=(.*);/", $array);
239         $m1 = array_values($m1); //reset resulting keys
240         unset($array);
241         $m2 = preg_split("/(^\s+lastmodified\=)|(;)/", $m1[0], 2,
242             PREG_SPLIT_NO_EMPTY);
243
244         // insert message id into actual message when appropriate, NOT
245         // into http header should be part of fixup_headers, in the
246         // format:
247         // <abbrphpwikiversion.mtimeepochTZ%InterWikiLinktothispage@hostname>
248         // Hopefully this provides a unique enough identifier without
249         // using md5. Even though this particular wiki may not
250         // actually be part of InterWiki, including this info provides
251         // the wiki name and name of the page which is being
252         // represented as a text message.
253         $this->MessageId = implode('', explode('.', PHPWIKI_VERSION))
254             . "-" . $m2[0] . date("O")
255             //. "-". rawurlencode(WIKI_NAME.":" . $request->getURLtoSelf())
256             . "-" . rawurlencode(WIKI_NAME . ":" . $this->pagename)
257             . "@" . rawurlencode(SERVER_NAME);
258     }
259
260     function fixup_headers(&$mailified)
261     {
262         $return = explode("\n", $mailified);
263
264         // Leave message intact for backing up, just add Message-Id header before transmitting.
265         $item_to_insert = "Message-Id: <" . $this->MessageId . ">";
266         $insert_into_key_position = 2;
267         $returnval_ignored = array_splice($return,
268             $insert_into_key_position,
269             0, $item_to_insert);
270
271         $mailified = implode("\n", array_values($return));
272     }
273
274     function fixup_headers_forsvn(&$mailified)
275     {
276         $array = explode("\n", $mailified);
277
278         // Massage headers to prepare for developer checkin to Subversion.
279         /*
280             Strip out all this junk:
281             author=MeMe;
282             version=74;
283             lastmodified=1041561552;
284             author_id=127.0.0.1;
285             hits=146;
286         */
287         $killme = array("author", "version", "lastmodified",
288             "author_id", "hits", "owner", "acl");
289         // UltraNasty, fixme:
290         foreach ($killme as $pattern) {
291             $array = preg_replace("/^\s\s$pattern\=.*;/",
292                 /*$replacement =*/
293                 "zzzjunk", $array);
294         }
295         // remove deleted values from array
296         for ($i = 0; $i < count($array); $i++) {
297             if (trim($array[$i]) != "zzzjunk") { //nasty, fixme
298                 //trigger_error("'$array[$i]'");//debugging
299                 $return[] = $array[$i];
300             }
301         }
302
303         $mailified = implode("\n", $return);
304     }
305 }
306
307 // Local Variables:
308 // mode: php
309 // tab-width: 8
310 // c-basic-offset: 4
311 // c-hanging-comment-ender-p: nil
312 // indent-tabs-mode: nil
313 // End: