Debian Codegraph @

includes/api/ApiQueryCategories.php

raw symbols SCIP
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 namespace MediaWiki\Api;
24
25 use MediaWiki\Title\Title;
26 use Wikimedia\ParamValidator\ParamValidator;
27 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
28
29 /**
30 * A query module to enumerate categories the set of pages belong to.
31 *
32 * @ingroup API
33 */
34 class ApiQueryCategories extends ApiQueryGeneratorBase {
35
36 public function __construct( ApiQuery $query, string $moduleName ) {
37 parent::__construct( $query, $moduleName, 'cl' );
38 }
39
40 public function execute() {
41 $this->run();
42 }
43
44 public function getCacheMode( $params ) {
45 return 'public';
46 }
47
48 public function executeGenerator( $resultPageSet ) {
49 $this->run( $resultPageSet );
50 }
51
52 /**
53 * @param ApiPageSet|null $resultPageSet
54 */
55 private function run( $resultPageSet = null ) {
56 $pages = $this->getPageSet()->getGoodPages();
57 if ( $pages === [] ) {
58 return; // nothing to do
59 }
60
61 $params = $this->extractRequestParams();
62 $prop = array_fill_keys( (array)$params['prop'], true );
63 $show = array_fill_keys( (array)$params['show'], true );
64
65 $this->addFields( [
66 'cl_from',
67 'cl_to'
68 ] );
69
70 $this->addFieldsIf( [ 'cl_sortkey', 'cl_sortkey_prefix' ], isset( $prop['sortkey'] ) );
71 $this->addFieldsIf( 'cl_timestamp', isset( $prop['timestamp'] ) );
72
73 $this->addTables( 'categorylinks' );
74 $this->addWhereFld( 'cl_from', array_keys( $pages ) );
75 if ( $params['categories'] ) {
76 $cats = [];
77 foreach ( $params['categories'] as $cat ) {
78 $title = Title::newFromText( $cat );
79 if ( !$title || $title->getNamespace() !== NS_CATEGORY ) {
80 $this->addWarning( [ 'apiwarn-invalidcategory', wfEscapeWikiText( $cat ) ] );
81 } else {
82 $cats[] = $title->getDBkey();
83 }
84 }
85 if ( !$cats ) {
86 // No titles so no results
87 return;
88 }
89 $this->addWhereFld( 'cl_to', $cats );
90 }
91
92 if ( $params['continue'] !== null ) {
93 $db = $this->getDB();
94 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
95 $op = $params['dir'] == 'descending' ? '<=' : '>=';
96 $this->addWhere( $db->buildComparison( $op, [
97 'cl_from' => $cont[0],
98 'cl_to' => $cont[1],
99 ] ) );
100 }
101
102 if ( isset( $show['hidden'] ) && isset( $show['!hidden'] ) ) {
103 $this->dieWithError( 'apierror-show' );
104 }
105 if ( isset( $show['hidden'] ) || isset( $show['!hidden'] ) || isset( $prop['hidden'] ) ) {
106 $this->addOption( 'STRAIGHT_JOIN' );
107 $this->addTables( [ 'page', 'page_props' ] );
108 $this->addFieldsIf( 'pp_propname', isset( $prop['hidden'] ) );
109 $this->addJoinConds( [
110 'page' => [ 'LEFT JOIN', [
111 'page_namespace' => NS_CATEGORY,
112 'page_title = cl_to' ] ],
113 'page_props' => [ 'LEFT JOIN', [
114 'pp_page=page_id',
115 'pp_propname' => 'hiddencat' ] ]
116 ] );
117 if ( isset( $show['hidden'] ) ) {
118 $this->addWhere( $this->getDB()->expr( 'pp_propname', '!=', null ) );
119 } elseif ( isset( $show['!hidden'] ) ) {
120 $this->addWhere( [ 'pp_propname' => null ] );
121 }
122 }
123
124 $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' );
125 // Don't order by cl_from if it's constant in the WHERE clause
126 if ( count( $pages ) === 1 ) {
127 $this->addOption( 'ORDER BY', 'cl_to' . $sort );
128 } else {
129 $this->addOption( 'ORDER BY', [
130 'cl_from' . $sort,
131 'cl_to' . $sort
132 ] );
133 }
134 $this->addOption( 'LIMIT', $params['limit'] + 1 );
135
136 $res = $this->select( __METHOD__ );
137
138 $count = 0;
139 if ( $resultPageSet === null ) {
140 foreach ( $res as $row ) {
141 if ( ++$count > $params['limit'] ) {
142 // We've reached the one extra which shows that
143 // there are additional pages to be had. Stop here...
144 $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
145 break;
146 }
147
148 $title = Title::makeTitle( NS_CATEGORY, $row->cl_to );
149 $vals = [];
150 ApiQueryBase::addTitleInfo( $vals, $title );
151 if ( isset( $prop['sortkey'] ) ) {
152 $vals['sortkey'] = bin2hex( $row->cl_sortkey );
153 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
154 }
155 if ( isset( $prop['timestamp'] ) ) {
156 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
157 }
158 if ( isset( $prop['hidden'] ) ) {
159 $vals['hidden'] = $row->pp_propname !== null;
160 }
161
162 $fit = $this->addPageSubItem( $row->cl_from, $vals );
163 if ( !$fit ) {
164 $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
165 break;
166 }
167 }
168 } else {
169 $titles = [];
170 foreach ( $res as $row ) {
171 if ( ++$count > $params['limit'] ) {
172 // We've reached the one extra which shows that
173 // there are additional pages to be had. Stop here...
174 $this->setContinueEnumParameter( 'continue', $row->cl_from . '|' . $row->cl_to );
175 break;
176 }
177
178 $titles[] = Title::makeTitle( NS_CATEGORY, $row->cl_to );
179 }
180 $resultPageSet->populateFromTitles( $titles );
181 }
182 }
183
184 public function getAllowedParams() {
185 return [
186 'prop' => [
187 ParamValidator::PARAM_ISMULTI => true,
188 ParamValidator::PARAM_TYPE => [
189 'sortkey',
190 'timestamp',
191 'hidden',
192 ],
193 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
194 ],
195 'show' => [
196 ParamValidator::PARAM_ISMULTI => true,
197 ParamValidator::PARAM_TYPE => [
198 'hidden',
199 '!hidden',
200 ]
201 ],
202 'limit' => [
203 ParamValidator::PARAM_DEFAULT => 10,
204 ParamValidator::PARAM_TYPE => 'limit',
205 IntegerDef::PARAM_MIN => 1,
206 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
207 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
208 ],
209 'continue' => [
210 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
211 ],
212 'categories' => [
213 ParamValidator::PARAM_ISMULTI => true,
214 ],
215 'dir' => [
216 ParamValidator::PARAM_DEFAULT => 'ascending',
217 ParamValidator::PARAM_TYPE => [
218 'ascending',
219 'descending'
220 ]
221 ],
222 ];
223 }
224
225 protected function getExamplesMessages() {
226 return [
227 'action=query&prop=categories&titles=Albert%20Einstein'
228 => 'apihelp-query+categories-example-simple',
229 'action=query&generator=categories&titles=Albert%20Einstein&prop=info'
230 => 'apihelp-query+categories-example-generator',
231 ];
232 }
233
234 public function getHelpUrls() {
235 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Categories';
236 }
237 }
238
239 /** @deprecated class alias since 1.43 */
240 class_alias( ApiQueryCategories::class, 'ApiQueryCategories' );