Debian Codegraph @

extensions/Linter/includes/LintUpdate.php

raw symbols SCIP
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 namespace MediaWiki\Linter;
22
23 use MediaWiki\Content\TextContent;
24 use MediaWiki\Deferred\DataUpdate;
25 use MediaWiki\Logger\LoggerFactory;
26 use MediaWiki\Page\ParserOutputAccess;
27 use MediaWiki\Page\WikiPageFactory;
28 use MediaWiki\Revision\RenderedRevision;
29 use MediaWiki\Revision\RevisionRecord;
30 use MediaWiki\Revision\SlotRecord;
31
32 class LintUpdate extends DataUpdate {
33
34 private WikiPageFactory $wikiPageFactory;
35 private ParserOutputAccess $parserOutputAccess;
36 private RenderedRevision $renderedRevision;
37
38 public function __construct(
39 WikiPageFactory $wikiPageFactory,
40 ParserOutputAccess $parserOutputAccess,
41 RenderedRevision $renderedRevision
42 ) {
43 parent::__construct();
44 $this->wikiPageFactory = $wikiPageFactory;
45 $this->parserOutputAccess = $parserOutputAccess;
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 $pOptions->setRenderReason( 'LintUpdate' );
69
70 LoggerFactory::getInstance( 'Linter' )->debug(
71 '{method}: Parsing {page}',
72 [
73 'method' => __METHOD__,
74 'page' => $page->getTitle()->getPrefixedDBkey(),
75 'touched' => $page->getTouched()
76 ]
77 );
78
79 // Don't update the parser cache, to avoid flooding it.
80 // This matches the behavior of RefreshLinksJob.
81 // However, unlike RefreshLinksJob, we don't parse if we already
82 // have the output in the cache. This avoids duplicating the effort
83 // of ParsoidCachePrewarmJob / DiscussionTools
84 // (note that even with OPT_NO_UPDATE_CACHE we still update the
85 // *local* cache, which prevents wasting effort on duplicate parses)
86 $this->parserOutputAccess->getParserOutput(
87 $page, $pOptions, $rev,
88 ParserOutputAccess::OPT_NO_UPDATE_CACHE
89 );
90 }
91 }