fix some issues with phpdoc
[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 /** @package MediaWiki */
217 class SearchResultSet {
218 /**
219 * Fetch an array of regular expression fragments for matching
220 * the search terms as parsed by this engine in a text extract.
221 *
222 * @return array
223 * @access public
224 * @abstract
225 */
226 function termMatches() {
227 return array();
228 }
229
230 function numRows() {
231 return 0;
232 }
233
234 /**
235 * Return true if results are included in this result set.
236 * @return bool
237 * @abstract
238 */
239 function hasResults() {
240 return false;
241 }
242
243 /**
244 * Some search modes return a total hit count for the query
245 * in the entire article database. This may include pages
246 * in namespaces that would not be matched on the given
247 * settings.
248 *
249 * Return null if no total hits number is supported.
250 *
251 * @return int
252 * @access public
253 */
254 function getTotalHits() {
255 return null;
256 }
257
258 /**
259 * Some search modes return a suggested alternate term if there are
260 * no exact hits. Returns true if there is one on this set.
261 *
262 * @return bool
263 * @access public
264 */
265 function hasSuggestion() {
266 return false;
267 }
268
269 /**
270 * Some search modes return a suggested alternate term if there are
271 * no exact hits. Check hasSuggestion() first.
272 *
273 * @return string
274 * @access public
275 */
276 function getSuggestion() {
277 return '';
278 }
279
280 /**
281 * Fetches next search result, or false.
282 * @return SearchResult
283 * @access public
284 * @abstract
285 */
286 function next() {
287 return false;
288 }
289 }
290
291 /** @package MediaWiki */
292 class SearchResult {
293 function SearchResult( $row ) {
294 $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
295 }
296
297 /**
298 * @return Title
299 * @access public
300 */
301 function getTitle() {
302 return $this->mTitle;
303 }
304
305 /**
306 * @return double or null if not supported
307 */
308 function getScore() {
309 return null;
310 }
311 }
312
313 /**
314 * @package MediaWiki
315 */
316 class SearchEngineDummy {
317 function search( $term ) {
318 return null;
319 }
320 }
321