| 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 MediaWiki\Api\ApiBase; |
| 24 |
use MediaWiki\Api\ApiQuery; |
| 25 |
use MediaWiki\Api\ApiQueryBase; |
| 26 |
use MediaWiki\Api\ApiResult; |
| 27 |
use MediaWiki\Title\Title; |
| 28 |
use Wikimedia\ParamValidator\ParamValidator; |
| 29 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 30 |
|
| 31 |
class ApiQueryLintErrors extends ApiQueryBase { |
| 32 |
private CategoryManager $categoryManager; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function __construct( |
| 40 |
ApiQuery $queryModule, |
| 41 |
string $moduleName, |
| 42 |
CategoryManager $categoryManager |
| 43 |
) { |
| 44 |
parent::__construct( $queryModule, $moduleName, 'lnt' ); |
| 45 |
$this->categoryManager = $categoryManager; |
| 46 |
} |
| 47 |
|
| 48 |
public function execute() { |
| 49 |
$params = $this->extractRequestParams(); |
| 50 |
|
| 51 |
$this->requireMaxOneParameter( $params, 'pageid', 'title' ); |
| 52 |
$this->requireMaxOneParameter( $params, 'namespace', 'title' ); |
| 53 |
|
| 54 |
$useIndex = true; |
| 55 |
$categories = $params['categories']; |
| 56 |
if ( !$categories ) { |
| 57 |
$categories = $this->categoryManager->getVisibleCategories(); |
| 58 |
} |
| 59 |
if ( count( $categories ) > 1 ) { |
| 60 |
$useIndex = false; |
| 61 |
} |
| 62 |
$this->addTables( 'linter' ); |
| 63 |
$this->addWhereFld( 'linter_cat', array_values( $this->categoryManager->getCategoryIds( |
| 64 |
$categories |
| 65 |
) ) ); |
| 66 |
$db = $this->getDB(); |
| 67 |
if ( $params['from'] !== null ) { |
| 68 |
$this->addWhere( $db->expr( "linter_id", '>=', $params['from'] ) ); |
| 69 |
} |
| 70 |
if ( $params['pageid'] !== null ) { |
| 71 |
|
| 72 |
$this->addWhereFld( 'linter_page', $params['pageid'] ); |
| 73 |
$useIndex = false; |
| 74 |
} |
| 75 |
if ( $params['namespace'] !== null ) { |
| 76 |
$this->addWhereFld( 'page_namespace', $params['namespace'] ); |
| 77 |
$useIndex = false; |
| 78 |
} |
| 79 |
if ( $params['title'] !== null ) { |
| 80 |
$title = $this->getTitleFromTitleOrPageId( [ 'title' => $params['title'] ] ); |
| 81 |
$this->addWhereFld( 'page_namespace', $title->getNamespace() ); |
| 82 |
$this->addWhereFld( 'page_title', $title->getDBkey() ); |
| 83 |
$useIndex = false; |
| 84 |
} |
| 85 |
$this->addTables( 'page' ); |
| 86 |
$this->addJoinConds( [ 'page' => [ 'INNER JOIN', 'page_id=linter_page' ] ] ); |
| 87 |
$this->addFields( [ |
| 88 |
'linter_id', 'linter_cat', 'linter_params', |
| 89 |
'linter_start', 'linter_end', |
| 90 |
'page_namespace', 'page_title', |
| 91 |
] ); |
| 92 |
if ( $useIndex ) { |
| 93 |
|
| 94 |
$this->addOption( 'USE INDEX', [ 'linter' => 'linter_cat_page_position' ] ); |
| 95 |
} |
| 96 |
|
| 97 |
$this->addOption( 'ORDER BY', 'linter_id' ); |
| 98 |
|
| 99 |
$this->addOption( 'LIMIT', $params['limit'] + 1 ); |
| 100 |
$rows = $this->select( __METHOD__ ); |
| 101 |
$result = $this->getResult(); |
| 102 |
$count = 0; |
| 103 |
foreach ( $rows as $row ) { |
| 104 |
$lintError = Database::makeLintError( $this->categoryManager, $row ); |
| 105 |
if ( !$lintError ) { |
| 106 |
continue; |
| 107 |
} |
| 108 |
$count++; |
| 109 |
if ( $count > $params['limit'] ) { |
| 110 |
$this->setContinueEnumParameter( 'from', $lintError->lintId ); |
| 111 |
break; |
| 112 |
} |
| 113 |
$title = Title::makeTitle( $row->page_namespace, $row->page_title ); |
| 114 |
|
| 115 |
$data = [ |
| 116 |
'pageid' => $title->getArticleID(), |
| 117 |
'ns' => $title->getNamespace(), |
| 118 |
'title' => $title->getPrefixedText(), |
| 119 |
'lintId' => $lintError->lintId, |
| 120 |
'category' => $lintError->category, |
| 121 |
'location' => $lintError->location, |
| 122 |
'templateInfo' => $lintError->templateInfo, |
| 123 |
'params' => $lintError->getExtraParams(), |
| 124 |
]; |
| 125 |
|
| 126 |
$data['params'][ApiResult::META_TYPE] = 'assoc'; |
| 127 |
$data['templateInfo'][ApiResult::META_TYPE] = 'assoc'; |
| 128 |
|
| 129 |
$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $data ); |
| 130 |
if ( !$fit ) { |
| 131 |
$this->setContinueEnumParameter( 'from', $lintError->lintId ); |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
public function getAllowedParams() { |
| 139 |
$visibleCats = $this->categoryManager->getVisibleCategories(); |
| 140 |
$invisibleCats = $this->categoryManager->getinvisibleCategories(); |
| 141 |
$categories = array_merge( $visibleCats, $invisibleCats ); |
| 142 |
return [ |
| 143 |
'categories' => [ |
| 144 |
ParamValidator::PARAM_TYPE => $categories, |
| 145 |
ParamValidator::PARAM_ISMULTI => true, |
| 146 |
|
| 147 |
ParamValidator::PARAM_DEFAULT => implode( '|', $visibleCats ), |
| 148 |
], |
| 149 |
'limit' => [ |
| 150 |
ParamValidator::PARAM_DEFAULT => 10, |
| 151 |
ParamValidator::PARAM_TYPE => 'limit', |
| 152 |
IntegerDef::PARAM_MIN => 1, |
| 153 |
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
| 154 |
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
| 155 |
], |
| 156 |
'namespace' => [ |
| 157 |
ParamValidator::PARAM_TYPE => 'namespace', |
| 158 |
ParamValidator::PARAM_ISMULTI => true, |
| 159 |
], |
| 160 |
'pageid' => [ |
| 161 |
ParamValidator::PARAM_TYPE => 'integer', |
| 162 |
ParamValidator::PARAM_ISMULTI => true, |
| 163 |
], |
| 164 |
'title' => [ |
| 165 |
ParamValidator::PARAM_TYPE => 'string', |
| 166 |
], |
| 167 |
'from' => [ |
| 168 |
ParamValidator::PARAM_TYPE => 'integer', |
| 169 |
], |
| 170 |
]; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
public function getExamplesMessages() { |
| 175 |
return [ |
| 176 |
'action=query&list=linterrors&lntcategories=obsolete-tag' => |
| 177 |
'apihelp-query+linterrors-example-1', |
| 178 |
]; |
| 179 |
} |
| 180 |
} |