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 getAllowedParams() {
00126 return array (
00127 'search' => null,
00128 'namespace' => array (
00129 ApiBase :: PARAM_DFLT => 0,
00130 ApiBase :: PARAM_TYPE => 'namespace',
00131 ApiBase :: PARAM_ISMULTI => true,
00132 ),
00133 'what' => array (
00134 ApiBase :: PARAM_DFLT => null,
00135 ApiBase :: PARAM_TYPE => array (
00136 'title',
00137 'text',
00138 )
00139 ),
00140 'redirects' => false,
00141 'offset' => 0,
00142 'limit' => array (
00143 ApiBase :: PARAM_DFLT => 10,
00144 ApiBase :: PARAM_TYPE => 'limit',
00145 ApiBase :: PARAM_MIN => 1,
00146 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00147 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00148 )
00149 );
00150 }
00151
00152 public function getParamDescription() {
00153 return array (
00154 'search' => 'Search for all page titles (or content) that has this value.',
00155 'namespace' => 'The namespace(s) to enumerate.',
00156 'what' => 'Search inside the text or titles.',
00157 'redirects' => 'Include redirect pages in the search.',
00158 'offset' => 'Use this value to continue paging (return by query)',
00159 'limit' => 'How many total pages to return.'
00160 );
00161 }
00162
00163 public function getDescription() {
00164 return 'Perform a full text search';
00165 }
00166
00167 protected function getExamples() {
00168 return array (
00169 'api.php?action=query&list=search&srsearch=meaning',
00170 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
00171 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
00172 );
00173 }
00174
00175 public function getVersion() {
00176 return __CLASS__ . ': $Id: ApiQuerySearch.php 47865 2009-02-27 16:03:01Z catrope $';
00177 }
00178 }