Debian Codegraph @

extensions/Linter/includes/ApiQueryLintErrors.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 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 * @param ApiQuery $queryModule
36 * @param string $moduleName
37 * @param CategoryManager $categoryManager
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 // This can be an array or a single pageid
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 // T200517#10236299: Force the use of the category index
94 $this->addOption( 'USE INDEX', [ 'linter' => 'linter_cat_page_position' ] );
95 }
96 // Be explicit about ORDER BY
97 $this->addOption( 'ORDER BY', 'linter_id' );
98 // Add +1 to limit to know if there's another row for continuation
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 // template info and params are an object
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 /** @inheritDoc */
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 // Default is to show all categories
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 /** @inheritDoc */
174 public function getExamplesMessages() {
175 return [
176 'action=query&list=linterrors&lntcategories=obsolete-tag' =>
177 'apihelp-query+linterrors-example-1',
178 ];
179 }
180 }