Debian Codegraph @

maintenance/renderDump.php

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