Refactor out the duplication I left with a TODO in r79659
authorSam Reed <reedy@users.mediawiki.org>
Sun, 27 Feb 2011 21:31:47 +0000 (21:31 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Sun, 27 Feb 2011 21:31:47 +0000 (21:31 +0000)
includes/api/ApiQueryBase.php
includes/api/ApiQueryExtLinksUsage.php
includes/api/ApiQueryExternalLinks.php

index 3c68ec5..3321987 100644 (file)
@@ -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' ),
index e9439c5..210eca9 100644 (file)
@@ -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'] );
index ba5c996..c4f8790 100644 (file)
@@ -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