81d2ad639bed20ace03962a7bdb1cd9981e0c5d7
[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( 0 );
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 $title = Title::newFromText( $term );
85
86 # Entering an IP address goes to the contributions page
87 if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
88 || User::isIP( trim( $term ) ) ) {
89 return Title::makeTitle( NS_SPECIAL, "Contributions/" . $title->getDbkey() );
90 }
91
92
93 # Entering a user goes to the user page whether it's there or not
94 if ( $title->getNamespace() == NS_USER ) {
95 return $title;
96 }
97
98 # Quoted term? Try without the quotes...
99 if( preg_match( '/^"([^"]+)"$/', $term, $matches ) ) {
100 return SearchEngine::getNearMatch( $matches[1] );
101 }
102
103 return NULL;
104 }
105
106 function legalSearchChars() {
107 return "A-Za-z_'0-9\\x80-\\xFF\\-";
108 }
109
110 /**
111 * Set the maximum number of results to return
112 * and how many to skip before returning the first.
113 *
114 * @param int $limit
115 * @param int $offset
116 * @access public
117 */
118 function setLimitOffset( $limit, $offset = 0 ) {
119 $this->limit = IntVal( $limit );
120 $this->offset = IntVal( $offset );
121 }
122
123 /**
124 * Set which namespaces the search should include.
125 * Give an array of namespace index numbers.
126 *
127 * @param array $namespaces
128 * @access public
129 */
130 function setNamespaces( $namespaces ) {
131 $this->namespaces = $namespaces;
132 }
133
134 /**
135 * Make a list of searchable namespaces and their canonical names.
136 * @return array
137 * @access public
138 */
139 function searchableNamespaces() {
140 global $wgContLang;
141 $arr = array();
142 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
143 if( $ns >= NS_MAIN ) {
144 $arr[$ns] = $name;
145 }
146 }
147 return $arr;
148 }
149
150 /**
151 * Return a 'cleaned up' search string
152 *
153 * @return string
154 * @access public
155 */
156 function filter( $text ) {
157 $lc = $this->legalSearchChars();
158 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
159 }
160 /**
161 * Load up the appropriate search engine class for the currently
162 * active database backend, and return a configured instance.
163 *
164 * @return SearchEngine
165 * @access private
166 */
167 function create() {
168 global $wgDBtype, $wgDBmysql4, $wgSearchType;
169 if( $wgSearchType ) {
170 $class = $wgSearchType;
171 } elseif( $wgDBtype == 'mysql' ) {
172 if( $wgDBmysql4 ) {
173 $class = 'SearchMySQL4';
174 require_once( 'SearchMySQL4.php' );
175 } else {
176 $class = 'SearchMysql3';
177 require_once( 'SearchMySQL3.php' );
178 }
179 } else if ( $wgDBtype == 'PostgreSQL' ) {
180 $class = 'SearchTsearch2';
181 require_once( 'SearchTsearch2.php' );
182 } else {
183 $class = 'SearchEngineDummy';
184 }
185 $search = new $class( wfGetDB( DB_SLAVE ) );
186 $search->setLimitOffset(0,0);
187 return $search;
188 }
189
190 /**
191 * Create or update the search index record for the given page.
192 * Title and text should be pre-processed.
193 *
194 * @param int $id
195 * @param string $title
196 * @param string $text
197 * @abstract
198 */
199 function update( $id, $title, $text ) {
200 // no-op
201 }
202
203 /**
204 * Update a search index record's title only.
205 * Title should be pre-processed.
206 *
207 * @param int $id
208 * @param string $title
209 * @abstract
210 */
211 function updateTitle( $id, $title ) {
212 // no-op
213 }
214 }
215
216 class SearchResultSet {
217 /**
218 * Fetch an array of regular expression fragments for matching
219 * the search terms as parsed by this engine in a text extract.
220 *
221 * @return array
222 * @access public
223 * @abstract
224 */
225 function termMatches() {
226 return array();
227 }
228
229 function numRows() {
230 return 0;
231 }
232
233 /**
234 * Return true if results are included in this result set.
235 * @return bool
236 * @abstract
237 */
238 function hasResults() {
239 return false;
240 }
241
242 /**
243 * Some search modes return a total hit count for the query
244 * in the entire article database. This may include pages
245 * in namespaces that would not be matched on the given
246 * settings.
247 *
248 * Return null if no total hits number is supported.
249 *
250 * @return int
251 * @access public
252 */
253 function getTotalHits() {
254 return null;
255 }
256
257 /**
258 * Some search modes return a suggested alternate term if there are
259 * no exact hits. Returns true if there is one on this set.
260 *
261 * @return bool
262 * @access public
263 */
264 function hasSuggestion() {
265 return false;
266 }
267
268 /**
269 * Some search modes return a suggested alternate term if there are
270 * no exact hits. Check hasSuggestion() first.
271 *
272 * @return string
273 * @access public
274 */
275 function getSuggestion() {
276 return '';
277 }
278
279 /**
280 * Fetches next search result, or false.
281 * @return SearchResult
282 * @access public
283 * @abstract
284 */
285 function next() {
286 return false;
287 }
288 }
289
290 class SearchResult {
291 function SearchResult( $row ) {
292 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
293 }
294
295 /**
296 * @return Title
297 * @access public
298 */
299 function getTitle() {
300 return $this->mTitle;
301 }
302
303 /**
304 * @return double or null if not supported
305 */
306 function getScore() {
307 return null;
308 }
309 }
310
311 /**
312 * @package MediaWiki
313 */
314 class SearchEngineDummy {
315 function search( $term ) {
316 return null;
317 }
318 }
319