2014-08-20 17:32:01 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* API module to handle links table back-queries
|
|
|
|
|
*
|
2017-06-13 16:51:53 +00:00
|
|
|
* Copyright © 2014 Wikimedia Foundation and contributors
|
2014-08-20 17:32:01 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @since 1.24
|
|
|
|
|
*/
|
|
|
|
|
|
2024-09-25 16:17:29 +00:00
|
|
|
namespace MediaWiki\Api;
|
|
|
|
|
|
2022-04-21 19:35:30 +00:00
|
|
|
use MediaWiki\Linker\LinksMigration;
|
2022-04-13 15:28:26 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-06-05 23:18:50 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
|
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
|
2022-04-13 15:28:26 +00:00
|
|
|
|
2014-08-20 17:32:01 +00:00
|
|
|
/**
|
|
|
|
|
* This implements prop=redirects, prop=linkshere, prop=catmembers,
|
|
|
|
|
* prop=transcludedin, and prop=fileusage
|
|
|
|
|
*
|
|
|
|
|
* @ingroup API
|
|
|
|
|
* @since 1.24
|
|
|
|
|
*/
|
|
|
|
|
class ApiQueryBacklinksprop extends ApiQueryGeneratorBase {
|
|
|
|
|
|
2020-10-28 19:44:09 +00:00
|
|
|
/** @var array Data for the various modules implemented by this class */
|
2016-02-17 09:09:32 +00:00
|
|
|
private static $settings = [
|
|
|
|
|
'redirects' => [
|
2014-08-20 17:32:01 +00:00
|
|
|
'code' => 'rd',
|
|
|
|
|
'prefix' => 'rd',
|
|
|
|
|
'linktable' => 'redirect',
|
2016-02-17 09:09:32 +00:00
|
|
|
'props' => [
|
2014-09-18 17:38:23 +00:00
|
|
|
'fragment',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-08-20 17:32:01 +00:00
|
|
|
'showredirects' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
'show' => [
|
2014-09-18 17:38:23 +00:00
|
|
|
'fragment',
|
|
|
|
|
'!fragment',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'linkshere' => [
|
2014-08-20 17:32:01 +00:00
|
|
|
'code' => 'lh',
|
|
|
|
|
'prefix' => 'pl',
|
|
|
|
|
'linktable' => 'pagelinks',
|
2022-05-24 16:31:59 +00:00
|
|
|
'indexes' => [ 'pl_namespace', 'pl_backlinks_namespace' ],
|
2014-08-20 17:32:01 +00:00
|
|
|
'from_namespace' => true,
|
|
|
|
|
'showredirects' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'transcludedin' => [
|
2014-08-20 17:32:01 +00:00
|
|
|
'code' => 'ti',
|
|
|
|
|
'prefix' => 'tl',
|
|
|
|
|
'linktable' => 'templatelinks',
|
|
|
|
|
'from_namespace' => true,
|
|
|
|
|
'showredirects' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'fileusage' => [
|
2014-08-20 17:32:01 +00:00
|
|
|
'code' => 'fu',
|
|
|
|
|
'prefix' => 'il',
|
|
|
|
|
'linktable' => 'imagelinks',
|
2022-05-24 16:31:59 +00:00
|
|
|
'indexes' => [ 'il_to', 'il_backlinks_namespace' ],
|
2014-08-20 17:32:01 +00:00
|
|
|
'from_namespace' => true,
|
|
|
|
|
'to_namespace' => NS_FILE,
|
|
|
|
|
'exampletitle' => 'File:Example.jpg',
|
|
|
|
|
'showredirects' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2014-08-20 17:32:01 +00:00
|
|
|
|
2023-08-28 15:32:58 +00:00
|
|
|
private LinksMigration $linksMigration;
|
2022-04-21 19:35:30 +00:00
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
ApiQuery $query,
|
2024-10-14 20:12:27 +00:00
|
|
|
string $moduleName,
|
2022-04-21 19:35:30 +00:00
|
|
|
LinksMigration $linksMigration
|
|
|
|
|
) {
|
2014-08-20 17:32:01 +00:00
|
|
|
parent::__construct( $query, $moduleName, self::$settings[$moduleName]['code'] );
|
2022-04-21 19:35:30 +00:00
|
|
|
$this->linksMigration = $linksMigration;
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
$this->run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function executeGenerator( $resultPageSet ) {
|
|
|
|
|
$this->run( $resultPageSet );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param ApiPageSet|null $resultPageSet
|
2014-08-20 17:32:01 +00:00
|
|
|
*/
|
2024-10-16 18:58:33 +00:00
|
|
|
private function run( ?ApiPageSet $resultPageSet = null ) {
|
2014-08-20 17:32:01 +00:00
|
|
|
$settings = self::$settings[$this->getModuleName()];
|
|
|
|
|
|
|
|
|
|
$db = $this->getDB();
|
|
|
|
|
$params = $this->extractRequestParams();
|
2021-06-11 02:52:06 +00:00
|
|
|
$prop = array_fill_keys( $params['prop'], true );
|
2014-08-20 17:32:01 +00:00
|
|
|
|
|
|
|
|
$pageSet = $this->getPageSet();
|
2021-04-30 14:41:54 +00:00
|
|
|
$titles = $pageSet->getGoodAndMissingPages();
|
2014-09-26 14:56:00 +00:00
|
|
|
$map = $pageSet->getGoodAndMissingTitlesByNamespace();
|
2014-08-20 17:32:01 +00:00
|
|
|
|
API: Allow finding log events and links to special pages
Log events are sometimes attributed to a special page; it should be
allowed to use rcnamespace or lenamespace to filter for these.
It's also possible for special pages to be the targets of redirects, so
list=allredirects and prop=redirects should find them.
Maybe someday we'll record links to and transclusions of special pages
too (see T19597), so we may as well make it possible to query for those
as well via list=alllinks, list=alltransclusions, list=backlinks,
list=embeddedin, prop=linkshere, prop=transcludedin, prop=links, and
prop=templates.
NS_MEDIA has similar considerations: although we currently "normalize"
page links to the corresponding File and I don't think anything logs the
Media title rather than the File, transclusions and redirects do show
up in those tables.
Bug: T154319
Change-Id: I00348f83855c6c703b6bd6015f6d3bedc5bfd1c5
2017-01-06 17:49:27 +00:00
|
|
|
// Add in special pages, they can theoretically have backlinks too.
|
|
|
|
|
// (although currently they only do for prop=redirects)
|
2021-04-30 14:41:54 +00:00
|
|
|
foreach ( $pageSet->getSpecialPages() as $id => $title ) {
|
API: Allow finding log events and links to special pages
Log events are sometimes attributed to a special page; it should be
allowed to use rcnamespace or lenamespace to filter for these.
It's also possible for special pages to be the targets of redirects, so
list=allredirects and prop=redirects should find them.
Maybe someday we'll record links to and transclusions of special pages
too (see T19597), so we may as well make it possible to query for those
as well via list=alllinks, list=alltransclusions, list=backlinks,
list=embeddedin, prop=linkshere, prop=transcludedin, prop=links, and
prop=templates.
NS_MEDIA has similar considerations: although we currently "normalize"
page links to the corresponding File and I don't think anything logs the
Media title rather than the File, transclusions and redirects do show
up in those tables.
Bug: T154319
Change-Id: I00348f83855c6c703b6bd6015f6d3bedc5bfd1c5
2017-01-06 17:49:27 +00:00
|
|
|
$titles[] = $title;
|
|
|
|
|
$map[$title->getNamespace()][$title->getDBkey()] = $id;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-20 17:32:01 +00:00
|
|
|
// Determine our fields to query on
|
|
|
|
|
$p = $settings['prefix'];
|
|
|
|
|
$hasNS = !isset( $settings['to_namespace'] );
|
|
|
|
|
if ( $hasNS ) {
|
2022-04-21 19:35:30 +00:00
|
|
|
if ( isset( $this->linksMigration::$mapping[$settings['linktable']] ) ) {
|
|
|
|
|
[ $bl_namespace, $bl_title ] = $this->linksMigration->getTitleFields( $settings['linktable'] );
|
2022-04-20 20:18:59 +00:00
|
|
|
} else {
|
|
|
|
|
$bl_namespace = "{$p}_namespace";
|
|
|
|
|
$bl_title = "{$p}_title";
|
|
|
|
|
}
|
2014-08-20 17:32:01 +00:00
|
|
|
} else {
|
2022-03-28 20:10:05 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
|
2014-08-20 17:32:01 +00:00
|
|
|
$bl_namespace = $settings['to_namespace'];
|
|
|
|
|
$bl_title = "{$p}_to";
|
|
|
|
|
|
2021-02-10 22:31:02 +00:00
|
|
|
$titles = array_filter( $titles, static function ( $t ) use ( $bl_namespace ) {
|
2014-08-20 17:32:01 +00:00
|
|
|
return $t->getNamespace() === $bl_namespace;
|
|
|
|
|
} );
|
2016-02-17 09:09:32 +00:00
|
|
|
$map = array_intersect_key( $map, [ $bl_namespace => true ] );
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
$bl_from = "{$p}_from";
|
|
|
|
|
|
|
|
|
|
if ( !$titles ) {
|
|
|
|
|
return; // nothing to do
|
|
|
|
|
}
|
2018-10-01 14:18:40 +00:00
|
|
|
if ( $params['namespace'] !== null && count( $params['namespace'] ) === 0 ) {
|
|
|
|
|
return; // nothing to do
|
|
|
|
|
}
|
2014-08-20 17:32:01 +00:00
|
|
|
|
|
|
|
|
// 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.
|
2016-02-17 09:09:32 +00:00
|
|
|
$sortby = [];
|
2014-08-20 17:32:01 +00:00
|
|
|
if ( $hasNS && count( $map ) > 1 ) {
|
2022-09-16 12:18:55 +00:00
|
|
|
$sortby[$bl_namespace] = 'int';
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
$theTitle = null;
|
|
|
|
|
foreach ( $map as $nsTitles ) {
|
2022-10-21 03:55:44 +00:00
|
|
|
$key = array_key_first( $nsTitles );
|
2022-12-16 23:48:27 +00:00
|
|
|
$theTitle ??= $key;
|
2014-08-20 17:32:01 +00:00
|
|
|
if ( count( $nsTitles ) > 1 || $key !== $theTitle ) {
|
2022-09-16 12:18:55 +00:00
|
|
|
$sortby[$bl_title] = 'string';
|
2014-08-20 17:32:01 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$miser_ns = null;
|
|
|
|
|
if ( $params['namespace'] !== null ) {
|
2015-09-23 13:31:39 +00:00
|
|
|
if ( empty( $settings['from_namespace'] ) ) {
|
2022-04-13 15:28:26 +00:00
|
|
|
if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
|
2015-09-23 13:31:39 +00:00
|
|
|
$miser_ns = $params['namespace'];
|
|
|
|
|
} else {
|
|
|
|
|
$this->addWhereFld( 'page_namespace', $params['namespace'] );
|
|
|
|
|
}
|
2014-08-20 17:32:01 +00:00
|
|
|
} else {
|
|
|
|
|
$this->addWhereFld( "{$p}_from_namespace", $params['namespace'] );
|
2017-12-04 18:36:48 +00:00
|
|
|
if ( !empty( $settings['from_namespace'] )
|
|
|
|
|
&& $params['namespace'] !== null && count( $params['namespace'] ) > 1
|
|
|
|
|
) {
|
2014-08-20 17:32:01 +00:00
|
|
|
$sortby["{$p}_from_namespace"] = 'int';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$sortby[$bl_from] = 'int';
|
|
|
|
|
|
|
|
|
|
// Now use the $sortby to figure out the continuation
|
2022-09-16 12:18:55 +00:00
|
|
|
$continueFields = array_keys( $sortby );
|
|
|
|
|
$continueTypes = array_values( $sortby );
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $params['continue'] !== null ) {
|
2022-09-16 12:18:55 +00:00
|
|
|
$continueValues = $this->parseContinueParamOrDie( $params['continue'], $continueTypes );
|
|
|
|
|
$conds = array_combine( $continueFields, $continueValues );
|
|
|
|
|
$this->addWhere( $db->buildComparison( '>=', $conds ) );
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Populate the rest of the query
|
2022-05-24 16:31:59 +00:00
|
|
|
[ $idxNoFromNS, $idxWithFromNS ] = $settings['indexes'] ?? [ '', '' ];
|
2022-05-16 13:18:21 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
|
2022-04-21 19:35:30 +00:00
|
|
|
if ( isset( $this->linksMigration::$mapping[$settings['linktable']] ) ) {
|
2022-04-20 20:18:59 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
|
2022-04-21 19:35:30 +00:00
|
|
|
$queryInfo = $this->linksMigration->getQueryInfo( $settings['linktable'] );
|
2022-04-20 20:18:59 +00:00
|
|
|
$this->addTables( array_merge( [ 'page' ], $queryInfo['tables'] ) );
|
|
|
|
|
$this->addJoinConds( $queryInfo['joins'] );
|
2022-05-24 16:31:59 +00:00
|
|
|
// TODO: Move to links migration
|
|
|
|
|
if ( in_array( 'linktarget', $queryInfo['tables'] ) ) {
|
|
|
|
|
$idxWithFromNS .= '_target_id';
|
|
|
|
|
}
|
2022-04-20 20:18:59 +00:00
|
|
|
} else {
|
|
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
|
|
|
|
|
$this->addTables( [ $settings['linktable'], 'page' ] );
|
|
|
|
|
}
|
2014-08-20 17:32:01 +00:00
|
|
|
$this->addWhere( "$bl_from = page_id" );
|
|
|
|
|
|
|
|
|
|
if ( $this->getModuleName() === 'redirects' ) {
|
2023-10-03 17:29:38 +00:00
|
|
|
$this->addWhereFld( 'rd_interwiki', '' );
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->addFields( array_keys( $sortby ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->addFields( [ 'bl_namespace' => $bl_namespace, 'bl_title' => $bl_title ] );
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $resultPageSet === null ) {
|
2014-08-20 17:32:01 +00:00
|
|
|
$fld_pageid = isset( $prop['pageid'] );
|
|
|
|
|
$fld_title = isset( $prop['title'] );
|
|
|
|
|
$fld_redirect = isset( $prop['redirect'] );
|
|
|
|
|
|
|
|
|
|
$this->addFieldsIf( 'page_id', $fld_pageid );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
|
2014-08-20 17:32:01 +00:00
|
|
|
$this->addFieldsIf( 'page_is_redirect', $fld_redirect );
|
|
|
|
|
|
|
|
|
|
// prop=redirects
|
|
|
|
|
$fld_fragment = isset( $prop['fragment'] );
|
|
|
|
|
$this->addFieldsIf( 'rd_fragment', $fld_fragment );
|
|
|
|
|
} else {
|
|
|
|
|
$this->addFields( $resultPageSet->getPageTableFields() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->addFieldsIf( 'page_namespace', $miser_ns !== null );
|
|
|
|
|
|
rdbms: Throw when makeWhereFrom2d() receives empty data
This semi-internal method has only four callers, each of which
explicitly or implicitly ensures that $data is empty. For example:
* ApiQueryBacklinksprop: The caller returns early return if $titles
is empty, and then proceeds to build $map from $titles PageSet.
I've made it more explicit here for added confidence and to satisfy
static analysis.
* cleanupPreferences.php: The caller explicilty checks $data
(as $deleteWhere) before calling makeWhereFrom2d().
* ResourceLoader\WikiModule, the caller checks $batch->isEmpty()
before calling $batch->constructSet().
More importantly, is that not one of these checks whether
makeWhereFrom2d() returns false, and blindly passes it on to
methods like IDatabase::select() or IDatabase::delete(), which does
not permit boolean false as $where/$conds, but also doesn't check
for it, which seems seems likely to wreak havoc at some point through
casting to empty string or empty array at some point.
Given there are very few callers, and given that the current
responsiblity to check the return value is never excercised, throw
an exception instead to make this code appear less fragile.
Follow-up CR from
<https://gerrit.wikimedia.org/r/c/mediawiki/core/+/892988>
Change-Id: Ifdf3ea54271ea856f2f555a5e087b8f521e348b3
2023-04-30 18:29:52 +00:00
|
|
|
if ( $hasNS && $map ) {
|
API: Allow finding log events and links to special pages
Log events are sometimes attributed to a special page; it should be
allowed to use rcnamespace or lenamespace to filter for these.
It's also possible for special pages to be the targets of redirects, so
list=allredirects and prop=redirects should find them.
Maybe someday we'll record links to and transclusions of special pages
too (see T19597), so we may as well make it possible to query for those
as well via list=alllinks, list=alltransclusions, list=backlinks,
list=embeddedin, prop=linkshere, prop=transcludedin, prop=links, and
prop=templates.
NS_MEDIA has similar considerations: although we currently "normalize"
page links to the corresponding File and I don't think anything logs the
Media title rather than the File, transclusions and redirects do show
up in those tables.
Bug: T154319
Change-Id: I00348f83855c6c703b6bd6015f6d3bedc5bfd1c5
2017-01-06 17:49:27 +00:00
|
|
|
// Can't use LinkBatch because it throws away Special titles.
|
|
|
|
|
// And we already have the needed data structure anyway.
|
|
|
|
|
$this->addWhere( $db->makeWhereFrom2d( $map, $bl_namespace, $bl_title ) );
|
2014-08-20 17:32:01 +00:00
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$where = [];
|
2014-08-20 17:32:01 +00:00
|
|
|
foreach ( $titles as $t ) {
|
|
|
|
|
if ( $t->getNamespace() == $bl_namespace ) {
|
2023-10-30 19:10:26 +00:00
|
|
|
$where[] = $db->expr( $bl_title, '=', $t->getDBkey() );
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-29 20:31:02 +00:00
|
|
|
$this->addWhere( $db->orExpr( $where ) );
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $params['show'] !== null ) {
|
|
|
|
|
// prop=redirects only
|
2021-06-11 02:52:06 +00:00
|
|
|
$show = array_fill_keys( $params['show'], true );
|
2024-03-23 00:58:33 +00:00
|
|
|
if ( ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ) ||
|
|
|
|
|
( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
|
2014-08-20 17:32:01 +00:00
|
|
|
) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-show' );
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
2023-10-30 19:10:26 +00:00
|
|
|
$this->addWhereIf( $db->expr( 'rd_fragment', '!=', '' ), isset( $show['fragment'] ) );
|
2023-10-03 17:29:38 +00:00
|
|
|
$this->addWhereIf( [ 'rd_fragment' => '' ], isset( $show['!fragment'] ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->addWhereIf( [ 'page_is_redirect' => 1 ], isset( $show['redirect'] ) );
|
|
|
|
|
$this->addWhereIf( [ 'page_is_redirect' => 0 ], isset( $show['!redirect'] ) );
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Override any ORDER BY from above with what we calculated earlier.
|
|
|
|
|
$this->addOption( 'ORDER BY', array_keys( $sortby ) );
|
2022-05-24 16:31:59 +00:00
|
|
|
|
|
|
|
|
// 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'] ) ) {
|
2023-12-06 18:20:08 +00:00
|
|
|
if (
|
|
|
|
|
$params['namespace'] !== null &&
|
|
|
|
|
count( $params['namespace'] ) == 1 &&
|
|
|
|
|
!empty( $settings['from_namespace'] )
|
|
|
|
|
) {
|
2022-05-24 16:31:59 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
|
|
|
|
|
$this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxWithFromNS ] );
|
|
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
|
|
|
|
|
} elseif ( !isset( $this->linksMigration::$mapping[$settings['linktable']] ) ) {
|
|
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
|
|
|
|
|
$this->addOption( 'USE INDEX', [ $settings['linktable'] => $idxNoFromNS ] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-20 17:32:01 +00:00
|
|
|
$this->addOption( 'LIMIT', $params['limit'] + 1 );
|
|
|
|
|
|
|
|
|
|
$res = $this->select( __METHOD__ );
|
|
|
|
|
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $resultPageSet === null ) {
|
2022-03-29 18:11:06 +00:00
|
|
|
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
|
2019-10-12 08:40:22 +00:00
|
|
|
if ( $fld_title ) {
|
|
|
|
|
$this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
|
|
|
|
|
}
|
2019-09-10 17:42:58 +00:00
|
|
|
|
2014-08-20 17:32:01 +00:00
|
|
|
$count = 0;
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
if ( ++$count > $params['limit'] ) {
|
|
|
|
|
// We've reached the one extra which shows that
|
|
|
|
|
// there are additional pages to be had. Stop here...
|
|
|
|
|
$this->setContinue( $row, $sortby );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
|
|
|
|
|
// Miser mode namespace check
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the ID of the current page
|
|
|
|
|
$id = $map[$row->bl_namespace][$row->bl_title];
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$vals = [];
|
2022-03-29 18:11:06 +00:00
|
|
|
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
|
2014-08-20 17:32:01 +00:00
|
|
|
if ( $fld_pageid ) {
|
2015-05-06 15:33:08 +00:00
|
|
|
$vals['pageid'] = (int)$row->page_id;
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
if ( $fld_title ) {
|
|
|
|
|
ApiQueryBase::addTitleInfo( $vals,
|
|
|
|
|
Title::makeTitle( $row->page_namespace, $row->page_title )
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-03-29 18:11:06 +00:00
|
|
|
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
|
2023-10-03 17:29:38 +00:00
|
|
|
if ( $fld_fragment && $row->rd_fragment !== '' ) {
|
2014-08-20 17:32:01 +00:00
|
|
|
$vals['fragment'] = $row->rd_fragment;
|
|
|
|
|
}
|
2022-03-29 18:11:06 +00:00
|
|
|
// @phan-suppress-next-line PhanPossiblyUndeclaredVariable set when used
|
2015-01-16 19:00:07 +00:00
|
|
|
if ( $fld_redirect ) {
|
|
|
|
|
$vals['redirect'] = (bool)$row->page_is_redirect;
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
$fit = $this->addPageSubItem( $id, $vals );
|
|
|
|
|
if ( !$fit ) {
|
|
|
|
|
$this->setContinue( $row, $sortby );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
$titles = [];
|
2014-08-20 17:32:01 +00:00
|
|
|
$count = 0;
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
if ( ++$count > $params['limit'] ) {
|
|
|
|
|
// We've reached the one extra which shows that
|
|
|
|
|
// there are additional pages to be had. Stop here...
|
|
|
|
|
$this->setContinue( $row, $sortby );
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-05-24 13:19:49 +00:00
|
|
|
|
|
|
|
|
if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
|
|
|
|
|
// Miser mode namespace check
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-20 17:32:01 +00:00
|
|
|
$titles[] = Title::makeTitle( $row->page_namespace, $row->page_title );
|
|
|
|
|
}
|
|
|
|
|
$resultPageSet->populateFromTitles( $titles );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function setContinue( $row, $sortby ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$cont = [];
|
2014-08-20 17:32:01 +00:00
|
|
|
foreach ( $sortby as $field => $v ) {
|
|
|
|
|
$cont[] = $row->$field;
|
|
|
|
|
}
|
2016-03-08 08:13:12 +00:00
|
|
|
$this->setContinueEnumParameter( 'continue', implode( '|', $cont ) );
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getCacheMode( $params ) {
|
|
|
|
|
return 'public';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
|
|
|
|
$settings = self::$settings[$this->getModuleName()];
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret = [
|
|
|
|
|
'prop' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => [
|
2014-08-20 17:32:01 +00:00
|
|
|
'pageid',
|
|
|
|
|
'title',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_ISMULTI => true,
|
|
|
|
|
ParamValidator::PARAM_DEFAULT => 'pageid|title',
|
2016-02-17 09:09:32 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
|
|
|
|
|
],
|
|
|
|
|
'namespace' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_ISMULTI => true,
|
|
|
|
|
ParamValidator::PARAM_TYPE => 'namespace',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-09-18 17:38:23 +00:00
|
|
|
'show' => null, // Will be filled/removed below
|
2016-02-17 09:09:32 +00:00
|
|
|
'limit' => [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_DEFAULT => 10,
|
|
|
|
|
ParamValidator::PARAM_TYPE => 'limit',
|
|
|
|
|
IntegerDef::PARAM_MIN => 1,
|
|
|
|
|
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
|
|
|
|
|
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'continue' => [
|
2014-09-18 17:38:23 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2014-08-20 17:32:01 +00:00
|
|
|
|
2022-04-13 15:28:26 +00:00
|
|
|
if ( empty( $settings['from_namespace'] ) &&
|
|
|
|
|
$this->getConfig()->get( MainConfigNames::MiserMode ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
|
2014-09-18 17:38:23 +00:00
|
|
|
'api-help-param-limited-in-miser-mode',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-09-18 17:38:23 +00:00
|
|
|
}
|
|
|
|
|
|
2014-08-20 17:32:01 +00:00
|
|
|
if ( !empty( $settings['showredirects'] ) ) {
|
2022-06-05 23:18:50 +00:00
|
|
|
$ret['prop'][ParamValidator::PARAM_TYPE][] = 'redirect';
|
|
|
|
|
$ret['prop'][ParamValidator::PARAM_DEFAULT] .= '|redirect';
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
if ( isset( $settings['props'] ) ) {
|
2022-06-05 23:18:50 +00:00
|
|
|
$ret['prop'][ParamValidator::PARAM_TYPE] = array_merge(
|
|
|
|
|
$ret['prop'][ParamValidator::PARAM_TYPE], $settings['props']
|
2014-08-20 17:32:01 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$show = [];
|
2014-08-20 17:32:01 +00:00
|
|
|
if ( !empty( $settings['showredirects'] ) ) {
|
|
|
|
|
$show[] = 'redirect';
|
|
|
|
|
$show[] = '!redirect';
|
|
|
|
|
}
|
|
|
|
|
if ( isset( $settings['show'] ) ) {
|
2014-09-18 17:38:23 +00:00
|
|
|
$show = array_merge( $show, $settings['show'] );
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
if ( $show ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$ret['show'] = [
|
2022-06-05 23:18:50 +00:00
|
|
|
ParamValidator::PARAM_TYPE => $show,
|
|
|
|
|
ParamValidator::PARAM_ISMULTI => true,
|
2023-08-05 12:59:10 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-09-18 17:38:23 +00:00
|
|
|
} else {
|
|
|
|
|
unset( $ret['show'] );
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2014-08-20 17:32:01 +00:00
|
|
|
$settings = self::$settings[$this->getModuleName()];
|
|
|
|
|
$name = $this->getModuleName();
|
2014-09-18 17:38:23 +00:00
|
|
|
$path = $this->getModulePath();
|
2019-11-30 11:55:36 +00:00
|
|
|
$title = $settings['exampletitle'] ?? Title::newMainPage()->getPrefixedText();
|
2014-08-20 17:32:01 +00:00
|
|
|
$etitle = rawurlencode( $title );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2014-09-18 17:38:23 +00:00
|
|
|
"action=query&prop={$name}&titles={$etitle}"
|
|
|
|
|
=> "apihelp-$path-example-simple",
|
|
|
|
|
"action=query&generator={$name}&titles={$etitle}&prop=info"
|
|
|
|
|
=> "apihelp-$path-example-generator",
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getHelpUrls() {
|
2015-04-07 09:55:31 +00:00
|
|
|
$name = ucfirst( $this->getModuleName() );
|
2017-04-04 22:52:57 +00:00
|
|
|
return "https://www.mediawiki.org/wiki/Special:MyLanguage/API:{$name}";
|
2014-08-20 17:32:01 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-09-25 16:17:29 +00:00
|
|
|
|
|
|
|
|
/** @deprecated class alias since 1.43 */
|
|
|
|
|
class_alias( ApiQueryBacklinksprop::class, 'ApiQueryBacklinksprop' );
|