Merge "Converted InfoAction to using the WAN cache"
[lhc/web/wiklou.git] / includes / api / ApiOpenSearch.php
index af09656..36026c2 100644 (file)
@@ -135,22 +135,24 @@ class ApiOpenSearch extends ApiBase {
 
                if ( $resolveRedir ) {
                        // Query for redirects
-                       $db = $this->getDb();
-                       $lb = new LinkBatch( $titles );
-                       $res = $db->select(
-                               array( 'page', 'redirect' ),
-                               array( 'page_namespace', 'page_title', 'rd_namespace', 'rd_title' ),
-                               array(
-                                       'rd_from = page_id',
-                                       'rd_interwiki IS NULL OR rd_interwiki = ' . $db->addQuotes( '' ),
-                                       $lb->constructSet( 'page', $db ),
-                               ),
-                               __METHOD__
-                       );
                        $redirects = array();
-                       foreach ( $res as $row ) {
-                               $redirects[$row->page_namespace][$row->page_title] =
-                                       array( $row->rd_namespace, $row->rd_title );
+                       $lb = new LinkBatch( $titles );
+                       if ( !$lb->isEmpty() ) {
+                               $db = $this->getDb();
+                               $res = $db->select(
+                                       array( 'page', 'redirect' ),
+                                       array( 'page_namespace', 'page_title', 'rd_namespace', 'rd_title' ),
+                                       array(
+                                               'rd_from = page_id',
+                                               'rd_interwiki IS NULL OR rd_interwiki = ' . $db->addQuotes( '' ),
+                                               $lb->constructSet( 'page', $db ),
+                                       ),
+                                       __METHOD__
+                               );
+                               foreach ( $res as $row ) {
+                                       $redirects[$row->page_namespace][$row->page_title] =
+                                               array( $row->rd_namespace, $row->rd_title );
+                               }
                        }
 
                        // Bypass any redirects
@@ -210,6 +212,7 @@ class ApiOpenSearch extends ApiBase {
                switch ( $this->getFormat() ) {
                        case 'json':
                                // http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.1
+                               $result->addArrayType( null, 'BCarray' );
                                $result->addValue( null, 0, strval( $search ) );
                                $terms = array();
                                $descriptions = array();
@@ -235,23 +238,24 @@ class ApiOpenSearch extends ApiBase {
                                );
                                $items = array();
                                foreach ( $results as $r ) {
-                                       $item = array();
-                                       $result->setContent( $item, $r['title']->getPrefixedText(), 'Text' );
-                                       $result->setContent( $item, $r['url'], 'Url' );
+                                       $item = array(
+                                               'Text' => $r['title']->getPrefixedText(),
+                                               'Url' => $r['url'],
+                                       );
                                        if ( is_string( $r['extract'] ) && $r['extract'] !== '' ) {
-                                               $result->setContent( $item, $r['extract'], 'Description' );
+                                               $item['Description'] = $r['extract'];
                                        }
                                        if ( is_array( $r['image'] ) && isset( $r['image']['source'] ) ) {
                                                $item['Image'] = array_intersect_key( $r['image'], $imageKeys );
                                        }
+                                       ApiResult::setSubelementsList( $item, array_keys( $item ) );
                                        $items[] = $item;
                                }
-                               $result->setIndexedTagName( $items, 'Item' );
+                               ApiResult::setIndexedTagName( $items, 'Item' );
                                $result->addValue( null, 'version', '2.0' );
                                $result->addValue( null, 'xmlns', 'http://opensearch.org/searchsuggest2' );
-                               $query = array();
-                               $result->setContent( $query, strval( $search ) );
-                               $result->addValue( null, 'Query', $query );
+                               $result->addValue( null, 'Query', strval( $search ) );
+                               $result->addSubelementsList( null, 'Query' );
                                $result->addValue( null, 'Section', $items );
                                break;
 
@@ -338,6 +342,7 @@ class ApiOpenSearch extends ApiBase {
         *
         * @param string $type MIME type
         * @return string
+        * @throws MWException
         */
        public static function getOpenSearchTemplate( $type ) {
                global $wgOpenSearchTemplate, $wgCanonicalServer;