Debian Codegraph @

extensions/Linter/includes/CategoryManager.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Linter;
22
23 use InvalidArgumentException;
24
25 /**
26 * CategoryManager services: functions for lint error categories.
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 * Map of category names to their hardcoded
37 * numerical ids for use in the database
38 *
39 * @var int[]
40 */
41 private $categoryIds = [];
42
43 /**
44 * @var string[][]
45 */
46 private $categories = [
47 self::HIGH => [],
48 self::MEDIUM => [],
49 self::LOW => [],
50 self::NONE => [],
51 ];
52
53 /**
54 * @var bool[]
55 * @phan-var array<string,bool>
56 */
57 private $hasNameParam = [];
58
59 /**
60 * @var bool[]
61 * @phan-var array<string,bool>
62 */
63 private $hasNoParams = [];
64
65 /**
66 * @var bool[]
67 * @phan-var array<string,bool>
68 */
69 private $isEnabled = [];
70
71 /**
72 * Do not instantiate directly: use MediaWikiServices to fetch.
73 * @param array $linterCategories
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 * @param string $name
103 * @return bool
104 */
105 public function hasNameParam( $name ) {
106 return isset( $this->hasNameParam[$name] );
107 }
108
109 /**
110 * @param string $name
111 * @return bool
112 */
113 public function hasNoParams( $name ) {
114 return isset( $this->hasNoParams[$name] );
115 }
116
117 public function isEnabled( string $name ): bool {
118 // Default to true so !isKnownCategory aren't dropped
119 return $this->isEnabled[$name] ?? true;
120 }
121
122 /**
123 * @return string[]
124 */
125 public function getHighPriority() {
126 return $this->categories[self::HIGH];
127 }
128
129 /**
130 * @return string[]
131 */
132 public function getMediumPriority() {
133 return $this->categories[self::MEDIUM];
134 }
135
136 /**
137 * @return string[]
138 */
139 public function getLowPriority() {
140 return $this->categories[self::LOW];
141 }
142
143 /**
144 * @return string[]
145 */
146 public function getNonePriority() {
147 return $this->categories[self::NONE];
148 }
149
150 /**
151 * Categories that are configured to be displayed to users
152 *
153 * @return string[]
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 * Categories that are configured to not be displayed to users
165 *
166 * @return string[]
167 */
168 public function getInvisibleCategories() {
169 return $this->categories[self::NONE];
170 }
171
172 /**
173 * Whether this category has a hardcoded id and can be
174 * inserted into the database
175 *
176 * @param string $name
177 * @return bool
178 */
179 public function isKnownCategory( $name ) {
180 return isset( $this->categoryIds[$name] );
181 }
182
183 /**
184 * @param int $id
185 * @return string
186 * @throws MissingCategoryException if we can't find the name for the id
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 * @param string[] $names
199 * @return int[]
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 * Get the int id for the category in lint_categories table
212 *
213 * @param string $name
214 * @param int|null $hint An optional hint, passed along from Parsoid.
215 * If the hint contains a suggested category ID but the Linter
216 * extension doesn't (yet) have one, use the ID from Parsoid's hint.
217 * This allows decoupling the Parsoid deploy of a new category
218 * from the corresponding Linter extension deploy.
219 * @return int
220 * @throws MissingCategoryException if we can't find the id for the name
221 * and there is no hint from Parsoid
222 */
223 public function getCategoryId( $name, $hint = null ) {
224 if ( isset( $this->categoryIds[$name] ) ) {
225 return $this->categoryIds[$name];
226 }
227
228 // Use hint from Parsoid, if available.
229 if ( $hint !== null ) {
230 return $hint;
231 }
232
233 throw new MissingCategoryException( "Cannot find id for '$name'" );
234 }
235 }