cec40c91660ea7f832ce8e6298169bdce1fa7500
[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 $matches = array();
130 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
131 return SearchEngine::getNearMatch( $matches[1] );
132 }
133
134 return NULL;
135 }
136
137 function legalSearchChars() {
138 return "A-Za-z_'0-9\\x80-\\xFF\\-";
139 }
140
141 /**
142 * Set the maximum number of results to return
143 * and how many to skip before returning the first.
144 *
145 * @param int $limit
146 * @param int $offset
147 * @access public
148 */
149 function setLimitOffset( $limit, $offset = 0 ) {
150 $this->limit = intval( $limit );
151 $this->offset = intval( $offset );
152 }
153
154 /**
155 * Set which namespaces the search should include.
156 * Give an array of namespace index numbers.
157 *
158 * @param array $namespaces
159 * @access public
160 */
161 function setNamespaces( $namespaces ) {
162 $this->namespaces = $namespaces;
163 }
164
165 /**
166 * Make a list of searchable namespaces and their canonical names.
167 * @return array
168 * @access public
169 */
170 function searchableNamespaces() {
171 global $wgContLang;
172 $arr = array();
173 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
174 if( $ns >= NS_MAIN ) {
175 $arr[$ns] = $name;
176 }
177 }
178 return $arr;
179 }
180
181 /**
182 * Return a 'cleaned up' search string
183 *
184 * @return string
185 * @access public
186 */
187 function filter( $text ) {
188 $lc = $this->legalSearchChars();
189 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
190 }
191 /**
192 * Load up the appropriate search engine class for the currently
193 * active database backend, and return a configured instance.
194 *
195 * @return SearchEngine
196 * @private
197 */
198 function create() {
199 global $wgDBtype, $wgSearchType;
200 if( $wgSearchType ) {
201 $class = $wgSearchType;
202 } elseif( $wgDBtype == 'mysql' ) {
203 $class = 'SearchMySQL4';
204 } else if ( $wgDBtype == 'postgres' ) {
205 $class = 'SearchPostgres';
206 } else {
207 $class = 'SearchEngineDummy';
208 }
209 $search = new $class( wfGetDB( DB_SLAVE ) );
210 $search->setLimitOffset(0,0);
211 return $search;
212 }
213
214 /**
215 * Create or update the search index record for the given page.
216 * Title and text should be pre-processed.
217 *
218 * @param int $id
219 * @param string $title
220 * @param string $text
221 * @abstract
222 */
223 function update( $id, $title, $text ) {
224 // no-op
225 }
226
227 /**
228 * Update a search index record's title only.
229 * Title should be pre-processed.
230 *
231 * @param int $id
232 * @param string $title
233 * @abstract
234 */
235 function updateTitle( $id, $title ) {
236 // no-op
237 }
238 }
239
240 /** @package MediaWiki */
241 class SearchResultSet {
242 /**
243 * Fetch an array of regular expression fragments for matching
244 * the search terms as parsed by this engine in a text extract.
245 *
246 * @return array
247 * @access public
248 * @abstract
249 */
250 function termMatches() {
251 return array();
252 }
253
254 function numRows() {
255 return 0;
256 }
257
258 /**
259 * Return true if results are included in this result set.
260 * @return bool
261 * @abstract
262 */
263 function hasResults() {
264 return false;
265 }
266
267 /**
268 * Some search modes return a total hit count for the query
269 * in the entire article database. This may include pages
270 * in namespaces that would not be matched on the given
271 * settings.
272 *
273 * Return null if no total hits number is supported.
274 *
275 * @return int
276 * @access public
277 */
278 function getTotalHits() {
279 return null;
280 }
281
282 /**
283 * Some search modes return a suggested alternate term if there are
284 * no exact hits. Returns true if there is one on this set.
285 *
286 * @return bool
287 * @access public
288 */
289 function hasSuggestion() {
290 return false;
291 }
292
293 /**
294 * Some search modes return a suggested alternate term if there are
295 * no exact hits. Check hasSuggestion() first.
296 *
297 * @return string
298 * @access public
299 */
300 function getSuggestion() {
301 return '';
302 }
303
304 /**
305 * Fetches next search result, or false.
306 * @return SearchResult
307 * @access public
308 * @abstract
309 */
310 function next() {
311 return false;
312 }
313 }
314
315 /** @package MediaWiki */
316 class SearchResult {
317 function SearchResult( $row ) {
318 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
319 }
320
321 /**
322 * @return Title
323 * @access public
324 */
325 function getTitle() {
326 return $this->mTitle;
327 }
328
329 /**
330 * @return double or null if not supported
331 */
332 function getScore() {
333 return null;
334 }
335 }
336
337 /**
338 * @package MediaWiki
339 */
340 class SearchEngineDummy {
341 function search( $term ) {
342 return null;
343 }
344 function setLimitOffset($l, $o) {}
345 function legalSearchChars() {}
346 function update() {}
347 function setnamespaces() {}
348 function searchtitle() {}
349 function searchtext() {}
350 }
351 ?>