]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/api/ApiQuerySearch.php
MediaWiki 1.15.5
[autoinstallsdev/mediawiki.git] / includes / api / ApiQuerySearch.php
index 268616b1643466a6cf8266cdac23868f239377c7..a8f3fcc8fe99c592faa2ca1a7c5101a4f70851eb 100644 (file)
@@ -30,8 +30,8 @@ if (!defined('MEDIAWIKI')) {
 
 /**
  * Query module to perform full text search within wiki titles and content
- * 
- * @addtogroup API
+ *
+ * @ingroup API
  */
 class ApiQuerySearch extends ApiQueryGeneratorBase {
 
@@ -52,21 +52,42 @@ 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) {
@@ -75,35 +96,46 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                                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',
@@ -121,7 +153,7 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                );
        }
 
-       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.',
@@ -132,7 +164,7 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                );
        }
 
-       protected function getDescription() {
+       public function getDescription() {
                return 'Perform a full text search';
        }
 
@@ -145,7 +177,6 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
        }
 
        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