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