Removing enchanted spaces
[lhc/web/wiklou.git] / includes / SearchEngine.php
1 <?php
2 /**
3 * Contain a class for special pages
4 * @package MediaWiki
5 */
6
7 /**
8 * @package MediaWiki
9 */
10 class SearchEngine {
11 var $limit = 10;
12 var $offset = 0;
13 var $searchTerms = array();
14 var $namespaces = array( 0 );
15 var $showRedirects = false;
16
17 /**
18 * Perform a full text search query and return a result set.
19 *
20 * @param string $term - Raw search term
21 * @param array $namespaces - List of namespaces to search
22 * @return ResultWrapper
23 * @access public
24 */
25 function searchText( $term ) {
26 return $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
27 }
28
29 /**
30 * Perform a title-only search query and return a result set.
31 *
32 * @param string $term - Raw search term
33 * @param array $namespaces - List of namespaces to search
34 * @return ResultWrapper
35 * @access public
36 */
37 function searchTitle( $term ) {
38 return $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
39 }
40
41 /**
42 * If an exact title match can be find, or a very slightly close match,
43 * return the title. If no match, returns NULL.
44 *
45 * @param string $term
46 * @return Title
47 * @access private
48 */
49 function getNearMatch( $term ) {
50 # Exact match? No need to look further.
51 $title = Title::newFromText( $term );
52 if ( $title->getNamespace() == NS_SPECIAL || 0 != $title->getArticleID() ) {
53 return $title;
54 }
55
56 # Now try all lower case (i.e. first letter capitalized)
57 #
58 $title = Title::newFromText( strtolower( $term ) );
59 if ( 0 != $title->getArticleID() ) {
60 return $title;
61 }
62
63 # Now try capitalized string
64 #
65 $title = Title::newFromText( ucwords( strtolower( $term ) ) );
66 if ( 0 != $title->getArticleID() ) {
67 return $title;
68 }
69
70 # Now try all upper case
71 #
72 $title = Title::newFromText( strtoupper( $term ) );
73 if ( 0 != $title->getArticleID() ) {
74 return $title;
75 }
76
77 $title = Title::newFromText( $term );
78
79 # Entering an IP address goes to the contributions page
80 if ( ( $title->getNameSpace() == NS_USER && User::isIP($title->getText() ) ) || User::isIP(trim($term)) ) {
81 $t2 = Title::makeTitle( NS_SPECIAL, "Contributions/" . $title->getText() );
82 return $t2;
83 }
84
85
86 # Entering a user goes to the user page whether it's there or not
87 if ( $title->getNameSpace() == NS_USER ) {
88 if (User::idFromName($title->getText())) {
89 return $title;
90 }
91 }
92
93 return NULL;
94 }
95
96 function legalSearchChars() {
97 return "A-Za-z_'0-9\\x80-\\xFF\\-";
98 }
99
100 /**
101 * Set the maximum number of results to return
102 * and how many to skip before returning the first.
103 *
104 * @param int $limit
105 * @param int $offset
106 * @access public
107 */
108 function setLimitOffset( $limit, $offset = 0 ) {
109 $this->limit = IntVal( $limit );
110 $this->offset = IntVal( $offset );
111 }
112
113 /**
114 * Set which namespaces the search should include.
115 * Give an array of namespace index numbers.
116 *
117 * @param array $namespaces
118 * @access public
119 */
120 function setNamespaces( $namespaces ) {
121 $this->namespaces = $namespaces;
122 }
123
124 /**
125 * Make a list of searchable namespaces and their canonical names.
126 * @return array
127 * @access public
128 */
129 function searchableNamespaces() {
130 global $wgContLang;
131 $arr = array();
132 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
133 if( $ns >= NS_MAIN ) {
134 $arr[$ns] = $name;
135 }
136 }
137 return $arr;
138 }
139
140 /**
141 * Fetch an array of regular expression fragments for matching
142 * the search terms as parsed by this engine in a text extract.
143 *
144 * @return array
145 * @access public
146 */
147 function termMatches() {
148 return $this->searchTerms;
149 }
150
151 /**
152 * Return a 'cleaned up' search string
153 *
154 * @return string
155 * @access public
156 */
157 function filter( $text ) {
158 $lc = $this->legalSearchChars();
159 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
160 }
161
162 /**
163 * Return a partial WHERE clause to exclude redirects, if so set
164 * @return string
165 * @access private
166 */
167 function queryRedirect() {
168 if( $this->showRedirects ) {
169 return 'AND cur_is_redirect=0';
170 } else {
171 return '';
172 }
173 }
174
175 /**
176 * Return a partial WHERE clause to limit the search to the given namespaces
177 * @return string
178 * @access private
179 */
180 function queryNamespaces() {
181 $namespaces = implode( ',', $this->namespaces );
182 if ($namespaces == '') {
183 $namespaces = '0';
184 }
185 return 'AND page_namespace IN (' . $namespaces . ')';
186 }
187
188 /**
189 * Return a LIMIT clause to limit results on the query.
190 * @return string
191 * @access private
192 */
193 function queryLimit() {
194 return $this->db->limitResult( $this->limit, $this->offset );
195 }
196
197 /**
198 * Does not do anything for generic search engine
199 * subclasses may define this though
200 * @return string
201 * @access private
202 */
203 function queryRanking($filteredTerm,$fulltext) {
204 return "";
205 }
206
207 /**
208 * Construct the full SQL query to do the search.
209 * The guts shoulds be constructed in queryMain()
210 * @param string $filteredTerm
211 * @param bool $fulltext
212 * @access private
213 */
214 function getQuery( $filteredTerm, $fulltext ) {
215 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
216 $this->queryRedirect() . ' ' .
217 $this->queryNamespaces() . ' ' .
218 $this->queryRanking($filteredTerm, $fulltext) . ' ' .
219 $this->queryLimit();
220 }
221
222 /**
223 * Load up the appropriate search engine class for the currently
224 * active database backend, and return a configured instance.
225 *
226 * @return SearchEngine
227 * @access private
228 */
229 function create() {
230 global $wgDBtype, $wgDBmysql4, $wgSearchType;
231 if( $wgDBtype == 'mysql' ) {
232 if( $wgDBmysql4 ) {
233 $class = 'SearchMySQL4';
234 require_once( 'SearchMySQL4.php' );
235 } else {
236 $class = 'SearchMysql3';
237 require_once( 'SearchMySQL3.php' );
238 }
239 } else if ( $wgDBtype == 'PostgreSQL' ) {
240 $class = 'SearchTsearch2';
241 require_once( 'SearchTsearch2.php' );
242 } else {
243 $class = 'SearchEngineDummy';
244 }
245 $search = new $class( wfGetDB( DB_SLAVE ) );
246 $search->setLimitOffset(0,0);
247 return $search;
248 }
249
250
251 }
252
253 /**
254 * @package MediaWiki
255 */
256 class SearchEngineDummy {
257 function search( $term ) {
258 return null;
259 }
260 }
261