wiki.techinc.nl/includes/api/ApiQueryBacklinks.php

582 lines
18 KiB
PHP
Raw Normal View History

2006-10-25 17:16:37 +00:00
<?php
/**
*
2006-10-25 17:16:37 +00:00
*
* Created on Oct 16, 2006
*
* Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
2006-10-25 17:16:37 +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.
2006-10-25 17:16:37 +00:00
* http://www.gnu.org/copyleft/gpl.html
*
* @file
2006-10-25 17:16:37 +00:00
*/
/**
* This is a three-in-one module to query:
2007-05-20 23:31:44 +00:00
* * backlinks - links pointing to the given page,
* * embeddedin - what pages transclude the given page within themselves,
* * imageusage - what pages use the given image
*
* @ingroup API
*/
2006-10-25 17:16:37 +00:00
class ApiQueryBacklinks extends ApiQueryGeneratorBase {
2011-02-19 00:30:18 +00:00
/**
* @var Title
*/
private $rootTitle;
private $params, $cont, $redirect;
private $bl_ns, $bl_from, $bl_from_ns, $bl_table, $bl_code, $bl_title, $bl_fields, $hasNS;
/**
* Maps ns and title to pageid
*
* @var array
*/
private $pageMap = array();
private $resultArr;
private $redirTitles = array();
private $continueStr = null;
2006-10-25 17:16:37 +00:00
// output element name, database column field prefix, database table
private $backlinksSettings = array(
'backlinks' => array(
'code' => 'bl',
'prefix' => 'pl',
'linktbl' => 'pagelinks',
2011-11-28 15:43:11 +00:00
'helpurl' => 'https://www.mediawiki.org/wiki/API:Backlinks',
),
'embeddedin' => array(
'code' => 'ei',
'prefix' => 'tl',
'linktbl' => 'templatelinks',
2011-11-28 15:43:11 +00:00
'helpurl' => 'https://www.mediawiki.org/wiki/API:Embeddedin',
),
'imageusage' => array(
'code' => 'iu',
'prefix' => 'il',
'linktbl' => 'imagelinks',
2011-11-28 15:43:11 +00:00
'helpurl' => 'https://www.mediawiki.org/wiki/API:Imageusage',
)
);
public function __construct( ApiQuery $query, $moduleName ) {
$settings = $this->backlinksSettings[$moduleName];
$prefix = $settings['prefix'];
$code = $settings['code'];
* API: BREAKING CHANGE: (bug 11430) Return fewer results than the limit in some cases to prevent running out of memory * This means queries could possibly return fewer results than the limit and still set a query-continue * Add iicontinue, rvcontinue, cicontinue, incontinue, amfrom to faciliate query-continue for these modules * Implemented by blocking additions to the ApiResult object if they would make it too large ** Important things like query-continue values and warnings are exempt from this check ** RSS feeds and exported XML are also exempted (size-checking them would be too messy) ** Result size is checked against $wgAPIMaxResultSize, which defaults to 8 MB For those who really care, per-file details follow: ApiResult.php: * Introduced ApiResult::$mSize which keeps track of the result size. * Introduced ApiResult::size() which calculates an array's size (which is the sum of the strlen()s of its elements). * ApiResult::addValue() now checks that the result size stays below $wgAPIMaxResultSize. If the item won't fit, it won't be added and addValue() will return false. Callers should check the return value and set a query-continue if it's false. * Closed the back door that is ApiResult::getData(): callers can't manipulate the data array directly anymore so they can't bypass the result size limit. * Added ApiResult::setIndexedTagName_internal() which will call setIndexedTagName() on an array already in the result. This is needed for the 'new' order of adding results, which means addValue()ing one result at a time until you hit the limit or run out, then calling this function to set the tag name. * Added ApiResult::disableSizeCheck() and enableSizeCheck() which disable and enable size checking in addValue(). This is used for stuff like query-continue elements and warnings which shouldn't count towards the result size. * Added ApiResult::unsetValue() which removes an element from the result and decreases $mSize. ApiBase.php: * Like ApiResult::getData(), ApiBase::getResultData() no longer returns a reference. * Use ApiResult::disableSizeCheck() in ApiBase::setWarning() ApiQueryBase.php: * Added ApiQueryBase::addPageSubItem(), which adds page subitems one item at a time. * addPageSubItem() and addPageSubItems() now return whether the subitem fit in the result. * Use ApiResult::disableSizeCheck() in setContinueEnumParameter() ApiMain.php: * Use ApiResult::disableSizeCheck() in ApiMain::substituteResultWithError() * Use getParameter() rather than $mRequest to obtain requestid DefaultSettings.php: * Added $wgAPIMaxResultSize, with a default value of 8 MB ApiQuery*.php: * Added results one at a time, and set a query-continue if the result is full. ApiQueryLangLinks.php and friends: * Migrated from addPageSubItems() to addPageSubItem(). This eliminates the need for $lastId. ApiQueryAllLinks.php, ApiQueryWatchlist.php, ApiQueryAllimages.php, ApiQuerySearch.php: * Renamed $data to something more appropriate ($pageids, $ids or $titles) ApiQuerySiteinfo.php: * Abuse siprop as a query-continue parameter and set it to all props that couldn't be processed. ApiQueryRandom.php: * Doesn't do continuations, because the result is supposed to be random. * Be smart enough to not run the second query if the results of the first didn't fit. ApiQueryImageInfo.php, ApiQueryRevisions.php, ApiQueryCategoryInfo.php, ApiQueryInfo.php: * Added continue parameter which basically skips the first so many items ApiQueryBacklinks.php: * Throw the result in a big array first and addValue() that one element at a time if necessary ** This is necessary because the results aren't retrieved in order * Introduced $this->pageMap to map namespace and title to page ID * Rewritten extractRowInfo() and extractRedirRowInfo() a little * Declared all private member variables explicitly ApiQueryDeletedrevs.php: * Use a pagemap just like in Backlinks * Introduce fake page IDs and keep track of them so we know where to add what ** This doesn't change the output format, because the fake page IDs start at 0 and are consecutive ApiQueryAllmessages.php: * Add amfrom to facilitate query-continue ApiQueryUsers.php: * Rewrite: put the getOtherUsersInfo() code in execute()
2009-02-05 14:30:59 +00:00
$this->resultArr = array();
parent::__construct( $query, $moduleName, $code );
$this->bl_ns = $prefix . '_namespace';
$this->bl_from = $prefix . '_from';
$this->bl_from_ns = $prefix . '_from_namespace';
$this->bl_table = $settings['linktbl'];
$this->bl_code = $code;
$this->helpUrl = $settings['helpurl'];
$this->hasNS = $moduleName !== 'imageusage';
if ( $this->hasNS ) {
$this->bl_title = $prefix . '_title';
$this->bl_fields = array(
$this->bl_ns,
$this->bl_title
);
} else {
$this->bl_title = $prefix . '_to';
$this->bl_fields = array(
$this->bl_title
);
}
2006-10-25 17:16:37 +00:00
}
public function execute() {
$this->run();
}
public function getCacheMode( $params ) {
return 'public';
}
2006-10-25 17:16:37 +00:00
public function executeGenerator( $resultPageSet ) {
$this->run( $resultPageSet );
2006-10-25 17:16:37 +00:00
}
2011-02-19 00:30:18 +00:00
/**
* @param ApiPageSet $resultPageSet
* @return void
2011-02-19 00:30:18 +00:00
*/
private function runFirstQuery( $resultPageSet = null ) {
$this->addTables( array( $this->bl_table, 'page' ) );
$this->addWhere( "{$this->bl_from}=page_id" );
if ( is_null( $resultPageSet ) ) {
$this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) );
} else {
$this->addFields( $resultPageSet->getPageTableFields() );
}
$this->addFields( array( 'page_is_redirect', 'from_ns' => 'page_namespace' ) );
$this->addWhereFld( $this->bl_title, $this->rootTitle->getDBkey() );
if ( $this->hasNS ) {
$this->addWhereFld( $this->bl_ns, $this->rootTitle->getNamespace() );
}
$this->addWhereFld( $this->bl_from_ns, $this->params['namespace'] );
if ( count( $this->cont ) >= 2 ) {
$op = $this->params['dir'] == 'descending' ? '<' : '>';
if ( count( $this->params['namespace'] ) > 1 ) {
$this->addWhere(
"{$this->bl_from_ns} $op {$this->cont[0]} OR " .
"({$this->bl_from_ns} = {$this->cont[0]} AND " .
"{$this->bl_from} $op= {$this->cont[1]})"
);
} else {
$this->addWhere( "{$this->bl_from} $op= {$this->cont[1]}" );
}
}
if ( $this->params['filterredir'] == 'redirects' ) {
$this->addWhereFld( 'page_is_redirect', 1 );
} elseif ( $this->params['filterredir'] == 'nonredirects' && !$this->redirect ) {
// bug 22245 - Check for !redirect, as filtering nonredirects, when
// getting what links to them is contradictory
2010-01-31 21:21:46 +00:00
$this->addWhereFld( 'page_is_redirect', 0 );
}
$this->addOption( 'LIMIT', $this->params['limit'] + 1 );
$sort = ( $this->params['dir'] == 'descending' ? ' DESC' : '' );
$orderBy = array();
if ( count( $this->params['namespace'] ) > 1 ) {
$orderBy[] = $this->bl_from_ns . $sort;
}
$orderBy[] = $this->bl_from . $sort;
$this->addOption( 'ORDER BY', $orderBy );
$this->addOption( 'STRAIGHT_JOIN' );
$res = $this->select( __METHOD__ );
$count = 0;
foreach ( $res as $row ) {
if ( ++$count > $this->params['limit'] ) {
// We've reached the one extra which shows that there are
// additional pages to be had. Stop here...
// Continue string may be overridden at a later step
$this->continueStr = "{$row->from_ns}|{$row->page_id}";
break;
}
// Fill in continuation fields for later steps
if ( count( $this->cont ) < 2 ) {
$this->cont[] = $row->from_ns;
$this->cont[] = $row->page_id;
}
$this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
$t = Title::makeTitle( $row->page_namespace, $row->page_title );
if ( $row->page_is_redirect ) {
$this->redirTitles[] = $t;
}
if ( is_null( $resultPageSet ) ) {
$a = array( 'pageid' => intval( $row->page_id ) );
ApiQueryBase::addTitleInfo( $a, $t );
if ( $row->page_is_redirect ) {
$a['redirect'] = '';
}
// Put all the results in an array first
$this->resultArr[$a['pageid']] = $a;
} else {
$resultPageSet->processDbRow( $row );
}
}
}
2011-02-19 00:30:18 +00:00
/**
* @param ApiPageSet $resultPageSet
* @return void
2011-02-19 00:30:18 +00:00
*/
private function runSecondQuery( $resultPageSet = null ) {
$db = $this->getDB();
$this->addTables( array( 'page', $this->bl_table ) );
$this->addWhere( "{$this->bl_from}=page_id" );
if ( is_null( $resultPageSet ) ) {
$this->addFields( array( 'page_id', 'page_title', 'page_namespace', 'page_is_redirect' ) );
} else {
$this->addFields( $resultPageSet->getPageTableFields() );
}
$this->addFields( array( $this->bl_title, 'from_ns' => 'page_namespace' ) );
if ( $this->hasNS ) {
$this->addFields( $this->bl_ns );
}
// We can't use LinkBatch here because $this->hasNS may be false
$titleWhere = array();
$allRedirNs = array();
$allRedirDBkey = array();
/** @var $t Title */
foreach ( $this->redirTitles as $t ) {
$redirNs = $t->getNamespace();
$redirDBkey = $t->getDBkey();
$titleWhere[] = "{$this->bl_title} = " . $db->addQuotes( $redirDBkey ) .
( $this->hasNS ? " AND {$this->bl_ns} = {$redirNs}" : '' );
$allRedirNs[$redirNs] = true;
$allRedirDBkey[$redirDBkey] = true;
}
$this->addWhere( $db->makeList( $titleWhere, LIST_OR ) );
$this->addWhereFld( 'page_namespace', $this->params['namespace'] );
if ( count( $this->cont ) >= 6 ) {
$op = $this->params['dir'] == 'descending' ? '<' : '>';
$where = "{$this->bl_from} $op= {$this->cont[5]}";
// Don't bother with namespace, title, or from_namespace if it's
// otherwise constant in the where clause.
if ( count( $this->params['namespace'] ) > 1 ) {
$where = "{$this->bl_from_ns} $op {$this->cont[4]} OR " .
"({$this->bl_from_ns} = {$this->cont[4]} AND ($where))";
}
if ( count( $allRedirDBkey ) > 1 ) {
$title = $db->addQuotes( $this->cont[3] );
$where = "{$this->bl_title} $op $title OR " .
"({$this->bl_title} = $title AND ($where))";
}
if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
$where = "{$this->bl_ns} $op {$this->cont[2]} OR " .
"({$this->bl_ns} = {$this->cont[2]} AND ($where))";
}
$this->addWhere( $where );
}
if ( $this->params['filterredir'] == 'redirects' ) {
$this->addWhereFld( 'page_is_redirect', 1 );
} elseif ( $this->params['filterredir'] == 'nonredirects' ) {
$this->addWhereFld( 'page_is_redirect', 0 );
}
$this->addOption( 'LIMIT', $this->params['limit'] + 1 );
$orderBy = array();
$sort = ( $this->params['dir'] == 'descending' ? ' DESC' : '' );
// Don't order by namespace/title/from_namespace if it's constant in the WHERE clause
if ( $this->hasNS && count( $allRedirNs ) > 1 ) {
$orderBy[] = $this->bl_ns . $sort;
}
if ( count( $allRedirDBkey ) > 1 ) {
$orderBy[] = $this->bl_title . $sort;
}
if ( count( $this->params['namespace'] ) > 1 ) {
$orderBy[] = $this->bl_from_ns . $sort;
}
$orderBy[] = $this->bl_from . $sort;
$this->addOption( 'ORDER BY', $orderBy );
$this->addOption( 'USE INDEX', array( 'page' => 'PRIMARY' ) );
$res = $this->select( __METHOD__ );
$count = 0;
foreach ( $res as $row ) {
$ns = $this->hasNS ? $row->{$this->bl_ns} : NS_FILE;
if ( ++$count > $this->params['limit'] ) {
// We've reached the one extra which shows that there are
// additional pages to be had. Stop here...
// Note we must keep the parameters for the first query constant
// This may be overridden at a later step
$title = $row->{$this->bl_title};
$this->continueStr = join( '|', array_slice( $this->cont, 0, 2 ) ) .
"|$ns|$title|{$row->from_ns}|{$row->page_id}";
break;
}
// Fill in continuation fields for later steps
if ( count( $this->cont ) < 6 ) {
$this->cont[] = $ns;
$this->cont[] = $row->{$this->bl_title};
$this->cont[] = $row->from_ns;
$this->cont[] = $row->page_id;
}
if ( is_null( $resultPageSet ) ) {
$a['pageid'] = intval( $row->page_id );
ApiQueryBase::addTitleInfo( $a, Title::makeTitle( $row->page_namespace, $row->page_title ) );
if ( $row->page_is_redirect ) {
$a['redirect'] = '';
}
$parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
// Put all the results in an array first
$this->resultArr[$parentID]['redirlinks'][$row->page_id] = $a;
} else {
$resultPageSet->processDbRow( $row );
}
}
}
2006-10-25 17:16:37 +00:00
2011-02-19 00:30:18 +00:00
/**
* @param ApiPageSet $resultPageSet
* @return void
2011-02-19 00:30:18 +00:00
*/
private function run( $resultPageSet = null ) {
$this->params = $this->extractRequestParams( false );
$this->redirect = isset( $this->params['redirect'] ) && $this->params['redirect'];
$userMax = ( $this->redirect ? ApiBase::LIMIT_BIG1 / 2 : ApiBase::LIMIT_BIG1 );
$botMax = ( $this->redirect ? ApiBase::LIMIT_BIG2 / 2 : ApiBase::LIMIT_BIG2 );
$result = $this->getResult();
if ( $this->params['limit'] == 'max' ) {
$this->params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
$result->setParsedLimit( $this->getModuleName(), $this->params['limit'] );
} else {
$this->params['limit'] = intval( $this->params['limit'] );
$this->validateLimit( 'limit', $this->params['limit'], 1, $userMax, $botMax );
2006-10-25 17:16:37 +00:00
}
$this->rootTitle = $this->getTitleOrPageId( $this->params )->getTitle();
// only image titles are allowed for the root in imageinfo mode
if ( !$this->hasNS && $this->rootTitle->getNamespace() !== NS_FILE ) {
$this->dieUsage(
"The title for {$this->getModuleName()} query must be a file",
'bad_image_title'
);
}
2006-10-25 17:16:37 +00:00
// Parse and validate continuation parameter
$this->cont = array();
if ( $this->params['continue'] !== null ) {
$db = $this->getDB();
$cont = explode( '|', $this->params['continue'] );
switch ( count( $cont ) ) {
case 8:
// redirect page ID for result adding
$this->cont[7] = (int)$cont[7];
$this->dieContinueUsageIf( $cont[7] !== (string)$this->cont[7] );
2006-10-25 17:16:37 +00:00
/* Fall through */
2010-01-31 21:21:46 +00:00
case 7:
// top-level page ID for result adding
$this->cont[6] = (int)$cont[6];
$this->dieContinueUsageIf( $cont[6] !== (string)$this->cont[6] );
/* Fall through */
case 6:
// ns for 2nd query (even for imageusage)
$this->cont[2] = (int)$cont[2];
$this->dieContinueUsageIf( $cont[2] !== (string)$this->cont[2] );
// title for 2nd query
$this->cont[3] = $cont[3];
// from_ns for 2nd query
$this->cont[4] = (int)$cont[4];
$this->dieContinueUsageIf( $cont[4] !== (string)$this->cont[4] );
// from_id for 1st query
$this->cont[5] = (int)$cont[5];
$this->dieContinueUsageIf( $cont[5] !== (string)$this->cont[5] );
/* Fall through */
case 2:
// from_ns for 1st query
$this->cont[0] = (int)$cont[0];
$this->dieContinueUsageIf( $cont[0] !== (string)$this->cont[0] );
// from_id for 1st query
$this->cont[1] = (int)$cont[1];
$this->dieContinueUsageIf( $cont[1] !== (string)$this->cont[1] );
break;
default:
$this->dieContinueUsageIf( true );
}
ksort( $this->cont );
}
$this->runFirstQuery( $resultPageSet );
if ( $this->redirect && count( $this->redirTitles ) ) {
$this->resetQueryParams();
$this->runSecondQuery( $resultPageSet );
2006-10-25 17:16:37 +00:00
}
// Fill in any missing fields in case it's needed below
$this->cont += array( 0, 0, 0, '', 0, 0, 0 );
if ( is_null( $resultPageSet ) ) {
// Try to add the result data in one go and pray that it fits
$code = $this->bl_code;
$data = array_map( function ( $arr ) use ( $result, $code ) {
if ( isset( $arr['redirlinks'] ) ) {
$arr['redirlinks'] = array_values( $arr['redirlinks'] );
$result->setIndexedTagName( $arr['redirlinks'], $code );
}
return $arr;
}, array_values( $this->resultArr ) );
$fit = $result->addValue( 'query', $this->getModuleName(), $data );
if ( !$fit ) {
// It didn't fit. Add elements one by one until the
// result is full.
ksort( $this->resultArr );
if ( count( $this->cont ) >= 7 ) {
$startAt = $this->cont[6];
} else {
reset( $this->resultArr );
$startAt = key( $this->resultArr );
}
$idx = 0;
foreach ( $this->resultArr as $pageID => $arr ) {
if ( $pageID < $startAt ) {
continue;
}
// Add the basic entry without redirlinks first
$fit = $result->addValue(
array( 'query', $this->getModuleName() ),
$idx, array_diff_key( $arr, array( 'redirlinks' => '' ) ) );
if ( !$fit ) {
$this->continueStr = join( '|', array_slice( $this->cont, 0, 6 ) ) .
"|$pageID";
* API: BREAKING CHANGE: (bug 11430) Return fewer results than the limit in some cases to prevent running out of memory * This means queries could possibly return fewer results than the limit and still set a query-continue * Add iicontinue, rvcontinue, cicontinue, incontinue, amfrom to faciliate query-continue for these modules * Implemented by blocking additions to the ApiResult object if they would make it too large ** Important things like query-continue values and warnings are exempt from this check ** RSS feeds and exported XML are also exempted (size-checking them would be too messy) ** Result size is checked against $wgAPIMaxResultSize, which defaults to 8 MB For those who really care, per-file details follow: ApiResult.php: * Introduced ApiResult::$mSize which keeps track of the result size. * Introduced ApiResult::size() which calculates an array's size (which is the sum of the strlen()s of its elements). * ApiResult::addValue() now checks that the result size stays below $wgAPIMaxResultSize. If the item won't fit, it won't be added and addValue() will return false. Callers should check the return value and set a query-continue if it's false. * Closed the back door that is ApiResult::getData(): callers can't manipulate the data array directly anymore so they can't bypass the result size limit. * Added ApiResult::setIndexedTagName_internal() which will call setIndexedTagName() on an array already in the result. This is needed for the 'new' order of adding results, which means addValue()ing one result at a time until you hit the limit or run out, then calling this function to set the tag name. * Added ApiResult::disableSizeCheck() and enableSizeCheck() which disable and enable size checking in addValue(). This is used for stuff like query-continue elements and warnings which shouldn't count towards the result size. * Added ApiResult::unsetValue() which removes an element from the result and decreases $mSize. ApiBase.php: * Like ApiResult::getData(), ApiBase::getResultData() no longer returns a reference. * Use ApiResult::disableSizeCheck() in ApiBase::setWarning() ApiQueryBase.php: * Added ApiQueryBase::addPageSubItem(), which adds page subitems one item at a time. * addPageSubItem() and addPageSubItems() now return whether the subitem fit in the result. * Use ApiResult::disableSizeCheck() in setContinueEnumParameter() ApiMain.php: * Use ApiResult::disableSizeCheck() in ApiMain::substituteResultWithError() * Use getParameter() rather than $mRequest to obtain requestid DefaultSettings.php: * Added $wgAPIMaxResultSize, with a default value of 8 MB ApiQuery*.php: * Added results one at a time, and set a query-continue if the result is full. ApiQueryLangLinks.php and friends: * Migrated from addPageSubItems() to addPageSubItem(). This eliminates the need for $lastId. ApiQueryAllLinks.php, ApiQueryWatchlist.php, ApiQueryAllimages.php, ApiQuerySearch.php: * Renamed $data to something more appropriate ($pageids, $ids or $titles) ApiQuerySiteinfo.php: * Abuse siprop as a query-continue parameter and set it to all props that couldn't be processed. ApiQueryRandom.php: * Doesn't do continuations, because the result is supposed to be random. * Be smart enough to not run the second query if the results of the first didn't fit. ApiQueryImageInfo.php, ApiQueryRevisions.php, ApiQueryCategoryInfo.php, ApiQueryInfo.php: * Added continue parameter which basically skips the first so many items ApiQueryBacklinks.php: * Throw the result in a big array first and addValue() that one element at a time if necessary ** This is necessary because the results aren't retrieved in order * Introduced $this->pageMap to map namespace and title to page ID * Rewritten extractRowInfo() and extractRedirRowInfo() a little * Declared all private member variables explicitly ApiQueryDeletedrevs.php: * Use a pagemap just like in Backlinks * Introduce fake page IDs and keep track of them so we know where to add what ** This doesn't change the output format, because the fake page IDs start at 0 and are consecutive ApiQueryAllmessages.php: * Add amfrom to facilitate query-continue ApiQueryUsers.php: * Rewrite: put the getOtherUsersInfo() code in execute()
2009-02-05 14:30:59 +00:00
break;
}
$hasRedirs = false;
$redirLinks = isset( $arr['redirlinks'] ) ? (array)$arr['redirlinks'] : array();
ksort( $redirLinks );
if ( count( $this->cont ) >= 8 && $pageID == $startAt ) {
$redirStartAt = $this->cont[7];
} else {
reset( $redirLinks );
$redirStartAt = key( $redirLinks );
}
foreach ( $redirLinks as $key => $redir ) {
if ( $key < $redirStartAt ) {
continue;
}
$fit = $result->addValue(
array( 'query', $this->getModuleName(), $idx, 'redirlinks' ),
null, $redir );
if ( !$fit ) {
$this->continueStr = join( '|', array_slice( $this->cont, 0, 6 ) ) .
"|$pageID|$key";
break;
}
$hasRedirs = true;
}
if ( $hasRedirs ) {
$result->setIndexedTagName_internal(
array( 'query', $this->getModuleName(), $idx, 'redirlinks' ),
$this->bl_code );
}
if ( !$fit ) {
break;
}
$idx++;
* API: BREAKING CHANGE: (bug 11430) Return fewer results than the limit in some cases to prevent running out of memory * This means queries could possibly return fewer results than the limit and still set a query-continue * Add iicontinue, rvcontinue, cicontinue, incontinue, amfrom to faciliate query-continue for these modules * Implemented by blocking additions to the ApiResult object if they would make it too large ** Important things like query-continue values and warnings are exempt from this check ** RSS feeds and exported XML are also exempted (size-checking them would be too messy) ** Result size is checked against $wgAPIMaxResultSize, which defaults to 8 MB For those who really care, per-file details follow: ApiResult.php: * Introduced ApiResult::$mSize which keeps track of the result size. * Introduced ApiResult::size() which calculates an array's size (which is the sum of the strlen()s of its elements). * ApiResult::addValue() now checks that the result size stays below $wgAPIMaxResultSize. If the item won't fit, it won't be added and addValue() will return false. Callers should check the return value and set a query-continue if it's false. * Closed the back door that is ApiResult::getData(): callers can't manipulate the data array directly anymore so they can't bypass the result size limit. * Added ApiResult::setIndexedTagName_internal() which will call setIndexedTagName() on an array already in the result. This is needed for the 'new' order of adding results, which means addValue()ing one result at a time until you hit the limit or run out, then calling this function to set the tag name. * Added ApiResult::disableSizeCheck() and enableSizeCheck() which disable and enable size checking in addValue(). This is used for stuff like query-continue elements and warnings which shouldn't count towards the result size. * Added ApiResult::unsetValue() which removes an element from the result and decreases $mSize. ApiBase.php: * Like ApiResult::getData(), ApiBase::getResultData() no longer returns a reference. * Use ApiResult::disableSizeCheck() in ApiBase::setWarning() ApiQueryBase.php: * Added ApiQueryBase::addPageSubItem(), which adds page subitems one item at a time. * addPageSubItem() and addPageSubItems() now return whether the subitem fit in the result. * Use ApiResult::disableSizeCheck() in setContinueEnumParameter() ApiMain.php: * Use ApiResult::disableSizeCheck() in ApiMain::substituteResultWithError() * Use getParameter() rather than $mRequest to obtain requestid DefaultSettings.php: * Added $wgAPIMaxResultSize, with a default value of 8 MB ApiQuery*.php: * Added results one at a time, and set a query-continue if the result is full. ApiQueryLangLinks.php and friends: * Migrated from addPageSubItems() to addPageSubItem(). This eliminates the need for $lastId. ApiQueryAllLinks.php, ApiQueryWatchlist.php, ApiQueryAllimages.php, ApiQuerySearch.php: * Renamed $data to something more appropriate ($pageids, $ids or $titles) ApiQuerySiteinfo.php: * Abuse siprop as a query-continue parameter and set it to all props that couldn't be processed. ApiQueryRandom.php: * Doesn't do continuations, because the result is supposed to be random. * Be smart enough to not run the second query if the results of the first didn't fit. ApiQueryImageInfo.php, ApiQueryRevisions.php, ApiQueryCategoryInfo.php, ApiQueryInfo.php: * Added continue parameter which basically skips the first so many items ApiQueryBacklinks.php: * Throw the result in a big array first and addValue() that one element at a time if necessary ** This is necessary because the results aren't retrieved in order * Introduced $this->pageMap to map namespace and title to page ID * Rewritten extractRowInfo() and extractRedirRowInfo() a little * Declared all private member variables explicitly ApiQueryDeletedrevs.php: * Use a pagemap just like in Backlinks * Introduce fake page IDs and keep track of them so we know where to add what ** This doesn't change the output format, because the fake page IDs start at 0 and are consecutive ApiQueryAllmessages.php: * Add amfrom to facilitate query-continue ApiQueryUsers.php: * Rewrite: put the getOtherUsersInfo() code in execute()
2009-02-05 14:30:59 +00:00
}
}
2006-10-25 17:16:37 +00:00
$result->setIndexedTagName_internal(
array( 'query', $this->getModuleName() ),
$this->bl_code
);
2006-10-25 17:16:37 +00:00
}
if ( !is_null( $this->continueStr ) ) {
$this->setContinueEnumParameter( 'continue', $this->continueStr );
}
2006-10-25 17:16:37 +00:00
}
public function getAllowedParams() {
$retval = array(
'title' => array(
ApiBase::PARAM_TYPE => 'string',
),
'pageid' => array(
ApiBase::PARAM_TYPE => 'integer',
),
'continue' => array(
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
),
'namespace' => array(
ApiBase::PARAM_ISMULTI => true,
ApiBase::PARAM_TYPE => 'namespace'
2006-10-25 17:16:37 +00:00
),
'dir' => array(
ApiBase::PARAM_DFLT => 'ascending',
ApiBase::PARAM_TYPE => array(
'ascending',
'descending'
)
),
'filterredir' => array(
ApiBase::PARAM_DFLT => 'all',
ApiBase::PARAM_TYPE => array(
'all',
'redirects',
'nonredirects'
)
),
'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
2006-10-25 17:16:37 +00:00
)
);
if ( $this->getModuleName() == 'embeddedin' ) {
return $retval;
}
$retval['redirect'] = false;
return $retval;
2006-10-25 17:16:37 +00:00
}
protected function getExamplesMessages() {
static $examples = array(
'backlinks' => array(
'action=query&list=backlinks&bltitle=Main%20Page'
=> 'apihelp-query+backlinks-example-simple',
'action=query&generator=backlinks&gbltitle=Main%20Page&prop=info'
=> 'apihelp-query+backlinks-example-generator',
),
'embeddedin' => array(
'action=query&list=embeddedin&eititle=Template:Stub'
=> 'apihelp-query+embeddedin-example-simple',
'action=query&generator=embeddedin&geititle=Template:Stub&prop=info'
=> 'apihelp-query+embeddedin-example-generator',
),
'imageusage' => array(
'action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg'
=> 'apihelp-query+imageusage-example-simple',
'action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info'
=> 'apihelp-query+imageusage-example-generator',
)
2006-10-25 17:16:37 +00:00
);
return $examples[$this->getModuleName()];
2006-10-25 17:16:37 +00:00
}
public function getHelpUrls() {
return $this->helpUrl;
}
2006-10-25 17:16:37 +00:00
}