Merge "Add attributes parameter to ShowSearchHitTitle"
[lhc/web/wiklou.git] / includes / api / ApiQueryLinks.php
index 6e5239f..508bdf3 100644 (file)
@@ -34,7 +34,7 @@ class ApiQueryLinks extends ApiQueryGeneratorBase {
        const LINKS = 'links';
        const TEMPLATES = 'templates';
 
-       private $table, $prefix, $helpUrl;
+       private $table, $prefix, $titlesParam, $helpUrl;
 
        public function __construct( ApiQuery $query, $moduleName ) {
                switch ( $moduleName ) {
@@ -42,13 +42,13 @@ class ApiQueryLinks extends ApiQueryGeneratorBase {
                                $this->table = 'pagelinks';
                                $this->prefix = 'pl';
                                $this->titlesParam = 'titles';
-                               $this->helpUrl = 'https://www.mediawiki.org/wiki/API:Links';
+                               $this->helpUrl = 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Links';
                                break;
                        case self::TEMPLATES:
                                $this->table = 'templatelinks';
                                $this->prefix = 'tl';
                                $this->titlesParam = 'templates';
-                               $this->helpUrl = 'https://www.mediawiki.org/wiki/API:Templates';
+                               $this->helpUrl = 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Templates';
                                break;
                        default:
                                ApiBase::dieDebug( __METHOD__, 'Unknown module name' );
@@ -87,22 +87,34 @@ class ApiQueryLinks extends ApiQueryGeneratorBase {
 
                $this->addTables( $this->table );
                $this->addWhereFld( $this->prefix . '_from', array_keys( $this->getPageSet()->getGoodTitles() ) );
-               $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] );
 
-               if ( !is_null( $params[$this->titlesParam] ) ) {
+               $multiNS = true;
+               $multiTitle = true;
+               if ( $params[$this->titlesParam] ) {
+                       // Filter the titles in PHP so our ORDER BY bug avoidance below works right.
+                       $filterNS = $params['namespace'] ? array_flip( $params['namespace'] ) : false;
+
                        $lb = new LinkBatch;
                        foreach ( $params[$this->titlesParam] as $t ) {
                                $title = Title::newFromText( $t );
                                if ( !$title ) {
-                                       $this->setWarning( "\"$t\" is not a valid title" );
-                               } else {
+                                       $this->addWarning( [ 'apiwarn-invalidtitle', wfEscapeWikiText( $t ) ] );
+                               } elseif ( !$filterNS || isset( $filterNS[$title->getNamespace()] ) ) {
                                        $lb->addObj( $title );
                                }
                        }
                        $cond = $lb->constructSet( $this->prefix, $this->getDB() );
                        if ( $cond ) {
                                $this->addWhere( $cond );
+                               $multiNS = count( $lb->data ) !== 1;
+                               $multiTitle = count( call_user_func_array( 'array_merge', $lb->data ) ) !== 1;
+                       } else {
+                               // No titles so no results
+                               return;
                        }
+               } elseif ( $params['namespace'] ) {
+                       $this->addWhereFld( $this->prefix . '_namespace', $params['namespace'] );
+                       $multiNS = count( $params['namespace'] ) !== 1;
                }
 
                if ( !is_null( $params['continue'] ) ) {
@@ -131,13 +143,15 @@ class ApiQueryLinks extends ApiQueryGeneratorBase {
                if ( count( $this->getPageSet()->getGoodTitles() ) != 1 ) {
                        $order[] = $this->prefix . '_from' . $sort;
                }
-               if ( count( $params['namespace'] ) != 1 ) {
+               if ( $multiNS ) {
                        $order[] = $this->prefix . '_namespace' . $sort;
                }
-
-               $order[] = $this->prefix . '_title' . $sort;
-               $this->addOption( 'ORDER BY', $order );
-               $this->addOption( 'USE INDEX', $this->prefix . '_from' );
+               if ( $multiTitle ) {
+                       $order[] = $this->prefix . '_title' . $sort;
+               }
+               if ( $order ) {
+                       $this->addOption( 'ORDER BY', $order );
+               }
                $this->addOption( 'LIMIT', $params['limit'] + 1 );
 
                $res = $this->select( __METHOD__ );
@@ -182,7 +196,8 @@ class ApiQueryLinks extends ApiQueryGeneratorBase {
                return [
                        'namespace' => [
                                ApiBase::PARAM_TYPE => 'namespace',
-                               ApiBase::PARAM_ISMULTI => true
+                               ApiBase::PARAM_ISMULTI => true,
+                               ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ],
                        ],
                        'limit' => [
                                ApiBase::PARAM_DFLT => 10,