* (bug 27479) API error when using both prop=pageprops and prop=info&inprop=displaytitle
[lhc/web/wiklou.git] / includes / api / ApiOpenSearch.php
index 397d715..19741b0 100644 (file)
@@ -1,9 +1,8 @@
 <?php
-
 /**
- * Created on Oct 13, 2006
  *
- * API for MediaWiki 1.8+
+ *
+ * Created on Oct 13, 2006
  *
  * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  *
  *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
  */
 
 if ( !defined( 'MEDIAWIKI' ) ) {
@@ -51,20 +52,39 @@ class ApiOpenSearch extends ApiBase {
 
                // MWSuggest or similar hit
                if ( $suggest && !$wgEnableOpenSearchSuggest ) {
-                       $srchres = array();
+                       $searches = array();
                } else {
-                       // Open search results may be stored for a very long
-                       // time
+                       // Open search results may be stored for a very long time
                        $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
-                       $this->getMain()->setCacheControl( array( 'must-revalidate' => false ) );
+                       $this->getMain()->setCacheMode( 'public' );
 
-                       $srchres = PrefixSearch::titleSearch( $search, $limit,
+                       $searches = PrefixSearch::titleSearch( $search, $limit,
                                $namespaces );
+                       
+                       // if the content language has variants, try to retrieve fallback results
+                       $fallbackLimit = $limit - count( $searches );
+                       if ( $fallbackLimit > 0 ) {
+                               global $wgContLang;
+
+                               $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
+                               $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
+
+                               foreach ( $fallbackSearches as $fbs ) {
+                                       $fallbackSearchResult = PrefixSearch::titleSearch( $fbs, $fallbackLimit,
+                                               $namespaces );
+                                       $searches = array_merge( $searches, $fallbackSearchResult );
+                                       $fallbackLimit -= count( $fallbackSearchResult );
+
+                                       if ( $fallbackLimit == 0 ) {
+                                               break;
+                                       }
+                               }
+                       }
                }
                // Set top level elements
                $result = $this->getResult();
                $result->addValue( null, 0, $search );
-               $result->addValue( null, 1, $srchres );
+               $result->addValue( null, 1, $searches );
        }
 
        public function getAllowedParams() {