MediaWiki  REL1_24
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 
00044         return $this->getMain()->createPrinterByName( $allowed[0] );
00045     }
00046 
00047     public function execute() {
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 && !$this->getConfig()->get( 'EnableOpenSearchSuggest' ) ) {
00056             $searches = array();
00057         } else {
00058             // Open search results may be stored for a very long time
00059             $this->getMain()->setCacheMaxAge( $this->getConfig()->get( 'SearchSuggestCacheExpiry' ) );
00060             $this->getMain()->setCacheMode( 'public' );
00061 
00062             $searcher = new StringPrefixSearch;
00063             $searches = $searcher->searchWithVariants( $search, $limit, $namespaces );
00064         }
00065         // Set top level elements
00066         $result = $this->getResult();
00067         $result->addValue( null, 0, $search );
00068         $result->addValue( null, 1, $searches );
00069     }
00070 
00071     public function getAllowedParams() {
00072         return array(
00073             'search' => null,
00074             'limit' => array(
00075                 ApiBase::PARAM_DFLT => $this->getConfig()->get( 'OpenSearchDefaultLimit' ),
00076                 ApiBase::PARAM_TYPE => 'limit',
00077                 ApiBase::PARAM_MIN => 1,
00078                 ApiBase::PARAM_MAX => 100,
00079                 ApiBase::PARAM_MAX2 => 100
00080             ),
00081             'namespace' => array(
00082                 ApiBase::PARAM_DFLT => NS_MAIN,
00083                 ApiBase::PARAM_TYPE => 'namespace',
00084                 ApiBase::PARAM_ISMULTI => true
00085             ),
00086             'suggest' => false,
00087             'format' => array(
00088                 ApiBase::PARAM_DFLT => 'json',
00089                 ApiBase::PARAM_TYPE => array( 'json', 'jsonfm' ),
00090             )
00091         );
00092     }
00093 
00094     public function getParamDescription() {
00095         return array(
00096             'search' => 'Search string',
00097             'limit' => 'Maximum amount of results to return',
00098             'namespace' => 'Namespaces to search',
00099             'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
00100             'format' => 'The format of the output',
00101         );
00102     }
00103 
00104     public function getDescription() {
00105         return 'Search the wiki using the OpenSearch protocol.';
00106     }
00107 
00108     public function getExamples() {
00109         return array(
00110             'api.php?action=opensearch&search=Te'
00111         );
00112     }
00113 
00114     public function getHelpUrls() {
00115         return 'https://www.mediawiki.org/wiki/API:Opensearch';
00116     }
00117 }