MediaWiki master
renderDump.php
Go to the documentation of this file.
1<?php
34
35// @codeCoverageIgnoreStart
36require_once __DIR__ . '/Maintenance.php';
37// @codeCoverageIgnoreEnd
38
46
48 private $count = 0;
49 private string $outputDirectory;
50 private float $startTime;
52 private $prefix;
53
54 public function __construct() {
55 parent::__construct();
56 $this->addDescription(
57 'Take page text out of an XML dump file and render basic HTML out to files' );
58 $this->addOption( 'output-dir', 'The directory to output the HTML files to', true, true );
59 $this->addOption( 'prefix', 'Prefix for the rendered files (defaults to wiki)', false, true );
60 $this->addOption( 'parser', 'Use an alternative parser class', false, true );
61 }
62
63 public function execute() {
64 $this->outputDirectory = $this->getOption( 'output-dir' );
65 $this->prefix = $this->getOption( 'prefix', 'wiki' );
66 $this->startTime = microtime( true );
67
68 if ( $this->hasOption( 'parser' ) ) {
69 $this->prefix .= '-' . $this->getOption( 'parser' );
70 // T236809: We'll need to provide an alternate ParserFactory
71 // service to make this work.
72 $this->fatalError( 'Parser class configuration temporarily disabled.' );
73 }
74
75 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
76
77 $source = new ImportStreamSource( $this->getStdin() );
78 $importer = $this->getServiceContainer()
79 ->getWikiImporterFactory()
80 ->getWikiImporter( $source, new UltimateAuthority( $user ) );
81
82 $importer->setRevisionCallback(
83 [ $this, 'handleRevision' ] );
84 $importer->setNoticeCallback( static function ( $msg, $params ) {
85 echo wfMessage( $msg, $params )->text() . "\n";
86 } );
87
88 $importer->doImport();
89
90 $delta = microtime( true ) - $this->startTime;
91 $this->error( "Rendered {$this->count} pages in " . round( $delta, 2 ) . " seconds " );
92 if ( $delta > 0 ) {
93 $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
94 }
95 $this->error( "\n" );
96 }
97
102 public function handleRevision( WikiRevision $rev ) {
103 $title = $rev->getTitle();
104 if ( !$title ) {
105 $this->error( "Got bogus revision with null title!" );
106
107 return;
108 }
109 $display = $title->getPrefixedText();
110
111 $this->count++;
112
113 $sanitized = rawurlencode( $display );
114 $filename = sprintf( "%s/%s-%07d-%s.html",
115 $this->outputDirectory,
116 $this->prefix,
117 $this->count,
118 $sanitized );
119 $this->output( sprintf( "%s\t%s\n", $filename, $display ) );
120
121 $user = new User();
122 $options = ParserOptions::newFromUser( $user );
123
124 $content = $rev->getContent();
125 $contentRenderer = $this->getServiceContainer()->getContentRenderer();
126 // ContentRenderer expects a RevisionRecord, and all we have is a
127 // WikiRevision from the dump. Make a fake MutableRevisionRecord to
128 // satisfy it -- the only thing ::getParserOutput actually needs is
129 // the revision ID and revision timestamp.
130 $mutableRev = new MutableRevisionRecord( $rev->getTitle() );
131 $mutableRev->setId( $rev->getID() );
132 $mutableRev->setTimestamp( $rev->getTimestamp() );
133 $output = $contentRenderer->getParserOutput(
134 $content, $title, $mutableRev, $options
135 );
136
137 file_put_contents( $filename,
138 "<!DOCTYPE html>\n" .
139 "<html lang=\"en\" dir=\"ltr\">\n" .
140 "<head>\n" .
141 "<meta charset=\"UTF-8\" />\n" .
142 "<meta name=\"color-scheme\" content=\"light dark\">" .
143 "<title>" . htmlspecialchars( $display, ENT_COMPAT ) . "</title>\n" .
144 "</head>\n" .
145 "<body>\n" .
146 // TODO T371004 move runOutputPipeline out of $parserOutput
147 $output->runOutputPipeline( $options, [] )->getContentHolderText() .
148 "</body>\n" .
149 "</html>" );
150 }
151}
152
153// @codeCoverageIgnoreStart
154$maintClass = DumpRenderer::class;
155require_once RUN_MAINTENANCE_IF_MAIN;
156// @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.
__construct()
Default constructor.
execute()
Do the actual work.
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
getStdin( $len=null)
Return input from stdin.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
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