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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Functions for lint error categories
|
|
|
|
*/
|
|
|
|
class CategoryManager {
|
|
|
|
|
2017-04-26 22:11:23 +00:00
|
|
|
const HIGH = 'high';
|
|
|
|
const MEDIUM = 'medium';
|
|
|
|
const LOW = 'low';
|
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
|
|
|
*/
|
2016-11-22 09:03:02 +00:00
|
|
|
private $categoryIds = [
|
|
|
|
'fostered' => 1,
|
|
|
|
'obsolete-tag' => 2,
|
|
|
|
'bogus-image-options' => 3,
|
|
|
|
'missing-end-tag' => 4,
|
|
|
|
'stripped-tag' => 5,
|
|
|
|
'self-closed-tag' => 6,
|
2017-04-06 17:41:18 +00:00
|
|
|
'deletable-table-tag' => 7,
|
2017-04-10 21:05:03 +00:00
|
|
|
'misnested-tag' => 8,
|
2017-04-12 21:27:41 +00:00
|
|
|
'pwrap-bug-workaround' => 9,
|
2017-09-18 18:03:11 +00:00
|
|
|
'tidy-whitespace-bug' => 10,
|
|
|
|
'multi-colon-escape' => 11,
|
2017-09-22 21:35:06 +00:00
|
|
|
'html5-misnesting' => 12,
|
2017-10-23 21:38:19 +00:00
|
|
|
'tidy-font-bug' => 13,
|
2017-12-07 21:02:21 +00:00
|
|
|
'multiple-unclosed-formatting-tags' => 14,
|
|
|
|
'unclosed-quotes-in-heading' => 15,
|
|
|
|
'multiline-html-table-in-list' => 16,
|
|
|
|
'misc-tidy-replacement-issues' => 17,
|
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 => [],
|
|
|
|
];
|
2016-12-15 01:07:40 +00:00
|
|
|
|
2017-07-05 19:56:20 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $parserMigrationCategories = [];
|
|
|
|
|
2017-12-07 21:42:13 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
private $hasNameParam = [];
|
|
|
|
|
2016-12-15 01:07:40 +00:00
|
|
|
public function __construct() {
|
|
|
|
global $wgLinterCategories;
|
|
|
|
foreach ( $wgLinterCategories as $name => $info ) {
|
|
|
|
if ( $info['enabled'] ) {
|
2017-04-26 22:11:23 +00:00
|
|
|
$this->categories[$info['priority']][] = $name;
|
2016-12-15 01:07:40 +00:00
|
|
|
}
|
2017-07-05 19:56:20 +00:00
|
|
|
if ( isset( $info['parser-migration'] ) ) {
|
|
|
|
$this->parserMigrationCategories[$name] = true;
|
|
|
|
}
|
2017-12-07 21:42:13 +00:00
|
|
|
if ( isset( $info['has-name'] ) ) {
|
|
|
|
$this->hasNameParam[$name] = true;
|
|
|
|
}
|
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] );
|
|
|
|
}
|
|
|
|
|
2017-07-05 19:56:20 +00:00
|
|
|
public function needsParserMigrationEdit( $name ) {
|
|
|
|
return isset( $this->parserMigrationCategories[$name] );
|
|
|
|
}
|
|
|
|
|
2017-12-07 21:42:13 +00:00
|
|
|
public function hasNameParam( $name ) {
|
|
|
|
return isset( $this->hasNameParam[$name] );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
2017-10-31 17:42:07 +00:00
|
|
|
* @throws MissingCategoryException if we can't find the name for the id
|
2016-11-22 09:03:02 +00:00
|
|
|
* @return string
|
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
|
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
|
2016-10-29 01:06:08 +00:00
|
|
|
*/
|
2016-11-22 09:03:02 +00:00
|
|
|
public function getCategoryId( $name ) {
|
|
|
|
if ( isset( $this->categoryIds[$name] ) ) {
|
|
|
|
return $this->categoryIds[$name];
|
2016-10-29 01:06:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 17:42:07 +00:00
|
|
|
throw new MissingCategoryException( "Cannot find id for '$name'" );
|
2016-10-29 01:06:08 +00:00
|
|
|
}
|
|
|
|
}
|