MediaWiki  master
StubGlobalUser.php
Go to the documentation of this file.
1 <?php
31 class StubGlobalUser extends StubObject {
32 
34  public static $destructorDeprecationDisarmed = false;
35 
37  public $realUser;
38 
42  public function __construct( User $realUser ) {
43  parent::__construct( 'wgUser' );
44  $this->realUser = $realUser;
45  }
46 
50  public function _newObject() {
51  // Based on DeprecatedGlobal::_newObject
52  /*
53  * Put the caller offset for wfDeprecated as 6, as
54  * that gives the function that uses this object, since:
55  *
56  * 1 = this function ( _newObject )
57  * 2 = StubGlobalUser::_unstub
58  * 3 = StubObject::_call
59  * 4 = StubObject::__call
60  * 5 = StubGlobalUser::<method of global called>
61  * 6 = Actual function using the global.
62  * (the same applies to _get/__get or _set/__set instead of _call/__call)
63  *
64  * Of course its theoretically possible to have other call
65  * sequences for this method, but that seems to be
66  * rather unlikely.
67  */
68  // Officially deprecated since 1.35
69  wfDeprecated( '$wgUser', '1.35', false, 6 );
70  return $this->realUser;
71  }
72 
79  public static function setUser( $user ) {
80  // This is intended to be interacting with the deprecated global
81  // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgUser
82  global $wgUser;
83 
84  self::$destructorDeprecationDisarmed = true;
85  // Supports StubGlobalUser parameter in case something fetched the existing value of
86  // $wgUser, set it to something else, and now is trying to restore it
87  $realUser = self::getRealUser( $user );
88  $wgUser = new self( $realUser );
89  self::$destructorDeprecationDisarmed = false;
90  }
91 
100  public static function getRealUser( $globalUser ): User {
101  if ( $globalUser instanceof StubGlobalUser ) {
102  return $globalUser->realUser;
103  } elseif ( $globalUser instanceof User ) {
104  return $globalUser;
105  } else {
106  throw new InvalidArgumentException(
107  '$globalUser must be a User (or StubGlobalUser), got ' .
108  ( is_object( $globalUser ) ? get_class( $globalUser ) : gettype( $globalUser ) )
109  );
110  }
111  }
112 
131  public function _unstub( $name = '_unstub', $level = 2 ) {
132  if ( !$GLOBALS[$this->global] instanceof self ) {
133  return $GLOBALS[$this->global]; // already unstubbed.
134  }
135 
136  $caller = wfGetCaller( $level );
137  wfDebug( "Unstubbing \${$this->global} on call of "
138  . "\${$this->global}::$name from $caller" );
139  $GLOBALS[$this->global] = $this->_newObject();
140  return $GLOBALS[$this->global];
141  }
142 
143  public function __destruct() {
144  if ( !self::$destructorDeprecationDisarmed ) {
145  wfDeprecatedMsg( '$wgUser reassignment detected', '1.37', false, 3 );
146  }
147  }
148 }
StubGlobalUser\$destructorDeprecationDisarmed
static bool $destructorDeprecationDisarmed
Definition: StubGlobalUser.php:34
StubGlobalUser\getRealUser
static getRealUser( $globalUser)
Get the relevant "real" user object based on either a User object or a StubGlobalUser wrapper.
Definition: StubGlobalUser.php:100
StubObject\$global
null string $global
Definition: StubObject.php:53
StubGlobalUser\_unstub
_unstub( $name='_unstub', $level=2)
This function creates a new object of the real class and replace it in the global variable.
Definition: StubGlobalUser.php:131
StubObject
Class to implement stub globals, which are globals that delay loading the their associated module cod...
Definition: StubObject.php:51
StubGlobalUser\setUser
static setUser( $user)
Reset the stub global user to a different "real" user object, while ensuring that any method calls on...
Definition: StubGlobalUser.php:79
wfDeprecatedMsg
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
Definition: GlobalFunctions.php:1028
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
StubGlobalUser\__destruct
__destruct()
Definition: StubGlobalUser.php:143
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:894
StubGlobalUser\$realUser
User $realUser
Definition: StubGlobalUser.php:37
StubGlobalUser\_newObject
_newObject()
Definition: StubGlobalUser.php:50
StubGlobalUser
Stub object for the global user ($wgUser) that makes it possible to change the relevant underlying ob...
Definition: StubGlobalUser.php:31
StubGlobalUser\__construct
__construct(User $realUser)
Definition: StubGlobalUser.php:42
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
wfGetCaller
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
Definition: GlobalFunctions.php:1360