From 08aa4418c30cfc18ccc69a0f0f9cb9e17be6c196 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Mon, 12 Aug 2013 09:28:15 +0200 Subject: Update to MediaWiki 1.21.1 --- includes/api/ApiQueryAllLinks.php | 138 ++++++++++++++++++++++++-------------- 1 file changed, 87 insertions(+), 51 deletions(-) (limited to 'includes/api/ApiQueryAllLinks.php') diff --git a/includes/api/ApiQueryAllLinks.php b/includes/api/ApiQueryAllLinks.php index da4840f0..e355f8b0 100644 --- a/includes/api/ApiQueryAllLinks.php +++ b/includes/api/ApiQueryAllLinks.php @@ -32,7 +32,34 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase { public function __construct( $query, $moduleName ) { - parent::__construct( $query, $moduleName, 'al' ); + switch ( $moduleName ) { + case 'alllinks': + $prefix = 'al'; + $this->table = 'pagelinks'; + $this->tablePrefix = 'pl_'; + $this->dfltNamespace = NS_MAIN; + $this->indexTag = 'l'; + $this->description = 'Enumerate all links that point to a given namespace'; + $this->descriptionLink = 'link'; + $this->descriptionLinked = 'linked'; + $this->descriptionLinking = 'linking'; + break; + case 'alltransclusions': + $prefix = 'at'; + $this->table = 'templatelinks'; + $this->tablePrefix = 'tl_'; + $this->dfltNamespace = NS_TEMPLATE; + $this->indexTag = 't'; + $this->description = 'List all transclusions (pages embedded using {{x}}), including non-existing'; + $this->descriptionLink = 'transclusion'; + $this->descriptionLinked = 'transcluded'; + $this->descriptionLinking = 'transcluding'; + break; + default: + ApiBase::dieDebug( __METHOD__, 'Unknown module name' ); + } + + parent::__construct( $query, $moduleName, $prefix ); } public function execute() { @@ -55,75 +82,71 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase { $db = $this->getDB(); $params = $this->extractRequestParams(); + $pfx = $this->tablePrefix; $prop = array_flip( $params['prop'] ); $fld_ids = isset( $prop['ids'] ); $fld_title = isset( $prop['title'] ); if ( $params['unique'] ) { - if ( !is_null( $resultPageSet ) ) { - $this->dieUsage( $this->getModuleName() . ' cannot be used as a generator in unique links mode', 'params' ); - } if ( $fld_ids ) { - $this->dieUsage( $this->getModuleName() . ' cannot return corresponding page ids in unique links mode', 'params' ); + $this->dieUsage( + "{$this->getModuleName()} cannot return corresponding page ids in unique {$this->descriptionLink}s mode", + 'params' ); } $this->addOption( 'DISTINCT' ); } - $this->addTables( 'pagelinks' ); - $this->addWhereFld( 'pl_namespace', $params['namespace'] ); + $this->addTables( $this->table ); + $this->addWhereFld( $pfx . 'namespace', $params['namespace'] ); - if ( !is_null( $params['from'] ) && !is_null( $params['continue'] ) ) { - $this->dieUsage( 'alcontinue and alfrom cannot be used together', 'params' ); - } - if ( !is_null( $params['continue'] ) ) { + $continue = !is_null( $params['continue'] ); + if ( $continue ) { $continueArr = explode( '|', $params['continue'] ); $op = $params['dir'] == 'descending' ? '<' : '>'; if ( $params['unique'] ) { - if ( count( $continueArr ) != 1 ) { - $this->dieUsage( 'Invalid continue parameter', 'badcontinue' ); - } + $this->dieContinueUsageIf( count( $continueArr ) != 1 ); $continueTitle = $db->addQuotes( $continueArr[0] ); - $this->addWhere( "pl_title $op= $continueTitle" ); + $this->addWhere( "{$pfx}title $op= $continueTitle" ); } else { - if ( count( $continueArr ) != 2 ) { - $this->dieUsage( 'Invalid continue parameter', 'badcontinue' ); - } + $this->dieContinueUsageIf( count( $continueArr ) != 2 ); $continueTitle = $db->addQuotes( $continueArr[0] ); $continueFrom = intval( $continueArr[1] ); $this->addWhere( - "pl_title $op $continueTitle OR " . - "(pl_title = $continueTitle AND " . - "pl_from $op= $continueFrom)" + "{$pfx}title $op $continueTitle OR " . + "({$pfx}title = $continueTitle AND " . + "{$pfx}from $op= $continueFrom)" ); } } - $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) ); + // 'continue' always overrides 'from' + $from = ( $continue || is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) ); $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) ); - $this->addWhereRange( 'pl_title', 'newer', $from, $to ); + $this->addWhereRange( $pfx . 'title', 'newer', $from, $to ); if ( isset( $params['prefix'] ) ) { - $this->addWhere( 'pl_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) ); + $this->addWhere( $pfx . 'title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) ); } - $this->addFields( 'pl_title' ); - $this->addFieldsIf( 'pl_from', !$params['unique'] ); + $this->addFields( array( 'pl_title' => $pfx . 'title' ) ); + $this->addFieldsIf( array( 'pl_from' => $pfx . 'from' ), !$params['unique'] ); - $this->addOption( 'USE INDEX', 'pl_namespace' ); + $this->addOption( 'USE INDEX', $pfx . 'namespace' ); $limit = $params['limit']; $this->addOption( 'LIMIT', $limit + 1 ); $sort = ( $params['dir'] == 'descending' ? ' DESC' : '' ); $orderBy = array(); - $orderBy[] = 'pl_title' . $sort; + $orderBy[] = $pfx . 'title' . $sort; if ( !$params['unique'] ) { - $orderBy[] = 'pl_from' . $sort; + $orderBy[] = $pfx . 'from' . $sort; } $this->addOption( 'ORDER BY', $orderBy ); $res = $this->select( __METHOD__ ); $pageids = array(); + $titles = array(); $count = 0; $result = $this->getResult(); foreach ( $res as $row ) { @@ -132,7 +155,7 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase { if ( $params['unique'] ) { $this->setContinueEnumParameter( 'continue', $row->pl_title ); } else { - $this->setContinueEnumParameter( 'continue', $row->pl_title . "|" . $row->pl_from ); + $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from ); } break; } @@ -151,17 +174,21 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase { if ( $params['unique'] ) { $this->setContinueEnumParameter( 'continue', $row->pl_title ); } else { - $this->setContinueEnumParameter( 'continue', $row->pl_title . "|" . $row->pl_from ); + $this->setContinueEnumParameter( 'continue', $row->pl_title . '|' . $row->pl_from ); } break; } + } elseif ( $params['unique'] ) { + $titles[] = Title::makeTitle( $params['namespace'], $row->pl_title ); } else { $pageids[] = $row->pl_from; } } if ( is_null( $resultPageSet ) ) { - $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'l' ); + $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->indexTag ); + } elseif ( $params['unique'] ) { + $resultPageSet->populateFromTitles( $titles ); } else { $resultPageSet->populateFromPageIDs( $pageids ); } @@ -183,7 +210,7 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase { ) ), 'namespace' => array( - ApiBase::PARAM_DFLT => 0, + ApiBase::PARAM_DFLT => $this->dfltNamespace, ApiBase::PARAM_TYPE => 'namespace' ), 'limit' => array( @@ -205,18 +232,23 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase { public function getParamDescription() { $p = $this->getModulePrefix(); + $link = $this->descriptionLink; + $linking = $this->descriptionLinking; return array( - 'from' => 'The page title to start enumerating from', - 'to' => 'The page title to stop enumerating at', - 'prefix' => 'Search for all page titles that begin with this value', - 'unique' => "Only show unique links. Cannot be used with generator or {$p}prop=ids", + 'from' => "The title of the $link to start enumerating from", + 'to' => "The title of the $link to stop enumerating at", + 'prefix' => "Search for all $link titles that begin with this value", + 'unique' => array( + "Only show distinct $link titles. Cannot be used with {$p}prop=ids.", + 'When used as a generator, yields target pages instead of source pages.', + ), 'prop' => array( 'What pieces of information to include', - " ids - Adds pageid of where the link is from (Cannot be used with {$p}unique)", - ' title - Adds the title of the link', + " ids - Adds the pageid of the $linking page (Cannot be used with {$p}unique)", + " title - Adds the title of the $link", ), 'namespace' => 'The namespace to enumerate', - 'limit' => 'How many total links to return', + 'limit' => "How many total items to return", 'continue' => 'When more results are available, use this to continue', 'dir' => 'The direction in which to list', ); @@ -235,30 +267,34 @@ class ApiQueryAllLinks extends ApiQueryGeneratorBase { } public function getDescription() { - return 'Enumerate all links that point to a given namespace'; + return $this->description; } public function getPossibleErrors() { $m = $this->getModuleName(); + $link = $this->descriptionLink; return array_merge( parent::getPossibleErrors(), array( - array( 'code' => 'params', 'info' => "{$m} cannot be used as a generator in unique links mode" ), - array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique links mode" ), - array( 'code' => 'params', 'info' => 'alcontinue and alfrom cannot be used together' ), - array( 'code' => 'badcontinue', 'info' => 'Invalid continue parameter' ), + array( 'code' => 'params', 'info' => "{$m} cannot return corresponding page ids in unique {$link}s mode" ), ) ); } public function getExamples() { + $p = $this->getModulePrefix(); + $link = $this->descriptionLink; + $linked = $this->descriptionLinked; return array( - 'api.php?action=query&list=alllinks&alunique=&alfrom=B', + "api.php?action=query&list=all{$link}s&{$p}from=B&{$p}prop=ids|title" + => "List $linked titles with page ids they are from, including missing ones. Start at B", + "api.php?action=query&list=all{$link}s&{$p}unique=&{$p}from=B" + => "List unique $linked titles", + "api.php?action=query&generator=all{$link}s&g{$p}unique=&g{$p}from=B" + => "Gets all $link targets, marking the missing ones", + "api.php?action=query&generator=all{$link}s&g{$p}from=B" + => "Gets pages containing the {$link}s", ); } public function getHelpUrls() { - return 'https://www.mediawiki.org/wiki/API:Alllinks'; - } - - public function getVersion() { - return __CLASS__ . ': $Id$'; + return "https://www.mediawiki.org/wiki/API:All{$this->descriptionLink}s"; } } -- cgit v1.2.2