MediaWiki
master
Toggle main menu visibility
renderDump.php
Go to the documentation of this file.
1
<?php
16
17
use
MediaWiki\Import\ImportStreamSource
;
18
use
MediaWiki\Import\WikiRevision
;
19
use
MediaWiki\Maintenance\Maintenance
;
20
use
MediaWiki\Parser\ParserOptions
;
21
use
MediaWiki\Permissions\UltimateAuthority
;
22
use
MediaWiki\Revision\MutableRevisionRecord
;
23
use MediaWiki\User\User;
24
use Wikimedia\Timestamp\ConvertibleTimestamp;
25
26
// @codeCoverageIgnoreStart
27
require_once __DIR__ .
'/Maintenance.php'
;
28
// @codeCoverageIgnoreEnd
29
36
class
DumpRenderer
extends
Maintenance
{
37
39
private
$count = 0;
40
private
string
$outputDirectory;
41
private
float
$startTime;
43
private
$prefix;
44
45
public
function
__construct
() {
46
parent::__construct();
47
$this->addDescription(
48
'Take page text out of an XML dump file and render basic HTML out to files'
);
49
$this->addOption(
'output-dir'
,
'The directory to output the HTML files to'
,
true
,
true
);
50
$this->addOption(
'prefix'
,
'Prefix for the rendered files (defaults to wiki)'
,
false
,
true
);
51
$this->addOption(
'parser'
,
'Use an alternative parser class'
,
false
,
true
);
52
}
53
54
public
function
execute
() {
55
$this->outputDirectory = $this->getOption(
'output-dir'
);
56
$this->prefix = $this->getOption(
'prefix'
,
'wiki'
);
57
$this->startTime = ConvertibleTimestamp::hrtime();
58
59
if
( $this->hasOption(
'parser'
) ) {
60
$this->prefix .=
'-'
. $this->getOption(
'parser'
);
61
// T236809: We'll need to provide an alternate ParserFactory
62
// service to make this work.
63
$this->fatalError(
'Parser class configuration temporarily disabled.'
);
64
}
65
66
$user =
User::newSystemUser
(
User::MAINTENANCE_SCRIPT_USER
, [
'steal'
=>
true
] );
67
68
$source
=
new
ImportStreamSource
( $this->getStdin() );
69
$importer = $this->getServiceContainer()
70
->getWikiImporterFactory()
71
->getWikiImporter(
$source
,
new
UltimateAuthority
( $user ) );
72
73
$importer->setRevisionCallback(
74
$this->
handleRevision
( ... ) );
75
$importer->setNoticeCallback(
static
function
( $msg, $params ) {
76
echo
wfMessage
( $msg, $params )->text() .
"\n"
;
77
} );
78
79
$importer->doImport();
80
81
$delta = ( ConvertibleTimestamp::hrtime() - $this->startTime ) / 1e9;
82
$this->error(
"Rendered {$this->count} pages in "
. round( $delta, 2 ) .
" seconds "
);
83
if
( $delta > 0 ) {
84
$this->error( round( $this->count / $delta, 2 ) .
" pages/sec"
);
85
}
86
$this->error(
"\n"
);
87
}
88
92
public
function
handleRevision
(
WikiRevision
$rev ) {
93
$title = $rev->
getTitle
();
94
if
( !$title ) {
95
$this->error(
"Got bogus revision with null title!"
);
96
97
return
;
98
}
99
$display = $title->getPrefixedText();
100
101
$this->count++;
102
103
$sanitized = rawurlencode( $display );
104
$filename = sprintf(
"%s/%s-%07d-%s.html"
,
105
$this->outputDirectory,
106
$this->prefix,
107
$this->count,
108
$sanitized );
109
$this->output( sprintf(
"%s\t%s\n"
, $filename, $display ) );
110
111
$user =
new
User
();
112
$options =
ParserOptions::newFromUser
( $user );
113
114
$content = $rev->
getContent
();
115
$contentRenderer = $this->getServiceContainer()->getContentRenderer();
116
// ContentRenderer expects a RevisionRecord, and all we have is a
117
// WikiRevision from the dump. Make a fake MutableRevisionRecord to
118
// satisfy it -- the only thing ::getParserOutput actually needs is
119
// the revision ID and revision timestamp.
120
$mutableRev =
new
MutableRevisionRecord
( $rev->
getTitle
() );
121
$mutableRev->setId( $rev->
getID
() );
122
$mutableRev->setTimestamp( $rev->
getTimestamp
() );
123
$output = $contentRenderer->getParserOutput(
124
$content, $title, $mutableRev, $options
125
);
126
127
file_put_contents( $filename,
128
"<!DOCTYPE html>\n"
.
129
"<html lang=\"en\" dir=\"ltr\">\n"
.
130
"<head>\n"
.
131
"<meta charset=\"UTF-8\" />\n"
.
132
"<meta name=\"color-scheme\" content=\"light dark\">"
.
133
"<title>"
. htmlspecialchars( $display, ENT_COMPAT ) .
"</title>\n"
.
134
"</head>\n"
.
135
"<body>\n"
.
136
// TODO T371004 move runOutputPipeline out of $parserOutput
137
$output->runOutputPipeline( $options, [] )->getContentHolderText() .
138
"</body>\n"
.
139
"</html>"
);
140
}
141
}
142
143
// @codeCoverageIgnoreStart
144
$maintClass
= DumpRenderer::class;
145
require_once RUN_MAINTENANCE_IF_MAIN;
146
// @codeCoverageIgnoreEnd
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition
GlobalFunctions.php:548
$maintClass
$maintClass
Definition
addChangeTag.php:56
DumpRenderer
Maintenance script that takes page text out of an XML dump file and render basic HTML out to files.
Definition
renderDump.php:36
DumpRenderer\handleRevision
handleRevision(WikiRevision $rev)
Callback function for each revision, turn into HTML and save.
Definition
renderDump.php:92
DumpRenderer\__construct
__construct()
Definition
renderDump.php:45
DumpRenderer\execute
execute()
Definition
renderDump.php:54
MediaWiki\Import\ImportStreamSource
Imports a XML dump from a file (either from file upload, files on disk, or HTTP).
Definition
ImportStreamSource.php:23
MediaWiki\Import\WikiRevision
Represents a revision, log entry or upload during the import process.
Definition
WikiRevision.php:36
MediaWiki\Import\WikiRevision\getContent
getContent( $role=SlotRecord::MAIN)
Definition
WikiRevision.php:461
MediaWiki\Import\WikiRevision\getTitle
getTitle()
Definition
WikiRevision.php:397
MediaWiki\Import\WikiRevision\getTimestamp
getTimestamp()
Definition
WikiRevision.php:413
MediaWiki\Import\WikiRevision\getID
getID()
Definition
WikiRevision.php:405
MediaWiki\Parser\ParserOptions
Set options of the Parser.
Definition
ParserOptions.php:70
MediaWiki\Parser\ParserOptions\newFromUser
static newFromUser( $user)
Get a ParserOptions object from a given user.
Definition
ParserOptions.php:1172
MediaWiki\Permissions\UltimateAuthority
Represents an authority that has all permissions.
Definition
UltimateAuthority.php:22
MediaWiki\Revision\MutableRevisionRecord
Definition
MutableRevisionRecord.php:31
MediaWiki\User\User
User class for the MediaWiki software.
Definition
User.php:129
MediaWiki\User\User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition
User.php:808
MediaWiki\User\User\MAINTENANCE_SCRIPT_USER
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition
User.php:164
$source
$source
Definition
mwdoc-filter.php:36
MediaWiki\Maintenance
Definition
applyChangeTag.php:11
maintenance
renderDump.php
Generated on
for MediaWiki by
1.18.0