MediaWiki master
ParsoidCachePrewarmJob.php
Go to the documentation of this file.
1<?php
22
30use MediaWiki\Parser\Parsoid\Config\SiteConfig as ParsoidSiteConfig;
33use Psr\Log\LoggerInterface;
34
41 private LoggerInterface $logger;
42 private ParserOutputAccess $parserOutputAccess;
43 private PageLookup $pageLookup;
44 private RevisionLookup $revisionLookup;
45 private ParsoidSiteConfig $parsoidSiteConfig;
46
54 public function __construct(
55 array $params,
56 ParserOutputAccess $parserOutputAccess,
57 PageLookup $pageLookup,
58 RevisionLookup $revisionLookup,
59 ParsoidSiteConfig $parsoidSiteConfig
60 ) {
61 parent::__construct( 'parsoidCachePrewarm', $params );
62
63 // TODO: find a way to inject the logger
64 $this->logger = LoggerFactory::getInstance( 'ParsoidCachePrewarmJob' );
65 $this->parserOutputAccess = $parserOutputAccess;
66 $this->pageLookup = $pageLookup;
67 $this->revisionLookup = $revisionLookup;
68 $this->parsoidSiteConfig = $parsoidSiteConfig;
69 }
70
82 public static function newSpec(
83 int $revisionId,
84 PageRecord $page,
85 array $params = []
87 $pageId = $page->getId();
88 $pageTouched = $page->getTouched();
89
90 $params += [ 'options' => 0 ];
91
93 "parsoidCachePrewarm:$pageId:$revisionId:$pageTouched:{$params['options']}"
94 );
95
96 $opts = [ 'removeDuplicates' => true ];
97
98 return new JobSpecification(
99 'parsoidCachePrewarm',
100 [
101 'revId' => $revisionId,
102 'pageId' => $pageId,
103 'page_touched' => $pageTouched,
104 ] + $params,
105 $opts
106 );
107 }
108
109 private function doParsoidCacheUpdate() {
110 $page = $this->pageLookup->getPageById( $this->params['pageId'] );
111 $revId = $this->params['revId'];
112
113 if ( $page === null ) {
114 // This happens when the page got deleted in the meantime.
115 $this->logger->info( "Page with ID {$this->params['pageId']} not found" );
116 return;
117 }
118
119 if ( $page->getLatest() !== $revId ) {
120 $this->logger->info(
121 'ParsoidCachePrewarmJob: The ID of the new revision does not match the page\'s current revision ID'
122 );
123 return;
124 }
125
126 $rev = $this->revisionLookup->getRevisionById( $revId );
127 if ( !$rev ) {
128 return;
129 }
130
131 $parserOpts = ParserOptions::newFromAnon();
132 $parserOpts->setUseParsoid();
133
134 $renderReason = $this->params['causeAction'] ?? $this->command;
135 $parserOpts->setRenderReason( $renderReason );
136
137 $mainSlot = $rev->getSlot( SlotRecord::MAIN );
138 if ( !$this->parsoidSiteConfig->supportsContentModel( $mainSlot->getModel() ) ) {
139 $this->logger->debug( __METHOD__ . ': Parsoid does not support content model ' . $mainSlot->getModel() );
140 return;
141 }
142
143 $this->logger->debug( __METHOD__ . ': generating Parsoid output' );
144
145 // We may get the OPT_FORCE_PARSE flag this way
146 $options = $this->params['options'] ?? 0;
147
148 // getParserOutput() will write to ParserCache.
149 $status = $this->parserOutputAccess->getParserOutput(
150 $page,
151 $parserOpts,
152 $rev,
153 $options
154 );
155
156 if ( !$status->isOK() ) {
157 $this->logger->error( __METHOD__ . ': Parsoid error', [
158 'errors' => $status->getErrors(),
159 'page' => $page->getDBkey(),
160 'rev' => $rev->getId(),
161 ] );
162 }
163 }
164
165 public function run() {
166 $this->doParsoidCacheUpdate();
167
168 return true;
169 }
170}
171
173class_alias( ParsoidCachePrewarmJob::class, 'ParsoidCachePrewarmJob' );
Job queue task description base code.
Describe and execute a background job.
Definition Job.php:41
array $params
Array of job parameters.
Definition Job.php:46
static newRootJobParams( $key)
Get "root job" parameters for a task.
Definition Job.php:310
static newSpec(int $revisionId, PageRecord $page, array $params=[])
__construct(array $params, ParserOutputAccess $parserOutputAccess, PageLookup $pageLookup, RevisionLookup $revisionLookup, ParsoidSiteConfig $parsoidSiteConfig)
Create PSR-3 logger objects.
Service for getting rendered output of a given page.
Set options of the Parser.
Site-level configuration for Parsoid.
Value object representing a content slot associated with a page revision.
Service for looking up information about wiki pages.
Data record representing a page that is (or used to be, or could be) an editable page on a wiki.
getLatest( $wikiId=self::LOCAL)
The ID of the page's latest revision.
getTouched()
Timestamp at which the page was last flagged for rerendering.
getDBkey()
Get the page title in DB key form.
Service for looking up page revisions.