]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - maintenance/renderDump.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / maintenance / renderDump.php
index 97797792a0152c51b5fd8496f900d1a79e37beb1..68a371c3b255667ba79fd34c7999d48f61ae2d1f 100644 (file)
@@ -7,7 +7,7 @@
  * Templates etc are pulled from the local wiki database, not from the dump.
  *
  * Copyright (C) 2006 Brion Vibber <brion@pobox.com>
- * http://www.mediawiki.org/
+ * https://www.mediawiki.org/
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * @ingroup Maintenance
  */
 
-$optionsWithArgs = array( 'report' );
+require_once __DIR__ . '/Maintenance.php';
 
-require_once( 'commandLine.inc' );
+/**
+ * Maintenance script that takes page text out of an XML dump file
+ * and render basic HTML out to files.
+ *
+ * @ingroup Maintenance
+ */
+class DumpRenderer extends Maintenance {
+
+       private $count = 0;
+       private $outputDirectory, $startTime;
+
+       public function __construct() {
+               parent::__construct();
+               $this->addDescription(
+                       'Take page text out of an XML dump file and render basic HTML out to files' );
+               $this->addOption( 'output-dir', 'The directory to output the HTML files to', true, true );
+               $this->addOption( 'prefix', 'Prefix for the rendered files (defaults to wiki)', false, true );
+               $this->addOption( 'parser', 'Use an alternative parser class', false, true );
+       }
+
+       public function execute() {
+               $this->outputDirectory = $this->getOption( 'output-dir' );
+               $this->prefix = $this->getOption( 'prefix', 'wiki' );
+               $this->startTime = microtime( true );
 
-class DumpRenderer {
-       function __construct( $dir ) {
-               $this->stderr = fopen( "php://stderr", "wt" );
-               $this->outputDirectory = $dir;
-               $this->count = 0;
+               if ( $this->hasOption( 'parser' ) ) {
+                       global $wgParserConf;
+                       $wgParserConf['class'] = $this->getOption( 'parser' );
+                       $this->prefix .= "-{$wgParserConf['class']}";
+               }
+
+               $source = new ImportStreamSource( $this->getStdin() );
+               $importer = new WikiImporter( $source, $this->getConfig() );
+
+               $importer->setRevisionCallback(
+                       [ $this, 'handleRevision' ] );
+
+               $importer->doImport();
+
+               $delta = microtime( true ) - $this->startTime;
+               $this->error( "Rendered {$this->count} pages in " . round( $delta, 2 ) . " seconds " );
+               if ( $delta > 0 ) {
+                       $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
+               }
+               $this->error( "\n" );
        }
 
-       function handleRevision( $rev ) {
+       /**
+        * Callback function for each revision, turn into HTML and save
+        * @param Revision $rev
+        */
+       public function handleRevision( $rev ) {
                $title = $rev->getTitle();
-               if (!$title) {
-                       fprintf( $this->stderr, "Got bogus revision with null title!" );
+               if ( !$title ) {
+                       $this->error( "Got bogus revision with null title!" );
+
                        return;
                }
                $display = $title->getPrefixedText();
-               
+
                $this->count++;
-               
+
                $sanitized = rawurlencode( $display );
-               $filename = sprintf( "%s/wiki-%07d-%s.html", 
+               $filename = sprintf( "%s/%s-%07d-%s.html",
                        $this->outputDirectory,
+                       $this->prefix,
                        $this->count,
                        $sanitized );
-               fprintf( $this->stderr, "%s\n", $filename, $display );
-               
-               // fixme
+               $this->output( sprintf( "%s\n", $filename, $display ) );
+
                $user = new User();
-               $parser = new Parser();
                $options = ParserOptions::newFromUser( $user );
-               
-               $output = $parser->parse( $rev->getText(), $title, $options );
-               
+
+               $content = $rev->getContent();
+               $output = $content->getParserOutput( $title, null, $options );
+
                file_put_contents( $filename,
-                       "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" " .
-                       "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" .
-                       "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" .
+                       "<!DOCTYPE html>\n" .
+                       "<html lang=\"en\" dir=\"ltr\">\n" .
                        "<head>\n" .
-                       "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n" .
+                       "<meta charset=\"UTF-8\" />\n" .
                        "<title>" . htmlspecialchars( $display ) . "</title>\n" .
-                       "</head>\n" . 
+                       "</head>\n" .
                        "<body>\n" .
                        $output->getText() .
                        "</body>\n" .
                        "</html>" );
        }
-
-       function run() {
-               $this->startTime = wfTime();
-
-               $file = fopen( 'php://stdin', 'rt' );
-               $source = new ImportStreamSource( $file );
-               $importer = new WikiImporter( $source );
-
-               $importer->setRevisionCallback(
-                       array( &$this, 'handleRevision' ) );
-
-               return $importer->doImport();
-       }
 }
 
-if( isset( $options['output-dir'] ) ) {
-       $dir = $options['output-dir'];
-} else {
-       wfDie( "Must use --output-dir=/some/dir\n" );
-}
-$render = new DumpRenderer( $dir );
-$render->run();
-
-
+$maintClass = "DumpRenderer";
+require_once RUN_MAINTENANCE_IF_MAIN;