Merge "Chunked upload: Return expected offset on offset error"
[lhc/web/wiklou.git] / includes / api / ApiOpenSearch.php
index 4a9e216..f24a03f 100644 (file)
@@ -59,7 +59,7 @@ class ApiOpenSearch extends ApiBase {
        }
 
        public function getCustomPrinter() {
-               switch( $this->getFormat() ) {
+               switch ( $this->getFormat() ) {
                        case 'json':
                                return $this->getMain()->createPrinterByName( 'json' . $this->fm );
 
@@ -96,7 +96,7 @@ class ApiOpenSearch extends ApiBase {
                        $this->search( $search, $limit, $namespaces, $resolveRedir, $results );
 
                        // Allow hooks to populate extracts and images
-                       wfRunHooks( 'ApiOpenSearchSuggest', array( &$results ) );
+                       Hooks::run( 'ApiOpenSearchSuggest', array( &$results ) );
 
                        // Trim extracts, if necessary
                        $length = $this->getConfig()->get( 'OpenSearchDescriptionLength' );
@@ -118,31 +118,41 @@ class ApiOpenSearch extends ApiBase {
         * @param int $limit Maximum items to return
         * @param array $namespaces Namespaces to search
         * @param bool $resolveRedir Whether to resolve redirects
-        * @param array &$results Put results here
+        * @param array &$results Put results here. Keys have to be integers.
         */
        protected function search( $search, $limit, $namespaces, $resolveRedir, &$results ) {
                // Find matching titles as Title objects
                $searcher = new TitlePrefixSearch;
                $titles = $searcher->searchWithVariants( $search, $limit, $namespaces );
+               if ( !$titles ) {
+                       return;
+               }
+
+               // Special pages need unique integer ids in the return list, so we just
+               // assign them negative numbers because those won't clash with the
+               // always positive articleIds that non-special pages get.
+               $nextSpecialPageId = -1;
 
                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
@@ -158,7 +168,12 @@ class ApiOpenSearch extends ApiBase {
                                }
                                if ( !isset( $seen[$ns][$dbkey] ) ) {
                                        $seen[$ns][$dbkey] = true;
-                                       $results[$title->getArticleId()] = array(
+                                       $resultId = $title->getArticleId();
+                                       if ( $resultId === 0 ) {
+                                               $resultId = $nextSpecialPageId;
+                                               $nextSpecialPageId -= 1;
+                                       }
+                                       $results[$resultId] = array(
                                                'title' => $title,
                                                'redirect from' => $from,
                                                'extract' => false,
@@ -170,7 +185,12 @@ class ApiOpenSearch extends ApiBase {
                        }
                } else {
                        foreach ( $titles as $title ) {
-                               $results[$title->getArticleId()] = array(
+                               $resultId = $title->getArticleId();
+                               if ( $resultId === 0 ) {
+                                       $resultId = $nextSpecialPageId;
+                                       $nextSpecialPageId -= 1;
+                               }
+                               $results[$resultId] = array(
                                        'title' => $title,
                                        'redirect from' => null,
                                        'extract' => false,
@@ -320,6 +340,7 @@ class ApiOpenSearch extends ApiBase {
         *
         * @param string $type MIME type
         * @return string
+        * @throws MWException
         */
        public static function getOpenSearchTemplate( $type ) {
                global $wgOpenSearchTemplate, $wgCanonicalServer;