Change the SearchEngine interface around:
[lhc/web/wiklou.git] / includes / SearchEngine.php
1 <?php
2 /**
3 * Contain a class for special pages
4 * @package MediaWiki
5 * @subpackage Search
6 */
7
8 /**
9 * @package MediaWiki
10 */
11 class SearchEngine {
12 var $limit = 10;
13 var $offset = 0;
14 var $searchTerms = array();
15 var $namespaces = array( 0 );
16 var $showRedirects = false;
17
18 /**
19 * Perform a full text search query and return a result set.
20 * If title searches are not supported or disabled, return null.
21 *
22 * @param string $term - Raw search term
23 * @return SearchResultSet
24 * @access public
25 * @abstract
26 */
27 function searchText( $term ) {
28 return null;
29 }
30
31 /**
32 * Perform a title-only search query and return a result set.
33 * If title searches are not supported or disabled, return null.
34 *
35 * @param string $term - Raw search term
36 * @return SearchResultSet
37 * @access public
38 * @abstract
39 */
40 function searchTitle( $term ) {
41 return null;
42 }
43
44 /**
45 * If an exact title match can be find, or a very slightly close match,
46 * return the title. If no match, returns NULL.
47 *
48 * @static
49 * @param string $term
50 * @return Title
51 * @access private
52 */
53 function getNearMatch( $term ) {
54 # Exact match? No need to look further.
55 $title = Title::newFromText( $term );
56 if ( $title->getNamespace() == NS_SPECIAL || $title->exists() ) {
57 return $title;
58 }
59
60 # Now try all lower case (i.e. first letter capitalized)
61 #
62 $title = Title::newFromText( strtolower( $term ) );
63 if ( $title->exists() ) {
64 return $title;
65 }
66
67 # Now try capitalized string
68 #
69 $title = Title::newFromText( ucwords( strtolower( $term ) ) );
70 if ( $title->exists() ) {
71 return $title;
72 }
73
74 # Now try all upper case
75 #
76 $title = Title::newFromText( strtoupper( $term ) );
77 if ( $title->exists() ) {
78 return $title;
79 }
80
81 $title = Title::newFromText( $term );
82
83 # Entering an IP address goes to the contributions page
84 if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
85 || User::isIP( trim( $term ) ) ) {
86 return Title::makeTitle( NS_SPECIAL, "Contributions/" . $title->getDbkey() );
87 }
88
89
90 # Entering a user goes to the user page whether it's there or not
91 if ( $title->getNamespace() == NS_USER ) {
92 return $title;
93 }
94
95 # Quoted term? Try without the quotes...
96 if( preg_match( '/^"([^"]+)"$/', $term, $matches ) ) {
97 return SearchEngine::getNearMatch( $matches[1] );
98 }
99
100 return NULL;
101 }
102
103 function legalSearchChars() {
104 return "A-Za-z_'0-9\\x80-\\xFF\\-";
105 }
106
107 /**
108 * Set the maximum number of results to return
109 * and how many to skip before returning the first.
110 *
111 * @param int $limit
112 * @param int $offset
113 * @access public
114 */
115 function setLimitOffset( $limit, $offset = 0 ) {
116 $this->limit = IntVal( $limit );
117 $this->offset = IntVal( $offset );
118 }
119
120 /**
121 * Set which namespaces the search should include.
122 * Give an array of namespace index numbers.
123 *
124 * @param array $namespaces
125 * @access public
126 */
127 function setNamespaces( $namespaces ) {
128 $this->namespaces = $namespaces;
129 }
130
131 /**
132 * Make a list of searchable namespaces and their canonical names.
133 * @return array
134 * @access public
135 */
136 function searchableNamespaces() {
137 global $wgContLang;
138 $arr = array();
139 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
140 if( $ns >= NS_MAIN ) {
141 $arr[$ns] = $name;
142 }
143 }
144 return $arr;
145 }
146
147 /**
148 * Return a 'cleaned up' search string
149 *
150 * @return string
151 * @access public
152 */
153 function filter( $text ) {
154 $lc = $this->legalSearchChars();
155 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
156 }
157 /**
158 * Load up the appropriate search engine class for the currently
159 * active database backend, and return a configured instance.
160 *
161 * @return SearchEngine
162 * @access private
163 */
164 function create() {
165 global $wgDBtype, $wgDBmysql4, $wgSearchType;
166 if( $wgSearchType ) {
167 $class = $wgSearchType;
168 } elseif( $wgDBtype == 'mysql' ) {
169 if( $wgDBmysql4 ) {
170 $class = 'SearchMySQL4';
171 require_once( 'SearchMySQL4.php' );
172 } else {
173 $class = 'SearchMysql3';
174 require_once( 'SearchMySQL3.php' );
175 }
176 } else if ( $wgDBtype == 'PostgreSQL' ) {
177 $class = 'SearchTsearch2';
178 require_once( 'SearchTsearch2.php' );
179 } else {
180 $class = 'SearchEngineDummy';
181 }
182 $search = new $class( wfGetDB( DB_SLAVE ) );
183 $search->setLimitOffset(0,0);
184 return $search;
185 }
186
187 /**
188 * Create or update the search index record for the given page.
189 * Title and text should be pre-processed.
190 *
191 * @param int $id
192 * @param string $title
193 * @param string $text
194 * @abstract
195 */
196 function update( $id, $title, $text ) {
197 // no-op
198 }
199
200 /**
201 * Update a search index record's title only.
202 * Title should be pre-processed.
203 *
204 * @param int $id
205 * @param string $title
206 * @abstract
207 */
208 function updateTitle( $id, $title ) {
209 // no-op
210 }
211 }
212
213 class SearchResultSet {
214 /**
215 * Fetch an array of regular expression fragments for matching
216 * the search terms as parsed by this engine in a text extract.
217 *
218 * @return array
219 * @access public
220 * @abstract
221 */
222 function termMatches() {
223 return array();
224 }
225
226 function numRows() {
227 return 0;
228 }
229
230 /**
231 * Return true if results are included in this result set.
232 * @return bool
233 * @abstract
234 */
235 function hasResults() {
236 return false;
237 }
238
239 /**
240 * Some search modes return a total hit count for the query
241 * in the entire article database. This may include pages
242 * in namespaces that would not be matched on the given
243 * settings.
244 *
245 * Return null if no total hits number is supported.
246 *
247 * @return int
248 * @access public
249 */
250 function getTotalHits() {
251 return null;
252 }
253
254 /**
255 * Some search modes return a suggested alternate term if there are
256 * no exact hits. Returns true if there is one on this set.
257 *
258 * @return bool
259 * @access public
260 */
261 function hasSuggestion() {
262 return false;
263 }
264
265 /**
266 * Some search modes return a suggested alternate term if there are
267 * no exact hits. Check hasSuggestion() first.
268 *
269 * @return string
270 * @access public
271 */
272 function getSuggestion() {
273 return '';
274 }
275
276 /**
277 * Fetches next search result, or false.
278 * @return SearchResult
279 * @access public
280 * @abstract
281 */
282 function next() {
283 return false;
284 }
285 }
286
287 class SearchResult {
288 function SearchResult( $row ) {
289 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
290 }
291
292 /**
293 * @return Title
294 * @access public
295 */
296 function getTitle() {
297 return $this->mTitle;
298 }
299
300 /**
301 * @return double or null if not supported
302 */
303 function getScore() {
304 return null;
305 }
306 }
307
308 /**
309 * @package MediaWiki
310 */
311 class SearchEngineDummy {
312 function search( $term ) {
313 return null;
314 }
315 }
316