Merge "Avoid DBPerformance warnings on PURGE/TRACE requests"
[lhc/web/wiklou.git] / includes / Block.php
index c017c6c..93df004 100644 (file)
@@ -100,8 +100,8 @@ class Block {
         * @since 1.26 accepts $options array instead of individual parameters; order
         * of parameters above reflects the original order
         */
-       function __construct( $options = array() ) {
-               $defaults = array(
+       function __construct( $options = [] ) {
+               $defaults = [
                        'address'         => '',
                        'user'            => null,
                        'by'              => null,
@@ -116,7 +116,7 @@ class Block {
                        'blockEmail'      => false,
                        'allowUsertalk'   => false,
                        'byText'          => '',
-               );
+               ];
 
                if ( func_num_args() > 1 || !is_array( $options ) ) {
                        $options = array_combine(
@@ -137,7 +137,7 @@ class Block {
 
                if ( $options['by'] ) {
                        # Local user
-                       $this->setBlocker( User::newFromID( $options['by'] ) );
+                       $this->setBlocker( User::newFromId( $options['by'] ) );
                } else {
                        # Foreign user
                        $this->setBlocker( $options['byText'] );
@@ -172,7 +172,7 @@ class Block {
                $res = $dbr->selectRow(
                        'ipblocks',
                        self::selectFields(),
-                       array( 'ipb_id' => $id ),
+                       [ 'ipb_id' => $id ],
                        __METHOD__
                );
                if ( $res ) {
@@ -188,7 +188,7 @@ class Block {
         * @return array
         */
        public static function selectFields() {
-               return array(
+               return [
                        'ipb_id',
                        'ipb_address',
                        'ipb_by',
@@ -204,7 +204,7 @@ class Block {
                        'ipb_block_email',
                        'ipb_allow_usertalk',
                        'ipb_parent_block_id',
-               );
+               ];
        }
 
        /**
@@ -245,11 +245,11 @@ class Block {
                $db = wfGetDB( $this->mFromMaster ? DB_MASTER : DB_SLAVE );
 
                if ( $this->type !== null ) {
-                       $conds = array(
-                               'ipb_address' => array( (string)$this->target ),
-                       );
+                       $conds = [
+                               'ipb_address' => [ (string)$this->target ],
+                       ];
                } else {
-                       $conds = array( 'ipb_address' => array() );
+                       $conds = [ 'ipb_address' => [] ];
                }
 
                # Be aware that the != '' check is explicit, since empty values will be
@@ -360,11 +360,11 @@ class Block {
                $safeEnd = $dbr->addQuotes( $end );
 
                return $dbr->makeList(
-                       array(
+                       [
                                "ipb_range_start $like",
                                "ipb_range_start <= $safeStart",
                                "ipb_range_end >= $safeEnd",
-                       ),
+                       ],
                        LIST_AND
                );
        }
@@ -442,8 +442,8 @@ class Block {
                }
 
                $dbw = wfGetDB( DB_MASTER );
-               $dbw->delete( 'ipblocks', array( 'ipb_parent_block_id' => $this->getId() ), __METHOD__ );
-               $dbw->delete( 'ipblocks', array( 'ipb_id' => $this->getId() ), __METHOD__ );
+               $dbw->delete( 'ipblocks', [ 'ipb_parent_block_id' => $this->getId() ], __METHOD__ );
+               $dbw->delete( 'ipblocks', [ 'ipb_id' => $this->getId() ], __METHOD__ );
 
                return $dbw->affectedRows() > 0;
        }
@@ -471,7 +471,7 @@ class Block {
                $row = $this->getDatabaseArray();
                $row['ipb_id'] = $dbw->nextSequenceValue( "ipblocks_ipb_id_seq" );
 
-               $dbw->insert( 'ipblocks', $row, __METHOD__, array( 'IGNORE' ) );
+               $dbw->insert( 'ipblocks', $row, __METHOD__, [ 'IGNORE' ] );
                $affected = $dbw->affectedRows();
                $this->mId = $dbw->insertId();
 
@@ -482,16 +482,16 @@ class Block {
                        # use a standard SELECT + DELETE to avoid annoying gap locks.
                        $ids = $dbw->selectFieldValues( 'ipblocks',
                                'ipb_id',
-                               array(
+                               [
                                        'ipb_address' => $row['ipb_address'],
                                        'ipb_user' => $row['ipb_user'],
                                        'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() )
-                               ),
+                               ],
                                __METHOD__
                        );
                        if ( $ids ) {
-                               $dbw->delete( 'ipblocks', array( 'ipb_id' => $ids ), __METHOD__ );
-                               $dbw->insert( 'ipblocks', $row, __METHOD__, array( 'IGNORE' ) );
+                               $dbw->delete( 'ipblocks', [ 'ipb_id' => $ids ], __METHOD__ );
+                               $dbw->insert( 'ipblocks', $row, __METHOD__, [ 'IGNORE' ] );
                                $affected = $dbw->affectedRows();
                                $this->mId = $dbw->insertId();
                        }
@@ -499,7 +499,7 @@ class Block {
 
                if ( $affected ) {
                        $auto_ipd_ids = $this->doRetroactiveAutoblock();
-                       return array( 'id' => $this->mId, 'autoIds' => $auto_ipd_ids );
+                       return [ 'id' => $this->mId, 'autoIds' => $auto_ipd_ids ];
                }
 
                return false;
@@ -521,7 +521,7 @@ class Block {
                $dbw->update(
                        'ipblocks',
                        $this->getDatabaseArray( $dbw ),
-                       array( 'ipb_id' => $this->getId() ),
+                       [ 'ipb_id' => $this->getId() ],
                        __METHOD__
                );
 
@@ -532,14 +532,14 @@ class Block {
                        $dbw->update(
                                'ipblocks',
                                $this->getAutoblockUpdateArray(),
-                               array( 'ipb_parent_block_id' => $this->getId() ),
+                               [ 'ipb_parent_block_id' => $this->getId() ],
                                __METHOD__
                        );
                } else {
                        // autoblock no longer required, delete corresponding autoblock(s)
                        $dbw->delete(
                                'ipblocks',
-                               array( 'ipb_parent_block_id' => $this->getId() ),
+                               [ 'ipb_parent_block_id' => $this->getId() ],
                                __METHOD__
                        );
                }
@@ -548,7 +548,7 @@ class Block {
 
                if ( $affected ) {
                        $auto_ipd_ids = $this->doRetroactiveAutoblock();
-                       return array( 'id' => $this->mId, 'autoIds' => $auto_ipd_ids );
+                       return [ 'id' => $this->mId, 'autoIds' => $auto_ipd_ids ];
                }
 
                return false;
@@ -568,10 +568,10 @@ class Block {
                if ( $this->forcedTargetID ) {
                        $uid = $this->forcedTargetID;
                } else {
-                       $uid = $this->target instanceof User ? $this->target->getID() : 0;
+                       $uid = $this->target instanceof User ? $this->target->getId() : 0;
                }
 
-               $a = array(
+               $a = [
                        'ipb_address'          => (string)$this->target,
                        'ipb_user'             => $uid,
                        'ipb_by'               => $this->getBy(),
@@ -589,7 +589,7 @@ class Block {
                        'ipb_block_email'      => $this->prevents( 'sendemail' ),
                        'ipb_allow_usertalk'   => !$this->prevents( 'editownusertalk' ),
                        'ipb_parent_block_id'  => $this->mParentBlockId
-               );
+               ];
 
                return $a;
        }
@@ -598,14 +598,14 @@ class Block {
         * @return array
         */
        protected function getAutoblockUpdateArray() {
-               return array(
+               return [
                        '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' ),
-               );
+               ];
        }
 
        /**
@@ -615,13 +615,13 @@ class Block {
         * @return array Block IDs of retroactive autoblocks made
         */
        protected function doRetroactiveAutoblock() {
-               $blockIds = array();
+               $blockIds = [];
                # If autoblock is enabled, autoblock the LAST IP(s) used
                if ( $this->isAutoblocking() && $this->getType() == self::TYPE_USER ) {
                        wfDebug( "Doing retroactive autoblocks for " . $this->getTarget() . "\n" );
 
                        $continue = Hooks::run(
-                               'PerformRetroactiveAutoblock', array( $this, &$blockIds ) );
+                               'PerformRetroactiveAutoblock', [ $this, &$blockIds ] );
 
                        if ( $continue ) {
                                self::defaultRetroactiveAutoblock( $this, $blockIds );
@@ -647,13 +647,13 @@ class Block {
 
                $dbr = wfGetDB( DB_SLAVE );
 
-               $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
-               $conds = array( 'rc_user_text' => (string)$block->getTarget() );
+               $options = [ 'ORDER BY' => 'rc_timestamp DESC' ];
+               $conds = [ 'rc_user_text' => (string)$block->getTarget() ];
 
                // Just the last IP used.
                $options['LIMIT'] = 1;
 
-               $res = $dbr->select( 'recentchanges', array( 'rc_ip' ), $conds,
+               $res = $dbr->select( 'recentchanges', [ 'rc_ip' ], $conds,
                        __METHOD__, $options );
 
                if ( !$res->numRows() ) {
@@ -734,7 +734,7 @@ class Block {
                }
 
                # Allow hooks to cancel the autoblock.
-               if ( !Hooks::run( 'AbortAutoblock', array( $autoblockIP, &$this ) ) ) {
+               if ( !Hooks::run( 'AbortAutoblock', [ $autoblockIP, &$this ] ) ) {
                        wfDebug( "Autoblock aborted by hook.\n" );
                        return false;
                }
@@ -839,13 +839,13 @@ class Block {
 
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->update( 'ipblocks',
-                               array( /* SET */
+                               [ /* SET */
                                        'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp ),
                                        'ipb_expiry' => $dbw->timestamp( $this->mExpiry ),
-                               ),
-                               array( /* WHERE */
-                                       'ipb_address' => (string)$this->getTarget()
-                               ),
+                               ],
+                               [ /* WHERE */
+                                       'ipb_id' => $this->getId(),
+                               ],
                                __METHOD__
                        );
                }
@@ -993,7 +993,7 @@ class Block {
                if ( $this->mAuto ) {
                        return Html::rawElement(
                                'span',
-                               array( 'class' => 'mw-autoblockid' ),
+                               [ 'class' => 'mw-autoblockid' ],
                                wfMessage( 'autoblockid', $this->mId )
                        );
                } else {
@@ -1027,7 +1027,7 @@ class Block {
                        function ( IDatabase $dbw, $fname ) {
                                $dbw->delete(
                                        'ipblocks',
-                                       array( 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ),
+                                       [ 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ],
                                        $fname
                                );
                        }
@@ -1068,7 +1068,7 @@ class Block {
 
                } elseif ( in_array(
                        $type,
-                       array( Block::TYPE_USER, Block::TYPE_IP, Block::TYPE_RANGE, null ) )
+                       [ Block::TYPE_USER, Block::TYPE_IP, Block::TYPE_RANGE, null ] )
                ) {
                        $block = new Block();
                        $block->fromMaster( $fromMaster );
@@ -1096,10 +1096,10 @@ class Block {
         */
        public static function getBlocksForIPList( array $ipChain, $isAnon, $fromMaster = false ) {
                if ( !count( $ipChain ) ) {
-                       return array();
+                       return [];
                }
 
-               $conds = array();
+               $conds = [];
                foreach ( array_unique( $ipChain ) as $ipaddr ) {
                        # Discard invalid IP addresses. Since XFF can be spoofed and we do not
                        # necessarily trust the header given to us, make sure that we are only
@@ -1120,7 +1120,7 @@ class Block {
                }
 
                if ( !count( $conds ) ) {
-                       return array();
+                       return [];
                }
 
                if ( $fromMaster ) {
@@ -1130,10 +1130,10 @@ class Block {
                }
                $conds = $db->makeList( $conds, LIST_OR );
                if ( !$isAnon ) {
-                       $conds = array( $conds, 'ipb_anon_only' => 0 );
+                       $conds = [ $conds, 'ipb_anon_only' => 0 ];
                }
                $selectFields = array_merge(
-                       array( 'ipb_range_start', 'ipb_range_end' ),
+                       [ 'ipb_range_start', 'ipb_range_end' ],
                        Block::selectFields()
                );
                $rows = $db->select( 'ipblocks',
@@ -1142,7 +1142,7 @@ class Block {
                        __METHOD__
                );
 
-               $blocks = array();
+               $blocks = [];
                foreach ( $rows as $row ) {
                        $block = self::newFromRow( $row );
                        if ( !$block->isExpired() ) {
@@ -1189,18 +1189,18 @@ class Block {
                        return strcmp( $bWeight, $aWeight ); // highest weight first
                } );
 
-               $blocksListExact = array(
+               $blocksListExact = [
                        'hard' => false,
                        'disable_create' => false,
                        'other' => false,
                        'auto' => false
-               );
-               $blocksListRange = array(
+               ];
+               $blocksListRange = [
                        'hard' => false,
                        'disable_create' => false,
                        'other' => false,
                        'auto' => false
-               );
+               ];
                $ipChain = array_reverse( $ipChain );
 
                /** @var Block $block */
@@ -1227,7 +1227,7 @@ class Block {
                                        }
                                        // We found closest exact match in the ip list, so go to the next Block
                                        break;
-                               } elseif ( array_filter( $blocksListExact ) == array()
+                               } elseif ( array_filter( $blocksListExact ) == []
                                        && $block->getRangeStart() <= $checkipHex
                                        && $block->getRangeEnd() >= $checkipHex
                                ) {
@@ -1245,7 +1245,7 @@ class Block {
                        }
                }
 
-               if ( array_filter( $blocksListExact ) == array() ) {
+               if ( array_filter( $blocksListExact ) == [] ) {
                        $blocksList = &$blocksListRange;
                } else {
                        $blocksList = &$blocksListExact;
@@ -1280,12 +1280,12 @@ class Block {
                # We may have been through this before
                if ( $target instanceof User ) {
                        if ( IP::isValid( $target->getName() ) ) {
-                               return array( $target, self::TYPE_IP );
+                               return [ $target, self::TYPE_IP ];
                        } else {
-                               return array( $target, self::TYPE_USER );
+                               return [ $target, self::TYPE_USER ];
                        }
                } elseif ( $target === null ) {
-                       return array( null, null );
+                       return [ null, null ];
                }
 
                $target = trim( $target );
@@ -1293,22 +1293,21 @@ class Block {
                if ( IP::isValid( $target ) ) {
                        # We can still create a User if it's an IP address, but we need to turn
                        # off validation checking (which would exclude IP addresses)
-                       return array(
+                       return [
                                User::newFromName( IP::sanitizeIP( $target ), false ),
                                Block::TYPE_IP
-                       );
+                       ];
 
                } elseif ( IP::isValidBlock( $target ) ) {
                        # Can't create a User from an IP range
-                       return array( IP::sanitizeRange( $target ), Block::TYPE_RANGE );
+                       return [ IP::sanitizeRange( $target ), Block::TYPE_RANGE ];
                }
 
                # Consider the possibility that this is not a username at all
                # but actually an old subpage (bug #29797)
                if ( strpos( $target, '/' ) !== false ) {
                        # An old subpage, drill down to the user behind it
-                       $parts = explode( '/', $target );
-                       $target = $parts[0];
+                       $target = explode( '/', $target )[0];
                }
 
                $userObj = User::newFromName( $target );
@@ -1316,15 +1315,15 @@ class Block {
                        # Note that since numbers are valid usernames, a $target of "12345" will be
                        # considered a User.  If you want to pass a block ID, prepend a hash "#12345",
                        # since hash characters are not valid in usernames or titles generally.
-                       return array( $userObj, Block::TYPE_USER );
+                       return [ $userObj, Block::TYPE_USER ];
 
                } elseif ( preg_match( '/^#\d+$/', $target ) ) {
                        # Autoblock reference in the form "#12345"
-                       return array( substr( $target, 1 ), Block::TYPE_AUTO );
+                       return [ substr( $target, 1 ), Block::TYPE_AUTO ];
 
                } else {
                        # WTF?
-                       return array( null, null );
+                       return [ null, null ];
                }
        }
 
@@ -1346,7 +1345,7 @@ class Block {
         * @todo FIXME: This should be an integral part of the Block member variables
         */
        public function getTargetAndType() {
-               return array( $this->getTarget(), $this->getType() );
+               return [ $this->getTarget(), $this->getType() ];
        }
 
        /**
@@ -1418,7 +1417,7 @@ class Block {
                $intended = $this->getTarget();
 
                $lang = $context->getLanguage();
-               return array(
+               return [
                        $this->mAuto ? 'autoblockedtext' : 'blockedtext',
                        $link,
                        $reason,
@@ -1428,6 +1427,6 @@ class Block {
                        $lang->formatExpiry( $this->mExpiry ),
                        (string)$intended,
                        $lang->userTimeAndDate( $this->mTimestamp, $context->getUser() ),
-               );
+               ];
        }
 }