/**
* Query module to perform full text search within wiki titles and content
- *
- * @addtogroup API
+ *
+ * @ingroup API
*/
class ApiQuerySearch extends ApiQueryGeneratorBase {
$params = $this->extractRequestParams();
$limit = $params['limit'];
- $query = $params['search'];
- if (is_null($query) || empty($query))
+ $query = $params['search'];
+ $what = $params['what'];
+ if (strval($query) === '')
$this->dieUsage("empty search string is not allowed", 'param-search');
$search = SearchEngine::create();
$search->setLimitOffset( $limit+1, $params['offset'] );
$search->setNamespaces( $params['namespace'] );
$search->showRedirects = $params['redirects'];
-
- if ($params['what'] == 'text')
+
+ if ($what == 'text') {
$matches = $search->searchText( $query );
- else
+ } elseif( $what == 'title' ) {
$matches = $search->searchTitle( $query );
+ } else {
+ // We default to title searches; this is a terrible legacy
+ // of the way we initially set up the MySQL fulltext-based
+ // search engine with separate title and text fields.
+ // In the future, the default should be for a combined index.
+ $what = 'title';
+ $matches = $search->searchTitle( $query );
+
+ // Not all search engines support a separate title search,
+ // for instance the Lucene-based engine we use on Wikipedia.
+ // In this case, fall back to full-text search (which will
+ // include titles in it!)
+ if( is_null( $matches ) ) {
+ $what = 'text';
+ $matches = $search->searchText( $query );
+ }
+ }
+ if (is_null($matches))
+ $this->dieUsage("{$what} search is disabled",
+ "search-{$what}-disabled");
- $data = array ();
+ $titles = array ();
$count = 0;
while( $result = $matches->next() ) {
if (++ $count > $limit) {
break;
}
+ // Silently skip broken and missing titles
+ if ($result->isBrokenTitle() || $result->isMissingRevision())
+ continue;
+
$title = $result->getTitle();
if (is_null($resultPageSet)) {
- $data[] = array(
- 'ns' => intval($title->getNamespace()),
- 'title' => $title->getPrefixedText());
+ $vals = array();
+ ApiQueryBase::addTitleInfo($vals, $title);
+ $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
+ if(!$fit)
+ {
+ $this->setContinueEnumParameter('offset', $params['offset'] + $count - 1);
+ break;
+ }
} else {
- $data[] = $title;
+ $titles[] = $title;
}
}
if (is_null($resultPageSet)) {
- $result = $this->getResult();
- $result->setIndexedTagName($data, 'p');
- $result->addValue('query', $this->getModuleName(), $data);
+ $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'p');
} else {
- $resultPageSet->populateFromTitles($data);
+ $resultPageSet->populateFromTitles($titles);
}
}
- protected function getAllowedParams() {
+ public function getCacheMode( $params ) {
+ return 'public';
+ }
+
+ public function getAllowedParams() {
return array (
'search' => null,
'namespace' => array (
ApiBase :: PARAM_DFLT => 0,
ApiBase :: PARAM_TYPE => 'namespace',
- ApiBase :: PARAM_ISMULTI => true,
+ ApiBase :: PARAM_ISMULTI => true,
),
'what' => array (
- ApiBase :: PARAM_DFLT => 'title',
+ ApiBase :: PARAM_DFLT => null,
ApiBase :: PARAM_TYPE => array (
'title',
'text',
);
}
- protected function getParamDescription() {
+ public function getParamDescription() {
return array (
'search' => 'Search for all page titles (or content) that has this value.',
'namespace' => 'The namespace(s) to enumerate.',
);
}
- protected function getDescription() {
+ public function getDescription() {
return 'Perform a full text search';
}
}
public function getVersion() {
- return __CLASS__ . ': $Id: ApiQuerySearch.php 24453 2007-07-30 08:09:15Z yurik $';
+ return __CLASS__ . ': $Id: ApiQuerySearch.php 69986 2010-07-27 03:57:39Z tstarling $';
}
-}
-
+}
\ No newline at end of file