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