r41139 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r41138‎ | r41139 | r41140 >
Date:04:52, 22 September 2008
Author:tstarling
Status:old
Tags:
Comment:
* Fix some documentation
* Fix K&R style braces, since demon started, we may as well finish. This was one of the last files I wrote before I was converted to Brion-style braces.
* Renamed a variable
Modified paths:
  • /trunk/phase3/includes/Block.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Block.php
@@ -13,8 +13,7 @@
1414 *
1515 * @todo This could be used everywhere, but it isn't.
1616 */
17 -class Block
18 -{
 17+class Block {
1918 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry,
2019 $mRangeStart, $mRangeEnd, $mAnonOnly, $mEnableAutoblock, $mHideName,
2120 $mBlockEmail, $mByName, $mAngryAutoblock;
@@ -52,15 +51,15 @@
5352
5453 /**
5554 * Load a block from the database, using either the IP address or
56 - * userid.
 55+ * user ID. Tries the user ID first, and if that doesn't work, tries
 56+ * the address.
5757 *
5858 * @return Block Object
5959 * @param $address string IP address of user/anon
6060 * @param $user int User id of user
6161 * @param $killExpired bool Delete expired blocks on load
6262 */
63 - static function newFromDB( $address, $user = 0, $killExpired = true )
64 - {
 63+ static function newFromDB( $address, $user = 0, $killExpired = true ) {
6564 $block = new Block();
6665 $block->load( $address, $user, $killExpired );
6766 if ( $block->isValid() ) {
@@ -72,11 +71,11 @@
7372
7473 /**
7574 * Load a blocked user from their block id.
 75+ *
7676 * @return Block object
7777 * @param $id int Block id to search for
7878 */
79 - static function newFromID( $id )
80 - {
 79+ static function newFromID( $id ) {
8180 $dbr = wfGetDB( DB_SLAVE );
8281 $res = $dbr->resultObject( $dbr->select( 'ipblocks', '*',
8382 array( 'ipb_id' => $id ), __METHOD__ ) );
@@ -92,8 +91,7 @@
9392 * Clear all member variables in the current object. Does not clear
9493 * the block from the DB.
9594 */
96 - function clear()
97 - {
 95+ function clear() {
9896 $this->mAddress = $this->mReason = $this->mTimestamp = '';
9997 $this->mId = $this->mAnonOnly = $this->mCreateAccount =
10098 $this->mEnableAutoblock = $this->mAuto = $this->mUser =
@@ -102,7 +100,10 @@
103101 }
104102
105103 /**
106 - * Get the DB object and set the reference parameter to the query options
 104+ * Get the DB object and set the reference parameter to the select options.
 105+ * The options array will contain FOR UPDATE if appropriate.
 106+ *
 107+ * @param array $options
107108 * @return Database
108109 */
109110 function &getDBOptions( &$options ) {
@@ -122,15 +123,14 @@
123124 }
124125
125126 /**
126 - * Get a ban from the DB, with either the given address or the given username
 127+ * Get a block from the DB, with either the given address or the given username
127128 *
128129 * @param $address string The IP address of the user, or blank to skip IP blocks
129130 * @param $user int The user ID, or zero for anonymous users
130131 * @param $killExpired bool Whether to delete expired rows while loading
131132 *
132133 */
133 - function load( $address = '', $user = 0, $killExpired = true )
134 - {
 134+ function load( $address = '', $user = 0, $killExpired = true ) {
135135 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
136136
137137 $options = array();
@@ -198,12 +198,12 @@
199199
200200 /**
201201 * Fill in member variables from a result wrapper
 202+ *
202203 * @return bool
203204 * @param $res ResultWrapper Row from the ipblocks table
204205 * @param $killExpired bool Whether to delete expired rows while loading
205206 */
206 - function loadFromResult( ResultWrapper $res, $killExpired = true )
207 - {
 207+ function loadFromResult( ResultWrapper $res, $killExpired = true ) {
208208 $ret = false;
209209 if ( 0 != $res->numRows() ) {
210210 # Get first block
@@ -237,13 +237,13 @@
238238 /**
239239 * Search the database for any range blocks matching the given address, and
240240 * load the row if one is found.
 241+ *
241242 * @return bool
242243 * @param $address string IP address range
243244 * @param $killExpired bool Whether to delete expired rows while loading
244245 * @param $userid int If not 0, then sets ipb_anon_only
245246 */
246 - function loadRange( $address, $killExpired = true, $user = 0 )
247 - {
 247+ function loadRange( $address, $killExpired = true, $user = 0 ) {
248248 $iaddr = IP::toHex( $address );
249249 if ( $iaddr === false ) {
250250 # Invalid address
@@ -282,10 +282,10 @@
283283 /**
284284 * Given a database row from the ipblocks table, initialize
285285 * member variables
 286+ *
286287 * @param $row ResultWrapper A row from the ipblocks table
287288 */
288 - function initFromRow( $row )
289 - {
 289+ function initFromRow( $row ) {
290290 $this->mAddress = $row->ipb_address;
291291 $this->mReason = $row->ipb_reason;
292292 $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
@@ -312,8 +312,7 @@
313313 * Once $mAddress has been set, get the range they came from.
314314 * Wrapper for IP::parseRange
315315 */
316 - function initialiseRange()
317 - {
 316+ function initialiseRange() {
318317 $this->mRangeStart = '';
319318 $this->mRangeEnd = '';
320319
@@ -324,10 +323,12 @@
325324
326325 /**
327326 * Callback with a Block object for every block
328 - * @return integer number of blocks;
 327+ *
 328+ * @deprecated Unlimited row count, do your own queries instead
 329+ *
 330+ * @return integer number of blocks
329331 */
330 - /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
331 - {
 332+ /*static*/ function enumBlocks( $callback, $tag, $flags = 0 ) {
332333 global $wgAntiLockFlags;
333334
334335 $block = new Block();
@@ -380,10 +381,10 @@
381382
382383 /**
383384 * Delete the row from the IP blocks table.
 385+ *
384386 * @return bool
385387 */
386 - function delete()
387 - {
 388+ function delete() {
388389 if (wfReadOnly()) {
389390 return false;
390391 }
@@ -397,11 +398,12 @@
398399 }
399400
400401 /**
401 - * Insert a block into the block table.
402 - * @return bool Whether or not the insertion was successful.
403 - */
404 - function insert()
405 - {
 402+ * Insert a block into the block table. Will fail if there is a conflicting
 403+ * block (same name and options) already in the database.
 404+ *
 405+ * @return bool Whether or not the insertion was successful.
 406+ */
 407+ function insert() {
406408 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
407409 $dbw = wfGetDB( DB_MASTER );
408410
@@ -436,11 +438,12 @@
437439 if ($affected)
438440 $this->doRetroactiveAutoblock();
439441
440 - return $affected;
 442+ return (bool)$affected;
441443 }
442444
443445 /**
444 - * Update block with new parameters.
 446+ * Update a block in the DB with new parameters.
 447+ * The ID field needs to be loaded first.
445448 */
446449 public function update() {
447450 wfDebug( "Block::update; timestamp {$this->mTimestamp}\n" );
@@ -500,6 +503,7 @@
501504 /**
502505 * Retroactively autoblocks the last IP used by the user (if it is a user)
503506 * blocked by this Block.
 507+ *
504508 * @return bool Whether or not a retroactive autoblock was made.
505509 */
506510 function doRetroactiveAutoblock() {
@@ -539,8 +543,9 @@
540544
541545 /**
542546 * Checks whether a given IP is on the autoblock whitelist.
 547+ *
543548 * @return bool
544 - * @param string $autoblockip The IP to check
 549+ * @param string $ip The IP to check
545550 */
546551 function isWhitelistedFromAutoblocks( $ip ) {
547552 global $wgMemc;
@@ -580,31 +585,32 @@
581586 }
582587
583588 /**
584 - * Autoblocks the given IP, referring to this Block.
585 - * @param string $autoblockip The IP to autoblock.
586 - * @param bool $justInserted The main block was just inserted
587 - * @return bool Whether or not an autoblock was inserted.
588 - */
589 - function doAutoblock( $autoblockip, $justInserted = false ) {
 589+ * Autoblocks the given IP, referring to this Block.
 590+ *
 591+ * @param string $autoblockIP The IP to autoblock.
 592+ * @param bool $justInserted The main block was just inserted
 593+ * @return bool Whether or not an autoblock was inserted.
 594+ */
 595+ function doAutoblock( $autoblockIP, $justInserted = false ) {
590596 # If autoblocks are disabled, go away.
591597 if ( !$this->mEnableAutoblock ) {
592598 return;
593599 }
594600
595601 # Check for presence on the autoblock whitelist
596 - if (Block::isWhitelistedFromAutoblocks($autoblockip)) {
 602+ if (Block::isWhitelistedFromAutoblocks($autoblockIP)) {
597603 return;
598604 }
599605
600606 ## Allow hooks to cancel the autoblock.
601 - if (!wfRunHooks( 'AbortAutoblock', array( $autoblockip, &$this ) )) {
 607+ if (!wfRunHooks( 'AbortAutoblock', array( $autoblockIP, &$this ) )) {
602608 wfDebug( "Autoblock aborted by hook." );
603609 return false;
604610 }
605611
606612 # It's okay to autoblock. Go ahead and create/insert the block.
607613
608 - $ipblock = Block::newFromDB( $autoblockip );
 614+ $ipblock = Block::newFromDB( $autoblockIP );
609615 if ( $ipblock ) {
610616 # If the user is already blocked. Then check if the autoblock would
611617 # exceed the user block. If it would exceed, then do nothing, else
@@ -623,8 +629,8 @@
624630 }
625631
626632 # Make a new block object with the desired properties
627 - wfDebug( "Autoblocking {$this->mAddress}@" . $autoblockip . "\n" );
628 - $ipblock->mAddress = $autoblockip;
 633+ wfDebug( "Autoblocking {$this->mAddress}@" . $autoblockIP . "\n" );
 634+ $ipblock->mAddress = $autoblockIP;
629635 $ipblock->mUser = 0;
630636 $ipblock->mBy = $this->mBy;
631637 $ipblock->mByName = $this->mByName;
@@ -650,8 +656,7 @@
651657 * Check if a block has expired. Delete it if it is.
652658 * @return bool
653659 */
654 - function deleteIfExpired()
655 - {
 660+ function deleteIfExpired() {
656661 $fname = 'Block::deleteIfExpired';
657662 wfProfileIn( $fname );
658663 if ( $this->isExpired() ) {
@@ -670,8 +675,7 @@
671676 * Has the block expired?
672677 * @return bool
673678 */
674 - function isExpired()
675 - {
 679+ function isExpired() {
676680 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
677681 if ( !$this->mExpiry ) {
678682 return false;
@@ -684,16 +688,14 @@
685689 * Is the block address valid (i.e. not a null string?)
686690 * @return bool
687691 */
688 - function isValid()
689 - {
 692+ function isValid() {
690693 return $this->mAddress != '';
691694 }
692695
693696 /**
694697 * Update the timestamp on autoblocks.
695698 */
696 - function updateTimestamp()
697 - {
 699+ function updateTimestamp() {
698700 if ( $this->mAuto ) {
699701 $this->mTimestamp = wfTimestamp();
700702 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
@@ -710,19 +712,9 @@
711713 }
712714 }
713715
714 - /*
715 - function getIntegerAddr()
716 - {
717 - return $this->mIntegerAddr;
718 - }
719 -
720 - function getNetworkBits()
721 - {
722 - return $this->mNetworkBits;
723 - }*/
724 -
725716 /**
726717 * Get the user id of the blocking sysop
 718+ *
727719 * @return int
728720 */
729721 public function getBy() {
@@ -731,21 +723,31 @@
732724
733725 /**
734726 * Get the username of the blocking sysop
 727+ *
735728 * @return string
736729 */
737 - function getByName()
738 - {
 730+ function getByName() {
739731 return $this->mByName;
740732 }
741733
 734+ /**
 735+ * Get/set the SELECT ... FOR UPDATE flag
 736+ */
742737 function forUpdate( $x = NULL ) {
743738 return wfSetVar( $this->mForUpdate, $x );
744739 }
745740
 741+ /**
 742+ * Get/set a flag determining whether the master is used for reads
 743+ */
746744 function fromMaster( $x = NULL ) {
747745 return wfSetVar( $this->mFromMaster, $x );
748746 }
749747
 748+ /**
 749+ * Get the block name, but with autoblocked IPs hidden as per standard privacy policy
 750+ * @return string
 751+ */
750752 function getRedactedName() {
751753 if ( $this->mAuto ) {
752754 return '#' . $this->mId;
@@ -756,6 +758,7 @@
757759
758760 /**
759761 * Encode expiry for DB
 762+ *
760763 * @return string
761764 * @param $expiry string Timestamp for expiry, or
762765 * @param $db Database object
@@ -770,6 +773,7 @@
771774
772775 /**
773776 * Decode expiry which has come from the DB
 777+ *
774778 * @return string
775779 * @param $expiry string Database expiry format
776780 * @param $timestampType Requested timestamp format
@@ -784,10 +788,10 @@
785789
786790 /**
787791 * Get a timestamp of the expiry for autoblocks
 792+ *
788793 * @return string
789794 */
790 - static function getAutoblockExpiry( $timestamp )
791 - {
 795+ static function getAutoblockExpiry( $timestamp ) {
792796 global $wgAutoblockExpiry;
793797 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
794798 }
@@ -836,16 +840,21 @@
837841 }
838842
839843 /**
840 - * This is a special keyword for timestamps in PostgreSQL, and
841 - * works with CHAR(14) as well because "i" sorts after all numbers.
 844+ * Get a value to insert into expiry field of the database when infinite expiry
 845+ * is desired. In principle this could be DBMS-dependant, but currently all
 846+ * supported DBMS's support the string "infinity", so we just use that.
 847+ *
842848 * @return string
843849 */
844850 static function infinity() {
 851+ # This is a special keyword for timestamps in PostgreSQL, and
 852+ # works with CHAR(14) as well because "i" sorts after all numbers.
845853 return 'infinity';
846854 }
847855
848856 /**
849857 * Convert a DB-encoded expiry into a real string that humans can read.
 858+ *
850859 * @param $encoded_expiry string Database encoded expiry time
851860 * @return string
852861 */