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