Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / jobqueue / jobs / ParsoidCachePrewarmJob.php
blob3c79b351b3605487ac5dd0183ca23d95ad5a4ca5
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\Logger\LoggerFactory;
22 use MediaWiki\Page\PageLookup;
23 use MediaWiki\Page\PageRecord;
24 use MediaWiki\Page\ParserOutputAccess;
25 use MediaWiki\Parser\ParserOptions;
26 use MediaWiki\Parser\Parsoid\Config\SiteConfig as ParsoidSiteConfig;
27 use MediaWiki\Revision\RevisionLookup;
28 use MediaWiki\Revision\SlotRecord;
29 use Psr\Log\LoggerInterface;
31 /**
32 * @ingroup JobQueue
33 * @internal
34 * @since 1.40
36 class ParsoidCachePrewarmJob extends Job {
37 private LoggerInterface $logger;
38 private ParserOutputAccess $parserOutputAccess;
39 private PageLookup $pageLookup;
40 private RevisionLookup $revisionLookup;
41 private ParsoidSiteConfig $parsoidSiteConfig;
43 /**
44 * @param array $params
45 * @param ParserOutputAccess $parserOutputAccess
46 * @param PageLookup $pageLookup
47 * @param RevisionLookup $revisionLookup
48 * @param ParsoidSiteConfig $parsoidSiteConfig
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 );
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;
67 /**
68 * @param int $revisionId
69 * @param PageRecord $page
70 * @param array $params Additional options for the job. Known keys:
71 * - causeAction: Indicate what action caused the job to be scheduled. Used for monitoring.
72 * - options: Flags to be passed to ParserOutputAccess:getParserOutput.
73 * May be set to ParserOutputAccess::OPT_FORCE_PARSE to force a parsing even if there
74 * already is cached output.
76 * @return JobSpecification
78 public static function newSpec(
79 int $revisionId,
80 PageRecord $page,
81 array $params = []
82 ): JobSpecification {
83 $pageId = $page->getId();
84 $pageTouched = $page->getTouched();
86 $params += [ 'options' => 0 ];
88 $params += self::newRootJobParams(
89 "parsoidCachePrewarm:$pageId:$revisionId:$pageTouched:{$params['options']}"
92 $opts = [ 'removeDuplicates' => true ];
94 return new JobSpecification(
95 'parsoidCachePrewarm',
97 'revId' => $revisionId,
98 'pageId' => $pageId,
99 'page_touched' => $pageTouched,
100 ] + $params,
101 $opts
105 private function doParsoidCacheUpdate() {
106 $page = $this->pageLookup->getPageById( $this->params['pageId'] );
107 $revId = $this->params['revId'];
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;
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'
119 return;
122 $rev = $this->revisionLookup->getRevisionById( $revId );
123 if ( !$rev ) {
124 return;
127 $parserOpts = ParserOptions::newFromAnon();
128 $parserOpts->setUseParsoid();
130 $renderReason = $this->params['causeAction'] ?? $this->command;
131 $parserOpts->setRenderReason( $renderReason );
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;
139 $this->logger->debug( __METHOD__ . ': generating Parsoid output' );
141 // We may get the OPT_FORCE_PARSE flag this way
142 $options = $this->params['options'] ?? 0;
144 // getParserOutput() will write to ParserCache.
145 $status = $this->parserOutputAccess->getParserOutput(
146 $page,
147 $parserOpts,
148 $rev,
149 $options
152 if ( !$status->isOK() ) {
153 $this->logger->error( __METHOD__ . ': Parsoid error', [
154 'errors' => $status->getErrors(),
155 'page' => $page->getDBkey(),
156 'rev' => $rev->getId(),
157 ] );
161 public function run() {
162 $this->doParsoidCacheUpdate();
164 return true;