added unit tests for WikitextContent
[lhc/web/wiklou.git] / includes / api / ApiQueryExternalLinks.php
index ba5c996..a9fbc83 100644 (file)
  * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( "ApiQueryBase.php" );
-}
-
 /**
  * A query module to list all external URLs found on a given set of pages.
  *
@@ -46,7 +41,6 @@ class ApiQueryExternalLinks extends ApiQueryBase {
                }
 
                $params = $this->extractRequestParams();
-               $db = $this->getDB();
 
                $query = $params['query'];
                $protocol = ApiQueryExtLinksUsage::getProtocolPrefix( $params['protocol'] );
@@ -59,21 +53,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( $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
@@ -81,8 +64,14 @@ class ApiQueryExternalLinks extends ApiQueryBase {
                        $this->addOption( 'ORDER BY', 'el_from' );
                }
 
+               // If we're querying all protocols, use DISTINCT to avoid repeating protocol-relative links twice
+               if ( $protocol === null ) {
+                       $this->addOption( 'DISTINCT' );
+               }
+
                $this->addOption( 'LIMIT', $params['limit'] + 1 );
-               if ( !is_null( $params['offset'] ) ) {
+               $offset = isset( $params['offset'] ) ? $params['offset'] : 0;
+               if ( $offset ) {
                        $this->addOption( 'OFFSET', $params['offset'] );
                }
 
@@ -93,14 +82,15 @@ class ApiQueryExternalLinks extends ApiQueryBase {
                        if ( ++$count > $params['limit'] ) {
                                // We've reached the one extra which shows that
                                // there are additional pages to be had. Stop here...
-                               $this->setContinueEnumParameter( 'offset', @$params['offset'] + $params['limit'] );
+                               $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
                                break;
                        }
                        $entry = array();
+                       // We *could* run this through wfExpandUrl() but I think it's better to output the link verbatim, even if it's protocol-relative --Roan
                        ApiResult::setContent( $entry, $row->el_to );
                        $fit = $this->addPageSubItem( $row->el_from, $entry );
                        if ( !$fit ) {
-                               $this->setContinueEnumParameter( 'offset', @$params['offset'] + $count - 1 );
+                               $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
                                break;
                        }
                }
@@ -119,7 +109,9 @@ class ApiQueryExternalLinks extends ApiQueryBase {
                                ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
                                ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
                        ),
-                       'offset' => null,
+                       'offset' => array(
+                               ApiBase::PARAM_TYPE => 'integer'
+                       ),
                        'protocol' => array(
                                ApiBase::PARAM_TYPE => ApiQueryExtLinksUsage::prepareProtocols(),
                                ApiBase::PARAM_DFLT => '',
@@ -151,13 +143,16 @@ class ApiQueryExternalLinks extends ApiQueryBase {
                ) );
        }
 
-       protected function getExamples() {
+       public function getExamples() {
                return array(
-                       'Get a list of external links on the [[Main Page]]:',
-                       '  api.php?action=query&prop=extlinks&titles=Main%20Page',
+                       'api.php?action=query&prop=extlinks&titles=Main%20Page' => 'Get a list of external links on the [[Main Page]]',
                );
        }
 
+       public function getHelpUrls() {
+               return 'https://www.mediawiki.org/wiki/API:Properties#extlinks_.2F_el';
+       }
+
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }