fixed potential XSS vulnerability
[mediawiki.git] / includes / SearchEngine.php
blob8ae7e6ec91ca727b2437122c639761b084cae8fd
1 <?php
2 /**
3 * Contain a class for special pages
4 * @package MediaWiki
5 * @subpackage Search
6 */
8 /**
9 * @package MediaWiki
11 class SearchEngine {
12 var $limit = 10;
13 var $offset = 0;
14 var $searchTerms = array();
15 var $namespaces = array( 0 );
16 var $showRedirects = false;
18 /**
19 * Perform a full text search query and return a result set.
21 * @param string $term - Raw search term
22 * @param array $namespaces - List of namespaces to search
23 * @return ResultWrapper
24 * @access public
26 function searchText( $term ) {
27 return $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
30 /**
31 * Perform a title-only search query and return a result set.
33 * @param string $term - Raw search term
34 * @param array $namespaces - List of namespaces to search
35 * @return ResultWrapper
36 * @access public
38 function searchTitle( $term ) {
39 return $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), false ) ) );
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.
46 * @static
47 * @param string $term
48 * @return Title
49 * @access private
51 function getNearMatch( $term ) {
52 # Exact match? No need to look further.
53 $title = Title::newFromText( $term );
54 if ( $title->getNamespace() == NS_SPECIAL || 0 != $title->getArticleID() ) {
55 return $title;
58 # Now try all lower case (i.e. first letter capitalized)
60 $title = Title::newFromText( strtolower( $term ) );
61 if ( 0 != $title->getArticleID() ) {
62 return $title;
65 # Now try capitalized string
67 $title = Title::newFromText( ucwords( strtolower( $term ) ) );
68 if ( 0 != $title->getArticleID() ) {
69 return $title;
72 # Now try all upper case
74 $title = Title::newFromText( strtoupper( $term ) );
75 if ( 0 != $title->getArticleID() ) {
76 return $title;
79 $title = Title::newFromText( $term );
81 # Entering an IP address goes to the contributions page
82 if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
83 || User::isIP( trim( $term ) ) ) {
84 return Title::makeTitle( NS_SPECIAL, "Contributions/" . $title->getDbkey() );
88 # Entering a user goes to the user page whether it's there or not
89 if ( $title->getNamespace() == NS_USER ) {
90 return $title;
93 # Quoted term? Try without the quotes...
94 if( preg_match( '/^"([^"]+)"$/', $term, $matches ) ) {
95 return SearchEngine::getNearMatch( $matches[1] );
98 return NULL;
101 function legalSearchChars() {
102 return "A-Za-z_'0-9\\x80-\\xFF\\-";
106 * Set the maximum number of results to return
107 * and how many to skip before returning the first.
109 * @param int $limit
110 * @param int $offset
111 * @access public
113 function setLimitOffset( $limit, $offset = 0 ) {
114 $this->limit = IntVal( $limit );
115 $this->offset = IntVal( $offset );
119 * Set which namespaces the search should include.
120 * Give an array of namespace index numbers.
122 * @param array $namespaces
123 * @access public
125 function setNamespaces( $namespaces ) {
126 $this->namespaces = $namespaces;
130 * Make a list of searchable namespaces and their canonical names.
131 * @return array
132 * @access public
134 function searchableNamespaces() {
135 global $wgContLang;
136 $arr = array();
137 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
138 if( $ns >= NS_MAIN ) {
139 $arr[$ns] = $name;
142 return $arr;
146 * Fetch an array of regular expression fragments for matching
147 * the search terms as parsed by this engine in a text extract.
149 * @return array
150 * @access public
152 function termMatches() {
153 return $this->searchTerms;
157 * Return a 'cleaned up' search string
159 * @return string
160 * @access public
162 function filter( $text ) {
163 $lc = $this->legalSearchChars();
164 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
168 * Return a partial WHERE clause to exclude redirects, if so set
169 * @return string
170 * @access private
172 function queryRedirect() {
173 if( $this->showRedirects ) {
174 return 'AND cur_is_redirect=0';
175 } else {
176 return '';
181 * Return a partial WHERE clause to limit the search to the given namespaces
182 * @return string
183 * @access private
185 function queryNamespaces() {
186 $namespaces = implode( ',', $this->namespaces );
187 if ($namespaces == '') {
188 $namespaces = '0';
190 return 'AND page_namespace IN (' . $namespaces . ')';
194 * Return a LIMIT clause to limit results on the query.
195 * @return string
196 * @access private
198 function queryLimit() {
199 return $this->db->limitResult( $this->limit, $this->offset );
203 * Does not do anything for generic search engine
204 * subclasses may define this though
205 * @return string
206 * @access private
208 function queryRanking($filteredTerm,$fulltext) {
209 return "";
213 * Construct the full SQL query to do the search.
214 * The guts shoulds be constructed in queryMain()
215 * @param string $filteredTerm
216 * @param bool $fulltext
217 * @access private
219 function getQuery( $filteredTerm, $fulltext ) {
220 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
221 $this->queryRedirect() . ' ' .
222 $this->queryNamespaces() . ' ' .
223 $this->queryRanking($filteredTerm, $fulltext) . ' ' .
224 $this->queryLimit();
228 * Load up the appropriate search engine class for the currently
229 * active database backend, and return a configured instance.
231 * @return SearchEngine
232 * @access private
234 function create() {
235 global $wgDBtype, $wgDBmysql4, $wgSearchType;
236 if( $wgDBtype == 'mysql' ) {
237 if( $wgDBmysql4 ) {
238 $class = 'SearchMySQL4';
239 require_once( 'SearchMySQL4.php' );
240 } else {
241 $class = 'SearchMysql3';
242 require_once( 'SearchMySQL3.php' );
244 } else if ( $wgDBtype == 'PostgreSQL' ) {
245 $class = 'SearchTsearch2';
246 require_once( 'SearchTsearch2.php' );
247 } else {
248 $class = 'SearchEngineDummy';
250 $search = new $class( wfGetDB( DB_SLAVE ) );
251 $search->setLimitOffset(0,0);
252 return $search;
259 * @package MediaWiki
261 class SearchEngineDummy {
262 function search( $term ) {
263 return null;