MediaWiki master
ParsoidCachePrewarmJob.php
Go to the documentation of this file.
1<?php
8
17use MediaWiki\Parser\Parsoid\Config\SiteConfig as ParsoidSiteConfig;
20use Psr\Log\LoggerInterface;
21
28 private LoggerInterface $logger;
29 private ParserOutputAccess $parserOutputAccess;
30 private PageLookup $pageLookup;
31 private RevisionLookup $revisionLookup;
32 private ParsoidSiteConfig $parsoidSiteConfig;
33
41 public function __construct(
42 array $params,
43 ParserOutputAccess $parserOutputAccess,
44 PageLookup $pageLookup,
45 RevisionLookup $revisionLookup,
46 ParsoidSiteConfig $parsoidSiteConfig
47 ) {
48 parent::__construct( 'parsoidCachePrewarm', $params );
49
50 // TODO: find a way to inject the logger
51 $this->logger = LoggerFactory::getInstance( 'ParsoidCachePrewarmJob' );
52 $this->parserOutputAccess = $parserOutputAccess;
53 $this->pageLookup = $pageLookup;
54 $this->revisionLookup = $revisionLookup;
55 $this->parsoidSiteConfig = $parsoidSiteConfig;
56 }
57
69 public static function newSpec(
70 int $revisionId,
71 PageRecord $page,
72 array $params = []
74 $pageId = $page->getId();
75 $pageTouched = $page->getTouched();
76
77 $params += [ 'options' => 0 ];
78
80 "parsoidCachePrewarm:$pageId:$revisionId:$pageTouched:{$params['options']}"
81 );
82
83 $opts = [ 'removeDuplicates' => true ];
84
85 return new JobSpecification(
86 'parsoidCachePrewarm',
87 [
88 'revId' => $revisionId,
89 'pageId' => $pageId,
90 'page_touched' => $pageTouched,
91 'namespace' => $page->getNamespace(),
92 'title' => $page->getDBkey()
93 ] + $params,
94 $opts
95 );
96 }
97
98 private function doParsoidCacheUpdate() {
99 $page = $this->pageLookup->getPageById( $this->params['pageId'] );
100 $revId = $this->params['revId'];
101
102 if ( $page === null ) {
103 // This happens when the page got deleted in the meantime.
104 $this->logger->info( "Page with ID {$this->params['pageId']} not found" );
105 return;
106 }
107
108 RequestContext::getMain()->setTitle( $this->title );
109
110 if ( $page->getLatest() !== $revId ) {
111 $this->logger->info(
112 'ParsoidCachePrewarmJob: The ID of the new revision does not match the page\'s current revision ID'
113 );
114 return;
115 }
116
117 $rev = $this->revisionLookup->getRevisionById( $revId );
118 if ( !$rev ) {
119 return;
120 }
121
122 $parserOpts = ParserOptions::newFromAnon();
123 $parserOpts->setUseParsoid();
124
125 $renderReason = $this->params['causeAction'] ?? $this->command;
126 $parserOpts->setRenderReason( $renderReason );
127
128 $mainSlot = $rev->getSlot( SlotRecord::MAIN );
129 if ( !$this->parsoidSiteConfig->supportsContentModel( $mainSlot->getModel() ) ) {
130 $this->logger->debug( __METHOD__ . ': Parsoid does not support content model ' . $mainSlot->getModel() );
131 return;
132 }
133
134 $this->logger->debug( __METHOD__ . ': generating Parsoid output' );
135
136 // We may get the OPT_FORCE_PARSE flag this way
137 $options = $this->params['options'] ?? 0;
138
139 // getParserOutput() will write to ParserCache.
140 $status = $this->parserOutputAccess->getParserOutput(
141 $page,
142 $parserOpts,
143 $rev,
144 $options
145 );
146
147 if ( !$status->isOK() ) {
148 $this->logger->error( __METHOD__ . ': Parsoid error', [
149 'errors' => $status->getErrors(),
150 'page' => $page->getDBkey(),
151 'rev' => $rev->getId(),
152 ] );
153 }
154 }
155
157 public function run() {
158 $this->doParsoidCacheUpdate();
159
160 return true;
161 }
162}
163
165class_alias( ParsoidCachePrewarmJob::class, 'ParsoidCachePrewarmJob' );
Group all the pieces relevant to the context of a request into one instance.
Job queue task description base code.
Describe and execute a background job.
Definition Job.php:28
array $params
Array of job parameters.
Definition Job.php:33
static newRootJobParams( $key)
Get "root job" parameters for a task.
Definition Job.php:296
static newSpec(int $revisionId, PageRecord $page, array $params=[])
run()
Run the job.If this method returns false or completes exceptionally, the job runner will retry execut...
__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.
getNamespace()
Returns the page's namespace number.
getDBkey()
Get the page title in DB key form.
Service for looking up page revisions.