From f3778856f22bf018cc08d858082f47c891c98959 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Wed, 14 Dec 2016 17:07:40 -0800 Subject: [PATCH] Add severity for error categories Categories can now have a severity level of "error" or "warning" designated, which places them in a different heading on Special:LintErrors. Bug: T152822 Change-Id: I1276b9502d90765e88dcb8ea78569dee910c5d88 --- extension.json | 30 ++++++++++++++++++----- i18n/en.json | 2 ++ i18n/qqq.json | 2 ++ includes/CategoryManager.php | 45 ++++++++++++++++++++++++++++++++-- includes/LintErrorsPager.php | 6 +++-- includes/SpecialLintErrors.php | 31 +++++++++++++++++------ 6 files changed, 98 insertions(+), 18 deletions(-) diff --git a/extension.json b/extension.json index 42b7614b..09969327 100644 --- a/extension.json +++ b/extension.json @@ -55,12 +55,30 @@ }, "config": { "LinterCategories": { - "fostered": true, - "obsolete-tag": true, - "bogus-image-options": true, - "missing-end-tag": true, - "stripped-tag": true, - "self-closed-tag": true + "fostered": { + "severity": "error", + "enabled": true + }, + "obsolete-tag": { + "severity": "warning", + "enabled": true + }, + "bogus-image-options": { + "severity": "error", + "enabled": true + }, + "missing-end-tag": { + "severity": "error", + "enabled": true + }, + "stripped-tag": { + "severity": "error", + "enabled": true + }, + "self-closed-tag": { + "severity": "warning", + "enabled": true + } }, "LinterSubmitterWhitelist": { "127.0.0.1": true diff --git a/i18n/en.json b/i18n/en.json index bc784f98..5ab4f959 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -29,6 +29,8 @@ "linter-numerrors": "($1 {{PLURAL:$1|error|errors}})", "linker-page-title-edit": "$1 ($2)", "linker-page-edit": "edit", + "linter-heading-errors": "Errors", + "linter-heading-warnings": "Warnings", "apihelp-query+linterrors-description": "Get a list of lint errors", "apihelp-query+linterrors-param-categories": "Categories of lint errors", "apihelp-query+linterrors-param-limit": "Number of results to query", diff --git a/i18n/qqq.json b/i18n/qqq.json index be13a9d2..750c46b9 100644 --- a/i18n/qqq.json +++ b/i18n/qqq.json @@ -31,6 +31,8 @@ "linter-numerrors": "Shown after a category link to indicate how many errors are in that category. $1 is the number of errors, and can be used for PLURAL.\n{{Identical|Error}}", "linker-page-title-edit": "Used in a table cell. $1 is a link to the page, $2 is a link to edit that page, the link text is {{msg-mw|linker-page-edit}}", "linker-page-edit": "Link text for edit link in {{msg-mw|linker-page-title-edit}} and {{msg-mw|linker-page-title-edit-template}}\n{{Identical|Edit}}", + "linter-heading-errors": "Heading on [[Special:LintErrors]]", + "linter-heading-warnings": "Heading on [[Special:LintErrors]]", "apihelp-query+linterrors-description": "{{doc-apihelp-description|query+linterrors}}", "apihelp-query+linterrors-param-categories": "{{doc-apihelp-param|query+linterrors|categories}}", "apihelp-query+linterrors-param-limit": "{{doc-apihelp-param|query+linterrors|limit}}", diff --git a/includes/CategoryManager.php b/includes/CategoryManager.php index 97c30c9b..ac3107c2 100644 --- a/includes/CategoryManager.php +++ b/includes/CategoryManager.php @@ -25,6 +25,9 @@ namespace MediaWiki\Linter; */ class CategoryManager { + const ERROR = 'error'; + const WARNING = 'warning'; + /** * Map of category names to their hardcoded * numerical ids for use in the database @@ -40,14 +43,52 @@ class CategoryManager { 'self-closed-tag' => 6, ]; + /** + * @var string[] + */ + private $errors = []; + + /** + * @var string[] + */ + private $warnings = []; + + public function __construct() { + global $wgLinterCategories; + foreach ( $wgLinterCategories as $name => $info ) { + if ( $info['enabled'] ) { + if ( $info['severity'] === self::ERROR ) { + $this->errors[] = $name; + } elseif ( $info['severity'] === self::WARNING ) { + $this->warnings[] = $name; + } + } + } + sort( $this->errors ); + sort( $this->warnings ); + } + + /** + * @return string[] + */ + public function getErrors() { + return $this->errors; + } + + /** + * @return string[] + */ + public function getWarnings() { + return $this->warnings; + } + /** * Categories that are configure to be displayed to users * * @return string[] */ public function getVisibleCategories() { - global $wgLinterCategories; - return array_keys( array_filter( $wgLinterCategories ) ); + return array_merge( $this->errors, $this->warnings ); } /** diff --git a/includes/LintErrorsPager.php b/includes/LintErrorsPager.php index e4c447cd..e4b49f1a 100644 --- a/includes/LintErrorsPager.php +++ b/includes/LintErrorsPager.php @@ -41,9 +41,11 @@ class LintErrorsPager extends TablePager { */ private $linkRenderer; - public function __construct( IContextSource $context, $category, LinkRenderer $linkRenderer ) { + public function __construct( IContextSource $context, $category, LinkRenderer $linkRenderer, + CategoryManager $catManager + ) { $this->category = $category; - $this->categoryId = ( new CategoryManager() )->getCategoryId( $this->category ); + $this->categoryId = $catManager->getCategoryId( $this->category ); $this->linkRenderer = $linkRenderer; parent::__construct( $context ); } diff --git a/includes/SpecialLintErrors.php b/includes/SpecialLintErrors.php index b2695a8a..f13e7652 100644 --- a/includes/SpecialLintErrors.php +++ b/includes/SpecialLintErrors.php @@ -34,14 +34,14 @@ class SpecialLintErrors extends SpecialPage { public function execute( $par ) { $this->setHeaders(); $this->outputHeader(); - $cats = ( new CategoryManager() )->getVisibleCategories(); - if ( in_array( $par, $cats ) ) { + $catManager = new CategoryManager(); + if ( in_array( $par, $catManager->getVisibleCategories() ) ) { $this->category = $par; } if ( !$this->category ) { $this->addHelpLink( 'Help:Extension:Linter' ); - $this->showCategoryList( $cats ); + $this->showCategoryListings( $catManager ); } else { $this->addHelpLink( "Help:Extension:Linter/{$this->category}" ); $out = $this->getOutput(); @@ -53,17 +53,31 @@ class SpecialLintErrors extends SpecialPage { $out->addBacklinkSubtitle( $this->getPageTitle() ); $out->addWikiMsg( "linter-category-{$this->category}-desc" ); $pager = new LintErrorsPager( - $this->getContext(), $this->category, $this->getLinkRenderer() + $this->getContext(), $this->category, $this->getLinkRenderer(), + $catManager ); $out->addParserOutput( $pager->getFullOutput() ); } } - private function showCategoryList( array $cats ) { + private function showCategoryListings( CategoryManager $catManager ) { + $totals = ( new Database( 0 ) )->getTotals(); + + $out = $this->getOutput(); + $out->addHTML( Html::element( 'h2', [], $this->msg( 'linter-heading-errors' )->text() ) ); + $out->addHTML( $this->buildCategoryList( $catManager->getErrors(), $totals ) ); + $out->addHTML( Html::element( 'h2', [], $this->msg( 'linter-heading-warnings' )->text() ) ); + $out->addHTML( $this->buildCategoryList( $catManager->getWarnings(), $totals ) ); + } + + /** + * @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"; - sort( $cats ); - $totals = ( new Database( 0 ) )->getTotals(); foreach ( $cats as $cat ) { $html .= Html::rawElement( 'li', [], $linkRenderer->makeKnownLink( $this->getPageTitle( $cat ), @@ -71,7 +85,8 @@ class SpecialLintErrors extends SpecialPage { ) . ' ' . $this->msg( "linter-numerrors" )->numParams( $totals[$cat] )->escaped() ) . "\n"; } $html .= Html::closeElement( 'ul' ); - $this->getOutput()->addHTML( $html ); + + return $html; } public function getGroupName() {