52 $wikiId = WikiAwareEntity::LOCAL
54 $this->dbProvider = $dbProvider;
55 $this->wikiId = $wikiId;
66 if ( $blockId ===
null || $blockId === [] ) {
70 $result = $this->dbProvider->getReplicaDatabase( $this->wikiId )
71 ->newSelectQueryBuilder()
72 ->select( [
'ir_ipb_id',
'ir_type',
'ir_value',
'page_namespace',
'page_title' ] )
73 ->from(
'ipblocks_restrictions' )
74 ->leftJoin(
'page',
null, [
'ir_type' => PageRestriction::TYPE_ID,
'ir_value=page_id' ] )
75 ->where( [
'ir_ipb_id' => $blockId ] )
76 ->caller( __METHOD__ )->fetchResultSet();
78 return $this->resultToRestrictions( $result );
88 public function insert( array $restrictions ) {
89 if ( !$restrictions ) {
94 foreach ( $restrictions as $restriction ) {
95 $rows[] = $restriction->toRow();
98 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
100 $dbw->newInsertQueryBuilder()
101 ->insertInto(
'ipblocks_restrictions' )
104 ->caller( __METHOD__ )->execute();
117 public function update( array $restrictions ) {
118 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
120 $dbw->startAtomic( __METHOD__ );
123 $restrictionList = $this->restrictionsByBlockId( $restrictions );
130 $blockIds = array_keys( $restrictionList );
132 $result = $dbw->newSelectQueryBuilder()
133 ->select( [
'ir_ipb_id',
'ir_type',
'ir_value' ] )
135 ->from(
'ipblocks_restrictions' )
136 ->where( [
'ir_ipb_id' => $blockIds ] )
137 ->caller( __METHOD__ )->fetchResultSet();
139 $existingList = $this->restrictionsByBlockId(
140 $this->resultToRestrictions( $result )
146 foreach ( $restrictionList as $blockId => $blockRestrictions ) {
152 $restrictionsToRemove = $this->restrictionsToRemove(
153 $existingList[$blockId] ?? [],
157 if ( !$restrictionsToRemove ) {
161 $success = $this->
delete( $restrictionsToRemove );
166 $dbw->endAtomic( __METHOD__ );
180 $parentBlockId = (int)$parentBlockId;
182 $db = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
184 $blockIds = $db->newSelectQueryBuilder()
188 ->where( [
'ipb_parent_block_id' => $parentBlockId ] )
189 ->caller( __METHOD__ )->fetchFieldValues();
195 if ( !$restrictions ) {
196 $blockIds = array_map(
'intval', $blockIds );
200 $db->startAtomic( __METHOD__ );
203 foreach ( $blockIds as $id ) {
208 $db->endAtomic( __METHOD__ );
220 public function delete( array $restrictions ) {
221 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
222 foreach ( $restrictions as $restriction ) {
223 $dbw->newDeleteQueryBuilder()
224 ->deleteFrom(
'ipblocks_restrictions' )
227 ->where( $restriction->toRow() )
228 ->caller( __METHOD__ )->execute();
242 $this->dbProvider->getPrimaryDatabase( $this->wikiId )
243 ->newDeleteQueryBuilder()
244 ->deleteFrom(
'ipblocks_restrictions' )
245 ->where( [
'ir_ipb_id' => $blockId ] )
246 ->caller( __METHOD__ )->execute();
260 public function equals( array $a, array $b ) {
261 $aCount = count( $a );
262 $bCount = count( $b );
265 if ( $aCount !== $bCount ) {
270 if ( $aCount === 0 && $bCount === 0 ) {
275 return $r->getHash();
278 $aHashes = array_map( $hasher, $a );
279 $bHashes = array_map( $hasher, $b );
284 return $aHashes === $bHashes;
295 public function setBlockId( $blockId, array $restrictions ) {
296 $blockRestrictions = [];
298 foreach ( $restrictions as $restriction ) {
301 $restriction = clone $restriction;
302 $restriction->setBlockId( $blockId );
304 $blockRestrictions[] = $restriction;
307 return $blockRestrictions;
318 private function restrictionsToRemove( array $existing, array $new ) {
319 $restrictionsByHash = [];
320 foreach ( $existing as $restriction ) {
321 $restrictionsByHash[$restriction->getHash()] = $restriction;
323 foreach ( $new as $restriction ) {
324 unset( $restrictionsByHash[$restriction->getHash()] );
326 return array_values( $restrictionsByHash );
336 private function restrictionsByBlockId( array $restrictions ) {
337 $blockRestrictions = [];
339 foreach ( $restrictions as $restriction ) {
340 $blockRestrictions[$restriction->getBlockId()][] = $restriction;
343 return $blockRestrictions;
352 private function resultToRestrictions( IResultWrapper $result ) {
354 foreach ( $result as $row ) {
355 $restriction = $this->rowToRestriction( $row );
357 if ( !$restriction ) {
361 $restrictions[] = $restriction;
364 return $restrictions;
373 private function rowToRestriction( stdClass $row ) {
374 switch ( (
int)$row->ir_type ) {
375 case PageRestriction::TYPE_ID:
376 return PageRestriction::newFromRow( $row );
377 case NamespaceRestriction::TYPE_ID:
378 return NamespaceRestriction::newFromRow( $row );
379 case ActionRestriction::TYPE_ID:
380 return ActionRestriction::newFromRow( $row );