MediaWiki master
ParsoidCachePrewarmJob.php
Go to the documentation of this file.
1<?php
24use MediaWiki\Parser\Parsoid\Config\SiteConfig as ParsoidSiteConfig;
28use Psr\Log\LoggerInterface;
29
36 private LoggerInterface $logger;
37 private ParsoidOutputAccess $parsoidOutputAccess;
38 private PageLookup $pageLookup;
39 private RevisionLookup $revisionLookup;
40 private ParsoidSiteConfig $parsoidSiteConfig;
41
49 public function __construct(
50 array $params,
51 ParsoidOutputAccess $parsoidOutputAccess,
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->parsoidOutputAccess = $parsoidOutputAccess;
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
128 $renderReason = $this->params['causeAction'] ?? $this->command;
129 $parserOpts->setRenderReason( $renderReason );
130
131 $mainSlot = $rev->getSlot( SlotRecord::MAIN );
132 if ( !$this->parsoidSiteConfig->supportsContentModel( $mainSlot->getModel() ) ) {
133 $this->logger->debug( __METHOD__ . ': Parsoid does not support content model ' . $mainSlot->getModel() );
134 return;
135 }
136
137 $this->logger->debug( __METHOD__ . ': generating Parsoid output' );
138
139 // We may get the OPT_FORCE_PARSE flag this way
140 $options = $this->params['options'] ?? 0;
141
142 // getParserOutput() will write to ParserCache.
143 $status = $this->parsoidOutputAccess->getParserOutput( $page, $parserOpts, $rev, $options );
144
145 if ( !$status->isOK() ) {
146 $this->logger->error( __METHOD__ . ': Parsoid error', [
147 'errors' => $status->getErrors(),
148 'page' => $page->getDBkey(),
149 'rev' => $rev->getId(),
150 ] );
151 }
152 }
153
154 public function run() {
155 $this->doParsoidCacheUpdate();
156
157 return true;
158 }
159}
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.
Site-level configuration for Parsoid.
MediaWiki service for getting rendered page content.
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, ParsoidOutputAccess $parsoidOutputAccess, 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.