Debian Codegraph @

extensions/Linter/includes/LintErrorsPager.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 use MediaWiki\Cache\LinkCache;
25 use MediaWiki\Context\IContextSource;
26 use MediaWiki\Html\Html;
27 use MediaWiki\Linker\LinkRenderer;
28 use MediaWiki\MediaWikiServices;
29 use MediaWiki\Pager\TablePager;
30 use MediaWiki\Permissions\PermissionManager;
31 use MediaWiki\Title\Title;
32 use MediaWiki\Title\TitleValue;
33 use Wikimedia\Rdbms\IExpression;
34 use Wikimedia\Rdbms\LikeValue;
35 use Wikimedia\Rdbms\SelectQueryBuilder;
36
37 class LintErrorsPager extends TablePager {
38
39 private CategoryManager $categoryManager;
40 private LinkCache $linkCache;
41 private LinkRenderer $linkRenderer;
42 private PermissionManager $permissionManager;
43
44 private ?string $category;
45 /** @var mixed */
46 private $categoryId;
47 private array $namespaces;
48 private bool $exactMatch;
49 private string $title;
50 private string $tag;
51
52 /**
53 * Allowed values are keys 'all', 'with' or 'without'
54 */
55 private string $throughTemplate;
56
57 /**
58 * @param IContextSource $context
59 * @param CategoryManager $categoryManager
60 * @param LinkCache $linkCache
61 * @param LinkRenderer $linkRenderer
62 * @param PermissionManager $permissionManager
63 * @param ?string $category
64 * @param array $namespaces
65 * @param bool $exactMatch
66 * @param string $title
67 * @param string $throughTemplate
68 * @param string $tag
69 */
70 public function __construct(
71 IContextSource $context,
72 CategoryManager $categoryManager,
73 LinkCache $linkCache,
74 LinkRenderer $linkRenderer,
75 PermissionManager $permissionManager,
76 ?string $category,
77 array $namespaces,
78 bool $exactMatch,
79 string $title,
80 string $throughTemplate,
81 string $tag
82 ) {
83 $this->categoryManager = $categoryManager;
84 $this->linkCache = $linkCache;
85 $this->linkRenderer = $linkRenderer;
86 $this->permissionManager = $permissionManager;
87
88 $this->category = $category;
89 if ( $category !== null ) {
90 $this->categoryId = $categoryManager->getCategoryId( $category );
91 } else {
92 $this->categoryId = array_values( $this->categoryManager->getCategoryIds(
93 $this->categoryManager->getVisibleCategories()
94 ) );
95 }
96
97 $this->namespaces = $namespaces;
98 $this->exactMatch = $exactMatch;
99 $this->title = $title;
100 $this->throughTemplate = $throughTemplate ?: 'all';
101 $this->tag = $tag ?: 'all';
102 parent::__construct( $context );
103 }
104
105 private function fillQueryBuilder( SelectQueryBuilder $queryBuilder ): void {
106 $mainConfig = MediaWikiServices::getInstance()->getMainConfig();
107 $queryBuilder
108 ->table( 'page' )
109 ->join( 'linter', null, 'page_id=linter_page' )
110 ->fields( LinkCache::getSelectFields() )
111 ->fields( [
112 'page_namespace', 'page_title',
113 'linter_id', 'linter_params',
114 'linter_start', 'linter_end',
115 'linter_cat'
116 ] )
117 ->where( [ 'linter_cat' => $this->categoryId ] );
118
119 $useIndex = false;
120
121 if ( $this->title !== '' ) {
122 $namespaces = $this->namespaces ?: [ NS_MAIN ];
123 // Specify page_namespace so that the index can be used (T360865)
124 // Also put a condition on linter_namespace, in case the DB
125 // decides to put the linter table first
126 $queryBuilder->where( [ 'page_namespace' => $namespaces, 'linter_namespace' => $namespaces ] );
127
128 if ( $this->exactMatch ) {
129 $queryBuilder->where( [
130 'page_title' => $this->title
131 ] );
132 } else {
133 $queryBuilder->where( $this->mDb->expr(
134 'page_title', IExpression::LIKE, new LikeValue( $this->title, $this->mDb->anyString() )
135 ) );
136 }
137 } elseif ( $this->namespaces ) {
138 $queryBuilder->where( [ 'linter_namespace' => $this->namespaces ] );
139 } else {
140 $useIndex = true;
141 }
142
143 if ( $this->throughTemplate !== 'all' ) {
144 $useIndex = false;
145 $op = ( $this->throughTemplate === 'with' ) ? '!=' : '=';
146 $queryBuilder->where( $this->mDb->expr( 'linter_template', $op, '' ) );
147 }
148 if ( $this->tag !== 'all' && ( new HtmlTags( $this ) )->checkAllowedHTMLTags( $this->tag ) ) {
149 $useIndex = false;
150 $queryBuilder->where( [ 'linter_tag' => $this->tag ] );
151 }
152
153 if ( $useIndex ) {
154 // T200517#10236299: Force the use of the category index
155 $queryBuilder->option( 'USE INDEX', [ 'linter' => 'linter_cat_page_position' ] );
156 }
157 }
158
159 /**
160 * @inheritDoc
161 */
162 public function getQueryInfo() {
163 $queryBuilder = $this->mDb->newSelectQueryBuilder();
164 $this->fillQueryBuilder( $queryBuilder );
165 return $queryBuilder->getQueryInfo();
166 }
167
168 protected function doBatchLookups() {
169 foreach ( $this->mResult as $row ) {
170 $titleValue = new TitleValue( (int)$row->page_namespace, $row->page_title );
171 $this->linkCache->addGoodLinkObjFromRow( $titleValue, $row );
172 }
173 }
174
175 /** @inheritDoc */
176 public function isFieldSortable( $field ) {
177 return false;
178 }
179
180 /**
181 * @param string $name
182 * @param string $value
183 * @return string
184 * @throws InvalidArgumentException
185 */
186 public function formatValue( $name, $value ) {
187 $row = $this->mCurrentRow;
188
189 // To support multiple lint errors of varying types for a single page, the
190 // category is set each time based on the category set in the lint error $row
191 // not by the class when lints are being reported by type for many pages
192 $category = $this->category;
193 if ( $category === null ) {
194 // Assert $row->linter_cat !== null ?
195 $category = $this->categoryManager->getCategoryName( $row->linter_cat );
196 } else {
197 $row->linter_cat = $this->categoryId;
198 }
199 $lintError = Database::makeLintError( $this->categoryManager, $row );
200
201 if ( !$lintError ) {
202 return '';
203 }
204
205 switch ( $name ) {
206 case 'title':
207 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
208 $viewLink = $this->linkRenderer->makeLink( $title );
209 $editMsgKey = $this->permissionManager->quickUserCan( 'edit', $this->getUser(), $title ) ?
210 'linter-page-edit' : 'linter-page-viewsource';
211 $editLink = $this->linkRenderer->makeLink(
212 $title,
213 $this->msg( $editMsgKey )->text(),
214 [],
215 [ 'action' => 'edit', 'lintid' => $lintError->lintId, ]
216 );
217
218 $historyLink = $this->linkRenderer->makeLink(
219 $title,
220 $this->msg( 'linter-page-history' )->text(),
221 [],
222 [ 'action' => 'history' ]
223 );
224
225 $editHistLinks = $this->getLanguage()->pipeList( [ $editLink, $historyLink ] );
226 return $this->msg( 'linter-page-title-edit' )
227 ->rawParams( $viewLink, $editHistLinks )
228 ->escaped();
229 case 'details':
230 if ( $category !== null && $this->categoryManager->hasNameParam( $category ) &&
231 isset( $lintError->params['name'] ) ) {
232 return Html::element( 'code', [], $lintError->params['name'] );
233 } elseif ( $category === 'bogus-image-options' && isset( $lintError->params['items'] ) ) {
234 $list = array_map( static function ( $in ) {
235 return Html::element( 'code', [], $in );
236 }, $lintError->params['items'] );
237 return $this->getLanguage()->commaList( $list );
238 } elseif ( $category === 'pwrap-bug-workaround' &&
239 isset( $lintError->params['root'] ) &&
240 isset( $lintError->params['child'] ) ) {
241 return Html::element( 'code', [],
242 $lintError->params['root'] . " > " . $lintError->params['child'] );
243 } elseif ( $category === 'tidy-whitespace-bug' &&
244 isset( $lintError->params['node'] ) &&
245 isset( $lintError->params['sibling'] ) ) {
246 return Html::element( 'code', [],
247 $lintError->params['node'] . " + " . $lintError->params['sibling'] );
248 } elseif ( $category === 'multi-colon-escape' &&
249 isset( $lintError->params['href'] ) ) {
250 return Html::element( 'code', [], $lintError->params['href'] );
251 } elseif ( $category === 'multiline-html-table-in-list' ) {
252 /* ancestor and name will be set */
253 return Html::element( 'code', [],
254 $lintError->params['ancestorName'] . " > " . $lintError->params['name'] );
255 } elseif ( $category === 'misc-tidy-replacement-issues' ) {
256 /* There will be a 'subtype' param to disambiguate */
257 return Html::element( 'code', [], $lintError->params['subtype'] );
258 } elseif ( $category === 'missing-image-alt-text' ) {
259 $title = Title::newFromText( $lintError->params['file'], NS_FILE );
260 return Html::element( 'a', [
261 'href' => $title->getLocalUrl(),
262 ], $title );
263 } elseif ( $category === 'duplicate-ids' ) {
264 return Html::element( 'code', [], $lintError->params['id'] );
265 }
266 return '';
267 case 'template':
268 if ( !$lintError->templateInfo ) {
269 return '&mdash;';
270 }
271
272 if ( isset( $lintError->templateInfo['multiPartTemplateBlock'] ) ) {
273 return $this->msg( 'multi-part-template-block' )->escaped();
274 } else {
275 // @phan-suppress-next-line PhanTypeArraySuspiciousNullable Null checked above
276 $templateName = $lintError->templateInfo['name'];
277 // Parsoid provides us with fully qualified template title
278 // So, fallback to the default main namespace
279 $templateTitle = Title::newFromText( $templateName );
280 if ( !$templateTitle ) {
281 // Shouldn't be possible...???
282 return '&mdash;';
283 }
284 }
285
286 return $this->linkRenderer->makeLink(
287 $templateTitle
288 );
289 case 'category':
290 return Html::element( 'code', [], $category ?? '' );
291 default:
292 throw new InvalidArgumentException( "Unexpected name: $name" );
293 }
294 }
295
296 /** @inheritDoc */
297 public function getDefaultSort() {
298 return 'linter_id';
299 }
300
301 /**
302 * @return string[]
303 */
304 public function getFieldNames() {
305 $names = [
306 'title' => $this->msg( 'linter-pager-title-header' )->text(),
307 ];
308 if ( !$this->category ) {
309 $names['category'] = $this->msg( 'linter-pager-category-header' )->text();
310 $names['details'] = $this->msg( "linter-pager-details-header" )->text();
311 } elseif ( !$this->categoryManager->hasNoParams( $this->category ) ) {
312 $names['details'] = $this->msg( "linter-pager-{$this->category}-details" )->text();
313 }
314 $names['template'] = $this->msg( "linter-pager-template-header" )->text();
315 return $names;
316 }
317 }