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.
46 public static function getNearMatch( $searchterm ) {
49 $allSearchTerms = array($searchterm);
51 if($wgContLang->hasVariants()){
52 $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
55 foreach($allSearchTerms as $term){
57 # Exact match? No need to look further.
58 $title = Title
::newFromText( $term );
62 if ( $title->getNamespace() == NS_SPECIAL ||
$title->exists() ) {
66 # Now try all lower case (i.e. first letter capitalized)
68 $title = Title
::newFromText( $wgContLang->lc( $term ) );
69 if ( $title->exists() ) {
73 # Now try capitalized string
75 $title = Title
::newFromText( $wgContLang->ucwords( $term ) );
76 if ( $title->exists() ) {
80 # Now try all upper case
82 $title = Title
::newFromText( $wgContLang->uc( $term ) );
83 if ( $title->exists() ) {
87 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
88 $title = Title
::newFromText( $wgContLang->ucwordbreaks($term) );
89 if ( $title->exists() ) {
93 global $wgCapitalLinks, $wgContLang;
94 if( !$wgCapitalLinks ) {
95 // Catch differs-by-first-letter-case-only
96 $title = Title
::newFromText( $wgContLang->ucfirst( $term ) );
97 if ( $title->exists() ) {
100 $title = Title
::newFromText( $wgContLang->lcfirst( $term ) );
101 if ( $title->exists() ) {
107 $title = Title
::newFromText( $searchterm );
109 # Entering an IP address goes to the contributions page
110 if ( ( $title->getNamespace() == NS_USER
&& User
::isIP($title->getText() ) )
111 || User
::isIP( trim( $searchterm ) ) ) {
112 return SpecialPage
::getTitleFor( 'Contributions', $title->getDbkey() );
116 # Entering a user goes to the user page whether it's there or not
117 if ( $title->getNamespace() == NS_USER
) {
121 # Go to images that exist even if there's no local page.
122 # There may have been a funny upload, or it may be on a shared
123 # file repository such as Wikimedia Commons.
124 if( $title->getNamespace() == NS_IMAGE
) {
125 $image = new Image( $title );
126 if( $image->exists() ) {
131 # MediaWiki namespace? Page may be "implied" if not customized.
132 # Just return it, with caps forced as the message system likes it.
133 if( $title->getNamespace() == NS_MEDIAWIKI
) {
134 return Title
::makeTitle( NS_MEDIAWIKI
, $wgContLang->ucfirst( $title->getText() ) );
137 # Quoted term? Try without the quotes...
139 if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
140 return SearchEngine
::getNearMatch( $matches[1] );
146 public static function legalSearchChars() {
147 return "A-Za-z_'0-9\\x80-\\xFF\\-";
151 * Set the maximum number of results to return
152 * and how many to skip before returning the first.
158 function setLimitOffset( $limit, $offset = 0 ) {
159 $this->limit
= intval( $limit );
160 $this->offset
= intval( $offset );
164 * Set which namespaces the search should include.
165 * Give an array of namespace index numbers.
167 * @param array $namespaces
170 function setNamespaces( $namespaces ) {
171 $this->namespaces
= $namespaces;
175 * Make a list of searchable namespaces and their canonical names.
178 public static function searchableNamespaces() {
181 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
182 if( $ns >= NS_MAIN
) {
190 * Return a 'cleaned up' search string
195 function filter( $text ) {
196 $lc = $this->legalSearchChars();
197 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
200 * Load up the appropriate search engine class for the currently
201 * active database backend, and return a configured instance.
203 * @return SearchEngine
205 public static function create() {
206 global $wgDBtype, $wgSearchType;
207 if( $wgSearchType ) {
208 $class = $wgSearchType;
209 } elseif( $wgDBtype == 'mysql' ) {
210 $class = 'SearchMySQL4';
211 } else if ( $wgDBtype == 'postgres' ) {
212 $class = 'SearchPostgres';
213 } else if ( $wgDBtype == 'oracle' ) {
214 $class = 'SearchOracle';
216 $class = 'SearchEngineDummy';
218 $search = new $class( wfGetDB( DB_SLAVE
) );
219 $search->setLimitOffset(0,0);
224 * Create or update the search index record for the given page.
225 * Title and text should be pre-processed.
228 * @param string $title
229 * @param string $text
232 function update( $id, $title, $text ) {
237 * Update a search index record's title only.
238 * Title should be pre-processed.
241 * @param string $title
244 function updateTitle( $id, $title ) {
253 class SearchResultSet
{
255 * Fetch an array of regular expression fragments for matching
256 * the search terms as parsed by this engine in a text extract.
262 function termMatches() {
271 * Return true if results are included in this result set.
275 function hasResults() {
280 * Some search modes return a total hit count for the query
281 * in the entire article database. This may include pages
282 * in namespaces that would not be matched on the given
285 * Return null if no total hits number is supported.
290 function getTotalHits() {
295 * Some search modes return a suggested alternate term if there are
296 * no exact hits. Returns true if there is one on this set.
301 function hasSuggestion() {
306 * Some search modes return a suggested alternate term if there are
307 * no exact hits. Check hasSuggestion() first.
312 function getSuggestion() {
317 * Fetches next search result, or false.
318 * @return SearchResult
332 function SearchResult( $row ) {
333 $this->mTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
340 function getTitle() {
341 return $this->mTitle
;
345 * @return double or null if not supported
347 function getScore() {
355 class SearchEngineDummy
{
356 function search( $term ) {
359 function setLimitOffset($l, $o) {}
360 function legalSearchChars() {}
362 function setnamespaces() {}
363 function searchtitle() {}
364 function searchtext() {}