3 * Contain a class for special pages
9 var $searchTerms = array();
10 var $namespaces = array( NS_MAIN
);
11 var $showRedirects = false;
14 * Perform a full text search query and return a result set.
15 * If title searches are not supported or disabled, return null.
17 * @param string $term - Raw search term
18 * @return SearchResultSet
22 function searchText( $term ) {
27 * Perform a title-only search query and return a result set.
28 * If title searches are not supported or disabled, return null.
30 * @param string $term - Raw search term
31 * @return SearchResultSet
35 function searchTitle( $term ) {
40 * If an exact title match can be find, or a very slightly close match,
41 * return the title. If no match, returns NULL.
48 function getNearMatch( $searchterm ) {
51 $allSearchTerms = array($searchterm);
53 if($wgContLang->hasVariants()){
54 $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
57 foreach($allSearchTerms as $term){
59 # Exact match? No need to look further.
60 $title = Title
::newFromText( $term );
64 if ( $title->getNamespace() == NS_SPECIAL ||
$title->exists() ) {
68 # Now try all lower case (i.e. first letter capitalized)
70 $title = Title
::newFromText( $wgContLang->lc( $term ) );
71 if ( $title->exists() ) {
75 # Now try capitalized string
77 $title = Title
::newFromText( $wgContLang->ucwords( $term ) );
78 if ( $title->exists() ) {
82 # Now try all upper case
84 $title = Title
::newFromText( $wgContLang->uc( $term ) );
85 if ( $title->exists() ) {
89 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
90 $title = Title
::newFromText( $wgContLang->ucwordbreaks($term) );
91 if ( $title->exists() ) {
95 global $wgCapitalLinks, $wgContLang;
96 if( !$wgCapitalLinks ) {
97 // Catch differs-by-first-letter-case-only
98 $title = Title
::newFromText( $wgContLang->ucfirst( $term ) );
99 if ( $title->exists() ) {
102 $title = Title
::newFromText( $wgContLang->lcfirst( $term ) );
103 if ( $title->exists() ) {
109 $title = Title
::newFromText( $searchterm );
111 # Entering an IP address goes to the contributions page
112 if ( ( $title->getNamespace() == NS_USER
&& User
::isIP($title->getText() ) )
113 || User
::isIP( trim( $searchterm ) ) ) {
114 return SpecialPage
::getTitleFor( 'Contributions', $title->getDbkey() );
118 # Entering a user goes to the user page whether it's there or not
119 if ( $title->getNamespace() == NS_USER
) {
123 # Go to images that exist even if there's no local page.
124 # There may have been a funny upload, or it may be on a shared
125 # file repository such as Wikimedia Commons.
126 if( $title->getNamespace() == NS_IMAGE
) {
127 $image = new Image( $title );
128 if( $image->exists() ) {
133 # MediaWiki namespace? Page may be "implied" if not customized.
134 # Just return it, with caps forced as the message system likes it.
135 if( $title->getNamespace() == NS_MEDIAWIKI
) {
136 return Title
::makeTitle( NS_MEDIAWIKI
, $wgContLang->ucfirst( $title->getText() ) );
139 # Quoted term? Try without the quotes...
141 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
142 return SearchEngine
::getNearMatch( $matches[1] );
148 public static function legalSearchChars() {
149 return "A-Za-z_'0-9\\x80-\\xFF\\-";
153 * Set the maximum number of results to return
154 * and how many to skip before returning the first.
160 function setLimitOffset( $limit, $offset = 0 ) {
161 $this->limit
= intval( $limit );
162 $this->offset
= intval( $offset );
166 * Set which namespaces the search should include.
167 * Give an array of namespace index numbers.
169 * @param array $namespaces
172 function setNamespaces( $namespaces ) {
173 $this->namespaces
= $namespaces;
177 * Make a list of searchable namespaces and their canonical names.
181 function searchableNamespaces() {
184 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
185 if( $ns >= NS_MAIN
) {
193 * Return a 'cleaned up' search string
198 function filter( $text ) {
199 $lc = $this->legalSearchChars();
200 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
203 * Load up the appropriate search engine class for the currently
204 * active database backend, and return a configured instance.
206 * @return SearchEngine
208 public static function create() {
209 global $wgDBtype, $wgSearchType;
210 if( $wgSearchType ) {
211 $class = $wgSearchType;
212 } elseif( $wgDBtype == 'mysql' ) {
213 $class = 'SearchMySQL4';
214 } else if ( $wgDBtype == 'postgres' ) {
215 $class = 'SearchPostgres';
216 } else if ( $wgDBtype == 'oracle' ) {
217 $class = 'SearchOracle';
219 $class = 'SearchEngineDummy';
221 $search = new $class( wfGetDB( DB_SLAVE
) );
222 $search->setLimitOffset(0,0);
227 * Create or update the search index record for the given page.
228 * Title and text should be pre-processed.
231 * @param string $title
232 * @param string $text
235 function update( $id, $title, $text ) {
240 * Update a search index record's title only.
241 * Title should be pre-processed.
244 * @param string $title
247 function updateTitle( $id, $title ) {
256 class SearchResultSet
{
258 * Fetch an array of regular expression fragments for matching
259 * the search terms as parsed by this engine in a text extract.
265 function termMatches() {
274 * Return true if results are included in this result set.
278 function hasResults() {
283 * Some search modes return a total hit count for the query
284 * in the entire article database. This may include pages
285 * in namespaces that would not be matched on the given
288 * Return null if no total hits number is supported.
293 function getTotalHits() {
298 * Some search modes return a suggested alternate term if there are
299 * no exact hits. Returns true if there is one on this set.
304 function hasSuggestion() {
309 * Some search modes return a suggested alternate term if there are
310 * no exact hits. Check hasSuggestion() first.
315 function getSuggestion() {
320 * Fetches next search result, or false.
321 * @return SearchResult
335 function SearchResult( $row ) {
336 $this->mTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
343 function getTitle() {
344 return $this->mTitle
;
348 * @return double or null if not supported
350 function getScore() {
358 class SearchEngineDummy
{
359 function search( $term ) {
362 function setLimitOffset($l, $o) {}
363 function legalSearchChars() {}
365 function setnamespaces() {}
366 function searchtitle() {}
367 function searchtext() {}