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