MediaWiki  master
renderDump.php
Go to the documentation of this file.
1 <?php
32 
33 require_once __DIR__ . '/Maintenance.php';
34 
41 class DumpRenderer extends Maintenance {
42 
43  private $count = 0;
44  private $outputDirectory, $startTime;
46  private $prefix;
47 
48  public function __construct() {
49  parent::__construct();
50  $this->addDescription(
51  'Take page text out of an XML dump file and render basic HTML out to files' );
52  $this->addOption( 'output-dir', 'The directory to output the HTML files to', true, true );
53  $this->addOption( 'prefix', 'Prefix for the rendered files (defaults to wiki)', false, true );
54  $this->addOption( 'parser', 'Use an alternative parser class', false, true );
55  }
56 
57  public function execute() {
58  $this->outputDirectory = $this->getOption( 'output-dir' );
59  $this->prefix = $this->getOption( 'prefix', 'wiki' );
60  $this->startTime = microtime( true );
61 
62  if ( $this->hasOption( 'parser' ) ) {
63  $this->prefix .= "-{$this->getOption( 'parser' )}";
64  // T236809: We'll need to provide an alternate ParserFactory
65  // service to make this work.
66  $this->fatalError( 'Parser class configuration temporarily disabled.' );
67  }
68 
69  $source = new ImportStreamSource( $this->getStdin() );
70  $importer = MediaWikiServices::getInstance()
71  ->getWikiImporterFactory()
72  ->getWikiImporter( $source );
73 
74  $importer->setRevisionCallback(
75  [ $this, 'handleRevision' ] );
76  $importer->setNoticeCallback( static function ( $msg, $params ) {
77  echo wfMessage( $msg, $params )->text() . "\n";
78  } );
79 
80  $importer->doImport();
81 
82  $delta = microtime( true ) - $this->startTime;
83  $this->error( "Rendered {$this->count} pages in " . round( $delta, 2 ) . " seconds " );
84  if ( $delta > 0 ) {
85  $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
86  }
87  $this->error( "\n" );
88  }
89 
94  public function handleRevision( WikiRevision $rev ) {
95  $title = $rev->getTitle();
96  if ( !$title ) {
97  $this->error( "Got bogus revision with null title!" );
98 
99  return;
100  }
101  $display = $title->getPrefixedText();
102 
103  $this->count++;
104 
105  $sanitized = rawurlencode( $display );
106  $filename = sprintf( "%s/%s-%07d-%s.html",
107  $this->outputDirectory,
108  $this->prefix,
109  $this->count,
110  $sanitized );
111  $this->output( sprintf( "%s\n", $filename, $display ) );
112 
113  $user = new User();
114  $options = ParserOptions::newFromUser( $user );
115 
116  $content = $rev->getContent();
117  $contentRenderer = MediaWikiServices::getInstance()->getContentRenderer();
118  $output = $contentRenderer->getParserOutput( $content, $title, null, $options );
119 
120  file_put_contents( $filename,
121  "<!DOCTYPE html>\n" .
122  "<html lang=\"en\" dir=\"ltr\">\n" .
123  "<head>\n" .
124  "<meta charset=\"UTF-8\" />\n" .
125  "<title>" . htmlspecialchars( $display, ENT_COMPAT ) . "</title>\n" .
126  "</head>\n" .
127  "<body>\n" .
128  $output->getText() .
129  "</body>\n" .
130  "</html>" );
131  }
132 }
133 
134 $maintClass = DumpRenderer::class;
135 require_once RUN_MAINTENANCE_IF_MAIN;
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Maintenance script that takes page text out of an XML dump file and render basic HTML out to files.
Definition: renderDump.php:41
handleRevision(WikiRevision $rev)
Callback function for each revision, turn into HTML and save.
Definition: renderDump.php:94
__construct()
Default constructor.
Definition: renderDump.php:48
execute()
Do the actual work.
Definition: renderDump.php:57
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...
Definition: Maintenance.php:66
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.
Service locator for MediaWiki core services.
static newFromUser( $user)
Get a ParserOptions object from a given user.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:70
Represents a revision, log entry or upload during the import process.
getContent( $role=SlotRecord::MAIN)
$source
$maintClass
Definition: renderDump.php:134
$content
Definition: router.php:76