r107130 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107129‎ | r107130 | r107131 >
Date:01:37, 23 December 2011
Author:laner
Status:deferred
Tags:ldap 
Comment:
Add memcache support for getCanonicalName, since this function is very expensive without it.
Modified paths:
  • /trunk/extensions/LdapAuthentication/LdapAuthentication.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LdapAuthentication/LdapAuthentication.php
@@ -955,15 +955,25 @@
956956 * @return string
957957 */
958958 public function getCanonicalName( $username ) {
959 - global $wgLDAPUseFetchedUsername;
 959+ global $wgLDAPUseFetchedUsername, $wgMemc;
 960+
960961 $this->printDebug( "Entering getCanonicalName", NONSENSITIVE );
961962
 963+ $key = wfMemcKey( 'ldapauthentication', 'canonicalname', $username );
 964+ $canonicalname = $username;
962965 if ( $username != '' ) {
963966 $this->printDebug( "Username isn't empty.", NONSENSITIVE );
964967
965968 # Fetch username, so that we can possibly use it.
966969 # Only do it if we haven't already fetched it.
967 - if ( !$this->userdn ) {
 970+ $userInfo = $wgMemc->get( $key );
 971+ if ( is_array( $userInfo ) ) {
 972+ $this->printDebug( "Fetched userInfo from memcache.", NONSENSITIVE );
 973+ if ( $userInfo["username"] == $username ) {
 974+ $this->printDebug( "Username matched a key in memcache, using the fetched name: " . $userInfo["canonicalname"] );
 975+ return $userInfo["canonicalname"];
 976+ }
 977+ } else {
968978 $this->connect();
969979 if ( $this->ldapconn ) {
970980 $this->printDebug( "Successfully connected", NONSENSITIVE );
@@ -977,29 +987,31 @@
978988 // We want to use the username returned by LDAP
979989 // if it exists
980990 if ( $this->LDAPUsername != '' ) {
981 - $username = $this->LDAPUsername;
 991+ $canonicalname = $this->LDAPUsername;
982992 if ( isset( $wgLDAPUseFetchedUsername[$_SESSION['wsDomain']] ) && $wgLDAPUseFetchedUsername[$_SESSION['wsDomain']] ) {
983 - $username[0] = strtoupper( $username[0] );
984 - return $username;
 993+ $canonicalname[0] = strtoupper( $canonicalname[0] );
 994+ $wgMemc->set( $key, array( "username" => $username, "canonicalname" => $canonicalname ), 3600 * 24 );
 995+ return $canonicalname;
985996 }
986 - $this->printDebug( "Using LDAPUsername: $username", NONSENSITIVE );
 997+ $this->printDebug( "Using LDAPUsername: $canonicalname", NONSENSITIVE );
987998 }
988999
9891000 if ( isset( $_SESSION['wsDomain'] ) && 'local' != $_SESSION['wsDomain'] ) {
9901001 // Change username to lowercase so that multiple user accounts
9911002 // won't be created for the same user.
9921003 // But don't do it for the local domain!
993 - $username = strtolower( $username );
 1004+ $canonicalname = strtolower( $username );
9941005 }
9951006
9961007 // The wiki considers an all lowercase name to be invalid; need to
9971008 // uppercase the first letter
998 - $username[0] = strtoupper( $username[0] );
 1009+ $canonicalname[0] = strtoupper( $canonicalname[0] );
9991010 }
10001011
1001 - $this->printDebug( "Munged username: $username", NONSENSITIVE );
 1012+ $this->printDebug( "Munged username: $canonicalname", NONSENSITIVE );
10021013
1003 - return $username;
 1014+ $wgMemc->set( $key, array( "username" => $username, "canonicalname" => $canonicalname ), 3600 * 24 );
 1015+ return $canonicalname;
10041016 }
10051017
10061018 /**

Status & tagging log