]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageDump.php
Remove pgsrc_version
[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: add unique Message-Id, don't
102         // strip any fields from Content-Type.
103         //
104         // ForCVS: 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         }
145         // We are displaing inline preview in a WikiPage, so wrap the
146         // text if it is too long--unless quoted-printable (TODO).
147         $mailified = wordwrap($mailified, 70);
148
149         $dlsvn = Button(array( //'page' => $page,
150                 'action' => $this->getName(),
151                 'format' => 'forsvn',
152                 'download' => true),
153             _("Download for Subversion"),
154             $page);
155         $dl = Button(array( //'page' => $page,
156                 'action' => $this->getName(),
157                 'download' => true),
158             _("Download for backup"),
159             $page);
160         $dlall = Button(array( //'page' => $page,
161                 'action' => $this->getName(),
162                 'format' => 'backup',
163                 'download' => true),
164             _("Download all revisions for backup"),
165             $page);
166
167         $h2 = HTML::h2(fmt("Preview: Page dump of %s",
168             WikiLink($page, 'auto')));
169         global $WikiTheme;
170         if (!$Sep = $WikiTheme->getButtonSeparator())
171             $Sep = " ";
172
173         if ($format == 'forsvn') {
174             $desc = _("(formatted for PhpWiki developers as pgsrc template, not for backing up)");
175             $altpreviewbuttons = HTML(
176                 Button(array('action' => $this->getName()),
177                     _("Preview as normal format"),
178                     $page),
179                 $Sep,
180                 Button(array(
181                         'action' => $this->getName(),
182                         'format' => 'backup'),
183                     _("Preview as backup format"),
184                     $page));
185         } elseif ($format == 'backup') {
186             $desc = _("(formatted for backing up: all revisions)"); // all revisions
187             $altpreviewbuttons = HTML(
188                 Button(array('action' => $this->getName(),
189                         'format' => 'forsvn'),
190                     _("Preview as developer format"),
191                     $page),
192                 $Sep,
193                 Button(array(
194                         'action' => $this->getName(),
195                         'format' => ''),
196                     _("Preview as normal format"),
197                     $page));
198         } else {
199             $desc = _("(normal formatting: latest revision only)");
200             $altpreviewbuttons = HTML(
201                 Button(array('action' => $this->getName(),
202                         'format' => 'forsvn'),
203                     _("Preview as developer format"),
204                     $page),
205                 $Sep,
206                 Button(array(
207                         'action' => $this->getName(),
208                         'format' => 'backup'),
209                     _("Preview as backup format"),
210                     $page));
211         }
212         $warning = HTML(
213             _("Please use one of the downloadable versions rather than copying and pasting from the above preview.")
214                 . " " .
215                 _("The wordwrap of the preview doesn't take nested markup or list indentation into consideration!")
216                 . " ",
217             HTML::em(
218                 _("PhpWiki developers should manually inspect the downloaded file for nested markup before rewrapping with emacs and checking into Subversion.")
219             )
220         );
221
222         return HTML($h2, HTML::em($desc),
223             HTML::pre($mailified),
224             $altpreviewbuttons,
225             HTML::div(array('class' => 'error'),
226                 HTML::strong(_("Warning:")),
227                 " ", $warning),
228             $dl, $Sep, $dlall, $Sep, $dlsvn
229         );
230     }
231
232     // function handle_plugin_args_cruft(&$argstr, &$args) {
233     // }
234
235     function generateMessageId($mailified)
236     {
237         $array = explode("\n", $mailified);
238         // Extract lastmodifed from mailified document for Content-Id
239         // and/or Message-Id header, NOT from DB (page could have been
240         // edited by someone else since we started).
241         $m1 = preg_grep("/^\s+lastmodified\=(.*);/", $array);
242         $m1 = array_values($m1); //reset resulting keys
243         unset($array);
244         $m2 = preg_split("/(^\s+lastmodified\=)|(;)/", $m1[0], 2,
245             PREG_SPLIT_NO_EMPTY);
246
247         // insert message id into actual message when appropriate, NOT
248         // into http header should be part of fixup_headers, in the
249         // format:
250         // <abbrphpwikiversion.mtimeepochTZ%InterWikiLinktothispage@hostname>
251         // Hopefully this provides a unique enough identifier without
252         // using md5. Even though this particular wiki may not
253         // actually be part of InterWiki, including this info provides
254         // the wiki name and name of the page which is being
255         // represented as a text message.
256         $this->MessageId = implode('', explode('.', PHPWIKI_VERSION))
257             . "-" . $m2[0] . date("O")
258             //. "-". rawurlencode(WIKI_NAME.":" . $request->getURLtoSelf())
259             . "-" . rawurlencode(WIKI_NAME . ":" . $this->pagename)
260             . "@" . rawurlencode(SERVER_NAME);
261     }
262
263     function fixup_headers(&$mailified)
264     {
265         $return = explode("\n", $mailified);
266
267         // Leave message intact for backing up, just add Message-Id header before transmitting.
268         $item_to_insert = "Message-Id: <" . $this->MessageId . ">";
269         $insert_into_key_position = 2;
270         $returnval_ignored = array_splice($return,
271             $insert_into_key_position,
272             0, $item_to_insert);
273
274         $mailified = implode("\n", array_values($return));
275     }
276
277     function fixup_headers_forsvn(&$mailified)
278     {
279         $array = explode("\n", $mailified);
280
281         // Massage headers to prepare for developer checkin to Subversion.
282         /*
283             Strip out all this junk:
284             author=MeMe;
285             version=74;
286             lastmodified=1041561552;
287             author_id=127.0.0.1;
288             hits=146;
289         */
290         $killme = array("author", "version", "lastmodified",
291             "author_id", "hits", "owner", "acl");
292         // UltraNasty, fixme:
293         foreach ($killme as $pattern) {
294             $array = preg_replace("/^\s\s$pattern\=.*;/",
295                 /*$replacement =*/
296                 "zzzjunk", $array);
297         }
298         // remove deleted values from array
299         for ($i = 0; $i < count($array); $i++) {
300             if (trim($array[$i]) != "zzzjunk") { //nasty, fixme
301                 //trigger_error("'$array[$i]'");//debugging
302                 $return[] = $array[$i];
303             }
304         }
305
306         $mailified = implode("\n", $return);
307     }
308 }
309
310 // Local Variables:
311 // mode: php
312 // tab-width: 8
313 // c-basic-offset: 4
314 // c-hanging-comment-ender-p: nil
315 // indent-tabs-mode: nil
316 // End: