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
;
17 * Check arbitrary wikitext for lint errors
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(
30 PageConfigFactory
$pageConfigFactory,
31 TitleFactory
$titleFactory,
32 ExtensionRegistry
$extensionRegistry,
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' ) {
52 $disabled = array_unique( $disabled );
53 return [ 'linterOverrides' => [ 'disabled' => $disabled ] ];
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, [] );
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(
83 new WikitextContent( $wikitext )
87 return $this->parsoid
->wikitext2lint(
88 $this->pageConfigFactory
->create( $title, null, $fakeRevision ),
89 $this->linterOptions( $disabled ),