Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DumpRenderer
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
12
 handleRevision
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
6
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 Brooke Vibber <bvibber@wikimedia.org>
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
31use MediaWiki\Permissions\UltimateAuthority;
32use MediaWiki\Revision\MutableRevisionRecord;
33use MediaWiki\User\User;
34
35require_once __DIR__ . '/Maintenance.php';
36
37/**
38 * Maintenance script that takes page text out of an XML dump file
39 * and render basic HTML out to files.
40 *
41 * @ingroup Maintenance
42 */
43class DumpRenderer extends Maintenance {
44
45    private $count = 0;
46    private $outputDirectory, $startTime;
47    /** @var string */
48    private $prefix;
49
50    public function __construct() {
51        parent::__construct();
52        $this->addDescription(
53            'Take page text out of an XML dump file and render basic HTML out to files' );
54        $this->addOption( 'output-dir', 'The directory to output the HTML files to', true, true );
55        $this->addOption( 'prefix', 'Prefix for the rendered files (defaults to wiki)', false, true );
56        $this->addOption( 'parser', 'Use an alternative parser class', false, true );
57    }
58
59    public function execute() {
60        $this->outputDirectory = $this->getOption( 'output-dir' );
61        $this->prefix = $this->getOption( 'prefix', 'wiki' );
62        $this->startTime = microtime( true );
63
64        if ( $this->hasOption( 'parser' ) ) {
65            $this->prefix .= '-' . $this->getOption( 'parser' );
66            // T236809: We'll need to provide an alternate ParserFactory
67            // service to make this work.
68            $this->fatalError( 'Parser class configuration temporarily disabled.' );
69        }
70
71        $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
72
73        $source = new ImportStreamSource( $this->getStdin() );
74        $importer = $this->getServiceContainer()
75            ->getWikiImporterFactory()
76            ->getWikiImporter( $source, new UltimateAuthority( $user ) );
77
78        $importer->setRevisionCallback(
79            [ $this, 'handleRevision' ] );
80        $importer->setNoticeCallback( static function ( $msg, $params ) {
81            echo wfMessage( $msg, $params )->text() . "\n";
82        } );
83
84        $importer->doImport();
85
86        $delta = microtime( true ) - $this->startTime;
87        $this->error( "Rendered {$this->count} pages in " . round( $delta, 2 ) . " seconds " );
88        if ( $delta > 0 ) {
89            $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
90        }
91        $this->error( "\n" );
92    }
93
94    /**
95     * Callback function for each revision, turn into HTML and save
96     * @param WikiRevision $rev
97     */
98    public function handleRevision( WikiRevision $rev ) {
99        $title = $rev->getTitle();
100        if ( !$title ) {
101            $this->error( "Got bogus revision with null title!" );
102
103            return;
104        }
105        $display = $title->getPrefixedText();
106
107        $this->count++;
108
109        $sanitized = rawurlencode( $display );
110        $filename = sprintf( "%s/%s-%07d-%s.html",
111            $this->outputDirectory,
112            $this->prefix,
113            $this->count,
114            $sanitized );
115        $this->output( sprintf( "%s\t%s\n", $filename, $display ) );
116
117        $user = new User();
118        $options = ParserOptions::newFromUser( $user );
119
120        $content = $rev->getContent();
121        $contentRenderer = $this->getServiceContainer()->getContentRenderer();
122        // ContentRenderer expects a RevisionRecord, and all we have is a
123        // WikiRevision from the dump.  Make a fake MutableRevisionRecord to
124        // satisfy it -- the only thing ::getParserOutput actually needs is
125        // the revision ID and revision timestamp.
126        $mutableRev = new MutableRevisionRecord( $rev->getTitle() );
127        $mutableRev->setId( $rev->getID() );
128        $mutableRev->setTimestamp( $rev->getTimestamp() );
129        $output = $contentRenderer->getParserOutput(
130            $content, $title, $mutableRev, $options
131        );
132
133        file_put_contents( $filename,
134            "<!DOCTYPE html>\n" .
135            "<html lang=\"en\" dir=\"ltr\">\n" .
136            "<head>\n" .
137            "<meta charset=\"UTF-8\" />\n" .
138            "<title>" . htmlspecialchars( $display, ENT_COMPAT ) . "</title>\n" .
139            "</head>\n" .
140            "<body>\n" .
141            $output->getText() .
142            "</body>\n" .
143            "</html>" );
144    }
145}
146
147$maintClass = DumpRenderer::class;
148require_once RUN_MAINTENANCE_IF_MAIN;