mediawiki-extensions-Linter/includes/CategoryManager.php
Kunal Mehta b41e74ce8b Hardcode category ids
Instead of having a complex auto-increment category manager that had
additional caching, just hardcode the (currently 6) ids for each
category, which allows us to simplify a lot of code.

If Parsoid sends a lint error in a category that we don't know about, it
is silently dropped.

Bug: T151287
Change-Id: Ice6edf1b7985390aa0c1c410d357bc565bb69108
2016-11-22 18:31:17 -08:00

106 lines
2.5 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 {
/**
* 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,
];
/**
* Categories that are configure to be displayed to users
*
* @return string[]
*/
public function getVisibleCategories() {
global $wgLinterCategories;
return array_keys( array_filter( $wgLinterCategories ) );
}
/**
* 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 \RuntimeException 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 \RuntimeException( "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 \RuntimeException 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 \RuntimeException( "Cannot find id for '$name'" );
}
}