Merge "CSSMin: Clean up $remote trailing slash fix"
[lhc/web/wiklou.git] / maintenance / renderDump.php
1 <?php
2 /**
3 * Take page text out of an XML dump file and render basic HTML out to files.
4 * This is *NOT* suitable for publishing or offline use; it's intended for
5 * running comparative tests of parsing behavior using real-world data.
6 *
7 * Templates etc are pulled from the local wiki database, not from the dump.
8 *
9 * Copyright (C) 2006 Brion Vibber <brion@pobox.com>
10 * http://www.mediawiki.org/
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @file
28 * @ingroup Maintenance
29 */
30
31 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
32
33 class DumpRenderer extends Maintenance {
34
35 private $count = 0;
36 private $outputDirectory, $startTime;
37
38 public function __construct() {
39 parent::__construct();
40 $this->mDescription = "Take page text out of an XML dump file and render basic HTML out to files";
41 $this->addOption( 'output-dir', 'The directory to output the HTML files to', true, true );
42 $this->addOption( 'prefix', 'Prefix for the rendered files (defaults to wiki)', false, true );
43 $this->addOption( 'parser', 'Use an alternative parser class', false, true );
44 }
45
46 public function execute() {
47 $this->outputDirectory = $this->getOption( 'output-dir' );
48 $this->prefix = $this->getOption( 'prefix', 'wiki' );
49 $this->startTime = wfTime();
50
51 if ( $this->hasOption( 'parser' ) ) {
52 global $wgParserConf;
53 $wgParserConf['class'] = $this->getOption( 'parser' );
54 $this->prefix .= "-{$wgParserConf['class']}";
55 }
56
57 $source = new ImportStreamSource( $this->getStdin() );
58 $importer = new WikiImporter( $source );
59
60 $importer->setRevisionCallback(
61 array( &$this, 'handleRevision' ) );
62
63 $importer->doImport();
64
65 $delta = wfTime() - $this->startTime;
66 $this->error( "Rendered {$this->count} pages in " . round($delta, 2) . " seconds " );
67 if ($delta > 0)
68 $this->error( round($this->count / $delta, 2) . " pages/sec" );
69 $this->error( "\n" );
70 }
71
72 /**
73 * Callback function for each revision, turn into HTML and save
74 * @param $rev Revision
75 */
76 public function handleRevision( $rev ) {
77 global $wgParserConf;
78
79 $title = $rev->getTitle();
80 if ( !$title ) {
81 $this->error( "Got bogus revision with null title!" );
82 return;
83 }
84 $display = $title->getPrefixedText();
85
86 $this->count++;
87
88 $sanitized = rawurlencode( $display );
89 $filename = sprintf( "%s/%s-%07d-%s.html",
90 $this->outputDirectory,
91 $this->prefix,
92 $this->count,
93 $sanitized );
94 $this->output( sprintf( "%s\n", $filename, $display ) );
95
96 $user = new User();
97 $parser = new $wgParserConf['class']();
98 $options = ParserOptions::newFromUser( $user );
99
100 $output = $parser->parse( $rev->getText(), $title, $options );
101
102 file_put_contents( $filename,
103 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" " .
104 "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" .
105 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" .
106 "<head>\n" .
107 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n" .
108 "<title>" . htmlspecialchars( $display ) . "</title>\n" .
109 "</head>\n" .
110 "<body>\n" .
111 $output->getText() .
112 "</body>\n" .
113 "</html>" );
114 }
115 }
116
117 $maintClass = "DumpRenderer";
118 require_once( RUN_MAINTENANCE_IF_MAIN );