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;
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  $output = $content->getParserOutput( $title, null, $options );
118 
119  file_put_contents( $filename,
120  "<!DOCTYPE html>\n" .
121  "<html lang=\"en\" dir=\"ltr\">\n" .
122  "<head>\n" .
123  "<meta charset=\"UTF-8\" />\n" .
124  "<title>" . htmlspecialchars( $display ) . "</title>\n" .
125  "</head>\n" .
126  "<body>\n" .
127  $output->getText() .
128  "</body>\n" .
129  "</html>" );
130  }
131 }
132 
133 $maintClass = DumpRenderer::class;
134 require_once RUN_MAINTENANCE_IF_MAIN;
$maintClass
$maintClass
Definition: renderDump.php:133
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:407
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:191
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:489
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
WikiRevision\getTitle
getTitle()
Definition: WikiRevision.php:429
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1182
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
ImportStreamSource
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
Definition: ImportStreamSource.php:32
DumpRenderer\handleRevision
handleRevision(WikiRevision $rev)
Callback function for each revision, turn into HTML and save.
Definition: renderDump.php:94
DumpRenderer\$outputDirectory
$outputDirectory
Definition: renderDump.php:44
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
DumpRenderer\__construct
__construct()
Default constructor.
Definition: renderDump.php:48
$title
$title
Definition: testCompression.php:38
DumpRenderer\$count
$count
Definition: renderDump.php:43
WikiRevision\getContent
getContent( $role=SlotRecord::MAIN)
Definition: WikiRevision.php:494
DumpRenderer\$prefix
string $prefix
Definition: renderDump.php:46
$content
$content
Definition: router.php:76
DumpRenderer\$startTime
$startTime
Definition: renderDump.php:44
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
WikiRevision
Represents a revision, log entry or upload during the import process.
Definition: WikiRevision.php:39
$source
$source
Definition: mwdoc-filter.php:34
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:464
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
DumpRenderer
Maintenance script that takes page text out of an XML dump file and render basic HTML out to files.
Definition: renderDump.php:41
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
DumpRenderer\execute
execute()
Do the actual work.
Definition: renderDump.php:57
ParserOptions\newFromUser
static newFromUser( $user)
Get a ParserOptions object from a given user.
Definition: ParserOptions.php:1087