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
55 function getDescription()
57 return _("View a single page dump online.");
60 function getDefaultArguments()
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
71 function run($dbi, $argstr, &$request, $basepage)
73 extract($this->getArgs($argstr, $request));
81 if (!$dbi->isWikiPage($page)) {
82 return $this->error(sprintf(_("Page ā%sā does not exist."), $page));
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"),
91 $p = $dbi->getPage($page);
92 include_once 'lib/loadsave.php';
93 $mailified = MailifyPage($p, ($format == 'backup') ? 99 : 1);
95 // fixup_headers massages the page dump headers depending on
96 // the 'format' argument, 'normal'(default) or 'forsvn'.
98 // Normal: add unique Message-Id, don't
99 // strip any fields from Content-Type.
101 // ForCVS: strip attributes from
102 // Content-Type field: "author", "version", "lastmodified",
103 // "author_id", "hits".
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);
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=\""
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'
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));
133 // Here comes our prepared mime file
135 exit(); // noreturn! php exits.
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);
141 $dlsvn = Button(array( //'page' => $page,
142 'action' => $this->getName(),
143 'format' => 'forsvn',
145 _("Download for Subversion"),
147 $dl = Button(array( //'page' => $page,
148 'action' => $this->getName(),
150 _("Download for backup"),
152 $dlall = Button(array( //'page' => $page,
153 'action' => $this->getName(),
154 'format' => 'backup',
156 _("Download all revisions for backup"),
159 $h2 = HTML::h2(fmt("Preview: Page dump of %s",
160 WikiLink($page, 'auto')));
162 if (!$Sep = $WikiTheme->getButtonSeparator())
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"),
173 'action' => $this->getName(),
174 'format' => 'backup'),
175 _("Preview as backup format"),
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"),
186 'action' => $this->getName(),
188 _("Preview as normal format"),
191 $desc = _("(normal formatting: latest revision only)");
192 $altpreviewbuttons = HTML(
193 Button(array('action' => $this->getName(),
194 'format' => 'forsvn'),
195 _("Preview as developer format"),
199 'action' => $this->getName(),
200 'format' => 'backup'),
201 _("Preview as backup format"),
205 _("Please use one of the downloadable versions rather than copying and pasting from the above preview.")
207 _("The wordwrap of the preview doesn't take nested markup or list indentation into consideration!")
210 _("PhpWiki developers should manually inspect the downloaded file for nested markup before rewrapping with emacs and checking into Subversion.")
214 return HTML($h2, HTML::em($desc),
215 HTML::pre($mailified),
217 HTML::div(array('class' => 'error'),
218 HTML::strong(_("Warning:")),
220 $dl, $Sep, $dlall, $Sep, $dlsvn
224 // function handle_plugin_args_cruft(&$argstr, &$args) {
227 function generateMessageId($mailified)
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
236 $m2 = preg_split("/(^\s+lastmodified\=)|(;)/", $m1[0], 2,
237 PREG_SPLIT_NO_EMPTY);
239 // insert message id into actual message when appropriate, NOT
240 // into http header should be part of fixup_headers, in the
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);
255 function fixup_headers(&$mailified)
257 $return = explode("\n", $mailified);
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,
266 $mailified = implode("\n", array_values($return));
269 function fixup_headers_forsvn(&$mailified)
271 $array = explode("\n", $mailified);
273 // Massage headers to prepare for developer checkin to Subversion.
275 Strip out all this junk:
278 lastmodified=1041561552;
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\=.*;/",
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];
298 $mailified = implode("\n", $return);
306 // c-hanging-comment-ender-p: nil
307 // indent-tabs-mode: nil