Merge "selenium: Add jpeg-js to devDependencies"
[lhc/web/wiklou.git] / includes / user / User.php
index d2a0707..18d62f5 100644 (file)
@@ -20,6 +20,8 @@
  * @file
  */
 
+use MediaWiki\Block\AbstractBlock;
+use MediaWiki\Block\SystemBlock;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Session\SessionManager;
 use MediaWiki\Session\Token;
@@ -278,7 +280,7 @@ class User implements IDBAccessObject, UserIdentity {
        protected $mImplicitGroups;
        /** @var array */
        protected $mFormerGroups;
-       /** @var Block */
+       /** @var AbstractBlock */
        protected $mGlobalBlock;
        /** @var bool */
        protected $mLocked;
@@ -290,13 +292,13 @@ class User implements IDBAccessObject, UserIdentity {
        /** @var WebRequest */
        private $mRequest;
 
-       /** @var Block */
+       /** @var AbstractBlock */
        public $mBlock;
 
        /** @var bool */
        protected $mAllowUsertalk;
 
-       /** @var Block */
+       /** @var AbstractBlock */
        private $mBlockedFromCreateAccount = false;
 
        /** @var int User::READ_* constant bitfield used to load data */
@@ -673,11 +675,20 @@ class User implements IDBAccessObject, UserIdentity {
         * @param int|null $userId User ID, if known
         * @param string|null $userName User name, if known
         * @param int|null $actorId Actor ID, if known
+        * @param bool|string $wikiId remote wiki to which the User/Actor ID applies, or false if none
         * @return User
         */
-       public static function newFromAnyId( $userId, $userName, $actorId ) {
+       public static function newFromAnyId( $userId, $userName, $actorId, $wikiId = false ) {
                global $wgActorTableSchemaMigrationStage;
 
+               // Stop-gap solution for the problem described in T222212.
+               // Force the User ID and Actor ID to zero for users loaded from the database
+               // of another wiki, to prevent subtle data corruption and confusing failure modes.
+               if ( $wikiId !== false ) {
+                       $userId = 0;
+                       $actorId = 0;
+               }
+
                $user = new User;
                $user->mFrom = 'defaults';
 
@@ -914,7 +925,7 @@ class User implements IDBAccessObject, UserIdentity {
                }
 
                if ( !( $flags & self::READ_LATEST ) && array_key_exists( $name, self::$idCacheByName ) ) {
-                       return self::$idCacheByName[$name];
+                       return is_null( self::$idCacheByName[$name] ) ? null : (int)self::$idCacheByName[$name];
                }
 
                list( $index, $options ) = DBAccessObjectUtils::getDBOptions( $flags );
@@ -1151,35 +1162,6 @@ class User implements IDBAccessObject, UserIdentity {
                return $this->checkPasswordValidity( $password )->isGood();
        }
 
-       /**
-        * Given unvalidated password input, return error message on failure.
-        *
-        * @param string $password Desired password
-        * @return bool|string|array True on success, string or array of error message on failure
-        * @deprecated since 1.33, use checkPasswordValidity
-        */
-       public function getPasswordValidity( $password ) {
-               wfDeprecated( __METHOD__, '1.33' );
-
-               $result = $this->checkPasswordValidity( $password );
-               if ( $result->isGood() ) {
-                       return true;
-               }
-
-               $messages = [];
-               foreach ( $result->getErrorsByType( 'error' ) as $error ) {
-                       $messages[] = $error['message'];
-               }
-               foreach ( $result->getErrorsByType( 'warning' ) as $warning ) {
-                       $messages[] = $warning['message'];
-               }
-               if ( count( $messages ) === 1 ) {
-                       return $messages[0];
-               }
-
-               return $messages;
-       }
-
        /**
         * Check if this is a valid password for this user
         *
@@ -1839,7 +1821,7 @@ class User implements IDBAccessObject, UserIdentity {
                        $fromReplica
                );
 
-               if ( $block instanceof Block ) {
+               if ( $block instanceof AbstractBlock ) {
                        wfDebug( __METHOD__ . ": Found block.\n" );
                        $this->mBlock = $block;
                        $this->mBlockedby = $block->getByName();
@@ -2153,7 +2135,7 @@ class User implements IDBAccessObject, UserIdentity {
         * @return bool True if blocked, false otherwise
         */
        public function isBlocked( $fromReplica = true ) {
-               return $this->getBlock( $fromReplica ) instanceof Block &&
+               return $this->getBlock( $fromReplica ) instanceof AbstractBlock &&
                        $this->getBlock()->appliesToRight( 'edit' );
        }
 
@@ -2161,11 +2143,11 @@ class User implements IDBAccessObject, UserIdentity {
         * Get the block affecting the user, or null if the user is not blocked
         *
         * @param bool $fromReplica Whether to check the replica DB instead of the master
-        * @return Block|null
+        * @return AbstractBlock|null
         */
        public function getBlock( $fromReplica = true ) {
                $this->getBlockedStatus( $fromReplica );
-               return $this->mBlock instanceof Block ? $this->mBlock : null;
+               return $this->mBlock instanceof AbstractBlock ? $this->mBlock : null;
        }
 
        /**
@@ -2221,7 +2203,7 @@ class User implements IDBAccessObject, UserIdentity {
         * @return bool True if blocked, false otherwise
         */
        public function isBlockedGlobally( $ip = '' ) {
-               return $this->getGlobalBlock( $ip ) instanceof Block;
+               return $this->getGlobalBlock( $ip ) instanceof AbstractBlock;
        }
 
        /**
@@ -2230,7 +2212,7 @@ class User implements IDBAccessObject, UserIdentity {
         * This is intended for quick UI checks.
         *
         * @param string $ip IP address, uses current client if none given
-        * @return Block|null Block object if blocked, null otherwise
+        * @return AbstractBlock|null Block object if blocked, null otherwise
         * @throws FatalError
         * @throws MWException
         */
@@ -2252,7 +2234,7 @@ class User implements IDBAccessObject, UserIdentity {
 
                if ( $blocked && $block === null ) {
                        // back-compat: UserIsBlockedGlobally didn't have $block param first
-                       $block = new Block( [
+                       $block = new SystemBlock( [
                                'address' => $ip,
                                'systemBlock' => 'global-block'
                        ] );
@@ -3665,12 +3647,25 @@ class User implements IDBAccessObject, UserIdentity {
                return true;
        }
 
+       /**
+        * Alias of isLoggedIn() with a name that describes its actual functionality. UserIdentity has
+        * only this new name and not the old isLoggedIn() variant.
+        *
+        * @return bool True if user is registered on this wiki, i.e., has a user ID. False if user is
+        *   anonymous or has no local account (which can happen when importing). This is equivalent to
+        *   getId() != 0 and is provided for code readability.
+        * @since 1.34
+        */
+       public function isRegistered() {
+               return $this->getId() != 0;
+       }
+
        /**
         * Get whether the user is logged in
         * @return bool
         */
        public function isLoggedIn() {
-               return $this->getId() != 0;
+               return $this->isRegistered();
        }
 
        /**
@@ -3678,7 +3673,7 @@ class User implements IDBAccessObject, UserIdentity {
         * @return bool
         */
        public function isAnon() {
-               return !$this->isLoggedIn();
+               return !$this->isRegistered();
        }
 
        /**
@@ -4085,7 +4080,7 @@ class User implements IDBAccessObject, UserIdentity {
                $newTouched = $this->newTouchedTimestamp();
 
                $dbw = wfGetDB( DB_MASTER );
-               $dbw->doAtomicSection( __METHOD__, function ( $dbw, $fname ) use ( $newTouched ) {
+               $dbw->doAtomicSection( __METHOD__, function ( IDatabase $dbw, $fname ) use ( $newTouched ) {
                        global $wgActorTableSchemaMigrationStage;
 
                        $dbw->update( 'user',
@@ -4211,7 +4206,7 @@ class User implements IDBAccessObject, UserIdentity {
                        $fields["user_$name"] = $value;
                }
 
-               return $dbw->doAtomicSection( __METHOD__, function ( $dbw, $fname ) use ( $fields ) {
+               return $dbw->doAtomicSection( __METHOD__, function ( IDatabase $dbw, $fname ) use ( $fields ) {
                        $dbw->insert( 'user', $fields, $fname, [ 'IGNORE' ] );
                        if ( $dbw->affectedRows() ) {
                                $newUser = self::newFromId( $dbw->insertId() );
@@ -4265,7 +4260,7 @@ class User implements IDBAccessObject, UserIdentity {
                $this->mTouched = $this->newTouchedTimestamp();
 
                $dbw = wfGetDB( DB_MASTER );
-               $status = $dbw->doAtomicSection( __METHOD__, function ( $dbw, $fname ) {
+               $status = $dbw->doAtomicSection( __METHOD__, function ( IDatabase $dbw, $fname ) {
                        $noPass = PasswordFactory::newInvalidPassword()->toString();
                        $dbw->insert( 'user',
                                [
@@ -4370,7 +4365,7 @@ class User implements IDBAccessObject, UserIdentity {
 
        /**
         * Get whether the user is explicitly blocked from account creation.
-        * @return bool|Block
+        * @return bool|AbstractBlock
         */
        public function isBlockedFromCreateAccount() {
                $this->getBlockedStatus();
@@ -4384,7 +4379,7 @@ class User implements IDBAccessObject, UserIdentity {
                if ( $this->mBlockedFromCreateAccount === false && !$this->isAllowed( 'ipblock-exempt' ) ) {
                        $this->mBlockedFromCreateAccount = Block::newFromTarget( null, $this->getRequest()->getIP() );
                }
-               return $this->mBlockedFromCreateAccount instanceof Block
+               return $this->mBlockedFromCreateAccount instanceof AbstractBlock
                        && $this->mBlockedFromCreateAccount->appliesToRight( 'createaccount' )
                        ? $this->mBlockedFromCreateAccount
                        : false;
@@ -5043,68 +5038,6 @@ class User implements IDBAccessObject, UserIdentity {
                return $wgImplicitGroups;
        }
 
-       /**
-        * Get the title of a page describing a particular group
-        * @deprecated since 1.29 Use UserGroupMembership::getGroupPage instead
-        *
-        * @param string $group Internal group name
-        * @return Title|bool Title of the page if it exists, false otherwise
-        */
-       public static function getGroupPage( $group ) {
-               wfDeprecated( __METHOD__, '1.29' );
-               return UserGroupMembership::getGroupPage( $group );
-       }
-
-       /**
-        * Create a link to the group in HTML, if available;
-        * else return the group name.
-        * @deprecated since 1.29 Use UserGroupMembership::getLink instead, or
-        * make the link yourself if you need custom text
-        *
-        * @param string $group Internal name of the group
-        * @param string $text The text of the link
-        * @return string HTML link to the group
-        */
-       public static function makeGroupLinkHTML( $group, $text = '' ) {
-               wfDeprecated( __METHOD__, '1.29' );
-
-               if ( $text == '' ) {
-                       $text = UserGroupMembership::getGroupName( $group );
-               }
-               $title = UserGroupMembership::getGroupPage( $group );
-               if ( $title ) {
-                       return MediaWikiServices::getInstance()
-                               ->getLinkRenderer()->makeLink( $title, $text );
-               }
-
-               return htmlspecialchars( $text );
-       }
-
-       /**
-        * Create a link to the group in Wikitext, if available;
-        * else return the group name.
-        * @deprecated since 1.29 Use UserGroupMembership::getLink instead, or
-        * make the link yourself if you need custom text
-        *
-        * @param string $group Internal name of the group
-        * @param string $text The text of the link
-        * @return string Wikilink to the group
-        */
-       public static function makeGroupLinkWiki( $group, $text = '' ) {
-               wfDeprecated( __METHOD__, '1.29' );
-
-               if ( $text == '' ) {
-                       $text = UserGroupMembership::getGroupName( $group );
-               }
-               $title = UserGroupMembership::getGroupPage( $group );
-               if ( $title ) {
-                       $page = $title->getFullText();
-                       return "[[$page|$text]]";
-               }
-
-               return $text;
-       }
-
        /**
         * Returns an array of the groups that a particular group can add/remove.
         *