]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageDump.php
Revert wrong smart quotes
[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     var $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         if (!$page)
83             return '';
84         if (!$dbi->isWikiPage($page))
85             return fmt("Page %s not found.",
86                 WikiLink($page, 'unknown'));
87
88         // Check if user is allowed to get the Page.
89         if (!mayAccessPage('view', $page)) {
90             return $this->error(sprintf(_("Illegal access to page %s: no read access"),
91                 $page));
92         }
93
94         $p = $dbi->getPage($page);
95         include_once 'lib/loadsave.php';
96         $mailified = MailifyPage($p, ($format == 'backup') ? 99 : 1);
97
98         // fixup_headers massages the page dump headers depending on
99         // the 'format' argument, 'normal'(default) or 'forsvn'.
100         //
101         // Normal: Don't add X-Rcs-Id, add unique Message-Id, don't
102         // strip any fields from Content-Type.
103         //
104         // ForCVS: Add empty X-Rcs-Id, strip attributes from
105         // Content-Type field: "author", "version", "lastmodified",
106         // "author_id", "hits".
107
108         $this->pagename = $page;
109         $this->generateMessageId($mailified);
110         if (($format == 'forsvn') || ($format == 'forcvs'))
111             $this->fixup_headers_forsvn($mailified);
112         else // backup or normal
113             $this->fixup_headers($mailified);
114
115         if ($download) {
116             // TODO: we need a way to hook into the generated headers, to override
117             // Content-Type, Set-Cookie, Cache-control, ...
118             $request->discardOutput(); // Hijack the http request from PhpWiki.
119             ob_end_clean(); // clean up after hijacking $request
120             //while (@ob_end_flush()); //debugging
121             $filename = FilenameForPage($page);
122             Header("Content-disposition: attachment; filename=\""
123                 . $filename . "\"");
124             // Read charset from generated page itself.
125             // Inconsequential at the moment, since loadsave.php
126             // always generates headers.
127             $charset = $p->get('charset');
128             if (!$charset) $charset = $GLOBALS['charset'];
129             // We generate 3 Content-Type headers! first in loadsave,
130             // then here and the mimified string $mailified also has it!
131             // This one is correct and overwrites the others.
132             Header("Content-Type: application/octet-stream; name=\""
133                 . $filename . "\"; charset=\"" . $charset
134                 . "\"");
135             $request->checkValidators();
136             // let $request provide last modified & etag
137             Header("Content-Id: <" . $this->MessageId . ">");
138             // be nice to http keepalive~s
139             Header("Content-Length: " . strlen($mailified));
140
141             // Here comes our prepared mime file
142             echo $mailified;
143             exit; // noreturn! php exits.
144             return;
145         }
146         // We are displaing inline preview in a WikiPage, so wrap the
147         // text if it is too long--unless quoted-printable (TODO).
148         $mailified = wordwrap($mailified, 70);
149
150         $dlsvn = Button(array( //'page' => $page,
151                 'action' => $this->getName(),
152                 'format' => 'forsvn',
153                 'download' => true),
154             _("Download for Subversion"),
155             $page);
156         $dl = Button(array( //'page' => $page,
157                 'action' => $this->getName(),
158                 'download' => true),
159             _("Download for backup"),
160             $page);
161         $dlall = Button(array( //'page' => $page,
162                 'action' => $this->getName(),
163                 'format' => 'backup',
164                 'download' => true),
165             _("Download all revisions for backup"),
166             $page);
167
168         $h2 = HTML::h2(fmt("Preview: Page dump of %s",
169             WikiLink($page, 'auto')));
170         global $WikiTheme;
171         if (!$Sep = $WikiTheme->getButtonSeparator())
172             $Sep = " ";
173
174         if ($format == 'forsvn') {
175             $desc = _("(formatted for PhpWiki developers as pgsrc template, not for backing up)");
176             $altpreviewbuttons = HTML(
177                 Button(array('action' => $this->getName()),
178                     _("Preview as normal format"),
179                     $page),
180                 $Sep,
181                 Button(array(
182                         'action' => $this->getName(),
183                         'format' => 'backup'),
184                     _("Preview as backup format"),
185                     $page));
186         } elseif ($format == 'backup') {
187             $desc = _("(formatted for backing up: all revisions)"); // all revisions
188             $altpreviewbuttons = HTML(
189                 Button(array('action' => $this->getName(),
190                         'format' => 'forsvn'),
191                     _("Preview as developer format"),
192                     $page),
193                 $Sep,
194                 Button(array(
195                         'action' => $this->getName(),
196                         'format' => ''),
197                     _("Preview as normal format"),
198                     $page));
199         } else {
200             $desc = _("(normal formatting: latest revision only)");
201             $altpreviewbuttons = HTML(
202                 Button(array('action' => $this->getName(),
203                         'format' => 'forsvn'),
204                     _("Preview as developer format"),
205                     $page),
206                 $Sep,
207                 Button(array(
208                         'action' => $this->getName(),
209                         'format' => 'backup'),
210                     _("Preview as backup format"),
211                     $page));
212         }
213         $warning = HTML(
214             _("Please use one of the downloadable versions rather than copying and pasting from the above preview.")
215                 . " " .
216                 _("The wordwrap of the preview doesn't take nested markup or list indentation into consideration!")
217                 . " ",
218             HTML::em(
219                 _("PhpWiki developers should manually inspect the downloaded file for nested markup before rewrapping with emacs and checking into Subversion.")
220             )
221         );
222
223         return HTML($h2, HTML::em($desc),
224             HTML::pre($mailified),
225             $altpreviewbuttons,
226             HTML::div(array('class' => 'error'),
227                 HTML::strong(_("Warning:")),
228                 " ", $warning),
229             $dl, $Sep, $dlall, $Sep, $dlsvn
230         );
231     }
232
233     // function handle_plugin_args_cruft(&$argstr, &$args) {
234     // }
235
236     function generateMessageId($mailified)
237     {
238         $array = explode("\n", $mailified);
239         // Extract lastmodifed from mailified document for Content-Id
240         // and/or Message-Id header, NOT from DB (page could have been
241         // edited by someone else since we started).
242         $m1 = preg_grep("/^\s+lastmodified\=(.*);/", $array);
243         $m1 = array_values($m1); //reset resulting keys
244         unset($array);
245         $m2 = preg_split("/(^\s+lastmodified\=)|(;)/", $m1[0], 2,
246             PREG_SPLIT_NO_EMPTY);
247
248         // insert message id into actual message when appropriate, NOT
249         // into http header should be part of fixup_headers, in the
250         // format:
251         // <abbrphpwikiversion.mtimeepochTZ%InterWikiLinktothispage@hostname>
252         // Hopefully this provides a unique enough identifier without
253         // using md5. Even though this particular wiki may not
254         // actually be part of InterWiki, including this info provides
255         // the wiki name and name of the page which is being
256         // represented as a text message.
257         $this->MessageId = implode('', explode('.', PHPWIKI_VERSION))
258             . "-" . $m2[0] . date("O")
259             //. "-". rawurlencode(WIKI_NAME.":" . $request->getURLtoSelf())
260             . "-" . rawurlencode(WIKI_NAME . ":" . $this->pagename)
261             . "@" . rawurlencode(SERVER_NAME);
262     }
263
264     function fixup_headers(&$mailified)
265     {
266         $return = explode("\n", $mailified);
267
268         // Leave message intact for backing up, just add Message-Id header before transmitting.
269         $item_to_insert = "Message-Id: <" . $this->MessageId . ">";
270         $insert_into_key_position = 2;
271         $returnval_ignored = array_splice($return,
272             $insert_into_key_position,
273             0, $item_to_insert);
274
275         $mailified = implode("\n", array_values($return));
276     }
277
278     function fixup_headers_forsvn(&$mailified)
279     {
280         $array = explode("\n", $mailified);
281
282         // Massage headers to prepare for developer checkin to Subversion.
283         $item_to_insert = "X-Rcs-Id: \$Id\$";
284         $insert_into_key_position = 2;
285         $returnval_ignored = array_splice($array,
286             $insert_into_key_position,
287             0, $item_to_insert);
288
289         $item_to_insert = "  pgsrc_version=\"2 \$Revision\$\";";
290         $insert_into_key_position = 5;
291         $returnval_ignored = array_splice($array,
292             $insert_into_key_position,
293             0, $item_to_insert);
294         /*
295             Strip out all this junk:
296             author=MeMe;
297             version=74;
298             lastmodified=1041561552;
299             author_id=127.0.0.1;
300             hits=146;
301         */
302         $killme = array("author", "version", "lastmodified",
303             "author_id", "hits", "owner", "acl");
304         // UltraNasty, fixme:
305         foreach ($killme as $pattern) {
306             $array = preg_replace("/^\s\s$pattern\=.*;/",
307                 /*$replacement =*/
308                 "zzzjunk", $array);
309         }
310         // remove deleted values from array
311         for ($i = 0; $i < count($array); $i++) {
312             if (trim($array[$i]) != "zzzjunk") { //nasty, fixme
313                 //trigger_error("'$array[$i]'");//debugging
314                 $return[] = $array[$i];
315             }
316         }
317
318         $mailified = implode("\n", $return);
319     }
320 }
321
322 // Local Variables:
323 // mode: php
324 // tab-width: 8
325 // c-basic-offset: 4
326 // c-hanging-comment-ender-p: nil
327 // indent-tabs-mode: nil
328 // End: