MediaWiki  master
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;
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
handleRevision( $rev)
Callback function for each revision, turn into HTML and save.
Definition: renderDump.php:89
error( $err, $die=0)
Throw an error to the user.
XML file reader for the page data importer.
getOption( $name, $default=null)
Get an option, or return the default.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:86
$source
hasOption( $name)
Checks to see if a particular option exists.
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
string $prefix
Definition: renderDump.php:44
addDescription( $text)
Set the description text.
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
$wgParserConf
Parser configuration.
output( $out, $channel=null)
Throw some output to the user.
static newFromUser( $user)
Get a ParserOptions object from a given user.
Maintenance script that takes page text out of an XML dump file and render basic HTML out to files...
Definition: renderDump.php:39
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getStdin( $len=null)
Return input from stdin.
$content
Definition: router.php:78
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
$maintClass
Definition: renderDump.php:128