class ApiQueryBacklinksprop extends ApiQueryGeneratorBase {
// Data for the various modules implemented by this class
- private static $settings = array(
- 'redirects' => array(
+ private static $settings = [
+ 'redirects' => [
'code' => 'rd',
'prefix' => 'rd',
'linktable' => 'redirect',
- 'props' => array(
+ 'props' => [
'fragment',
- ),
+ ],
'showredirects' => false,
- 'show' => array(
+ 'show' => [
'fragment',
'!fragment',
- ),
- ),
- 'linkshere' => array(
+ ],
+ ],
+ 'linkshere' => [
'code' => 'lh',
'prefix' => 'pl',
'linktable' => 'pagelinks',
+ 'indexes' => [ 'pl_namespace', 'pl_backlinks_namespace' ],
'from_namespace' => true,
'showredirects' => true,
- ),
- 'transcludedin' => array(
+ ],
+ 'transcludedin' => [
'code' => 'ti',
'prefix' => 'tl',
'linktable' => 'templatelinks',
+ 'indexes' => [ 'tl_namespace', 'tl_backlinks_namespace' ],
'from_namespace' => true,
'showredirects' => true,
- ),
- 'fileusage' => array(
+ ],
+ 'fileusage' => [
'code' => 'fu',
'prefix' => 'il',
'linktable' => 'imagelinks',
+ 'indexes' => [ 'il_to', 'il_backlinks_namespace' ],
'from_namespace' => true,
'to_namespace' => NS_FILE,
'exampletitle' => 'File:Example.jpg',
'showredirects' => true,
- ),
- );
+ ],
+ ];
public function __construct( ApiQuery $query, $moduleName ) {
parent::__construct( $query, $moduleName, self::$settings[$moduleName]['code'] );
$titles = array_filter( $titles, function ( $t ) use ( $bl_namespace ) {
return $t->getNamespace() === $bl_namespace;
} );
- $map = array_intersect_key( $map, array( $bl_namespace => true ) );
+ $map = array_intersect_key( $map, [ $bl_namespace => true ] );
}
$bl_from = "{$p}_from";
// Figure out what we're sorting by, and add associated WHERE clauses.
// MySQL's query planner screws up if we include a field in ORDER BY
// when it's constant in WHERE, so we have to test that for each field.
- $sortby = array();
+ $sortby = [];
if ( $hasNS && count( $map ) > 1 ) {
$sortby[$bl_namespace] = 'ns';
}
}
$miser_ns = null;
if ( $params['namespace'] !== null ) {
- if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
- $miser_ns = $params['namespace'];
+ if ( empty( $settings['from_namespace'] ) ) {
+ if ( $this->getConfig()->get( 'MiserMode' ) ) {
+ $miser_ns = $params['namespace'];
+ } else {
+ $this->addWhereFld( 'page_namespace', $params['namespace'] );
+ }
} else {
$this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
if ( !empty( $settings['from_namespace'] ) && count( $params['namespace'] ) > 1 ) {
$this->dieContinueUsageIf( count( $cont ) != count( $sortby ) );
$where = '';
$i = count( $sortby ) - 1;
- $cont_ns = 0;
- $cont_title = '';
foreach ( array_reverse( $sortby, true ) as $field => $type ) {
$v = $cont[$i];
switch ( $type ) {
case 'ns':
- $cont_ns = (int)$v;
- /* fall through */
case 'int':
$v = (int)$v;
$this->dieContinueUsageIf( $v != $cont[$i] );
break;
-
- case 'title':
- $cont_title = $v;
- /* fall through */
default:
$v = $db->addQuotes( $v );
break;
}
// Populate the rest of the query
- $this->addTables( array( $settings['linktable'], 'page' ) );
+ $this->addTables( [ $settings['linktable'], 'page' ] );
$this->addWhere( "$bl_from = page_id" );
if ( $this->getModuleName() === 'redirects' ) {
}
$this->addFields( array_keys( $sortby ) );
- $this->addFields( array( 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ) );
+ $this->addFields( [ 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ] );
if ( is_null( $resultPageSet ) ) {
$fld_pageid = isset( $prop['pageid'] );
$fld_title = isset( $prop['title'] );
$fld_redirect = isset( $prop['redirect'] );
$this->addFieldsIf( 'page_id', $fld_pageid );
- $this->addFieldsIf( array( 'page_title', 'page_namespace' ), $fld_title );
+ $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
$this->addFieldsIf( 'page_is_redirect', $fld_redirect );
// prop=redirects
$lb = new LinkBatch( $titles );
$this->addWhere( $lb->constructSet( $p, $db ) );
} else {
- $where = array();
+ $where = [];
foreach ( $titles as $t ) {
if ( $t->getNamespace() == $bl_namespace ) {
$where[] = "$bl_title = " . $db->addQuotes( $t->getDBkey() );
"rd_fragment = $emptyString OR rd_fragment IS NULL",
isset( $show['!fragment'] )
);
- $this->addWhereIf( array( 'page_is_redirect' => 1 ), isset( $show['redirect'] ) );
- $this->addWhereIf( array( 'page_is_redirect' => 0 ), isset( $show['!redirect'] ) );
+ $this->addWhereIf( [ 'page_is_redirect' => 1 ], isset( $show['redirect'] ) );
+ $this->addWhereIf( [ 'page_is_redirect' => 0 ], isset( $show['!redirect'] ) );
}
// Override any ORDER BY from above with what we calculated earlier.
$this->addOption( 'ORDER BY', array_keys( $sortby ) );
+ // MySQL's optimizer chokes if we have too many values in "$bl_title IN
+ // (...)" and chooses the wrong index, so specify the correct index to
+ // use for the query. See T139056 for details.
+ if ( !empty( $settings['indexes'] ) ) {
+ list( $idxNoFromNS, $idxWithFromNS ) = $settings['indexes'];
+ if ( $params['namespace'] !== null && !empty( $settings['from_namespace'] ) ) {
+ $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxWithFromNS ] );
+ } else {
+ $this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxNoFromNS ] );
+ }
+ }
+
$this->addOption( 'LIMIT', $params['limit'] + 1 );
$res = $this->select( __METHOD__ );
// Get the ID of the current page
$id = $map[$row->bl_namespace][$row->bl_title];
- $vals = array();
+ $vals = [];
if ( $fld_pageid ) {
$vals['pageid'] = (int)$row->page_id;
}
}
}
} else {
- $titles = array();
+ $titles = [];
$count = 0;
foreach ( $res as $row ) {
if ( ++$count > $params['limit'] ) {
}
private function setContinue( $row, $sortby ) {
- $cont = array();
+ $cont = [];
foreach ( $sortby as $field => $v ) {
$cont[] = $row->$field;
}
- $this->setContinueEnumParameter( 'continue', join( '|', $cont ) );
+ $this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
}
public function getCacheMode( $params ) {
public function getAllowedParams() {
$settings = self::$settings[$this->getModuleName()];
- $ret = array(
- 'prop' => array(
- ApiBase::PARAM_TYPE => array(
+ $ret = [
+ 'prop' => [
+ ApiBase::PARAM_TYPE => [
'pageid',
'title',
- ),
+ ],
ApiBase::PARAM_ISMULTI => true,
ApiBase::PARAM_DFLT => 'pageid|title',
- ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
- ),
- 'namespace' => array(
+ ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
+ ],
+ 'namespace' => [
ApiBase::PARAM_ISMULTI => true,
ApiBase::PARAM_TYPE => 'namespace',
- ),
+ ],
'show' => null, // Will be filled/removed below
- 'limit' => array(
+ 'limit' => [
ApiBase::PARAM_DFLT => 10,
ApiBase::PARAM_TYPE => 'limit',
ApiBase::PARAM_MIN => 1,
ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
- ),
- 'continue' => array(
+ ],
+ 'continue' => [
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
- ),
- );
+ ],
+ ];
if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) {
- $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = array(
+ $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
'api-help-param-limited-in-miser-mode',
- );
+ ];
}
if ( !empty( $settings['showredirects'] ) ) {
);
}
- $show = array();
+ $show = [];
if ( !empty( $settings['showredirects'] ) ) {
$show[] = 'redirect';
$show[] = '!redirect';
$show = array_merge( $show, $settings['show'] );
}
if ( $show ) {
- $ret['show'] = array(
+ $ret['show'] = [
ApiBase::PARAM_TYPE => $show,
ApiBase::PARAM_ISMULTI => true,
- );
+ ];
} else {
unset( $ret['show'] );
}
$title = isset( $settings['exampletitle'] ) ? $settings['exampletitle'] : 'Main Page';
$etitle = rawurlencode( $title );
- return array(
+ return [
"action=query&prop={$name}&titles={$etitle}"
=> "apihelp-$path-example-simple",
"action=query&generator={$name}&titles={$etitle}&prop=info"
=> "apihelp-$path-example-generator",
- );
+ ];
}
public function getHelpUrls() {