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\PageRestriction
;
26 use MediaWiki\Block\Restriction\Restriction
;
27 use Wikimedia\Rdbms\IResultWrapper
;
28 use Wikimedia\Rdbms\IDatabase
;
30 class BlockRestriction
{
33 * Retrieves the restrictions from the database by block id.
35 * @param int|array $blockId
36 * @param IDatabase|null $db
37 * @return Restriction[]
39 public static function loadByBlockId( $blockId, IDatabase
$db = null ) {
40 if ( is_null( $blockId ) ||
$blockId === [] ) {
44 $db = $db ?
: wfGetDb( DB_REPLICA
);
46 $result = $db->select(
47 [ 'ipblocks_restrictions', 'page' ],
48 [ 'ir_ipb_id', 'ir_type', 'ir_value', 'page_namespace', 'page_title' ],
49 [ 'ir_ipb_id' => $blockId ],
52 [ 'page' => [ 'LEFT JOIN', [ 'ir_type' => PageRestriction
::TYPE_ID
, 'ir_value=page_id' ] ] ]
55 return self
::resultToRestrictions( $result );
59 * Inserts the restrictions into the database.
61 * @param Restriction[] $restrictions
64 public static function insert( array $restrictions ) {
65 if ( empty( $restrictions ) ) {
70 foreach ( $restrictions as $restriction ) {
71 if ( !$restriction instanceof Restriction
) {
74 $rows[] = $restriction->toRow();
77 if ( empty( $rows ) ) {
81 $dbw = wfGetDB( DB_MASTER
);
84 'ipblocks_restrictions',
92 * Updates the list of restrictions. This method does not allow removing all
93 * of the restrictions. To do that, use ::deleteByBlockId().
95 * @param Restriction[] $restrictions
98 public static function update( array $restrictions ) {
99 $dbw = wfGetDB( DB_MASTER
);
101 $dbw->startAtomic( __METHOD__
);
103 // Organize the restrictions by blockid.
104 $restrictionList = self
::restrictionsByBlockId( $restrictions );
106 // Load the existing restrictions and organize by block id. Any block ids
107 // that were passed into this function will be used to load all of the
108 // existing restrictions. This list might be the same, or may be completely
111 $blockIds = array_keys( $restrictionList );
112 if ( !empty( $blockIds ) ) {
113 $result = $dbw->select(
114 [ 'ipblocks_restrictions' ],
115 [ 'ir_ipb_id', 'ir_type', 'ir_value' ],
116 [ 'ir_ipb_id' => $blockIds ],
121 $existingList = self
::restrictionsByBlockId(
122 self
::resultToRestrictions( $result )
127 // Perform the actions on a per block-id basis.
128 foreach ( $restrictionList as $blockId => $blockRestrictions ) {
129 // Insert all of the restrictions first, ignoring ones that already exist.
130 $success = self
::insert( $blockRestrictions );
132 // Update the result. The first false is the result, otherwise, true.
133 $result = $success && $result;
135 $restrictionsToRemove = self
::restrictionsToRemove(
136 $existingList[$blockId] ??
[],
140 // Nothing to remove.
141 if ( empty( $restrictionsToRemove ) ) {
145 $success = self
::delete( $restrictionsToRemove );
147 // Update the result. The first false is the result, otherwise, true.
148 $result = $success && $result;
151 $dbw->endAtomic( __METHOD__
);
157 * Updates the list of restrictions by parent id.
159 * @param int $parentBlockId
160 * @param Restriction[] $restrictions
163 public static function updateByParentBlockId( $parentBlockId, array $restrictions ) {
164 // If removing all of the restrictions, then just delete them all.
165 if ( empty( $restrictions ) ) {
166 return self
::deleteByParentBlockId( $parentBlockId );
169 $parentBlockId = (int)$parentBlockId;
171 $db = wfGetDb( DB_MASTER
);
173 $db->startAtomic( __METHOD__
);
175 $blockIds = $db->selectFieldValues(
178 [ 'ipb_parent_block_id' => $parentBlockId ],
184 foreach ( $blockIds as $id ) {
185 $success = self
::update( self
::setBlockId( $id, $restrictions ) );
186 // Update the result. The first false is the result, otherwise, true.
187 $result = $success && $result;
190 $db->endAtomic( __METHOD__
);
196 * Delete the restrictions.
198 * @param Restriction[]|null $restrictions
199 * @throws MWException
202 public static function delete( array $restrictions ) {
203 $dbw = wfGetDB( DB_MASTER
);
205 foreach ( $restrictions as $restriction ) {
206 if ( !$restriction instanceof Restriction
) {
210 $success = $dbw->delete(
211 'ipblocks_restrictions',
212 // The restriction row is made up of a compound primary key. Therefore,
213 // the row and the delete conditions are the same.
214 $restriction->toRow(),
217 // Update the result. The first false is the result, otherwise, true.
218 $result = $success && $result;
225 * Delete the restrictions by Block ID.
227 * @param int|array $blockId
228 * @throws MWException
231 public static function deleteByBlockId( $blockId ) {
232 $dbw = wfGetDB( DB_MASTER
);
234 'ipblocks_restrictions',
235 [ 'ir_ipb_id' => $blockId ],
241 * Delete the restrictions by Parent Block ID.
243 * @param int|array $parentBlockId
244 * @throws MWException
247 public static function deleteByParentBlockId( $parentBlockId ) {
248 $dbw = wfGetDB( DB_MASTER
);
249 return $dbw->deleteJoin(
250 'ipblocks_restrictions',
254 [ 'ipb_parent_block_id' => $parentBlockId ],
260 * Checks if two arrays of Restrictions are effectively equal. This is a loose
261 * equality check as the restrictions do not have to contain the same block
264 * @param Restriction[] $a
265 * @param Restriction[] $b
268 public static function equals( array $a, array $b ) {
269 $filter = function ( $restriction ) {
270 return $restriction instanceof Restriction
;
273 // Ensure that every item in the array is a Restriction. This prevents a
274 // fatal error from calling Restriction::getHash if something in the array
275 // is not a restriction.
276 $a = array_filter( $a, $filter );
277 $b = array_filter( $b, $filter );
279 $aCount = count( $a );
280 $bCount = count( $b );
282 // If the count is different, then they are obviously a different set.
283 if ( $aCount !== $bCount ) {
287 // If both sets contain no items, then they are the same set.
288 if ( $aCount === 0 && $bCount === 0 ) {
292 $hasher = function ( $r ) {
293 return $r->getHash();
296 $aHashes = array_map( $hasher, $a );
297 $bHashes = array_map( $hasher, $b );
302 return $aHashes === $bHashes;
306 * Set the blockId on a set of restrictions and return a new set.
308 * @param int $blockId
309 * @param Restriction[] $restrictions
310 * @return Restriction[]
312 public static function setBlockId( $blockId, array $restrictions ) {
313 $blockRestrictions = [];
315 foreach ( $restrictions as $restriction ) {
316 if ( !$restriction instanceof Restriction
) {
320 // Clone the restriction so any references to the current restriction are
321 // not suddenly changed to a different blockId.
322 $restriction = clone $restriction;
323 $restriction->setBlockId( $blockId );
325 $blockRestrictions[] = $restriction;
328 return $blockRestrictions;
332 * Get the restrictions that should be removed, which are existing
333 * restrictions that are not in the new list of restrictions.
335 * @param Restriction[] $existing
336 * @param Restriction[] $new
339 private static function restrictionsToRemove( array $existing, array $new ) {
340 return array_filter( $existing, function ( $e ) use ( $new ) {
341 foreach ( $new as $restriction ) {
342 if ( !$restriction instanceof Restriction
) {
346 if ( $restriction->equals( $e ) ) {
356 * Converts an array of restrictions to an associative array of restrictions
357 * where the keys are the block ids.
359 * @param Restriction[] $restrictions
362 private static function restrictionsByBlockId( array $restrictions ) {
363 $blockRestrictions = [];
365 foreach ( $restrictions as $restriction ) {
366 // Ensure that all of the items in the array are restrictions.
367 if ( !$restriction instanceof Restriction
) {
371 if ( !isset( $blockRestrictions[$restriction->getBlockId()] ) ) {
372 $blockRestrictions[$restriction->getBlockId()] = [];
375 $blockRestrictions[$restriction->getBlockId()][] = $restriction;
378 return $blockRestrictions;
382 * Convert an Result Wrapper to an array of restrictions.
384 * @param IResultWrapper $result
385 * @return Restriction[]
387 private static function resultToRestrictions( IResultWrapper
$result ) {
389 foreach ( $result as $row ) {
390 $restriction = self
::rowToRestriction( $row );
392 if ( !$restriction ) {
396 $restrictions[] = $restriction;
399 return $restrictions;
403 * Convert a result row from the database into a restriction object.
405 * @param \stdClass $row
406 * @return Restriction|null
408 private static function rowToRestriction( \stdClass
$row ) {
409 switch ( $row->ir_type
) {
410 case PageRestriction
::TYPE_ID
:
411 return PageRestriction
::newFromRow( $row );