mediawiki-extensions-Linter/includes/CategoryManager.php
Kunal Mehta 3a8d3b9e03 Handle non-existent categories in the database better
If a newer version of MediaWiki gets rolled back, it's possible for
there to be lint entries in the database that don't exist according to
CategoryManager.

Instead of showing an error to the user, just silently hide those rows.

All callers to CategoryManager::getCategoryId() already check the
category exists. The callers for CategoryManager::getCategoryName() will
catch the MissingCategoryException, and log it if necessary. Notably
LinterError::makeLintError() will return false on invalid rows, and all
callers have been updated to handle that.

Bug: T179423
Change-Id: Ia5f56f18a51fa871511b02410222a6079efbfff6
2017-10-31 10:42:07 -07:00

175 lines
3.9 KiB
PHP

<?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;
/**
* Functions for lint error categories
*/
class CategoryManager {
const HIGH = 'high';
const MEDIUM = 'medium';
const LOW = 'low';
/**
* Map of category names to their hardcoded
* numerical ids for use in the database
*
* @var int[]
*/
private $categoryIds = [
'fostered' => 1,
'obsolete-tag' => 2,
'bogus-image-options' => 3,
'missing-end-tag' => 4,
'stripped-tag' => 5,
'self-closed-tag' => 6,
'deletable-table-tag' => 7,
'misnested-tag' => 8,
'pwrap-bug-workaround' => 9,
'tidy-whitespace-bug' => 10,
'multi-colon-escape' => 11,
'html5-misnesting' => 12,
'tidy-font-bug' => 13,
];
/**
* @var string[]
*/
private $categories = [
self::HIGH => [],
self::MEDIUM => [],
self::LOW => [],
];
/**
* @var string[]
*/
private $parserMigrationCategories = [];
public function __construct() {
global $wgLinterCategories;
foreach ( $wgLinterCategories as $name => $info ) {
if ( $info['enabled'] ) {
$this->categories[$info['priority']][] = $name;
}
if ( isset( $info['parser-migration'] ) ) {
$this->parserMigrationCategories[$name] = true;
}
}
sort( $this->categories[self::HIGH] );
sort( $this->categories[self::MEDIUM] );
sort( $this->categories[self::LOW] );
}
public function needsParserMigrationEdit( $name ) {
return isset( $this->parserMigrationCategories[$name] );
}
/**
* @return string[]
*/
public function getHighPriority() {
return $this->categories[self::HIGH];
}
/**
* @return string[]
*/
public function getMediumPriority() {
return $this->categories[self::MEDIUM];
}
/**
* @return string[]
*/
public function getLowPriority() {
return $this->categories[self::LOW];
}
/**
* Categories that are configured to be displayed to users
*
* @return string[]
*/
public function getVisibleCategories() {
return array_merge(
$this->categories[self::HIGH],
$this->categories[self::MEDIUM],
$this->categories[self::LOW]
);
}
/**
* Whether this category has a hardcoded id and can be
* inserted into the database
*
* @param string $name
* @return bool
*/
public function isKnownCategory( $name ) {
return isset( $this->categoryIds[$name] );
}
/**
* @param int $id
* @throws MissingCategoryException if we can't find the name for the id
* @return string
*/
public function getCategoryName( $id ) {
$flip = array_flip( $this->categoryIds );
if ( isset( $flip[$id] ) ) {
return $flip[$id];
}
throw new MissingCategoryException( "Could not find name for id $id" );
}
/**
* @param string[] $names
* @return int[]
*/
public function getCategoryIds( array $names ) {
$result = [];
foreach ( $names as $name ) {
$result[$name] = $this->getCategoryId( $name );
}
return $result;
}
/**
* Get the int id for the category in lint_categories table
*
* @param string $name
* @return int
* @throws MissingCategoryException if we can't find the id for the name
*/
public function getCategoryId( $name ) {
if ( isset( $this->categoryIds[$name] ) ) {
return $this->categoryIds[$name];
}
throw new MissingCategoryException( "Cannot find id for '$name'" );
}
}