MediaWiki master
StubGlobalUser.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\StubObject;
22
23use InvalidArgumentException;
25
37
39 public static $destructorDeprecationDisarmed = false;
40
42 public $realUser;
43
47 public function __construct( User $realUser ) {
48 parent::__construct( 'wgUser' );
49 $this->realUser = $realUser;
50 }
51
56 // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
57 public function _newObject() {
58 // Based on MediaWiki\StubObject\DeprecatedGlobal::_newObject
59 /*
60 * Put the caller offset for wfDeprecated as 6, as
61 * that gives the function that uses this object, since:
62 *
63 * 1 = this function ( _newObject )
64 * 2 = MediaWiki\StubObject\StubGlobalUser::_unstub
65 * 3 = MediaWiki\StubObject\StubObject::_call
66 * 4 = MediaWiki\StubObject\StubObject::__call
67 * 5 = MediaWiki\StubObject\StubGlobalUser::<method of global called>
68 * 6 = Actual function using the global.
69 * (the same applies to _get/__get or _set/__set instead of _call/__call)
70 *
71 * Of course its theoretically possible to have other call
72 * sequences for this method, but that seems to be
73 * rather unlikely.
74 */
75 // Officially deprecated since 1.35
76 wfDeprecated( '$wgUser', '1.35', false, 6 );
77 return $this->realUser;
78 }
79
86 public static function setUser( $user ) {
87 // This is intended to be interacting with the deprecated global
88 // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgUser
89 global $wgUser;
90
91 self::$destructorDeprecationDisarmed = true;
92 // Supports MediaWiki\StubObject\StubGlobalUser parameter in case something fetched the existing value of
93 // $wgUser, set it to something else, and now is trying to restore it
95 $wgUser = new self( $realUser );
96 self::$destructorDeprecationDisarmed = false;
97 }
98
107 public static function getRealUser( $globalUser ): User {
108 if ( $globalUser instanceof StubGlobalUser ) {
109 return $globalUser->realUser;
110 } elseif ( $globalUser instanceof User ) {
111 return $globalUser;
112 } else {
113 throw new InvalidArgumentException(
114 '$globalUser must be a User (or MediaWiki\StubObject\StubGlobalUser), got ' .
115 ( is_object( $globalUser ) ? get_class( $globalUser ) : gettype( $globalUser ) )
116 );
117 }
118 }
119
138 // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
139 public function _unstub( $name = '_unstub', $level = 2 ) {
140 if ( !$GLOBALS[$this->global] instanceof self ) {
141 return $GLOBALS[$this->global]; // already unstubbed.
142 }
143
144 $caller = wfGetCaller( $level );
145 wfDebug( "Unstubbing \${$this->global} on call of "
146 . "\${$this->global}::$name from $caller" );
147 $GLOBALS[$this->global] = $this->_newObject();
148 return $GLOBALS[$this->global];
149 }
150
151 public function __destruct() {
152 if ( !self::$destructorDeprecationDisarmed ) {
153 wfDeprecatedMsg( '$wgUser reassignment detected', '1.37', false, 3 );
154 }
155 }
156}
157
159class_alias( StubGlobalUser::class, 'StubGlobalUser' );
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:545
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Stub object for the global user ($wgUser) that makes it possible to change the relevant underlying ob...
static setUser( $user)
Reset the stub global user to a different "real" user object, while ensuring that any method calls on...
static getRealUser( $globalUser)
Get the relevant "real" user object based on either a User object or a MediaWiki\StubObject\StubGloba...
_unstub( $name='_unstub', $level=2)
This function creates a new object of the real class and replace it in the global variable.
Class to implement stub globals, which are globals that delay loading the their associated module cod...
internal since 1.36
Definition User.php:93