| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\StubObject; |
| 22 | |
| 23 | use InvalidArgumentException; |
| 24 | use MediaWiki\User\User; |
| 25 | |
| 26 | /** |
| 27 | * Stub object for the global user ($wgUser) that makes it possible to change the |
| 28 | * relevant underlying object while still ensuring that deprecation warnings will |
| 29 | * be emitted upon method calls. |
| 30 | * |
| 31 | * @internal Will be removed in 1.38 |
| 32 | * |
| 33 | * @since 1.37 |
| 34 | * @author Danny712 |
| 35 | */ |
| 36 | class StubGlobalUser extends StubObject { |
| 37 | |
| 38 | /** @var bool */ |
| 39 | public static $destructorDeprecationDisarmed = false; |
| 40 | |
| 41 | /** @var User */ |
| 42 | public $realUser; |
| 43 | |
| 44 | /** |
| 45 | * @param User $realUser |
| 46 | */ |
| 47 | public function __construct( User $realUser ) { |
| 48 | parent::__construct( 'wgUser' ); |
| 49 | $this->realUser = $realUser; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @deprecated since 1.35. |
| 54 | * @return User |
| 55 | */ |
| 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 | |
| 80 | /** |
| 81 | * Reset the stub global user to a different "real" user object, while ensuring that |
| 82 | * any method calls on that object will still trigger deprecation notices. |
| 83 | * |
| 84 | * @param StubGlobalUser|User $user |
| 85 | */ |
| 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 |
| 94 | $realUser = self::getRealUser( $user ); |
| 95 | $wgUser = new self( $realUser ); |
| 96 | self::$destructorDeprecationDisarmed = false; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the relevant "real" user object based on either a User object or a MediaWiki\StubObject\StubGlobalUser |
| 101 | * wrapper. Bypasses deprecation notices from converting a MediaWiki\StubObject\StubGlobalUser to an actual |
| 102 | * user, and does not change $wgUser. |
| 103 | * |
| 104 | * @param StubGlobalUser|User $globalUser |
| 105 | * @return User |
| 106 | */ |
| 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 | get_debug_type( $globalUser ) |
| 116 | ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * This function creates a new object of the real class and replace it in |
| 122 | * the global variable. |
| 123 | * This is public, for the convenience of external callers wishing to access |
| 124 | * properties, e.g. eval.php |
| 125 | * |
| 126 | * Overriding MediaWiki\StubObject\StubObject::_unstub because for some reason that thinks there is |
| 127 | * an unstub loop when trying to use the magic __set() logic, but there isn't |
| 128 | * any loop because _newObject() returns a specific instance of User rather |
| 129 | * than calling any methods that could then try to use $wgUser. The main difference |
| 130 | * between this and the parent method is that we don't try to check for |
| 131 | * recursion loops. |
| 132 | * |
| 133 | * @param string $name Name of the method called in this object. |
| 134 | * @param int $level Level to go in the stack trace to get the function |
| 135 | * who called this function. |
| 136 | * @return User The unstubbed version |
| 137 | */ |
| 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 | |
| 158 | /** @deprecated class alias since 1.40 */ |
| 159 | class_alias( StubGlobalUser::class, 'StubGlobalUser' ); |