Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 135 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
ApiQueryCategories | |
0.00% |
0 / 135 |
|
0.00% |
0 / 8 |
1190 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCacheMode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
executeGenerator | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
run | |
0.00% |
0 / 86 |
|
0.00% |
0 / 1 |
756 | |||
getAllowedParams | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
2 | |||
getExamplesMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com" |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License along |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
18 | * http://www.gnu.org/copyleft/gpl.html |
19 | * |
20 | * @file |
21 | */ |
22 | |
23 | use MediaWiki\Title\Title; |
24 | use Wikimedia\ParamValidator\ParamValidator; |
25 | use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
26 | |
27 | /** |
28 | * A query module to enumerate categories the set of pages belong to. |
29 | * |
30 | * @ingroup API |
31 | */ |
32 | class ApiQueryCategories extends ApiQueryGeneratorBase { |
33 | |
34 | /** |
35 | * @param ApiQuery $query |
36 | * @param string $moduleName |
37 | */ |
38 | public function __construct( ApiQuery $query, $moduleName ) { |
39 | parent::__construct( $query, $moduleName, 'cl' ); |
40 | } |
41 | |
42 | public function execute() { |
43 | $this->run(); |
44 | } |
45 | |
46 | public function getCacheMode( $params ) { |
47 | return 'public'; |
48 | } |
49 | |
50 | public function executeGenerator( $resultPageSet ) { |
51 | $this->run( $resultPageSet ); |
52 | } |
53 | |
54 | /** |
55 | * @param ApiPageSet|null $resultPageSet |
56 | */ |
57 | private function run( $resultPageSet = null ) { |
58 | $pages = $this->getPageSet()->getGoodPages(); |
59 | if ( $pages === [] ) { |
60 | return; // nothing to do |
61 | } |
62 | |
63 | $params = $this->extractRequestParams(); |
64 | $prop = array_fill_keys( (array)$params['prop'], true ); |
65 | $show = array_fill_keys( (array)$params['show'], true ); |
66 | |
67 | $this->addFields( [ |
68 | 'cl_from', |
69 | 'cl_to' |
70 | ] ); |
71 | |
72 | $this->addFieldsIf( [ 'cl_sortkey', 'cl_sortkey_prefix' ], isset( $prop['sortkey'] ) ); |
73 | $this->addFieldsIf( 'cl_timestamp', isset( $prop['timestamp'] ) ); |
74 | |
75 | $this->addTables( 'categorylinks' ); |
76 | $this->addWhereFld( 'cl_from', array_keys( $pages ) ); |
77 | if ( $params['categories'] ) { |
78 | $cats = []; |
79 | foreach ( $params['categories'] as $cat ) { |
80 | $title = Title::newFromText( $cat ); |
81 | if ( !$title || $title->getNamespace() !== NS_CATEGORY ) { |
82 | $this->addWarning( [ 'apiwarn-invalidcategory', wfEscapeWikiText( $cat ) ] ); |
83 | } else { |
84 | $cats[] = $title->getDBkey(); |
85 | } |
86 | } |
87 | if ( !$cats ) { |
88 | // No titles so no results |
89 | return; |
90 | } |
91 | $this->addWhereFld( 'cl_to', $cats ); |
92 | } |
93 | |
94 | if ( $params['continue'] !== null ) { |
95 | $db = $this->getDB(); |
96 | $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] ); |
97 | $op = $params['dir'] == 'descending' ? '<=' : '>='; |
98 | $this->addWhere( $db->buildComparison( $op, [ |
99 | 'cl_from' => $cont[0], |
100 | 'cl_to' => $cont[1], |
101 | ] ) ); |
102 | } |
103 | |
104 | if ( isset( $show['hidden'] ) && isset( $show['!hidden'] ) ) { |
105 | $this->dieWithError( 'apierror-show' ); |
106 | } |
107 | if ( isset( $show['hidden'] ) || isset( $show['!hidden'] ) || isset( $prop['hidden'] ) ) { |
108 | $this->addOption( 'STRAIGHT_JOIN' ); |
109 | $this->addTables( [ 'page', 'page_props' ] ); |
110 | $this->addFieldsIf( 'pp_propname', isset( $prop['hidden'] ) ); |
111 | $this->addJoinConds( [ |
112 | 'page' => [ 'LEFT JOIN', [ |
113 | 'page_namespace' => NS_CATEGORY, |
114 | 'page_title = cl_to' ] ], |
115 | 'page_props' => [ 'LEFT JOIN', [ |
116 | 'pp_page=page_id', |
117 | 'pp_propname' => 'hiddencat' ] ] |
118 | ] ); |
119 | if ( isset( $show['hidden'] ) ) { |
120 | $this->addWhere( [ 'pp_propname IS NOT NULL' ] ); |
121 | } elseif ( isset( $show['!hidden'] ) ) { |
122 | $this->addWhere( [ 'pp_propname' => null ] ); |
123 | } |
124 | } |
125 | |
126 | $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); |
127 | // Don't order by cl_from if it's constant in the WHERE clause |
128 | if ( count( $pages ) === 1 ) { |
129 | $this->addOption( 'ORDER BY', 'cl_to' . $sort ); |
130 | } else { |
131 | $this->addOption( 'ORDER BY', [ |
132 | 'cl_from' . $sort, |
133 | 'cl_to' . $sort |
134 | ] ); |
135 | } |
136 | $this->addOption( 'LIMIT', $params['limit'] + 1 ); |
137 | |
138 | $res = $this->select( __METHOD__ ); |
139 | |
140 | $count = 0; |
141 | if ( $resultPageSet === null ) { |
142 | foreach ( $res as $row ) { |
143 | if ( ++$count > $params['limit'] ) { |
144 | // We've reached the one extra which shows that |
145 | // there are additional pages to be had. Stop here... |
146 | $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to ); |
147 | break; |
148 | } |
149 | |
150 | $title = Title::makeTitle( NS_CATEGORY, $row->cl_to ); |
151 | $vals = []; |
152 | ApiQueryBase::addTitleInfo( $vals, $title ); |
153 | if ( isset( $prop['sortkey'] ) ) { |
154 | $vals['sortkey'] = bin2hex( $row->cl_sortkey ); |
155 | $vals['sortkeyprefix'] = $row->cl_sortkey_prefix; |
156 | } |
157 | if ( isset( $prop['timestamp'] ) ) { |
158 | $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp ); |
159 | } |
160 | if ( isset( $prop['hidden'] ) ) { |
161 | $vals['hidden'] = $row->pp_propname !== null; |
162 | } |
163 | |
164 | $fit = $this->addPageSubItem( $row->cl_from, $vals ); |
165 | if ( !$fit ) { |
166 | $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to ); |
167 | break; |
168 | } |
169 | } |
170 | } else { |
171 | $titles = []; |
172 | foreach ( $res as $row ) { |
173 | if ( ++$count > $params['limit'] ) { |
174 | // We've reached the one extra which shows that |
175 | // there are additional pages to be had. Stop here... |
176 | $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to ); |
177 | break; |
178 | } |
179 | |
180 | $titles[] = Title::makeTitle( NS_CATEGORY, $row->cl_to ); |
181 | } |
182 | $resultPageSet->populateFromTitles( $titles ); |
183 | } |
184 | } |
185 | |
186 | public function getAllowedParams() { |
187 | return [ |
188 | 'prop' => [ |
189 | ParamValidator::PARAM_ISMULTI => true, |
190 | ParamValidator::PARAM_TYPE => [ |
191 | 'sortkey', |
192 | 'timestamp', |
193 | 'hidden', |
194 | ], |
195 | ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
196 | ], |
197 | 'show' => [ |
198 | ParamValidator::PARAM_ISMULTI => true, |
199 | ParamValidator::PARAM_TYPE => [ |
200 | 'hidden', |
201 | '!hidden', |
202 | ] |
203 | ], |
204 | 'limit' => [ |
205 | ParamValidator::PARAM_DEFAULT => 10, |
206 | ParamValidator::PARAM_TYPE => 'limit', |
207 | IntegerDef::PARAM_MIN => 1, |
208 | IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1, |
209 | IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2 |
210 | ], |
211 | 'continue' => [ |
212 | ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
213 | ], |
214 | 'categories' => [ |
215 | ParamValidator::PARAM_ISMULTI => true, |
216 | ], |
217 | 'dir' => [ |
218 | ParamValidator::PARAM_DEFAULT => 'ascending', |
219 | ParamValidator::PARAM_TYPE => [ |
220 | 'ascending', |
221 | 'descending' |
222 | ] |
223 | ], |
224 | ]; |
225 | } |
226 | |
227 | protected function getExamplesMessages() { |
228 | return [ |
229 | 'action=query&prop=categories&titles=Albert%20Einstein' |
230 | => 'apihelp-query+categories-example-simple', |
231 | 'action=query&generator=categories&titles=Albert%20Einstein&prop=info' |
232 | => 'apihelp-query+categories-example-generator', |
233 | ]; |
234 | } |
235 | |
236 | public function getHelpUrls() { |
237 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Categories'; |
238 | } |
239 | } |