MediaWiki master
ApiQueryCategories.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
28
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
55 private function run( $resultPageSet = null ) {
56 $pages = $this->getPageSet()->getGoodPages();
57 if ( $pages === [] ) {
58 return; // nothing to do
59 }
60
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 ],
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
240class_alias( ApiQueryCategories::class, 'ApiQueryCategories' );
const NS_CATEGORY
Definition Defines.php:79
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
run()
Run the job.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1565
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1756
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:224
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition ApiBase.php:1483
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:184
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:251
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:249
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
getDB()
Get the Query database connection (read-only)
select( $method, $extraQuery=[], ?array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addFields( $value)
Add a set of fields to select to the internal array.
A query module to enumerate categories the set of pages belong to.
getHelpUrls()
Return links to more detailed help pages about the module.
executeGenerator( $resultPageSet)
Execute this module as a generator.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getExamplesMessages()
Returns usage examples for this module.
__construct(ApiQuery $query, string $moduleName)
getCacheMode( $params)
Get the cache mode for the data generated by this module.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
getPageSet()
Get the PageSet object to work on.
This is the main query class.
Definition ApiQuery.php:48
Represents a title within MediaWiki.
Definition Title.php:78
Service for formatting and validating API parameters.
Type definition for integer types.