Merge "Fix and make some types in PHPDoc and JSDoc tags more specific"
[lhc/web/wiklou.git] / includes / user / User.php
index 3317a1b..fa84c94 100644 (file)
@@ -305,12 +305,6 @@ class User implements IDBAccessObject {
        /** @var integer User::READ_* constant bitfield used to load data */
        protected $queryFlagsUsed = self::READ_NORMAL;
 
-       /** @var string Indicates type of block (used for eventlogging)
-        * Permitted values: 'cookie-block', 'proxy-block', 'openproxy-block', 'xff-block',
-        * 'config-block'
-        */
-       public $blockTrigger = false;
-
        public static $idCacheByName = [];
 
        /**
@@ -609,7 +603,7 @@ class User implements IDBAccessObject {
                        ]
                );
 
-               return $id ? User::newFromId( $id ) : null;
+               return $id ? self::newFromId( $id ) : null;
        }
 
        /**
@@ -693,19 +687,31 @@ class User implements IDBAccessObject {
                        return null;
                }
 
-               $fields = self::selectFields();
-
-               $dbw = wfGetDB( DB_MASTER );
-               $row = $dbw->selectRow(
+               $dbr = wfGetDB( DB_REPLICA );
+               $row = $dbr->selectRow(
                        'user',
-                       $fields,
+                       self::selectFields(),
                        [ 'user_name' => $name ],
                        __METHOD__
                );
+               if ( !$row ) {
+                       // Try the master database...
+                       $dbw = wfGetDB( DB_MASTER );
+                       $row = $dbw->selectRow(
+                               'user',
+                               self::selectFields(),
+                               [ 'user_name' => $name ],
+                               __METHOD__
+                       );
+               }
+
                if ( !$row ) {
                        // No user. Create it?
-                       return $options['create'] ? self::createNew( $name, [ 'token' => self::INVALID_TOKEN ] ) : null;
+                       return $options['create']
+                               ? self::createNew( $name, [ 'token' => self::INVALID_TOKEN ] )
+                               : null;
                }
+
                $user = self::newFromRow( $row );
 
                // A user is considered to exist as a non-system user if it can
@@ -836,7 +842,7 @@ class User implements IDBAccessObject {
                global $wgContLang, $wgMaxNameChars;
 
                if ( $name == ''
-                       || User::isIP( $name )
+                       || self::isIP( $name )
                        || strpos( $name, '/' ) !== false
                        || strlen( $name ) > $wgMaxNameChars
                        || $name != $wgContLang->ucfirst( $name )
@@ -1103,17 +1109,17 @@ class User implements IDBAccessObject {
                        case false:
                                break;
                        case 'valid':
-                               if ( !User::isValidUserName( $name ) ) {
+                               if ( !self::isValidUserName( $name ) ) {
                                        $name = false;
                                }
                                break;
                        case 'usable':
-                               if ( !User::isUsableName( $name ) ) {
+                               if ( !self::isUsableName( $name ) ) {
                                        $name = false;
                                }
                                break;
                        case 'creatable':
-                               if ( !User::isCreatableName( $name ) ) {
+                               if ( !self::isCreatableName( $name ) ) {
                                        $name = false;
                                }
                                break;
@@ -1585,7 +1591,7 @@ class User implements IDBAccessObject {
                // since extensions may change the set of searchable namespaces depending
                // on user groups/permissions.
                foreach ( $wgNamespacesToBeSearchedDefault as $nsnum => $val ) {
-                       $defOpt['searchNs' . $nsnum] = (boolean)$val;
+                       $defOpt['searchNs' . $nsnum] = (bool)$val;
                }
                $defOpt['skin'] = Skin::normalizeKey( $wgDefaultSkin );
 
@@ -1664,7 +1670,6 @@ class User implements IDBAccessObject {
                                        'address' => $ip,
                                        'systemBlock' => 'proxy',
                                ] );
-                               $this->blockTrigger = 'proxy-block';
                        } elseif ( $this->isAnon() && $this->isDnsBlacklisted( $ip ) ) {
                                $block = new Block( [
                                        'byText' => wfMessage( 'sorbs' )->text(),
@@ -1672,7 +1677,6 @@ class User implements IDBAccessObject {
                                        'address' => $ip,
                                        'systemBlock' => 'dnsbl',
                                ] );
-                               $this->blockTrigger = 'openproxy-block';
                        }
                }
 
@@ -1691,7 +1695,6 @@ class User implements IDBAccessObject {
                                # Mangle the reason to alert the user that the block
                                # originated from matching the X-Forwarded-For header.
                                $block->mReason = wfMessage( 'xffblockreason', $block->mReason )->text();
-                               $this->blockTrigger = 'xff-block';
                        }
                }
 
@@ -1707,7 +1710,6 @@ class User implements IDBAccessObject {
                                'anonOnly' => true,
                                'systemBlock' => 'wgSoftBlockRanges',
                        ] );
-                       $this->blockTrigger = 'config-block';
                }
 
                if ( $block instanceof Block ) {
@@ -1721,7 +1723,6 @@ class User implements IDBAccessObject {
                        $this->mBlockedby = '';
                        $this->mHideName = 0;
                        $this->mAllowUsertalk = false;
-                       $this->blockTrigger = false;
                }
 
                // Avoid PHP 7.1 warning of passing $this by reference
@@ -1754,7 +1755,6 @@ class User implements IDBAccessObject {
                                $useBlockCookie = ( $config->get( 'CookieSetOnAutoblock' ) === true );
                                if ( $blockIsValid && $useBlockCookie ) {
                                        // Use the block.
-                                       $this->blockTrigger = 'cookie-block';
                                        return $tmpBlock;
                                } else {
                                        // If the block is not valid, remove the cookie.
@@ -1941,11 +1941,12 @@ class User implements IDBAccessObject {
                $id = $this->getId();
                $userLimit = false;
                $isNewbie = $this->isNewbie();
+               $cache = ObjectCache::getLocalClusterInstance();
 
                if ( $id == 0 ) {
                        // limits for anons
                        if ( isset( $limits['anon'] ) ) {
-                               $keys[wfMemcKey( 'limiter', $action, 'anon' )] = $limits['anon'];
+                               $keys[$cache->makeKey( 'limiter', $action, 'anon' )] = $limits['anon'];
                        }
                } else {
                        // limits for logged-in users
@@ -1954,7 +1955,7 @@ class User implements IDBAccessObject {
                        }
                        // limits for newbie logged-in users
                        if ( $isNewbie && isset( $limits['newbie'] ) ) {
-                               $keys[wfMemcKey( 'limiter', $action, 'user', $id )] = $limits['newbie'];
+                               $keys[$cache->makeKey( 'limiter', $action, 'user', $id )] = $limits['newbie'];
                        }
                }
 
@@ -1991,7 +1992,7 @@ class User implements IDBAccessObject {
                if ( $userLimit !== false ) {
                        list( $max, $period ) = $userLimit;
                        wfDebug( __METHOD__ . ": effective user limit: $max in {$period}s\n" );
-                       $keys[wfMemcKey( 'limiter', $action, 'user', $id )] = $userLimit;
+                       $keys[$cache->makeKey( 'limiter', $action, 'user', $id )] = $userLimit;
                }
 
                // ip-based limits for all ping-limitable users
@@ -2018,8 +2019,6 @@ class User implements IDBAccessObject {
                        }
                }
 
-               $cache = ObjectCache::getLocalClusterInstance();
-
                $triggered = false;
                foreach ( $keys as $key => $limit ) {
                        list( $max, $period ) = $limit;
@@ -2213,7 +2212,7 @@ class User implements IDBAccessObject {
         * @return int The user's ID; 0 if the user is anonymous or nonexistent
         */
        public function getId() {
-               if ( $this->mId === null && $this->mName !== null && User::isIP( $this->mName ) ) {
+               if ( $this->mId === null && $this->mName !== null && self::isIP( $this->mName ) ) {
                        // Special case, we know the user is anonymous
                        return 0;
                } elseif ( !$this->isItemLoaded( 'id' ) ) {
@@ -2495,7 +2494,7 @@ class User implements IDBAccessObject {
                        $cache->delete( $key, 1 );
                } else {
                        wfGetDB( DB_MASTER )->onTransactionPreCommitOrIdle(
-                               function() use ( $cache, $key ) {
+                               function () use ( $cache, $key ) {
                                        $cache->delete( $key );
                                },
                                __METHOD__
@@ -2528,8 +2527,9 @@ class User implements IDBAccessObject {
        public function touch() {
                $id = $this->getId();
                if ( $id ) {
-                       $key = wfMemcKey( 'user-quicktouched', 'id', $id );
-                       ObjectCache::getMainWANInstance()->touchCheckKey( $key );
+                       $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+                       $key = $cache->makeKey( 'user-quicktouched', 'id', $id );
+                       $cache->touchCheckKey( $key );
                        $this->mQuickTouched = null;
                }
        }
@@ -2556,8 +2556,8 @@ class User implements IDBAccessObject {
 
                if ( $this->mId ) {
                        if ( $this->mQuickTouched === null ) {
-                               $key = wfMemcKey( 'user-quicktouched', 'id', $this->mId );
-                               $cache = ObjectCache::getMainWANInstance();
+                               $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
+                               $key = $cache->makeKey( 'user-quicktouched', 'id', $this->mId );
 
                                $this->mQuickTouched = wfTimestamp( TS_MW, $cache->getCheckKeyTime( $key ) );
                        }
@@ -3686,7 +3686,7 @@ class User implements IDBAccessObject {
                        }
 
                        // Try to update the DB post-send and only if needed...
-                       DeferredUpdates::addCallableUpdate( function() use ( $title, $oldid ) {
+                       DeferredUpdates::addCallableUpdate( function () use ( $title, $oldid ) {
                                if ( !$this->getNewtalk() ) {
                                        return; // no notifications to clear
                                }
@@ -4131,7 +4131,7 @@ class User implements IDBAccessObject {
                }
                $dbw->insert( 'user', $fields, __METHOD__, [ 'IGNORE' ] );
                if ( $dbw->affectedRows() ) {
-                       $newUser = User::newFromId( $dbw->insertId() );
+                       $newUser = self::newFromId( $dbw->insertId() );
                } else {
                        $newUser = null;
                }
@@ -4945,7 +4945,8 @@ class User implements IDBAccessObject {
                }
                $title = UserGroupMembership::getGroupPage( $group );
                if ( $title ) {
-                       return Linker::link( $title, htmlspecialchars( $text ) );
+                       return MediaWikiServices::getInstance()
+                               ->getLinkRenderer()->makeLink( $title, $text );
                } else {
                        return htmlspecialchars( $text );
                }
@@ -5035,7 +5036,7 @@ class User implements IDBAccessObject {
                        // Do nothing
                } elseif ( $wgGroupsAddToSelf[$group] === true ) {
                        // No idea WHY this would be used, but it's there
-                       $groups['add-self'] = User::getAllGroups();
+                       $groups['add-self'] = self::getAllGroups();
                } elseif ( is_array( $wgGroupsAddToSelf[$group] ) ) {
                        $groups['add-self'] = $wgGroupsAddToSelf[$group];
                }
@@ -5043,7 +5044,7 @@ class User implements IDBAccessObject {
                if ( empty( $wgGroupsRemoveFromSelf[$group] ) ) {
                        // Do nothing
                } elseif ( $wgGroupsRemoveFromSelf[$group] === true ) {
-                       $groups['remove-self'] = User::getAllGroups();
+                       $groups['remove-self'] = self::getAllGroups();
                } elseif ( is_array( $wgGroupsRemoveFromSelf[$group] ) ) {
                        $groups['remove-self'] = $wgGroupsRemoveFromSelf[$group];
                }
@@ -5064,7 +5065,7 @@ class User implements IDBAccessObject {
                        // compatibility with old "userrights lets you change
                        // everything")
                        // Using array_merge to make the groups reindexed
-                       $all = array_merge( User::getAllGroups() );
+                       $all = array_merge( self::getAllGroups() );
                        return [
                                'add' => $all,
                                'remove' => $all,
@@ -5490,7 +5491,7 @@ class User implements IDBAccessObject {
                global $wgLang;
 
                $groups = [];
-               foreach ( User::getGroupsWithPermission( $permission ) as $group ) {
+               foreach ( self::getGroupsWithPermission( $permission ) as $group ) {
                        $groups[] = UserGroupMembership::getLink( $group, RequestContext::getMain(), 'wiki' );
                }