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 * @param array $options Options to pass to the select query.
38 * @return Restriction[]
40 public static function loadByBlockId( $blockId, IDatabase
$db = null ) {
41 if ( is_null( $blockId ) ||
$blockId === [] ) {
45 $db = $db ?
: wfGetDb( DB_REPLICA
);
47 $result = $db->select(
48 [ 'ipblocks_restrictions', 'page' ],
49 [ 'ir_ipb_id', 'ir_type', 'ir_value', 'page_namespace', 'page_title' ],
50 [ 'ir_ipb_id' => $blockId ],
53 [ 'page' => [ 'LEFT JOIN', [ 'ir_type' => PageRestriction
::TYPE_ID
, 'ir_value=page_id' ] ] ]
56 return self
::resultToRestrictions( $result );
60 * Inserts the restrictions into the database.
62 * @param Restriction[] $restrictions
65 public static function insert( array $restrictions ) {
66 if ( empty( $restrictions ) ) {
71 foreach ( $restrictions as $restriction ) {
72 if ( !$restriction instanceof Restriction
) {
75 $rows[] = $restriction->toRow();
78 if ( empty( $rows ) ) {
82 $dbw = wfGetDB( DB_MASTER
);
85 'ipblocks_restrictions',
93 * Updates the list of restrictions. This method does not allow removing all
94 * of the restrictions. To do that, use ::deleteByBlockId().
96 * @param Restriction[] $restrictions
99 public static function update( array $restrictions ) {
100 $dbw = wfGetDB( DB_MASTER
);
102 $dbw->startAtomic( __METHOD__
);
104 // Organize the restrictions by blockid.
105 $restrictionList = self
::restrictionsByBlockId( $restrictions );
107 // Load the existing restrictions and organize by block id. Any block ids
108 // that were passed into this function will be used to load all of the
109 // existing restrictions. This list might be the same, or may be completely
112 $blockIds = array_keys( $restrictionList );
113 if ( !empty( $blockIds ) ) {
114 $result = $dbw->select(
115 [ 'ipblocks_restrictions' ],
116 [ 'ir_ipb_id', 'ir_type', 'ir_value' ],
117 [ 'ir_ipb_id' => $blockIds ],
122 $existingList = self
::restrictionsByBlockId(
123 self
::resultToRestrictions( $result )
128 // Perform the actions on a per block-id basis.
129 foreach ( $restrictionList as $blockId => $blockRestrictions ) {
130 // Insert all of the restrictions first, ignoring ones that already exist.
131 $success = self
::insert( $blockRestrictions );
133 // Update the result. The first false is the result, otherwise, true.
134 $result = $success && $result;
136 $restrictionsToRemove = self
::restrictionsToRemove(
137 $existingList[$blockId] ??
[],
141 // Nothing to remove.
142 if ( empty( $restrictionsToRemove ) ) {
146 $success = self
::delete( $restrictionsToRemove );
148 // Update the result. The first false is the result, otherwise, true.
149 $result = $success && $result;
152 $dbw->endAtomic( __METHOD__
);
158 * Updates the list of restrictions by parent id.
160 * @param int $parentBlockId
161 * @param Restriction[] $restrictions
164 public static function updateByParentBlockId( $parentBlockId, array $restrictions ) {
165 // If removing all of the restrictions, then just delete them all.
166 if ( empty( $restrictions ) ) {
167 return self
::deleteByParentBlockId( $parentBlockId );
170 $parentBlockId = (int)$parentBlockId;
172 $db = wfGetDb( DB_MASTER
);
174 $db->startAtomic( __METHOD__
);
176 $blockIds = $db->selectFieldValues(
179 [ 'ipb_parent_block_id' => $parentBlockId ],
185 foreach ( $blockIds as $id ) {
186 $success = self
::update( self
::setBlockId( $id, $restrictions ) );
187 // Update the result. The first false is the result, otherwise, true.
188 $result = $success && $result;
191 $db->endAtomic( __METHOD__
);
197 * Delete the restrictions.
199 * @param Restriction[]|null $restrictions
200 * @throws MWException
203 public static function delete( array $restrictions ) {
204 $dbw = wfGetDB( DB_MASTER
);
206 foreach ( $restrictions as $restriction ) {
207 if ( !$restriction instanceof Restriction
) {
211 $success = $dbw->delete(
212 'ipblocks_restrictions',
213 // The restriction row is made up of a compound primary key. Therefore,
214 // the row and the delete conditions are the same.
215 $restriction->toRow(),
218 // Update the result. The first false is the result, otherwise, true.
219 $result = $success && $result;
226 * Delete the restrictions by Block ID.
228 * @param int|array $blockId
229 * @throws MWException
232 public static function deleteByBlockId( $blockId ) {
233 $dbw = wfGetDB( DB_MASTER
);
235 'ipblocks_restrictions',
236 [ 'ir_ipb_id' => $blockId ],
242 * Delete the restrictions by Parent Block ID.
244 * @param int|array $parentBlockId
245 * @throws MWException
248 public static function deleteByParentBlockId( $parentBlockId ) {
249 $dbw = wfGetDB( DB_MASTER
);
250 return $dbw->deleteJoin(
251 'ipblocks_restrictions',
255 [ 'ipb_parent_block_id' => $parentBlockId ],
261 * Checks if two arrays of Restrictions are effectively equal. This is a loose
262 * equality check as the restrictions do not have to contain the same block
265 * @param Restriction[] $a
266 * @param Restriction[] $b
269 public static function equals( array $a, array $b ) {
270 $filter = function ( $restriction ) {
271 return $restriction instanceof Restriction
;
274 // Ensure that every item in the array is a Restriction. This prevents a
275 // fatal error from calling Restriction::getHash if something in the array
276 // is not a restriction.
277 $a = array_filter( $a, $filter );
278 $b = array_filter( $b, $filter );
280 $aCount = count( $a );
281 $bCount = count( $b );
283 // If the count is different, then they are obviously a different set.
284 if ( $aCount !== $bCount ) {
288 // If both sets contain no items, then they are the same set.
289 if ( $aCount === 0 && $bCount === 0 ) {
293 $hasher = function ( $r ) {
294 return $r->getHash();
297 $aHashes = array_map( $hasher, $a );
298 $bHashes = array_map( $hasher, $b );
303 return $aHashes === $bHashes;
307 * Set the blockId on a set of restrictions and return a new set.
309 * @param int $blockId
310 * @param Restriction[] $restrictions
311 * @return Restriction[]
313 public static function setBlockId( $blockId, array $restrictions ) {
314 $blockRestrictions = [];
316 foreach ( $restrictions as $restriction ) {
317 if ( !$restriction instanceof Restriction
) {
321 // Clone the restriction so any references to the current restriction are
322 // not suddenly changed to a different blockId.
323 $restriction = clone $restriction;
324 $restriction->setBlockId( $blockId );
326 $blockRestrictions[] = $restriction;
329 return $blockRestrictions;
333 * Get the restrictions that should be removed, which are existing
334 * restrictions that are not in the new list of restrictions.
336 * @param Restriction[] $existing
337 * @param Restriction[] $new
340 private static function restrictionsToRemove( array $existing, array $new ) {
341 return array_filter( $existing, function ( $e ) use ( $new ) {
342 foreach ( $new as $restriction ) {
343 if ( !$restriction instanceof Restriction
) {
347 if ( $restriction->equals( $e ) ) {
357 * Converts an array of restrictions to an associative array of restrictions
358 * where the keys are the block ids.
360 * @param Restriction[] $restrictions
363 private static function restrictionsByBlockId( array $restrictions ) {
364 $blockRestrictions = [];
366 foreach ( $restrictions as $restriction ) {
367 // Ensure that all of the items in the array are restrictions.
368 if ( !$restriction instanceof Restriction
) {
372 if ( !isset( $blockRestrictions[$restriction->getBlockId()] ) ) {
373 $blockRestrictions[$restriction->getBlockId()] = [];
376 $blockRestrictions[$restriction->getBlockId()][] = $restriction;
379 return $blockRestrictions;
383 * Convert an Result Wrapper to an array of restrictions.
385 * @param IResultWrapper $result
386 * @return Restriction[]
388 private static function resultToRestrictions( IResultWrapper
$result ) {
390 foreach ( $result as $row ) {
391 $restriction = self
::rowToRestriction( $row );
393 if ( !$restriction ) {
397 $restrictions[] = $restriction;
400 return $restrictions;
404 * Convert a result row from the database into a restriction object.
406 * @param \stdClass $row
407 * @return Restriction|null
409 private static function rowToRestriction( \stdClass
$row ) {
410 switch ( $row->ir_type
) {
411 case PageRestriction
::TYPE_ID
:
412 return PageRestriction
::newFromRow( $row );