MediaWiki
REL1_21
|
00001 <?php 00030 class ApiOpenSearch extends ApiBase { 00031 00032 public function getCustomPrinter() { 00033 return $this->getMain()->createPrinterByName( 'json' ); 00034 } 00035 00036 public function execute() { 00037 global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry; 00038 $params = $this->extractRequestParams(); 00039 $search = $params['search']; 00040 $limit = $params['limit']; 00041 $namespaces = $params['namespace']; 00042 $suggest = $params['suggest']; 00043 00044 // Some script that was loaded regardless of wgEnableOpenSearchSuggest, likely cached. 00045 if ( $suggest && !$wgEnableOpenSearchSuggest ) { 00046 $searches = array(); 00047 } else { 00048 // Open search results may be stored for a very long time 00049 $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry ); 00050 $this->getMain()->setCacheMode( 'public' ); 00051 00052 $searches = PrefixSearch::titleSearch( $search, $limit, 00053 $namespaces ); 00054 00055 // if the content language has variants, try to retrieve fallback results 00056 $fallbackLimit = $limit - count( $searches ); 00057 if ( $fallbackLimit > 0 ) { 00058 global $wgContLang; 00059 00060 $fallbackSearches = $wgContLang->autoConvertToAllVariants( $search ); 00061 $fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) ); 00062 00063 foreach ( $fallbackSearches as $fbs ) { 00064 $fallbackSearchResult = PrefixSearch::titleSearch( $fbs, $fallbackLimit, 00065 $namespaces ); 00066 $searches = array_merge( $searches, $fallbackSearchResult ); 00067 $fallbackLimit -= count( $fallbackSearchResult ); 00068 00069 if ( $fallbackLimit == 0 ) { 00070 break; 00071 } 00072 } 00073 } 00074 } 00075 // Set top level elements 00076 $result = $this->getResult(); 00077 $result->addValue( null, 0, $search ); 00078 $result->addValue( null, 1, $searches ); 00079 } 00080 00081 public function getAllowedParams() { 00082 return array( 00083 'search' => null, 00084 'limit' => array( 00085 ApiBase::PARAM_DFLT => 10, 00086 ApiBase::PARAM_TYPE => 'limit', 00087 ApiBase::PARAM_MIN => 1, 00088 ApiBase::PARAM_MAX => 100, 00089 ApiBase::PARAM_MAX2 => 100 00090 ), 00091 'namespace' => array( 00092 ApiBase::PARAM_DFLT => NS_MAIN, 00093 ApiBase::PARAM_TYPE => 'namespace', 00094 ApiBase::PARAM_ISMULTI => true 00095 ), 00096 'suggest' => false, 00097 ); 00098 } 00099 00100 public function getParamDescription() { 00101 return array( 00102 'search' => 'Search string', 00103 'limit' => 'Maximum amount of results to return', 00104 'namespace' => 'Namespaces to search', 00105 'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false', 00106 ); 00107 } 00108 00109 public function getDescription() { 00110 return 'Search the wiki using the OpenSearch protocol'; 00111 } 00112 00113 public function getExamples() { 00114 return array( 00115 'api.php?action=opensearch&search=Te' 00116 ); 00117 } 00118 00119 public function getHelpUrls() { 00120 return 'https://www.mediawiki.org/wiki/API:Opensearch'; 00121 } 00122 }