File: PageDump.php

package info (click to toggle)
phpwiki 1.3.12p3-5etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 16,956 kB
  • ctags: 21,608
  • sloc: php: 82,335; xml: 3,840; sh: 1,522; sql: 1,198; perl: 625; makefile: 562; awk: 28
file content (382 lines) | stat: -rw-r--r-- 15,803 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php // -*-php-*-
rcs_id('$Id: PageDump.php,v 1.18 2004/10/14 19:19:34 rurban Exp $');
/**
 * PhpWikiPlugin for PhpWiki developers to generate single page dumps
 * for checking into cvs, or for users or the admin to produce a
 * downloadable page dump of a single page.
 * 
 * This plugin will also be useful to (semi-)automatically sync pages
 * directly between two wikis. First the LoadFile function of
 * PhpWikiAdministration needs to be updated to handle URLs again, and
 * add loading capability from InterWiki addresses.

 * Multiple revisions in one file handled by format=backup
 *
 * TODO: What about comments/summary field? quoted-printable?
 *
 * Usage:
 *  Direct URL access:
 *   http://...phpwiki/PageDump?page=HomePage?format=forcvs
 *   http://...phpwiki/index.php?PageDump&page=HomePage
 *   http://...phpwiki/index.php?PageDump&page=HomePage&download=1
 *  Static:
 *   <?plugin PageDump page=HomePage?>
 *  Dynamic form (put both on the page):
 *   <?plugin PageDump?>
 *   <?plugin-form PageDump?>
 *  Typical usage: as actionbar button
 */

