MediaWiki REL1_35
renderDump.php
Go to the documentation of this file.
1<?php
31require_once __DIR__ . '/Maintenance.php';
32
40
41 private $count = 0;
44 private $prefix;
45
46 public function __construct() {
47 parent::__construct();
48 $this->addDescription(
49 'Take page text out of an XML dump file and render basic HTML out to files' );
50 $this->addOption( 'output-dir', 'The directory to output the HTML files to', true, true );
51 $this->addOption( 'prefix', 'Prefix for the rendered files (defaults to wiki)', false, true );
52 $this->addOption( 'parser', 'Use an alternative parser class', false, true );
53 }
54
55 public function execute() {
56 $this->outputDirectory = $this->getOption( 'output-dir' );
57 $this->prefix = $this->getOption( 'prefix', 'wiki' );
58 $this->startTime = microtime( true );
59
60 if ( $this->hasOption( 'parser' ) ) {
61 $this->prefix .= "-{$this->getOption( 'parser' )}";
62 // T236809: We'll need to provide an alternate ParserFactory
63 // service to make this work.
64 $this->fatalError( 'Parser class configuration temporarily disabled.' );
65 }
66
67 $source = new ImportStreamSource( $this->getStdin() );
68 $importer = new WikiImporter( $source, $this->getConfig() );
69
70 $importer->setRevisionCallback(
71 [ $this, 'handleRevision' ] );
72 $importer->setNoticeCallback( 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
90 public function handleRevision( WikiRevision $rev ) {
91 $title = $rev->getTitle();
92 if ( !$title ) {
93 $this->error( "Got bogus revision with null title!" );
94
95 return;
96 }
97 $display = $title->getPrefixedText();
98
99 $this->count++;
100
101 $sanitized = rawurlencode( $display );
102 $filename = sprintf( "%s/%s-%07d-%s.html",
103 $this->outputDirectory,
104 $this->prefix,
105 $this->count,
106 $sanitized );
107 $this->output( sprintf( "%s\n", $filename, $display ) );
108
109 $user = new User();
110 $options = ParserOptions::newFromUser( $user );
111
112 $content = $rev->getContent();
113 $output = $content->getParserOutput( $title, null, $options );
114
115 file_put_contents( $filename,
116 "<!DOCTYPE html>\n" .
117 "<html lang=\"en\" dir=\"ltr\">\n" .
118 "<head>\n" .
119 "<meta charset=\"UTF-8\" />\n" .
120 "<title>" . htmlspecialchars( $display ) . "</title>\n" .
121 "</head>\n" .
122 "<body>\n" .
123 $output->getText() .
124 "</body>\n" .
125 "</html>" );
126 }
127}
128
129$maintClass = DumpRenderer::class;
130require_once RUN_MAINTENANCE_IF_MAIN;
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
const RUN_MAINTENANCE_IF_MAIN
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.
string $prefix
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.
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.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
XML file reader for the page data importer.
Represents a revision, log entry or upload during the import process.
getContent( $role=SlotRecord::MAIN)
$source
$maintClass
$content
Definition router.php:76