Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 37 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
GlobalBlock | |
0.00% |
0 / 37 |
|
0.00% |
0 / 11 |
462 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
getBy | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getByName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
getBlocker | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getIdentifier | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getXff | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setGlobalBlocker | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
56 | |||
appliesToRight | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
appliesToPasswordReset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
isCreateAccountBlocked | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\GlobalBlocking; |
4 | |
5 | use CentralIdLookup; |
6 | use MediaWiki\Block\AbstractBlock; |
7 | use MediaWiki\MediaWikiServices; |
8 | use MediaWiki\User\User; |
9 | use MediaWiki\User\UserIdentity; |
10 | use MediaWiki\WikiMap\WikiMap; |
11 | use stdClass; |
12 | |
13 | class GlobalBlock extends AbstractBlock { |
14 | /** @var int */ |
15 | private $id; |
16 | |
17 | /** @var array */ |
18 | protected $error; |
19 | |
20 | /** @var bool */ |
21 | protected $xff; |
22 | |
23 | /** @var UserIdentity|null */ |
24 | private $blocker; |
25 | |
26 | /** |
27 | * @param stdClass $block |
28 | * @param array $options |
29 | */ |
30 | public function __construct( stdClass $block, $options ) { |
31 | parent::__construct( $options ); |
32 | |
33 | $db = MediaWikiServices::getInstance() |
34 | ->getDBLoadBalancerFactory() |
35 | ->getMainLB( $this->getWikiId() ) |
36 | ->getConnection( DB_REPLICA, [], $this->getWikiId() ); |
37 | $this->setExpiry( $db->decodeExpiry( $options['expiry'] ) ); |
38 | |
39 | $this->id = $block->gb_id; |
40 | $this->xff = (bool)$options['xff']; |
41 | $this->setGlobalBlocker( $block ); |
42 | } |
43 | |
44 | /** |
45 | * @inheritDoc |
46 | */ |
47 | public function getBy( $wikiId = self::LOCAL ): int { |
48 | $this->assertWiki( $wikiId ); |
49 | return ( $this->blocker ) ? $this->blocker->getId( $wikiId ) : 0; |
50 | } |
51 | |
52 | /** |
53 | * @inheritDoc |
54 | */ |
55 | public function getByName(): string { |
56 | return ( $this->blocker ) ? $this->blocker->getName() : ''; |
57 | } |
58 | |
59 | /** |
60 | * @inheritDoc |
61 | */ |
62 | public function getBlocker(): ?UserIdentity { |
63 | return $this->blocker; |
64 | } |
65 | |
66 | /** |
67 | * @inheritDoc |
68 | */ |
69 | public function getIdentifier( $wikiId = self::LOCAL ) { |
70 | return $this->getId( $wikiId ); |
71 | } |
72 | |
73 | /** @inheritDoc */ |
74 | public function getId( $wikiId = self::LOCAL ): ?int { |
75 | return $this->id; |
76 | } |
77 | |
78 | public function getXff() { |
79 | return $this->xff; |
80 | } |
81 | |
82 | /** |
83 | * @param stdClass $block DB row from globalblocks table |
84 | */ |
85 | public function setGlobalBlocker( stdClass $block ) { |
86 | $lookup = MediaWikiServices::getInstance() |
87 | ->getCentralIdLookupFactory() |
88 | ->getLookup(); |
89 | |
90 | $user = $lookup->localUserFromCentralId( $block->gb_by_central_id, CentralIdLookup::AUDIENCE_RAW ); |
91 | |
92 | // If the block was inserted from this wiki, then we know the blocker exists |
93 | if ( $user && $block->gb_by_wiki === WikiMap::getCurrentWikiId() ) { |
94 | $this->blocker = $user; |
95 | return; |
96 | } |
97 | |
98 | // If the blocker is the same user on the foreign wiki and the current wiki |
99 | // then we can use the username |
100 | if ( $user && $user->getId() && $lookup->isAttached( $user ) |
101 | && $lookup->isAttached( $user, $block->gb_by_wiki ) |
102 | ) { |
103 | $this->blocker = $user; |
104 | return; |
105 | } |
106 | |
107 | $username = $lookup->nameFromCentralId( $block->gb_by_central_id, CentralIdLookup::AUDIENCE_RAW ); |
108 | |
109 | // They don't exist locally, so we need to use an interwiki username |
110 | $this->blocker = User::newFromName( "{$block->gb_by_wiki}>{$username}", false ); |
111 | } |
112 | |
113 | /** |
114 | * @inheritDoc |
115 | */ |
116 | public function appliesToRight( $right ) { |
117 | $res = parent::appliesToRight( $right ); |
118 | switch ( $right ) { |
119 | case 'upload': |
120 | return true; |
121 | case 'createaccount': |
122 | return true; |
123 | } |
124 | return $res; |
125 | } |
126 | |
127 | /** |
128 | * @inheritDoc |
129 | */ |
130 | public function appliesToPasswordReset() { |
131 | return true; |
132 | } |
133 | |
134 | /** |
135 | * @inheritDoc |
136 | */ |
137 | public function isCreateAccountBlocked( $x = null ): bool { |
138 | return true; |
139 | } |
140 | } |