(bug 5378) General logs link in Special:Contributions
[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 * @access 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 * @access private
191 */
192 function create() {
193 global $wgDBtype, $wgSearchType;
194 if( $wgSearchType ) {
195 $class = $wgSearchType;
196 } elseif( $wgDBtype == 'mysql' ) {
197 $class = 'SearchMySQL4';
198 require_once( 'SearchMySQL4.php' );
199 } else if ( $wgDBtype == 'PostgreSQL' ) {
200 $class = 'SearchTsearch2';
201 require_once( 'SearchTsearch2.php' );
202 } else {
203 $class = 'SearchEngineDummy';
204 }
205 $search = new $class( wfGetDB( DB_SLAVE ) );
206 $search->setLimitOffset(0,0);
207 return $search;
208 }
209
210 /**
211 * Create or update the search index record for the given page.
212 * Title and text should be pre-processed.
213 *
214 * @param int $id
215 * @param string $title
216 * @param string $text
217 * @abstract
218 */
219 function update( $id, $title, $text ) {
220 // no-op
221 }
222
223 /**
224 * Update a search index record's title only.
225 * Title should be pre-processed.
226 *
227 * @param int $id
228 * @param string $title
229 * @abstract
230 */
231 function updateTitle( $id, $title ) {
232 // no-op
233 }
234 }
235
236 /** @package MediaWiki */
237 class SearchResultSet {
238 /**
239 * Fetch an array of regular expression fragments for matching
240 * the search terms as parsed by this engine in a text extract.
241 *
242 * @return array
243 * @access public
244 * @abstract
245 */
246 function termMatches() {
247 return array();
248 }
249
250 function numRows() {
251 return 0;
252 }
253
254 /**
255 * Return true if results are included in this result set.
256 * @return bool
257 * @abstract
258 */
259 function hasResults() {
260 return false;
261 }
262
263 /**
264 * Some search modes return a total hit count for the query
265 * in the entire article database. This may include pages
266 * in namespaces that would not be matched on the given
267 * settings.
268 *
269 * Return null if no total hits number is supported.
270 *
271 * @return int
272 * @access public
273 */
274 function getTotalHits() {
275 return null;
276 }
277
278 /**
279 * Some search modes return a suggested alternate term if there are
280 * no exact hits. Returns true if there is one on this set.
281 *
282 * @return bool
283 * @access public
284 */
285 function hasSuggestion() {
286 return false;
287 }
288
289 /**
290 * Some search modes return a suggested alternate term if there are
291 * no exact hits. Check hasSuggestion() first.
292 *
293 * @return string
294 * @access public
295 */
296 function getSuggestion() {
297 return '';
298 }
299
300 /**
301 * Fetches next search result, or false.
302 * @return SearchResult
303 * @access public
304 * @abstract
305 */
306 function next() {
307 return false;
308 }
309 }
310
311 /** @package MediaWiki */
312 class SearchResult {
313 function SearchResult( $row ) {
314 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
315 }
316
317 /**
318 * @return Title
319 * @access public
320 */
321 function getTitle() {
322 return $this->mTitle;
323 }
324
325 /**
326 * @return double or null if not supported
327 */
328 function getScore() {
329 return null;
330 }
331 }
332
333 /**
334 * @package MediaWiki
335 */
336 class SearchEngineDummy {
337 function search( $term ) {
338 return null;
339 }
340 function setLimitOffset($l, $o) {}
341 function legalSearchChars() {}
342 function update() {}
343 function setnamespaces() {}
344 function searchtitle() {}
345 function searchtext() {}
346 }
347 ?>