f802aebaf86ec49f4d93e81304ddeab19b918e89
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 # Eliminate Blanks at start
51 $term = ereg_replace('[[:blank:]]*', '', $term);
53 # Exact match? No need to look further.
54 $title = Title
::newFromText( $term );
55 if ( $title->getNamespace() == NS_SPECIAL ||
0 != $title->getArticleID() ) {
59 # Now try all lower case (i.e. first letter capitalized)
61 $title = Title
::newFromText( strtolower( $term ) );
62 if ( 0 != $title->getArticleID() ) {
66 # Now try capitalized string
68 $title = Title
::newFromText( ucwords( strtolower( $term ) ) );
69 if ( 0 != $title->getArticleID() ) {
73 # Now try all upper case
75 $title = Title
::newFromText( strtoupper( $term ) );
76 if ( 0 != $title->getArticleID() ) {
80 # Entering an IP address goes to the contributions page
81 if ( preg_match( '/^(user:)?\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', strtolower($term) ) ) {
82 $title = Title
::makeTitle( NS_SPECIAL
, "Contributions/" . $term );
86 # Entering a user goes to the user page whether it's there or not
87 if ( preg_match( '/^user:/', strtolower($term) ) ) {
88 if (User
::idFromName($term)) {
89 $title = Title
::newFromURL( $term );
97 function legalSearchChars() {
98 return "A-Za-z_'0-9\\x80-\\xFF\\-";
102 * Set the maximum number of results to return
103 * and how many to skip before returning the first.
109 function setLimitOffset( $limit, $offset = 0 ) {
110 $this->limit
= IntVal( $limit );
111 $this->offset
= IntVal( $offset );
115 * Set which namespaces the search should include.
116 * Give an array of namespace index numbers.
118 * @param array $namespaces
121 function setNamespaces( $namespaces ) {
122 $this->namespaces
= $namespaces;
126 * Make a list of searchable namespaces and their canonical names.
130 function searchableNamespaces() {
133 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
134 if( $ns >= NS_MAIN
) {
142 * Fetch an array of regular expression fragments for matching
143 * the search terms as parsed by this engine in a text extract.
148 function termMatches() {
149 return $this->searchTerms
;
153 * Return a 'cleaned up' search string
158 function filter( $text ) {
159 $lc = $this->legalSearchChars();
160 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
164 * Return a partial WHERE clause to exclude redirects, if so set
168 function queryRedirect() {
169 if( $this->showRedirects
) {
170 return 'AND cur_is_redirect=0';
177 * Return a partial WHERE clause to limit the search to the given namespaces
181 function queryNamespaces() {
182 $namespaces = implode( ',', $this->namespaces
);
183 if ($namespaces == '') {
186 return 'AND page_namespace IN (' . $namespaces . ')';
190 * Return a LIMIT clause to limit results on the query.
194 function queryLimit() {
195 return $this->db
->limitResult( $this->limit
, $this->offset
);
199 * Does not do anything for generic search engine
200 * subclasses may define this though
204 function queryRanking($filteredTerm,$fulltext) {
209 * Construct the full SQL query to do the search.
210 * The guts shoulds be constructed in queryMain()
211 * @param string $filteredTerm
212 * @param bool $fulltext
215 function getQuery( $filteredTerm, $fulltext ) {
216 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
217 $this->queryRedirect() . ' ' .
218 $this->queryNamespaces() . ' ' .
219 $this->queryRanking($filteredTerm, $fulltext) . ' ' .
224 * Load up the appropriate search engine class for the currently
225 * active database backend, and return a configured instance.
227 * @return SearchEngine
231 global $wgDBtype, $wgDBmysql4, $wgSearchType;
232 if( $wgDBtype == 'mysql' ) {
234 $class = 'SearchMySQL4';
235 require_once( 'SearchMySQL4.php' );
237 $class = 'SearchMysql3';
238 require_once( 'SearchMySQL3.php' );
240 } else if ( $wgDBtype == 'PostgreSQL' ) {
241 $class = 'SearchTsearch2';
242 require_once( 'SearchTsearch2.php' );
244 $class = 'SearchEngineDummy';
246 $search = new $class( wfGetDB( DB_SLAVE
) );
247 $search->setLimitOffset(0,0);
257 class SearchEngineDummy
{
258 function search( $term ) {