From 573abcbd8c222ce114f81779a9d3af7ab20e7a84 Mon Sep 17 00:00:00 2001 From: Sam Reed Date: Sun, 27 Feb 2011 21:31:47 +0000 Subject: [PATCH] Refactor out the duplication I left with a TODO in r79659 --- includes/api/ApiQueryBase.php | 26 ++++++++++++++++++++++++++ includes/api/ApiQueryExtLinksUsage.php | 16 +++------------- includes/api/ApiQueryExternalLinks.php | 17 +++-------------- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/includes/api/ApiQueryBase.php b/includes/api/ApiQueryBase.php index 3c68ec5d9b..3321987f4c 100644 --- a/includes/api/ApiQueryBase.php +++ b/includes/api/ApiQueryBase.php @@ -440,6 +440,32 @@ abstract class ApiQueryBase extends ApiBase { return substr( $this->keyToTitle( $keyPart . 'x' ), 0, - 1 ); } + /** + * @param $query String + * @param $protocol String + * @return null|string + */ + public function prepareUrlQuerySearchString( $query = null, $protocol = null) { + $db = $this->getDb(); + 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 ); + return 'el_index ' . $db->buildLike( $likeQuery ); + } elseif ( !is_null( $protocol ) ) { + return 'el_index ' . $db->buildLike( "$protocol", $db->anyString() ); + } + + return null; + } + public function getPossibleErrors() { return array_merge( parent::getPossibleErrors(), array( array( 'invalidtitle', 'title' ), diff --git a/includes/api/ApiQueryExtLinksUsage.php b/includes/api/ApiQueryExtLinksUsage.php index e9439c56e3..210eca9440 100644 --- a/includes/api/ApiQueryExtLinksUsage.php +++ b/includes/api/ApiQueryExtLinksUsage.php @@ -66,20 +66,10 @@ class ApiQueryExtLinksUsage extends ApiQueryGeneratorBase { $this->addWhere( 'page_id=el_from' ); $this->addWhereFld( 'page_namespace', $params['namespace'] ); - if ( !is_null( $query ) || $query != '' ) { - if ( is_null( $protocol ) ) { - $protocol = 'http://'; - } - - $likeQuery = LinkFilter::makeLikeArray( $query, $protocol ); - if ( !$likeQuery ) { - $this->dieUsage( 'Invalid query', 'bad_query' ); - } + $whereQuery = $this->prepareUrlQuerySearchString( $db, $query, $protocol ); - $likeQuery = LinkFilter::keepOneWildcard( $likeQuery ); - $this->addWhere( 'el_index ' . $db->buildLike( $likeQuery ) ); - } elseif ( !is_null( $protocol ) ) { - $this->addWhere( 'el_index ' . $db->buildLike( "$protocol", $db->anyString() ) ); + if ( $whereQuery !== null ) { + $this->addWhere( $whereQuery ); } $prop = array_flip( $params['prop'] ); diff --git a/includes/api/ApiQueryExternalLinks.php b/includes/api/ApiQueryExternalLinks.php index ba5c996b32..c4f8790d95 100644 --- a/includes/api/ApiQueryExternalLinks.php +++ b/includes/api/ApiQueryExternalLinks.php @@ -59,21 +59,10 @@ 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' ); - } + $whereQuery = $this->prepareUrlQuerySearchString( $db, $query, $protocol ); - $likeQuery = LinkFilter::keepOneWildcard( $likeQuery ); - $this->addWhere( 'el_index ' . $db->buildLike( $likeQuery ) ); - } elseif ( !is_null( $protocol ) ) { - $this->addWhere( 'el_index ' . $db->buildLike( "$protocol", $db->anyString() ) ); + if ( $whereQuery !== null ) { + $this->addWhere( $whereQuery ); } // Don't order by el_from if it's constant in the WHERE clause -- 2.20.1