00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027
00028 require_once ( "ApiBase.php" );
00029 }
00030
00034 class ApiOpenSearch extends ApiBase {
00035
00036 public function __construct( $main, $action ) {
00037 parent :: __construct( $main, $action );
00038 }
00039
00040 public function getCustomPrinter() {
00041 return $this->getMain()->createPrinterByName( 'json' );
00042 }
00043
00044 public function execute() {
00045 global $wgEnableOpenSearchSuggest, $wgSearchSuggestCacheExpiry;
00046 $params = $this->extractRequestParams();
00047 $search = $params['search'];
00048 $limit = $params['limit'];
00049 $namespaces = $params['namespace'];
00050 $suggest = $params['suggest'];
00051
00052
00053 if ( $suggest && !$wgEnableOpenSearchSuggest )
00054 $srchres = array();
00055 else {
00056
00057
00058 $this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
00059 $this->getMain()->setCacheMode( 'public' );
00060
00061 $srchres = PrefixSearch::titleSearch( $search, $limit,
00062 $namespaces );
00063 }
00064
00065 $result = $this->getResult();
00066 $result->addValue( null, 0, $search );
00067 $result->addValue( null, 1, $srchres );
00068 }
00069
00070 public function getAllowedParams() {
00071 return array (
00072 'search' => null,
00073 'limit' => array(
00074 ApiBase :: PARAM_DFLT => 10,
00075 ApiBase :: PARAM_TYPE => 'limit',
00076 ApiBase :: PARAM_MIN => 1,
00077 ApiBase :: PARAM_MAX => 100,
00078 ApiBase :: PARAM_MAX2 => 100
00079 ),
00080 'namespace' => array(
00081 ApiBase :: PARAM_DFLT => NS_MAIN,
00082 ApiBase :: PARAM_TYPE => 'namespace',
00083 ApiBase :: PARAM_ISMULTI => true
00084 ),
00085 'suggest' => false,
00086 );
00087 }
00088
00089 public function getParamDescription() {
00090 return array (
00091 'search' => 'Search string',
00092 'limit' => 'Maximum amount of results to return',
00093 'namespace' => 'Namespaces to search',
00094 'suggest' => 'Do nothing if $wgEnableOpenSearchSuggest is false',
00095 );
00096 }
00097
00098 public function getDescription() {
00099 return 'This module implements OpenSearch protocol';
00100 }
00101
00102 protected function getExamples() {
00103 return array (
00104 'api.php?action=opensearch&search=Te'
00105 );
00106 }
00107
00108 public function getVersion() {
00109 return __CLASS__ . ': $Id: ApiOpenSearch.php 69932 2010-07-26 08:03:21Z tstarling $';
00110 }
00111 }