Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
25 / 25 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
LintErrorChecker | |
100.00% |
25 / 25 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
linterOptions | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
check | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
checkSome | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | // SPDX-License-Identifier: GPL-2.0-or-later |
3 | |
4 | namespace MediaWiki\Parser\Parsoid; |
5 | |
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; |
15 | |
16 | /** |
17 | * Check arbitrary wikitext for lint errors |
18 | * |
19 | * @since 1.43 |
20 | */ |
21 | class LintErrorChecker { |
22 | private Parsoid $parsoid; |
23 | private PageConfigFactory $pageConfigFactory; |
24 | private TitleFactory $titleFactory; |
25 | private ExtensionRegistry $extensionRegistry; |
26 | private Config $mainConfig; |
27 | |
28 | public function __construct( |
29 | Parsoid $parsoid, |
30 | PageConfigFactory $pageConfigFactory, |
31 | TitleFactory $titleFactory, |
32 | ExtensionRegistry $extensionRegistry, |
33 | Config $mainConfig |
34 | |
35 | ) { |
36 | $this->parsoid = $parsoid; |
37 | $this->pageConfigFactory = $pageConfigFactory; |
38 | $this->titleFactory = $titleFactory; |
39 | $this->extensionRegistry = $extensionRegistry; |
40 | $this->mainConfig = $mainConfig; |
41 | } |
42 | |
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; |
49 | } |
50 | } |
51 | } |
52 | $disabled = array_unique( $disabled ); |
53 | return [ 'linterOverrides' => [ 'disabled' => $disabled ] ]; |
54 | } |
55 | |
56 | /** |
57 | * Check the given wikitext for lint errors |
58 | * |
59 | * While not strictly required, you'll get better results if the wikitext has already gone through PST |
60 | * |
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) |
63 | */ |
64 | public function check( string $wikitext ): array { |
65 | return $this->checkSome( $wikitext, [] ); |
66 | } |
67 | |
68 | /** |
69 | * Check the given wikitext for lint errors against a subset of lint categories |
70 | * |
71 | * While not strictly required, you'll get better results if the wikitext has already gone through PST |
72 | * |
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) |
76 | */ |
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 ) |
84 | ) |
85 | ); |
86 | |
87 | return $this->parsoid->wikitext2lint( |
88 | $this->pageConfigFactory->create( $title, null, $fakeRevision ), |
89 | $this->linterOptions( $disabled ), |
90 | new ParserOutput() |
91 | ); |
92 | } |
93 | } |