parsoid: use real ParserOutput, not StubMetadataCollector in wikitext2lint
[mediawiki.git] / includes / parser / Parsoid / LintErrorChecker.php
blob45d80eebbdf4c849b7f09a4503cdaeba43769bc6
1 <?php
2 // SPDX-License-Identifier: GPL-2.0-or-later
4 namespace MediaWiki\Parser\Parsoid;
6 use MediaWiki\Config\Config;
7 use MediaWiki\Content\WikitextContent;
8 use MediaWiki\Parser\ParserOutput;
9 use MediaWiki\Parser\Parsoid\Config\PageConfigFactory;
10 use MediaWiki\Registration\ExtensionRegistry;
11 use MediaWiki\Revision\MutableRevisionRecord;
12 use MediaWiki\Revision\SlotRecord;
13 use MediaWiki\Title\TitleFactory;
14 use Wikimedia\Parsoid\Parsoid;
16 /**
17 * Check arbitrary wikitext for lint errors
19 * @since 1.43
21 class LintErrorChecker {
22 private Parsoid $parsoid;
23 private PageConfigFactory $pageConfigFactory;
24 private TitleFactory $titleFactory;
25 private ExtensionRegistry $extensionRegistry;
26 private Config $mainConfig;
28 public function __construct(
29 Parsoid $parsoid,
30 PageConfigFactory $pageConfigFactory,
31 TitleFactory $titleFactory,
32 ExtensionRegistry $extensionRegistry,
33 Config $mainConfig
35 ) {
36 $this->parsoid = $parsoid;
37 $this->pageConfigFactory = $pageConfigFactory;
38 $this->titleFactory = $titleFactory;
39 $this->extensionRegistry = $extensionRegistry;
40 $this->mainConfig = $mainConfig;
43 private function linterOptions( array $disabled ): array {
44 // FIXME: We shouldn't be this interwined with an extension (T360809)
45 if ( $this->extensionRegistry->isLoaded( 'Linter' ) ) {
46 foreach ( $this->mainConfig->get( 'LinterCategories' ) as $name => $cat ) {
47 if ( $cat['priority'] === 'none' ) {
48 $disabled[] = $name;
52 $disabled = array_unique( $disabled );
53 return [ 'linterOverrides' => [ 'disabled' => $disabled ] ];
56 /**
57 * Check the given wikitext for lint errors
59 * While not strictly required, you'll get better results if the wikitext has already gone through PST
61 * @param string $wikitext Wikitext after PST
62 * @return array Array of error objects returned by Parsoid's lint API (empty array for no errors)
64 public function check( string $wikitext ): array {
65 return $this->checkSome( $wikitext, [] );
68 /**
69 * Check the given wikitext for lint errors against a subset of lint categories
71 * While not strictly required, you'll get better results if the wikitext has already gone through PST
73 * @param string $wikitext Wikitext after PST
74 * @param string[] $disabled Array of lint categories to disable
75 * @return array Array of error objects returned by Parsoid's lint API (empty array for no errors)
77 public function checkSome( string $wikitext, array $disabled ): array {
78 $title = $this->titleFactory->newMainPage();
79 $fakeRevision = new MutableRevisionRecord( $title );
80 $fakeRevision->setSlot(
81 SlotRecord::newUnsaved(
82 SlotRecord::MAIN,
83 new WikitextContent( $wikitext )
87 return $this->parsoid->wikitext2lint(
88 $this->pageConfigFactory->create( $title, null, $fakeRevision ),
89 $this->linterOptions( $disabled ),
90 new ParserOutput()