3 * Contain a class for special pages
13 var $searchTerms = array();
14 var $namespaces = array( 0 );
15 var $showRedirects = false;
18 * Perform a full text search query and return a result set.
20 * @param string $term - Raw search term
21 * @param array $namespaces - List of namespaces to search
22 * @return ResultWrapper
25 function searchText( $term ) {
26 return $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
30 * Perform a title-only search query and return a result set.
32 * @param string $term - Raw search term
33 * @param array $namespaces - List of namespaces to search
34 * @return ResultWrapper
37 function searchTitle( $term ) {
38 return $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
42 * If an exact title match can be find, or a very slightly close match,
43 * return the title. If no match, returns NULL.
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() ) {
56 # Now try all lower case (i.e. first letter capitalized)
58 $title = Title
::newFromText( strtolower( $term ) );
59 if ( 0 != $title->getArticleID() ) {
63 # Now try capitalized string
65 $title = Title
::newFromText( ucwords( strtolower( $term ) ) );
66 if ( 0 != $title->getArticleID() ) {
70 # Now try all upper case
72 $title = Title
::newFromText( strtoupper( $term ) );
73 if ( 0 != $title->getArticleID() ) {
77 # Entering an IP address goes to the contributions page
78 if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $term ) ) {
79 $title = Title
::makeTitle( NS_SPECIAL
, "Contributions/" . $term );
86 function legalSearchChars() {
87 return "A-Za-z_'0-9\\x80-\\xFF\\-";
91 * Set the maximum number of results to return
92 * and how many to skip before returning the first.
98 function setLimitOffset( $limit, $offset = 0 ) {
99 $this->limit
= IntVal( $limit );
100 $this->offset
= IntVal( $offset );
104 * Set which namespaces the search should include.
105 * Give an array of namespace index numbers.
107 * @param array $namespaces
110 function setNamespaces( $namespaces ) {
111 $this->namespaces
= $namespaces;
115 * Make a list of searchable namespaces and their canonical names.
119 function searchableNamespaces() {
122 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
123 if( $ns >= NS_MAIN
) {
131 * Fetch an array of regular expression fragments for matching
132 * the search terms as parsed by this engine in a text extract.
137 function termMatches() {
138 return $this->searchTerms
;
142 * Return a 'cleaned up' search string
147 function filter( $text ) {
148 $lc = $this->legalSearchChars();
149 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
153 * Return a partial WHERE clause to exclude redirects, if so set
157 function queryRedirect() {
158 if( $this->showRedirects
) {
159 return 'AND cur_is_redirect=0';
166 * Return a partial WHERE clause to limit the search to the given namespaces
170 function queryNamespaces() {
171 $namespaces = implode( ',', $this->namespaces
);
172 if ($namespaces == '') {
175 return 'AND page_namespace IN (' . $namespaces . ')';
179 * Return a LIMIT clause to limit results on the query.
183 function queryLimit() {
184 return $this->db
->limitResult( $this->limit
, $this->offset
);
188 * Does not do anything for generic search engine
189 * subclasses may define this though
193 function queryRanking($filteredTerm,$fulltext) {
198 * Construct the full SQL query to do the search.
199 * The guts shoulds be constructed in queryMain()
200 * @param string $filteredTerm
201 * @param bool $fulltext
204 function getQuery( $filteredTerm, $fulltext ) {
205 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
206 $this->queryRedirect() . ' ' .
207 $this->queryNamespaces() . ' ' .
208 $this->queryRanking($filteredTerm, $fulltext) . ' ' .
213 * Load up the appropriate search engine class for the currently
214 * active database backend, and return a configured instance.
216 * @return SearchEngine
220 global $wgDBtype, $wgDBmysql4, $wgSearchType;
221 if( $wgDBtype == 'mysql' ) {
223 $class = 'SearchMySQL4';
224 require_once( 'SearchMySQL4.php' );
226 $class = 'SearchMysql3';
227 require_once( 'SearchMySQL3.php' );
229 } else if ( $wgDBtype == 'PostgreSQL' ) {
230 $class = 'SearchTsearch2';
231 require_once( 'SearchTsearch2.php' );
233 $class = 'SearchEngineDummy';
235 $search = new $class( wfGetDB( DB_SLAVE
) );
236 $search->setLimitOffset(0,0);
246 class SearchEngineDummy
{
247 function search( $term ) {