MediaWiki master
ParsoidCachePrewarmJob.php
Go to the documentation of this file.
1<?php
25use MediaWiki\Parser\Parsoid\Config\SiteConfig as ParsoidSiteConfig;
28use Psr\Log\LoggerInterface;
29
36 private LoggerInterface $logger;
37 private ParserOutputAccess $parserOutputAccess;
38 private PageLookup $pageLookup;
39 private RevisionLookup $revisionLookup;
40 private ParsoidSiteConfig $parsoidSiteConfig;
41
49 public function __construct(
50 array $params,
51 ParserOutputAccess $parserOutputAccess,
52 PageLookup $pageLookup,
53 RevisionLookup $revisionLookup,
54 ParsoidSiteConfig $parsoidSiteConfig
55 ) {
56 parent::__construct( 'parsoidCachePrewarm', $params );
57
58 // TODO: find a way to inject the logger
59 $this->logger = LoggerFactory::getInstance( 'ParsoidCachePrewarmJob' );
60 $this->parserOutputAccess = $parserOutputAccess;
61 $this->pageLookup = $pageLookup;
62 $this->revisionLookup = $revisionLookup;
63 $this->parsoidSiteConfig = $parsoidSiteConfig;
64 }
65
77 public static function newSpec(
78 int $revisionId,
79 PageRecord $page,
80 array $params = []
82 $pageId = $page->getId();
83 $pageTouched = $page->getTouched();
84
85 $params += [ 'options' => 0 ];
86
87 $params += self::newRootJobParams(
88 "parsoidCachePrewarm:$pageId:$revisionId:$pageTouched:{$params['options']}"
89 );
90
91 $opts = [ 'removeDuplicates' => true ];
92
93 return new JobSpecification(
94 'parsoidCachePrewarm',
95 [
96 'revId' => $revisionId,
97 'pageId' => $pageId,
98 'page_touched' => $pageTouched,
99 ] + $params,
100 $opts
101 );
102 }
103
104 private function doParsoidCacheUpdate() {
105 $page = $this->pageLookup->getPageById( $this->params['pageId'] );
106 $revId = $this->params['revId'];
107
108 if ( $page === null ) {
109 // This happens when the page got deleted in the meantime.
110 $this->logger->info( "Page with ID {$this->params['pageId']} not found" );
111 return;
112 }
113
114 if ( $page->getLatest() !== $revId ) {
115 $this->logger->info(
116 'ParsoidCachePrewarmJob: The ID of the new revision does not match the page\'s current revision ID'
117 );
118 return;
119 }
120
121 $rev = $this->revisionLookup->getRevisionById( $revId );
122 if ( !$rev ) {
123 return;
124 }
125
126 $parserOpts = ParserOptions::newFromAnon();
127 $parserOpts->setUseParsoid();
128
129 $renderReason = $this->params['causeAction'] ?? $this->command;
130 $parserOpts->setRenderReason( $renderReason );
131
132 $mainSlot = $rev->getSlot( SlotRecord::MAIN );
133 if ( !$this->parsoidSiteConfig->supportsContentModel( $mainSlot->getModel() ) ) {
134 $this->logger->debug( __METHOD__ . ': Parsoid does not support content model ' . $mainSlot->getModel() );
135 return;
136 }
137
138 $this->logger->debug( __METHOD__ . ': generating Parsoid output' );
139
140 // We may get the OPT_FORCE_PARSE flag this way
141 $options = $this->params['options'] ?? 0;
142
143 // getParserOutput() will write to ParserCache.
144 $status = $this->parserOutputAccess->getParserOutput(
145 $page,
146 $parserOpts,
147 $rev,
148 $options
149 );
150
151 if ( !$status->isOK() ) {
152 $this->logger->error( __METHOD__ . ': Parsoid error', [
153 'errors' => $status->getErrors(),
154 'page' => $page->getDBkey(),
155 'rev' => $rev->getId(),
156 ] );
157 }
158 }
159
160 public function run() {
161 $this->doParsoidCacheUpdate();
162
163 return true;
164 }
165}
array $params
The job parameters.
Job queue task description base code.
Describe and execute a background job.
Definition Job.php:41
Create PSR-3 logger objects.
Service for getting rendered output of a given page.
Site-level configuration for Parsoid.
Value object representing a content slot associated with a page revision.
static newFromAnon()
Get a ParserOptions object for an anonymous user.
static newSpec(int $revisionId, PageRecord $page, array $params=[])
__construct(array $params, ParserOutputAccess $parserOutputAccess, PageLookup $pageLookup, RevisionLookup $revisionLookup, ParsoidSiteConfig $parsoidSiteConfig)
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.