41 PageRestriction::TYPE_ID => PageRestriction::class,
42 NamespaceRestriction::TYPE_ID => NamespaceRestriction::class,
43 ActionRestriction::TYPE_ID => ActionRestriction::class,
67 if ( $blockId ===
null || $blockId === [] ) {
71 $db = $db ?: $this->loadBalancer->getConnectionRef(
DB_REPLICA );
73 $result = $db->select(
74 [
'ipblocks_restrictions',
'page' ],
75 [
'ir_ipb_id',
'ir_type',
'ir_value',
'page_namespace',
'page_title' ],
76 [
'ir_ipb_id' => $blockId ],
79 [
'page' => [
'LEFT JOIN', [
'ir_type' => PageRestriction::TYPE_ID,
'ir_value=page_id' ] ] ]
92 public function insert( array $restrictions ) {
93 if ( !$restrictions ) {
98 foreach ( $restrictions as $restriction ) {
102 $rows[] = $restriction->toRow();
109 $dbw = $this->loadBalancer->getConnectionRef(
DB_PRIMARY );
112 'ipblocks_restrictions',
129 public function update( array $restrictions ) {
130 $dbw = $this->loadBalancer->getConnectionRef(
DB_PRIMARY );
132 $dbw->startAtomic( __METHOD__ );
142 $blockIds = array_keys( $restrictionList );
143 if ( !empty( $blockIds ) ) {
144 $result = $dbw->select(
145 [
'ipblocks_restrictions' ],
146 [
'ir_ipb_id',
'ir_type',
'ir_value' ],
147 [
'ir_ipb_id' => $blockIds ],
159 foreach ( $restrictionList as $blockId => $blockRestrictions ) {
167 $existingList[$blockId] ?? [],
171 if ( empty( $restrictionsToRemove ) ) {
175 $success = $this->
delete( $restrictionsToRemove );
181 $dbw->endAtomic( __METHOD__ );
196 if ( empty( $restrictions ) ) {
200 $parentBlockId = (int)$parentBlockId;
202 $db = $this->loadBalancer->getConnectionRef(
DB_PRIMARY );
204 $db->startAtomic( __METHOD__ );
206 $blockIds = $db->selectFieldValues(
209 [
'ipb_parent_block_id' => $parentBlockId ],
215 foreach ( $blockIds as $id ) {
221 $db->endAtomic( __METHOD__ );
234 public function delete( array $restrictions ) {
235 $dbw = $this->loadBalancer->getConnectionRef(
DB_PRIMARY );
237 foreach ( $restrictions as $restriction ) {
243 'ipblocks_restrictions',
246 $restriction->toRow(),
265 $dbw = $this->loadBalancer->getConnectionRef(
DB_PRIMARY );
267 'ipblocks_restrictions',
268 [
'ir_ipb_id' => $blockId ],
282 $dbw = $this->loadBalancer->getConnectionRef(
DB_PRIMARY );
283 return $dbw->deleteJoin(
284 'ipblocks_restrictions',
288 [
'ipb_parent_block_id' => $parentBlockId ],
303 public function equals( array $a, array $b ) {
304 $filter =
static function ( $restriction ) {
311 $a = array_filter( $a, $filter );
312 $b = array_filter( $b, $filter );
314 $aCount = count( $a );
315 $bCount = count( $b );
318 if ( $aCount !== $bCount ) {
323 if ( $aCount === 0 && $bCount === 0 ) {
327 $hasher =
static function ( $r ) {
328 return $r->getHash();
331 $aHashes = array_map( $hasher, $a );
332 $bHashes = array_map( $hasher, $b );
337 return $aHashes === $bHashes;
348 public function setBlockId( $blockId, array $restrictions ) {
349 $blockRestrictions = [];
351 foreach ( $restrictions as $restriction ) {
358 $restriction = clone $restriction;
359 $restriction->setBlockId( $blockId );
361 $blockRestrictions[] = $restriction;
364 return $blockRestrictions;
376 return array_filter( $existing,
static function ( $e ) use ( $new ) {
377 foreach ( $new as $restriction ) {
382 if ( $restriction->equals( $e ) ) {
399 $blockRestrictions = [];
401 foreach ( $restrictions as $restriction ) {
407 if ( !isset( $blockRestrictions[$restriction->getBlockId()] ) ) {
408 $blockRestrictions[$restriction->getBlockId()] = [];
411 $blockRestrictions[$restriction->getBlockId()][] = $restriction;
414 return $blockRestrictions;
425 foreach ( $result as $row ) {
428 if ( !$restriction ) {
432 $restrictions[] = $restriction;
435 return $restrictions;
445 if ( array_key_exists( (
int)$row->ir_type, self::TYPES_MAP ) ) {
446 $class = self::TYPES_MAP[ (int)$row->ir_type ];
447 return call_user_func( [ $class,
'newFromRow' ], $row );