Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / SearchEngine.php
1 <?php
2 /**
3 * Contain a class for special pages
4 * @addtogroup Search
5 */
6
7 /**
8 */
9 class SearchEngine {
10 var $limit = 10;
11 var $offset = 0;
12 var $searchTerms = array();
13 var $namespaces = array( NS_MAIN );
14 var $showRedirects = false;
15
16 /**
17 * Perform a full text search query and return a result set.
18 * If title searches are not supported or disabled, return null.
19 *
20 * @param string $term - Raw search term
21 * @return SearchResultSet
22 * @access public
23 * @abstract
24 */
25 function searchText( $term ) {
26 return null;
27 }
28
29 /**
30 * Perform a title-only search query and return a result set.
31 * If title searches are not supported or disabled, return null.
32 *
33 * @param string $term - Raw search term
34 * @return SearchResultSet
35 * @access public
36 * @abstract
37 */
38 function searchTitle( $term ) {
39 return null;
40 }
41
42 /**
43 * If an exact title match can be find, or a very slightly close match,
44 * return the title. If no match, returns NULL.
45 *
46 * @static
47 * @param string $term
48 * @return Title
49 * @private
50 */
51 function getNearMatch( $searchterm ) {
52 global $wgContLang;
53
54 $allSearchTerms = array($searchterm);
55
56 if($wgContLang->hasVariants()){
57 $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
58 }
59
60 foreach($allSearchTerms as $term){
61
62 # Exact match? No need to look further.
63 $title = Title::newFromText( $term );
64 if (is_null($title))
65 return NULL;
66
67 if ( $title->getNamespace() == NS_SPECIAL || $title->exists() ) {
68 return $title;
69 }
70
71 # Now try all lower case (i.e. first letter capitalized)
72 #
73 $title = Title::newFromText( $wgContLang->lc( $term ) );
74 if ( $title->exists() ) {
75 return $title;
76 }
77
78 # Now try capitalized string
79 #
80 $title = Title::newFromText( $wgContLang->ucwords( $term ) );
81 if ( $title->exists() ) {
82 return $title;
83 }
84
85 # Now try all upper case
86 #
87 $title = Title::newFromText( $wgContLang->uc( $term ) );
88 if ( $title->exists() ) {
89 return $title;
90 }
91
92 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
93 $title = Title::newFromText( $wgContLang->ucwordbreaks($term) );
94 if ( $title->exists() ) {
95 return $title;
96 }
97
98 global $wgCapitalLinks, $wgContLang;
99 if( !$wgCapitalLinks ) {
100 // Catch differs-by-first-letter-case-only
101 $title = Title::newFromText( $wgContLang->ucfirst( $term ) );
102 if ( $title->exists() ) {
103 return $title;
104 }
105 $title = Title::newFromText( $wgContLang->lcfirst( $term ) );
106 if ( $title->exists() ) {
107 return $title;
108 }
109 }
110 }
111
112 $title = Title::newFromText( $searchterm );
113
114 # Entering an IP address goes to the contributions page
115 if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
116 || User::isIP( trim( $searchterm ) ) ) {
117 return SpecialPage::getTitleFor( 'Contributions', $title->getDbkey() );
118 }
119
120
121 # Entering a user goes to the user page whether it's there or not
122 if ( $title->getNamespace() == NS_USER ) {
123 return $title;
124 }
125
126 # Quoted term? Try without the quotes...
127 $matches = array();
128 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
129 return SearchEngine::getNearMatch( $matches[1] );
130 }
131
132 return NULL;
133 }
134
135 public static function legalSearchChars() {
136 return "A-Za-z_'0-9\\x80-\\xFF\\-";
137 }
138
139 /**
140 * Set the maximum number of results to return
141 * and how many to skip before returning the first.
142 *
143 * @param int $limit
144 * @param int $offset
145 * @access public
146 */
147 function setLimitOffset( $limit, $offset = 0 ) {
148 $this->limit = intval( $limit );
149 $this->offset = intval( $offset );
150 }
151
152 /**
153 * Set which namespaces the search should include.
154 * Give an array of namespace index numbers.
155 *
156 * @param array $namespaces
157 * @access public
158 */
159 function setNamespaces( $namespaces ) {
160 $this->namespaces = $namespaces;
161 }
162
163 /**
164 * Make a list of searchable namespaces and their canonical names.
165 * @return array
166 * @access public
167 */
168 function searchableNamespaces() {
169 global $wgContLang;
170 $arr = array();
171 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
172 if( $ns >= NS_MAIN ) {
173 $arr[$ns] = $name;
174 }
175 }
176 return $arr;
177 }
178
179 /**
180 * Return a 'cleaned up' search string
181 *
182 * @return string
183 * @access public
184 */
185 function filter( $text ) {
186 $lc = $this->legalSearchChars();
187 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
188 }
189 /**
190 * Load up the appropriate search engine class for the currently
191 * active database backend, and return a configured instance.
192 *
193 * @return SearchEngine
194 */
195 public static function create() {
196 global $wgDBtype, $wgSearchType;
197 if( $wgSearchType ) {
198 $class = $wgSearchType;
199 } elseif( $wgDBtype == 'mysql' ) {
200 $class = 'SearchMySQL4';
201 } else if ( $wgDBtype == 'postgres' ) {
202 $class = 'SearchPostgres';
203 } else {
204 $class = 'SearchEngineDummy';
205 }
206 $search = new $class( wfGetDB( DB_SLAVE ) );
207 $search->setLimitOffset(0,0);
208 return $search;
209 }
210
211 /**
212 * Create or update the search index record for the given page.
213 * Title and text should be pre-processed.
214 *
215 * @param int $id
216 * @param string $title
217 * @param string $text
218 * @abstract
219 */
220 function update( $id, $title, $text ) {
221 // no-op
222 }
223
224 /**
225 * Update a search index record's title only.
226 * Title should be pre-processed.
227 *
228 * @param int $id
229 * @param string $title
230 * @abstract
231 */
232 function updateTitle( $id, $title ) {
233 // no-op
234 }
235 }
236
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 class SearchResult {
312 function SearchResult( $row ) {
313 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
314 }
315
316 /**
317 * @return Title
318 * @access public
319 */
320 function getTitle() {
321 return $this->mTitle;
322 }
323
324 /**
325 * @return double or null if not supported
326 */
327 function getScore() {
328 return null;
329 }
330 }
331
332 /**
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 ?>