Debian Codegraph @

maintenance/prewarmParsoidParserCache.php

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