Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.67% covered (success)
96.67%
29 / 30
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
LintUpdate
96.67% covered (success)
96.67%
29 / 30
50.00% covered (danger)
50.00%
1 / 2
4
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 doUpdate
96.15% covered (success)
96.15%
25 / 26
0.00% covered (danger)
0.00%
0 / 1
3
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
21namespace MediaWiki\Linter;
22
23use MediaWiki\Content\TextContent;
24use MediaWiki\Deferred\DataUpdate;
25use MediaWiki\Logger\LoggerFactory;
26use MediaWiki\Page\WikiPageFactory;
27use MediaWiki\Parser\Parsoid\ParsoidParser;
28use MediaWiki\Revision\RenderedRevision;
29use MediaWiki\Revision\RevisionRecord;
30use MediaWiki\Revision\SlotRecord;
31
32class LintUpdate extends DataUpdate {
33
34    private ParsoidParser $parsoid;
35    private WikiPageFactory $wikiPageFactory;
36    private RenderedRevision $renderedRevision;
37
38    public function __construct(
39        ParsoidParser $parsoid,
40        WikiPageFactory $wikiPageFactory,
41        RenderedRevision $renderedRevision
42    ) {
43        parent::__construct();
44        $this->parsoid = $parsoid;
45        $this->wikiPageFactory = $wikiPageFactory;
46        $this->renderedRevision = $renderedRevision;
47    }
48
49    public function doUpdate() {
50        $rev = $this->renderedRevision->getRevision();
51        $mainSlot = $rev->getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
52
53        $page = $this->wikiPageFactory->newFromTitle( $rev->getPage() );
54
55        if ( $page->getLatest() !== $rev->getId() ) {
56            // The given revision is no longer the latest revision.
57            return;
58        }
59
60        $content = $mainSlot->getContent();
61        if ( !$content instanceof TextContent ) {
62            // Linting is only defined for text
63            return;
64        }
65
66        $pOptions = $page->makeParserOptions( 'canonical' );
67        $pOptions->setUseParsoid();
68
69        LoggerFactory::getInstance( 'Linter' )->debug(
70            '{method}: Parsing {page}',
71            [
72                'method' => __METHOD__,
73                'page' => $page->getTitle()->getPrefixedDBkey(),
74                'touched' => $page->getTouched()
75            ]
76        );
77
78        // Don't update the parser cache, to avoid flooding it.
79        // This matches the behavior of RefreshLinksJob.
80        // However, unlike RefreshLinksJob, we don't parse if we already
81        // have the output in the cache. This avoids duplicating the effort
82        // of ParsoidCachePrewarmJob.
83        $this->parsoid->parse(
84            $content->getText(),
85            $rev->getPage(),
86            $pOptions,
87            true,
88            true,
89            $rev->getId()
90        );
91    }
92}