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 ( 'ApiQueryBase.php' );
00029 }
00030
00036 class ApiQuerySearch extends ApiQueryGeneratorBase {
00037
00038 public function __construct( $query, $moduleName ) {
00039 parent :: __construct( $query, $moduleName, 'sr' );
00040 }
00041
00042 public function execute() {
00043 $this->run();
00044 }
00045
00046 public function executeGenerator( $resultPageSet ) {
00047 $this->run( $resultPageSet );
00048 }
00049
00050 private function run( $resultPageSet = null ) {
00051 global $wgContLang;
00052 $params = $this->extractRequestParams();
00053
00054
00055 $limit = $params['limit'];
00056 $query = $params['search'];
00057 $what = $params['what'];
00058 $searchInfo = array_flip( $params['info'] );
00059 $prop = array_flip( $params['prop'] );
00060
00061 if ( strval( $query ) === '' )
00062 $this->dieUsage( "empty search string is not allowed", 'param-search' );
00063
00064
00065 $search = SearchEngine::create();
00066 $search->setLimitOffset( $limit + 1, $params['offset'] );
00067 $search->setNamespaces( $params['namespace'] );
00068 $search->showRedirects = $params['redirects'];
00069
00070
00071 if ( $what == 'text' ) {
00072 $matches = $search->searchText( $query );
00073 } elseif ( $what == 'title' ) {
00074 $matches = $search->searchTitle( $query );
00075 } else {
00076
00077
00078
00079
00080 $what = 'title';
00081 $matches = $search->searchTitle( $query );
00082
00083
00084
00085
00086
00087 if ( is_null( $matches ) ) {
00088 $what = 'text';
00089 $matches = $search->searchText( $query );
00090 }
00091 }
00092 if ( is_null( $matches ) )
00093 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
00094
00095
00096 if ( isset( $searchInfo['totalhits'] ) ) {
00097 $totalhits = $matches->getTotalHits();
00098 if ( $totalhits !== null ) {
00099 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
00100 'totalhits', $totalhits );
00101 }
00102 }
00103 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
00104 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
00105 'suggestion', $matches->getSuggestionQuery() );
00106 }
00107
00108
00109 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
00110 $titles = array ();
00111 $count = 0;
00112 while ( $result = $matches->next() ) {
00113 if ( ++ $count > $limit ) {
00114
00115 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
00116 break;
00117 }
00118
00119
00120 if ( $result->isBrokenTitle() || $result->isMissingRevision() )
00121 continue;
00122
00123 $title = $result->getTitle();
00124 if ( is_null( $resultPageSet ) ) {
00125 $vals = array();
00126 ApiQueryBase::addTitleInfo( $vals, $title );
00127
00128 if ( isset( $prop['snippet'] ) )
00129 $vals['snippet'] = $result->getTextSnippet( $terms );
00130 if ( isset( $prop['size'] ) )
00131 $vals['size'] = $result->getByteSize();
00132 if ( isset( $prop['wordcount'] ) )
00133 $vals['wordcount'] = $result->getWordCount();
00134 if ( isset( $prop['timestamp'] ) )
00135 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
00136
00137
00138 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
00139 null, $vals );
00140 if ( !$fit ) {
00141 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
00142 break;
00143 }
00144 } else {
00145 $titles[] = $title;
00146 }
00147 }
00148
00149 if ( is_null( $resultPageSet ) ) {
00150 $this->getResult()->setIndexedTagName_internal( array(
00151 'query', $this->getModuleName()
00152 ), 'p' );
00153 } else {
00154 $resultPageSet->populateFromTitles( $titles );
00155 }
00156 }
00157
00158 public function getCacheMode( $params ) {
00159 return 'public';
00160 }
00161
00162 public function getAllowedParams() {
00163 return array (
00164 'search' => null,
00165 'namespace' => array (
00166 ApiBase :: PARAM_DFLT => 0,
00167 ApiBase :: PARAM_TYPE => 'namespace',
00168 ApiBase :: PARAM_ISMULTI => true,
00169 ),
00170 'what' => array (
00171 ApiBase :: PARAM_DFLT => null,
00172 ApiBase :: PARAM_TYPE => array (
00173 'title',
00174 'text',
00175 )
00176 ),
00177 'info' => array(
00178 ApiBase :: PARAM_DFLT => 'totalhits|suggestion',
00179 ApiBase :: PARAM_TYPE => array (
00180 'totalhits',
00181 'suggestion',
00182 ),
00183 ApiBase :: PARAM_ISMULTI => true,
00184 ),
00185 'prop' => array(
00186 ApiBase :: PARAM_DFLT => 'size|wordcount|timestamp|snippet',
00187 ApiBase :: PARAM_TYPE => array (
00188 'size',
00189 'wordcount',
00190 'timestamp',
00191 'snippet',
00192 ),
00193 ApiBase :: PARAM_ISMULTI => true,
00194 ),
00195 'redirects' => false,
00196 'offset' => 0,
00197 'limit' => array (
00198 ApiBase :: PARAM_DFLT => 10,
00199 ApiBase :: PARAM_TYPE => 'limit',
00200 ApiBase :: PARAM_MIN => 1,
00201 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_SML1,
00202 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_SML2
00203 )
00204 );
00205 }
00206
00207 public function getParamDescription() {
00208 return array (
00209 'search' => 'Search for all page titles (or content) that has this value.',
00210 'namespace' => 'The namespace(s) to enumerate.',
00211 'what' => 'Search inside the text or titles.',
00212 'info' => 'What metadata to return.',
00213 'prop' => 'What properties to return.',
00214 'redirects' => 'Include redirect pages in the search.',
00215 'offset' => 'Use this value to continue paging (return by query)',
00216 'limit' => 'How many total pages to return.'
00217 );
00218 }
00219
00220 public function getDescription() {
00221 return 'Perform a full text search';
00222 }
00223
00224 public function getPossibleErrors() {
00225 return array_merge( parent::getPossibleErrors(), array(
00226 array( 'code' => 'param-search', 'info' => 'empty search string is not allowed' ),
00227 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
00228 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
00229 ) );
00230 }
00231
00232 protected function getExamples() {
00233 return array (
00234 'api.php?action=query&list=search&srsearch=meaning',
00235 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
00236 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
00237 );
00238 }
00239
00240 public function getVersion() {
00241 return __CLASS__ . ': $Id: ApiQuerySearch.php 69932 2010-07-26 08:03:21Z tstarling $';
00242 }
00243 }