| 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 |
|
| 16 |
require_once __DIR__ . '/Maintenance.php'; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
private function normalizeNamespace( string $namespace ): int { |
| 110 |
return $this->getServiceContainer()->getNamespaceInfo() |
| 111 |
->getCanonicalIndex( strtolower( $namespace ) ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function execute() { |
| 120 |
$force = $this->getOption( 'force' ); |
| 121 |
$startFrom = $this->getOption( 'start-from' ); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
$namespaceIndex = null; |
| 126 |
$namespace = $this->getOption( 'namespace' ); |
| 127 |
if ( $namespace !== null ) { |
| 128 |
$namespaceIndex = $this->normalizeNamespace( $namespace ); |
| 129 |
} |
| 130 |
|
| 131 |
if ( $force !== null ) { |
| 132 |
|
| 133 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 202 |
$maintClass = PrewarmParsoidParserCache::class; |
| 203 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 204 |
|