932ca5055d08c613ba1be80b6bda1ab51927f6fc
[lhc/web/wiklou.git] / includes / api / ApiQuerySearch.php
1 <?php
2
3 /*
4 * Created on July 30, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * Query module to perform full text search within wiki titles and content
33 *
34 * @ingroup API
35 */
36 class ApiQuerySearch extends ApiQueryGeneratorBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'sr');
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator($resultPageSet) {
47 $this->run($resultPageSet);
48 }
49
50 private function run($resultPageSet = null) {
51
52 $params = $this->extractRequestParams();
53
54 $limit = $params['limit'];
55 $query = $params['search'];
56 $what = $params['what'];
57 if (is_null($query) || empty($query))
58 $this->dieUsage("empty search string is not allowed", 'param-search');
59
60 $search = SearchEngine::create();
61 $search->setLimitOffset( $limit+1, $params['offset'] );
62 $search->setNamespaces( $params['namespace'] );
63 $search->showRedirects = $params['redirects'];
64
65 if ($what == 'text') {
66 $matches = $search->searchText( $query );
67 } elseif( $what == 'title' ) {
68 $matches = $search->searchTitle( $query );
69 } else {
70 // We default to title searches; this is a terrible legacy
71 // of the way we initially set up the MySQL fulltext-based
72 // search engine with separate title and text fields.
73 // In the future, the default should be for a combined index.
74 $matches = $search->searchTitle( $query );
75
76 // Not all search engines support a separate title search,
77 // for instance the Lucene-based engine we use on Wikipedia.
78 // In this case, fall back to full-text search (which will
79 // include titles in it!)
80 if( is_null( $matches ) )
81 $matches = $search->searchText( $query );
82 }
83 if (is_null($matches))
84 $this->dieUsage("{$what} search is disabled",
85 "search-{$what}-disabled");
86
87 $data = array ();
88 $count = 0;
89 while( $result = $matches->next() ) {
90 if (++ $count > $limit) {
91 // We've reached the one extra which shows that there are additional items to be had. Stop here...
92 $this->setContinueEnumParameter('offset', $params['offset'] + $params['limit']);
93 break;
94 }
95
96 // Silently skip broken titles
97 if ($result->isBrokenTitle()) continue;
98
99 $title = $result->getTitle();
100 if (is_null($resultPageSet)) {
101 $data[] = array(
102 'ns' => intval($title->getNamespace()),
103 'title' => $title->getPrefixedText());
104 } else {
105 $data[] = $title;
106 }
107 }
108
109 if (is_null($resultPageSet)) {
110 $result = $this->getResult();
111 $result->setIndexedTagName($data, 'p');
112 $result->addValue('query', $this->getModuleName(), $data);
113 } else {
114 $resultPageSet->populateFromTitles($data);
115 }
116 }
117
118 public function getAllowedParams() {
119 return array (
120 'search' => null,
121 'namespace' => array (
122 ApiBase :: PARAM_DFLT => 0,
123 ApiBase :: PARAM_TYPE => 'namespace',
124 ApiBase :: PARAM_ISMULTI => true,
125 ),
126 'what' => array (
127 ApiBase :: PARAM_DFLT => null,
128 ApiBase :: PARAM_TYPE => array (
129 'title',
130 'text',
131 )
132 ),
133 'redirects' => false,
134 'offset' => 0,
135 'limit' => array (
136 ApiBase :: PARAM_DFLT => 10,
137 ApiBase :: PARAM_TYPE => 'limit',
138 ApiBase :: PARAM_MIN => 1,
139 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
140 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
141 )
142 );
143 }
144
145 public function getParamDescription() {
146 return array (
147 'search' => 'Search for all page titles (or content) that has this value.',
148 'namespace' => 'The namespace(s) to enumerate.',
149 'what' => 'Search inside the text or titles.',
150 'redirects' => 'Include redirect pages in the search.',
151 'offset' => 'Use this value to continue paging (return by query)',
152 'limit' => 'How many total pages to return.'
153 );
154 }
155
156 public function getDescription() {
157 return 'Perform a full text search';
158 }
159
160 protected function getExamples() {
161 return array (
162 'api.php?action=query&list=search&srsearch=meaning',
163 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
164 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
165 );
166 }
167
168 public function getVersion() {
169 return __CLASS__ . ': $Id$';
170 }
171 }