Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
66.10% |
39 / 59 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
ParsoidCachePrewarmJob | |
66.10% |
39 / 59 |
|
50.00% |
2 / 4 |
12.16 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
newSpec | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
doParsoidCacheUpdate | |
60.00% |
21 / 35 |
|
0.00% |
0 / 1 |
8.30 | |||
run | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
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. |
7 | * |
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. |
12 | * |
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 |
17 | * |
18 | * @file |
19 | */ |
20 | |
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; |
30 | |
31 | /** |
32 | * @ingroup JobQueue |
33 | * @internal |
34 | * @since 1.40 |
35 | */ |
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; |
42 | |
43 | /** |
44 | * @param array $params |
45 | * @param ParserOutputAccess $parserOutputAccess |
46 | * @param PageLookup $pageLookup |
47 | * @param RevisionLookup $revisionLookup |
48 | * @param ParsoidSiteConfig $parsoidSiteConfig |
49 | */ |
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 | |
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. |
75 | * |
76 | * @return JobSpecification |
77 | */ |
78 | public static function newSpec( |
79 | int $revisionId, |
80 | PageRecord $page, |
81 | array $params = [] |
82 | ): JobSpecification { |
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 | } |