59 $blockTargetMigrationStage,
60 $wikiId = WikiAwareEntity::LOCAL
62 $this->dbProvider = $dbProvider;
63 $this->wikiId = $wikiId;
75 if ( $blockId ===
null || $blockId === [] ) {
79 $result = $this->dbProvider->getReplicaDatabase( $this->wikiId )
80 ->newSelectQueryBuilder()
81 ->select( [
'ir_ipb_id',
'ir_type',
'ir_value',
'page_namespace',
'page_title' ] )
82 ->from(
'ipblocks_restrictions' )
83 ->leftJoin(
'page',
null, [
'ir_type' => PageRestriction::TYPE_ID,
'ir_value=page_id' ] )
84 ->where( [
'ir_ipb_id' => $blockId ] )
85 ->caller( __METHOD__ )->fetchResultSet();
87 return $this->resultToRestrictions( $result );
97 public function insert( array $restrictions ) {
98 if ( !$restrictions ) {
103 foreach ( $restrictions as $restriction ) {
104 $rows[] = $restriction->toRow();
107 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
109 $dbw->newInsertQueryBuilder()
110 ->insertInto(
'ipblocks_restrictions' )
113 ->caller( __METHOD__ )->execute();
126 public function update( array $restrictions ) {
127 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
129 $dbw->startAtomic( __METHOD__ );
132 $restrictionList = $this->restrictionsByBlockId( $restrictions );
139 $blockIds = array_keys( $restrictionList );
141 $result = $dbw->newSelectQueryBuilder()
142 ->select( [
'ir_ipb_id',
'ir_type',
'ir_value' ] )
144 ->from(
'ipblocks_restrictions' )
145 ->where( [
'ir_ipb_id' => $blockIds ] )
146 ->caller( __METHOD__ )->fetchResultSet();
148 $existingList = $this->restrictionsByBlockId(
149 $this->resultToRestrictions( $result )
155 foreach ( $restrictionList as $blockId => $blockRestrictions ) {
161 $restrictionsToRemove = $this->restrictionsToRemove(
162 $existingList[$blockId] ?? [],
166 if ( !$restrictionsToRemove ) {
170 $success = $this->
delete( $restrictionsToRemove );
175 $dbw->endAtomic( __METHOD__ );
189 $parentBlockId = (int)$parentBlockId;
191 $db = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
194 $blockIds = $db->newSelectQueryBuilder()
195 ->select( [
'ipb_id' ] )
198 ->where( [
'ipb_parent_block_id' => $parentBlockId ] )
199 ->caller( __METHOD__ )->fetchFieldValues();
201 $blockIds = $db->newSelectQueryBuilder()
205 ->where( [
'bl_parent_block_id' => $parentBlockId ] )
206 ->caller( __METHOD__ )->fetchFieldValues();
213 if ( !$restrictions ) {
214 $blockIds = array_map(
'intval', $blockIds );
218 $db->startAtomic( __METHOD__ );
221 foreach ( $blockIds as $id ) {
226 $db->endAtomic( __METHOD__ );
238 public function delete( array $restrictions ) {
239 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
240 foreach ( $restrictions as $restriction ) {
241 $dbw->newDeleteQueryBuilder()
242 ->deleteFrom(
'ipblocks_restrictions' )
245 ->where( $restriction->toRow() )
246 ->caller( __METHOD__ )->execute();
260 $this->dbProvider->getPrimaryDatabase( $this->wikiId )
261 ->newDeleteQueryBuilder()
262 ->deleteFrom(
'ipblocks_restrictions' )
263 ->where( [
'ir_ipb_id' => $blockId ] )
264 ->caller( __METHOD__ )->execute();
278 public function equals( array $a, array $b ) {
279 $aCount = count( $a );
280 $bCount = count( $b );
283 if ( $aCount !== $bCount ) {
288 if ( $aCount === 0 && $bCount === 0 ) {
293 return $r->getHash();
296 $aHashes = array_map( $hasher, $a );
297 $bHashes = array_map( $hasher, $b );
302 return $aHashes === $bHashes;
313 public function setBlockId( $blockId, array $restrictions ) {
314 $blockRestrictions = [];
316 foreach ( $restrictions as $restriction ) {
319 $restriction = clone $restriction;
320 $restriction->setBlockId( $blockId );
322 $blockRestrictions[] = $restriction;
325 return $blockRestrictions;
336 private function restrictionsToRemove( array $existing, array $new ) {
337 $restrictionsByHash = [];
338 foreach ( $existing as $restriction ) {
339 $restrictionsByHash[$restriction->getHash()] = $restriction;
341 foreach ( $new as $restriction ) {
342 unset( $restrictionsByHash[$restriction->getHash()] );
344 return array_values( $restrictionsByHash );
354 private function restrictionsByBlockId( array $restrictions ) {
355 $blockRestrictions = [];
357 foreach ( $restrictions as $restriction ) {
358 $blockRestrictions[$restriction->getBlockId()][] = $restriction;
361 return $blockRestrictions;
370 private function resultToRestrictions( IResultWrapper $result ) {
372 foreach ( $result as $row ) {
373 $restriction = $this->rowToRestriction( $row );
375 if ( !$restriction ) {
379 $restrictions[] = $restriction;
382 return $restrictions;
391 private function rowToRestriction( stdClass $row ) {
392 switch ( (
int)$row->ir_type ) {
393 case PageRestriction::TYPE_ID:
394 return PageRestriction::newFromRow( $row );
395 case NamespaceRestriction::TYPE_ID:
396 return NamespaceRestriction::newFromRow( $row );
397 case ActionRestriction::TYPE_ID:
398 return ActionRestriction::newFromRow( $row );