2016-10-13 08:14:18 +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;
|
|
|
|
|
|
|
|
use IContextSource;
|
|
|
|
use InvalidArgumentException;
|
2024-04-05 02:41:38 +00:00
|
|
|
use MediaWiki\Cache\LinkCache;
|
2024-01-06 16:03:30 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2016-10-13 08:14:18 +00:00
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
|
|
|
use MediaWiki\MediaWikiServices;
|
2024-01-06 16:03:30 +00:00
|
|
|
use MediaWiki\Pager\TablePager;
|
2024-04-05 02:41:38 +00:00
|
|
|
use MediaWiki\Permissions\PermissionManager;
|
2023-08-19 04:17:17 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2024-01-06 16:03:30 +00:00
|
|
|
use MediaWiki\Title\TitleValue;
|
2023-12-11 14:52:27 +00:00
|
|
|
use Wikimedia\Rdbms\IExpression;
|
|
|
|
use Wikimedia\Rdbms\LikeValue;
|
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
2016-10-13 08:14:18 +00:00
|
|
|
|
|
|
|
class LintErrorsPager extends TablePager {
|
|
|
|
|
2024-04-05 02:41:38 +00:00
|
|
|
private CategoryManager $categoryManager;
|
|
|
|
private LinkCache $linkCache;
|
|
|
|
private LinkRenderer $linkRenderer;
|
|
|
|
private PermissionManager $permissionManager;
|
2017-07-05 19:56:20 +00:00
|
|
|
|
2024-04-05 02:41:38 +00:00
|
|
|
private ?string $category;
|
2024-04-18 23:27:49 +00:00
|
|
|
/** @var mixed */
|
|
|
|
private $categoryId;
|
2024-04-05 02:41:38 +00:00
|
|
|
private array $namespaces;
|
|
|
|
private bool $exactMatch;
|
|
|
|
private string $title;
|
|
|
|
private string $tag;
|
2022-03-04 20:21:04 +00:00
|
|
|
|
2022-08-25 20:15:57 +00:00
|
|
|
/**
|
|
|
|
* Allowed values are keys 'all', 'with' or 'without'
|
|
|
|
*/
|
2024-04-05 02:41:38 +00:00
|
|
|
private string $throughTemplate;
|
2022-08-25 20:15:57 +00:00
|
|
|
|
2021-04-06 16:54:41 +00:00
|
|
|
/**
|
|
|
|
* @param IContextSource $context
|
2024-04-05 02:41:38 +00:00
|
|
|
* @param CategoryManager $categoryManager
|
|
|
|
* @param LinkCache $linkCache
|
2021-04-06 16:54:41 +00:00
|
|
|
* @param LinkRenderer $linkRenderer
|
2024-04-05 02:41:38 +00:00
|
|
|
* @param PermissionManager $permissionManager
|
|
|
|
* @param ?string $category
|
2023-06-13 22:26:52 +00:00
|
|
|
* @param array $namespaces
|
2022-03-10 19:48:19 +00:00
|
|
|
* @param bool $exactMatch
|
|
|
|
* @param string $title
|
2022-08-25 20:15:57 +00:00
|
|
|
* @param string $throughTemplate
|
|
|
|
* @param string $tag
|
2021-04-06 16:54:41 +00:00
|
|
|
*/
|
2024-04-05 02:41:38 +00:00
|
|
|
public function __construct(
|
|
|
|
IContextSource $context,
|
|
|
|
CategoryManager $categoryManager,
|
|
|
|
LinkCache $linkCache,
|
|
|
|
LinkRenderer $linkRenderer,
|
|
|
|
PermissionManager $permissionManager,
|
|
|
|
?string $category,
|
|
|
|
array $namespaces,
|
|
|
|
bool $exactMatch,
|
|
|
|
string $title,
|
|
|
|
string $throughTemplate,
|
|
|
|
string $tag
|
2016-12-15 01:07:40 +00:00
|
|
|
) {
|
2024-04-05 02:41:38 +00:00
|
|
|
$this->categoryManager = $categoryManager;
|
|
|
|
$this->linkCache = $linkCache;
|
|
|
|
$this->linkRenderer = $linkRenderer;
|
|
|
|
$this->permissionManager = $permissionManager;
|
|
|
|
|
2016-10-13 08:14:18 +00:00
|
|
|
$this->category = $category;
|
2021-09-30 23:20:01 +00:00
|
|
|
if ( $category !== null ) {
|
2024-04-05 02:41:38 +00:00
|
|
|
$this->categoryId = $categoryManager->getCategoryId( $category );
|
2021-09-30 23:20:01 +00:00
|
|
|
} else {
|
2024-04-18 23:27:49 +00:00
|
|
|
$this->categoryId = array_values( $this->categoryManager->getCategoryIds(
|
|
|
|
$this->categoryManager->getVisibleCategories()
|
|
|
|
) );
|
2021-09-30 23:20:01 +00:00
|
|
|
}
|
2024-04-18 23:27:49 +00:00
|
|
|
|
2023-06-13 22:26:52 +00:00
|
|
|
$this->namespaces = $namespaces;
|
2022-03-10 19:48:19 +00:00
|
|
|
$this->exactMatch = $exactMatch;
|
|
|
|
$this->title = $title;
|
2024-02-10 10:53:17 +00:00
|
|
|
$this->throughTemplate = $throughTemplate ?: 'all';
|
|
|
|
$this->tag = $tag ?: 'all';
|
2016-10-13 08:14:18 +00:00
|
|
|
parent::__construct( $context );
|
|
|
|
}
|
|
|
|
|
2023-12-11 14:52:27 +00:00
|
|
|
private function fillQueryBuilder( SelectQueryBuilder $queryBuilder ): void {
|
|
|
|
$mainConfig = MediaWikiServices::getInstance()->getMainConfig();
|
|
|
|
$queryBuilder
|
2024-03-25 05:13:49 +00:00
|
|
|
->table( 'page' )
|
|
|
|
->join( 'linter', null, 'page_id=linter_page' )
|
2023-12-11 14:52:27 +00:00
|
|
|
->fields( LinkCache::getSelectFields() )
|
|
|
|
->fields( [
|
|
|
|
'page_namespace', 'page_title',
|
|
|
|
'linter_id', 'linter_params',
|
|
|
|
'linter_start', 'linter_end',
|
|
|
|
'linter_cat'
|
2024-04-18 23:27:49 +00:00
|
|
|
] )
|
|
|
|
->where( [ 'linter_cat' => $this->categoryId ] );
|
2023-06-13 22:26:52 +00:00
|
|
|
|
2024-03-25 05:13:49 +00:00
|
|
|
if ( $this->title !== '' ) {
|
|
|
|
$namespaces = $this->namespaces ?: [ NS_MAIN ];
|
|
|
|
// Specify page_namespace so that the index can be used (T360865)
|
|
|
|
$queryBuilder->where( [ 'page_namespace' => $namespaces ] );
|
|
|
|
if ( $mainConfig->get( 'LinterUseNamespaceColumnStage' ) ) {
|
|
|
|
// Also put a condition on linter_namespace, in case the DB
|
|
|
|
// decides to put the linter table first
|
|
|
|
$queryBuilder->where( [ 'linter_namespace' => $namespaces ] );
|
2023-12-21 10:35:17 +00:00
|
|
|
}
|
2024-03-25 05:13:49 +00:00
|
|
|
if ( $this->exactMatch ) {
|
|
|
|
$queryBuilder->where( [
|
|
|
|
'page_title' => $this->title
|
|
|
|
] );
|
|
|
|
} else {
|
|
|
|
$queryBuilder->where( $this->mDb->expr(
|
|
|
|
'page_title', IExpression::LIKE, new LikeValue( $this->title, $this->mDb->anyString() )
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
} elseif ( $this->namespaces ) {
|
|
|
|
$namespaceCol = $mainConfig->get( 'LinterUseNamespaceColumnStage' )
|
|
|
|
? "linter_namespace" : "page_namespace";
|
|
|
|
$queryBuilder->where( [ $namespaceCol => $this->namespaces ] );
|
2022-03-04 20:21:04 +00:00
|
|
|
}
|
2024-03-25 05:13:49 +00:00
|
|
|
|
2023-12-11 14:52:27 +00:00
|
|
|
if ( $mainConfig->get( 'LinterUserInterfaceTagAndTemplateStage' ) ) {
|
|
|
|
if ( $this->throughTemplate !== 'all' ) {
|
|
|
|
$op = ( $this->throughTemplate === 'with' ) ? '!=' : '=';
|
|
|
|
$queryBuilder->where( $this->mDb->expr( 'linter_template', $op, '' ) );
|
2022-08-25 20:15:57 +00:00
|
|
|
}
|
2023-12-11 14:52:27 +00:00
|
|
|
if ( $this->tag !== 'all' && ( new HtmlTags( $this ) )->checkAllowedHTMLTags( $this->tag ) ) {
|
|
|
|
$queryBuilder->where( [ 'linter_tag' => $this->tag ] );
|
2022-08-25 20:15:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-12-11 14:52:27 +00:00
|
|
|
}
|
2022-08-25 20:15:57 +00:00
|
|
|
|
2023-12-11 14:52:27 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getQueryInfo() {
|
|
|
|
$queryBuilder = $this->mDb->newSelectQueryBuilder();
|
|
|
|
$this->fillQueryBuilder( $queryBuilder );
|
|
|
|
return $queryBuilder->getQueryInfo();
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function doBatchLookups() {
|
|
|
|
foreach ( $this->mResult as $row ) {
|
|
|
|
$titleValue = new TitleValue( (int)$row->page_namespace, $row->page_title );
|
2024-04-05 02:41:38 +00:00
|
|
|
$this->linkCache->addGoodLinkObjFromRow( $titleValue, $row );
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 16:54:41 +00:00
|
|
|
/** @inheritDoc */
|
2016-10-13 08:14:18 +00:00
|
|
|
public function isFieldSortable( $field ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-06 16:54:41 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
* @return string
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
2016-10-13 08:14:18 +00:00
|
|
|
public function formatValue( $name, $value ) {
|
|
|
|
$row = $this->mCurrentRow;
|
2021-09-30 23:20:01 +00:00
|
|
|
|
|
|
|
// To support multiple lint errors of varying types for a single page, the
|
|
|
|
// category is set each time based on the category set in the lint error $row
|
|
|
|
// not by the class when lints are being reported by type for many pages
|
2024-04-05 02:41:38 +00:00
|
|
|
$category = $this->category;
|
2024-04-18 23:27:49 +00:00
|
|
|
if ( $category === null ) {
|
|
|
|
// Assert $row->linter_cat !== null ?
|
2021-09-30 23:20:01 +00:00
|
|
|
$category = $this->categoryManager->getCategoryName( $row->linter_cat );
|
|
|
|
} else {
|
|
|
|
$row->linter_cat = $this->categoryId;
|
|
|
|
}
|
2024-04-04 22:55:43 +00:00
|
|
|
$lintError = Database::makeLintError( $this->categoryManager, $row );
|
2021-09-30 23:20:01 +00:00
|
|
|
|
2017-10-31 17:42:07 +00:00
|
|
|
if ( !$lintError ) {
|
|
|
|
return '';
|
|
|
|
}
|
2022-03-10 19:48:19 +00:00
|
|
|
|
2016-10-13 08:14:18 +00:00
|
|
|
switch ( $name ) {
|
|
|
|
case 'title':
|
|
|
|
$title = Title::makeTitle( $row->page_namespace, $row->page_title );
|
|
|
|
$viewLink = $this->linkRenderer->makeLink( $title );
|
2024-04-05 02:41:38 +00:00
|
|
|
$editMsgKey = $this->permissionManager->quickUserCan( 'edit', $this->getUser(), $title ) ?
|
2017-11-25 04:38:25 +00:00
|
|
|
'linter-page-edit' : 'linter-page-viewsource';
|
2016-10-13 08:14:18 +00:00
|
|
|
$editLink = $this->linkRenderer->makeLink(
|
|
|
|
$title,
|
2017-11-25 04:35:15 +00:00
|
|
|
$this->msg( $editMsgKey )->text(),
|
2016-10-13 08:14:18 +00:00
|
|
|
[],
|
2023-10-13 16:23:23 +00:00
|
|
|
[ 'action' => 'edit', 'lintid' => $lintError->lintId, ]
|
2016-10-13 08:14:18 +00:00
|
|
|
);
|
|
|
|
|
2017-03-22 20:20:26 +00:00
|
|
|
$historyLink = $this->linkRenderer->makeLink(
|
|
|
|
$title,
|
2017-11-25 04:38:25 +00:00
|
|
|
$this->msg( 'linter-page-history' )->text(),
|
2017-03-22 20:20:26 +00:00
|
|
|
[],
|
|
|
|
[ 'action' => 'history' ]
|
|
|
|
);
|
|
|
|
|
|
|
|
$editHistLinks = $this->getLanguage()->pipeList( [ $editLink, $historyLink ] );
|
2017-11-25 04:38:25 +00:00
|
|
|
return $this->msg( 'linter-page-title-edit' )
|
2017-03-22 20:20:26 +00:00
|
|
|
->rawParams( $viewLink, $editHistLinks )
|
|
|
|
->escaped();
|
2016-10-13 08:14:18 +00:00
|
|
|
case 'details':
|
2024-04-05 02:41:38 +00:00
|
|
|
if ( $category !== null && $this->categoryManager->hasNameParam( $category ) &&
|
2017-12-07 21:42:13 +00:00
|
|
|
isset( $lintError->params['name'] ) ) {
|
2016-10-13 08:14:18 +00:00
|
|
|
return Html::element( 'code', [], $lintError->params['name'] );
|
2021-09-30 23:20:01 +00:00
|
|
|
} elseif ( $category === 'bogus-image-options' && isset( $lintError->params['items'] ) ) {
|
2021-05-04 07:58:08 +00:00
|
|
|
$list = array_map( static function ( $in ) {
|
2016-10-13 08:14:18 +00:00
|
|
|
return Html::element( 'code', [], $in );
|
|
|
|
}, $lintError->params['items'] );
|
|
|
|
return $this->getLanguage()->commaList( $list );
|
2021-09-30 23:20:01 +00:00
|
|
|
} elseif ( $category === 'pwrap-bug-workaround' &&
|
2017-04-12 21:27:41 +00:00
|
|
|
isset( $lintError->params['root'] ) &&
|
|
|
|
isset( $lintError->params['child'] ) ) {
|
|
|
|
return Html::element( 'code', [],
|
|
|
|
$lintError->params['root'] . " > " . $lintError->params['child'] );
|
2021-09-30 23:20:01 +00:00
|
|
|
} elseif ( $category === 'tidy-whitespace-bug' &&
|
2017-08-10 16:20:09 +00:00
|
|
|
isset( $lintError->params['node'] ) &&
|
|
|
|
isset( $lintError->params['sibling'] ) ) {
|
|
|
|
return Html::element( 'code', [],
|
|
|
|
$lintError->params['node'] . " + " . $lintError->params['sibling'] );
|
2021-09-30 23:20:01 +00:00
|
|
|
} elseif ( $category === 'multi-colon-escape' &&
|
2017-09-18 18:03:11 +00:00
|
|
|
isset( $lintError->params['href'] ) ) {
|
|
|
|
return Html::element( 'code', [], $lintError->params['href'] );
|
2021-09-30 23:20:01 +00:00
|
|
|
} elseif ( $category === 'multiline-html-table-in-list' ) {
|
2017-12-07 21:02:21 +00:00
|
|
|
/* ancestor and name will be set */
|
|
|
|
return Html::element( 'code', [],
|
|
|
|
$lintError->params['ancestorName'] . " > " . $lintError->params['name'] );
|
2021-09-30 23:20:01 +00:00
|
|
|
} elseif ( $category === 'misc-tidy-replacement-issues' ) {
|
2017-12-07 21:02:21 +00:00
|
|
|
/* There will be a 'subtype' param to disambiguate */
|
|
|
|
return Html::element( 'code', [], $lintError->params['subtype'] );
|
2024-05-29 15:34:47 +00:00
|
|
|
} elseif ( $category === 'missing-image-alt-text' ) {
|
|
|
|
$title = Title::newFromText( $lintError->params['file'], NS_FILE );
|
|
|
|
return Html::element( 'a', [
|
|
|
|
'href' => $title->getLocalUrl(),
|
|
|
|
], $title );
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
return '';
|
|
|
|
case 'template':
|
|
|
|
if ( !$lintError->templateInfo ) {
|
|
|
|
return '—';
|
|
|
|
}
|
2017-05-08 21:45:21 +00:00
|
|
|
|
|
|
|
if ( isset( $lintError->templateInfo['multiPartTemplateBlock'] ) ) {
|
|
|
|
return $this->msg( 'multi-part-template-block' )->escaped();
|
|
|
|
} else {
|
2019-10-20 12:10:45 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeArraySuspiciousNullable Null checked above
|
2017-05-08 21:45:21 +00:00
|
|
|
$templateName = $lintError->templateInfo['name'];
|
2017-07-05 20:15:48 +00:00
|
|
|
// Parsoid provides us with fully qualified template title
|
|
|
|
// So, fallback to the default main namespace
|
|
|
|
$templateTitle = Title::newFromText( $templateName );
|
2017-05-08 21:45:21 +00:00
|
|
|
if ( !$templateTitle ) {
|
|
|
|
// Shouldn't be possible...???
|
|
|
|
return '—';
|
|
|
|
}
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->linkRenderer->makeLink(
|
|
|
|
$templateTitle
|
|
|
|
);
|
2021-09-30 23:20:01 +00:00
|
|
|
case 'category':
|
2024-04-05 02:41:38 +00:00
|
|
|
return Html::element( 'code', [], $category ?? '' );
|
2016-10-13 08:14:18 +00:00
|
|
|
default:
|
|
|
|
throw new InvalidArgumentException( "Unexpected name: $name" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 16:54:41 +00:00
|
|
|
/** @inheritDoc */
|
2016-10-13 08:14:18 +00:00
|
|
|
public function getDefaultSort() {
|
|
|
|
return 'linter_id';
|
|
|
|
}
|
|
|
|
|
2021-04-06 16:54:41 +00:00
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2016-10-13 08:14:18 +00:00
|
|
|
public function getFieldNames() {
|
|
|
|
$names = [
|
2022-01-11 17:44:01 +00:00
|
|
|
'title' => $this->msg( 'linter-pager-title-header' )->text(),
|
2016-10-13 08:14:18 +00:00
|
|
|
];
|
2022-01-10 16:30:07 +00:00
|
|
|
if ( !$this->category ) {
|
2022-03-10 19:48:19 +00:00
|
|
|
$names['category'] = $this->msg( 'linter-pager-category-header' )->text();
|
2022-01-10 16:30:07 +00:00
|
|
|
$names['details'] = $this->msg( "linter-pager-details-header" )->text();
|
|
|
|
} elseif ( !$this->categoryManager->hasNoParams( $this->category ) ) {
|
2016-11-22 09:18:29 +00:00
|
|
|
$names['details'] = $this->msg( "linter-pager-{$this->category}-details" )->text();
|
2016-10-13 08:14:18 +00:00
|
|
|
}
|
2022-01-11 17:41:56 +00:00
|
|
|
$names['template'] = $this->msg( "linter-pager-template-header" )->text();
|
2016-10-13 08:14:18 +00:00
|
|
|
return $names;
|
|
|
|
}
|
|
|
|
}
|