MediaWiki master
ApiQueryBacklinksprop.php
Go to the documentation of this file.
1<?php
32
41
43 private static $settings = [
44 'redirects' => [
45 'code' => 'rd',
46 'prefix' => 'rd',
47 'linktable' => 'redirect',
48 'props' => [
49 'fragment',
50 ],
51 'showredirects' => false,
52 'show' => [
53 'fragment',
54 '!fragment',
55 ],
56 ],
57 'linkshere' => [
58 'code' => 'lh',
59 'prefix' => 'pl',
60 'linktable' => 'pagelinks',
61 'indexes' => [ 'pl_namespace', 'pl_backlinks_namespace' ],
62 'from_namespace' => true,
63 'showredirects' => true,
64 ],
65 'transcludedin' => [
66 'code' => 'ti',
67 'prefix' => 'tl',
68 'linktable' => 'templatelinks',
69 'from_namespace' => true,
70 'showredirects' => true,
71 ],
72 'fileusage' => [
73 'code' => 'fu',
74 'prefix' => 'il',
75 'linktable' => 'imagelinks',
76 'indexes' => [ 'il_to', 'il_backlinks_namespace' ],
77 'from_namespace' => true,
78 'to_namespace' => NS_FILE,
79 'exampletitle' => 'File:Example.jpg',
80 'showredirects' => true,
81 ],
82 ];
83
84 private LinksMigration $linksMigration;
85
91 public function __construct(
92 ApiQuery $query,
93 $moduleName,
94 LinksMigration $linksMigration
95 ) {
96 parent::__construct( $query, $moduleName, self::$settings[$moduleName]['code'] );
97 $this->linksMigration = $linksMigration;
98 }
99
100 public function execute() {
101 $this->run();
102 }
103
104 public function executeGenerator( $resultPageSet ) {
105 $this->run( $resultPageSet );
106 }
107
111 private function run( ApiPageSet $resultPageSet = null ) {
112 $settings = self::$settings[$this->getModuleName()];
113
114 $db = $this->getDB();
115 $params = $this->extractRequestParams();
116 $prop = array_fill_keys( $params['prop'], true );
117
118 $pageSet = $this->getPageSet();
119 $titles = $pageSet->getGoodAndMissingPages();
120 $map = $pageSet->getGoodAndMissingTitlesByNamespace();
121
122 // Add in special pages, they can theoretically have backlinks too.
123 // (although currently they only do for prop=redirects)
124 foreach ( $pageSet->getSpecialPages() as $id => $title ) {
125 $titles[] = $title;
126 $map[$title->getNamespace()][$title->getDBkey()] = $id;
127 }
128
129 // Determine our fields to query on
130 $p = $settings['prefix'];
131 $hasNS = !isset( $settings['to_namespace'] );
132 if ( $hasNS ) {
133 if ( isset( $this->linksMigration::$mapping[$settings['linktable']] ) ) {
134 [ $bl_namespace, $bl_title ] = $this->linksMigration->getTitleFields( $settings['linktable'] );
135 } else {
136 $bl_namespace = "{$p}_namespace";
137 $bl_title = "{$p}_title";
138 }
139 } else {
140 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
141 $bl_namespace = $settings['to_namespace'];
142 $bl_title = "{$p}_to";
143
144 $titles = array_filter( $titles, static function ( $t ) use ( $bl_namespace ) {
145 return $t->getNamespace() === $bl_namespace;
146 } );
147 $map = array_intersect_key( $map, [ $bl_namespace => true ] );
148 }
149 $bl_from = "{$p}_from";
150
151 if ( !$titles ) {
152 return; // nothing to do
153 }
154 if ( $params['namespace'] !== null && count( $params['namespace'] ) === 0 ) {
155 return; // nothing to do
156 }
157
158 // Figure out what we're sorting by, and add associated WHERE clauses.
159 // MySQL's query planner screws up if we include a field in ORDER BY
160 // when it's constant in WHERE, so we have to test that for each field.
161 $sortby = [];
162 if ( $hasNS && count( $map ) > 1 ) {
163 $sortby[$bl_namespace] = 'int';
164 }
165 $theTitle = null;
166 foreach ( $map as $nsTitles ) {
167 $key = array_key_first( $nsTitles );
168 $theTitle ??= $key;
169 if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
170 $sortby[$bl_title] = 'string';
171 break;
172 }
173 }
174 $miser_ns = null;
175 if ( $params['namespace'] !== null ) {
176 if ( empty( $settings['from_namespace'] ) ) {
177 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
178 $miser_ns = $params['namespace'];
179 } else {
180 $this->addWhereFld( 'page_namespace', $params['namespace'] );
181 }
182 } else {
183 $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
184 if ( !empty( $settings['from_namespace'] )
185 && $params['namespace'] !== null && count( $params['namespace'] ) > 1
186 ) {
187 $sortby["{$p}_from_namespace"] = 'int';
188 }
189 }
190 }
191 $sortby[$bl_from] = 'int';
192
193 // Now use the $sortby to figure out the continuation
194 $continueFields = array_keys( $sortby );
195 $continueTypes = array_values( $sortby );
196 if ( $params['continue'] !== null ) {
197 $continueValues = $this->parseContinueParamOrDie( $params['continue'], $continueTypes );
198 $conds = array_combine( $continueFields, $continueValues );
199 $this->addWhere( $db->buildComparison( '>=', $conds ) );
200 }
201
202 // Populate the rest of the query
203 [ $idxNoFromNS, $idxWithFromNS ] = $settings['indexes'] ?? [ '', '' ];
204 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
205 if ( isset( $this->linksMigration::$mapping[$settings['linktable']] ) ) {
206 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
207 $queryInfo = $this->linksMigration->getQueryInfo( $settings['linktable'] );
208 $this->addTables( array_merge( [ 'page' ], $queryInfo['tables'] ) );
209 $this->addJoinConds( $queryInfo['joins'] );
210 // TODO: Move to links migration
211 if ( in_array( 'linktarget', $queryInfo['tables'] ) ) {
212 $idxWithFromNS .= '_target_id';
213 }
214 } else {
215 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
216 $this->addTables( [ $settings['linktable'], 'page' ] );
217 }
218 $this->addWhere( "$bl_from = page_id" );
219
220 if ( $this->getModuleName() === 'redirects' ) {
221 $this->addWhereFld( 'rd_interwiki', '' );
222 }
223
224 $this->addFields( array_keys( $sortby ) );
225 $this->addFields( [ 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ] );
226 if ( $resultPageSet === null ) {
227 $fld_pageid = isset( $prop['pageid'] );
228 $fld_title = isset( $prop['title'] );
229 $fld_redirect = isset( $prop['redirect'] );
230
231 $this->addFieldsIf( 'page_id', $fld_pageid );
232 $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
233 $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
234
235 // prop=redirects
236 $fld_fragment = isset( $prop['fragment'] );
237 $this->addFieldsIf( 'rd_fragment', $fld_fragment );
238 } else {
239 $this->addFields( $resultPageSet->getPageTableFields() );
240 }
241
242 $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
243
244 if ( $hasNS && $map ) {
245 // Can't use LinkBatch because it throws away Special titles.
246 // And we already have the needed data structure anyway.
247 $this->addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
248 } else {
249 $where = [];
250 foreach ( $titles as $t ) {
251 if ( $t->getNamespace() == $bl_namespace ) {
252 $where[] = $db->expr( $bl_title, '=', $t->getDBkey() );
253 }
254 }
255 $this->addWhere( new OrExpressionGroup( ...$where ) );
256 }
257
258 if ( $params['show'] !== null ) {
259 // prop=redirects only
260 $show = array_fill_keys( $params['show'], true );
261 if ( ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ) ||
262 ( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
263 ) {
264 $this->dieWithError( 'apierror-show' );
265 }
266 $this->addWhereIf( $db->expr( 'rd_fragment', '!=', '' ), isset( $show['fragment'] ) );
267 $this->addWhereIf( [ 'rd_fragment' => '' ], isset( $show['!fragment'] ) );
268 $this->addWhereIf( [ 'page_is_redirect' => 1 ], isset( $show['redirect'] ) );
269 $this->addWhereIf( [ 'page_is_redirect' => 0 ], isset( $show['!redirect'] ) );
270 }
271
272 // Override any ORDER BY from above with what we calculated earlier.
273 $this->addOption( 'ORDER BY', array_keys( $sortby ) );
274
275 // MySQL's optimizer chokes if we have too many values in "$bl_title IN
276 // (...)" and chooses the wrong index, so specify the correct index to
277 // use for the query. See T139056 for details.
278 if ( !empty( $settings['indexes'] ) ) {
279 if (
280 $params['namespace'] !== null &&
281 count( $params['namespace'] ) == 1 &&
282 !empty( $settings['from_namespace'] )
283 ) {
284 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
285 $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxWithFromNS ] );
286 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
287 } elseif ( !isset( $this->linksMigration::$mapping[$settings['linktable']] ) ) {
288 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
289 $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxNoFromNS ] );
290 }
291 }
292
293 $this->addOption( 'LIMIT', $params['limit'] + 1 );
294
295 $res = $this->select( __METHOD__ );
296
297 if ( $resultPageSet === null ) {
298 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
299 if ( $fld_title ) {
300 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
301 }
302
303 $count = 0;
304 foreach ( $res as $row ) {
305 if ( ++$count > $params['limit'] ) {
306 // We've reached the one extra which shows that
307 // there are additional pages to be had. Stop here...
308 $this->setContinue( $row, $sortby );
309 break;
310 }
311
312 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
313 // Miser mode namespace check
314 continue;
315 }
316
317 // Get the ID of the current page
318 $id = $map[$row->bl_namespace][$row->bl_title];
319
320 $vals = [];
321 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
322 if ( $fld_pageid ) {
323 $vals['pageid'] = (int)$row->page_id;
324 }
325 if ( $fld_title ) {
327 Title::makeTitle( $row->page_namespace, $row->page_title )
328 );
329 }
330 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
331 if ( $fld_fragment && $row->rd_fragment !== '' ) {
332 $vals['fragment'] = $row->rd_fragment;
333 }
334 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
335 if ( $fld_redirect ) {
336 $vals['redirect'] = (bool)$row->page_is_redirect;
337 }
338 $fit = $this->addPageSubItem( $id, $vals );
339 if ( !$fit ) {
340 $this->setContinue( $row, $sortby );
341 break;
342 }
343 }
344 } else {
345 $titles = [];
346 $count = 0;
347 foreach ( $res as $row ) {
348 if ( ++$count > $params['limit'] ) {
349 // We've reached the one extra which shows that
350 // there are additional pages to be had. Stop here...
351 $this->setContinue( $row, $sortby );
352 break;
353 }
354
355 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
356 // Miser mode namespace check
357 continue;
358 }
359
360 $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
361 }
362 $resultPageSet->populateFromTitles( $titles );
363 }
364 }
365
366 private function setContinue( $row, $sortby ) {
367 $cont = [];
368 foreach ( $sortby as $field => $v ) {
369 $cont[] = $row->$field;
370 }
371 $this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
372 }
373
374 public function getCacheMode( $params ) {
375 return 'public';
376 }
377
378 public function getAllowedParams() {
379 $settings = self::$settings[$this->getModuleName()];
380
381 $ret = [
382 'prop' => [
383 ParamValidator::PARAM_TYPE => [
384 'pageid',
385 'title',
386 ],
387 ParamValidator::PARAM_ISMULTI => true,
388 ParamValidator::PARAM_DEFAULT => 'pageid|title',
390 ],
391 'namespace' => [
392 ParamValidator::PARAM_ISMULTI => true,
393 ParamValidator::PARAM_TYPE => 'namespace',
394 ],
395 'show' => null, // Will be filled/removed below
396 'limit' => [
397 ParamValidator::PARAM_DEFAULT => 10,
398 ParamValidator::PARAM_TYPE => 'limit',
399 IntegerDef::PARAM_MIN => 1,
400 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
401 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
402 ],
403 'continue' => [
404 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
405 ],
406 ];
407
408 if ( empty( $settings['from_namespace'] ) &&
409 $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
410 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
411 'api-help-param-limited-in-miser-mode',
412 ];
413 }
414
415 if ( !empty( $settings['showredirects'] ) ) {
416 $ret['prop'][ParamValidator::PARAM_TYPE][] = 'redirect';
417 $ret['prop'][ParamValidator::PARAM_DEFAULT] .= '|redirect';
418 }
419 if ( isset( $settings['props'] ) ) {
420 $ret['prop'][ParamValidator::PARAM_TYPE] = array_merge(
421 $ret['prop'][ParamValidator::PARAM_TYPE], $settings['props']
422 );
423 }
424
425 $show = [];
426 if ( !empty( $settings['showredirects'] ) ) {
427 $show[] = 'redirect';
428 $show[] = '!redirect';
429 }
430 if ( isset( $settings['show'] ) ) {
431 $show = array_merge( $show, $settings['show'] );
432 }
433 if ( $show ) {
434 $ret['show'] = [
435 ParamValidator::PARAM_TYPE => $show,
436 ParamValidator::PARAM_ISMULTI => true,
438 ];
439 } else {
440 unset( $ret['show'] );
441 }
442
443 return $ret;
444 }
445
446 protected function getExamplesMessages() {
447 $settings = self::$settings[$this->getModuleName()];
448 $name = $this->getModuleName();
449 $path = $this->getModulePath();
450 $title = $settings['exampletitle'] ?? Title::newMainPage()->getPrefixedText();
451 $etitle = rawurlencode( $title );
452
453 return [
454 "action=query&prop={$name}&titles={$etitle}"
455 => "apihelp-$path-example-simple",
456 "action=query&generator={$name}&titles={$etitle}&prop=info"
457 => "apihelp-$path-example-generator",
458 ];
459 }
460
461 public function getHelpUrls() {
462 $name = ucfirst( $this->getModuleName() );
463 return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
464 }
465}
const NS_FILE
Definition Defines.php:71
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:1540
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition ApiBase.php:179
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1731
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:212
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:237
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:821
getModulePath()
Get the path to this module.
Definition ApiBase.php:621
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:172
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:239
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:542
This class contains a list of pages that the client has requested.
This implements prop=redirects, prop=linkshere, prop=catmembers, prop=transcludedin,...
executeGenerator( $resultPageSet)
Execute this module as a generator.
getHelpUrls()
Return links to more detailed help pages about the module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getExamplesMessages()
Returns usage examples for this module.
__construct(ApiQuery $query, $moduleName, LinksMigration $linksMigration)
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
addWhereIf( $value, $condition)
Same as addWhere(), but add the WHERE clauses only if a condition is met.
addFields( $value)
Add a set of fields to select to the internal array.
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
getDB()
Get the Query database connection (read-only)
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addWhere( $value)
Add a set of WHERE clauses to the internal array.
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:43
Service for compat reading of links tables.
A class containing constants representing the names of configuration variables.
Represents a title within MediaWiki.
Definition Title.php:79
Service for formatting and validating API parameters.
Type definition for integer types.
Representing a group of expressions chained via OR.