Fix prefix search for special pages
authorNik Everett <neverett@wikimedia.org>
Fri, 12 Dec 2014 22:24:01 +0000 (17:24 -0500)
committerNik Everett <neverett@wikimedia.org>
Mon, 15 Dec 2014 16:25:16 +0000 (11:25 -0500)
Prefix search for special pages was returning only the first match.

Change-Id: I5849696de76ca588f7e626d7da319b8bddb3dce9

docs/hooks.txt
includes/api/ApiOpenSearch.php

index 0644536..369ad9f 100644 (file)
@@ -409,7 +409,8 @@ $options: Array Options passed to ApiHelp::getHelp
 
 'ApiOpenSearchSuggest': Called when constructing the OpenSearch results. Hooks
 can alter or append to the array.
-&$results: array of associative arrays. Keys are:
+&$results: array with integer keys to associative arrays. Keys in associative
+array:
   - title: Title object.
   - redirect from: Title or null.
   - extract: Description for this result.
index f4b99ae..af09656 100644 (file)
@@ -118,7 +118,7 @@ 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
@@ -128,6 +128,11 @@ class ApiOpenSearch extends ApiBase {
                        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();
@@ -161,7 +166,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,
@@ -173,7 +183,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,