1
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
<?php
use MediaWiki\Maintenance\Maintenance;
use MediaWiki\Page\PageLookup;
use MediaWiki\Page\PageRecord;
use MediaWiki\Page\ParserOutputAccess;
use MediaWiki\Parser\ParserOptions;
use MediaWiki\Parser\Parsoid\Config\SiteConfig as ParsoidSiteConfig;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Status\Status;
use Wikimedia\Parsoid\Core\ClientError;
use Wikimedia\Parsoid\Core\ResourceLimitExceededException;
use Wikimedia\Rdbms\SelectQueryBuilder;
// @codeCoverageIgnoreStart
require_once __DIR__ . '/Maintenance.php';
// @codeCoverageIgnoreEnd
/**
* Maintenance script for populating parser cache with parsoid output.
*
* @since 1.41
*
* @license GPL-2.0-or-later
* @author Richika Rana
*/
class PrewarmParsoidParserCache extends Maintenance {
private int $forceParse = 0;
private ParserOutputAccess $parserOutputAccess;
private PageLookup $pageLookup;
private RevisionLookup $revisionLookup;
private ParsoidSiteConfig $parsoidSiteConfig;
public function __construct() {
parent::__construct();
$this->addDescription(
'Populate parser cache with parsoid output. By default, script attempt to run' .
'for supported content model pages (in a specified batch if provided)'
);
$this->addOption(
'force',
'Re-parse pages even if the cached entry seems up to date',
false,
false
);
$this->addOption( 'start-from', 'Start from this page ID', false, true );
$this->addOption( 'namespace', 'Filter pages in this namespace', false, true );
$this->setBatchSize( 100 );
}
private function getPageLookup(): PageLookup {
$this->pageLookup = $this->getServiceContainer()->getPageStore();
return $this->pageLookup;
}
private function getRevisionLookup(): RevisionLookup {
$this->revisionLookup = $this->getServiceContainer()->getRevisionLookup();
return $this->revisionLookup;
}
private function getParserOutputAccess(): ParserOutputAccess {
$this->parserOutputAccess = $this->getServiceContainer()->getParserOutputAccess();
return $this->parserOutputAccess;
}
private function getParsoidSiteConfig(): ParsoidSiteConfig {
$this->parsoidSiteConfig = $this->getServiceContainer()->getParsoidSiteConfig();
return $this->parsoidSiteConfig;
}
private function getQueryBuilder(): SelectQueryBuilder {
$dbr = $this->getReplicaDB();
return $dbr->newSelectQueryBuilder()
->select( [ 'page_id' ] )
->from( 'page' )
->caller( __METHOD__ )
->orderBy( 'page_id', SelectQueryBuilder::SORT_ASC );
}
private function parse(
PageRecord $page,
RevisionRecord $revision
): Status {
$popts = ParserOptions::newFromAnon();
$popts->setUseParsoid();
try {
return $this->getParserOutputAccess()->getParserOutput(
$page,
$popts,
$revision,
$this->forceParse
);
} catch ( ClientError $e ) {
return Status::newFatal( 'parsoid-client-error', $e->getMessage() );
} catch ( ResourceLimitExceededException $e ) {
return Status::newFatal( 'parsoid-resource-limit-exceeded', $e->getMessage() );
}
}
/*
* NamespaceInfo::getCanonicalIndex() requires the namespace to be in lowercase,
* so let's do some normalization and return its canonical index.
*
* @param string $namespace The namespace string from the command line
* @return int The canonical index of the namespace
*/
private function normalizeNamespace( string $namespace ): int {
return $this->getServiceContainer()->getNamespaceInfo()
->getCanonicalIndex( strtolower( $namespace ) );
}
/**
* Populate parser cache with parsoid output.
*
* @return bool
*/
public function execute() {
$force = $this->getOption( 'force' );
$startFrom = $this->getOption( 'start-from' );
// We need the namespace index instead of the name to perform the query
// on, because that's what the page table stores (in the page_namespace field).
$namespaceIndex = null;
$namespace = $this->getOption( 'namespace' );
if ( $namespace !== null ) {
$namespaceIndex = $this->normalizeNamespace( $namespace );
}
if ( $force !== null ) {
// If --force is supplied, for a parse for supported pages or supported
// pages in the specified batch.
$this->forceParse = ParserOutputAccess::OPT_FORCE_PARSE;
}
$startFrom = (int)$startFrom;
$this->output( "\nWarming parsoid parser cache with Parsoid output...\n\n" );
while ( true ) {
$query = $this->getQueryBuilder();
if ( $namespaceIndex !== null ) {
$query = $query->where( [ 'page_namespace' => $namespaceIndex ] );
}
$query = $query->where( $this->getReplicaDB()->expr( 'page_id', '>=', $startFrom ) )
->limit( $this->getBatchSize() );
$result = $query->fetchResultSet();
if ( !$result->numRows() ) {
break;
}
$currentBatch = $startFrom + ( $this->getBatchSize() - 1 );
$this->output( "\n\nBatch: $startFrom - $currentBatch\n----\n" );
// Look through pages by pageId and populate the parserCache
foreach ( $result as $row ) {
$page = $this->getPageLookup()->getPageById( $row->page_id );
$startFrom = ( (int)$row->page_id + 1 );
if ( $page === null ) {
$this->output( "\n[Skipped] Page ID: $row->page_id not found.\n" );
continue;
}
$latestRevision = $page->getLatest();
$revision = $this->getRevisionLookup()->getRevisionById( $latestRevision );
$mainSlot = $revision->getSlot( SlotRecord::MAIN );
// POA will write a dummy output to PC, but we don't want that here. Just skip!
if ( !$this->getParsoidSiteConfig()->supportsContentModel( $mainSlot->getModel() ) ) {
$this->output(
'[Skipped] Content model "' .
$mainSlot->getModel() .
"\" not supported for page ID: $row->page_id.\n"
);
continue;
}
$status = $this->parse( $page, $revision );
if ( !$status->isOK() ) {
$this->output(
__METHOD__ .
": Error parsing page ID: $row->page_id or writing to parser cache\n"
);
continue;
}
$this->output( "[Done] Page ID: $row->page_id ✔️\n" );
}
$this->waitForReplication();
}
$this->output( "\nDone pre-warming parsoid parser cache...\n" );
return true;
}
}
// @codeCoverageIgnoreStart
$maintClass = PrewarmParsoidParserCache::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd
|