4 * Copyright (C) 2003 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
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.
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.
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.
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.
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.
33 * Multiple revisions in one file handled by format=backup
35 * TODO: What about comments/summary field? quoted-printable?
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
43 * <<PageDump page=HomePage>>
44 * Dynamic form (put both on the page):
46 * <?plugin-form PageDump?>
47 * Typical usage: as actionbar button
50 class WikiPlugin_PageDump
60 function getDescription()
62 return _("View a single page dump online.");
65 function getDefaultArguments()
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
76 function run($dbi, $argstr, &$request, $basepage)
78 extract($this->getArgs($argstr, $request));
84 if (!$dbi->isWikiPage($page))
85 return fmt("Page %s not found.",
86 WikiLink($page, 'unknown'));
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"),
94 $p = $dbi->getPage($page);
95 include_once 'lib/loadsave.php';
96 $mailified = MailifyPage($p, ($format == 'backup') ? 99 : 1);
98 // fixup_headers massages the page dump headers depending on
99 // the 'format' argument, 'normal'(default) or 'forsvn'.
101 // Normal: add unique Message-Id, don't
102 // strip any fields from Content-Type.
104 // ForCVS: strip attributes from
105 // Content-Type field: "author", "version", "lastmodified",
106 // "author_id", "hits".
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);
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=\""
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
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));
141 // Here comes our prepared mime file
143 exit(); // noreturn! php exits.
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);
149 $dlsvn = Button(array( //'page' => $page,
150 'action' => $this->getName(),
151 'format' => 'forsvn',
153 _("Download for Subversion"),
155 $dl = Button(array( //'page' => $page,
156 'action' => $this->getName(),
158 _("Download for backup"),
160 $dlall = Button(array( //'page' => $page,
161 'action' => $this->getName(),
162 'format' => 'backup',
164 _("Download all revisions for backup"),
167 $h2 = HTML::h2(fmt("Preview: Page dump of %s",
168 WikiLink($page, 'auto')));
170 if (!$Sep = $WikiTheme->getButtonSeparator())
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"),
181 'action' => $this->getName(),
182 'format' => 'backup'),
183 _("Preview as backup format"),
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"),
194 'action' => $this->getName(),
196 _("Preview as normal format"),
199 $desc = _("(normal formatting: latest revision only)");
200 $altpreviewbuttons = HTML(
201 Button(array('action' => $this->getName(),
202 'format' => 'forsvn'),
203 _("Preview as developer format"),
207 'action' => $this->getName(),
208 'format' => 'backup'),
209 _("Preview as backup format"),
213 _("Please use one of the downloadable versions rather than copying and pasting from the above preview.")
215 _("The wordwrap of the preview doesn't take nested markup or list indentation into consideration!")
218 _("PhpWiki developers should manually inspect the downloaded file for nested markup before rewrapping with emacs and checking into Subversion.")
222 return HTML($h2, HTML::em($desc),
223 HTML::pre($mailified),
225 HTML::div(array('class' => 'error'),
226 HTML::strong(_("Warning:")),
228 $dl, $Sep, $dlall, $Sep, $dlsvn
232 // function handle_plugin_args_cruft(&$argstr, &$args) {
235 function generateMessageId($mailified)
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
244 $m2 = preg_split("/(^\s+lastmodified\=)|(;)/", $m1[0], 2,
245 PREG_SPLIT_NO_EMPTY);
247 // insert message id into actual message when appropriate, NOT
248 // into http header should be part of fixup_headers, in the
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);
263 function fixup_headers(&$mailified)
265 $return = explode("\n", $mailified);
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,
274 $mailified = implode("\n", array_values($return));
277 function fixup_headers_forsvn(&$mailified)
279 $array = explode("\n", $mailified);
281 // Massage headers to prepare for developer checkin to Subversion.
283 Strip out all this junk:
286 lastmodified=1041561552;
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\=.*;/",
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];
306 $mailified = implode("\n", $return);
314 // c-hanging-comment-ender-p: nil
315 // indent-tabs-mode: nil