Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
71.15% |
37 / 52 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
ParsoidCachePrewarmJob | |
71.15% |
37 / 52 |
|
50.00% |
2 / 4 |
10.94 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
newSpec | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
doParsoidCacheUpdate | |
65.52% |
19 / 29 |
|
0.00% |
0 / 1 |
7.48 | |||
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\Parser\Parsoid\ParsoidOutputAccess; |
25 | use MediaWiki\Revision\RevisionLookup; |
26 | use MediaWiki\Revision\SlotRecord; |
27 | use Psr\Log\LoggerInterface; |
28 | |
29 | /** |
30 | * @ingroup JobQueue |
31 | * @internal |
32 | * @since 1.40 |
33 | */ |
34 | class ParsoidCachePrewarmJob extends Job { |
35 | private LoggerInterface $logger; |
36 | private ParsoidOutputAccess $parsoidOutputAccess; |
37 | private PageLookup $pageLookup; |
38 | private RevisionLookup $revisionLookup; |
39 | |
40 | /** |
41 | * @param array $params |
42 | * @param ParsoidOutputAccess $parsoidOutputAccess |
43 | * @param PageLookup $pageLookup |
44 | * @param RevisionLookup $revisionLookup |
45 | */ |
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 | |
61 | /** |
62 | * @param int $revisionId |
63 | * @param PageRecord $page |
64 | * @param array $params Additional options for the job. Known keys: |
65 | * - causeAction: Indicate what action caused the job to be scheduled. Used for monitoring. |
66 | * - options: Flags to be passed to ParsoidOutputAccess:getParserOutput. |
67 | * May be set to ParserOutputAccess::OPT_FORCE_PARSE to force a parsing even if there |
68 | * already is cached output. |
69 | * |
70 | * @return JobSpecification |
71 | */ |
72 | public static function newSpec( |
73 | int $revisionId, |
74 | PageRecord $page, |
75 | array $params = [] |
76 | ): JobSpecification { |
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 | } |