Make autoblocks update with the parent block
authorTyler Anthony Romeo <tylerromeo@gmail.com>
Sun, 2 Jun 2013 17:36:30 +0000 (13:36 -0400)
committerTyler Anthony Romeo <tylerromeo@gmail.com>
Fri, 3 Jan 2014 09:06:52 +0000 (04:06 -0500)
Change SpecialBlock so that when a block already exists, it just updates
the existing block rather than deleting and inserting a new row. Also,
Block::update() was changed to update existing autoblocks with the new
block parameters.

Bug: 48813
Change-Id: I5403f6e1d7f8f07273cd5da8068b928fdddcdfc9

includes/Block.php
includes/specials/SpecialBlock.php

index 34b89e7..6f52a40 100644 (file)
@@ -488,13 +488,14 @@ class Block {
         * Update a block in the DB with new parameters.
         * The ID field needs to be loaded first.
         *
-        * @return Int number of affected rows, which should probably be 1 or something has
-        *     gone slightly awry
+        * @return bool|array False on failure, array on success: ('id' => block ID, 'autoIds' => array of autoblock IDs)
         */
        public function update() {
                wfDebug( "Block::update; timestamp {$this->mTimestamp}\n" );
                $dbw = wfGetDB( DB_MASTER );
 
+               $dbw->startAtomic( __METHOD__ );
+
                $dbw->update(
                        'ipblocks',
                        $this->getDatabaseArray( $dbw ),
@@ -502,7 +503,23 @@ class Block {
                        __METHOD__
                );
 
-               return $dbw->affectedRows();
+               $affected = $dbw->affectedRows();
+
+               $dbw->update(
+                       'ipblocks',
+                       $this->getAutoblockUpdateArray(),
+                       array( 'ipb_parent_block_id' => $this->getId() ),
+                       __METHOD__
+               );
+
+               $dbw->endAtomic( __METHOD__ );
+
+               if ( $affected ) {
+                       $auto_ipd_ids = $this->doRetroactiveAutoblock();
+                       return array( 'id' => $this->mId, 'autoIds' => $auto_ipd_ids );
+               }
+
+               return false;
        }
 
        /**
@@ -545,6 +562,20 @@ class Block {
                return $a;
        }
 
+       /**
+        * @return Array
+        */
+       protected function getAutoblockUpdateArray() {
+               return array(
+                       'ipb_by'               => $this->getBy(),
+                       'ipb_by_text'          => $this->getByName(),
+                       'ipb_reason'           => $this->mReason,
+                       'ipb_create_account'   => $this->prevents( 'createaccount' ),
+                       'ipb_deleted'          => (int)$this->mHideName, // typecast required for SQLite
+                       'ipb_allow_usertalk'   => !$this->prevents( 'editownusertalk' ),
+               );
+       }
+
        /**
         * Retroactively autoblocks the last IP used by the user (if it is a user)
         * blocked by this Block.
index 3b73a37..beb4d4c 100644 (file)
@@ -726,8 +726,17 @@ class SpecialBlock extends FormSpecialPage {
                                        return array( 'cant-see-hidden-user' );
                                }
 
-                               $currentBlock->delete();
-                               $status = $block->insert();
+                               $currentBlock->isHardblock( $block->isHardblock() );
+                               $currentBlock->prevents( 'createaccount', $block->prevents( 'createaccount' ) );
+                               $currentBlock->mExpiry = $block->mExpiry;
+                               $currentBlock->isAutoblocking( $block->isAutoblocking() );
+                               $currentBlock->mHideName = $block->mHideName;
+                               $currentBlock->prevents( 'sendemail', $block->prevents( 'sendemail' ) );
+                               $currentBlock->prevents( 'editownusertalk', $block->prevents( 'editownusertalk' ) );
+                               $currentBlock->mReason = $block->mReason;
+
+                               $status = $currentBlock->update();
+
                                $logaction = 'reblock';
 
                                # Unset _deleted fields if requested