Add a new User::getDisplayName() to return the name that should be displayed in the...
authorDaniel Friesen <dantman@users.mediawiki.org>
Thu, 10 Nov 2011 06:55:21 +0000 (06:55 +0000)
committerDaniel Friesen <dantman@users.mediawiki.org>
Thu, 10 Nov 2011 06:55:21 +0000 (06:55 +0000)
Add a UserDisplayName hook to allow extensions to give custom display names for users.
Add a $wgRealNameInInterface to use the real name of a user as the display name.
To start of the first use of the display name functionality tweak SkinTemplate to declare the userdisplayname and use it inside of personal_urls.

RELEASE-NOTES-1.19
docs/hooks.txt
includes/DefaultSettings.php
includes/SkinTemplate.php
includes/User.php

index ddeefd0..3184c4a 100644 (file)
@@ -76,6 +76,8 @@ production.
 * The default user signature now contains a talk link in addition to the user link.
 * (bug 25306) Add link of old page title to MediaWiki:Delete_and_move_reason
 * Added hook BitmapHandlerCheckImageArea
+* (experimental) $wgRealNameInInterface can be enabled to display a user's
+  real name in some parts of the interface instead of a username.
 
 === Bug fixes in 1.19 ===
 * $wgUploadNavigationUrl should be used for file redlinks if
index e7f37d2..4b4c666 100644 (file)
@@ -2029,6 +2029,10 @@ your own hashing method
        hashing method
 &$hash: If the hook returns false, this String will be used as the hash
 
+'UserDisplayName': Called in User::getDisplayName()
+$user: The user object to fetch the display name for
+&$displayName: The display name. Will be null. Set to a name to override default name.
+
 'UserEffectiveGroups': Called in User::getEffectiveGroups()
 $user: User to get groups for
 &$groups: Current effective groups
index 41bb561..dccd91c 100644 (file)
@@ -2243,6 +2243,12 @@ $wgXhtmlNamespaces = array();
  */
 $wgShowIPinHeader      = true;
 
+/**
+ * Use a user's real name inside the user interface for display instead of the username
+ * (experimental)
+ */
+$wgRealNameInInterface = true;
+
 /**
  * Site notice shown at the top of each page
  *
index 70ca9f7..8015523 100644 (file)
@@ -175,6 +175,7 @@ class SkinTemplate extends Skin {
                $this->thisquery = wfArrayToCGI( $query );
                $this->loggedin = $user->isLoggedIn();
                $this->username = $user->getName();
+               $this->userdisplayname = $user->getDisplayName();
 
                if ( $user->isLoggedIn() || $this->showIPinHeader() ) {
                        $this->userpageUrlDetails = self::makeUrlDetails( $this->userpage );
@@ -305,6 +306,7 @@ class SkinTemplate extends Skin {
                $tpl->set( 'capitalizeallnouns', $this->getLang()->capitalizeAllNouns() ? ' capitalize-all-nouns' : '' );
                $tpl->set( 'showjumplinks', $user->getOption( 'showjumplinks' ) );
                $tpl->set( 'username', $user->isAnon() ? null : $this->username );
+               $tpl->set( 'userdisplayname', $user->isAnon() ? null : $this->userdisplayname );
                $tpl->setRef( 'userpage', $this->userpage );
                $tpl->setRef( 'userpageurl', $this->userpageUrlDetails['href'] );
                $tpl->set( 'userlang', $userlang );
@@ -558,7 +560,7 @@ class SkinTemplate extends Skin {
                $returnto = wfArrayToCGI( $a );
                if( $this->loggedin ) {
                        $personal_urls['userpage'] = array(
-                               'text' => $this->username,
+                               'text' => $this->userdisplayname,
                                'href' => &$this->userpageUrlDetails['href'],
                                'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
                                'active' => ( $this->userpageUrlDetails['href'] == $pageurl )
@@ -653,7 +655,7 @@ class SkinTemplate extends Skin {
                        if( $this->showIPinHeader() ) {
                                $href = &$this->userpageUrlDetails['href'];
                                $personal_urls['anonuserpage'] = array(
-                                       'text' => $this->username,
+                                       'text' => $this->userdisplayname,
                                        'href' => $href,
                                        'class' => $this->userpageUrlDetails['exists'] ? false : 'new',
                                        'active' => ( $pageurl == $href )
index 18d6561..074eeec 100644 (file)
@@ -199,7 +199,7 @@ class User {
         */
        var $mNewtalk, $mDatePreference, $mBlockedby, $mHash, $mRights,
                $mBlockreason, $mEffectiveGroups, $mImplicitGroups, $mFormerGroups, $mBlockedGlobally,
-               $mLocked, $mHideName, $mOptions;
+               $mLocked, $mHideName, $mOptions, $mDisplayName;
 
        /**
         * @var WebRequest
@@ -1191,6 +1191,7 @@ class User {
                $this->mEffectiveGroups = null;
                $this->mImplicitGroups = null;
                $this->mOptions = null;
+               $this->mDisplayName = null;
 
                if ( $reloadFrom ) {
                        $this->mLoadedItems = array();
@@ -2135,6 +2136,32 @@ class User {
                $this->mRealName = $str;
        }
 
+       /**
+        * Return the name of this user we should used to display in the user interface
+        * @return String The user's display name
+        */
+       public function getDisplayName() {
+               global $wgRealNameInInterface;
+               if ( is_null( $this->mDisplayName ) ) {
+                       $displayName = null;
+                       
+                       // Allow hooks to set a display name
+                       wfRunHooks( 'UserDisplayName', array( $this, &$displayName ) );
+
+                       if ( is_null( $displayName ) && $wgRealNameInInterface && $this->getRealName() ) {
+                               // If $wgRealNameInInterface is true use the real name as the display name if it's set
+                               $displayName = $this->getRealName();
+                       }
+                       
+                       if ( is_null( $displayName ) ) {
+                               $displayName = $this->getName();
+                       }
+
+                       $this->mDisplayName = $displayName;
+               }
+               return $this->mDisplayName;
+       }
+
        /**
         * Get the user's current setting for a given option.
         *