MediaWiki  1.34.0
ApiQueryBacklinksprop.php
Go to the documentation of this file.
1 <?php
34 
35  // Data for the various modules implemented by this class
36  private static $settings = [
37  'redirects' => [
38  'code' => 'rd',
39  'prefix' => 'rd',
40  'linktable' => 'redirect',
41  'props' => [
42  'fragment',
43  ],
44  'showredirects' => false,
45  'show' => [
46  'fragment',
47  '!fragment',
48  ],
49  ],
50  'linkshere' => [
51  'code' => 'lh',
52  'prefix' => 'pl',
53  'linktable' => 'pagelinks',
54  'indexes' => [ 'pl_namespace', 'pl_backlinks_namespace' ],
55  'from_namespace' => true,
56  'showredirects' => true,
57  ],
58  'transcludedin' => [
59  'code' => 'ti',
60  'prefix' => 'tl',
61  'linktable' => 'templatelinks',
62  'indexes' => [ 'tl_namespace', 'tl_backlinks_namespace' ],
63  'from_namespace' => true,
64  'showredirects' => true,
65  ],
66  'fileusage' => [
67  'code' => 'fu',
68  'prefix' => 'il',
69  'linktable' => 'imagelinks',
70  'indexes' => [ 'il_to', 'il_backlinks_namespace' ],
71  'from_namespace' => true,
72  'to_namespace' => NS_FILE,
73  'exampletitle' => 'File:Example.jpg',
74  'showredirects' => true,
75  ],
76  ];
77 
78  public function __construct( ApiQuery $query, $moduleName ) {
79  parent::__construct( $query, $moduleName, self::$settings[$moduleName]['code'] );
80  }
81 
82  public function execute() {
83  $this->run();
84  }
85 
86  public function executeGenerator( $resultPageSet ) {
87  $this->run( $resultPageSet );
88  }
89 
93  private function run( ApiPageSet $resultPageSet = null ) {
94  $settings = self::$settings[$this->getModuleName()];
95 
96  $db = $this->getDB();
97  $params = $this->extractRequestParams();
98  $prop = array_flip( $params['prop'] );
99  $emptyString = $db->addQuotes( '' );
100 
101  $pageSet = $this->getPageSet();
102  $titles = $pageSet->getGoodAndMissingTitles();
103  $map = $pageSet->getGoodAndMissingTitlesByNamespace();
104 
105  // Add in special pages, they can theoretically have backlinks too.
106  // (although currently they only do for prop=redirects)
107  foreach ( $pageSet->getSpecialTitles() as $id => $title ) {
108  $titles[] = $title;
109  $map[$title->getNamespace()][$title->getDBkey()] = $id;
110  }
111 
112  // Determine our fields to query on
113  $p = $settings['prefix'];
114  $hasNS = !isset( $settings['to_namespace'] );
115  if ( $hasNS ) {
116  $bl_namespace = "{$p}_namespace";
117  $bl_title = "{$p}_title";
118  } else {
119  $bl_namespace = $settings['to_namespace'];
120  $bl_title = "{$p}_to";
121 
122  $titles = array_filter( $titles, function ( $t ) use ( $bl_namespace ) {
123  return $t->getNamespace() === $bl_namespace;
124  } );
125  $map = array_intersect_key( $map, [ $bl_namespace => true ] );
126  }
127  $bl_from = "{$p}_from";
128 
129  if ( !$titles ) {
130  return; // nothing to do
131  }
132  if ( $params['namespace'] !== null && count( $params['namespace'] ) === 0 ) {
133  return; // nothing to do
134  }
135 
136  // Figure out what we're sorting by, and add associated WHERE clauses.
137  // MySQL's query planner screws up if we include a field in ORDER BY
138  // when it's constant in WHERE, so we have to test that for each field.
139  $sortby = [];
140  if ( $hasNS && count( $map ) > 1 ) {
141  $sortby[$bl_namespace] = 'ns';
142  }
143  $theTitle = null;
144  foreach ( $map as $nsTitles ) {
145  reset( $nsTitles );
146  $key = key( $nsTitles );
147  if ( $theTitle === null ) {
148  $theTitle = $key;
149  }
150  if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
151  $sortby[$bl_title] = 'title';
152  break;
153  }
154  }
155  $miser_ns = null;
156  if ( $params['namespace'] !== null ) {
157  if ( empty( $settings['from_namespace'] ) ) {
158  if ( $this->getConfig()->get( 'MiserMode' ) ) {
159  $miser_ns = $params['namespace'];
160  } else {
161  $this->addWhereFld( 'page_namespace', $params['namespace'] );
162  }
163  } else {
164  $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
165  if ( !empty( $settings['from_namespace'] )
166  && $params['namespace'] !== null && count( $params['namespace'] ) > 1
167  ) {
168  $sortby["{$p}_from_namespace"] = 'int';
169  }
170  }
171  }
172  $sortby[$bl_from] = 'int';
173 
174  // Now use the $sortby to figure out the continuation
175  if ( !is_null( $params['continue'] ) ) {
176  $cont = explode( '|', $params['continue'] );
177  $this->dieContinueUsageIf( count( $cont ) != count( $sortby ) );
178  $where = '';
179  $i = count( $sortby ) - 1;
180  foreach ( array_reverse( $sortby, true ) as $field => $type ) {
181  $v = $cont[$i];
182  switch ( $type ) {
183  case 'ns':
184  case 'int':
185  $v = (int)$v;
186  $this->dieContinueUsageIf( $v != $cont[$i] );
187  break;
188  default:
189  $v = $db->addQuotes( $v );
190  break;
191  }
192 
193  if ( $where === '' ) {
194  $where = "$field >= $v";
195  } else {
196  $where = "$field > $v OR ($field = $v AND ($where))";
197  }
198 
199  $i--;
200  }
201  $this->addWhere( $where );
202  }
203 
204  // Populate the rest of the query
205  $this->addTables( [ $settings['linktable'], 'page' ] );
206  $this->addWhere( "$bl_from = page_id" );
207 
208  if ( $this->getModuleName() === 'redirects' ) {
209  $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" );
210  }
211 
212  $this->addFields( array_keys( $sortby ) );
213  $this->addFields( [ 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ] );
214  if ( is_null( $resultPageSet ) ) {
215  $fld_pageid = isset( $prop['pageid'] );
216  $fld_title = isset( $prop['title'] );
217  $fld_redirect = isset( $prop['redirect'] );
218 
219  $this->addFieldsIf( 'page_id', $fld_pageid );
220  $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
221  $this->addFieldsIf( 'page_is_redirect', $fld_redirect );
222 
223  // prop=redirects
224  $fld_fragment = isset( $prop['fragment'] );
225  $this->addFieldsIf( 'rd_fragment', $fld_fragment );
226  } else {
227  $this->addFields( $resultPageSet->getPageTableFields() );
228  }
229 
230  $this->addFieldsIf( 'page_namespace', $miser_ns !== null );
231 
232  if ( $hasNS ) {
233  // Can't use LinkBatch because it throws away Special titles.
234  // And we already have the needed data structure anyway.
235  $this->addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
236  } else {
237  $where = [];
238  foreach ( $titles as $t ) {
239  if ( $t->getNamespace() == $bl_namespace ) {
240  $where[] = "$bl_title = " . $db->addQuotes( $t->getDBkey() );
241  }
242  }
243  $this->addWhere( $db->makeList( $where, LIST_OR ) );
244  }
245 
246  if ( $params['show'] !== null ) {
247  // prop=redirects only
248  $show = array_flip( $params['show'] );
249  if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ||
250  isset( $show['redirect'] ) && isset( $show['!redirect'] )
251  ) {
252  $this->dieWithError( 'apierror-show' );
253  }
254  $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) );
255  $this->addWhereIf(
256  "rd_fragment = $emptyString OR rd_fragment IS NULL",
257  isset( $show['!fragment'] )
258  );
259  $this->addWhereIf( [ 'page_is_redirect' => 1 ], isset( $show['redirect'] ) );
260  $this->addWhereIf( [ 'page_is_redirect' => 0 ], isset( $show['!redirect'] ) );
261  }
262 
263  // Override any ORDER BY from above with what we calculated earlier.
264  $this->addOption( 'ORDER BY', array_keys( $sortby ) );
265 
266  // MySQL's optimizer chokes if we have too many values in "$bl_title IN
267  // (...)" and chooses the wrong index, so specify the correct index to
268  // use for the query. See T139056 for details.
269  if ( !empty( $settings['indexes'] ) ) {
270  list( $idxNoFromNS, $idxWithFromNS ) = $settings['indexes'];
271  if ( $params['namespace'] !== null && !empty( $settings['from_namespace'] ) ) {
272  $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxWithFromNS ] );
273  } else {
274  $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxNoFromNS ] );
275  }
276  }
277 
278  // MySQL (or at least 5.5.5-10.0.23-MariaDB) chooses a really bad query
279  // plan if it thinks there will be more matching rows in the linktable
280  // than are in page. Use STRAIGHT_JOIN here to force it to use the
281  // intended, fast plan. See T145079 for details.
282  $this->addOption( 'STRAIGHT_JOIN' );
283 
284  $this->addOption( 'LIMIT', $params['limit'] + 1 );
285 
286  $res = $this->select( __METHOD__ );
287 
288  if ( is_null( $resultPageSet ) ) {
289  if ( $fld_title ) {
290  $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
291  }
292 
293  $count = 0;
294  foreach ( $res as $row ) {
295  if ( ++$count > $params['limit'] ) {
296  // We've reached the one extra which shows that
297  // there are additional pages to be had. Stop here...
298  $this->setContinue( $row, $sortby );
299  break;
300  }
301 
302  if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
303  // Miser mode namespace check
304  continue;
305  }
306 
307  // Get the ID of the current page
308  $id = $map[$row->bl_namespace][$row->bl_title];
309 
310  $vals = [];
311  if ( $fld_pageid ) {
312  $vals['pageid'] = (int)$row->page_id;
313  }
314  if ( $fld_title ) {
316  Title::makeTitle( $row->page_namespace, $row->page_title )
317  );
318  }
319  if ( $fld_fragment && $row->rd_fragment !== null && $row->rd_fragment !== '' ) {
320  $vals['fragment'] = $row->rd_fragment;
321  }
322  if ( $fld_redirect ) {
323  $vals['redirect'] = (bool)$row->page_is_redirect;
324  }
325  $fit = $this->addPageSubItem( $id, $vals );
326  if ( !$fit ) {
327  $this->setContinue( $row, $sortby );
328  break;
329  }
330  }
331  } else {
332  $titles = [];
333  $count = 0;
334  foreach ( $res as $row ) {
335  if ( ++$count > $params['limit'] ) {
336  // We've reached the one extra which shows that
337  // there are additional pages to be had. Stop here...
338  $this->setContinue( $row, $sortby );
339  break;
340  }
341 
342  if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
343  // Miser mode namespace check
344  continue;
345  }
346 
347  $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
348  }
349  $resultPageSet->populateFromTitles( $titles );
350  }
351  }
352 
353  private function setContinue( $row, $sortby ) {
354  $cont = [];
355  foreach ( $sortby as $field => $v ) {
356  $cont[] = $row->$field;
357  }
358  $this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
359  }
360 
361  public function getCacheMode( $params ) {
362  return 'public';
363  }
364 
365  public function getAllowedParams() {
366  $settings = self::$settings[$this->getModuleName()];
367 
368  $ret = [
369  'prop' => [
371  'pageid',
372  'title',
373  ],
374  ApiBase::PARAM_ISMULTI => true,
375  ApiBase::PARAM_DFLT => 'pageid|title',
377  ],
378  'namespace' => [
379  ApiBase::PARAM_ISMULTI => true,
380  ApiBase::PARAM_TYPE => 'namespace',
381  ],
382  'show' => null, // Will be filled/removed below
383  'limit' => [
384  ApiBase::PARAM_DFLT => 10,
385  ApiBase::PARAM_TYPE => 'limit',
386  ApiBase::PARAM_MIN => 1,
389  ],
390  'continue' => [
391  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
392  ],
393  ];
394 
395  if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
396  $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
397  'api-help-param-limited-in-miser-mode',
398  ];
399  }
400 
401  if ( !empty( $settings['showredirects'] ) ) {
402  $ret['prop'][ApiBase::PARAM_TYPE][] = 'redirect';
403  $ret['prop'][ApiBase::PARAM_DFLT] .= '|redirect';
404  }
405  if ( isset( $settings['props'] ) ) {
406  $ret['prop'][ApiBase::PARAM_TYPE] = array_merge(
407  $ret['prop'][ApiBase::PARAM_TYPE], $settings['props']
408  );
409  }
410 
411  $show = [];
412  if ( !empty( $settings['showredirects'] ) ) {
413  $show[] = 'redirect';
414  $show[] = '!redirect';
415  }
416  if ( isset( $settings['show'] ) ) {
417  $show = array_merge( $show, $settings['show'] );
418  }
419  if ( $show ) {
420  $ret['show'] = [
421  ApiBase::PARAM_TYPE => $show,
422  ApiBase::PARAM_ISMULTI => true,
423  ];
424  } else {
425  unset( $ret['show'] );
426  }
427 
428  return $ret;
429  }
430 
431  protected function getExamplesMessages() {
432  $settings = self::$settings[$this->getModuleName()];
433  $name = $this->getModuleName();
434  $path = $this->getModulePath();
435  $title = $settings['exampletitle'] ?? 'Main Page';
436  $etitle = rawurlencode( $title );
437 
438  return [
439  "action=query&prop={$name}&titles={$etitle}"
440  => "apihelp-$path-example-simple",
441  "action=query&generator={$name}&titles={$etitle}&prop=info"
442  => "apihelp-$path-example-generator",
443  ];
444  }
445 
446  public function getHelpUrls() {
447  $name = ucfirst( $this->getModuleName() );
448  return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
449  }
450 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:193
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:131
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiQueryBacklinksprop\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryBacklinksprop.php:431
NS_FILE
const NS_FILE
Definition: Defines.php:66
ApiQueryBacklinksprop\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryBacklinksprop.php:82
ApiQueryBacklinksprop\run
run(ApiPageSet $resultPageSet=null)
Definition: ApiQueryBacklinksprop.php:93
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:350
ApiQueryBacklinksprop\executeGenerator
executeGenerator( $resultPageSet)
Execute this module as a generator.
Definition: ApiQueryBacklinksprop.php:86
$res
$res
Definition: testCompression.php:52
ApiQueryBacklinksprop\setContinue
setContinue( $row, $sortby)
Definition: ApiQueryBacklinksprop.php:353
ApiBase\PARAM_HELP_MSG_APPEND
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition: ApiBase.php:138
ApiQueryBase\addFieldsIf
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
Definition: ApiQueryBase.php:207
ApiPageSet
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:40
ApiQueryBacklinksprop\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryBacklinksprop.php:78
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:84
ApiBase\PARAM_MIN
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:106
ApiQueryBase\executeGenderCacheFromResultWrapper
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
Definition: ApiQueryBase.php:582
LIST_OR
const LIST_OR
Definition: Defines.php:42
ApiQueryGeneratorBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryGeneratorBase.php:58
ApiBase\getModulePath
getModulePath()
Get the path to this module.
Definition: ApiBase.php:584
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:259
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only)
Definition: ApiQueryBase.php:107
ApiBase\PARAM_MAX
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:97
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:161
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:375
$t
$t
Definition: make-normalization-table.php:143
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
ApiQueryBacklinksprop\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryBacklinksprop.php:361
$title
$title
Definition: testCompression.php:34
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
ApiQueryBacklinksprop\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryBacklinksprop.php:446
ApiQueryBase\$where
$where
Definition: ApiQueryBase.php:37
ApiQueryBacklinksprop\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryBacklinksprop.php:365
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:2208
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
Definition: ApiQueryBase.php:261
ApiQueryGeneratorBase
Definition: ApiQueryGeneratorBase.php:26
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:261
$path
$path
Definition: NoLocalSettings.php:25
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
ApiQueryBacklinksprop
This implements prop=redirects, prop=linkshere, prop=catmembers, prop=transcludedin,...
Definition: ApiQueryBacklinksprop.php:33
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
ApiBase\PARAM_MAX2
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right,...
Definition: ApiBase.php:103
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:228
ApiBase\PARAM_HELP_MSG_PER_VALUE
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition: ApiBase.php:164
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:471
ApiQueryBase\addWhereIf
addWhereIf( $value, $condition)
Same as addWhere(), but add the WHERE clauses only if a condition is met.
Definition: ApiQueryBase.php:246
ApiQueryBacklinksprop\$settings
static $settings
Definition: ApiQueryBacklinksprop.php:36
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:443
$type
$type
Definition: testCompression.php:48