3 * Contain a class for special pages
14 var $searchTerms = array();
15 var $namespaces = array( NS_MAIN
);
16 var $showRedirects = false;
19 * Perform a full text search query and return a result set.
20 * If title searches are not supported or disabled, return null.
22 * @param string $term - Raw search term
23 * @return SearchResultSet
27 function searchText( $term ) {
32 * Perform a title-only search query and return a result set.
33 * If title searches are not supported or disabled, return null.
35 * @param string $term - Raw search term
36 * @return SearchResultSet
40 function searchTitle( $term ) {
45 * If an exact title match can be find, or a very slightly close match,
46 * return the title. If no match, returns NULL.
53 function getNearMatch( $term ) {
54 # Exact match? No need to look further.
55 $title = Title
::newFromText( $term );
59 if ( $title->getNamespace() == NS_SPECIAL ||
$title->exists() ) {
63 # Now try all lower case (i.e. first letter capitalized)
65 $title = Title
::newFromText( strtolower( $term ) );
66 if ( $title->exists() ) {
70 # Now try capitalized string
72 $title = Title
::newFromText( ucwords( strtolower( $term ) ) );
73 if ( $title->exists() ) {
77 # Now try all upper case
79 $title = Title
::newFromText( strtoupper( $term ) );
80 if ( $title->exists() ) {
84 # Now try Word-Caps-Breaking-At-Word-Breaks, for hyphenated names etc
85 $title = Title
::newFromText( preg_replace_callback(
86 '/\b([\w\x80-\xff]+)\b/',
87 create_function( '$matches', '
89 return $wgContLang->ucfirst($matches[1]);
92 if ( $title->exists() ) {
96 global $wgCapitalLinks, $wgContLang;
97 if( !$wgCapitalLinks ) {
98 // Catch differs-by-first-letter-case-only
99 $title = Title
::newFromText( $wgContLang->ucfirst( $term ) );
100 if ( $title->exists() ) {
103 $title = Title
::newFromText( $wgContLang->lcfirst( $term ) );
104 if ( $title->exists() ) {
109 $title = Title
::newFromText( $term );
111 # Entering an IP address goes to the contributions page
112 if ( ( $title->getNamespace() == NS_USER
&& User
::isIP($title->getText() ) )
113 || User
::isIP( trim( $term ) ) ) {
114 return Title
::makeTitle( NS_SPECIAL
, "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 # Quoted term? Try without the quotes...
124 if( preg_match( '/^"([^"]+)"$/', $term, $matches ) ) {
125 return SearchEngine
::getNearMatch( $matches[1] );
131 function legalSearchChars() {
132 return "A-Za-z_'0-9\\x80-\\xFF\\-";
136 * Set the maximum number of results to return
137 * and how many to skip before returning the first.
143 function setLimitOffset( $limit, $offset = 0 ) {
144 $this->limit
= intval( $limit );
145 $this->offset
= intval( $offset );
149 * Set which namespaces the search should include.
150 * Give an array of namespace index numbers.
152 * @param array $namespaces
155 function setNamespaces( $namespaces ) {
156 $this->namespaces
= $namespaces;
160 * Make a list of searchable namespaces and their canonical names.
164 function searchableNamespaces() {
167 foreach( $wgContLang->getNamespaces() as $ns => $name ) {
168 if( $ns >= NS_MAIN
) {
176 * Return a 'cleaned up' search string
181 function filter( $text ) {
182 $lc = $this->legalSearchChars();
183 return trim( preg_replace( "/[^{$lc}]/", " ", $text ) );
186 * Load up the appropriate search engine class for the currently
187 * active database backend, and return a configured instance.
189 * @return SearchEngine
193 global $wgDBtype, $wgSearchType;
194 if( $wgSearchType ) {
195 $class = $wgSearchType;
196 } elseif( $wgDBtype == 'mysql' ) {
197 $class = 'SearchMySQL4';
198 require_once( 'SearchMySQL4.php' );
199 } else if ( $wgDBtype == 'PostgreSQL' ) {
200 $class = 'SearchTsearch2';
201 require_once( 'SearchTsearch2.php' );
203 $class = 'SearchEngineDummy';
205 $search = new $class( wfGetDB( DB_SLAVE
) );
206 $search->setLimitOffset(0,0);
211 * Create or update the search index record for the given page.
212 * Title and text should be pre-processed.
215 * @param string $title
216 * @param string $text
219 function update( $id, $title, $text ) {
224 * Update a search index record's title only.
225 * Title should be pre-processed.
228 * @param string $title
231 function updateTitle( $id, $title ) {
236 /** @package MediaWiki */
237 class SearchResultSet
{
239 * Fetch an array of regular expression fragments for matching
240 * the search terms as parsed by this engine in a text extract.
246 function termMatches() {
255 * Return true if results are included in this result set.
259 function hasResults() {
264 * Some search modes return a total hit count for the query
265 * in the entire article database. This may include pages
266 * in namespaces that would not be matched on the given
269 * Return null if no total hits number is supported.
274 function getTotalHits() {
279 * Some search modes return a suggested alternate term if there are
280 * no exact hits. Returns true if there is one on this set.
285 function hasSuggestion() {
290 * Some search modes return a suggested alternate term if there are
291 * no exact hits. Check hasSuggestion() first.
296 function getSuggestion() {
301 * Fetches next search result, or false.
302 * @return SearchResult
311 /** @package MediaWiki */
313 function SearchResult( $row ) {
314 $this->mTitle
= Title
::makeTitle( $row->page_namespace
, $row->page_title
);
321 function getTitle() {
322 return $this->mTitle
;
326 * @return double or null if not supported
328 function getScore() {
336 class SearchEngineDummy
{
337 function search( $term ) {
340 function setLimitOffset($l, $o) {}
341 function legalSearchChars() {}
343 function setnamespaces() {}
344 function searchtitle() {}
345 function searchtext() {}