- $search = null;
- extract($this->ExtractRequestParams());
-
- // Open search results may be stored for a very long time
- $this->getMain()->setCacheMaxAge(1200);
-
- $title = Title :: newFromText($search);
- if(!$title)
- return; // Return empty result
-
- // Prepare nested request
- $params = new FauxRequest(array (
- 'action' => 'query',
- 'list' => 'allpages',
- 'apnamespace' => $title->getNamespace(),
- 'aplimit' => 10,
- 'apprefix' => $title->getDBkey()
- ));
-
- // Execute
- $module = new ApiMain($params);
- $module->execute();
-
- // Get resulting data
- $data = $module->getResultData();
-
- // Reformat useful data for future printing by JSON engine
- $srchres = array ();
- foreach ($data['query']['allpages'] as & $pageinfo) {
- // Note: this data will no be printable by the xml engine
- // because it does not support lists of unnamed items
- $srchres[] = $pageinfo['title'];
+ global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry;
+ $params = $this->extractRequestParams();
+ $search = $params['search'];
+ $limit = $params['limit'];
+ $namespaces = $params['namespace'];
+ $suggest = $params['suggest'];
+
+ // MWSuggest or similar hit
+ if ( $suggest && !$wgEnableOpenSearchSuggest ) {
+ $searches = array();
+ } else {
+ // Open search results may be stored for a very long time
+ $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
+ $this->getMain()->setCacheMode( 'public' );
+
+ $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;
+ }
+ }
+ }