MediaWiki  1.34.0
renderDump.php
Go to the documentation of this file.
1 <?php
31 require_once __DIR__ . '/Maintenance.php';
32 
39 class DumpRenderer extends Maintenance {
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  global $wgParserConf;
62  $wgParserConf['class'] = $this->getOption( 'parser' );
63  $this->prefix .= "-{$wgParserConf['class']}";
64  }
65 
66  $source = new ImportStreamSource( $this->getStdin() );
67  $importer = new WikiImporter( $source, $this->getConfig() );
68 
69  $importer->setRevisionCallback(
70  [ $this, 'handleRevision' ] );
71  $importer->setNoticeCallback( function ( $msg, $params ) {
72  echo wfMessage( $msg, $params )->text() . "\n";
73  } );
74 
75  $importer->doImport();
76 
77  $delta = microtime( true ) - $this->startTime;
78  $this->error( "Rendered {$this->count} pages in " . round( $delta, 2 ) . " seconds " );
79  if ( $delta > 0 ) {
80  $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
81  }
82  $this->error( "\n" );
83  }
84 
89  public function handleRevision( $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\n", $filename, $display ) );
107 
108  $user = new User();
109  $options = ParserOptions::newFromUser( $user );
110 
111  $content = $rev->getContent();
112  $output = $content->getParserOutput( $title, null, $options );
113 
114  file_put_contents( $filename,
115  "<!DOCTYPE html>\n" .
116  "<html lang=\"en\" dir=\"ltr\">\n" .
117  "<head>\n" .
118  "<meta charset=\"UTF-8\" />\n" .
119  "<title>" . htmlspecialchars( $display ) . "</title>\n" .
120  "</head>\n" .
121  "<body>\n" .
122  $output->getText() .
123  "</body>\n" .
124  "</html>" );
125  }
126 }
127 
128 $maintClass = DumpRenderer::class;
129 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
WikiImporter
XML file reader for the page data importer.
Definition: WikiImporter.php:35
$maintClass
$maintClass
Definition: renderDump.php:128
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:426
$wgParserConf
$wgParserConf
Parser configuration.
Definition: DefaultSettings.php:4135
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
DumpRenderer\handleRevision
handleRevision( $rev)
Callback function for each revision, turn into HTML and save.
Definition: renderDump.php:89
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
ImportStreamSource
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
Definition: ImportStreamSource.php:32
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:613
DumpRenderer\$outputDirectory
$outputDirectory
Definition: renderDump.php:42
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:267
DumpRenderer\__construct
__construct()
Default constructor.
Definition: renderDump.php:46
$title
$title
Definition: testCompression.php:34
$output
$output
Definition: SyntaxHighlight.php:335
DumpRenderer\$count
$count
Definition: renderDump.php:41
DumpRenderer\$prefix
string $prefix
Definition: renderDump.php:44
$content
$content
Definition: router.php:78
DumpRenderer\$startTime
$startTime
Definition: renderDump.php:42
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:302
$source
$source
Definition: mwdoc-filter.php:34
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:481
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:288
DumpRenderer
Maintenance script that takes page text out of an XML dump file and render basic HTML out to files.
Definition: renderDump.php:39
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
DumpRenderer\execute
execute()
Do the actual work.
Definition: renderDump.php:55
ParserOptions\newFromUser
static newFromUser( $user)
Get a ParserOptions object from a given user.
Definition: ParserOptions.php:1027