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