API: Fix ApiQueryBacklinks redirlinks
authorBrad Jorsch <bjorsch@wikimedia.org>
Fri, 10 Oct 2014 19:22:35 +0000 (15:22 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Fri, 10 Oct 2014 19:27:39 +0000 (15:27 -0400)
The fast case needs to pass the redirlinks subarray through array_values
and set an indexed tag name.

Bug: 71907
Change-Id: I865aad6485a2598c9e574ab5c38e8e0923d11ddd

includes/api/ApiQueryBacklinks.php

index 52445d2..f00a6e7 100644 (file)
@@ -287,7 +287,6 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
 
                $res = $this->select( __METHOD__ );
                $count = 0;
-               $result = $this->getResult();
                foreach ( $res as $row ) {
                        $ns = $this->hasNS ? $row->{$this->bl_ns} : NS_FILE;
 
@@ -319,10 +318,6 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
                                $parentID = $this->pageMap[$ns][$row->{$this->bl_title}];
                                // Put all the results in an array first
                                $this->resultArr[$parentID]['redirlinks'][$row->page_id] = $a;
-                               $result->setIndexedTagName(
-                                       $this->resultArr[$parentID]['redirlinks'],
-                                       $this->bl_code
-                               );
                        } else {
                                $resultPageSet->processDbRow( $row );
                        }
@@ -427,7 +422,15 @@ class ApiQueryBacklinks extends ApiQueryGeneratorBase {
 
                if ( is_null( $resultPageSet ) ) {
                        // Try to add the result data in one go and pray that it fits
-                       $fit = $result->addValue( 'query', $this->getModuleName(), array_values( $this->resultArr ) );
+                       $code = $this->bl_code;
+                       $data = array_map( function ( $arr ) use ( $result, $code ) {
+                               if ( isset( $arr['redirlinks'] ) ) {
+                                       $arr['redirlinks'] = array_values( $arr['redirlinks'] );
+                                       $result->setIndexedTagName( $arr['redirlinks'], $code );
+                               }
+                               return $arr;
+                       }, array_values( $this->resultArr ) );
+                       $fit = $result->addValue( 'query', $this->getModuleName(), $data );
                        if ( !$fit ) {
                                // It didn't fit. Add elements one by one until the
                                // result is full.