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