MediaWiki master
StubGlobalUser.php
Go to the documentation of this file.
1<?php
2
3// phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
4
10namespace MediaWiki\StubObject;
11
12use InvalidArgumentException;
14
26
28 public static $destructorDeprecationDisarmed = false;
29
31 public $realUser;
32
33 public function __construct( User $realUser ) {
34 parent::__construct( 'wgUser' );
35 $this->realUser = $realUser;
36 }
37
42 public function _newObject() {
43 // Based on MediaWiki\StubObject\DeprecatedGlobal::_newObject
44 /*
45 * Put the caller offset for wfDeprecated as 6, as
46 * that gives the function that uses this object, since:
47 *
48 * 1 = this function ( _newObject )
49 * 2 = MediaWiki\StubObject\StubGlobalUser::_unstub
50 * 3 = MediaWiki\StubObject\StubObject::_call
51 * 4 = MediaWiki\StubObject\StubObject::__call
52 * 5 = MediaWiki\StubObject\StubGlobalUser::<method of global called>
53 * 6 = Actual function using the global.
54 * (the same applies to _get/__get or _set/__set instead of _call/__call)
55 *
56 * Of course its theoretically possible to have other call
57 * sequences for this method, but that seems to be
58 * rather unlikely.
59 */
60 // Officially deprecated since 1.35
61 wfDeprecated( '$wgUser', '1.35', false, 6 );
62 return $this->realUser;
63 }
64
71 public static function setUser( $user ) {
72 // This is intended to be interacting with the deprecated global
73 // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgUser
74 global $wgUser;
75
76 self::$destructorDeprecationDisarmed = true;
77 // Supports MediaWiki\StubObject\StubGlobalUser parameter in case something fetched the existing value of
78 // $wgUser, set it to something else, and now is trying to restore it
80 $wgUser = new self( $realUser );
81 self::$destructorDeprecationDisarmed = false;
82 }
83
92 public static function getRealUser( $globalUser ): User {
93 if ( $globalUser instanceof StubGlobalUser ) {
94 return $globalUser->realUser;
95 } elseif ( $globalUser instanceof User ) {
96 return $globalUser;
97 } else {
98 throw new InvalidArgumentException(
99 '$globalUser must be a User (or MediaWiki\StubObject\StubGlobalUser), got ' .
100 get_debug_type( $globalUser )
101 );
102 }
103 }
104
123 public function _unstub( $name = '_unstub', $level = 2 ) {
124 if ( !$GLOBALS[$this->global] instanceof self ) {
125 return $GLOBALS[$this->global]; // already unstubbed.
126 }
127
128 $caller = wfGetCaller( $level );
129 wfDebug( "Unstubbing \${$this->global} on call of "
130 . "\${$this->global}::$name from $caller" );
131 $GLOBALS[$this->global] = $this->_newObject();
132 return $GLOBALS[$this->global];
133 }
134
135 public function __destruct() {
136 if ( !self::$destructorDeprecationDisarmed ) {
137 wfDeprecatedMsg( '$wgUser reassignment detected', '1.37', false, 3 );
138 }
139 }
140}
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:558
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
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...
User class for the MediaWiki software.
Definition User.php:130