Yet more doc tweaks:
[lhc/web/wiklou.git] / includes / SearchEngine.php
1 <?php
2 /**
3 * Contain a class for special pages
4 * @addtogroup Search
5 */
6 class SearchEngine {
7 var $limit = 10;
8 var $offset = 0;
9 var $searchTerms = array();
10 var $namespaces = array( NS_MAIN );
11 var $showRedirects = false;
12
13 /**
14 * Perform a full text search query and return a result set.
15 * If title searches are not supported or disabled, return null.
16 *
17 * @param string $term - Raw search term
18 * @return SearchResultSet
19 * @access public
20 * @abstract
21 */
22 function searchText( $term ) {
23 return null;
24 }
25
26 /**
27 * Perform a title-only search query and return a result set.
28 * If title searches are not supported or disabled, return null.
29 *
30 * @param string $term - Raw search term
31 * @return SearchResultSet
32 * @access public
33 * @abstract
34 */
35 function searchTitle( $term ) {
36 return null;
37 }
38
39 /**
40 * If an exact title match can be find, or a very slightly close match,
41 * return the title. If no match, returns NULL.
42 *
43 * @static
44 * @param string $term
45 * @return Title
46 * @private
47 */
48 function getNearMatch( $searchterm ) {
49 global $wgContLang;
50
51 $allSearchTerms = array($searchterm);
52
53 if($wgContLang->hasVariants()){
54 $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
55 }
56
57 foreach($allSearchTerms as $term){
58
59 # Exact match? No need to look further.
60 $title = Title::newFromText( $term );
61 if (is_null($title))
62 return NULL;
63
64 if ( $title->getNamespace() == NS_SPECIAL || $title->exists() ) {
65 return $title;
66 }
67
68 # Now try all lower case (i.e. first letter capitalized)
69 #
70 $title = Title::newFromText( $wgContLang->lc( $term ) );
71 if ( $title->exists() ) {
72 return $title;
73 }
74
75 # Now try capitalized string
76 #
77 $title = Title::newFromText( $wgContLang->ucwords( $term ) );
78 if ( $title->exists() ) {
79 return $title;
80 }
81
82 # Now try all upper case
83 #
84 $title = Title::newFromText( $wgContLang->uc( $term ) );
85 if ( $title->exists() ) {
86 return $title;
87 }
88
89 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
90 $title = Title::newFromText( $wgContLang->ucwordbreaks($term) );
91 if ( $title->exists() ) {
92 return $title;
93 }
94
95 global $wgCapitalLinks, $wgContLang;
96 if( !$wgCapitalLinks ) {
97 // Catch differs-by-first-letter-case-only
98 $title = Title::newFromText( $wgContLang->ucfirst( $term ) );
99 if ( $title->exists() ) {
100 return $title;
101 }
102 $title = Title::newFromText( $wgContLang->lcfirst( $term ) );
103 if ( $title->exists() ) {
104 return $title;
105 }
106 }
107 }
108
109 $title = Title::newFromText( $searchterm );
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( $searchterm ) ) ) {
114 return SpecialPage::getTitleFor( '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 $matches = array();
125 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
126 return SearchEngine::getNearMatch( $matches[1] );
127 }
128
129 return NULL;
130 }
131
132 public static function legalSearchChars() {
133 return "A-Za-z_'0-9\\x80-\\xFF\\-";
134 }
135
136 /**
137 * Set the maximum number of results to return
138 * and how many to skip before returning the first.
139 *
140 * @param int $limit
141 * @param int $offset
142 * @access public
143 */
144 function setLimitOffset( $limit, $offset = 0 ) {
145 $this->limit = intval( $limit );
146 $this->offset = intval( $offset );
147 }
148
149 /**
150 * Set which namespaces the search should include.
151 * Give an array of namespace index numbers.
152 *
153 * @param array $namespaces
154 * @access public
155 */
156 function setNamespaces( $namespaces ) {
157 $this->namespaces = $namespaces;
158 }
159
160 /**
161 * Make a list of searchable namespaces and their canonical names.
162 * @return array
163 * @access public
164 */
165 function searchableNamespaces() {
166 global $wgContLang;
167 $arr = array();
168 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
169 if( $ns >= NS_MAIN ) {
170 $arr[$ns] = $name;
171 }
172 }
173 return $arr;
174 }
175
176 /**
177 * Return a 'cleaned up' search string
178 *
179 * @return string
180 * @access public
181 */
182 function filter( $text ) {
183 $lc = $this->legalSearchChars();
184 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
185 }
186 /**
187 * Load up the appropriate search engine class for the currently
188 * active database backend, and return a configured instance.
189 *
190 * @return SearchEngine
191 */
192 public static function create() {
193 global $wgDBtype, $wgSearchType;
194 if( $wgSearchType ) {
195 $class = $wgSearchType;
196 } elseif( $wgDBtype == 'mysql' ) {
197 $class = 'SearchMySQL4';
198 } else if ( $wgDBtype == 'postgres' ) {
199 $class = 'SearchPostgres';
200 } else if ( $wgDBtype == 'oracle' ) {
201 $class = 'SearchOracle';
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
237 /**
238 * @addtogroup Search
239 */
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
315 /**
316 * @addtogroup Search
317 */
318 class SearchResult {
319 function SearchResult( $row ) {
320 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
321 }
322
323 /**
324 * @return Title
325 * @access public
326 */
327 function getTitle() {
328 return $this->mTitle;
329 }
330
331 /**
332 * @return double or null if not supported
333 */
334 function getScore() {
335 return null;
336 }
337 }
338
339 /**
340 * @addtogroup Search
341 */
342 class SearchEngineDummy {
343 function search( $term ) {
344 return null;
345 }
346 function setLimitOffset($l, $o) {}
347 function legalSearchChars() {}
348 function update() {}
349 function setnamespaces() {}
350 function searchtitle() {}
351 function searchtext() {}
352 }
353 ?>