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  $this->prefix .= "-{$this->getOption( 'parser' )}";
62  // T236809: We'll need to provide an alternate ParserFactory
63  // service to make this work.
64  $this->fatalError( 'Parser class configuration temporarily disabled.' );
65  }
66 
67  $source = new ImportStreamSource( $this->getStdin() );
68  $importer = new WikiImporter( $source, $this->getConfig() );
69 
70  $importer->setRevisionCallback(
71  [ $this, 'handleRevision' ] );
72  $importer->setNoticeCallback( function ( $msg, $params ) {
73  echo wfMessage( $msg, $params )->text() . "\n";
74  } );
75 
76  $importer->doImport();
77 
78  $delta = microtime( true ) - $this->startTime;
79  $this->error( "Rendered {$this->count} pages in " . round( $delta, 2 ) . " seconds " );
80  if ( $delta > 0 ) {
81  $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
82  }
83  $this->error( "\n" );
84  }
85 
90  public function handleRevision( WikiRevision $rev ) {
91  $title = $rev->getTitle();
92  if ( !$title ) {
93  $this->error( "Got bogus revision with null title!" );
94 
95  return;
96  }
97  $display = $title->getPrefixedText();
98 
99  $this->count++;
100 
101  $sanitized = rawurlencode( $display );
102  $filename = sprintf( "%s/%s-%07d-%s.html",
103  $this->outputDirectory,
104  $this->prefix,
105  $this->count,
106  $sanitized );
107  $this->output( sprintf( "%s\n", $filename, $display ) );
108 
109  $user = new User();
110  $options = ParserOptions::newFromUser( $user );
111 
112  $content = $rev->getContent();
113  $output = $content->getParserOutput( $title, null, $options );
114 
115  file_put_contents( $filename,
116  "<!DOCTYPE html>\n" .
117  "<html lang=\"en\" dir=\"ltr\">\n" .
118  "<head>\n" .
119  "<meta charset=\"UTF-8\" />\n" .
120  "<title>" . htmlspecialchars( $display ) . "</title>\n" .
121  "</head>\n" .
122  "<body>\n" .
123  $output->getText() .
124  "</body>\n" .
125  "</html>" );
126  }
127 }
128 
129 $maintClass = DumpRenderer::class;
130 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
WikiImporter
XML file reader for the page data importer.
Definition: WikiImporter.php:37
$maintClass
$maintClass
Definition: renderDump.php:129
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:406
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:487
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
WikiRevision\getTitle
getTitle()
Definition: WikiRevision.php:428
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1219
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
ImportStreamSource
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
Definition: ImportStreamSource.php:32
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:596
DumpRenderer\handleRevision
handleRevision(WikiRevision $rev)
Callback function for each revision, turn into HTML and save.
Definition: renderDump.php:90
DumpRenderer\$outputDirectory
$outputDirectory
Definition: renderDump.php:42
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
DumpRenderer\__construct
__construct()
Default constructor.
Definition: renderDump.php:46
$title
$title
Definition: testCompression.php:38
DumpRenderer\$count
$count
Definition: renderDump.php:41
WikiRevision\getContent
getContent( $role=SlotRecord::MAIN)
Definition: WikiRevision.php:493
DumpRenderer\$prefix
string $prefix
Definition: renderDump.php:44
$content
$content
Definition: router.php:76
DumpRenderer\$startTime
$startTime
Definition: renderDump.php:42
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:281
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:463
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
DumpRenderer
Maintenance script that takes page text out of an XML dump file and render basic HTML out to files.
Definition: renderDump.php:39
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
DumpRenderer\execute
execute()
Do the actual work.
Definition: renderDump.php:55
ParserOptions\newFromUser
static newFromUser( $user)
Get a ParserOptions object from a given user.
Definition: ParserOptions.php:1088