Jump to content

User:Otheus/Advanced Go

From Meta, a Wikimedia project coordination wiki
 
A proposal to move this page to MediaWiki.org was rejected.

Template:Extension

Advanced Go is a patch, applied to SearchEngine.php of MediaWiki 1.5.5. It should not break any existing functionality, and it should be just a tad faster. The patch was created so the admin can add namespaces to the Go search. DO this by adding a global array named $wGoNamespaces in LocalSettings.php. Example:

$wGoNamespaces = array( NS_HELP, NS_USER, NS_PROJECT );

Search Order

[edit]

The Go button on the search bar will check these, in order:

  • if exact match found in NS_MAIN
  • if NS_SPECIAL, unconditional match
  • if an IP address, go to Special:Contributions/$IP (I think)
  • if NS_USER, unconditional match
  • Loop and try near matches first in main Namespace, then wGoNamespaces():
    • exact (regretably necessary)
    • lowercase version of name
    • lowercase version of name, beginning of each word capitalized
    • all caps
    • language-specific lowercase-first/upercase-first (depending on wgCapitalLinks)
    • strip quotes (but non-recursively) The patched version did this recurisvely, but it's unclear if it did it correctly

Diff

[edit]
43a44,65
> 	function getNearMatchAux( $term ) {
> 		$title = Title::newFromText( $term );
> 
> 		/* the near-match case */
> 		if ( $title->exists() ) {
> 			return $title;
> 		}
> 
> 		/* search Go namespaces found in $wgGoNamespaces */
> 		global $wgGoNamespaces;
> 		if (is_null($wgGoNamespaces) || !is_array($wgGoNamespaces)) 
> 			return NULL;
> 
> 		foreach($wgGoNamespaces as $ns) {
> 			$title = Title::newFromText( $term, $ns );
> 			if ($title->exists() ) {
> 				return $title;
> 			}
> 		}
> 		return NULL;
> 	}
> 
54d75
< 		# Exact match? No need to look further.
56,57c77
< 		if (is_null($title))
< 			return NULL;
---
> 		if (is_null($title)) { return NULL; }
59,61c79,80
< 		if ( $title->getNamespace() == NS_SPECIAL || $title->exists() ) {
< 			return $title;
< 		}
---
> 		# exact match 
> 		if ( $title->exists()) { return $title; }
63,68c82,83
< 		# Now try all lower case (i.e. first letter capitalized)
< 		#
< 		$title = Title::newFromText( strtolower( $term ) );
< 		if ( $title->exists() ) {
< 			return $title;
< 		}
---
> 		# special page
> 		if ( $title->getNamespace() == NS_SPECIAL) { return $title; }
70,74c85,90
< 		# Now try capitalized string
< 		#
< 		$title = Title::newFromText( ucwords( strtolower( $term ) ) );
< 		if ( $title->exists() ) {
< 			return $title;
---
> 		# Entering an IP address goes to the contributions page
> 		#  Note: the code that was here before didn't make any sense. 
> 		#  Why check ns=NS_USER if in the end, you only look at the IP?
> 		if ( User::isIP( trim( $term ) ) ) {
> 			return Title::makeTitle( NS_SPECIAL, "Contributions/" . 
> 				$title->getDbkey() );
77,82c93,94
< 		# Now try all upper case
< 		#
< 		$title = Title::newFromText( strtoupper( $term ) );
< 		if ( $title->exists() ) {
< 			return $title;
< 		}
---
> 		# Entering a user goes to the user page whether it's there or not
> 		if ( $title->getNamespace() == NS_USER ) { return $title; }
85,97d96
< 		if( !$wgCapitalLinks ) {
< 			// Catch differs-by-first-letter-case-only
< 			$title = Title::newFromText( $wgContLang->ucfirst( $term ) );
< 			if ( $title->exists() ) {
< 				return $title;
< 			}
< 			$title = Title::newFromText( $wgContLang->lcfirst( $term ) );
< 			if ( $title->exists() ) {
< 				return $title;
< 			}
< 		}
< 
< 		$title = Title::newFromText( $term );
99,103c98,126
< 		# Entering an IP address goes to the contributions page
< 		if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
< 			|| User::isIP( trim( $term ) ) ) {
< 			return Title::makeTitle( NS_SPECIAL, "Contributions/" . $title->getDbkey() );
< 		}
---
> 		#
> 		#  Do Near Matches by permuting over the various name possibilities
> 		# 
> 		#  Nb: Otheus: I think this is more readable to a logician.
> 		#
> 		($title = 
> 			# try other namespaces (based on global prefs)
> 			SearchEngine::getNearMatchAux( $term ) 
> 			) or
> 		($title = 
> 			# Now try all lower case (i.e. first letter capitalized)
> 			SearchEngine::getNearMatchAux( strtolower( $term ) ) )  or
> 		($title = 
> 			# Now try capitalized string
> 			SearchEngine::getNearMatchAux( ucwords( strtolower( $term ) ) ) ) or 
> 		($title = 
> 			# Now try all upper case
> 			SearchEngine::getNearMatchAux( strtoupper( $term ) ) ) or 
> 		($title = 
> 			# Try language-specific up/lower case
> 			SearchEngine::getNearMatchAux(  $wgCapitalLinks 
> 			  	?  $wgContLang->lcfirst( $term ) 
> 				:  $wgContLang->ucfirst( $term ) 
> 			) ) or
> 		($title = 
> 			( preg_match( '/^"([^"]+)"$/', $term, $matches )  
> 				?  SearchEngine::getNearMatchAux(  $matches[1] )
> 				:  NULL 
> 			) ) or
104a128,129
> 			# .. other attempts here
> 		1;
106,107d130
< 		# Entering a user goes to the user page whether it's there or not
< 		if ( $title->getNamespace() == NS_USER ) {
111,118d133
< 		# Quoted term? Try without the quotes...
< 		if( preg_match( '/^"([^"]+)"$/', $term, $matches ) ) {
< 			return SearchEngine::getNearMatch( $matches[1] );
< 		}
< 		
< 		return NULL;
< 	}
<