From 8e938814c46152a6fd6acc60f1370b52d7ffcb93 Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Wed, 20 Aug 2014 13:32:01 -0400 Subject: [PATCH] API: Add prop modules for backlinks With the success of prop=redirects, let's create prop=linkshere, prop=transcludedin, and prop=fileusage. Let's also add a miser-mode-sensitive 'namespace' parameter to prop=redirects, since the other three have a 'namespace' parameter using the new *_from_namespace fields. Change-Id: Ib650db9988a553771582eed0c183d0cae4b71252 --- RELEASE-NOTES-1.24 | 6 + includes/AutoLoader.php | 2 +- includes/api/ApiQuery.php | 5 +- includes/api/ApiQueryBacklinksprop.php | 472 +++++++++++++++++++++++++ includes/api/ApiQueryRedirects.php | 270 -------------- 5 files changed, 483 insertions(+), 272 deletions(-) create mode 100644 includes/api/ApiQueryBacklinksprop.php delete mode 100644 includes/api/ApiQueryRedirects.php diff --git a/RELEASE-NOTES-1.24 b/RELEASE-NOTES-1.24 index 3eb24a32b9..ce083e084c 100644 --- a/RELEASE-NOTES-1.24 +++ b/RELEASE-NOTES-1.24 @@ -269,6 +269,10 @@ production. * (bug 63326) Add blockedtimestamp field to output of blockinfo property for the list=allusers and list=users modules. * prop=imageinfo no longer requires iiurlwidth to be set when using iiurlparam. +* Added prop=linkshere, prop=fileusage, and prop=transcludedin, which are + roughly equivalent to list=backlinks, list=imageusage, and list=embeddedin + but can work on a list of titles (including titles from a generator). +* prop=redirects can now filter returned redirects by namespace. === Action API internal changes in 1.24 === * Methods for handling continuation are added to ApiResult, so actions other @@ -298,6 +302,8 @@ production. ApiTokensGetTokenTypes are deprecated, but are still called to support backwards-compatible token access. * ApiBase::validateLimit and ApiBase::validateTimestamp are now protected. +* ApiQueryRedirects was removed; prop=redirects is now implemented by + ApiQueryBacklinksProp along with the newly-added prop modules. * The following methods have been deprecated and may be removed in a future release: * ApiBase::getResultProperties diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 44c405d2f1..3a68ff10e2 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -267,6 +267,7 @@ $wgAutoloadLocalClasses = array( 'ApiQueryAllPages' => 'includes/api/ApiQueryAllPages.php', 'ApiQueryAllUsers' => 'includes/api/ApiQueryAllUsers.php', 'ApiQueryBacklinks' => 'includes/api/ApiQueryBacklinks.php', + 'ApiQueryBacklinksprop' => 'includes/api/ApiQueryBacklinksprop.php', 'ApiQueryBase' => 'includes/api/ApiQueryBase.php', 'ApiQueryBlocks' => 'includes/api/ApiQueryBlocks.php', 'ApiQueryCategories' => 'includes/api/ApiQueryCategories.php', @@ -300,7 +301,6 @@ $wgAutoloadLocalClasses = array( 'ApiQueryRandom' => 'includes/api/ApiQueryRandom.php', 'ApiQueryRecentChanges' => 'includes/api/ApiQueryRecentChanges.php', 'ApiQueryFileRepoInfo' => 'includes/api/ApiQueryFileRepoInfo.php', - 'ApiQueryRedirects' => 'includes/api/ApiQueryRedirects.php', 'ApiQueryRevisions' => 'includes/api/ApiQueryRevisions.php', 'ApiQuerySearch' => 'includes/api/ApiQuerySearch.php', 'ApiQuerySiteinfo' => 'includes/api/ApiQuerySiteinfo.php', diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index 8407fad794..7c750e41bd 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -47,17 +47,20 @@ class ApiQuery extends ApiBase { 'contributors' => 'ApiQueryContributors', 'duplicatefiles' => 'ApiQueryDuplicateFiles', 'extlinks' => 'ApiQueryExternalLinks', + 'fileusage' => 'ApiQueryBacklinksprop', 'images' => 'ApiQueryImages', 'imageinfo' => 'ApiQueryImageInfo', 'info' => 'ApiQueryInfo', 'links' => 'ApiQueryLinks', + 'linkshere' => 'ApiQueryBacklinksprop', 'iwlinks' => 'ApiQueryIWLinks', 'langlinks' => 'ApiQueryLangLinks', 'pageprops' => 'ApiQueryPageProps', - 'redirects' => 'ApiQueryRedirects', + 'redirects' => 'ApiQueryBacklinksprop', 'revisions' => 'ApiQueryRevisions', 'stashimageinfo' => 'ApiQueryStashImageInfo', 'templates' => 'ApiQueryLinks', + 'transcludedin' => 'ApiQueryBacklinksprop', ); /** diff --git a/includes/api/ApiQueryBacklinksprop.php b/includes/api/ApiQueryBacklinksprop.php new file mode 100644 index 0000000000..cd682612bc --- /dev/null +++ b/includes/api/ApiQueryBacklinksprop.php @@ -0,0 +1,472 @@ + + * + * 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 + */ + +/** + * This implements prop=redirects, prop=linkshere, prop=catmembers, + * prop=transcludedin, and prop=fileusage + * + * @ingroup API + * @since 1.24 + */ +class ApiQueryBacklinksprop extends ApiQueryGeneratorBase { + + // Data for the various modules implemented by this class + private static $settings = array( + 'redirects' => array( + 'code' => 'rd', + 'prefix' => 'rd', + 'linktable' => 'redirect', + 'what' => 'redirects to', + 'description' => 'Returns all redirects to the given pages.', + 'props' => array( + 'fragment' => 'Fragment of each redirect, if any', + ), + 'showredirects' => false, + 'show' => array( + 'fragment' => 'Only show redirects with a fragment', + '!fragment' => 'Only show redirects without a fragment', + ), + ), + 'linkshere' => array( + 'code' => 'lh', + 'prefix' => 'pl', + 'linktable' => 'pagelinks', + 'from_namespace' => true, + 'what' => 'pages linking to', + 'description' => 'Find all pages that link to the given pages.', + 'showredirects' => true, + ), + 'transcludedin' => array( + 'code' => 'ti', + 'prefix' => 'tl', + 'linktable' => 'templatelinks', + 'from_namespace' => true, + 'what' => 'pages transcluding', + 'description' => 'Find all pages that transclude the given pages.', + 'showredirects' => true, + ), + 'fileusage' => array( + 'code' => 'fu', + 'prefix' => 'il', + 'linktable' => 'imagelinks', + 'from_namespace' => true, + 'to_namespace' => NS_FILE, + 'what' => 'pages using', + 'exampletitle' => 'File:Example.jpg', + 'description' => 'Find all pages that use the given files.', + 'showredirects' => true, + ), + ); + + public function __construct( ApiQuery $query, $moduleName ) { + parent::__construct( $query, $moduleName, self::$settings[$moduleName]['code'] ); + } + + public function execute() { + $this->run(); + } + + public function executeGenerator( $resultPageSet ) { + $this->run( $resultPageSet ); + } + + /** + * @param ApiPageSet $resultPageSet + */ + private function run( ApiPageSet $resultPageSet = null ) { + $settings = self::$settings[$this->getModuleName()]; + + $db = $this->getDB(); + $params = $this->extractRequestParams(); + $prop = array_flip( $params['prop'] ); + $emptyString = $db->addQuotes( '' ); + + $pageSet = $this->getPageSet(); + $titles = $pageSet->getGoodTitles() + $pageSet->getMissingTitles(); + $map = $pageSet->getAllTitlesByNamespace(); + + // Determine our fields to query on + $p = $settings['prefix']; + $hasNS = !isset( $settings['to_namespace'] ); + if ( $hasNS ) { + $bl_namespace = "{$p}_namespace"; + $bl_title = "{$p}_title"; + } else { + $bl_namespace = $settings['to_namespace']; + $bl_title = "{$p}_to"; + + $titles = array_filter( $titles, function ( $t ) use ( $bl_namespace ) { + return $t->getNamespace() === $bl_namespace; + } ); + $map = array_intersect_key( $map, array( $bl_namespace => true ) ); + } + $bl_from = "{$p}_from"; + + if ( !$titles ) { + return; // nothing to do + } + + // 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(); + if ( $hasNS && count( $map ) > 1 ) { + $sortby[$bl_namespace] = 'ns'; + } + $theTitle = null; + foreach ( $map as $nsTitles ) { + reset( $nsTitles ); + $key = key( $nsTitles ); + if ( $theTitle === null ) { + $theTitle = $key; + } + if ( count( $nsTitles ) > 1 || $key !== $theTitle ) { + $sortby[$bl_title] = 'title'; + break; + } + } + $miser_ns = null; + if ( $params['namespace'] !== null ) { + if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) { + $miser_ns = $params['namespace']; + } else { + $this->addWhereFld( "{$p}_from_namespace", $params['namespace'] ); + if ( !empty( $settings['from_namespace'] ) && count( $params['namespace'] ) > 1 ) { + $sortby["{$p}_from_namespace"] = 'int'; + } + } + } + $sortby[$bl_from] = 'int'; + + // Now use the $sortby to figure out the continuation + if ( !is_null( $params['continue'] ) ) { + $cont = explode( '|', $params['continue'] ); + $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; + } + + if ( $where === '' ) { + $where = "$field >= $v"; + } else { + $where = "$field > $v OR ($field = $v AND ($where))"; + } + + $i--; + } + $this->addWhere( $where ); + } + + // Populate the rest of the query + $this->addTables( array( $settings['linktable'], 'page' ) ); + $this->addWhere( "$bl_from = page_id" ); + + if ( $this->getModuleName() === 'redirects' ) { + $this->addWhere( "rd_interwiki = $emptyString OR rd_interwiki IS NULL" ); + } + + $this->addFields( array_keys( $sortby ) ); + $this->addFields( array( '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_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 ); + + if ( $hasNS ) { + $lb = new LinkBatch( $titles ); + $this->addWhere( $lb->constructSet( $p, $db ) ); + } else { + $where = array(); + foreach ( $titles as $t ) { + if ( $t->getNamespace() == $bl_namespace ) { + $where[] = "$bl_title = " . $db->addQuotes( $t->getDBkey() ); + } + } + $this->addWhere( $db->makeList( $where, LIST_OR ) ); + } + + if ( $params['show'] !== null ) { + // prop=redirects only + $show = array_flip( $params['show'] ); + if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) || + isset( $show['redirect'] ) && isset( $show['!redirect'] ) + ) { + $this->dieUsageMsg( 'show' ); + } + $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) ); + $this->addWhereIf( + "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'] ) ); + } + + // Override any ORDER BY from above with what we calculated earlier. + $this->addOption( 'ORDER BY', array_keys( $sortby ) ); + + $this->addOption( 'LIMIT', $params['limit'] + 1 ); + + $res = $this->select( __METHOD__ ); + + if ( is_null( $resultPageSet ) ) { + $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]; + + $vals = array(); + if ( $fld_pageid ) { + $vals['pageid'] = $row->page_id; + } + if ( $fld_title ) { + ApiQueryBase::addTitleInfo( $vals, + Title::makeTitle( $row->page_namespace, $row->page_title ) + ); + } + if ( $fld_fragment && $row->rd_fragment !== null && $row->rd_fragment !== '' ) { + $vals['fragment'] = $row->rd_fragment; + } + if ( $fld_redirect && $row->page_is_redirect ) { + $vals['redirect'] = ''; + } + $fit = $this->addPageSubItem( $id, $vals ); + if ( !$fit ) { + $this->setContinue( $row, $sortby ); + break; + } + } + } else { + $titles = array(); + $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; + } + $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title ); + } + $resultPageSet->populateFromTitles( $titles ); + } + } + + private function setContinue( $row, $sortby ) { + $cont = array(); + foreach ( $sortby as $field => $v ) { + $cont[] = $row->$field; + } + $this->setContinueEnumParameter( 'continue', join( '|', $cont ) ); + } + + public function getCacheMode( $params ) { + return 'public'; + } + + public function getAllowedParams() { + $settings = self::$settings[$this->getModuleName()]; + + $ret = array( + 'prop' => array( + ApiBase::PARAM_TYPE => array( + 'pageid', + 'title', + ), + ApiBase::PARAM_ISMULTI => true, + ApiBase::PARAM_DFLT => 'pageid|title', + ), + 'namespace' => array( + ApiBase::PARAM_ISMULTI => true, + ApiBase::PARAM_TYPE => 'namespace', + ), + 'limit' => array( + 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' => null, + ); + + if ( !empty( $settings['showredirects'] ) ) { + $ret['prop'][ApiBase::PARAM_TYPE][] = 'redirect'; + $ret['prop'][ApiBase::PARAM_DFLT] .= '|redirect'; + } + if ( isset( $settings['props'] ) ) { + $ret['prop'][ApiBase::PARAM_TYPE] = array_merge( + $ret['prop'][ApiBase::PARAM_TYPE], array_keys( $settings['props'] ) + ); + } + + $show = array(); + if ( !empty( $settings['showredirects'] ) ) { + $show[] = 'redirect'; + $show[] = '!redirect'; + } + if ( isset( $settings['show'] ) ) { + $show = array_merge( $show, array_keys( $settings['show'] ) ); + } + if ( $show ) { + $ret['show'] = array( + ApiBase::PARAM_TYPE => $show, + ApiBase::PARAM_ISMULTI => true, + ); + } + + return $ret; + } + + public function getParamDescription() { + $settings = self::$settings[$this->getModuleName()]; + $p = $this->getModulePrefix(); + + $ret = array( + 'prop' => array( + 'Which properties to get:', + ), + 'show' => array( + 'Show only items that meet this criteria.', + ), + 'namespace' => 'Only include pages in these namespaces', + 'limit' => 'How many to return', + 'continue' => 'When more results are available, use this to continue', + ); + + if ( empty( $settings['from_namespace'] ) && $this->getConfig()->get( 'MiserMode' ) ) { + $ret['namespace'] = array( + $ret['namespace'], + "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results", + 'returned before continuing; in extreme cases, zero results may be returned.', + ); + if ( isset( $ret['type'] ) ) { + $ret['namespace'][] = "Note that you can use {$p}type=subcat or {$p}type=file " . + "instead of {$p}namespace=14 or 6."; + } + } + + $props = array( + 'pageid' => 'Adds the ID of page', + 'title' => 'Adds the title and namespace ID of the page', + ); + if ( !empty( $settings['showredirects'] ) ) { + $props['redirect'] = 'Indicate if the page is a redirect'; + } + if ( isset( $settings['props'] ) ) { + $props += $settings['props']; + } + foreach ( $props as $k => $v ) { + $ret['props'][] = sprintf( "%-9s - %s", $k, $v ); + } + + $show = array(); + if ( !empty( $settings['showredirects'] ) ) { + $show += array( + 'redirect' => 'Only show redirects', + '!redirect' => 'Only show non-redirects', + ); + } + if ( isset( $settings['show'] ) ) { + $show += $settings['show']; + } + foreach ( $show as $k => $v ) { + $ret['show'][] = sprintf( "%-9s - %s", $k, $v ); + } + + return $ret; + } + + public function getDescription() { + return self::$settings[$this->getModuleName()]['description']; + } + + public function getExamples() { + $settings = self::$settings[$this->getModuleName()]; + $name = $this->getModuleName(); + $what = $settings['what']; + $title = isset( $settings['exampletitle'] ) ? $settings['exampletitle'] : 'Main Page'; + $etitle = rawurlencode( $title ); + + return array( + "api.php?action=query&prop={$name}&titles={$etitle}" + => "Get a list of $what [[$title]]", + "api.php?action=query&generator={$name}&titles={$etitle}&prop=info" + => "Get information about $what [[$title]]", + ); + } + + public function getHelpUrls() { + $name = $this->getModuleName(); + $prefix = $this->getModulePrefix(); + return "https://www.mediawiki.org/wiki/API:Properties#{$name}_.2F_{$prefix}"; + } +} diff --git a/includes/api/ApiQueryRedirects.php b/includes/api/ApiQueryRedirects.php deleted file mode 100644 index afb2c5618f..0000000000 --- a/includes/api/ApiQueryRedirects.php +++ /dev/null @@ -1,270 +0,0 @@ - - * - * 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.23 - */ - -/** - * This query lists redirects to the given pages. - * - * @ingroup API - */ -class ApiQueryRedirects extends ApiQueryGeneratorBase { - - public function __construct( ApiQuery $query, $moduleName ) { - parent::__construct( $query, $moduleName, 'rd' ); - } - - public function execute() { - $this->run(); - } - - public function executeGenerator( $resultPageSet ) { - $this->run( $resultPageSet ); - } - - /** - * @param ApiPageSet $resultPageSet - */ - private function run( ApiPageSet $resultPageSet = null ) { - $db = $this->getDB(); - $params = $this->extractRequestParams(); - $emptyString = $db->addQuotes( '' ); - - $pageSet = $this->getPageSet(); - $titles = $pageSet->getGoodTitles() + $pageSet->getMissingTitles(); - - if ( !is_null( $params['continue'] ) ) { - $cont = explode( '|', $params['continue'] ); - $this->dieContinueUsageIf( count( $cont ) != 3 ); - $rd_namespace = (int)$cont[0]; - $this->dieContinueUsageIf( $rd_namespace != $cont[0] ); - $rd_title = $db->addQuotes( $cont[1] ); - $rd_from = (int)$cont[2]; - $this->dieContinueUsageIf( $rd_from != $cont[2] ); - $this->addWhere( - "rd_namespace > $rd_namespace OR " . - "(rd_namespace = $rd_namespace AND " . - "(rd_title > $rd_title OR " . - "(rd_title = $rd_title AND " . - "rd_from >= $rd_from)))" - ); - - // Remove titles that we're past already - $titles = array_filter( $titles, function ( $t ) use ( $rd_namespace, $rd_title ) { - $ns = $t->getNamespace(); - return ( $ns > $rd_namespace || - $ns == $rd_namespace && $t->getDBKey() >= $rd_title - ); - } ); - } - - if ( !$titles ) { - return; // nothing to do - } - - $this->addTables( array( 'redirect', 'page' ) ); - $this->addFields( array( - 'rd_from', - 'rd_namespace', - 'rd_title', - ) ); - - if ( is_null( $resultPageSet ) ) { - $prop = array_flip( $params['prop'] ); - $fld_pageid = isset( $prop['pageid'] ); - $fld_title = isset( $prop['title'] ); - $fld_fragment = isset( $prop['fragment'] ); - - $this->addFieldsIf( 'rd_fragment', $fld_fragment ); - $this->addFieldsIf( array( 'page_namespace', 'page_title' ), $fld_title ); - } else { - $this->addFields( array( 'page_namespace', 'page_title' ) ); - } - - $lb = new LinkBatch( $titles ); - $this->addWhere( array( - 'rd_from = page_id', - "rd_interwiki = $emptyString OR rd_interwiki IS NULL", - $lb->constructSet( 'rd', $db ), - ) ); - - if ( $params['show'] !== null ) { - $show = array_flip( $params['show'] ); - if ( isset( $show['fragment'] ) && isset( $show['!fragment'] ) ) { - $this->dieUsageMsg( 'show' ); - } - $this->addWhereIf( "rd_fragment != $emptyString", isset( $show['fragment'] ) ); - $this->addWhereIf( - "rd_fragment = $emptyString OR rd_fragment IS NULL", - isset( $show['!fragment'] ) - ); - } - - $map = $pageSet->getAllTitlesByNamespace(); - - // Why, MySQL? Why do you do this to us? - $sortby = array(); - if ( count( $map ) > 1 ) { - $sortby[] = 'rd_namespace'; - } - $theTitle = null; - foreach ( $map as $nsTitles ) { - reset( $nsTitles ); - $key = key( $nsTitles ); - if ( $theTitle === null ) { - $theTitle = $key; - } - if ( count( $nsTitles ) > 1 || $key !== $theTitle ) { - $sortby[] = 'rd_title'; - break; - } - } - $sortby[] = 'rd_from'; - $this->addOption( 'ORDER BY', $sortby ); - - $this->addOption( 'LIMIT', $params['limit'] + 1 ); - - $res = $this->select( __METHOD__ ); - - if ( is_null( $resultPageSet ) ) { - $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->setContinueEnumParameter( 'continue', - "$row->rd_namespace|$row->rd_title|$row->rd_from" - ); - break; - } - - # Get the ID of the current page - $id = $map[$row->rd_namespace][$row->rd_title]; - - $vals = array(); - if ( $fld_pageid ) { - $vals['pageid'] = $row->rd_from; - } - if ( $fld_title ) { - ApiQueryBase::addTitleInfo( $vals, - Title::makeTitle( $row->page_namespace, $row->page_title ) - ); - } - if ( $fld_fragment && $row->rd_fragment !== null && $row->rd_fragment !== '' ) { - $vals['fragment'] = $row->rd_fragment; - } - $fit = $this->addPageSubItem( $id, $vals ); - if ( !$fit ) { - $this->setContinueEnumParameter( 'continue', - "$row->rd_namespace|$row->rd_title|$row->rd_from" - ); - break; - } - } - } else { - $titles = array(); - $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->setContinueEnumParameter( 'continue', - "$row->rd_namespace|$row->rd_title|$row->rd_from" - ); - break; - } - $titles[] = Title::makeTitle( $row->page_namespace, $row->page_title ); - } - $resultPageSet->populateFromTitles( $titles ); - } - } - - public function getCacheMode( $params ) { - return 'public'; - } - - public function getAllowedParams() { - return array( - 'prop' => array( - ApiBase::PARAM_TYPE => array( - 'pageid', - 'title', - 'fragment', - ), - ApiBase::PARAM_ISMULTI => true, - ApiBase::PARAM_DFLT => 'pageid|title', - ), - 'show' => array( - ApiBase::PARAM_TYPE => array( - 'fragment', '!fragment', - ), - ApiBase::PARAM_ISMULTI => true, - ), - 'limit' => array( - 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' => null, - ); - } - - public function getParamDescription() { - return array( - 'prop' => array( - 'Which properties to get:', - ' pageid - Page id of each redirect', - ' title - Title of each redirect', - ' fragment - Fragment of each redirect, if any', - ), - 'show' => array( - 'Show only items that meet this criteria.', - ' fragment - Only show redirects with a fragment', - ' !fragment - Only show redirects without a fragment', - ), - 'limit' => 'How many redirects to return', - 'continue' => 'When more results are available, use this to continue', - ); - } - - public function getDescription() { - return 'Returns all redirects to the given page(s).'; - } - - public function getExamples() { - return array( - 'api.php?action=query&prop=redirects&titles=Main%20Page' - => 'Get a list of redirects to the [[Main Page]]', - 'api.php?action=query&generator=redirects&titles=Main%20Page&prop=info' - => 'Get information about all redirects to the [[Main Page]]', - ); - } - - public function getHelpUrls() { - return 'https://www.mediawiki.org/wiki/API:Properties#redirects_.2F_rd'; - } -} -- 2.20.1