MediaWiki master
ParsoidCachePrewarmJob.php
Go to the documentation of this file.
1<?php
27use Psr\Log\LoggerInterface;
28
35 private LoggerInterface $logger;
36 private ParsoidOutputAccess $parsoidOutputAccess;
37 private PageLookup $pageLookup;
38 private RevisionLookup $revisionLookup;
39
46 public function __construct(
47 array $params,
48 ParsoidOutputAccess $parsoidOutputAccess,
49 PageLookup $pageLookup,
50 RevisionLookup $revisionLookup
51 ) {
52 parent::__construct( 'parsoidCachePrewarm', $params );
53
54 // TODO: find a way to inject the logger
55 $this->logger = LoggerFactory::getInstance( 'ParsoidCachePrewarmJob' );
56 $this->parsoidOutputAccess = $parsoidOutputAccess;
57 $this->pageLookup = $pageLookup;
58 $this->revisionLookup = $revisionLookup;
59 }
60
72 public static function newSpec(
73 int $revisionId,
74 PageRecord $page,
75 array $params = []
77 $pageId = $page->getId();
78 $pageTouched = $page->getTouched();
79
80 $params += [ 'options' => 0 ];
81
82 $params += self::newRootJobParams(
83 "parsoidCachePrewarm:$pageId:$revisionId:$pageTouched:{$params['options']}"
84 );
85
86 $opts = [ 'removeDuplicates' => true ];
87
88 return new JobSpecification(
89 'parsoidCachePrewarm',
90 [
91 'revId' => $revisionId,
92 'pageId' => $pageId,
93 'page_touched' => $pageTouched,
94 ] + $params,
95 $opts
96 );
97 }
98
99 private function doParsoidCacheUpdate() {
100 $page = $this->pageLookup->getPageById( $this->params['pageId'] );
101 $revId = $this->params['revId'];
102
103 if ( $page === null ) {
104 // This happens when the page got deleted in the meantime.
105 $this->logger->info( "Page with ID {$this->params['pageId']} not found" );
106 return;
107 }
108
109 if ( $page->getLatest() !== $revId ) {
110 $this->logger->info(
111 'ParsoidCachePrewarmJob: The ID of the new revision does not match the page\'s current revision ID'
112 );
113 return;
114 }
115
116 $rev = $this->revisionLookup->getRevisionById( $revId );
117 if ( !$rev ) {
118 return;
119 }
120
121 $parserOpts = ParserOptions::newFromAnon();
122
123 $renderReason = $this->params['causeAction'] ?? $this->command;
124 $parserOpts->setRenderReason( $renderReason );
125
126 $mainSlot = $rev->getSlot( SlotRecord::MAIN );
127 if ( !$this->parsoidOutputAccess->supportsContentModel( $mainSlot->getModel() ) ) {
128 $this->logger->debug( __METHOD__ . ': Parsoid does not support content model ' . $mainSlot->getModel() );
129 return;
130 }
131
132 $this->logger->debug( __METHOD__ . ': generating Parsoid output' );
133
134 // We may get the OPT_FORCE_PARSE flag this way
135 $options = $this->params['options'] ?? 0;
136
137 // getParserOutput() will write to ParserCache.
138 $status = $this->parsoidOutputAccess->getParserOutput( $page, $parserOpts, $rev, $options );
139
140 if ( !$status->isOK() ) {
141 $this->logger->error( __METHOD__ . ': Parsoid error', [
142 'errors' => $status->getErrors(),
143 'page' => $page->getDBkey(),
144 'rev' => $rev->getId(),
145 ] );
146 }
147 }
148
149 public function run() {
150 $this->doParsoidCacheUpdate();
151
152 return true;
153 }
154}
Job queue task description base code.
Class to both describe a background job and handle jobs.
Definition Job.php:40
array $params
Array of job parameters.
Definition Job.php:45
Create PSR-3 logger objects.
MediaWiki service for getting Parsoid Output objects.
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)
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.