MediaWiki REL1_40
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 $page,
75 array $params = []
77 if ( !is_int( $page ) && !( $page instanceof PageRecord ) ) {
78 throw new InvalidArgumentException(
79 '$page must be a int or a PageRecord object.' );
80 }
81
82 $pageId = is_int( $page ) ? $page : $page->getId();
83
84 $params += [ 'options' => 0 ];
85
86 $opts = [];
87 if ( $page instanceof PageRecord ) {
88 $pageTouched = $page->getTouched();
89 $params += self::newRootJobParams(
90 "parsoidCachePrewarm:$pageId:$revisionId:$pageTouched:{$params['options']}"
91 );
92 $params += [ 'page_touched' => $pageTouched ];
93 $opts = [ 'removeDuplicates' => true ];
94 }
95
96 return new JobSpecification(
97 'parsoidCachePrewarm',
98 [
99 'revId' => $revisionId,
100 'pageId' => $pageId,
101 ] + $params,
102 $opts
103 );
104 }
105
106 private function doParsoidCacheUpdate() {
107 $page = $this->pageLookup->getPageById( $this->params['pageId'] );
108 $revId = $this->params['revId'];
109
110 if ( $page === null ) {
111 // This happens when the page got deleted in the meantime.
112 $this->logger->info( "Page with ID {$this->params['pageId']} not found" );
113 return;
114 }
115
116 if ( $page->getLatest() !== $revId ) {
117 $this->logger->info(
118 'ParsoidCachePrewarmJob: The ID of the new revision does not match the page\'s current revision ID'
119 );
120 return;
121 }
122
123 $rev = $this->revisionLookup->getRevisionById( $revId );
124 if ( !$rev ) {
125 return;
126 }
127
128 $parserOpts = ParserOptions::newFromAnon();
129
130 $renderReason = $this->params['causeAction'] ?? $this->command;
131 $parserOpts->setRenderReason( $renderReason );
132
133 $mainSlot = $rev->getSlot( SlotRecord::MAIN );
134 if ( !$this->parsoidOutputAccess->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->parsoidOutputAccess->getParserOutput(
146 $page,
147 $parserOpts,
148 $rev,
149 $options | ParsoidOutputAccess::OPT_LOG_LINT_DATA
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}
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:88
Job queue task description base code.
Class to both describe a background job and handle jobs.
Definition Job.php:39
PSR-3 logger instance factory.
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, $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.
Service for looking up page revisions.
return true
Definition router.php:92