Revert revert of setSingleton(), unrelated to broken, accidentally committed code...
[lhc/web/wiklou.git] / includes / SearchEngine.php
1 <?php
2 /**
3 * Contain a class for special pages
4 * @addtogroup Search
5 */
6 class SearchEngine {
7 var $limit = 10;
8 var $offset = 0;
9 var $searchTerms = array();
10 var $namespaces = array( NS_MAIN );
11 var $showRedirects = false;
12
13 /**
14 * Perform a full text search query and return a result set.
15 * If title searches are not supported or disabled, return null.
16 *
17 * @param string $term - Raw search term
18 * @return SearchResultSet
19 * @access public
20 * @abstract
21 */
22 function searchText( $term ) {
23 return null;
24 }
25
26 /**
27 * Perform a title-only search query and return a result set.
28 * If title searches are not supported or disabled, return null.
29 *
30 * @param string $term - Raw search term
31 * @return SearchResultSet
32 * @access public
33 * @abstract
34 */
35 function searchTitle( $term ) {
36 return null;
37 }
38
39 /**
40 * If an exact title match can be find, or a very slightly close match,
41 * return the title. If no match, returns NULL.
42 *
43 * @param string $term
44 * @return Title
45 */
46 public static function getNearMatch( $searchterm ) {
47 global $wgContLang;
48
49 $allSearchTerms = array($searchterm);
50
51 if($wgContLang->hasVariants()){
52 $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
53 }
54
55 foreach($allSearchTerms as $term){
56
57 # Exact match? No need to look further.
58 $title = Title::newFromText( $term );
59 if (is_null($title))
60 return NULL;
61
62 if ( $title->getNamespace() == NS_SPECIAL || $title->exists() ) {
63 return $title;
64 }
65
66 # Now try all lower case (i.e. first letter capitalized)
67 #
68 $title = Title::newFromText( $wgContLang->lc( $term ) );
69 if ( $title->exists() ) {
70 return $title;
71 }
72
73 # Now try capitalized string
74 #
75 $title = Title::newFromText( $wgContLang->ucwords( $term ) );
76 if ( $title->exists() ) {
77 return $title;
78 }
79
80 # Now try all upper case
81 #
82 $title = Title::newFromText( $wgContLang->uc( $term ) );
83 if ( $title->exists() ) {
84 return $title;
85 }
86
87 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
88 $title = Title::newFromText( $wgContLang->ucwordbreaks($term) );
89 if ( $title->exists() ) {
90 return $title;
91 }
92
93 global $wgCapitalLinks, $wgContLang;
94 if( !$wgCapitalLinks ) {
95 // Catch differs-by-first-letter-case-only
96 $title = Title::newFromText( $wgContLang->ucfirst( $term ) );
97 if ( $title->exists() ) {
98 return $title;
99 }
100 $title = Title::newFromText( $wgContLang->lcfirst( $term ) );
101 if ( $title->exists() ) {
102 return $title;
103 }
104 }
105 }
106
107 $title = Title::newFromText( $searchterm );
108
109 # Entering an IP address goes to the contributions page
110 if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
111 || User::isIP( trim( $searchterm ) ) ) {
112 return SpecialPage::getTitleFor( 'Contributions', $title->getDbkey() );
113 }
114
115
116 # Entering a user goes to the user page whether it's there or not
117 if ( $title->getNamespace() == NS_USER ) {
118 return $title;
119 }
120
121 # Go to images that exist even if there's no local page.
122 # There may have been a funny upload, or it may be on a shared
123 # file repository such as Wikimedia Commons.
124 if( $title->getNamespace() == NS_IMAGE ) {
125 $image = wfFindFile( $title );
126 if( $image ) {
127 return $title;
128 }
129 }
130
131 # MediaWiki namespace? Page may be "implied" if not customized.
132 # Just return it, with caps forced as the message system likes it.
133 if( $title->getNamespace() == NS_MEDIAWIKI ) {
134 return Title::makeTitle( NS_MEDIAWIKI, $wgContLang->ucfirst( $title->getText() ) );
135 }
136
137 # Quoted term? Try without the quotes...
138 $matches = array();
139 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
140 return SearchEngine::getNearMatch( $matches[1] );
141 }
142
143 return NULL;
144 }
145
146 public static function legalSearchChars() {
147 return "A-Za-z_'0-9\\x80-\\xFF\\-";
148 }
149
150 /**
151 * Set the maximum number of results to return
152 * and how many to skip before returning the first.
153 *
154 * @param int $limit
155 * @param int $offset
156 * @access public
157 */
158 function setLimitOffset( $limit, $offset = 0 ) {
159 $this->limit = intval( $limit );
160 $this->offset = intval( $offset );
161 }
162
163 /**
164 * Set which namespaces the search should include.
165 * Give an array of namespace index numbers.
166 *
167 * @param array $namespaces
168 * @access public
169 */
170 function setNamespaces( $namespaces ) {
171 $this->namespaces = $namespaces;
172 }
173
174 /**
175 * Make a list of searchable namespaces and their canonical names.
176 * @return array
177 */
178 public static function searchableNamespaces() {
179 global $wgContLang;
180 $arr = array();
181 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
182 if( $ns >= NS_MAIN ) {
183 $arr[$ns] = $name;
184 }
185 }
186 return $arr;
187 }
188
189 /**
190 * Return a 'cleaned up' search string
191 *
192 * @return string
193 * @access public
194 */
195 function filter( $text ) {
196 $lc = $this->legalSearchChars();
197 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
198 }
199 /**
200 * Load up the appropriate search engine class for the currently
201 * active database backend, and return a configured instance.
202 *
203 * @return SearchEngine
204 */
205 public static function create() {
206 global $wgDBtype, $wgSearchType;
207 if( $wgSearchType ) {
208 $class = $wgSearchType;
209 } elseif( $wgDBtype == 'mysql' ) {
210 $class = 'SearchMySQL4';
211 } else if ( $wgDBtype == 'postgres' ) {
212 $class = 'SearchPostgres';
213 } else if ( $wgDBtype == 'oracle' ) {
214 $class = 'SearchOracle';
215 } else {
216 $class = 'SearchEngineDummy';
217 }
218 $search = new $class( wfGetDB( DB_SLAVE ) );
219 $search->setLimitOffset(0,0);
220 return $search;
221 }
222
223 /**
224 * Create or update the search index record for the given page.
225 * Title and text should be pre-processed.
226 *
227 * @param int $id
228 * @param string $title
229 * @param string $text
230 * @abstract
231 */
232 function update( $id, $title, $text ) {
233 // no-op
234 }
235
236 /**
237 * Update a search index record's title only.
238 * Title should be pre-processed.
239 *
240 * @param int $id
241 * @param string $title
242 * @abstract
243 */
244 function updateTitle( $id, $title ) {
245 // no-op
246 }
247 }
248
249
250 /**
251 * @addtogroup Search
252 */
253 class SearchResultSet {
254 /**
255 * Fetch an array of regular expression fragments for matching
256 * the search terms as parsed by this engine in a text extract.
257 *
258 * @return array
259 * @access public
260 * @abstract
261 */
262 function termMatches() {
263 return array();
264 }
265
266 function numRows() {
267 return 0;
268 }
269
270 /**
271 * Return true if results are included in this result set.
272 * @return bool
273 * @abstract
274 */
275 function hasResults() {
276 return false;
277 }
278
279 /**
280 * Some search modes return a total hit count for the query
281 * in the entire article database. This may include pages
282 * in namespaces that would not be matched on the given
283 * settings.
284 *
285 * Return null if no total hits number is supported.
286 *
287 * @return int
288 * @access public
289 */
290 function getTotalHits() {
291 return null;
292 }
293
294 /**
295 * Some search modes return a suggested alternate term if there are
296 * no exact hits. Returns true if there is one on this set.
297 *
298 * @return bool
299 * @access public
300 */
301 function hasSuggestion() {
302 return false;
303 }
304
305 /**
306 * Some search modes return a suggested alternate term if there are
307 * no exact hits. Check hasSuggestion() first.
308 *
309 * @return string
310 * @access public
311 */
312 function getSuggestion() {
313 return '';
314 }
315
316 /**
317 * Fetches next search result, or false.
318 * @return SearchResult
319 * @access public
320 * @abstract
321 */
322 function next() {
323 return false;
324 }
325
326 /**
327 * Frees the result set, if applicable.
328 * @ access public
329 */
330 function free() {
331 // ...
332 }
333 }
334
335
336 /**
337 * @addtogroup Search
338 */
339 class SearchResult {
340 function SearchResult( $row ) {
341 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
342 }
343
344 /**
345 * @return Title
346 * @access public
347 */
348 function getTitle() {
349 return $this->mTitle;
350 }
351
352 /**
353 * @return double or null if not supported
354 */
355 function getScore() {
356 return null;
357 }
358 }
359
360 /**
361 * @addtogroup Search
362 */
363 class SearchEngineDummy {
364 function search( $term ) {
365 return null;
366 }
367 function setLimitOffset($l, $o) {}
368 function legalSearchChars() {}
369 function update() {}
370 function setnamespaces() {}
371 function searchtitle() {}
372 function searchtext() {}
373 }
374