Change calls from Xml::namespaceSelector() to Html::namespaceSelector() since the...
[lhc/web/wiklou.git] / includes / User.php
index b0d291d..684a05c 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',
@@ -168,8 +165,8 @@ class User {
        //@{
        var $mId, $mName, $mRealName, $mPassword, $mNewpassword, $mNewpassTime,
                $mEmail, $mTouched, $mToken, $mEmailAuthenticated,
-               $mEmailToken, $mEmailTokenExpires, $mRegistration, $mGroups, $mOptionOverrides,
-               $mCookiePassword, $mEditCount, $mAllowUsertalk;
+               $mEmailToken, $mEmailTokenExpires, $mRegistration, $mEditCount,
+               $mGroups, $mOptionOverrides;
        //@}
 
        /**
@@ -212,6 +209,11 @@ class User {
         */
        var $mBlock;
 
+       /**
+        * @var bool
+        */
+       var $mAllowUsertalk;
+
        /**
         * @var Block
         */
@@ -394,7 +396,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 +417,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,7 +450,7 @@ class User {
        /**
         * Get the username corresponding to a given user ID
         * @param $id Int User ID
-        * @return String|false The corresponding username
+        * @return String|bool The corresponding username
         */
        public static function whoIs( $id ) {
                $dbr = wfGetDB( DB_SLAVE );
@@ -459,7 +461,7 @@ class User {
         * Get the real name of a user given their user ID
         *
         * @param $id Int User ID
-        * @return String|false The corresponding user's real name
+        * @return String|bool The corresponding user's real name
         */
        public static function whoIsReal( $id ) {
                $dbr = wfGetDB( DB_SLAVE );
@@ -551,6 +553,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 +735,7 @@ class User {
         * @deprecated since 1.18 call Sanitizer::isValidEmail() directly
         */
        public static function isValidEmailAddr( $addr ) {
+               wfDeprecated( __METHOD__, '1.18' );
                return Sanitizer::validateEmail( $addr );
        }
 
@@ -1269,10 +1273,6 @@ class User {
                // overwriting mBlockedby, surely?
                $this->load();
 
-               $this->mBlockedby = 0;
-               $this->mHideName = 0;
-               $this->mAllowUsertalk = 0;
-
                # We only need to worry about passing the IP address to the Block generator if the
                # user is not immune to autoblocks/hardblocks, and they are the current user so we
                # know which IP address they're actually coming from
@@ -1283,30 +1283,37 @@ class User {
                }
 
                # User/IP blocking
-               $this->mBlock = Block::newFromTarget( $this->getName(), $ip, !$bFromSlave );
-               if ( $this->mBlock instanceof Block ) {
-                       wfDebug( __METHOD__ . ": Found block.\n" );
-                       $this->mBlockedby = $this->mBlock->getByName();
-                       $this->mBlockreason = $this->mBlock->mReason;
-                       $this->mHideName = $this->mBlock->mHideName;
-                       $this->mAllowUsertalk = !$this->mBlock->prevents( 'editownusertalk' );
-               }
+               $block = Block::newFromTarget( $this->getName(), $ip, !$bFromSlave );
 
                # Proxy blocking
-               if ( $ip !== null && !$this->isAllowed( 'proxyunbannable' ) && !in_array( $ip, $wgProxyWhitelist ) ) {
+               if ( !$block instanceof Block && $ip !== null && !$this->isAllowed( 'proxyunbannable' )
+                       && !in_array( $ip, $wgProxyWhitelist ) ) 
+               {
                        # Local list
                        if ( self::isLocallyBlockedProxy( $ip ) ) {
-                               $this->mBlockedby = wfMsg( 'proxyblocker' );
-                               $this->mBlockreason = wfMsg( 'proxyblockreason' );
+                               $block = new Block;
+                               $block->setBlocker( wfMsg( 'proxyblocker' ) );
+                               $block->mReason = wfMsg( 'proxyblockreason' );
+                               $block->setTarget( $ip );
+                       } elseif ( $this->isAnon() && $this->isDnsBlacklisted( $ip ) ) {
+                               $block = new Block;
+                               $block->setBlocker( wfMsg( 'sorbs' ) );
+                               $block->mReason = wfMsg( 'sorbsreason' );
+                               $block->setTarget( $ip );
                        }
+               }
 
-                       # DNSBL
-                       if ( !$this->mBlockedby && !$this->getID() ) {
-                               if ( $this->isDnsBlacklisted( $ip ) ) {
-                                       $this->mBlockedby = wfMsg( 'sorbs' );
-                                       $this->mBlockreason = wfMsg( 'sorbsreason' );
-                               }
-                       }
+               if ( $block instanceof Block ) {
+                       wfDebug( __METHOD__ . ": Found block.\n" );
+                       $this->mBlock = $block;
+                       $this->mBlockedby = $block->getByName();
+                       $this->mBlockreason = $block->mReason;
+                       $this->mHideName = $block->mHideName;
+                       $this->mAllowUsertalk = !$block->prevents( 'editownusertalk' );
+               } else {
+                       $this->mBlockedby = '';
+                       $this->mHideName = 0;
+                       $this->mAllowUsertalk = false;
                }
 
                # Extensions
@@ -2046,16 +2053,6 @@ class User {
                }
        }
 
-       /**
-        * Set the cookie password
-        *
-        * @param $str String New cookie password
-        */
-       private function setCookiePassword( $str ) {
-               $this->load();
-               $this->mCookiePassword = md5( $str );
-       }
-
        /**
         * Set the password for a password reminder or new account email
         *
@@ -2111,7 +2108,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 ) );
        }
 
@@ -2287,29 +2288,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;
        }
 
        /**
@@ -2434,7 +2422,7 @@ class User {
                }
                $this->loadGroups();
                $this->mGroups[] = $group;
-               $this->mRights = null;
+               $this->mRights = User::getGroupPermissions( $this->getEffectiveGroups( true ) );
 
                $this->invalidateCache();
        }
@@ -2464,7 +2452,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();
        }
@@ -2521,10 +2509,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
                }
@@ -2536,7 +2523,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 );
        }
 
        /**
@@ -2578,6 +2565,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();
        }
 
@@ -2611,14 +2599,6 @@ 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
@@ -2653,28 +2633,15 @@ class User {
                // The query to find out if it is watched is cached both in memcached and per-invocation,
                // and when it does have to be executed, it can be on a slave
                // If this is the user's newtalk page, we always update the timestamp
-               if( $title->getNamespace() == NS_USER_TALK &&
+               $force = '';
+               if ( $title->getNamespace() == NS_USER_TALK &&
                        $title->getText() == $this->getName() )
                {
-                       $watched = true;
-               } else {
-                       $watched = $this->isWatched( $title );
+                       $force = 'force';
                }
 
-               // If the page is watched by the user (or may be watched), update the timestamp on any
-               // any matching rows
-               if ( $watched ) {
-                       $dbw = wfGetDB( DB_MASTER );
-                       $dbw->update( 'watchlist',
-                                       array( /* SET */
-                                               'wl_notificationtimestamp' => null
-                                       ), array( /* WHERE */
-                                               'wl_title' => $title->getDBkey(),
-                                               'wl_namespace' => $title->getNamespace(),
-                                               'wl_user' => $this->getID()
-                                       ), __METHOD__
-                       );
-               }
+               $wi = WatchedItem::fromUserTitle( $this, $title );
+               $wi->resetNotificationTimestamp( $force );
        }
 
        /**
@@ -2710,6 +2677,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;
 
@@ -2762,6 +2730,14 @@ class User {
 
                $this->load();
                if ( 0 == $this->mId ) return;
+               if ( !$this->mToken ) {
+                       // When token is empty or NULL generate a new one and then save it to the database
+                       // This allows a wiki to re-secure itself after a leak of it's user table or $wgSecretKey
+                       // Simply by setting every cell in the user_token column to NULL and letting them be
+                       // regenerated as users log back into the wiki.
+                       $this->setToken();
+                       $this->saveSettings();
+               }
                $session = array(
                        'wsUserID' => $this->mId,
                        'wsToken' => $this->mToken,
@@ -2907,6 +2883,7 @@ class User {
                        'user_token' => $user->mToken,
                        'user_registration' => $dbw->timestamp( $user->mRegistration ),
                        'user_editcount' => 0,
+                       'user_touched' => $dbw->timestamp( self::newTouchedTimestamp() ),
                );
                foreach ( $params as $name => $value ) {
                        $fields["user_$name"] = $value;
@@ -2925,6 +2902,9 @@ class User {
         */
        public function addToDatabase() {
                $this->load();
+
+               $this->mTouched = self::newTouchedTimestamp();
+
                $dbw = wfGetDB( DB_MASTER );
                $seqVal = $dbw->nextSequenceValue( 'user_user_id_seq' );
                $dbw->insert( 'user',
@@ -2940,6 +2920,7 @@ class User {
                                'user_token' => $this->mToken,
                                'user_registration' => $dbw->timestamp( $this->mRegistration ),
                                'user_editcount' => 0,
+                               'user_touched' => $dbw->timestamp( $this->mTouched ),
                        ), __METHOD__
                );
                $this->mId = $dbw->insertId();
@@ -2997,11 +2978,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
@@ -3167,16 +3149,17 @@ class User {
 
        /**
         * Alias for getEditToken.
-        * @deprecated since 1.19, use getEditToken instead. 
-        * 
+        * @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
@@ -3232,7 +3215,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" );
                }
@@ -3249,7 +3232,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 );
        }
 
@@ -3375,7 +3358,7 @@ class User {
         *
         * @note Call saveSettings() after calling this function to commit the change.
         *
-        * @return true
+        * @return bool
         */
        public function confirmEmail() {
                $this->setEmailAuthenticationTimestamp( wfTimestampNow() );
@@ -3388,7 +3371,7 @@ class User {
         * address if it was already confirmed.
         *
         * @note Call saveSettings() after calling this function to commit the change.
-        * @return true
+        * @return bool Returns true
         */
        function invalidateEmail() {
                $this->load();
@@ -3515,70 +3498,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;
                        }
                }
@@ -3947,7 +3900,7 @@ 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
@@ -3986,7 +3939,7 @@ class User {
         * Add an autocreate newuser log entry for this user
         * Used by things like CentralAuth and perhaps other authplugins.
         *
-        * @return true
+        * @return bool
         */
        public function addNewUserLogEntryAutoCreate() {
                global $wgNewUserLog;
@@ -4026,6 +3979,7 @@ class User {
                                __METHOD__
                        );
 
+                       $this->mOptionOverrides = array();
                        foreach ( $res as $row ) {
                                $this->mOptionOverrides[$row->up_property] = $row->up_value;
                                $this->mOptions[$row->up_property] = $row->up_value;
@@ -4081,10 +4035,8 @@ class User {
                        }
                }
 
-               $dbw->begin();
                $dbw->delete( 'user_properties', array( 'up_user' => $this->getId() ), __METHOD__ );
                $dbw->insert( 'user_properties', $insert_rows, __METHOD__ );
-               $dbw->commit();
        }
 
        /**