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