MediaWiki master
renderDump.php
Go to the documentation of this file.
1<?php
16
23use MediaWiki\User\User;
24use Wikimedia\Timestamp\ConvertibleTimestamp;
25
26// @codeCoverageIgnoreStart
27require_once __DIR__ . '/Maintenance.php';
28// @codeCoverageIgnoreEnd
29
37
39 private $count = 0;
40 private string $outputDirectory;
41 private float $startTime;
43 private $prefix;
44
45 public function __construct() {
46 parent::__construct();
47 $this->addDescription(
48 'Take page text out of an XML dump file and render basic HTML out to files' );
49 $this->addOption( 'output-dir', 'The directory to output the HTML files to', true, true );
50 $this->addOption( 'prefix', 'Prefix for the rendered files (defaults to wiki)', false, true );
51 $this->addOption( 'parser', 'Use an alternative parser class', false, true );
52 }
53
54 public function execute() {
55 $this->outputDirectory = $this->getOption( 'output-dir' );
56 $this->prefix = $this->getOption( 'prefix', 'wiki' );
57 $this->startTime = ConvertibleTimestamp::hrtime();
58
59 if ( $this->hasOption( 'parser' ) ) {
60 $this->prefix .= '-' . $this->getOption( 'parser' );
61 // T236809: We'll need to provide an alternate ParserFactory
62 // service to make this work.
63 $this->fatalError( 'Parser class configuration temporarily disabled.' );
64 }
65
66 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
67
68 $source = new ImportStreamSource( $this->getStdin() );
69 $importer = $this->getServiceContainer()
70 ->getWikiImporterFactory()
71 ->getWikiImporter( $source, new UltimateAuthority( $user ) );
72
73 $importer->setRevisionCallback(
74 $this->handleRevision( ... ) );
75 $importer->setNoticeCallback( static function ( $msg, $params ) {
76 echo wfMessage( $msg, $params )->text() . "\n";
77 } );
78
79 $importer->doImport();
80
81 $delta = ( ConvertibleTimestamp::hrtime() - $this->startTime ) / 1e9;
82 $this->error( "Rendered {$this->count} pages in " . round( $delta, 2 ) . " seconds " );
83 if ( $delta > 0 ) {
84 $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
85 }
86 $this->error( "\n" );
87 }
88
92 public function handleRevision( WikiRevision $rev ) {
93 $title = $rev->getTitle();
94 if ( !$title ) {
95 $this->error( "Got bogus revision with null title!" );
96
97 return;
98 }
99 $display = $title->getPrefixedText();
100
101 $this->count++;
102
103 $sanitized = rawurlencode( $display );
104 $filename = sprintf( "%s/%s-%07d-%s.html",
105 $this->outputDirectory,
106 $this->prefix,
107 $this->count,
108 $sanitized );
109 $this->output( sprintf( "%s\t%s\n", $filename, $display ) );
110
111 $user = new User();
112 $options = ParserOptions::newFromUser( $user );
113
114 $content = $rev->getContent();
115 $contentRenderer = $this->getServiceContainer()->getContentRenderer();
116 // ContentRenderer expects a RevisionRecord, and all we have is a
117 // WikiRevision from the dump. Make a fake MutableRevisionRecord to
118 // satisfy it -- the only thing ::getParserOutput actually needs is
119 // the revision ID and revision timestamp.
120 $mutableRev = new MutableRevisionRecord( $rev->getTitle() );
121 $mutableRev->setId( $rev->getID() );
122 $mutableRev->setTimestamp( $rev->getTimestamp() );
123 $output = $contentRenderer->getParserOutput(
124 $content, $title, $mutableRev, $options
125 );
126
127 file_put_contents( $filename,
128 "<!DOCTYPE html>\n" .
129 "<html lang=\"en\" dir=\"ltr\">\n" .
130 "<head>\n" .
131 "<meta charset=\"UTF-8\" />\n" .
132 "<meta name=\"color-scheme\" content=\"light dark\">" .
133 "<title>" . htmlspecialchars( $display, ENT_COMPAT ) . "</title>\n" .
134 "</head>\n" .
135 "<body>\n" .
136 // TODO T371004 move runOutputPipeline out of $parserOutput
137 $output->runOutputPipeline( $options, [] )->getContentHolderText() .
138 "</body>\n" .
139 "</html>" );
140 }
141}
142
143// @codeCoverageIgnoreStart
144$maintClass = DumpRenderer::class;
145require_once RUN_MAINTENANCE_IF_MAIN;
146// @codeCoverageIgnoreEnd
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
$maintClass
Maintenance script that takes page text out of an XML dump file and render basic HTML out to files.
handleRevision(WikiRevision $rev)
Callback function for each revision, turn into HTML and save.
Imports a XML dump from a file (either from file upload, files on disk, or HTTP).
Represents a revision, log entry or upload during the import process.
getContent( $role=SlotRecord::MAIN)
Set options of the Parser.
static newFromUser( $user)
Get a ParserOptions object from a given user.
Represents an authority that has all permissions.
User class for the MediaWiki software.
Definition User.php:129
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:808
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition User.php:164
$source