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