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