class WikiPlugin_PageDump
extends WikiPlugin
{
    var $MessageId;

    function getName() {
        return _("PageDump");
    }
    function getDescription() {
        return _("View a single page dump online.");
    }

    function getVersion() {
        return preg_replace("/[Revision: $]/", '',
                            "\$Revision: 1.18 $");
    }

    function getDefaultArguments() {
        return array('s'    => false,
                     'page' => '[pagename]',
                     //'encoding' => 'binary', // 'binary', 'quoted-printable'
                     'format' => false, // 'normal', 'forcvs', 'backup'
                     // display within WikiPage or give a downloadable
                     // raw pgsrc?
                     'download' => false);
    }

    function run($dbi, $argstr, &$request, $basepage) {
        extract($this->getArgs($argstr, $request));
        // allow plugin-form
        if (!empty($s))
            $page = $s;
        if (!$page)
            return '';
        if (! $dbi->isWikiPage($page) )
            return fmt("Page %s not found.",
                       WikiLink($page, 'unknown'));

        $p = $dbi->getPage($page);
        include_once("lib/loadsave.php");
        $mailified = MailifyPage($p, ($format == 'backup') ? 99 : 1);

        // fixup_headers massages the page dump headers depending on
        // the 'format' argument, 'normal'(default) or 'forcvs'.
        //
        // Normal: Don't add X-Rcs-Id, add unique Message-Id, don't
        // strip any fields from Content-Type.
        //
        // ForCVS: Add empty X-Rcs-Id, strip attributes from
        // Content-Type field: "author", "version", "lastmodified",
        // "author_id", "hits".

        $this->pagename = $page;
        $this->generateMessageId($mailified);
        if ($format == 'forcvs')
            $this->fixup_headers_forcvs($mailified);
        else // backup or normal
            $this->fixup_headers($mailified);

        if ($download) {
            // TODO: we need a way to hook into the generated headers, to override 
            // Content-Type, Set-Cookie, Cache-control, ...
            $request->discardOutput(); // Hijack the http request from PhpWiki.
            ob_end_clean(); // clean up after hijacking $request
            //ob_end_flush(); //debugging
            Header("Content-disposition: attachment; filename=\""
                   . FilenameForPage($page) . "\"");
            // Read charset from generated page itself.
            // Inconsequential at the moment, since loadsave.php
            // always generates headers
            $charset = $p->get('charset');
            if (!$charset) $charset = $GLOBALS['charset'];
            // We generate 3 Content-Type headers! first in loadsave,
            // then here and the mimified string $mailified also has it!
            Header("Content-Type: text/plain; name=\""
                   . FilenameForPage($page) . "\"; charset=\"" . $charset
                   . "\"");
            $request->checkValidators();
            // let $request provide last modifed & etag
            Header("Content-Id: <" . $this->MessageId . ">");
            // be nice to http keepalive~s
            // FIXME: he length is wrong BTW. must strip the header.
            Header("Content-Length: " . strlen($mailified)); 

            // Here comes our prepared mime file
            echo $mailified;
            exit; // noreturn! php exits.
            return;
        }
        // We are displaing inline preview in a WikiPage, so wrap the
        // text if it is too long--unless quoted-printable (TODO).
        $mailified = safe_wordwrap($mailified, 70);

        $dlcvs = Button(array(//'page' => $page,
                              'action' => $this->getName(),
                              'format'=> 'forcvs',
                              'download'=> true),
                        _("Download for CVS"),
                        $page);
        $dl = Button(array(//'page' => $page,
                           'action' => $this->getName(),
                           'download'=> true),
                     _("Download for backup"),
                     $page);
        $dlall = Button(array(//'page' => $page,
                           'action' => $this->getName(),
                           'format'=> 'backup',
                           'download'=> true),
                     _("Download all revisions for backup"),
                     $page);

        $h2 = HTML::h2(fmt("Preview: Page dump of %s",
                           WikiLink($page, 'auto')));
        global $WikiTheme;
        if (!$Sep = $WikiTheme->getButtonSeparator())
            $Sep = " ";

        if ($format == 'forcvs') {
            $desc = _("(formatted for PhpWiki developers as pgsrc template, not for backing up)");
            $altpreviewbuttons = HTML(
                                      Button(array('action' => $this->getName()),
                                             _("Preview as normal format"),
                                             $page),
                                      $Sep,
                                      Button(array(
                                                   'action' => $this->getName(),
                                                   'format'=> 'backup'),
                                             _("Preview as backup format"),
                                             $page));
        }
        elseif ($format == 'backup') {
            $desc = _("(formatted for backing up: all revisions)"); // all revisions
            $altpreviewbuttons = HTML(
                                      Button(array('action' => $this->getName(),
                                                   'format'=> 'forcvs'),
                                             _("Preview as developer format"),
                                             $page),
                                      $Sep,
                                      Button(array(
                                                   'action' => $this->getName(),
                                                   'format'=> ''),
                                             _("Preview as normal format"),
                                             $page));
        } else {
            $desc = _("(normal formatting: latest revision only)");
            $altpreviewbuttons = HTML(
                                      Button(array('action' => $this->getName(),
                                                   'format'=> 'forcvs'),
                                             _("Preview as developer format"),
                                             $page),
                                      $Sep,
                                      Button(array(
                                                   'action' => $this->getName(),
                                                   'format'=> 'backup'),
                                             _("Preview as backup format"),
                                             $page));
        }
        $warning = HTML(
_("Please use one of the downloadable versions rather than copying and pasting from the above preview.")
. " " .
_("The wordwrap of the preview doesn't take nested markup or list indentation into consideration!")
. " ",
HTML::em(
_("PhpWiki developers should manually inspect the downloaded file for nested markup before rewrapping with emacs and checking into CVS.")
         )
                        );

        return HTML($h2, HTML::em($desc),
                    HTML::pre($mailified),
                    $altpreviewbuttons,
                    HTML::div(array('class' => 'errors'),
                              HTML::strong(_("Warning:")),
                              " ", $warning),
                    $dl, $Sep, $dlall, $Sep, $dlcvs
                    );
    }

    // function handle_plugin_args_cruft(&$argstr, &$args) {
    // }

    function generateMessageId($mailified) {
        $array = explode("\n", $mailified);
        // Extract lastmodifed from mailified document for Content-Id
        // and/or Message-Id header, NOT from DB (page could have been
        // edited by someone else since we started).
        $m1 = preg_grep("/^\s+lastmodified\=(.*);/", $array);
        $m1 = array_values($m1); //reset resulting keys
        unset($array);
        $m2 = preg_split("/(^\s+lastmodified\=)|(;)/", $m1[0], 2,
                         PREG_SPLIT_NO_EMPTY);

        // insert message id into actual message when appropriate, NOT
        // into http header should be part of fixup_headers, in the
        // format:
        // <abbrphpwikiversion.mtimeepochTZ%InterWikiLinktothispage@hostname>
        // Hopefully this provides a unique enough identifier without
        // using md5. Even though this particular wiki may not
        // actually be part of InterWiki, including this info provides
        // the wiki name and name of the page which is being
        // represented as a text message.
        $this->MessageId = implode('', explode('.', PHPWIKI_VERSION))
            . "-" . $m2[0] . date("O")
            //. "-". rawurlencode(WIKI_NAME.":" . $request->getURLtoSelf())
            . "-". rawurlencode(WIKI_NAME.":" . $this->pagename)
            . "@". rawurlencode(SERVER_NAME);
    }

    function fixup_headers(&$mailified) {
        $return = explode("\n", $mailified);

        // Leave message intact for backing up, just add Message-Id header before transmitting.
        $item_to_insert = "Message-Id: <" . $this->MessageId .">";
        $insert_into_key_position = 2;
        $returnval_ignored = array_splice($return,
                                          $insert_into_key_position,
                                          0, $item_to_insert);

        $mailified = implode("\n", array_values($return));
    }

    function fixup_headers_forcvs(&$mailified) {
        $array = explode("\n", $mailified);

        // Massage headers to prepare for developer checkin to CVS.
        $item_to_insert = "X-Rcs-Id: \$Id\$";
        $insert_into_key_position = 2;
        $returnval_ignored = array_splice($array,
                                          $insert_into_key_position,
                                          0, $item_to_insert);

        $item_to_insert = "  pgsrc_version=\"2 \$Revision\$\";";
        $insert_into_key_position = 5;
        $returnval_ignored = array_splice($array,
                                          $insert_into_key_position,
                                          0, $item_to_insert);
        /*
            Strip out all this junk:
            author=MeMe;
            version=74;
            lastmodified=1041561552;
            author_id=127.0.0.1;
            hits=146;
        */
        $killme = array("author", "version", "lastmodified",
                        "author_id", "hits", "owner", "acl");
        // UltraNasty, fixme:
        foreach ($killme as $pattern) {
            $array = preg_replace("/^\s\s$pattern\=.*;/",
                                  /*$replacement =*/"zzzjunk", $array);
        }
        // remove deleted values from array
        for ($i = 0; $i < count($array); $i++ ) {
            if(trim($array[$i]) != "zzzjunk") { //nasty, fixme
            //trigger_error("'$array[$i]'");//debugging
                $return[] = $array[$i];
            }
        }

        $mailified = implode("\n", $return);
    }
};

