24 private string|
false $wikiId;
28 string|
false $wikiId = WikiAwareEntity::LOCAL
30 $this->dbProvider = $dbProvider;
31 $this->wikiId = $wikiId;
42 if ( $blockId ===
null || $blockId === [] ) {
46 $result = $this->dbProvider->getReplicaDatabase( $this->wikiId )
47 ->newSelectQueryBuilder()
48 ->select( [
'ir_ipb_id',
'ir_type',
'ir_value',
'page_namespace',
'page_title' ] )
49 ->from(
'ipblocks_restrictions' )
50 ->leftJoin(
'page',
null, [
'ir_type' => PageRestriction::TYPE_ID,
'ir_value=page_id' ] )
51 ->where( [
'ir_ipb_id' => $blockId ] )
52 ->caller( __METHOD__ )->fetchResultSet();
54 return $this->resultToRestrictions( $result );
64 public function insert( array $restrictions ) {
65 if ( !$restrictions ) {
70 foreach ( $restrictions as $restriction ) {
71 $rows[] = $restriction->toRow();
74 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
76 $dbw->newInsertQueryBuilder()
77 ->insertInto(
'ipblocks_restrictions' )
80 ->caller( __METHOD__ )->execute();
93 public function update( array $restrictions ) {
94 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
96 $dbw->startAtomic( __METHOD__ );
99 $restrictionList = $this->restrictionsByBlockId( $restrictions );
106 $blockIds = array_keys( $restrictionList );
108 $result = $dbw->newSelectQueryBuilder()
109 ->select( [
'ir_ipb_id',
'ir_type',
'ir_value' ] )
111 ->from(
'ipblocks_restrictions' )
112 ->where( [
'ir_ipb_id' => $blockIds ] )
113 ->caller( __METHOD__ )->fetchResultSet();
115 $existingList = $this->restrictionsByBlockId(
116 $this->resultToRestrictions( $result )
122 foreach ( $restrictionList as $blockId => $blockRestrictions ) {
128 $restrictionsToRemove = $this->restrictionsToRemove(
129 $existingList[$blockId] ?? [],
133 if ( !$restrictionsToRemove ) {
137 $success = $this->
delete( $restrictionsToRemove );
142 $dbw->endAtomic( __METHOD__ );
156 $parentBlockId = (int)$parentBlockId;
158 $db = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
160 $blockIds = $db->newSelectQueryBuilder()
164 ->where( [
'bl_parent_block_id' => $parentBlockId ] )
165 ->caller( __METHOD__ )->fetchFieldValues();
171 if ( !$restrictions ) {
172 $blockIds = array_map(
'intval', $blockIds );
176 $db->startAtomic( __METHOD__ );
179 foreach ( $blockIds as $id ) {
184 $db->endAtomic( __METHOD__ );
196 public function delete( array $restrictions ) {
197 $dbw = $this->dbProvider->getPrimaryDatabase( $this->wikiId );
198 foreach ( $restrictions as $restriction ) {
199 $dbw->newDeleteQueryBuilder()
200 ->deleteFrom(
'ipblocks_restrictions' )
203 ->where( $restriction->toRow() )
204 ->caller( __METHOD__ )->execute();
218 $this->dbProvider->getPrimaryDatabase( $this->wikiId )
219 ->newDeleteQueryBuilder()
220 ->deleteFrom(
'ipblocks_restrictions' )
221 ->where( [
'ir_ipb_id' => $blockId ] )
222 ->caller( __METHOD__ )->execute();
236 public function equals( array $a, array $b ) {
237 $aCount = count( $a );
238 $bCount = count( $b );
241 if ( $aCount !== $bCount ) {
246 if ( $aCount === 0 && $bCount === 0 ) {
252 $aHashes = array_map( $hasher, $a );
253 $bHashes = array_map( $hasher, $b );
258 return $aHashes === $bHashes;
269 public function setBlockId( $blockId, array $restrictions ) {
270 $blockRestrictions = [];
272 foreach ( $restrictions as $restriction ) {
275 $restriction = clone $restriction;
276 $restriction->setBlockId( $blockId );
278 $blockRestrictions[] = $restriction;
281 return $blockRestrictions;
292 private function restrictionsToRemove( array $existing, array $new ) {
293 $restrictionsByHash = [];
294 foreach ( $existing as $restriction ) {
295 $restrictionsByHash[$restriction->getHash()] = $restriction;
297 foreach ( $new as $restriction ) {
298 unset( $restrictionsByHash[$restriction->getHash()] );
300 return array_values( $restrictionsByHash );
310 private function restrictionsByBlockId( array $restrictions ) {
311 $blockRestrictions = [];
313 foreach ( $restrictions as $restriction ) {
314 $blockRestrictions[$restriction->getBlockId()][] = $restriction;
317 return $blockRestrictions;
326 private function resultToRestrictions( IResultWrapper $result ) {
328 foreach ( $result as $row ) {
329 $restriction = $this->rowToRestriction( $row );
331 if ( !$restriction ) {
335 $restrictions[] = $restriction;
338 return $restrictions;
347 private function rowToRestriction( stdClass $row ) {
348 switch ( (
int)$row->ir_type ) {
349 case PageRestriction::TYPE_ID:
350 return PageRestriction::newFromRow( $row );
351 case NamespaceRestriction::TYPE_ID:
352 return NamespaceRestriction::newFromRow( $row );
353 case ActionRestriction::TYPE_ID:
354 return ActionRestriction::newFromRow( $row );