Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 95
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryLintErrors
0.00% covered (danger)
0.00%
0 / 95
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 58
0.00% covered (danger)
0.00%
0 / 1
110
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
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
21namespace MediaWiki\Linter;
22
23use ApiBase;
24use ApiQuery;
25use ApiQueryBase;
26use ApiResult;
27use MediaWiki\Title\Title;
28use Wikimedia\ParamValidator\ParamValidator;
29use Wikimedia\ParamValidator\TypeDef\IntegerDef;
30
31class 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        $categories = $params['categories'];
55        if ( !$categories ) {
56            $categories = $this->categoryManager->getVisibleCategories();
57        }
58
59        $this->addTables( 'linter' );
60        $this->addWhereFld( 'linter_cat', array_values( $this->categoryManager->getCategoryIds(
61            $categories
62        ) ) );
63        $db = $this->getDB();
64        if ( $params['from'] !== null ) {
65            $this->addWhere( $db->expr( "linter_id", '>=', $params['from'] ) );
66        }
67        if ( $params['pageid'] !== null ) {
68            // This can be an array or a single pageid
69            $this->addWhereFld( 'linter_page', $params['pageid'] );
70        }
71        if ( $params['namespace'] !== null ) {
72            $this->addWhereFld( 'page_namespace', $params['namespace'] );
73        }
74        if ( $params['title'] !== null ) {
75            $title = $this->getTitleFromTitleOrPageId( [ 'title' => $params['title'] ] );
76            $this->addWhereFld( 'page_namespace', $title->getNamespace() );
77            $this->addWhereFld( 'page_title', $title->getDBkey() );
78        }
79        $this->addTables( 'page' );
80        $this->addJoinConds( [ 'page' => [ 'INNER JOIN', 'page_id=linter_page' ] ] );
81        $this->addFields( [
82            'linter_id', 'linter_cat', 'linter_params',
83            'linter_start', 'linter_end',
84            'page_namespace', 'page_title',
85        ] );
86        // Be explicit about ORDER BY
87        $this->addOption( 'ORDER BY', 'linter_id' );
88        // Add +1 to limit to know if there's another row for continuation
89        $this->addOption( 'LIMIT', $params['limit'] + 1 );
90        $rows = $this->select( __METHOD__ );
91        $result = $this->getResult();
92        $count = 0;
93        foreach ( $rows as $row ) {
94            $lintError = Database::makeLintError( $this->categoryManager, $row );
95            if ( !$lintError ) {
96                continue;
97            }
98            $count++;
99            if ( $count > $params['limit'] ) {
100                $this->setContinueEnumParameter( 'from', $lintError->lintId );
101                break;
102            }
103            $title = Title::makeTitle( $row->page_namespace, $row->page_title );
104
105            $data = [
106                'pageid' => $title->getArticleID(),
107                'ns' => $title->getNamespace(),
108                'title' => $title->getPrefixedText(),
109                'lintId' => $lintError->lintId,
110                'category' => $lintError->category,
111                'location' => $lintError->location,
112                'templateInfo' => $lintError->templateInfo,
113                'params' => $lintError->getExtraParams(),
114            ];
115            // template info and params are an object
116            $data['params'][ApiResult::META_TYPE] = 'assoc';
117            $data['templateInfo'][ApiResult::META_TYPE] = 'assoc';
118
119            $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $data );
120            if ( !$fit ) {
121                $this->setContinueEnumParameter( 'from', $lintError->lintId );
122                break;
123            }
124        }
125    }
126
127    /** @inheritDoc */
128    public function getAllowedParams() {
129        $visibleCats = $this->categoryManager->getVisibleCategories();
130        $invisibleCats = $this->categoryManager->getinvisibleCategories();
131        $categories = array_merge( $visibleCats, $invisibleCats );
132        return [
133            'categories' => [
134                ParamValidator::PARAM_TYPE => $categories,
135                ParamValidator::PARAM_ISMULTI => true,
136                // Default is to show all categories
137                ParamValidator::PARAM_DEFAULT => implode( '|', $visibleCats ),
138            ],
139            'limit' => [
140                ParamValidator::PARAM_DEFAULT => 10,
141                ParamValidator::PARAM_TYPE => 'limit',
142                IntegerDef::PARAM_MIN => 1,
143                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
144                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
145            ],
146            'namespace' => [
147                ParamValidator::PARAM_TYPE => 'namespace',
148                ParamValidator::PARAM_ISMULTI => true,
149            ],
150            'pageid' => [
151                ParamValidator::PARAM_TYPE => 'integer',
152                ParamValidator::PARAM_ISMULTI => true,
153            ],
154            'title' => [
155                ParamValidator::PARAM_TYPE => 'string',
156            ],
157            'from' => [
158                ParamValidator::PARAM_TYPE => 'integer',
159            ],
160        ];
161    }
162
163    /** @inheritDoc */
164    public function getExamplesMessages() {
165        return [
166            'action=query&list=linterrors&lntcategories=obsolete-tag' =>
167                'apihelp-query+linterrors-example-1',
168        ];
169    }
170}