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 cur_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 * Construct the full SQL query to do the search.
187 * The guts shoulds be constructed in queryMain()
188 * @param string $filteredTerm
189 * @param bool $fulltext
192 function getQuery( $filteredTerm, $fulltext ) {
193 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
194 $this->queryRedirect() . ' ' .
195 $this->queryNamespaces() . ' ' .
202 class SearchEngineDummy
{
203 function search( $term ) {