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