3 * Block restriction interface.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Block
;
25 use MediaWiki\Block\Restriction\NamespaceRestriction
;
26 use MediaWiki\Block\Restriction\PageRestriction
;
27 use MediaWiki\Block\Restriction\Restriction
;
29 use Wikimedia\Rdbms\IResultWrapper
;
30 use Wikimedia\Rdbms\IDatabase
;
31 use Wikimedia\Rdbms\ILoadBalancer
;
33 class BlockRestrictionStore
{
36 * Map of all of the restriction types.
39 PageRestriction
::TYPE_ID
=> PageRestriction
::class,
40 NamespaceRestriction
::TYPE_ID
=> NamespaceRestriction
::class,
46 private $loadBalancer;
49 * @param LoadBalancer $loadBalancer load balancer for acquiring database connections
51 public function __construct( ILoadBalancer
$loadBalancer ) {
52 $this->loadBalancer
= $loadBalancer;
56 * Retrieves the restrictions from the database by block id.
59 * @param int|array $blockId
60 * @param IDatabase|null $db
61 * @return Restriction[]
63 public function loadByBlockId( $blockId, IDatabase
$db = null ) {
64 if ( $blockId === null ||
$blockId === [] ) {
68 $db = $db ?
: $this->loadBalancer
->getConnection( DB_REPLICA
);
70 $result = $db->select(
71 [ 'ipblocks_restrictions', 'page' ],
72 [ 'ir_ipb_id', 'ir_type', 'ir_value', 'page_namespace', 'page_title' ],
73 [ 'ir_ipb_id' => $blockId ],
76 [ 'page' => [ 'LEFT JOIN', [ 'ir_type' => PageRestriction
::TYPE_ID
, 'ir_value=page_id' ] ] ]
79 return $this->resultToRestrictions( $result );
83 * Inserts the restrictions into the database.
86 * @param Restriction[] $restrictions
89 public function insert( array $restrictions ) {
90 if ( !$restrictions ) {
95 foreach ( $restrictions as $restriction ) {
96 if ( !$restriction instanceof Restriction
) {
99 $rows[] = $restriction->toRow();
106 $dbw = $this->loadBalancer
->getConnection( DB_MASTER
);
109 'ipblocks_restrictions',
119 * Updates the list of restrictions. This method does not allow removing all
120 * of the restrictions. To do that, use ::deleteByBlockId().
123 * @param Restriction[] $restrictions
126 public function update( array $restrictions ) {
127 $dbw = $this->loadBalancer
->getConnection( DB_MASTER
);
129 $dbw->startAtomic( __METHOD__
);
131 // Organize the restrictions by blockid.
132 $restrictionList = $this->restrictionsByBlockId( $restrictions );
134 // Load the existing restrictions and organize by block id. Any block ids
135 // that were passed into this function will be used to load all of the
136 // existing restrictions. This list might be the same, or may be completely
139 $blockIds = array_keys( $restrictionList );
140 if ( !empty( $blockIds ) ) {
141 $result = $dbw->select(
142 [ 'ipblocks_restrictions' ],
143 [ 'ir_ipb_id', 'ir_type', 'ir_value' ],
144 [ 'ir_ipb_id' => $blockIds ],
149 $existingList = $this->restrictionsByBlockId(
150 $this->resultToRestrictions( $result )
155 // Perform the actions on a per block-id basis.
156 foreach ( $restrictionList as $blockId => $blockRestrictions ) {
157 // Insert all of the restrictions first, ignoring ones that already exist.
158 $success = $this->insert( $blockRestrictions );
160 // Update the result. The first false is the result, otherwise, true.
161 $result = $success && $result;
163 $restrictionsToRemove = $this->restrictionsToRemove(
164 $existingList[$blockId] ??
[],
168 if ( empty( $restrictionsToRemove ) ) {
172 $success = $this->delete( $restrictionsToRemove );
174 // Update the result. The first false is the result, otherwise, true.
175 $result = $success && $result;
178 $dbw->endAtomic( __METHOD__
);
184 * Updates the list of restrictions by parent id.
187 * @param int $parentBlockId
188 * @param Restriction[] $restrictions
191 public function updateByParentBlockId( $parentBlockId, array $restrictions ) {
192 // If removing all of the restrictions, then just delete them all.
193 if ( empty( $restrictions ) ) {
194 return $this->deleteByParentBlockId( $parentBlockId );
197 $parentBlockId = (int)$parentBlockId;
199 $db = $this->loadBalancer
->getConnection( DB_MASTER
);
201 $db->startAtomic( __METHOD__
);
203 $blockIds = $db->selectFieldValues(
206 [ 'ipb_parent_block_id' => $parentBlockId ],
212 foreach ( $blockIds as $id ) {
213 $success = $this->update( $this->setBlockId( $id, $restrictions ) );
214 // Update the result. The first false is the result, otherwise, true.
215 $result = $success && $result;
218 $db->endAtomic( __METHOD__
);
224 * Delete the restrictions.
227 * @param Restriction[]|null $restrictions
228 * @throws MWException
231 public function delete( array $restrictions ) {
232 $dbw = $this->loadBalancer
->getConnection( DB_MASTER
);
234 foreach ( $restrictions as $restriction ) {
235 if ( !$restriction instanceof Restriction
) {
239 $success = $dbw->delete(
240 'ipblocks_restrictions',
241 // The restriction row is made up of a compound primary key. Therefore,
242 // the row and the delete conditions are the same.
243 $restriction->toRow(),
246 // Update the result. The first false is the result, otherwise, true.
247 $result = $success && $result;
254 * Delete the restrictions by Block ID.
257 * @param int|array $blockId
258 * @throws MWException
261 public function deleteByBlockId( $blockId ) {
262 $dbw = $this->loadBalancer
->getConnection( DB_MASTER
);
264 'ipblocks_restrictions',
265 [ 'ir_ipb_id' => $blockId ],
271 * Delete the restrictions by Parent Block ID.
274 * @param int|array $parentBlockId
275 * @throws MWException
278 public function deleteByParentBlockId( $parentBlockId ) {
279 $dbw = $this->loadBalancer
->getConnection( DB_MASTER
);
280 return $dbw->deleteJoin(
281 'ipblocks_restrictions',
285 [ 'ipb_parent_block_id' => $parentBlockId ],
291 * Checks if two arrays of Restrictions are effectively equal. This is a loose
292 * equality check as the restrictions do not have to contain the same block
296 * @param Restriction[] $a
297 * @param Restriction[] $b
300 public function equals( array $a, array $b ) {
301 $filter = function ( $restriction ) {
302 return $restriction instanceof Restriction
;
305 // Ensure that every item in the array is a Restriction. This prevents a
306 // fatal error from calling Restriction::getHash if something in the array
307 // is not a restriction.
308 $a = array_filter( $a, $filter );
309 $b = array_filter( $b, $filter );
311 $aCount = count( $a );
312 $bCount = count( $b );
314 // If the count is different, then they are obviously a different set.
315 if ( $aCount !== $bCount ) {
319 // If both sets contain no items, then they are the same set.
320 if ( $aCount === 0 && $bCount === 0 ) {
324 $hasher = function ( $r ) {
325 return $r->getHash();
328 $aHashes = array_map( $hasher, $a );
329 $bHashes = array_map( $hasher, $b );
334 return $aHashes === $bHashes;
338 * Set the blockId on a set of restrictions and return a new set.
341 * @param int $blockId
342 * @param Restriction[] $restrictions
343 * @return Restriction[]
345 public function setBlockId( $blockId, array $restrictions ) {
346 $blockRestrictions = [];
348 foreach ( $restrictions as $restriction ) {
349 if ( !$restriction instanceof Restriction
) {
353 // Clone the restriction so any references to the current restriction are
354 // not suddenly changed to a different blockId.
355 $restriction = clone $restriction;
356 $restriction->setBlockId( $blockId );
358 $blockRestrictions[] = $restriction;
361 return $blockRestrictions;
365 * Get the restrictions that should be removed, which are existing
366 * restrictions that are not in the new list of restrictions.
368 * @param Restriction[] $existing
369 * @param Restriction[] $new
372 private function restrictionsToRemove( array $existing, array $new ) {
373 return array_filter( $existing, function ( $e ) use ( $new ) {
374 foreach ( $new as $restriction ) {
375 if ( !$restriction instanceof Restriction
) {
379 if ( $restriction->equals( $e ) ) {
389 * Converts an array of restrictions to an associative array of restrictions
390 * where the keys are the block ids.
392 * @param Restriction[] $restrictions
395 private function restrictionsByBlockId( array $restrictions ) {
396 $blockRestrictions = [];
398 foreach ( $restrictions as $restriction ) {
399 // Ensure that all of the items in the array are restrictions.
400 if ( !$restriction instanceof Restriction
) {
404 if ( !isset( $blockRestrictions[$restriction->getBlockId()] ) ) {
405 $blockRestrictions[$restriction->getBlockId()] = [];
408 $blockRestrictions[$restriction->getBlockId()][] = $restriction;
411 return $blockRestrictions;
415 * Convert an Result Wrapper to an array of restrictions.
417 * @param IResultWrapper $result
418 * @return Restriction[]
420 private function resultToRestrictions( IResultWrapper
$result ) {
422 foreach ( $result as $row ) {
423 $restriction = $this->rowToRestriction( $row );
425 if ( !$restriction ) {
429 $restrictions[] = $restriction;
432 return $restrictions;
436 * Convert a result row from the database into a restriction object.
438 * @param \stdClass $row
439 * @return Restriction|null
441 private function rowToRestriction( \stdClass
$row ) {
442 if ( array_key_exists( (int)$row->ir_type
, $this->types
) ) {
443 $class = $this->types
[ (int)$row->ir_type
];
444 return call_user_func( [ $class, 'newFromRow' ], $row );