| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Linter; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class CategoryManager { |
| 29 |
|
| 30 |
private const HIGH = 'high'; |
| 31 |
private const MEDIUM = 'medium'; |
| 32 |
private const LOW = 'low'; |
| 33 |
private const NONE = 'none'; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private $categoryIds = []; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private $categories = [ |
| 47 |
self::HIGH => [], |
| 48 |
self::MEDIUM => [], |
| 49 |
self::LOW => [], |
| 50 |
self::NONE => [], |
| 51 |
]; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private $hasNameParam = []; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private $hasNoParams = []; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private $isEnabled = []; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function __construct( array $linterCategories ) { |
| 76 |
foreach ( $linterCategories as $name => $info ) { |
| 77 |
$this->isEnabled[$name] = $info['enabled']; |
| 78 |
if ( $info['enabled'] ) { |
| 79 |
$this->categories[$info['priority']][] = $name; |
| 80 |
} |
| 81 |
if ( $info['has-name'] ?? false ) { |
| 82 |
$this->hasNameParam[$name] = true; |
| 83 |
} |
| 84 |
if ( $info['no-params'] ?? false ) { |
| 85 |
$this->hasNoParams[$name] = true; |
| 86 |
} |
| 87 |
if ( isset( $info['dbid'] ) ) { |
| 88 |
if ( isset( $this->categoryIds[$name] ) ) { |
| 89 |
throw new InvalidArgumentException( "duplicate ID: $name" ); |
| 90 |
} |
| 91 |
$this->categoryIds[$name] = $info['dbid']; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
sort( $this->categories[self::HIGH] ); |
| 96 |
sort( $this->categories[self::MEDIUM] ); |
| 97 |
sort( $this->categories[self::LOW] ); |
| 98 |
sort( $this->categories[self::NONE] ); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function hasNameParam( $name ) { |
| 106 |
return isset( $this->hasNameParam[$name] ); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function hasNoParams( $name ) { |
| 114 |
return isset( $this->hasNoParams[$name] ); |
| 115 |
} |
| 116 |
|
| 117 |
public function isEnabled( string $name ): bool { |
| 118 |
|
| 119 |
return $this->isEnabled[$name] ?? true; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function getHighPriority() { |
| 126 |
return $this->categories[self::HIGH]; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function getMediumPriority() { |
| 133 |
return $this->categories[self::MEDIUM]; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function getLowPriority() { |
| 140 |
return $this->categories[self::LOW]; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function getNonePriority() { |
| 147 |
return $this->categories[self::NONE]; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function getVisibleCategories() { |
| 156 |
return array_merge( |
| 157 |
$this->categories[self::HIGH], |
| 158 |
$this->categories[self::MEDIUM], |
| 159 |
$this->categories[self::LOW] |
| 160 |
); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
public function getInvisibleCategories() { |
| 169 |
return $this->categories[self::NONE]; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public function isKnownCategory( $name ) { |
| 180 |
return isset( $this->categoryIds[$name] ); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public function getCategoryName( $id ) { |
| 189 |
$flip = array_flip( $this->categoryIds ); |
| 190 |
if ( isset( $flip[$id] ) ) { |
| 191 |
return $flip[$id]; |
| 192 |
} |
| 193 |
|
| 194 |
throw new MissingCategoryException( "Could not find name for id $id" ); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public function getCategoryIds( array $names ) { |
| 202 |
$result = []; |
| 203 |
foreach ( $names as $name ) { |
| 204 |
$result[$name] = $this->getCategoryId( $name ); |
| 205 |
} |
| 206 |
|
| 207 |
return $result; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public function getCategoryId( $name, $hint = null ) { |
| 224 |
if ( isset( $this->categoryIds[$name] ) ) { |
| 225 |
return $this->categoryIds[$name]; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
if ( $hint !== null ) { |
| 230 |
return $hint; |
| 231 |
} |
| 232 |
|
| 233 |
throw new MissingCategoryException( "Cannot find id for '$name'" ); |
| 234 |
} |
| 235 |
} |