Merge "Provide command to adjust phpunit.xml for code coverage"
[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 * https://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 __DIR__ . '/Maintenance.php';
32
33 /**
34 * Maintenance script that takes page text out of an XML dump file
35 * and render basic HTML out to files.
36 *
37 * @ingroup Maintenance
38 */
39 class DumpRenderer extends Maintenance {
40
41 private $count = 0;
42 private $outputDirectory, $startTime;
43 /** @var string */
44 private $prefix;
45
46 public function __construct() {
47 parent::__construct();
48 $this->addDescription(
49 'Take page text out of an XML dump file and render basic HTML out to files' );
50 $this->addOption( 'output-dir', 'The directory to output the HTML files to', true, true );
51 $this->addOption( 'prefix', 'Prefix for the rendered files (defaults to wiki)', false, true );
52 $this->addOption( 'parser', 'Use an alternative parser class', false, true );
53 }
54
55 public function execute() {
56 $this->outputDirectory = $this->getOption( 'output-dir' );
57 $this->prefix = $this->getOption( 'prefix', 'wiki' );
58 $this->startTime = microtime( true );
59
60 if ( $this->hasOption( 'parser' ) ) {
61 global $wgParserConf;
62 $wgParserConf['class'] = $this->getOption( 'parser' );
63 $this->prefix .= "-{$wgParserConf['class']}";
64 }
65
66 $source = new ImportStreamSource( $this->getStdin() );
67 $importer = new WikiImporter( $source, $this->getConfig() );
68
69 $importer->setRevisionCallback(
70 [ $this, 'handleRevision' ] );
71 $importer->setNoticeCallback( function ( $msg, $params ) {
72 echo wfMessage( $msg, $params )->text() . "\n";
73 } );
74
75 $importer->doImport();
76
77 $delta = microtime( true ) - $this->startTime;
78 $this->error( "Rendered {$this->count} pages in " . round( $delta, 2 ) . " seconds " );
79 if ( $delta > 0 ) {
80 $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
81 }
82 $this->error( "\n" );
83 }
84
85 /**
86 * Callback function for each revision, turn into HTML and save
87 * @param Revision $rev
88 */
89 public function handleRevision( $rev ) {
90 $title = $rev->getTitle();
91 if ( !$title ) {
92 $this->error( "Got bogus revision with null title!" );
93
94 return;
95 }
96 $display = $title->getPrefixedText();
97
98 $this->count++;
99
100 $sanitized = rawurlencode( $display );
101 $filename = sprintf( "%s/%s-%07d-%s.html",
102 $this->outputDirectory,
103 $this->prefix,
104 $this->count,
105 $sanitized );
106 $this->output( sprintf( "%s\n", $filename, $display ) );
107
108 $user = new User();
109 $options = ParserOptions::newFromUser( $user );
110
111 $content = $rev->getContent();
112 $output = $content->getParserOutput( $title, null, $options );
113
114 file_put_contents( $filename,
115 "<!DOCTYPE html>\n" .
116 "<html lang=\"en\" dir=\"ltr\">\n" .
117 "<head>\n" .
118 "<meta charset=\"UTF-8\" />\n" .
119 "<title>" . htmlspecialchars( $display ) . "</title>\n" .
120 "</head>\n" .
121 "<body>\n" .
122 $output->getText() .
123 "</body>\n" .
124 "</html>" );
125 }
126 }
127
128 $maintClass = DumpRenderer::class;
129 require_once RUN_MAINTENANCE_IF_MAIN;