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
00052 $params = $this->extractRequestParams();
00053
00054 $limit = $params['limit'];
00055 $query = $params['search'];
00056 $what = $params['what'];
00057 if (strval($query) === '')
00058 $this->dieUsage("empty search string is not allowed", 'param-search');
00059
00060 $search = SearchEngine::create();
00061 $search->setLimitOffset( $limit+1, $params['offset'] );
00062 $search->setNamespaces( $params['namespace'] );
00063 $search->showRedirects = $params['redirects'];
00064
00065 if ($what == 'text') {
00066 $matches = $search->searchText( $query );
00067 } elseif( $what == 'title' ) {
00068 $matches = $search->searchTitle( $query );
00069 } else {
00070
00071
00072
00073
00074 $what = 'title';
00075 $matches = $search->searchTitle( $query );
00076
00077
00078
00079
00080
00081 if( is_null( $matches ) ) {
00082 $what = 'text';
00083 $matches = $search->searchText( $query );
00084 }
00085 }
00086 if (is_null($matches))
00087 $this->dieUsage("{$what} search is disabled",
00088 "search-{$what}-disabled");
00089
00090 $titles = array ();
00091 $count = 0;
00092 while( $result = $matches->next() ) {
00093 if (++ $count > $limit) {
00094
00095 $this->setContinueEnumParameter('offset', $params['offset'] + $params['limit']);
00096 break;
00097 }
00098
00099
00100 if ($result->isBrokenTitle() || $result->isMissingRevision())
00101 continue;
00102
00103 $title = $result->getTitle();
00104 if (is_null($resultPageSet)) {
00105 $vals = array();
00106 ApiQueryBase::addTitleInfo($vals, $title);
00107 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
00108 if(!$fit)
00109 {
00110 $this->setContinueEnumParameter('offset', $params['offset'] + $count - 1);
00111 break;
00112 }
00113 } else {
00114 $titles[] = $title;
00115 }
00116 }
00117
00118 if (is_null($resultPageSet)) {
00119 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'p');
00120 } else {
00121 $resultPageSet->populateFromTitles($titles);
00122 }
00123 }
00124
00125 public function getCacheMode( $params ) {
00126 return 'public';
00127 }
00128
00129 public function getAllowedParams() {
00130 return array (
00131 'search' => null,
00132 'namespace' => array (
00133 ApiBase :: PARAM_DFLT => 0,
00134 ApiBase :: PARAM_TYPE => 'namespace',
00135 ApiBase :: PARAM_ISMULTI => true,
00136 ),
00137 'what' => array (
00138 ApiBase :: PARAM_DFLT => null,
00139 ApiBase :: PARAM_TYPE => array (
00140 'title',
00141 'text',
00142 )
00143 ),
00144 'redirects' => false,
00145 'offset' => 0,
00146 'limit' => array (
00147 ApiBase :: PARAM_DFLT => 10,
00148 ApiBase :: PARAM_TYPE => 'limit',
00149 ApiBase :: PARAM_MIN => 1,
00150 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00151 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00152 )
00153 );
00154 }
00155
00156 public function getParamDescription() {
00157 return array (
00158 'search' => 'Search for all page titles (or content) that has this value.',
00159 'namespace' => 'The namespace(s) to enumerate.',
00160 'what' => 'Search inside the text or titles.',
00161 'redirects' => 'Include redirect pages in the search.',
00162 'offset' => 'Use this value to continue paging (return by query)',
00163 'limit' => 'How many total pages to return.'
00164 );
00165 }
00166
00167 public function getDescription() {
00168 return 'Perform a full text search';
00169 }
00170
00171 protected function getExamples() {
00172 return array (
00173 'api.php?action=query&list=search&srsearch=meaning',
00174 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
00175 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
00176 );
00177 }
00178
00179 public function getVersion() {
00180 return __CLASS__ . ': $Id: ApiQuerySearch.php 69986 2010-07-27 03:57:39Z tstarling $';
00181 }
00182 }