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