<?php
/**
- * API for MediaWiki 1.8+
+ *
*
* Created on July 30, 2007
*
* @file
*/
-if ( !defined( 'MEDIAWIKI' ) ) {
- // Eclipse helper - will be ignored in production
- require_once( 'ApiQueryBase.php' );
-}
-
/**
* Query module to perform full text search within wiki titles and content
*
$this->run( $resultPageSet );
}
+ /**
+ * @param $resultPageSet ApiPageSet
+ * @return void
+ */
private function run( $resultPageSet = null ) {
global $wgContLang;
$params = $this->extractRequestParams();
$search->setNamespaces( $params['namespace'] );
$search->showRedirects = $params['redirects'];
+ $query = $search->transformSearchTerm( $query );
+ $query = $search->replacePrefixes( $query );
+
// Perform the actual search
if ( $what == 'text' ) {
$matches = $search->searchText( $query );
$this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
}
+ $apiResult = $this->getResult();
// Add search meta data to result
if ( isset( $searchInfo['totalhits'] ) ) {
$totalhits = $matches->getTotalHits();
if ( $totalhits !== null ) {
- $this->getResult()->addValue( array( 'query', 'searchinfo' ),
+ $apiResult->addValue( array( 'query', 'searchinfo' ),
'totalhits', $totalhits );
}
}
if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
- $this->getResult()->addValue( array( 'query', 'searchinfo' ),
+ $apiResult->addValue( array( 'query', 'searchinfo' ),
'suggestion', $matches->getSuggestionQuery() );
}
$terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
$titles = array();
$count = 0;
- while ( $result = $matches->next() ) {
+ $result = $matches->next();
+
+ while ( $result ) {
if ( ++ $count > $limit ) {
// We've reached the one extra which shows that there are additional items to be had. Stop here...
$this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
// Silently skip broken and missing titles
if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
+ $result = $matches->next();
continue;
}
if ( isset( $prop['timestamp'] ) ) {
$vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
}
+ if ( !is_null( $result->getScore() ) && isset( $prop['score'] ) ) {
+ $vals['score'] = $result->getScore();
+ }
+ if ( isset( $prop['titlesnippet'] ) ) {
+ $vals['titlesnippet'] = $result->getTitleSnippet( $terms );
+ }
+ if ( !is_null( $result->getRedirectTitle() ) ) {
+ if ( isset( $prop['redirecttitle'] ) ) {
+ $vals['redirecttitle'] = $result->getRedirectTitle();
+ }
+ if ( isset( $prop['redirectsnippet'] ) ) {
+ $vals['redirectsnippet'] = $result->getRedirectSnippet( $terms );
+ }
+ }
+ if ( !is_null( $result->getSectionTitle() ) ) {
+ if ( isset( $prop['sectiontitle'] ) ) {
+ $vals['sectiontitle'] = $result->getSectionTitle()->getFragment();
+ }
+ if ( isset( $prop['sectionsnippet'] ) ) {
+ $vals['sectionsnippet'] = $result->getSectionSnippet();
+ }
+ }
+ if ( isset( $prop['hasrelated'] ) && $result->hasRelated() ) {
+ $vals['hasrelated'] = "";
+ }
// Add item to results and see whether it fits
- $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
+ $fit = $apiResult->addValue( array( 'query', $this->getModuleName() ),
null, $vals );
if ( !$fit ) {
$this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
} else {
$titles[] = $title;
}
+
+ $result = $matches->next();
}
if ( is_null( $resultPageSet ) ) {
- $this->getResult()->setIndexedTagName_internal( array(
+ $apiResult->setIndexedTagName_internal( array(
'query', $this->getModuleName()
), 'p' );
} else {
'size',
'wordcount',
'timestamp',
+ 'score',
'snippet',
+ 'titlesnippet',
+ 'redirecttitle',
+ 'redirectsnippet',
+ 'sectiontitle',
+ 'sectionsnippet',
+ 'hasrelated',
),
ApiBase::PARAM_ISMULTI => true,
),
'info' => 'What metadata to return',
'prop' => array(
'What properties to return',
- ' size - Adds the size of the page in bytes',
- ' wordcount - Adds the word count of the page',
- ' timestamp - Adds the timestamp of when the page was last edited',
- ' snippet - Adds a parsed snippet of the page',
+ ' size - Adds the size of the page in bytes',
+ ' wordcount - Adds the word count of the page',
+ ' timestamp - Adds the timestamp of when the page was last edited',
+ ' score - Adds the score (if any) from the search engine',
+ ' snippet - Adds a parsed snippet of the page',
+ ' titlesnippet - Adds a parsed snippet of the page title',
+ ' redirectsnippet - Adds a parsed snippet of the redirect title',
+ ' redirecttitle - Adds the title of the matching redirect',
+ ' sectionsnippet - Adds a parsed snippet of the matching section title',
+ ' sectiontitle - Adds the title of the matching section',
+ ' hasrelated - Indicates whether a related search is available',
),
'redirects' => 'Include redirect pages in the search',
'offset' => 'Use this value to continue paging (return by query)',
) );
}
- protected function getExamples() {
+ public function getExamples() {
return array(
'api.php?action=query&list=search&srsearch=meaning',
'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
);
}
+ public function getHelpUrls() {
+ return 'http://www.mediawiki.org/wiki/API:Search';
+ }
+
public function getVersion() {
return __CLASS__ . ': $Id$';
}