Fix for r99911: don't use getImageArea() to determine the area for comparison against...
[lhc/web/wiklou.git] / includes / api / ApiQuerySearch.php
index 86ba895..a7e0974 100644 (file)
@@ -1,9 +1,8 @@
 <?php
-
 /**
- * Created on July 30, 2007
  *
- * API for MediaWiki 1.8+
+ *
+ * Created on July 30, 2007
  *
  * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  *
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @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
  *
@@ -47,6 +43,10 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                $this->run( $resultPageSet );
        }
 
+       /**
+        * @param $resultPageSet ApiPageSet
+        * @return void
+        */
        private function run( $resultPageSet = null ) {
                global $wgContLang;
                $params = $this->extractRequestParams();
@@ -58,16 +58,15 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                $searchInfo = array_flip( $params['info'] );
                $prop = array_flip( $params['prop'] );
 
-               if ( strval( $query ) === '' ) {
-                       $this->dieUsage( 'empty search string is not allowed', 'param-search' );
-               }
-
                // Create search engine instance and set options
                $search = SearchEngine::create();
                $search->setLimitOffset( $limit + 1, $params['offset'] );
                $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 );
@@ -96,16 +95,17 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                        $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() );
                }
 
@@ -113,7 +113,9 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                $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'] );
@@ -122,6 +124,7 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
 
                        // Silently skip broken and missing titles
                        if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
+                               $result = $matches->next();
                                continue;
                        }
 
@@ -142,9 +145,34 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                                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 );
@@ -153,10 +181,12 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                        } else {
                                $titles[] = $title;
                        }
+
+                       $result = $matches->next();
                }
 
                if ( is_null( $resultPageSet ) ) {
-                       $this->getResult()->setIndexedTagName_internal( array(
+                       $apiResult->setIndexedTagName_internal( array(
                                                'query', $this->getModuleName()
                                        ), 'p' );
                } else {
@@ -164,9 +194,16 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                }
        }
 
+       public function getCacheMode( $params ) {
+               return 'public';
+       }
+
        public function getAllowedParams() {
                return array(
-                       'search' => null,
+                       'search' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
                        'namespace' => array(
                                ApiBase::PARAM_DFLT => 0,
                                ApiBase::PARAM_TYPE => 'namespace',
@@ -194,7 +231,14 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                                        'size',
                                        'wordcount',
                                        'timestamp',
+                                       'score',
                                        'snippet',
+                                       'titlesnippet',
+                                       'redirecttitle',
+                                       'redirectsnippet',
+                                       'sectiontitle',
+                                       'sectionsnippet',
+                                       'hasrelated',
                                ),
                                ApiBase::PARAM_ISMULTI => true,
                        ),
@@ -218,10 +262,17 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                        '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)',
@@ -235,13 +286,12 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
 
        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
-                       array( 'code' => 'param-search', 'info' => 'empty search string is not allowed' ),
                        array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
                        array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
                ) );
        }
 
-       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',
@@ -249,6 +299,10 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                );
        }
 
+       public function getHelpUrls() {
+               return 'http://www.mediawiki.org/wiki/API:Search';
+       }
+
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }