Two new parser tests related to bug 6200
[lhc/web/wiklou.git] / maintenance / renderDump.php
index 9779779..36e35c2 100644 (file)
  * @file
  * @ingroup Maintenance
  */
+require_once( dirname( __FILE__ ) . '/Maintenance.php' );
 
-$optionsWithArgs = array( 'report' );
+class DumpRenderer extends Maintenance {
 
-require_once( 'commandLine.inc' );
+       private $count = 0;
+       private $outputDirectory, $startTime;
 
-class DumpRenderer {
-       function __construct( $dir ) {
-               $this->stderr = fopen( "php://stderr", "wt" );
-               $this->outputDirectory = $dir;
-               $this->count = 0;
+       public function __construct() {
+               parent::__construct();
+               $this->mDescription = "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 );
        }
 
-       function handleRevision( $rev ) {
+       public function execute() {
+               $this->outputDirectory = $this->getOption( 'output-dir' );
+               $this->prefix = $this->getOption( 'prefix', 'wiki' );
+               $this->startTime = wfTime();
+
+               if ( $this->hasOption( 'parser' ) ) {
+                       global $wgParserConf;
+                       $wgParserConf['class'] = $this->getOption( 'parser' );
+                       $this->prefix .= "-{$wgParserConf['class']}";
+               }
+
+               $source = new ImportStreamSource( $this->getStdin() );
+               $importer = new WikiImporter( $source );
+
+               $importer->setRevisionCallback(
+                       array( &$this, 'handleRevision' ) );
+
+               $importer->doImport();          
+               
+               $delta = wfTime() - $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" );
+       }
+       
+       /**
+        * Callback function for each revision, turn into HTML and save
+        * @param $rev Revision
+        */
+       public function handleRevision( $rev ) {
+               global $wgParserConf;
+               
                $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();
+               $parser = new $wgParserConf['class']();
                $options = ParserOptions::newFromUser( $user );
-               
+
                $output = $parser->parse( $rev->getText(), $title, $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" .
@@ -70,33 +106,13 @@ class DumpRenderer {
                        "<head>\n" .
                        "<meta http-equiv=\"Content-Type\" content=\"text/html; 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( DO_MAINTENANCE );