r98539 - make a note of change in comments
[lhc/web/wiklou.git] / includes / User.php
index b961285..ab0a35b 100644 (file)
@@ -115,7 +115,6 @@ class User {
                'deletedhistory',
                'deletedtext',
                'deleterevision',
-               'disableaccount',
                'edit',
                'editinterface',
                'editusercssjs', #deprecated
@@ -143,13 +142,11 @@ class User {
                'reupload',
                'reupload-shared',
                'rollback',
-               'selenium',
                'sendemail',
                'siteadmin',
                'suppressionlog',
                'suppressredirect',
                'suppressrevision',
-               'trackback',
                'unblockself',
                'undelete',
                'unwatchedpages',
@@ -200,7 +197,7 @@ class User {
         */
        var $mNewtalk, $mDatePreference, $mBlockedby, $mHash, $mRights,
                $mBlockreason, $mEffectiveGroups, $mImplicitGroups, $mFormerGroups, $mBlockedGlobally,
-               $mLocked, $mHideName, $mOptions;
+               $mLocked, $mHideName, $mOptions, $mDisplayName;
 
        /**
         * @var WebRequest
@@ -394,7 +391,7 @@ class User {
         * If the code is invalid or has expired, returns NULL.
         *
         * @param $code String Confirmation code
-        * @return User
+        * @return User object, or null
         */
        public static function newFromConfirmationCode( $code ) {
                $dbr = wfGetDB( DB_SLAVE );
@@ -415,7 +412,7 @@ class User {
         *
         * @param $request WebRequest object to use; $wgRequest will be used if
         *        ommited.
-        * @return User
+        * @return User object
         */
        public static function newFromSession( WebRequest $request = null ) {
                $user = new User;
@@ -448,9 +445,9 @@ class User {
        /**
         * Get the username corresponding to a given user ID
         * @param $id Int User ID
-        * @return String The corresponding username
+        * @return String|false The corresponding username
         */
-       static function whoIs( $id ) {
+       public static function whoIs( $id ) {
                $dbr = wfGetDB( DB_SLAVE );
                return $dbr->selectField( 'user', 'user_name', array( 'user_id' => $id ), __METHOD__ );
        }
@@ -459,7 +456,7 @@ class User {
         * Get the real name of a user given their user ID
         *
         * @param $id Int User ID
-        * @return String The corresponding user's real name
+        * @return String|false The corresponding user's real name
         */
        public static function whoIsReal( $id ) {
                $dbr = wfGetDB( DB_SLAVE );
@@ -551,6 +548,7 @@ class User {
                        return false;
                }
 
+
                // Ensure that the name can't be misresolved as a different title,
                // such as with extra namespace keys at the start.
                $parsed = Title::newFromText( $name );
@@ -732,6 +730,7 @@ class User {
         * @deprecated since 1.18 call Sanitizer::isValidEmail() directly
         */
        public static function isValidEmailAddr( $addr ) {
+               wfDeprecated( __METHOD__, '1.18' );
                return Sanitizer::validateEmail( $addr );
        }
 
@@ -1192,6 +1191,7 @@ class User {
                $this->mEffectiveGroups = null;
                $this->mImplicitGroups = null;
                $this->mOptions = null;
+               $this->mDisplayName = null;
 
                if ( $reloadFrom ) {
                        $this->mLoadedItems = array();
@@ -1218,6 +1218,12 @@ class User {
                }
                $defOpt['skin'] = $wgDefaultSkin;
 
+               // FIXME: Ideally we'd cache the results of this function so the hook is only run once,
+               // but that breaks the parser tests because they rely on being able to change $wgContLang
+               // mid-request and see that change reflected in the return value of this function.
+               // Which is insane and would never happen during normal MW operation, but is also not
+               // likely to get fixed unless and until we context-ify everything.
+               // See also https://www.mediawiki.org/wiki/Special:Code/MediaWiki/101488#c25275
                wfRunHooks( 'UserGetDefaultOptions', array( &$defOpt ) );
 
                return $defOpt;
@@ -1284,9 +1290,6 @@ class User {
                        $this->mBlockreason = $this->mBlock->mReason;
                        $this->mHideName = $this->mBlock->mHideName;
                        $this->mAllowUsertalk = !$this->mBlock->prevents( 'editownusertalk' );
-                       if ( $this->isLoggedIn() && $wgUser->getID() == $this->getID() ) {
-                               $this->spreadBlock();
-                       }
                }
 
                # Proxy blocking
@@ -2108,7 +2111,11 @@ class User {
         */
        public function setEmail( $str ) {
                $this->load();
+               if( $str == $this->mEmail ) {
+                       return;
+               }
                $this->mEmail = $str;
+               $this->invalidateEmail();
                wfRunHooks( 'UserSetEmail', array( $this, &$this->mEmail ) );
        }
 
@@ -2133,6 +2140,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.
         *
@@ -2284,29 +2317,16 @@ class User {
 
        /**
         * Get the permissions this user has.
-        * @param $ns int If numeric, get permissions for this namespace
         * @return Array of String permission names
         */
-       public function getRights( $ns = null ) {
-               $key = is_null( $ns ) ? '*' : intval( $ns );
-
+       public function getRights() {
                if ( is_null( $this->mRights ) ) {
-                       $this->mRights = array();
-               }
-
-               if ( !isset( $this->mRights[$key] ) ) {
-                       $this->mRights[$key] = self::getGroupPermissions( $this->getEffectiveGroups(), $ns );
-                       wfRunHooks( 'UserGetRights', array( $this, &$this->mRights[$key], $ns ) );
+                       $this->mRights = self::getGroupPermissions( $this->getEffectiveGroups() );
+                       wfRunHooks( 'UserGetRights', array( $this, &$this->mRights ) );
                        // Force reindexation of rights when a hook has unset one of them
-                       $this->mRights[$key] = array_values( $this->mRights[$key] );
+                       $this->mRights = array_values( $this->mRights );
                }
-               if ( is_null( $ns ) ) {
-                       return $this->mRights[$key];
-               } else {
-                       // Merge non namespace specific rights
-                       return array_merge( $this->mRights[$key], $this->getRights() );
-               }
-
+               return $this->mRights;
        }
 
        /**
@@ -2431,7 +2451,7 @@ class User {
                }
                $this->loadGroups();
                $this->mGroups[] = $group;
-               $this->mRights = null;
+               $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups( true ) );
 
                $this->invalidateCache();
        }
@@ -2461,7 +2481,7 @@ class User {
                }
                $this->loadGroups();
                $this->mGroups = array_diff( $this->mGroups, array( $group ) );
-               $this->mRights = null;
+               $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups( true ) );
 
                $this->invalidateCache();
        }
@@ -2518,10 +2538,9 @@ class User {
        /**
         * Internal mechanics of testing a permission
         * @param $action String
-        * @param $ns int|null Namespace optional
         * @return bool
         */
-       public function isAllowed( $action = '', $ns = null ) {
+       public function isAllowed( $action = '' ) {
                if ( $action === '' ) {
                        return true; // In the spirit of DWIM
                }
@@ -2533,7 +2552,7 @@ class User {
                }
                # Use strict parameter to avoid matching numeric 0 accidentally inserted
                # by misconfiguration: 0 == 'foo'
-               return in_array( $action, $this->getRights( $ns ), true );
+               return in_array( $action, $this->getRights(), true );
        }
 
        /**
@@ -2575,6 +2594,7 @@ class User {
         * @deprecated since 1.18 Use ->getSkin() in the most relevant outputting context you have
         */
        public function getSkin() {
+               wfDeprecated( __METHOD__, '1.18' );
                return RequestContext::getMain()->getSkin();
        }
 
@@ -2608,6 +2628,14 @@ class User {
                $this->invalidateCache();
        }
 
+       /**
+        * Cleans up watchlist by removing invalid entries from it
+        */
+       public function cleanupWatchlist() {
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->delete( 'watchlist', array( 'wl_namespace < 0', 'wl_user' => $this->getId() ), __METHOD__ );
+       }
+
        /**
         * Clear the user's notification timestamp for the given title.
         * If e-notif e-mails are on, they will receive notification mails on
@@ -2699,6 +2727,7 @@ class User {
         * @deprecated in 1.19 due to removal of user_options from the user table
         */
        private function decodeOptions( $str ) {
+               wfDeprecated( __METHOD__, '1.19' );
                if( !$str )
                        return;
 
@@ -2940,22 +2969,35 @@ class User {
        }
 
        /**
-        * If this (non-anonymous) user is blocked, block any IP address
-        * they've successfully logged in from.
+        * If this user is logged-in and blocked,
+        * block any IP address they've successfully logged in from.
+        * @return bool A block was spread
         */
-       public function spreadBlock() {
+       public function spreadAnyEditBlock() {
+               if ( $this->isLoggedIn() && $this->isBlocked() ) {
+                       return $this->spreadBlock();
+               }
+               return false;
+       }
+
+       /**
+        * If this (non-anonymous) user is blocked,
+        * block the IP address they've successfully logged in from.
+        * @return bool A block was spread
+        */
+       protected function spreadBlock() {
                wfDebug( __METHOD__ . "()\n" );
                $this->load();
                if ( $this->mId == 0 ) {
-                       return;
+                       return false;
                }
 
                $userblock = Block::newFromTarget( $this->getName() );
                if ( !$userblock ) {
-                       return;
+                       return false;
                }
 
-               $userblock->doAutoblock( $this->getRequest()->getIP() );
+               return (bool)$userblock->doAutoblock( $this->getRequest()->getIP() );
        }
 
        /**
@@ -2973,11 +3015,12 @@ class User {
         * @return String Page rendering hash
         */
        public function getPageRenderingHash() {
+               wfDeprecated( __METHOD__, '1.17' );
+               
                global $wgUseDynamicDates, $wgRenderHashAppend, $wgLang, $wgContLang;
                if( $this->mHash ){
                        return $this->mHash;
                }
-               wfDeprecated( __METHOD__ );
 
                // stubthreshold is only included below for completeness,
                // since it disables the parser cache, its value will always
@@ -3141,17 +3184,32 @@ class User {
                }
        }
 
+       /**
+        * Alias for getEditToken.
+        * @deprecated since 1.19, use getEditToken instead.
+        *
+        * @param $salt String|Array of Strings Optional function-specific data for hashing
+        * @param $request WebRequest object to use or null to use $wgRequest
+        * @return String The new edit token
+        */
+       public function editToken( $salt = '', $request = null ) {
+               wfDeprecated( __METHOD__, '1.19' );
+               return $this->getEditToken( $salt, $request );
+       }
+
        /**
         * Initialize (if necessary) and return a session token value
         * which can be used in edit forms to show that the user's
         * login credentials aren't being hijacked with a foreign form
         * submission.
         *
+        * @since 1.19
+        *
         * @param $salt String|Array of Strings Optional function-specific data for hashing
         * @param $request WebRequest object to use or null to use $wgRequest
         * @return String The new edit token
         */
-       public function editToken( $salt = '', $request = null ) {
+       public function getEditToken( $salt = '', $request = null ) {
                if ( $request == null ) {
                        $request = $this->getRequest();
                }
@@ -3194,7 +3252,7 @@ class User {
         * @return Boolean: Whether the token matches
         */
        public function matchEditToken( $val, $salt = '', $request = null ) {
-               $sessionToken = $this->editToken( $salt, $request );
+               $sessionToken = $this->getEditToken( $salt, $request );
                if ( $val != $sessionToken ) {
                        wfDebug( "User::matchEditToken: broken session data\n" );
                }
@@ -3211,7 +3269,7 @@ class User {
         * @return Boolean: Whether the token matches
         */
        public function matchEditTokenNoSuffix( $val, $salt = '', $request = null ) {
-               $sessionToken = $this->editToken( $salt, $request );
+               $sessionToken = $this->getEditToken( $salt, $request );
                return substr( $sessionToken, 0, 32 ) == substr( $val, 0, 32 );
        }
 
@@ -3477,70 +3535,40 @@ class User {
         * Get the permissions associated with a given list of groups
         *
         * @param $groups Array of Strings List of internal group names
-        * @param $ns int
-        *
         * @return Array of Strings List of permission key names for given groups combined
         */
-       public static function getGroupPermissions( array $groups, $ns = null ) {
+       public static function getGroupPermissions( $groups ) {
                global $wgGroupPermissions, $wgRevokePermissions;
                $rights = array();
-
-               // Grant every granted permission first
+               // grant every granted permission first
                foreach( $groups as $group ) {
                        if( isset( $wgGroupPermissions[$group] ) ) {
-                               $rights = array_merge( $rights, self::extractRights(
-                                       $wgGroupPermissions[$group], $ns ) );
+                               $rights = array_merge( $rights,
+                                       // array_filter removes empty items
+                                       array_keys( array_filter( $wgGroupPermissions[$group] ) ) );
                        }
                }
-
-               // Revoke the revoked permissions
+               // now revoke the revoked permissions
                foreach( $groups as $group ) {
                        if( isset( $wgRevokePermissions[$group] ) ) {
-                               $rights = array_diff( $rights, self::extractRights(
-                                       $wgRevokePermissions[$group], $ns ) );
+                               $rights = array_diff( $rights,
+                                       array_keys( array_filter( $wgRevokePermissions[$group] ) ) );
                        }
                }
                return array_unique( $rights );
        }
 
-       /**
-        * Helper for User::getGroupPermissions
-        * @param $list array
-        * @param $ns int
-        * @return array
-        */
-       private static function extractRights( $list, $ns ) {
-               $rights = array();
-               foreach( $list as $right => $value ) {
-                       if ( is_array( $value ) ) {
-                               # This is a list of namespaces where the permission applies
-                               if ( !is_null( $ns ) && !empty( $value[$ns] ) ) {
-                                       $rights[] = $right;
-                               }
-                       } else {
-                               # This is a boolean indicating that the permission applies
-                               if ( $value ) {
-                                       $rights[] = $right;
-                               }
-                       }
-               }
-               return $rights;
-       }
-
        /**
         * Get all the groups who have a given permission
         *
         * @param $role String Role to check
-        * @param $ns int
-        *
-        *
         * @return Array of Strings List of internal group names with the given permission
         */
-       public static function getGroupsWithPermission( $role, $ns = null ) {
+       public static function getGroupsWithPermission( $role ) {
                global $wgGroupPermissions;
                $allowedGroups = array();
                foreach ( $wgGroupPermissions as $group => $rights ) {
-                       if ( in_array( $role, self::getGroupPermissions( array( $group ), $ns ), true ) ) {
+                       if ( isset( $rights[$role] ) && $rights[$role] ) {
                                $allowedGroups[] = $group;
                        }
                }
@@ -3562,10 +3590,11 @@ class User {
         * Get the localized descriptive name for a member of a group, if it exists
         *
         * @param $group String Internal group name
+        * @param $username String Username for gender (since 1.19)
         * @return String Localized name for group member
         */
-       public static function getGroupMember( $group ) {
-               $msg = wfMessage( "group-$group-member" );
+       public static function getGroupMember( $group, $username = '#' ) {
+               $msg = wfMessage( "group-$group-member", $username );
                return $msg->isBlank() ? $group : $msg->text();
        }
 
@@ -3908,12 +3937,12 @@ class User {
        }
 
        /**
-        * Add a newuser log entry for this user
+        * Add a newuser log entry for this user. Before 1.19 the return value was always true.
         *
         * @param $byEmail Boolean: account made by email?
         * @param $reason String: user supplied reason
         *
-        * @return true
+        * @return int|bool True if not $wgNewUserLog; otherwise ID of log item or 0 on failure
         */
        public function addNewUserLogEntry( $byEmail = false, $reason = '' ) {
                global $wgUser, $wgContLang, $wgNewUserLog;
@@ -3935,13 +3964,12 @@ class User {
                        }
                }
                $log = new LogPage( 'newusers' );
-               $log->addEntry(
+               return (int)$log->addEntry(
                        $action,
                        $this->getUserPage(),
                        $reason,
                        array( $this->getId() )
                );
-               return true;
        }
 
        /**
@@ -4043,10 +4071,8 @@ class User {
                        }
                }
 
-               $dbw->begin();
                $dbw->delete( 'user_properties', array( 'up_user' => $this->getId() ), __METHOD__ );
                $dbw->insert( 'user_properties', $insert_rows, __METHOD__ );
-               $dbw->commit();
        }
 
        /**