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 $title = Title
::newFromText( $term );
79 # Entering an IP address goes to the contributions page
80 if ( ( $title->getNamespace() == NS_USER
&& User
::isIP($title->getText() ) )
81 || User
::isIP( trim( $term ) ) ) {
82 return Title
::makeTitle( NS_SPECIAL
, "Contributions/" . $title->getDbkey() );
86 # Entering a user goes to the user page whether it's there or not
87 if ( $title->getNamespace() == NS_USER
) {
94 function legalSearchChars() {
95 return "A-Za-z_'0-9\\x80-\\xFF\\-";
99 * Set the maximum number of results to return
100 * and how many to skip before returning the first.
106 function setLimitOffset( $limit, $offset = 0 ) {
107 $this->limit
= IntVal( $limit );
108 $this->offset
= IntVal( $offset );
112 * Set which namespaces the search should include.
113 * Give an array of namespace index numbers.
115 * @param array $namespaces
118 function setNamespaces( $namespaces ) {
119 $this->namespaces
= $namespaces;
123 * Make a list of searchable namespaces and their canonical names.
127 function searchableNamespaces() {
130 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
131 if( $ns >= NS_MAIN
) {
139 * Fetch an array of regular expression fragments for matching
140 * the search terms as parsed by this engine in a text extract.
145 function termMatches() {
146 return $this->searchTerms
;
150 * Return a 'cleaned up' search string
155 function filter( $text ) {
156 $lc = $this->legalSearchChars();
157 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
161 * Return a partial WHERE clause to exclude redirects, if so set
165 function queryRedirect() {
166 if( $this->showRedirects
) {
167 return 'AND cur_is_redirect=0';
174 * Return a partial WHERE clause to limit the search to the given namespaces
178 function queryNamespaces() {
179 $namespaces = implode( ',', $this->namespaces
);
180 if ($namespaces == '') {
183 return 'AND page_namespace IN (' . $namespaces . ')';
187 * Return a LIMIT clause to limit results on the query.
191 function queryLimit() {
192 return $this->db
->limitResult( $this->limit
, $this->offset
);
196 * Does not do anything for generic search engine
197 * subclasses may define this though
201 function queryRanking($filteredTerm,$fulltext) {
206 * Construct the full SQL query to do the search.
207 * The guts shoulds be constructed in queryMain()
208 * @param string $filteredTerm
209 * @param bool $fulltext
212 function getQuery( $filteredTerm, $fulltext ) {
213 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
214 $this->queryRedirect() . ' ' .
215 $this->queryNamespaces() . ' ' .
216 $this->queryRanking($filteredTerm, $fulltext) . ' ' .
221 * Load up the appropriate search engine class for the currently
222 * active database backend, and return a configured instance.
224 * @return SearchEngine
228 global $wgDBtype, $wgDBmysql4, $wgSearchType;
229 if( $wgDBtype == 'mysql' ) {
231 $class = 'SearchMySQL4';
232 require_once( 'SearchMySQL4.php' );
234 $class = 'SearchMysql3';
235 require_once( 'SearchMySQL3.php' );
237 } else if ( $wgDBtype == 'PostgreSQL' ) {
238 $class = 'SearchTsearch2';
239 require_once( 'SearchTsearch2.php' );
241 $class = 'SearchEngineDummy';
243 $search = new $class( wfGetDB( DB_SLAVE
) );
244 $search->setLimitOffset(0,0);
254 class SearchEngineDummy
{
255 function search( $term ) {