| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
use MediaWiki\Parser\ParserOptions; |
| 32 |
use MediaWiki\Permissions\UltimateAuthority; |
| 33 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 34 |
use MediaWiki\User\User; |
| 35 |
|
| 36 |
|
| 37 |
require_once __DIR__ . '/Maintenance.php'; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class DumpRenderer extends Maintenance { |
| 47 |
|
| 48 |
|
| 49 |
private $count = 0; |
| 50 |
private string $outputDirectory; |
| 51 |
private float $startTime; |
| 52 |
|
| 53 |
private $prefix; |
| 54 |
|
| 55 |
public function __construct() { |
| 56 |
parent::__construct(); |
| 57 |
$this->addDescription( |
| 58 |
'Take page text out of an XML dump file and render basic HTML out to files' ); |
| 59 |
$this->addOption( 'output-dir', 'The directory to output the HTML files to', true, true ); |
| 60 |
$this->addOption( 'prefix', 'Prefix for the rendered files (defaults to wiki)', false, true ); |
| 61 |
$this->addOption( 'parser', 'Use an alternative parser class', false, true ); |
| 62 |
} |
| 63 |
|
| 64 |
public function execute() { |
| 65 |
$this->outputDirectory = $this->getOption( 'output-dir' ); |
| 66 |
$this->prefix = $this->getOption( 'prefix', 'wiki' ); |
| 67 |
$this->startTime = microtime( true ); |
| 68 |
|
| 69 |
if ( $this->hasOption( 'parser' ) ) { |
| 70 |
$this->prefix .= '-' . $this->getOption( 'parser' ); |
| 71 |
|
| 72 |
|
| 73 |
$this->fatalError( 'Parser class configuration temporarily disabled.' ); |
| 74 |
} |
| 75 |
|
| 76 |
$user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
| 77 |
|
| 78 |
$source = new ImportStreamSource( $this->getStdin() ); |
| 79 |
$importer = $this->getServiceContainer() |
| 80 |
->getWikiImporterFactory() |
| 81 |
->getWikiImporter( $source, new UltimateAuthority( $user ) ); |
| 82 |
|
| 83 |
$importer->setRevisionCallback( |
| 84 |
[ $this, 'handleRevision' ] ); |
| 85 |
$importer->setNoticeCallback( static function ( $msg, $params ) { |
| 86 |
echo wfMessage( $msg, $params )->text() . "\n"; |
| 87 |
} ); |
| 88 |
|
| 89 |
$importer->doImport(); |
| 90 |
|
| 91 |
$delta = microtime( true ) - $this->startTime; |
| 92 |
$this->error( "Rendered {$this->count} pages in " . round( $delta, 2 ) . " seconds " ); |
| 93 |
if ( $delta > 0 ) { |
| 94 |
$this->error( round( $this->count / $delta, 2 ) . " pages/sec" ); |
| 95 |
} |
| 96 |
$this->error( "\n" ); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function handleRevision( WikiRevision $rev ) { |
| 104 |
$title = $rev->getTitle(); |
| 105 |
if ( !$title ) { |
| 106 |
$this->error( "Got bogus revision with null title!" ); |
| 107 |
|
| 108 |
return; |
| 109 |
} |
| 110 |
$display = $title->getPrefixedText(); |
| 111 |
|
| 112 |
$this->count++; |
| 113 |
|
| 114 |
$sanitized = rawurlencode( $display ); |
| 115 |
$filename = sprintf( "%s/%s-%07d-%s.html", |
| 116 |
$this->outputDirectory, |
| 117 |
$this->prefix, |
| 118 |
$this->count, |
| 119 |
$sanitized ); |
| 120 |
$this->output( sprintf( "%s\t%s\n", $filename, $display ) ); |
| 121 |
|
| 122 |
$user = new User(); |
| 123 |
$options = ParserOptions::newFromUser( $user ); |
| 124 |
|
| 125 |
$content = $rev->getContent(); |
| 126 |
$contentRenderer = $this->getServiceContainer()->getContentRenderer(); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
$mutableRev = new MutableRevisionRecord( $rev->getTitle() ); |
| 132 |
$mutableRev->setId( $rev->getID() ); |
| 133 |
$mutableRev->setTimestamp( $rev->getTimestamp() ); |
| 134 |
$output = $contentRenderer->getParserOutput( |
| 135 |
$content, $title, $mutableRev, $options |
| 136 |
); |
| 137 |
|
| 138 |
file_put_contents( $filename, |
| 139 |
"<!DOCTYPE html>\n" . |
| 140 |
"<html lang=\"en\" dir=\"ltr\">\n" . |
| 141 |
"<head>\n" . |
| 142 |
"<meta charset=\"UTF-8\" />\n" . |
| 143 |
"<meta name=\"color-scheme\" content=\"light dark\">" . |
| 144 |
"<title>" . htmlspecialchars( $display, ENT_COMPAT ) . "</title>\n" . |
| 145 |
"</head>\n" . |
| 146 |
"<body>\n" . |
| 147 |
|
| 148 |
$output->runOutputPipeline( $options, [] )->getContentHolderText() . |
| 149 |
"</body>\n" . |
| 150 |
"</html>" ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
$maintClass = DumpRenderer::class; |
| 156 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 157 |
|