File: SpecialLintErrors.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 (389 lines) | stat: -rw-r--r-- 13,229 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

namespace MediaWiki\Linter;

use MediaWiki\Cache\LinkCache;
use MediaWiki\Html\Html;
use MediaWiki\HTMLForm\HTMLForm;
use MediaWiki\Output\OutputPage;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\Request\WebRequest;
use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Title\MalformedTitleException;
use MediaWiki\Title\NamespaceInfo;
use MediaWiki\Title\TitleParser;

class SpecialLintErrors extends SpecialPage {

	private NamespaceInfo $namespaceInfo;
	private TitleParser $titleParser;
	private LinkCache $linkCache;
	private PermissionManager $permissionManager;
	private CategoryManager $categoryManager;
	private TotalsLookup $totalsLookup;

	/**
	 * @var string|null
	 */
	private $category;

	/**
	 * @param NamespaceInfo $namespaceInfo
	 * @param TitleParser $titleParser
	 * @param LinkCache $linkCache
	 * @param PermissionManager $permissionManager
	 * @param CategoryManager $categoryManager
	 * @param TotalsLookup $totalsLookup
	 */
	public function __construct(
		NamespaceInfo $namespaceInfo,
		TitleParser $titleParser,
		LinkCache $linkCache,
		PermissionManager $permissionManager,
		CategoryManager $categoryManager,
		TotalsLookup $totalsLookup
	) {
		parent::__construct( 'LintErrors' );
		$this->namespaceInfo = $namespaceInfo;
		$this->titleParser = $titleParser;
		$this->linkCache = $linkCache;
		$this->permissionManager = $permissionManager;
		$this->categoryManager = $categoryManager;
		$this->totalsLookup = $totalsLookup;
	}

	/**
	 * @param string $titleLabel
	 */
	protected function showFilterForm( $titleLabel ) {
		$selectOptions = [
			(string)$this->msg( 'linter-form-exact-match' )->escaped() => true,
			(string)$this->msg( 'linter-form-prefix-match' )->escaped() => false,
		];
		$namespaces = $this->getContext()->getRequest()->getVal( "wpNamespaceRestrictions" );
		$fields = [
			'NamespaceRestrictions' => [
				'type' => 'namespacesmultiselect',
				'label' => $this->msg( 'linter-form-namespace' )->text(),
				'exists' => true,
				'cssclass' => 'mw-block-partial-restriction',
				'default' => $namespaces,
				'input' => [ 'autocomplete' => false ]
			],
			'titlefield' => [
				'type' => 'title',
				'name' => $titleLabel,
				'label-message' => 'linter-form-title-prefix',
				'exists' => true,
				'required' => false
			],
			'exactmatchradio' => [
				'type' => 'radio',
				'name' => 'exactmatch',
				'options' => $selectOptions,
				'label-message' => 'linter-form-exact-or-prefix',
				'default' => true
			]
		];

		$selectTemplateOptions = [
			(string)$this->msg( 'linter-form-template-option-all' )->escaped() => 'all',
			(string)$this->msg( 'linter-form-template-option-with' )->escaped() => 'with',
			(string)$this->msg( 'linter-form-template-option-without' )->escaped() => 'without',
		];
		$htmlTags = new HtmlTags( $this );
		$tagAndTemplateFields = [
			'tag' => [
				'type' => 'select',
				'name' => 'tag',
				'label-message' => 'linter-form-tag',
				'options' => $htmlTags->getAllowedHTMLTags()
			],
			'template' => [
				'type' => 'select',
				'name' => 'template',
				'label-message' => 'linter-form-template',
				'options' => $selectTemplateOptions
			]
		];
		$fields = array_merge( $fields, $tagAndTemplateFields );

		$form = HTMLForm::factory( 'ooui', $fields, $this->getContext() );
		$form->setWrapperLegend( true );
		if ( $this->category !== null ) {
			$form->addHeaderHtml( $this->msg( "linter-category-{$this->category}-desc" )->parse() );
		}
		$form->setMethod( 'get' );
		$form->prepareForm()->displayForm( false );
	}

