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
55 // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
56 public function _newObject() {
57 // Based on MediaWiki\StubObject\DeprecatedGlobal::_newObject
58 /*
59 * Put the caller offset for wfDeprecated as 6, as
60 * that gives the function that uses this object, since:
61 *
62 * 1 = this function ( _newObject )
63 * 2 = MediaWiki\StubObject\StubGlobalUser::_unstub
64 * 3 = MediaWiki\StubObject\StubObject::_call
65 * 4 = MediaWiki\StubObject\StubObject::__call
66 * 5 = MediaWiki\StubObject\StubGlobalUser::<method of global called>
67 * 6 = Actual function using the global.
68 * (the same applies to _get/__get or _set/__set instead of _call/__call)
69 *
70 * Of course its theoretically possible to have other call
71 * sequences for this method, but that seems to be
72 * rather unlikely.
73 */
74 // Officially deprecated since 1.35
75 wfDeprecated( '$wgUser', '1.35', false, 6 );
76 return $this->realUser;
77 }
78
85 public static function setUser( $user ) {
86 // This is intended to be interacting with the deprecated global
87 // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgUser
88 global $wgUser;
89
90 self::$destructorDeprecationDisarmed = true;
91 // Supports MediaWiki\StubObject\StubGlobalUser parameter in case something fetched the existing value of
92 // $wgUser, set it to something else, and now is trying to restore it
94 $wgUser = new self( $realUser );
95 self::$destructorDeprecationDisarmed = false;
96 }
97
106 public static function getRealUser( $globalUser ): User {
107 if ( $globalUser instanceof StubGlobalUser ) {
108 return $globalUser->realUser;
109 } elseif ( $globalUser instanceof User ) {
110 return $globalUser;
111 } else {
112 throw new InvalidArgumentException(
113 '$globalUser must be a User (or MediaWiki\StubObject\StubGlobalUser), got ' .
114 ( is_object( $globalUser ) ? get_class( $globalUser ) : gettype( $globalUser ) )
115 );
116 }
117 }
118
137 // phpcs:ignore PSR2.Methods.MethodDeclaration.Underscore
138 public function _unstub( $name = '_unstub', $level = 2 ) {
139 if ( !$GLOBALS[$this->global] instanceof self ) {
140 return $GLOBALS[$this->global]; // already unstubbed.
141 }
142
143 $caller = wfGetCaller( $level );
144 wfDebug( "Unstubbing \${$this->global} on call of "
145 . "\${$this->global}::$name from $caller" );
146 $GLOBALS[$this->global] = $this->_newObject();
147 return $GLOBALS[$this->global];
148 }
149
150 public function __destruct() {
151 if ( !self::$destructorDeprecationDisarmed ) {
152 wfDeprecatedMsg( '$wgUser reassignment detected', '1.37', false, 3 );
153 }
154 }
155}
156
158class_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:543
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