45 $wikiId = WikiAwareEntity::LOCAL
47 $this->dbProvider = $dbProvider;
48 $this->wikiId = $wikiId;
59 if ( $blockId ===
null || $blockId === [] ) {
63 $result = $this->dbProvider->getReplicaDatabase( $this->wikiId )
64 ->newSelectQueryBuilder()
65 ->select( [
'ir_ipb_id',
'ir_type',
'ir_value',
'page_namespace',
'page_title' ] )
66 ->from(
'ipblocks_restrictions' )
67 ->leftJoin(
'page',
null, [
'ir_type' => PageRestriction::TYPE_ID,
'ir_value=page_id' ] )
68 ->where( [
'ir_ipb_id' => $blockId ] )
69 ->caller( __METHOD__ )->fetchResultSet();
71 return $this->resultToRestrictions( $result );
81 public function insert( array $restrictions ) {
82 if ( !$restrictions ) {
87 foreach ( $restrictions as $restriction ) {
88 $rows[] = $restriction->toRow();
91 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
93 $dbw->newInsertQueryBuilder()
94 ->insertInto(
'ipblocks_restrictions' )
97 ->caller( __METHOD__ )->execute();
110 public function update( array $restrictions ) {
111 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
113 $dbw->startAtomic( __METHOD__ );
116 $restrictionList = $this->restrictionsByBlockId( $restrictions );
123 $blockIds = array_keys( $restrictionList );
125 $result = $dbw->newSelectQueryBuilder()
126 ->select( [
'ir_ipb_id',
'ir_type',
'ir_value' ] )
128 ->from(
'ipblocks_restrictions' )
129 ->where( [
'ir_ipb_id' => $blockIds ] )
130 ->caller( __METHOD__ )->fetchResultSet();
132 $existingList = $this->restrictionsByBlockId(
133 $this->resultToRestrictions( $result )
139 foreach ( $restrictionList as $blockId => $blockRestrictions ) {
145 $restrictionsToRemove = $this->restrictionsToRemove(
146 $existingList[$blockId] ?? [],
150 if ( !$restrictionsToRemove ) {
154 $success = $this->
delete( $restrictionsToRemove );
159 $dbw->endAtomic( __METHOD__ );
173 $parentBlockId = (int)$parentBlockId;
175 $db = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
177 $blockIds = $db->newSelectQueryBuilder()
181 ->where( [
'bl_parent_block_id' => $parentBlockId ] )
182 ->caller( __METHOD__ )->fetchFieldValues();
188 if ( !$restrictions ) {
189 $blockIds = array_map(
'intval', $blockIds );
193 $db->startAtomic( __METHOD__ );
196 foreach ( $blockIds as $id ) {
201 $db->endAtomic( __METHOD__ );
213 public function delete( array $restrictions ) {
214 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
215 foreach ( $restrictions as $restriction ) {
216 $dbw->newDeleteQueryBuilder()
217 ->deleteFrom(
'ipblocks_restrictions' )
220 ->where( $restriction->toRow() )
221 ->caller( __METHOD__ )->execute();
235 $this->dbProvider->getPrimaryDatabase( $this->wikiId )
236 ->newDeleteQueryBuilder()
237 ->deleteFrom(
'ipblocks_restrictions' )
238 ->where( [
'ir_ipb_id' => $blockId ] )
239 ->caller( __METHOD__ )->execute();
253 public function equals( array $a, array $b ) {
254 $aCount = count( $a );
255 $bCount = count( $b );
258 if ( $aCount !== $bCount ) {
263 if ( $aCount === 0 && $bCount === 0 ) {
268 return $r->getHash();
271 $aHashes = array_map( $hasher, $a );
272 $bHashes = array_map( $hasher, $b );
277 return $aHashes === $bHashes;
288 public function setBlockId( $blockId, array $restrictions ) {
289 $blockRestrictions = [];
291 foreach ( $restrictions as $restriction ) {
294 $restriction = clone $restriction;
295 $restriction->setBlockId( $blockId );
297 $blockRestrictions[] = $restriction;
300 return $blockRestrictions;
311 private function restrictionsToRemove( array $existing, array $new ) {
312 $restrictionsByHash = [];
313 foreach ( $existing as $restriction ) {
314 $restrictionsByHash[$restriction->getHash()] = $restriction;
316 foreach ( $new as $restriction ) {
317 unset( $restrictionsByHash[$restriction->getHash()] );
319 return array_values( $restrictionsByHash );
329 private function restrictionsByBlockId( array $restrictions ) {
330 $blockRestrictions = [];
332 foreach ( $restrictions as $restriction ) {
333 $blockRestrictions[$restriction->getBlockId()][] = $restriction;
336 return $blockRestrictions;
345 private function resultToRestrictions( IResultWrapper $result ) {
347 foreach ( $result as $row ) {
348 $restriction = $this->rowToRestriction( $row );
350 if ( !$restriction ) {
354 $restrictions[] = $restriction;
357 return $restrictions;
366 private function rowToRestriction( stdClass $row ) {
367 switch ( (
int)$row->ir_type ) {
368 case PageRestriction::TYPE_ID:
369 return PageRestriction::newFromRow( $row );
370 case NamespaceRestriction::TYPE_ID:
371 return NamespaceRestriction::newFromRow( $row );
372 case ActionRestriction::TYPE_ID:
373 return ActionRestriction::newFromRow( $row );