2016-10-29 01:06:08 +00:00
|
|
|
<?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;
|
|
|
|
|
2018-04-04 13:51:21 +00:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
2016-10-29 01:06:08 +00:00
|
|
|
/**
|
2024-04-04 22:55:43 +00:00
|
|
|
* CategoryManager services: functions for lint error categories.
|
2016-10-29 01:06:08 +00:00
|
|
|
*/
|
|
|
|
class CategoryManager {
|
|
|
|
|
2020-09-19 17:06:39 +00:00
|
|
|
private const HIGH = 'high';
|
|
|
|
private const MEDIUM = 'medium';
|
|
|
|
private const LOW = 'low';
|
2023-05-03 13:56:09 +00:00
|
|
|
private const NONE = 'none';
|
2016-12-15 01:07:40 +00:00
|
|
|
|
2016-10-29 01:06:08 +00:00
|
|
|
/**
|
2016-11-22 09:03:02 +00:00
|
|
|
* Map of category names to their hardcoded
|
|
|
|
* numerical ids for use in the database
|
|
|
|
*
|
|
|
|
* @var int[]
|
2016-10-29 01:06:08 +00:00
|
|
|
*/
|
2017-12-07 21:47:55 +00:00
|
|
|
private $categoryIds = [];
|
2016-11-22 09:03:02 +00:00
|
|
|
|
2016-12-15 01:07:40 +00:00
|
|
|
/**
|
2017-10-17 00:27:52 +00:00
|
|
|
* @var string[][]
|
2016-12-15 01:07:40 +00:00
|
|
|
*/
|
2017-04-26 22:11:23 +00:00
|
|
|
private $categories = [
|
|
|
|
self::HIGH => [],
|
|
|
|
self::MEDIUM => [],
|
|
|
|
self::LOW => [],
|
2023-05-03 13:56:09 +00:00
|
|
|
self::NONE => [],
|
2017-04-26 22:11:23 +00:00
|
|
|
];
|
2016-12-15 01:07:40 +00:00
|
|
|
|
2017-12-07 21:42:13 +00:00
|
|
|
/**
|
2019-12-17 05:56:43 +00:00
|
|
|
* @var bool[]
|
|
|
|
* @phan-var array<string,bool>
|
2017-12-07 21:42:13 +00:00
|
|
|
*/
|
|
|
|
private $hasNameParam = [];
|
|
|
|
|
2021-12-14 15:11:28 +00:00
|
|
|
/**
|
|
|
|
* @var bool[]
|
|
|
|
* @phan-var array<string,bool>
|
|
|
|
*/
|
|
|
|
private $hasNoParams = [];
|
|
|
|
|
2024-07-22 17:27:33 +00:00
|
|
|
/**
|
|
|
|
* @var bool[]
|
|
|
|
* @phan-var array<string,bool>
|
|
|
|
*/
|
|
|
|
private $isEnabled = [];
|
|
|
|
|
2024-04-04 22:55:43 +00:00
|
|
|
/**
|
|
|
|
* Do not instantiate directly: use MediaWikiServices to fetch.
|
|
|
|
* @param array $linterCategories
|
|
|
|
*/
|
|
|
|
public function __construct( array $linterCategories ) {
|
2022-08-03 16:49:19 +00:00
|
|
|
foreach ( $linterCategories as $name => $info ) {
|
2024-07-22 17:27:33 +00:00
|
|
|
$this->isEnabled[$name] = $info['enabled'];
|
2016-12-15 01:07:40 +00:00
|
|
|
if ( $info['enabled'] ) {
|
2017-04-26 22:11:23 +00:00
|
|
|
$this->categories[$info['priority']][] = $name;
|
2016-12-15 01:07:40 +00:00
|
|
|
}
|
2019-02-19 18:58:43 +00:00
|
|
|
if ( $info['has-name'] ?? false ) {
|
2017-12-07 21:42:13 +00:00
|
|
|
$this->hasNameParam[$name] = true;
|
|
|
|
}
|
2021-12-14 15:11:28 +00:00
|
|
|
if ( $info['no-params'] ?? false ) {
|
|
|
|
$this->hasNoParams[$name] = true;
|
|
|
|
}
|
2017-12-07 21:47:55 +00:00
|
|
|
if ( isset( $info['dbid'] ) ) {
|
|
|
|
if ( isset( $this->categoryIds[$name] ) ) {
|
|
|
|
throw new InvalidArgumentException( "duplicate ID: $name" );
|
|
|
|
}
|
|
|
|
$this->categoryIds[$name] = $info['dbid'];
|
|
|
|
}
|
2016-12-15 01:07:40 +00:00
|
|
|
}
|
2017-04-26 22:11:23 +00:00
|
|
|
|
|
|
|
sort( $this->categories[self::HIGH] );
|
|
|
|
sort( $this->categories[self::MEDIUM] );
|
|
|
|
sort( $this->categories[self::LOW] );
|
2023-05-03 13:56:09 +00:00
|
|
|
sort( $this->categories[self::NONE] );
|
2017-04-26 22:11:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-06 16:54:41 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-12-07 21:42:13 +00:00
|
|
|
public function hasNameParam( $name ) {
|
|
|
|
return isset( $this->hasNameParam[$name] );
|
|
|
|
}
|
|
|
|
|
2021-12-14 15:11:28 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasNoParams( $name ) {
|
|
|
|
return isset( $this->hasNoParams[$name] );
|
|
|
|
}
|
|
|
|
|
2024-07-22 17:27:33 +00:00
|
|
|
public function isEnabled( string $name ): bool {
|
|
|
|
// Default to true so !isKnownCategory aren't dropped
|
|
|
|
return $this->isEnabled[$name] ?? true;
|
|
|
|
}
|
|
|
|
|
2017-04-26 22:11:23 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getHighPriority() {
|
|
|
|
return $this->categories[self::HIGH];
|
2016-12-15 01:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2017-04-26 22:11:23 +00:00
|
|
|
public function getMediumPriority() {
|
|
|
|
return $this->categories[self::MEDIUM];
|
2016-12-15 01:07:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2017-04-26 22:11:23 +00:00
|
|
|
public function getLowPriority() {
|
|
|
|
return $this->categories[self::LOW];
|
2016-12-15 01:07:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 13:56:09 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNonePriority() {
|
|
|
|
return $this->categories[self::NONE];
|
|
|
|
}
|
|
|
|
|
2016-10-29 01:06:08 +00:00
|
|
|
/**
|
2017-04-26 22:11:23 +00:00
|
|
|
* Categories that are configured to be displayed to users
|
2016-11-22 09:03:02 +00:00
|
|
|
*
|
|
|
|
* @return string[]
|
2016-10-29 01:06:08 +00:00
|
|
|
*/
|
2016-11-22 09:03:02 +00:00
|
|
|
public function getVisibleCategories() {
|
2017-04-26 22:11:23 +00:00
|
|
|
return array_merge(
|
|
|
|
$this->categories[self::HIGH],
|
|
|
|
$this->categories[self::MEDIUM],
|
|
|
|
$this->categories[self::LOW]
|
|
|
|
);
|
2016-10-29 01:06:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-03 13:56:09 +00:00
|
|
|
/**
|
2024-03-14 01:55:06 +00:00
|
|
|
* Categories that are configured to not be displayed to users
|
2023-05-03 13:56:09 +00:00
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getInvisibleCategories() {
|
2024-03-14 01:55:06 +00:00
|
|
|
return $this->categories[self::NONE];
|
2023-05-03 13:56:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-29 01:06:08 +00:00
|
|
|
/**
|
2016-11-22 09:03:02 +00:00
|
|
|
* Whether this category has a hardcoded id and can be
|
|
|
|
* inserted into the database
|
|
|
|
*
|
2016-10-29 01:06:08 +00:00
|
|
|
* @param string $name
|
2016-11-22 09:03:02 +00:00
|
|
|
* @return bool
|
2016-10-29 01:06:08 +00:00
|
|
|
*/
|
2016-11-22 09:03:02 +00:00
|
|
|
public function isKnownCategory( $name ) {
|
|
|
|
return isset( $this->categoryIds[$name] );
|
2016-10-29 01:06:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-22 09:03:02 +00:00
|
|
|
* @param int $id
|
|
|
|
* @return string
|
2020-03-26 15:50:30 +00:00
|
|
|
* @throws MissingCategoryException if we can't find the name for the id
|
2016-10-29 01:06:08 +00:00
|
|
|
*/
|
|
|
|
public function getCategoryName( $id ) {
|
2016-11-22 09:03:02 +00:00
|
|
|
$flip = array_flip( $this->categoryIds );
|
2016-10-29 01:06:08 +00:00
|
|
|
if ( isset( $flip[$id] ) ) {
|
|
|
|
return $flip[$id];
|
|
|
|
}
|
|
|
|
|
2017-10-31 17:42:07 +00:00
|
|
|
throw new MissingCategoryException( "Could not find name for id $id" );
|
2016-10-29 01:06:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-22 09:03:02 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $names
|
|
|
|
* @return int[]
|
|
|
|
*/
|
2016-10-29 01:06:08 +00:00
|
|
|
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
|
2017-12-07 22:30:14 +00:00
|
|
|
* @param int|null $hint An optional hint, passed along from Parsoid.
|
|
|
|
* If the hint contains a suggested category ID but the Linter
|
|
|
|
* extension doesn't (yet) have one, use the ID from Parsoid's hint.
|
|
|
|
* This allows decoupling the Parsoid deploy of a new category
|
|
|
|
* from the corresponding Linter extension deploy.
|
2016-11-22 09:03:02 +00:00
|
|
|
* @return int
|
2017-10-31 17:42:07 +00:00
|
|
|
* @throws MissingCategoryException if we can't find the id for the name
|
2017-12-07 22:30:14 +00:00
|
|
|
* and there is no hint from Parsoid
|
2016-10-29 01:06:08 +00:00
|
|
|
*/
|
2017-12-07 22:30:14 +00:00
|
|
|
public function getCategoryId( $name, $hint = null ) {
|
2016-11-22 09:03:02 +00:00
|
|
|
if ( isset( $this->categoryIds[$name] ) ) {
|
|
|
|
return $this->categoryIds[$name];
|
2016-10-29 01:06:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-07 22:30:14 +00:00
|
|
|
// Use hint from Parsoid, if available.
|
|
|
|
if ( $hint !== null ) {
|
|
|
|
return $hint;
|
|
|
|
}
|
|
|
|
|
2017-10-31 17:42:07 +00:00
|
|
|
throw new MissingCategoryException( "Cannot find id for '$name'" );
|
2016-10-29 01:06:08 +00:00
|
|
|
}
|
|
|
|
}
|