From f49b53df4609274e296af893cbcee916903a32f7 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Fri, 4 Apr 2008 11:22:58 +0000 Subject: [PATCH] API: * Cleaned up ApiPageSet::getRedirectTargets(), now uses the redirect table rather than the pagelinks table (this fixes bug 13965) * Added possibility to obtain all external links through list=exturlusage (per MinuteElectron's request on the mediawiki-api list) --- RELEASE-NOTES | 2 + includes/api/ApiPageSet.php | 73 +++++++------------------- includes/api/ApiQueryExtLinksUsage.php | 33 ++++++------ 3 files changed, 39 insertions(+), 69 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index afa87786bb..7aae02e674 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -187,6 +187,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * Added fallback8bitEncoding field to meta=siteinfo&siprop=general output * (bug 13544) Added prop=revid to action=parse * (bug 13603) Added siprop=usergroups to meta=siteinfo +* Cleaned up redirect resolution +* Added possibility to obtain all external links through list=exturlusage === Languages updated in 1.13 === diff --git a/includes/api/ApiPageSet.php b/includes/api/ApiPageSet.php index 24e9b2d2f1..1acfb2be75 100644 --- a/includes/api/ApiPageSet.php +++ b/includes/api/ApiPageSet.php @@ -512,68 +512,33 @@ class ApiPageSet extends ApiQueryBase { } private function getRedirectTargets() { - - $linkBatch = new LinkBatch(); + $lb = new LinkBatch(); $db = $this->getDB(); - // find redirect targets for all redirect pages $this->profileDBIn(); - $res = $db->select('pagelinks', array ( - 'pl_from', - 'pl_namespace', - 'pl_title' - ), array ( - 'pl_from' => array_keys($this->mPendingRedirectIDs - )), __METHOD__); + $res = $db->select('redirect', array( + 'rd_from', + 'rd_namespace', + 'rd_title' + ), array('rd_from' => array_keys($this->mPendingRedirectIDs)), + __METHOD__ + ); $this->profileDBOut(); - while ($row = $db->fetchObject($res)) { - - $plfrom = intval($row->pl_from); - - // Bug 7304 workaround - // ( http://bugzilla.wikipedia.org/show_bug.cgi?id=7304 ) - // A redirect page may have more than one link. - // This code will only use the first link returned. - if (isset ($this->mPendingRedirectIDs[$plfrom])) { // remove line when bug 7304 is fixed - - $titleStrFrom = $this->mPendingRedirectIDs[$plfrom]->getPrefixedText(); - $titleStrTo = Title :: makeTitle($row->pl_namespace, $row->pl_title)->getPrefixedText(); - unset ($this->mPendingRedirectIDs[$plfrom]); // remove line when bug 7304 is fixed - - // Avoid an infinite loop by checking if we have already processed this target - if (!isset ($this->mAllPages[$row->pl_namespace][$row->pl_title])) { - $linkBatch->add($row->pl_namespace, $row->pl_title); - } - } else { - // This redirect page has more than one link. - // This is very slow, but safer until bug 7304 is resolved - $title = Title :: newFromID($plfrom); - $titleStrFrom = $title->getPrefixedText(); - - $article = new Article($title); - $text = $article->getContent(); - $titleTo = Title :: newFromRedirect($text); - $titleStrTo = $titleTo->getPrefixedText(); - - if (is_null($titleStrTo)) - ApiBase :: dieDebug(__METHOD__, 'Bug7304 workaround: redir target from {$title->getPrefixedText()} not found'); - - // Avoid an infinite loop by checking if we have already processed this target - if (!isset ($this->mAllPages[$titleTo->getNamespace()][$titleTo->getDBkey()])) { - $linkBatch->addObj($titleTo); - } - } - - $this->mRedirectTitles[$titleStrFrom] = $titleStrTo; + while($row = $db->fetchObject($res)) + { + $rdfrom = intval($row->rd_from); + $from = Title::newFromId($row->rd_from)->getPrefixedText(); + $to = Title::makeTitle($row->rd_namespace, $row->rd_title)->getPrefixedText(); + unset($this->mPendingRedirectIDs[$rdfrom]); + if(!isset($this->mAllPages[$row->rd_namespace][$row->rd_title])) + $lb->add($row->rd_namespace, $row->rd_title); + $this->mRedirectTitles[$from] = $to; } $db->freeResult($res); - - // All IDs must exist in the page table - if (!empty($this->mPendingRedirectIDs[$plfrom])) + if(!empty($this->mPendingRedirectIDs)) ApiBase :: dieDebug(__METHOD__, 'Invalid redirect IDs were found'); - - return $linkBatch; + return $lb; } /** diff --git a/includes/api/ApiQueryExtLinksUsage.php b/includes/api/ApiQueryExtLinksUsage.php index 2c9b3bee49..2919adbf77 100644 --- a/includes/api/ApiQueryExtLinksUsage.php +++ b/includes/api/ApiQueryExtLinksUsage.php @@ -51,31 +51,34 @@ class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase { $protocol = $params['protocol']; $query = $params['query']; - if (is_null($query)) - $this->dieUsage('Missing required query parameter', 'params'); // Find the right prefix global $wgUrlProtocols; - foreach ($wgUrlProtocols as $p) { - if( substr( $p, 0, strlen( $protocol ) ) === $protocol ) { - $protocol = $p; - break; + if(!is_null($protocol) && $protocol != '' && !in_array($protocol, $wgUrlProtocols)) + { + foreach ($wgUrlProtocols as $p) { + if( substr( $p, 0, strlen( $protocol ) ) === $protocol ) { + $protocol = $p; + break; + } } } - $likeQuery = LinkFilter::makeLike($query , $protocol); - if (!$likeQuery) - $this->dieUsage('Invalid query', 'bad_query'); - $likeQuery = substr($likeQuery, 0, strpos($likeQuery,'%')+1); - + $db = $this->getDb(); $this->addTables(array('page','externallinks')); // must be in this order for 'USE INDEX' $this->addOption('USE INDEX', 'el_index'); - - $db = $this->getDB(); $this->addWhere('page_id=el_from'); - $this->addWhere('el_index LIKE ' . $db->addQuotes( $likeQuery )); $this->addWhereFld('page_namespace', $params['namespace']); + if(!is_null($query) || $query != '') + { + $likeQuery = LinkFilter::makeLike($query , $protocol); + if (!$likeQuery) + $this->dieUsage('Invalid query', 'bad_query'); + $likeQuery = substr($likeQuery, 0, strpos($likeQuery,'%')+1); + $this->addWhere('el_index LIKE ' . $db->addQuotes( $likeQuery )); + } + $prop = array_flip($params['prop']); $fld_ids = isset($prop['ids']); $fld_title = isset($prop['title']); @@ -178,7 +181,7 @@ class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase { 'prop' => 'What pieces of information to include', 'offset' => 'Used for paging. Use the value returned for "continue"', 'protocol' => 'Protocol of the url', - 'query' => 'Search string without protocol. See [[Special:LinkSearch]]', + 'query' => 'Search string without protocol. See [[Special:LinkSearch]]. Leave empty to list all external links (euprotocol will be ignored)', 'namespace' => 'The page namespace(s) to enumerate.', 'limit' => 'How many entries to return.' ); -- 2.20.1