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