MediaWiki REL1_39
StubGlobalUser.php
Go to the documentation of this file.
1<?php
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
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}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfGetCaller( $level=2)
Get the name of the function which called this function wfGetCaller( 1 ) is the function with the wfG...
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
$wgUser
Definition Setup.php:504
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:82
Stub object for the global user ($wgUser) that makes it possible to change the relevant underlying ob...
static bool $destructorDeprecationDisarmed
static getRealUser( $globalUser)
Get the relevant "real" user object based on either a User object or a StubGlobalUser wrapper.
__construct(User $realUser)
_unstub( $name='_unstub', $level=2)
This function creates a new object of the real class and replace it in the global variable.
static setUser( $user)
Reset the stub global user to a different "real" user object, while ensuring that any method calls on...
Class to implement stub globals, which are globals that delay loading the their associated module cod...
internal since 1.36
Definition User.php:70