}
/**
- * This is three-in-one module to query:
+ * This is a three-in-one module to query:
* * backlinks - links pointing to the given page,
* * embeddedin - what pages transclude the given page within themselves,
* * imageusage - what pages use the given image
*
- * @addtogroup API
+ * @ingroup API
*/
class ApiQueryBacklinks extends ApiQueryGeneratorBase {
- private $params, $rootTitle, $contRedirs, $contLevel, $contTitle, $contID, $redirID;
+ private $params, $rootTitle, $contRedirs, $contLevel, $contTitle, $contID, $redirID, $redirect;
+ private $bl_ns, $bl_from, $bl_table, $bl_code, $bl_title, $bl_sort, $bl_fields, $hasNS;
+ private $pageMap, $resultArr;
// output element name, database column field prefix, database table
private $backlinksSettings = array (
);
public function __construct($query, $moduleName) {
- $code = $prefix = $linktbl = null;
extract($this->backlinksSettings[$moduleName]);
+ $this->resultArr = array();
parent :: __construct($query, $moduleName, $code);
$this->bl_ns = $prefix . '_namespace';
private function prepareFirstQuery($resultPageSet = null) {
/* SELECT page_id, page_title, page_namespace, page_is_redirect
- * FROM pagelinks JOIN page ON pl_from=page_id
- * WHERE pl_title='Foo' AND pl_namespace=0
+ * FROM pagelinks, page WHERE pl_from=page_id
+ * AND pl_title='Foo' AND pl_namespace=0
* LIMIT 11 ORDER BY pl_from
*/
- $db = $this->getDb();
- list($tblpage, $tbllinks) = $db->tableNamesN('page', $this->bl_table);
- $this->addTables("$tbllinks JOIN $tblpage ON {$this->bl_from}=page_id");
+ $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'));
else
$this->addFields($resultPageSet->getPageTableFields());
$this->addFields('page_is_redirect');
- $this->addWhereFld($this->bl_title, $this->rootTitle->getDbKey());
+ $this->addWhereFld($this->bl_title, $this->rootTitle->getDBkey());
if($this->hasNS)
$this->addWhereFld($this->bl_ns, $this->rootTitle->getNamespace());
$this->addWhereFld('page_namespace', $this->params['namespace']);
if(!is_null($this->contID))
- $this->addWhere("page_id>={$this->contID}");
+ $this->addWhere("{$this->bl_from}>={$this->contID}");
if($this->params['filterredir'] == 'redirects')
$this->addWhereFld('page_is_redirect', 1);
if($this->params['filterredir'] == 'nonredirects')
private function prepareSecondQuery($resultPageSet = null) {
/* SELECT page_id, page_title, page_namespace, page_is_redirect, pl_title, pl_namespace
- * FROM pagelinks JOIN page ON pl_from=page_id
- * WHERE (pl_title='Foo' AND pl_namespace=0) OR (pl_title='Bar' AND pl_namespace=1)
- * LIMIT 11 ORDER BY pl_namespace, pl_title, pl_from
+ FROM pagelinks, page WHERE pl_from=page_id
+ AND (pl_title='Foo' AND pl_namespace=0) OR (pl_title='Bar' AND pl_namespace=1)
+ ORDER BY pl_namespace, pl_title, pl_from LIMIT 11
*/
- $db = $this->getDb();
- list($tblpage, $tbllinks) = $db->tableNamesN('page', $this->bl_table);
- $this->addTables("$tbllinks JOIN $tblpage ON {$this->bl_from}=page_id");
+ $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($this->bl_title);
if($this->hasNS)
$this->addFields($this->bl_ns);
- $titleWhere = '';
+ // We can't use LinkBatch here because $this->hasNS may be false
+ $titleWhere = array();
foreach($this->redirTitles as $t)
- $titleWhere .= ($titleWhere != '' ? " OR " : '') .
- "({$this->bl_title} = '{$t->getDBKey()}'" .
- ($this->hasNS ? " AND {$this->bl_ns} = '{$t->getNamespace()}'" : "") .
- ")";
- $this->addWhere($titleWhere);
+ $titleWhere[] = "{$this->bl_title} = ".$db->addQuotes($t->getDBkey()).
+ ($this->hasNS ? " AND {$this->bl_ns} = '{$t->getNamespace()}'" : "");
+ $this->addWhere($db->makeList($titleWhere, LIST_OR));
$this->addWhereFld('page_namespace', $this->params['namespace']);
if(!is_null($this->redirID))
- $this->addWhere("page_id>={$this->redirID}");
+ {
+ $first = $this->redirTitles[0];
+ $title = $db->strencode($first->getDBkey());
+ $ns = $first->getNamespace();
+ $from = $this->redirID;
+ if($this->hasNS)
+ $this->addWhere("{$this->bl_ns} > $ns OR ".
+ "({$this->bl_ns} = $ns AND ".
+ "({$this->bl_title} > '$title' OR ".
+ "({$this->bl_title} = '$title' AND ".
+ "{$this->bl_from} >= $from)))");
+ else
+ $this->addWhere("{$this->bl_title} > '$title' OR ".
+ "({$this->bl_title} = '$title' AND ".
+ "{$this->bl_from} >= $from)");
+
+ }
if($this->params['filterredir'] == 'redirects')
$this->addWhereFld('page_is_redirect', 1);
if($this->params['filterredir'] == 'nonredirects')
$this->addWhereFld('page_is_redirect', 0);
$this->addOption('LIMIT', $this->params['limit'] + 1);
$this->addOption('ORDER BY', $this->bl_sort);
+ $this->addOption('USE INDEX', array('page' => 'PRIMARY'));
}
private function run($resultPageSet = null) {
$this->params = $this->extractRequestParams(false);
- $userMax = ( $this->params['redirect'] ? ApiBase::LIMIT_BIG1/2 : ApiBase::LIMIT_BIG1 );
- $botMax = ( $this->params['redirect'] ? ApiBase::LIMIT_BIG2/2 : ApiBase::LIMIT_BIG2 );
+ $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 );
if( $this->params['limit'] == 'max' ) {
$this->params['limit'] = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
$this->getResult()->addValue( 'limits', $this->getModuleName(), $this->params['limit'] );
$this->prepareFirstQuery($resultPageSet);
$db = $this->getDB();
- $res = $this->select(__METHOD__);
+ $res = $this->select(__METHOD__.'::firstQuery');
$count = 0;
- $this->data = array ();
+ $this->pageMap = array(); // Maps ns and title to pageid
$this->continueStr = null;
$this->redirTitles = array();
while ($row = $db->fetchObject($res)) {
$this->extractRowInfo($row);
else
{
+ $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
if($row->page_is_redirect)
$this->redirTitles[] = Title::makeTitle($row->page_namespace, $row->page_title);
$resultPageSet->processDbRow($row);
}
$db->freeResult($res);
- if($this->params['redirect'] && !empty($this->redirTitles))
+ if($this->redirect && count($this->redirTitles))
{
$this->resetQueryParams();
$this->prepareSecondQuery($resultPageSet);
- $res = $this->select(__METHOD__);
+ $res = $this->select(__METHOD__.'::secondQuery');
$count = 0;
while($row = $db->fetchObject($res))
{
// We've reached the one extra which shows that there are additional pages to be had. Stop here...
// We need to keep the parent page of this redir in
if($this->hasNS)
- $contTitle = Title::makeTitle($row->{$this->bl_ns}, $row->{$this->bl_title});
+ $parentID = $this->pageMap[$row->{$this->bl_ns}][$row->{$this->bl_title}];
else
- $contTitle = Title::makeTitle(NS_IMAGE, $row->{$this->bl_title});
- $this->continueStr = $this->getContinueRedirStr($contTitle->getArticleID(), $row->page_id);
+ $parentID = $this->pageMap[NS_IMAGE][$row->{$this->bl_title}];
+ $this->continueStr = $this->getContinueRedirStr($parentID, $row->page_id);
break;
}
}
$db->freeResult($res);
}
- if(!is_null($this->continueStr))
- $this->setContinueEnumParameter('continue', $this->continueStr);
-
if (is_null($resultPageSet)) {
- $resultData = array();
- foreach($this->data as $ns => $a)
- foreach($a as $title => $arr)
- $resultData[$arr['pageid']] = $arr;
- $result = $this->getResult();
- $result->setIndexedTagName($resultData, $this->bl_code);
- $result->addValue('query', $this->getModuleName(), $resultData);
+ // Try to add the result data in one go and pray that it fits
+ $fit = $this->getResult()->addValue('query', $this->getModuleName(), array_values($this->resultArr));
+ if(!$fit)
+ {
+ // It didn't fit. Add elements one by one until the
+ // result is full.
+ foreach($this->resultArr as $pageID => $arr)
+ {
+ // Add the basic entry without redirlinks first
+ $fit = $this->getResult()->addValue(
+ array('query', $this->getModuleName()),
+ null, array_diff_key($arr, array('redirlinks' => '')));
+ if(!$fit)
+ {
+ $this->continueStr = $this->getContinueStr($pageID);
+ break;
+ }
+
+ $hasRedirs = false;
+ foreach((array)@$arr['redirlinks'] as $key => $redir)
+ {
+ $fit = $this->getResult()->addValue(
+ array('query', $this->getModuleName(), $pageID, 'redirlinks'),
+ $key, $redir);
+ if(!$fit)
+ {
+ $this->continueStr = $this->getContinueRedirStr($pageID, $redir['pageid']);
+ break;
+ }
+ $hasRedirs = true;
+ }
+ if($hasRedirs)
+ $this->getResult()->setIndexedTagName_internal(
+ array('query', $this->getModuleName(), $pageID, 'redirlinks'),
+ $this->bl_code);
+ if(!$fit)
+ break;
+ }
+ }
+
+ $this->getResult()->setIndexedTagName_internal(
+ array('query', $this->getModuleName()),
+ $this->bl_code);
}
+ if(!is_null($this->continueStr))
+ $this->setContinueEnumParameter('continue', $this->continueStr);
}
private function extractRowInfo($row) {
- if(!isset($this->data[$row->page_namespace][$row->page_title])) {
- $this->data[$row->page_namespace][$row->page_title]['pageid'] = $row->page_id;
- ApiQueryBase::addTitleInfo($this->data[$row->page_namespace][$row->page_title], Title::makeTitle($row->page_namespace, $row->page_title));
- if($row->page_is_redirect)
- {
- $this->data[$row->page_namespace][$row->page_title]['redirect'] = '';
- $this->redirTitles[] = Title::makeTitle($row->page_namespace, $row->page_title);
- }
+ $this->pageMap[$row->page_namespace][$row->page_title] = $row->page_id;
+ $t = Title::makeTitle($row->page_namespace, $row->page_title);
+ $a = array('pageid' => intval($row->page_id));
+ ApiQueryBase::addTitleInfo($a, $t);
+ if($row->page_is_redirect)
+ {
+ $a['redirect'] = '';
+ $this->redirTitles[] = $t;
}
+ // Put all the results in an array first
+ $this->resultArr[$a['pageid']] = $a;
}
private function extractRedirRowInfo($row)
{
- $a['pageid'] = $row->page_id;
+ $a['pageid'] = intval($row->page_id);
ApiQueryBase::addTitleInfo($a, Title::makeTitle($row->page_namespace, $row->page_title));
if($row->page_is_redirect)
$a['redirect'] = '';
- $ns = $this->hasNS ? $row->{$this->bl_ns} : NS_IMAGE;
- $this->data[$ns][$row->{$this->bl_title}]['redirlinks'][] = $a;
- $this->getResult()->setIndexedTagName($this->data[$ns][$row->{$this->bl_title}]['redirlinks'], $this->bl_code);
+ $ns = $this->hasNS ? $row->{$this->bl_ns} : NS_FILE;
+ $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
+ // Put all the results in an array first
+ $this->resultArr[$parentID]['redirlinks'][] = $a;
+ $this->getResult()->setIndexedTagName($this->resultArr[$parentID]['redirlinks'], $this->bl_code);
}
protected function processContinue() {
- $pageSet = $this->getPageSet();
- $count = $pageSet->getTitleCount();
-
if (!is_null($this->params['continue']))
$this->parseContinueParam();
else {
- $title = $this->params['title'];
- if (!is_null($title)) {
- $this->rootTitle = Title :: newFromText($title);
- } else { // This case is obsolete. Will support this for a while
- if ($count !== 1)
- $this->dieUsage("The {$this->getModuleName()} query requires one title to start", 'bad_title_count');
- $this->rootTitle = current($pageSet->getTitles()); // only one title there
- $this->setWarning('Using titles parameter is obsolete for this list. Use ' . $this->encodeParamName('title') . ' instead.');
+ if ( $this->params['title'] !== "" ) {
+ $title = Title::newFromText( $this->params['title'] );
+ if ( !$title ) {
+ $this->dieUsageMsg(array('invalidtitle', $this->params['title']));
+ } else {
+ $this->rootTitle = $title;
+ }
+ } else {
+ $this->dieUsageMsg(array('missingparam', 'title'));
}
}
// only image titles are allowed for the root in imageinfo mode
- if (!$this->hasNS && $this->rootTitle->getNamespace() !== NS_IMAGE)
+ if (!$this->hasNS && $this->rootTitle->getNamespace() !== NS_FILE)
$this->dieUsage("The title for {$this->getModuleName()} query must be an image", 'bad_image_title');
}
public function getParamDescription() {
$retval = array (
- 'title' => 'Title to search. If null, titles= parameter will be used instead, but will be obsolete soon.',
+ 'title' => 'Title to search.',
'continue' => 'When more results are available, use this to continue.',
'namespace' => 'The namespace to enumerate.',
'filterredir' => 'How to filter for redirects'
if($this->getModuleName() != 'embeddedin')
return array_merge($retval, array(
'redirect' => 'If linking page is a redirect, find all pages that link to that redirect as well. Maximum limit is halved.',
- 'limit' => "How many total pages to return. If {$this->bl_code}redirect is enabled, limit applies to each level separately."
+ 'limit' => "How many total pages to return. If {$this->bl_code}redirect is enabled, limit applies to each level separately (which means you may get up to 2 * limit results)."
));
return array_merge($retval, array(
'limit' => "How many total pages to return."
"api.php?action=query&generator=embeddedin&geititle=Template:Stub&prop=info"
),
'imageusage' => array (
- "api.php?action=query&list=imageusage&iutitle=Image:Albert%20Einstein%20Head.jpg",
- "api.php?action=query&generator=imageusage&giutitle=Image:Albert%20Einstein%20Head.jpg&prop=info"
+ "api.php?action=query&list=imageusage&iutitle=File:Albert%20Einstein%20Head.jpg",
+ "api.php?action=query&generator=imageusage&giutitle=File:Albert%20Einstein%20Head.jpg&prop=info"
)
);