* (bug 26485) add a elextlinks param to prop=extlinks
authorSam Reed <reedy@users.mediawiki.org>
Wed, 5 Jan 2011 19:45:19 +0000 (19:45 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Wed, 5 Jan 2011 19:45:19 +0000 (19:45 +0000)
Refactored some code out of ApiQueryExtLinksUsage

Had to duplicate some code into ApiQueryExternalLinks (Boo). Marked with a TODO to fix it up

RELEASE-NOTES
includes/api/ApiQueryExtLinksUsage.php
includes/api/ApiQueryExternalLinks.php

index a1cbb4c..d6cdc75 100644 (file)
@@ -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 ===
 
index 1e26a9a..e9439c5 100644 (file)
@@ -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(
index 7268a5e..53a7607 100644 (file)
@@ -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]]:',