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() ) ) || User
::isIP(trim($term)) ) {
81 $t2 = Title
::makeTitle( NS_SPECIAL
, "Contributions/" . $title->getText() );
86 # Entering a user goes to the user page whether it's there or not
87 if ( $title->getNameSpace() == NS_USER
) {
88 if (User
::idFromName($title->getText())) {
96 function legalSearchChars() {
97 return "A-Za-z_'0-9\\x80-\\xFF\\-";
101 * Set the maximum number of results to return
102 * and how many to skip before returning the first.
108 function setLimitOffset( $limit, $offset = 0 ) {
109 $this->limit
= IntVal( $limit );
110 $this->offset
= IntVal( $offset );
114 * Set which namespaces the search should include.
115 * Give an array of namespace index numbers.
117 * @param array $namespaces
120 function setNamespaces( $namespaces ) {
121 $this->namespaces
= $namespaces;
125 * Make a list of searchable namespaces and their canonical names.
129 function searchableNamespaces() {
132 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
133 if( $ns >= NS_MAIN
) {
141 * Fetch an array of regular expression fragments for matching
142 * the search terms as parsed by this engine in a text extract.
147 function termMatches() {
148 return $this->searchTerms
;
152 * Return a 'cleaned up' search string
157 function filter( $text ) {
158 $lc = $this->legalSearchChars();
159 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
163 * Return a partial WHERE clause to exclude redirects, if so set
167 function queryRedirect() {
168 if( $this->showRedirects
) {
169 return 'AND cur_is_redirect=0';
176 * Return a partial WHERE clause to limit the search to the given namespaces
180 function queryNamespaces() {
181 $namespaces = implode( ',', $this->namespaces
);
182 if ($namespaces == '') {
185 return 'AND page_namespace IN (' . $namespaces . ')';
189 * Return a LIMIT clause to limit results on the query.
193 function queryLimit() {
194 return $this->db
->limitResult( $this->limit
, $this->offset
);
198 * Does not do anything for generic search engine
199 * subclasses may define this though
203 function queryRanking($filteredTerm,$fulltext) {
208 * Construct the full SQL query to do the search.
209 * The guts shoulds be constructed in queryMain()
210 * @param string $filteredTerm
211 * @param bool $fulltext
214 function getQuery( $filteredTerm, $fulltext ) {
215 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
216 $this->queryRedirect() . ' ' .
217 $this->queryNamespaces() . ' ' .
218 $this->queryRanking($filteredTerm, $fulltext) . ' ' .
223 * Load up the appropriate search engine class for the currently
224 * active database backend, and return a configured instance.
226 * @return SearchEngine
230 global $wgDBtype, $wgDBmysql4, $wgSearchType;
231 if( $wgDBtype == 'mysql' ) {
233 $class = 'SearchMySQL4';
234 require_once( 'SearchMySQL4.php' );
236 $class = 'SearchMysql3';
237 require_once( 'SearchMySQL3.php' );
239 } else if ( $wgDBtype == 'PostgreSQL' ) {
240 $class = 'SearchTsearch2';
241 require_once( 'SearchTsearch2.php' );
243 $class = 'SearchEngineDummy';
245 $search = new $class( wfGetDB( DB_SLAVE
) );
246 $search->setLimitOffset(0,0);
256 class SearchEngineDummy
{
257 function search( $term ) {