File: LintErrorChecker.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (93 lines) | stat: -rw-r--r-- 2,880 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
// SPDX-License-Identifier: GPL-2.0-or-later

namespace MediaWiki\Parser\Parsoid;

use MediaWiki\Config\Config;
use MediaWiki\Content\WikitextContent;
use MediaWiki\Parser\ParserOutput;
use MediaWiki\Parser\Parsoid\Config\PageConfigFactory;
use MediaWiki\Registration\ExtensionRegistry;
use MediaWiki\Revision\MutableRevisionRecord;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Title\TitleFactory;
use Wikimedia\Parsoid\Parsoid;

/**
 * Check arbitrary wikitext for lint errors
 *
 * @since 1.43
 */
class LintErrorChecker {
	private Parsoid $parsoid;
	private PageConfigFactory $pageConfigFactory;
	private TitleFactory $titleFactory;
	private ExtensionRegistry $extensionRegistry;
	private Config $mainConfig;

	public function __construct(
		Parsoid $parsoid,
		PageConfigFactory $pageConfigFactory,
		TitleFactory $titleFactory,
		ExtensionRegistry $extensionRegistry,
		Config $mainConfig

	) {
		$this->parsoid = $parsoid;
		$this->pageConfigFactory = $pageConfigFactory;
		$this->titleFactory = $titleFactory;
		$this->extensionRegistry = $extensionRegistry;
		$this->mainConfig = $mainConfig;
	}

	private function linterOptions( array $disabled ): array {
		// FIXME: We shouldn't be this interwined with an extension (T360809)
		if ( $this->extensionRegistry->isLoaded( 'Linter' ) ) {
			foreach ( $this->mainConfig->get( 'LinterCategories' ) as $name => $cat ) {
				if ( $cat['priority'] === 'none' ) {
					$disabled[] = $name;
				}
			}
		}
		$disabled = array_unique( $disabled );
		return [ 'linterOverrides' => [ 'disabled' => $disabled ] ];
	}

	/**
	 * Check the given wikitext for lint errors
	 *
	 * While not strictly required, you'll get better results if the wikitext has already gone through PST
	 *
	 * @param string $wikitext Wikitext after PST
	 * @return array Array of error objects returned by Parsoid's lint API (empty array for no errors)
	 */
	public function check( string $wikitext ): array {
		return $this->checkSome( $wikitext, [] );
	}

	/**
	 * Check the given wikitext for lint errors against a subset of lint categories
	 *
	 * While not strictly required, you'll get better results if the wikitext has already gone through PST
	 *
	 * @param string $wikitext Wikitext after PST
	 * @param string[] $disabled Array of lint categories to disable
	 * @return array Array of error objects returned by Parsoid's lint API (empty array for no errors)
	 */
	public function checkSome( string $wikitext, array $disabled ): array {
		$title = $this->titleFactory->newMainPage();
		$fakeRevision = new MutableRevisionRecord( $title );
		$fakeRevision->setSlot(
			SlotRecord::newUnsaved(
				SlotRecord::MAIN,
				new WikitextContent( $wikitext )
			)
		);

		return $this->parsoid->wikitext2lint(
			$this->pageConfigFactory->create( $title, null, $fakeRevision ),
			$this->linterOptions( $disabled ),
			new ParserOutput()
		);
	}
}