| 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 |
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 |
|
| 46 |
private $categoryId; |
| 47 |
private array $namespaces; |
| 48 |
private bool $exactMatch; |
| 49 |
private string $title; |
| 50 |
private string $tag; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private string $throughTemplate; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 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 |
|
| 124 |
|
| 125 |
|
| 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 |
|
| 155 |
$queryBuilder->option( 'USE INDEX', [ 'linter' => 'linter_cat_page_position' ] ); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 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 |
|
| 176 |
public function isFieldSortable( $field ) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function formatValue( $name, $value ) { |
| 187 |
$row = $this->mCurrentRow; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
$category = $this->category; |
| 193 |
if ( $category === null ) { |
| 194 |
|
| 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 |
|
| 253 |
return Html::element( 'code', [], |
| 254 |
$lintError->params['ancestorName'] . " > " . $lintError->params['name'] ); |
| 255 |
} elseif ( $category === 'misc-tidy-replacement-issues' ) { |
| 256 |
|
| 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 '—'; |
| 270 |
} |
| 271 |
|
| 272 |
if ( isset( $lintError->templateInfo['multiPartTemplateBlock'] ) ) { |
| 273 |
return $this->msg( 'multi-part-template-block' )->escaped(); |
| 274 |
} else { |
| 275 |
|
| 276 |
$templateName = $lintError->templateInfo['name']; |
| 277 |
|
| 278 |
|
| 279 |
$templateTitle = Title::newFromText( $templateName ); |
| 280 |
if ( !$templateTitle ) { |
| 281 |
|
| 282 |
return '—'; |
| 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 |
|
| 297 |
public function getDefaultSort() { |
| 298 |
return 'linter_id'; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 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 |
} |