40 PageRestriction::TYPE_ID => PageRestriction::class,
41 NamespaceRestriction::TYPE_ID => NamespaceRestriction::class,
65 if ( $blockId ===
null || $blockId === [] ) {
69 $db = $db ?: $this->loadBalancer->getConnectionRef(
DB_REPLICA );
71 $result = $db->select(
72 [
'ipblocks_restrictions',
'page' ],
73 [
'ir_ipb_id',
'ir_type',
'ir_value',
'page_namespace',
'page_title' ],
74 [
'ir_ipb_id' => $blockId ],
77 [
'page' => [
'LEFT JOIN', [
'ir_type' => PageRestriction::TYPE_ID,
'ir_value=page_id' ] ] ]
90 public function insert( array $restrictions ) {
91 if ( !$restrictions ) {
96 foreach ( $restrictions as $restriction ) {
100 $rows[] = $restriction->toRow();
107 $dbw = $this->loadBalancer->getConnectionRef(
DB_MASTER );
110 'ipblocks_restrictions',
127 public function update( array $restrictions ) {
128 $dbw = $this->loadBalancer->getConnectionRef(
DB_MASTER );
130 $dbw->startAtomic( __METHOD__ );
140 $blockIds = array_keys( $restrictionList );
141 if ( !empty( $blockIds ) ) {
142 $result = $dbw->select(
143 [
'ipblocks_restrictions' ],
144 [
'ir_ipb_id',
'ir_type',
'ir_value' ],
145 [
'ir_ipb_id' => $blockIds ],
157 foreach ( $restrictionList as $blockId => $blockRestrictions ) {
165 $existingList[$blockId] ?? [],
169 if ( empty( $restrictionsToRemove ) ) {
173 $success = $this->
delete( $restrictionsToRemove );
179 $dbw->endAtomic( __METHOD__ );
194 if ( empty( $restrictions ) ) {
198 $parentBlockId = (int)$parentBlockId;
200 $db = $this->loadBalancer->getConnectionRef(
DB_MASTER );
202 $db->startAtomic( __METHOD__ );
204 $blockIds = $db->selectFieldValues(
207 [
'ipb_parent_block_id' => $parentBlockId ],
213 foreach ( $blockIds as $id ) {
219 $db->endAtomic( __METHOD__ );
232 public function delete( array $restrictions ) {
233 $dbw = $this->loadBalancer->getConnectionRef(
DB_MASTER );
235 foreach ( $restrictions as $restriction ) {
241 'ipblocks_restrictions',
244 $restriction->toRow(),
263 $dbw = $this->loadBalancer->getConnectionRef(
DB_MASTER );
265 'ipblocks_restrictions',
266 [
'ir_ipb_id' => $blockId ],
280 $dbw = $this->loadBalancer->getConnectionRef(
DB_MASTER );
281 return $dbw->deleteJoin(
282 'ipblocks_restrictions',
286 [
'ipb_parent_block_id' => $parentBlockId ],
301 public function equals( array $a, array $b ) {
302 $filter =
function ( $restriction ) {
309 $a = array_filter( $a,
$filter );
310 $b = array_filter( $b,
$filter );
312 $aCount = count( $a );
313 $bCount = count( $b );
316 if ( $aCount !== $bCount ) {
321 if ( $aCount === 0 && $bCount === 0 ) {
325 $hasher =
function ( $r ) {
326 return $r->getHash();
329 $aHashes = array_map( $hasher, $a );
330 $bHashes = array_map( $hasher, $b );
335 return $aHashes === $bHashes;
346 public function setBlockId( $blockId, array $restrictions ) {
347 $blockRestrictions = [];
349 foreach ( $restrictions as $restriction ) {
356 $restriction = clone $restriction;
357 $restriction->setBlockId( $blockId );
359 $blockRestrictions[] = $restriction;
362 return $blockRestrictions;
374 return array_filter( $existing,
function ( $e ) use ( $new ) {
375 foreach ( $new as $restriction ) {
380 if ( $restriction->equals( $e ) ) {
397 $blockRestrictions = [];
399 foreach ( $restrictions as $restriction ) {
405 if ( !isset( $blockRestrictions[$restriction->getBlockId()] ) ) {
406 $blockRestrictions[$restriction->getBlockId()] = [];
409 $blockRestrictions[$restriction->getBlockId()][] = $restriction;
412 return $blockRestrictions;
423 foreach ( $result as $row ) {
426 if ( !$restriction ) {
430 $restrictions[] = $restriction;
433 return $restrictions;
443 if ( array_key_exists( (
int)$row->ir_type, $this->types ) ) {
444 $class = $this->types[ (int)$row->ir_type ];
445 return call_user_func( [ $class,
'newFromRow' ], $row );