| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Block; |
| 4 |
|
| 5 |
use MediaWiki\Request\WebRequest; |
| 6 |
use MediaWiki\User\UserIdentity; |
| 7 |
use Stringable; |
| 8 |
use WeakReference; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class BlockCacheKey implements Stringable { |
| 14 |
|
| 15 |
private bool $hasRequest; |
| 16 |
|
| 17 |
private ?WeakReference $requestRef; |
| 18 |
|
| 19 |
private WeakReference $userRef; |
| 20 |
|
| 21 |
private bool $isAnon; |
| 22 |
|
| 23 |
private bool $fromPrimary; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function __construct( ?WebRequest $request, UserIdentity $user, bool $fromPrimary ) { |
| 31 |
$this->requestRef = $request ? WeakReference::create( $request ) : null; |
| 32 |
$this->hasRequest = (bool)$request; |
| 33 |
$this->userRef = WeakReference::create( $user ); |
| 34 |
$this->isAnon = $user->getId() === 0; |
| 35 |
$this->fromPrimary = $fromPrimary; |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
private function requestEquals( self $other ): bool { |
| 45 |
if ( $this->hasRequest !== $other->hasRequest ) { |
| 46 |
return false; |
| 47 |
} elseif ( $this->hasRequest ) { |
| 48 |
return $this->requestRef->get() |
| 49 |
&& $this->requestRef->get() === $other->requestRef->get(); |
| 50 |
} else { |
| 51 |
return true; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function matchesStored( self $storedKey ): bool { |
| 66 |
return $this->requestEquals( $storedKey ) |
| 67 |
&& $this->userRef->get() |
| 68 |
&& $this->userRef->get() === $storedKey->userRef->get() |
| 69 |
&& ( |
| 70 |
( $this->fromPrimary === $storedKey->fromPrimary ) |
| 71 |
|
| 72 |
|
| 73 |
|| ( !$this->fromPrimary && $storedKey->fromPrimary ) |
| 74 |
); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function getPartialKey(): string { |
| 84 |
if ( $this->hasRequest ) { |
| 85 |
return 'req'; |
| 86 |
} |
| 87 |
return $this->isAnon ? 'anon' : 'user'; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function isUser( UserIdentity $user ): bool { |
| 101 |
return $this->userRef->get() === $user; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private static function dumpWeakRef( ?WeakReference $ref ): string { |
| 109 |
if ( $ref === null ) { |
| 110 |
return 'none'; |
| 111 |
} else { |
| 112 |
$obj = $ref->get(); |
| 113 |
if ( $obj ) { |
| 114 |
return '#' . spl_object_id( $obj ); |
| 115 |
} else { |
| 116 |
return 'expired'; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public function __toString(): string { |
| 127 |
return 'BlockCacheKey{' . |
| 128 |
'request=' . self::dumpWeakRef( $this->requestRef ) . ',' . |
| 129 |
'user=' . self::dumpWeakRef( $this->userRef ) . ',' . |
| 130 |
( $this->fromPrimary ? 'primary' : 'replica' ) . |
| 131 |
'}'; |
| 132 |
} |
| 133 |
} |