3 * Contain a class for special pages
14 var $searchTerms = array();
15 var $namespaces = array( 0 );
16 var $showRedirects = false;
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
26 function searchText( $term ) {
27 return $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
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
38 function searchTitle( $term ) {
39 return $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
43 * If an exact title match can be find, or a very slightly close match,
44 * return the title. If no match, returns NULL.
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() ) {
58 # Now try all lower case (i.e. first letter capitalized)
60 $title = Title
::newFromText( strtolower( $term ) );
61 if ( 0 != $title->getArticleID() ) {
65 # Now try capitalized string
67 $title = Title
::newFromText( ucwords( strtolower( $term ) ) );
68 if ( 0 != $title->getArticleID() ) {
72 # Now try all upper case
74 $title = Title
::newFromText( strtoupper( $term ) );
75 if ( 0 != $title->getArticleID() ) {
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
) {
93 # Quoted term? Try without the quotes...
94 if( preg_match( '/^"([^"]+)"$/', $term, $matches ) ) {
95 return SearchEngine
::getNearMatch( $matches[1] );
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.
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
125 function setNamespaces( $namespaces ) {
126 $this->namespaces
= $namespaces;
130 * Make a list of searchable namespaces and their canonical names.
134 function searchableNamespaces() {
137 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
138 if( $ns >= NS_MAIN
) {
146 * Fetch an array of regular expression fragments for matching
147 * the search terms as parsed by this engine in a text extract.
152 function termMatches() {
153 return $this->searchTerms
;
157 * Return a 'cleaned up' search string
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
172 function queryRedirect() {
173 if( $this->showRedirects
) {
174 return 'AND cur_is_redirect=0';
181 * Return a partial WHERE clause to limit the search to the given namespaces
185 function queryNamespaces() {
186 $namespaces = implode( ',', $this->namespaces
);
187 if ($namespaces == '') {
190 return 'AND page_namespace IN (' . $namespaces . ')';
194 * Return a LIMIT clause to limit results on the query.
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
208 function queryRanking($filteredTerm,$fulltext) {
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
219 function getQuery( $filteredTerm, $fulltext ) {
220 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
221 $this->queryRedirect() . ' ' .
222 $this->queryNamespaces() . ' ' .
223 $this->queryRanking($filteredTerm, $fulltext) . ' ' .
228 * Load up the appropriate search engine class for the currently
229 * active database backend, and return a configured instance.
231 * @return SearchEngine
235 global $wgDBtype, $wgDBmysql4, $wgSearchType;
236 if( $wgDBtype == 'mysql' ) {
238 $class = 'SearchMySQL4';
239 require_once( 'SearchMySQL4.php' );
241 $class = 'SearchMysql3';
242 require_once( 'SearchMySQL3.php' );
244 } else if ( $wgDBtype == 'PostgreSQL' ) {
245 $class = 'SearchTsearch2';
246 require_once( 'SearchTsearch2.php' );
248 $class = 'SearchEngineDummy';
250 $search = new $class( wfGetDB( DB_SLAVE
) );
251 $search->setLimitOffset(0,0);
261 class SearchEngineDummy
{
262 function search( $term ) {