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