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