	/**
	 * cleanTitle parses a title and handles a malformed titles, namespaces that are mismatched
	 * and exact title searches that find no matching records, and produce appropriate error messages
	 *
	 * @param string $title
	 * @param array $namespaces
	 * @return array
	 */
	public function cleanTitle( string $title, $namespaces ): array {
		// Check all titles for malformation regardless of exact match or prefix match
		try {
			$titleElements = $this->titleParser->parseTitle( $title );
		} catch ( MalformedTitleException  $e ) {
			return [ 'titlefield' => null, 'error' => 'linter-invalid-title' ];
		}

		// The drop-down namespace defaults to 'all' which is returned as a null, indicating match all namespaces.
		// If 'main' is selected in the drop-down, int 0 is returned. Other namespaces are returned as int values > 0.
		//
		// If the user does not specify a namespace in the title text box, parseTitle sets it to int 0 as the default.
		// If the user entered ':' (main) namespace as the namespace prefix of a title such as ":MyPageTitle",
		// parseTitle will also return int 0 as the namespace. Other valid system namespaces entered as prefixes
		// in the title text box are returned by parseTitle as int values > 0.
		// To determine if the user entered the ':' (main) namespace when int 0 is returned, a separate check for
		// the substring ':' at offset 0 must be performed.

		$titleNamespace = $titleElements->getNamespace();
		// Determine if the user entered ':' (resolves to main) as the namespace part of the title,
		// or was it was set by default by parseTitle() to 0, but the user intended to search across 'all' namespaces.
		if ( $titleNamespace === 0 && $title[0] !== ':' ) {
			$titleNamespace = null;
		}

		if ( $namespaces && $titleNamespace !== null && !in_array( $titleNamespace, $namespaces ) ) {
			// Show the namespace mismatch error if the namespaces specified in drop-down and title text do not match.
			return [ 'titlefield' => null, 'error' => 'linter-namespace-mismatch' ];
		}

		// If no namespaces are selected (null), return the namespace from the title text
		$namespaces = $namespaces ?: [ $titleNamespace ];

		return [ 'titlefield' => $titleElements->getDBkey(), 'namespace' => $namespaces ];
	}

	/**
	 * @param OutputPage $out
	 * @param string|null $message
	 */
	private function displayError( $out, $message ) {
		$out->addHTML(
			Html::element( 'span', [ 'class' => 'error' ],
				$this->msg( $message )->text() )
		);
	}

	/**
	 * Extract namespace settings from the request object,
	 * returning an array of namespace id numbers
	 *
	 * @param WebRequest $request
	 * @return array
	 */
	protected function findNamespaces( $request ) {
		$namespaceRequestValues = $request->getRawVal( 'wpNamespaceRestrictions' ) ?? '';
		if ( $namespaceRequestValues === '' ) {
			return [];
		}

		// Security measure: only allow active namespace IDs to reach the query
		return array_values(
			array_intersect(
				// Remove -2 = "media" and -1 = "Special" namespace elements
				array_filter(
					array_keys(
						$this->namespaceInfo->getCanonicalNamespaces()
					),
					static function ( $x ) {
						return $x >= 0;
					}
				),
				array_map( 'intval', explode( "\n", $namespaceRequestValues ) )
			)
		);
	}

