3 * Contain a class for special pages
11 var $searchTerms = array();
12 var $namespaces = array( 0 );
13 var $showRedirects = false;
16 * Perform a full text search query and return a result set.
18 * @param string $term - Raw search term
19 * @param array $namespaces - List of namespaces to search
20 * @return ResultWrapper
23 function searchText( $term ) {
24 return $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
28 * Perform a title-only search query and return a result set.
30 * @param string $term - Raw search term
31 * @param array $namespaces - List of namespaces to search
32 * @return ResultWrapper
35 function searchTitle( $term ) {
36 return $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
40 * If an exact title match can be find, or a very slightly close match,
41 * return the title. If no match, returns NULL.
47 function getNearMatch( $term ) {
48 # Exact match? No need to look further.
49 $title = Title
::newFromText( $term );
50 if ( $title->getNamespace() == NS_SPECIAL ||
0 != $title->getArticleID() ) {
54 # Now try all lower case (i.e. first letter capitalized)
56 $title = Title
::newFromText( strtolower( $term ) );
57 if ( 0 != $title->getArticleID() ) {
61 # Now try capitalized string
63 $title = Title
::newFromText( ucwords( strtolower( $term ) ) );
64 if ( 0 != $title->getArticleID() ) {
68 # Now try all upper case
70 $title = Title
::newFromText( strtoupper( $term ) );
71 if ( 0 != $title->getArticleID() ) {
75 # Entering an IP address goes to the contributions page
76 if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $term ) ) {
77 $title = Title
::makeTitle( NS_SPECIAL
, "Contributions/" . $term );
84 function legalSearchChars() {
85 return "A-Za-z_'0-9\\x80-\\xFF\\-";
89 * Set the maximum number of results to return
90 * and how many to skip before returning the first.
96 function setLimitOffset( $limit, $offset = 0 ) {
97 $this->limit
= IntVal( $limit );
98 $this->offset
= IntVal( $offset );
102 * Set which namespaces the search should include.
103 * Give an array of namespace index numbers.
105 * @param array $namespaces
108 function setNamespaces( $namespaces ) {
109 $this->namespaces
= $namespaces;
113 * Make a list of searchable namespaces and their canonical names.
117 function searchableNamespaces() {
120 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
129 * Fetch an array of regular expression fragments for matching
130 * the search terms as parsed by this engine in a text extract.
135 function termMatches() {
136 return $this->searchTerms
;
140 * Return a 'cleaned up' search string
145 function filter( $text ) {
146 $lc = $this->legalSearchChars();
147 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
151 * Return a partial WHERE clause to exclude redirects, if so set
155 function queryRedirect() {
156 if( $this->showRedirects
) {
157 return 'AND cur_is_redirect=0';
164 * Return a partial WHERE clause to limit the search to the given namespaces
168 function queryNamespaces() {
169 $namespaces = implode( ',', $this->namespaces
);
170 if ($namespaces == '') {
173 return 'AND page_namespace IN (' . $namespaces . ')';
177 * Return a LIMIT clause to limit results on the query.
181 function queryLimit() {
182 return $this->db
->limitResult( $this->limit
, $this->offset
);
186 * Does not do anything for generic search engine
187 * subclasses may define this though
191 function queryRanking($filteredTerm,$fulltext) {
196 * Construct the full SQL query to do the search.
197 * The guts shoulds be constructed in queryMain()
198 * @param string $filteredTerm
199 * @param bool $fulltext
202 function getQuery( $filteredTerm, $fulltext ) {
203 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
204 $this->queryRedirect() . ' ' .
205 $this->queryNamespaces() . ' ' .
206 $this->queryRanking($filteredTerm, $fulltext) . ' ' .
211 * Load up the appropriate search engine class for the currently
212 * active database backend, and return a configured instance.
214 * @return SearchEngine
218 global $wgDBtype, $wgDBmysql4, $wgSearchType;
219 if( $wgDBtype == 'mysql' ) {
221 $class = 'SearchMySQL4';
222 require_once( 'SearchMySQL4.php' );
224 $class = 'SearchMysql3';
225 require_once( 'SearchMySQL3.php' );
227 } else if ( $wgDBtype == 'PostgreSQL' ) {
228 $class = 'SearchTsearch2';
229 require_once( 'SearchTsearch2.php' );
231 $class = 'SearchEngineDummy';
233 $search = new $class( wfGetDB( DB_SLAVE
) );
234 $search->setLimitOffset(0,0);
242 class SearchEngineDummy
{
243 function search( $term ) {