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