	/**
	 * @param string|null $subPage
	 */
	public function execute( $subPage ) {
		$request = $this->getRequest();
		$out = $this->getOutput();

		$params = $request->getQueryValues();

		$this->setHeaders();
		$this->outputHeader( $subPage || isset( $params[ 'titlesearch' ] ) ? 'disable-summary' : '' );

		$namespaces = $this->findNamespaces( $request );

		$exactMatch = $request->getBool( 'exactmatch', true );
		$tagName = $this->getRequest()->getText( 'tag' );
		// map command line tag name through an associative array to protect request from an SQL injection security risk
		$htmlTags = new HtmlTags( $this );
		$allowedHtmlTags = $htmlTags->getAllowedHTMLTags();
		$tag = $allowedHtmlTags[ $tagName ] ?? 'all';
		$template = $this->getRequest()->getText( 'template' );

		// If the request contains a 'titlesearch' parameter, then the user entered a page title
		// or just the first few characters of the title. They also may have entered the first few characters
		// of a custom namespace (just the text before a ':') to search for and pressed the associated Submit button.
		// Added the pageback parameter to inform the code that the '<- Special:LintErrors' link had been used to allow
		// the UI to redisplay with previous form values, instead of just resubmitting the query.
		if ( $subPage === null && isset( $params[ 'titlesearch' ] ) && !isset( $params[ 'pageback'] ) ) {
			unset( $params[ 'title' ] );
			$params = array_merge( [ 'pageback' => true ], $params );
			$out->addBacklinkSubtitle( $this->getPageTitle(), $params );

			$title = $request->getText( 'titlesearch' );
			$titleSearch = $this->cleanTitle( $title, $namespaces );

			if ( $titleSearch[ 'titlefield' ] !== null ) {
				$out->setPageTitleMsg( $this->msg( 'linter-prefix-search-subpage', $titleSearch[ 'titlefield' ] ) );

				$pager = new LintErrorsPager(
					$this->getContext(),
					$this->categoryManager,
					$this->linkCache,
					$this->getLinkRenderer(),
					$this->permissionManager,
					null,
					$namespaces,
					$exactMatch, $titleSearch[ 'titlefield' ], $template, $tag
				);
				$out->addParserOutput( $pager->getFullOutput() );
			} else {
				$this->displayError( $out, $titleSearch[ 'error' ] );
			}
			return;
		}

		if ( in_array( $subPage, array_merge(
			$this->categoryManager->getVisibleCategories(),
			$this->categoryManager->getInvisibleCategories()
		) ) ) {
			$this->category = $subPage;
		}

		if ( !$this->category ) {
			$this->addHelpLink( 'Help:Extension:Linter' );
			$this->showCategoryListings();
		} else {
			$this->addHelpLink( "Help:Lint_errors/{$this->category}" );
			$out->setPageTitleMsg(
				$this->msg( 'linterrors-subpage',
					$this->msg( "linter-category-{$this->category}" )->text()
				)
			);
			$out->addBacklinkSubtitle( $this->getPageTitle() );

			$title = $request->getText( 'titlecategorysearch' );
			// For category-based searches, allow an undefined title to display all records
			if ( $title === '' ) {
				$titleCategorySearch = [ 'titlefield' => '', 'namespace' => $namespaces, 'pageid' => null ];
			} else {
				$titleCategorySearch = $this->cleanTitle( $title, $namespaces );
			}

			if ( $titleCategorySearch[ 'titlefield' ] !== null ) {
				$this->showFilterForm( 'titlecategorysearch' );
				$pager = new LintErrorsPager(
					$this->getContext(),
					$this->categoryManager,
					$this->linkCache,
					$this->getLinkRenderer(),
					$this->permissionManager,
					$this->category,
					$namespaces,
					$exactMatch, $titleCategorySearch[ 'titlefield' ], $template, $tag
				);
				$out->addParserOutput( $pager->getFullOutput() );
			} else {
				$this->displayError( $out, $titleCategorySearch[ 'error' ] );
			}
		}
	}

	/**
	 * @param string $priority
	 * @param int[] $totals name => count
	 * @param string[] $categories
	 */
	private function displayList( $priority, $totals, array $categories ) {
		$out = $this->getOutput();
		$msgName = 'linter-heading-' . $priority . '-priority';
		$out->addHTML( Html::element( 'h2', [], $this->msg( $msgName )->text() ) );
		$out->addHTML( $this->buildCategoryList( $categories, $totals ) );
	}

	/**
	 */
	private function displaySearchPage() {
		$out = $this->getOutput();
		$out->addHTML( Html::element( 'h2', [],
			$this->msg( "linter-lints-prefix-search-page-desc" )->text() ) );
		$this->showFilterForm( 'titlesearch' );
	}

	private function showCategoryListings() {
		$totals = $this->totalsLookup->getTotals();

		// Display lint issues by priority
		$this->displayList( 'high', $totals, $this->categoryManager->getHighPriority() );
		$this->displayList( 'medium', $totals, $this->categoryManager->getMediumPriority() );
		$this->displayList( 'low', $totals, $this->categoryManager->getLowPriority() );

		$this->displaySearchPage();
	}

	/**
	 * @param string[] $cats
	 * @param int[] $totals name => count
	 * @return string
	 */
	private function buildCategoryList( array $cats, array $totals ) {
		$linkRenderer = $this->getLinkRenderer();
		$html = Html::openElement( 'ul' ) . "\n";
		foreach ( $cats as $cat ) {
			$html .= Html::rawElement( 'li', [], $linkRenderer->makeKnownLink(
				$this->getPageTitle( $cat ),
				$this->msg( "linter-category-$cat" )->text()
			) . ' ' . Html::element( 'bdi', [],
				$this->msg( "linter-numerrors" )->numParams( $totals[$cat] )->text()
			) ) . "\n";
		}
		$html .= Html::closeElement( 'ul' );

		return $html;
	}

	/** @inheritDoc */
	public function getGroupName() {
		return 'maintenance';
	}

	/**
	 * @return string[]
	 */
	protected function getSubpagesForPrefixSearch() {
		return $this->categoryManager->getVisibleCategories();
	}

}