From be25e4316d8d18891f2e8ea6a141e756e8d685bb Mon Sep 17 00:00:00 2001 From: Sam Reed Date: Wed, 5 Jan 2011 19:45:19 +0000 Subject: [PATCH] * (bug 26485) add a elextlinks param to prop=extlinks Refactored some code out of ApiQueryExtLinksUsage Had to duplicate some code into ApiQueryExternalLinks (Boo). Marked with a TODO to fix it up --- RELEASE-NOTES | 1 + includes/api/ApiQueryExtLinksUsage.php | 36 +++++++++++++++--------- includes/api/ApiQueryExternalLinks.php | 39 ++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 14 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index a1cbb4c877..d6cdc757d6 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -77,6 +77,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 26460) Add support for listing category members by category pageid * (bug 26482) add a imimages param to prop=images * (bug 26498) allow LinksUpdate with API +* (bug 26485) add a elextlinks param to prop=extlinks === Languages updated in 1.18 === diff --git a/includes/api/ApiQueryExtLinksUsage.php b/includes/api/ApiQueryExtLinksUsage.php index 1e26a9ac8e..e9439c56e3 100644 --- a/includes/api/ApiQueryExtLinksUsage.php +++ b/includes/api/ApiQueryExtLinksUsage.php @@ -50,24 +50,15 @@ class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase { $this->run( $resultPageSet ); } + /** + * @para $resultPageSet ApiPageSet + * @return void + */ private function run( $resultPageSet = null ) { $params = $this->extractRequestParams(); - $protocol = $params['protocol']; $query = $params['query']; - - // Find the right prefix - global $wgUrlProtocols; - if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) { - foreach ( $wgUrlProtocols as $p ) { - if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) { - $protocol = $p; - break; - } - } - } else { - $protocol = null; - } + $protocol = self::getProtocolPrefix( $params['protocol'] ); $db = $this->getDB(); $this->addTables( array( 'page', 'externallinks' ) ); // must be in this order for 'USE INDEX' @@ -195,6 +186,23 @@ class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase { return $protocols; } + public static function getProtocolPrefix( $protocol ) { + // Find the right prefix + global $wgUrlProtocols; + if ( $protocol && !in_array( $protocol, $wgUrlProtocols ) ) { + foreach ( $wgUrlProtocols as $p ) { + if ( substr( $p, 0, strlen( $protocol ) ) === $protocol ) { + $protocol = $p; + break; + } + } + + return $protocol; + } else { + return null; + } + } + public function getParamDescription() { $p = $this->getModulePrefix(); return array( diff --git a/includes/api/ApiQueryExternalLinks.php b/includes/api/ApiQueryExternalLinks.php index 7268a5ee7e..53a76076b9 100644 --- a/includes/api/ApiQueryExternalLinks.php +++ b/includes/api/ApiQueryExternalLinks.php @@ -46,6 +46,11 @@ class ApiQueryExternalLinks extends ApiQueryBase { } $params = $this->extractRequestParams(); + $db = $this->getDB(); + + $query = $params['query']; + $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] ); + $this->addFields( array( 'el_from', 'el_to' @@ -54,6 +59,23 @@ class ApiQueryExternalLinks extends ApiQueryBase { $this->addTables( 'externallinks' ); $this->addWhereFld( 'el_from', array_keys( $this->getPageSet()->getGoodTitles() ) ); + //TODO: Refactor out,duplicated from ApiQueryExtLinksUsage + if ( !is_null( $query ) || $query != '' ) { + if ( is_null( $protocol ) ) { + $protocol = 'http://'; + } + + $likeQuery = LinkFilter::makeLikeArray( $query, $protocol ); + if ( !$likeQuery ) { + $this->dieUsage( 'Invalid query', 'bad_query' ); + } + + $likeQuery = LinkFilter::keepOneWildcard( $likeQuery ); + $this->addWhere( 'el_index ' . $db->buildLike( $likeQuery ) ); + } elseif ( !is_null( $protocol ) ) { + $this->addWhere( 'el_index ' . $db->buildLike( "$protocol", $db->anyString() ) ); + } + // Don't order by el_from if it's constant in the WHERE clause if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) { $this->addOption( 'ORDER BY', 'el_from' ); @@ -98,13 +120,24 @@ class ApiQueryExternalLinks extends ApiQueryBase { ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 ), 'offset' => null, + 'protocol' => array( + ApiBase::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(), + ApiBase::PARAM_DFLT => '', + ), + 'query' => null, ); } public function getParamDescription() { + $p = $this->getModulePrefix(); return array( 'limit' => 'How many links to return', 'offset' => 'When more results are available, use this to continue', + 'protocol' => array( + "Protocol of the url. If empty and {$p}query set, the protocol is http.", + "Leave both this and {$p}query empty to list all external links" + ), + 'query' => 'Search string without protocol. Useful for checking whether a certain page contains a certain external url', ); } @@ -112,6 +145,12 @@ class ApiQueryExternalLinks extends ApiQueryBase { return 'Returns all external urls (not interwikies) from the given page(s)'; } + public function getPossibleErrors() { + return array_merge( parent::getPossibleErrors(), array( + array( 'code' => 'bad_query', 'info' => 'Invalid query' ), + ) ); + } + protected function getExamples() { return array( 'Get a list of external links on the [[Main Page]]:', -- 2.20.1