// $Log: PageDump.php,v $
// Revision 1.18  2004/10/14 19:19:34  rurban
// loadsave: check if the dumped file will be accessible from outside.
// and some other minor fixes. (cvsclient native not yet ready)
//
// Revision 1.17  2004/09/16 07:49:01  rurban
// use the page charset instead if the global one on download
//   (need to clarify header order, since we print the same header type 3 times!)
// wordwrap workaround (security concern)
//
// Revision 1.16  2004/07/01 06:31:23  rurban
// doc upcase only
//
// Revision 1.15  2004/06/29 10:09:06  rurban
// better desc
//
// Revision 1.14  2004/06/29 10:07:40  rurban
// added dump of all revisions by format=backup (screen and download)
//
// Revision 1.13  2004/06/17 10:39:18  rurban
// fix reverse translation of possible actionpage
//
// Revision 1.12  2004/06/16 13:32:43  rurban
// fix urlencoding of pagename in PageDump buttons
//
// Revision 1.11  2004/06/14 11:31:39  rurban
// renamed global $Theme to $WikiTheme (gforge nameclash)
// inherit PageList default options from PageList
//   default sortby=pagename
// use options in PageList_Selectable (limit, sortby, ...)
// added action revert, with button at action=diff
// added option regex to WikiAdminSearchReplace
//
// Revision 1.10  2004/06/07 22:28:05  rurban
// add acl field to mimified dump
//
// Revision 1.9  2004/06/07 19:50:41  rurban
// add owner field to mimified dump
//
// Revision 1.8  2004/05/25 12:43:29  rurban
// ViewSource link, better actionpage usage
//
// Revision 1.7  2004/05/04 17:21:06  rurban
// revert previous patch
//
// Revision 1.6  2004/05/03 20:44:55  rurban
// fixed gettext strings
// new SqlResult plugin
// _WikiTranslation: fixed init_locale
//
// Revision 1.5  2004/05/03 17:42:44  rurban
// fix cvs tags: "$tag$" => "$tag: $"
//
// Revision 1.4  2004/04/18 01:11:52  rurban
// more numeric pagename fixes.
// fixed action=upload with merge conflict warnings.
// charset changed from constant to global (dynamic utf-8 switching)
//
// Revision 1.3  2004/02/17 12:11:36  rurban
// added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
//
// Revision 1.2  2003/12/12 01:08:30  carstenklapp
// QuickFix for invalid Message-Id header format.
//
// Revision 1.1  2003/12/12 00:52:55  carstenklapp
// New feature: Plugin to download page dumps of individual pages. In the
// future this could be used as a rudimentary way to sync pages between
// wikis.
// Internal changes: enhanced and renamed from the experimental
// _MailifyPage plugin.
//
// Revision 1.3  2003/11/16 00:11:25  carstenklapp
// Fixed previous Log comment interfering with PHP (sorry).
// Improved error handling.
//
// Revision 1.2  2003/11/15 23:37:51  carstenklapp
// Enhanced plugin to allow invocation with \<\?plugin-form PageDump\?\>.
//
// Revision 1.1  2003/02/20 18:03:04  carstenklapp
// New experimental WikiPlugin for internal use only by PhpWiki developers.
//

// For emacs users
// Local Variables:
// mode: php
// tab-width: 8
// c-basic-offset: 4
// c-hanging-comment-ender-p: nil
// indent-tabs-mode: nil
// End:
?>