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