MediaWiki master
renderDump.php
Go to the documentation of this file.
1<?php
35
36// @codeCoverageIgnoreStart
37require_once __DIR__ . '/Maintenance.php';
38// @codeCoverageIgnoreEnd
39
46class DumpRenderer extends Maintenance {
47
49 private $count = 0;
50 private string $outputDirectory;
51 private float $startTime;
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
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;
156require_once RUN_MAINTENANCE_IF_MAIN;
157// @codeCoverageIgnoreEnd
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
array $params
The job parameters.
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)
Set options of the Parser.
Represents an authority that has all permissions.
internal since 1.36
Definition User.php:93
Represents a revision, log entry or upload during the import process.
getContent( $role=SlotRecord::MAIN)
$source
$maintClass