6 var $searchTerms = array();
7 var $namespaces = array( 0 );
8 var $showRedirects = false;
11 * Perform a full text search query and return a result set.
13 * @param string $term - Raw search term
14 * @param array $namespaces - List of namespaces to search
15 * @return ResultWrapper
18 function searchText( $term ) {
19 return $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), true ) ) );
23 * Perform a title-only search query and return a result set.
25 * @param string $term - Raw search term
26 * @param array $namespaces - List of namespaces to search
27 * @return ResultWrapper
30 function searchTitle( $term ) {
31 return $this->db
->resultObject( $this->db
->query( $this->getQuery( $this->filter( $term ), false ) ) );
35 * If an exact title match can be find, or a very slightly close match,
36 * return the title. If no match, returns NULL.
42 function getNearMatch( $term ) {
43 # Exact match? No need to look further.
44 $title = Title
::newFromText( $term );
45 if ( $title->getNamespace() == NS_SPECIAL ||
0 != $title->getArticleID() ) {
49 # Now try all lower case (i.e. first letter capitalized)
51 $title = Title
::newFromText( strtolower( $term ) );
52 if ( 0 != $title->getArticleID() ) {
56 # Now try capitalized string
58 $title = Title
::newFromText( ucwords( strtolower( $term ) ) );
59 if ( 0 != $title->getArticleID() ) {
63 # Now try all upper case
65 $title = Title
::newFromText( strtoupper( $term ) );
66 if ( 0 != $title->getArticleID() ) {
70 # Entering an IP address goes to the contributions page
71 if ( preg_match( '/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/', $term ) ) {
72 $title = Title
::makeTitle( NS_SPECIAL
, "Contributions/" . $term );
79 function legalSearchChars() {
80 return "A-Za-z_'0-9\\x80-\\xFF\\-";
84 * Set the maximum number of results to return
85 * and how many to skip before returning the first.
91 function setLimitOffset( $limit, $offset = 0 ) {
92 $this->limit
= IntVal( $limit );
93 $this->offset
= IntVal( $offset );
97 * Set which namespaces the search should include.
98 * Give an array of namespace index numbers.
100 * @param array $namespaces
103 function setNamespaces( $namespaces ) {
104 $this->namespaces
= $namespaces;
108 * Make a list of searchable namespaces and their canonical names.
112 function searchableNamespaces() {
115 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
124 * Fetch an array of regular expression fragments for matching
125 * the search terms as parsed by this engine in a text extract.
130 function termMatches() {
131 return $this->searchTerms
;
135 * Return a 'cleaned up' search string
140 function filter( $text ) {
141 $lc = $this->legalSearchChars();
142 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
146 * Return a partial WHERE clause to exclude redirects, if so set
150 function queryRedirect() {
151 if( $this->showRedirects
) {
152 return 'AND cur_is_redirect=0';
159 * Return a partial WHERE clause to limit the search to the given namespaces
163 function queryNamespaces() {
164 $namespaces = implode( ',', $this->namespaces
);
165 if ($namespaces == '') {
168 return 'AND cur_namespace IN (' . $namespaces . ')';
172 * Return a LIMIT clause to limit results on the query.
176 function queryLimit() {
177 return $this->db
->limitResult( $this->limit
, $this->offset
);
181 * Construct the full SQL query to do the search.
182 * The guts shoulds be constructed in queryMain()
183 * @param string $filteredTerm
184 * @param bool $fulltext
187 function getQuery( $filteredTerm, $fulltext ) {
188 return $this->queryMain( $filteredTerm, $fulltext ) . ' ' .
189 $this->queryRedirect() . ' ' .
190 $this->queryNamespaces() . ' ' .
196 class SearchEngineDummy
{
197 function search( $term ) {