00001 <?php
00016 class Block {
00017 var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry,
00018 $mRangeStart, $mRangeEnd, $mAnonOnly, $mEnableAutoblock, $mHideName,
00019 $mBlockEmail, $mByName, $mAngryAutoblock, $mAllowUsertalk;
00020 var $mNetworkBits, $mIntegerAddr, $mForUpdate, $mFromMaster;
00021
00022 const EB_KEEP_EXPIRED = 1;
00023 const EB_FOR_UPDATE = 2;
00024 const EB_RANGE_ONLY = 4;
00025
00026 function __construct( $address = '', $user = 0, $by = 0, $reason = '',
00027 $timestamp = '' , $auto = 0, $expiry = '', $anonOnly = 0, $createAccount = 0, $enableAutoblock = 0,
00028 $hideName = 0, $blockEmail = 0, $allowUsertalk = 0 )
00029 {
00030 $this->mId = 0;
00031 # Expand valid IPv6 addresses
00032 $address = IP::sanitizeIP( $address );
00033 $this->mAddress = $address;
00034 $this->mUser = $user;
00035 $this->mBy = $by;
00036 $this->mReason = $reason;
00037 $this->mTimestamp = wfTimestamp( TS_MW, $timestamp );
00038 $this->mAuto = $auto;
00039 $this->mAnonOnly = $anonOnly;
00040 $this->mCreateAccount = $createAccount;
00041 $this->mExpiry = self::decodeExpiry( $expiry );
00042 $this->mEnableAutoblock = $enableAutoblock;
00043 $this->mHideName = $hideName;
00044 $this->mBlockEmail = $blockEmail;
00045 $this->mAllowUsertalk = $allowUsertalk;
00046 $this->mForUpdate = false;
00047 $this->mFromMaster = false;
00048 $this->mByName = false;
00049 $this->mAngryAutoblock = false;
00050 $this->initialiseRange();
00051 }
00052
00063 public static function newFromDB( $address, $user = 0, $killExpired = true ) {
00064 $block = new Block;
00065 $block->load( $address, $user, $killExpired );
00066 if ( $block->isValid() ) {
00067 return $block;
00068 } else {
00069 return null;
00070 }
00071 }
00072
00079 public static function newFromID( $id ) {
00080 $dbr = wfGetDB( DB_SLAVE );
00081 $res = $dbr->resultObject( $dbr->select( 'ipblocks', '*',
00082 array( 'ipb_id' => $id ), __METHOD__ ) );
00083 $block = new Block;
00084 if ( $block->loadFromResult( $res ) ) {
00085 return $block;
00086 } else {
00087 return null;
00088 }
00089 }
00090
00096 public function equals( Block $block ) {
00097 return (
00098 $this->mAddress == $block->mAddress
00099 && $this->mUser == $block->mUser
00100 && $this->mAuto == $block->mAuto
00101 && $this->mAnonOnly == $block->mAnonOnly
00102 && $this->mCreateAccount == $block->mCreateAccount
00103 && $this->mExpiry == $block->mExpiry
00104 && $this->mEnableAutoblock == $block->mEnableAutoblock
00105 && $this->mHideName == $block->mHideName
00106 && $this->mBlockEmail == $block->mBlockEmail
00107 && $this->mAllowUsertalk == $block->mAllowUsertalk
00108 && $this->mReason == $block->mReason
00109 );
00110 }
00111
00116 public function clear() {
00117 $this->mAddress = $this->mReason = $this->mTimestamp = '';
00118 $this->mId = $this->mAnonOnly = $this->mCreateAccount =
00119 $this->mEnableAutoblock = $this->mAuto = $this->mUser =
00120 $this->mBy = $this->mHideName = $this->mBlockEmail = $this->mAllowUsertalk = 0;
00121 $this->mByName = false;
00122 }
00123
00131 protected function &getDBOptions( &$options ) {
00132 global $wgAntiLockFlags;
00133
00134 if ( $this->mForUpdate || $this->mFromMaster ) {
00135 $db = wfGetDB( DB_MASTER );
00136 if ( !$this->mForUpdate || ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK ) ) {
00137 $options = array();
00138 } else {
00139 $options = array( 'FOR UPDATE' );
00140 }
00141 } else {
00142 $db = wfGetDB( DB_SLAVE );
00143 $options = array();
00144 }
00145 return $db;
00146 }
00147
00157 public function load( $address = '', $user = 0, $killExpired = true ) {
00158 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
00159
00160 $options = array();
00161 $db =& $this->getDBOptions( $options );
00162
00163 if ( 0 == $user && $address == '' ) {
00164 # Invalid user specification, not blocked
00165 $this->clear();
00166 return false;
00167 }
00168
00169 # Try user block
00170 if ( $user ) {
00171 $res = $db->resultObject( $db->select( 'ipblocks', '*', array( 'ipb_user' => $user ),
00172 __METHOD__, $options ) );
00173 if ( $this->loadFromResult( $res, $killExpired ) ) {
00174 return true;
00175 }
00176 }
00177
00178 # Try IP block
00179 # TODO: improve performance by merging this query with the autoblock one
00180 # Slightly tricky while handling killExpired as well
00181 if ( $address ) {
00182 $conds = array( 'ipb_address' => $address, 'ipb_auto' => 0 );
00183 $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
00184
00185 if ( $this->loadFromResult( $res, $killExpired ) ) {
00186 if ( $user && $this->mAnonOnly ) {
00187 # Block is marked anon-only
00188 # Whitelist this IP address against autoblocks and range blocks
00189 # (but not account creation blocks -- bug 13611)
00190 if ( !$this->mCreateAccount ) {
00191 $this->clear();
00192 }
00193 return false;
00194 } else {
00195 return true;
00196 }
00197 }
00198 }
00199
00200 # Try range block
00201 if ( $this->loadRange( $address, $killExpired, $user ) ) {
00202 if ( $user && $this->mAnonOnly ) {
00203 # Respect account creation blocks on logged-in users -- bug 13611
00204 if ( !$this->mCreateAccount ) {
00205 $this->clear();
00206 }
00207 return false;
00208 } else {
00209 return true;
00210 }
00211 }
00212
00213 # Try autoblock
00214 if ( $address ) {
00215 $conds = array( 'ipb_address' => $address, 'ipb_auto' => 1 );
00216
00217 if ( $user ) {
00218 $conds['ipb_anon_only'] = 0;
00219 }
00220
00221 $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
00222
00223 if ( $this->loadFromResult( $res, $killExpired ) ) {
00224 return true;
00225 }
00226 }
00227
00228 # Give up
00229 $this->clear();
00230 return false;
00231 }
00232
00240 protected function loadFromResult( ResultWrapper $res, $killExpired = true ) {
00241 $ret = false;
00242
00243 if ( 0 != $res->numRows() ) {
00244 # Get first block
00245 $row = $res->fetchObject();
00246 $this->initFromRow( $row );
00247
00248 if ( $killExpired ) {
00249 # If requested, delete expired rows
00250 do {
00251 $killed = $this->deleteIfExpired();
00252 if ( $killed ) {
00253 $row = $res->fetchObject();
00254 if ( $row ) {
00255 $this->initFromRow( $row );
00256 }
00257 }
00258 } while ( $killed && $row );
00259
00260 # If there were any left after the killing finished, return true
00261 if ( $row ) {
00262 $ret = true;
00263 }
00264 } else {
00265 $ret = true;
00266 }
00267 }
00268 $res->free();
00269 return $ret;
00270 }
00271
00281 public function loadRange( $address, $killExpired = true, $user = 0 ) {
00282 $iaddr = IP::toHex( $address );
00283
00284 if ( $iaddr === false ) {
00285 # Invalid address
00286 return false;
00287 }
00288
00289 # Only scan ranges which start in this /16, this improves search speed
00290 # Blocks should not cross a /16 boundary.
00291 $range = substr( $iaddr, 0, 4 );
00292
00293 $options = array();
00294 $db =& $this->getDBOptions( $options );
00295 $conds = array(
00296 'ipb_range_start' . $db->buildLike( $range, $db->anyString() ),
00297 "ipb_range_start <= '$iaddr'",
00298 "ipb_range_end >= '$iaddr'"
00299 );
00300
00301 if ( $user ) {
00302 $conds['ipb_anon_only'] = 0;
00303 }
00304
00305 $res = $db->resultObject( $db->select( 'ipblocks', '*', $conds, __METHOD__, $options ) );
00306 $success = $this->loadFromResult( $res, $killExpired );
00307 return $success;
00308 }
00309
00316 public function initFromRow( $row ) {
00317 $this->mAddress = $row->ipb_address;
00318 $this->mReason = $row->ipb_reason;
00319 $this->mTimestamp = wfTimestamp( TS_MW, $row->ipb_timestamp );
00320 $this->mUser = $row->ipb_user;
00321 $this->mBy = $row->ipb_by;
00322 $this->mAuto = $row->ipb_auto;
00323 $this->mAnonOnly = $row->ipb_anon_only;
00324 $this->mCreateAccount = $row->ipb_create_account;
00325 $this->mEnableAutoblock = $row->ipb_enable_autoblock;
00326 $this->mBlockEmail = $row->ipb_block_email;
00327 $this->mAllowUsertalk = $row->ipb_allow_usertalk;
00328 $this->mHideName = $row->ipb_deleted;
00329 $this->mId = $row->ipb_id;
00330 $this->mExpiry = self::decodeExpiry( $row->ipb_expiry );
00331
00332 if ( isset( $row->user_name ) ) {
00333 $this->mByName = $row->user_name;
00334 } else {
00335 $this->mByName = $row->ipb_by_text;
00336 }
00337
00338 $this->mRangeStart = $row->ipb_range_start;
00339 $this->mRangeEnd = $row->ipb_range_end;
00340 }
00341
00346 protected function initialiseRange() {
00347 $this->mRangeStart = '';
00348 $this->mRangeEnd = '';
00349
00350 if ( $this->mUser == 0 ) {
00351 list( $this->mRangeStart, $this->mRangeEnd ) = IP::parseRange( $this->mAddress );
00352 }
00353 }
00354
00360 public function delete() {
00361 if ( wfReadOnly() ) {
00362 return false;
00363 }
00364
00365 if ( !$this->mId ) {
00366 throw new MWException( "Block::delete() now requires that the mId member be filled\n" );
00367 }
00368
00369 $dbw = wfGetDB( DB_MASTER );
00370 $dbw->delete( 'ipblocks', array( 'ipb_id' => $this->mId ), __METHOD__ );
00371 return $dbw->affectedRows() > 0;
00372 }
00373
00380 public function insert() {
00381 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
00382 $dbw = wfGetDB( DB_MASTER );
00383
00384 $this->validateBlockParams();
00385 $this->initialiseRange();
00386
00387 # Don't collide with expired blocks
00388 Block::purgeExpired();
00389
00390 $ipb_id = $dbw->nextSequenceValue( 'ipblocks_ipb_id_seq' );
00391 $dbw->insert(
00392 'ipblocks',
00393 array(
00394 'ipb_id' => $ipb_id,
00395 'ipb_address' => $this->mAddress,
00396 'ipb_user' => $this->mUser,
00397 'ipb_by' => $this->mBy,
00398 'ipb_by_text' => $this->mByName,
00399 'ipb_reason' => $this->mReason,
00400 'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp ),
00401 'ipb_auto' => $this->mAuto,
00402 'ipb_anon_only' => $this->mAnonOnly,
00403 'ipb_create_account' => $this->mCreateAccount,
00404 'ipb_enable_autoblock' => $this->mEnableAutoblock,
00405 'ipb_expiry' => self::encodeExpiry( $this->mExpiry, $dbw ),
00406 'ipb_range_start' => $this->mRangeStart,
00407 'ipb_range_end' => $this->mRangeEnd,
00408 'ipb_deleted' => intval( $this->mHideName ),
00409 'ipb_block_email' => $this->mBlockEmail,
00410 'ipb_allow_usertalk' => $this->mAllowUsertalk
00411 ),
00412 'Block::insert',
00413 array( 'IGNORE' )
00414 );
00415 $affected = $dbw->affectedRows();
00416
00417 if ( $affected )
00418 $this->doRetroactiveAutoblock();
00419
00420 return (bool)$affected;
00421 }
00422
00427 public function update() {
00428 wfDebug( "Block::update; timestamp {$this->mTimestamp}\n" );
00429 $dbw = wfGetDB( DB_MASTER );
00430
00431 $this->validateBlockParams();
00432
00433 $dbw->update(
00434 'ipblocks',
00435 array(
00436 'ipb_user' => $this->mUser,
00437 'ipb_by' => $this->mBy,
00438 'ipb_by_text' => $this->mByName,
00439 'ipb_reason' => $this->mReason,
00440 'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp ),
00441 'ipb_auto' => $this->mAuto,
00442 'ipb_anon_only' => $this->mAnonOnly,
00443 'ipb_create_account' => $this->mCreateAccount,
00444 'ipb_enable_autoblock' => $this->mEnableAutoblock,
00445 'ipb_expiry' => self::encodeExpiry( $this->mExpiry, $dbw ),
00446 'ipb_range_start' => $this->mRangeStart,
00447 'ipb_range_end' => $this->mRangeEnd,
00448 'ipb_deleted' => $this->mHideName,
00449 'ipb_block_email' => $this->mBlockEmail,
00450 'ipb_allow_usertalk' => $this->mAllowUsertalk
00451 ),
00452 array( 'ipb_id' => $this->mId ),
00453 'Block::update'
00454 );
00455
00456 return $dbw->affectedRows();
00457 }
00458
00463 protected function validateBlockParams() {
00464 # Unset ipb_anon_only for user blocks, makes no sense
00465 if ( $this->mUser ) {
00466 $this->mAnonOnly = 0;
00467 }
00468
00469 # Unset ipb_enable_autoblock for IP blocks, makes no sense
00470 if ( !$this->mUser ) {
00471 $this->mEnableAutoblock = 0;
00472 }
00473
00474 # bug 18860: non-anon-only IP blocks should be allowed to block email
00475 if ( !$this->mUser && $this->mAnonOnly ) {
00476 $this->mBlockEmail = 0;
00477 }
00478 if ( !$this->mByName ) {
00479 if ( $this->mBy ) {
00480 $this->mByName = User::whoIs( $this->mBy );
00481 } else {
00482 global $wgUser;
00483 $this->mByName = $wgUser->getName();
00484 }
00485 }
00486 }
00487
00494 public function doRetroactiveAutoblock() {
00495 $dbr = wfGetDB( DB_SLAVE );
00496 # If autoblock is enabled, autoblock the LAST IP used
00497 # - stolen shamelessly from CheckUser_body.php
00498
00499 if ( $this->mEnableAutoblock && $this->mUser ) {
00500 wfDebug( "Doing retroactive autoblocks for " . $this->mAddress . "\n" );
00501
00502 $options = array( 'ORDER BY' => 'rc_timestamp DESC' );
00503 $conds = array( 'rc_user_text' => $this->mAddress );
00504
00505 if ( $this->mAngryAutoblock ) {
00506
00507 $conds[] = 'rc_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( time() - ( 7 * 86400 ) ) );
00508 $options['LIMIT'] = 5;
00509 } else {
00510
00511 $options['LIMIT'] = 1;
00512 }
00513
00514 $res = $dbr->select( 'recentchanges', array( 'rc_ip' ), $conds,
00515 __METHOD__ , $options );
00516
00517 if ( !$dbr->numRows( $res ) ) {
00518 # No results, don't autoblock anything
00519 wfDebug( "No IP found to retroactively autoblock\n" );
00520 } else {
00521 while ( $row = $dbr->fetchObject( $res ) ) {
00522 if ( $row->rc_ip )
00523 $this->doAutoblock( $row->rc_ip );
00524 }
00525 }
00526 }
00527 }
00528
00535 public static function isWhitelistedFromAutoblocks( $ip ) {
00536 global $wgMemc;
00537
00538
00539
00540 $key = wfMemcKey( 'ipb', 'autoblock', 'whitelist' );
00541 $lines = $wgMemc->get( $key );
00542 if ( !$lines ) {
00543 $lines = explode( "\n", wfMsgForContentNoTrans( 'autoblock_whitelist' ) );
00544 $wgMemc->set( $key, $lines, 3600 * 24 );
00545 }
00546
00547 wfDebug( "Checking the autoblock whitelist..\n" );
00548
00549 foreach ( $lines as $line ) {
00550 # List items only
00551 if ( substr( $line, 0, 1 ) !== '*' ) {
00552 continue;
00553 }
00554
00555 $wlEntry = substr( $line, 1 );
00556 $wlEntry = trim( $wlEntry );
00557
00558 wfDebug( "Checking $ip against $wlEntry..." );
00559
00560 # Is the IP in this range?
00561 if ( IP::isInRange( $ip, $wlEntry ) ) {
00562 wfDebug( " IP $ip matches $wlEntry, not autoblocking\n" );
00563 return true;
00564 } else {
00565 wfDebug( " No match\n" );
00566 }
00567 }
00568
00569 return false;
00570 }
00571
00579 public function doAutoblock( $autoblockIP, $justInserted = false ) {
00580 # If autoblocks are disabled, go away.
00581 if ( !$this->mEnableAutoblock ) {
00582 return;
00583 }
00584
00585 # Check for presence on the autoblock whitelist
00586 if ( Block::isWhitelistedFromAutoblocks( $autoblockIP ) ) {
00587 return;
00588 }
00589
00590 # # Allow hooks to cancel the autoblock.
00591 if ( !wfRunHooks( 'AbortAutoblock', array( $autoblockIP, &$this ) ) ) {
00592 wfDebug( "Autoblock aborted by hook.\n" );
00593 return false;
00594 }
00595
00596 # It's okay to autoblock. Go ahead and create/insert the block.
00597
00598 $ipblock = Block::newFromDB( $autoblockIP );
00599 if ( $ipblock ) {
00600 # If the user is already blocked. Then check if the autoblock would
00601 # exceed the user block. If it would exceed, then do nothing, else
00602 # prolong block time
00603 if ( $this->mExpiry &&
00604 ( $this->mExpiry < Block::getAutoblockExpiry( $ipblock->mTimestamp ) ) ) {
00605 return;
00606 }
00607 # Just update the timestamp
00608 if ( !$justInserted ) {
00609 $ipblock->updateTimestamp();
00610 }
00611 return;
00612 } else {
00613 $ipblock = new Block;
00614 }
00615
00616 # Make a new block object with the desired properties
00617 wfDebug( "Autoblocking {$this->mAddress}@" . $autoblockIP . "\n" );
00618 $ipblock->mAddress = $autoblockIP;
00619 $ipblock->mUser = 0;
00620 $ipblock->mBy = $this->mBy;
00621 $ipblock->mByName = $this->mByName;
00622 $ipblock->mReason = wfMsgForContent( 'autoblocker', $this->mAddress, $this->mReason );
00623 $ipblock->mTimestamp = wfTimestampNow();
00624 $ipblock->mAuto = 1;
00625 $ipblock->mCreateAccount = $this->mCreateAccount;
00626 # Continue suppressing the name if needed
00627 $ipblock->mHideName = $this->mHideName;
00628 $ipblock->mAllowUsertalk = $this->mAllowUsertalk;
00629 # If the user is already blocked with an expiry date, we don't
00630 # want to pile on top of that!
00631 if ( $this->mExpiry ) {
00632 $ipblock->mExpiry = min( $this->mExpiry, Block::getAutoblockExpiry( $this->mTimestamp ) );
00633 } else {
00634 $ipblock->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
00635 }
00636 # Insert it
00637 return $ipblock->insert();
00638 }
00639
00644 public function deleteIfExpired() {
00645 wfProfileIn( __METHOD__ );
00646 if ( $this->isExpired() ) {
00647 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
00648 $this->delete();
00649 $retVal = true;
00650 } else {
00651 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
00652 $retVal = false;
00653 }
00654 wfProfileOut( __METHOD__ );
00655 return $retVal;
00656 }
00657
00662 public function isExpired() {
00663 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
00664 if ( !$this->mExpiry ) {
00665 return false;
00666 } else {
00667 return wfTimestampNow() > $this->mExpiry;
00668 }
00669 }
00670
00675 public function isValid() {
00676 return $this->mAddress != '';
00677 }
00678
00682 public function updateTimestamp() {
00683 if ( $this->mAuto ) {
00684 $this->mTimestamp = wfTimestamp();
00685 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
00686
00687 $dbw = wfGetDB( DB_MASTER );
00688 $dbw->update( 'ipblocks',
00689 array(
00690 'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp ),
00691 'ipb_expiry' => $dbw->timestamp( $this->mExpiry ),
00692 ), array(
00693 'ipb_address' => $this->mAddress
00694 ), 'Block::updateTimestamp'
00695 );
00696 }
00697 }
00698
00704 public function getBy() {
00705 return $this->mBy;
00706 }
00707
00713 public function getByName() {
00714 return $this->mByName;
00715 }
00716
00720 public function forUpdate( $x = null ) {
00721 return wfSetVar( $this->mForUpdate, $x );
00722 }
00723
00727 public function fromMaster( $x = null ) {
00728 return wfSetVar( $this->mFromMaster, $x );
00729 }
00730
00735 public function getRedactedName() {
00736 if ( $this->mAuto ) {
00737 return '#' . $this->mId;
00738 } else {
00739 return $this->mAddress;
00740 }
00741 }
00742
00750 public static function encodeExpiry( $expiry, $db ) {
00751 if ( $expiry == '' || $expiry == Block::infinity() ) {
00752 return Block::infinity();
00753 } else {
00754 return $db->timestamp( $expiry );
00755 }
00756 }
00757
00765 public static function decodeExpiry( $expiry, $timestampType = TS_MW ) {
00766 if ( $expiry == '' || $expiry == Block::infinity() ) {
00767 return Block::infinity();
00768 } else {
00769 return wfTimestamp( $timestampType, $expiry );
00770 }
00771 }
00772
00778 public static function getAutoblockExpiry( $timestamp ) {
00779 global $wgAutoblockExpiry;
00780 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
00781 }
00782
00789 public static function normaliseRange( $range ) {
00790 $parts = explode( '/', $range );
00791 if ( count( $parts ) == 2 ) {
00792
00793 if ( IP::isIPv6( $range ) && $parts[1] >= 64 && $parts[1] <= 128 ) {
00794 $bits = $parts[1];
00795 $ipint = IP::toUnsigned6( $parts[0] );
00796 # Native 32 bit functions WON'T work here!!!
00797 # Convert to a padded binary number
00798 $network = wfBaseConvert( $ipint, 10, 2, 128 );
00799 # Truncate the last (128-$bits) bits and replace them with zeros
00800 $network = str_pad( substr( $network, 0, $bits ), 128, 0, STR_PAD_RIGHT );
00801 # Convert back to an integer
00802 $network = wfBaseConvert( $network, 2, 10 );
00803 # Reform octet address
00804 $newip = IP::toOctet( $network );
00805 $range = "$newip/{$parts[1]}";
00806 }
00807 elseif ( IP::isIPv4( $range ) && $parts[1] >= 16 && $parts[1] <= 32 ) {
00808 $shift = 32 - $parts[1];
00809 $ipint = IP::toUnsigned( $parts[0] );
00810 $ipint = $ipint >> $shift << $shift;
00811 $newip = long2ip( $ipint );
00812 $range = "$newip/{$parts[1]}";
00813 }
00814 }
00815 return $range;
00816 }
00817
00821 public static function purgeExpired() {
00822 $dbw = wfGetDB( DB_MASTER );
00823 $dbw->delete( 'ipblocks', array( 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ), __METHOD__ );
00824 }
00825
00833 public static function infinity() {
00834 # This is a special keyword for timestamps in PostgreSQL, and
00835 # works with CHAR(14) as well because "i" sorts after all numbers.
00836 return 'infinity';
00837 }
00838
00845 public static function formatExpiry( $encoded_expiry ) {
00846 static $msg = null;
00847
00848 if ( is_null( $msg ) ) {
00849 $msg = array();
00850 $keys = array( 'infiniteblock', 'expiringblock' );
00851 foreach ( $keys as $key ) {
00852 $msg[$key] = wfMsgHtml( $key );
00853 }
00854 }
00855
00856 $expiry = Block::decodeExpiry( $encoded_expiry );
00857 if ( $expiry == 'infinity' ) {
00858 $expirystr = $msg['infiniteblock'];
00859 } else {
00860 global $wgLang;
00861 $expiredatestr = htmlspecialchars( $wgLang->date( $expiry, true ) );
00862 $expiretimestr = htmlspecialchars( $wgLang->time( $expiry, true ) );
00863 $expirystr = wfMsgReplaceArgs( $msg['expiringblock'], array( $expiredatestr, $expiretimestr ) );
00864 }
00865 return $expirystr;
00866 }
00867
00873 public static function parseExpiryInput( $expiry_input ) {
00874 if ( $expiry_input == 'infinite' || $expiry_input == 'indefinite' ) {
00875 $expiry = 'infinity';
00876 } else {
00877 $expiry = strtotime( $expiry_input );
00878
00879 if ( $expiry < 0 || $expiry === false ) {
00880 return false;
00881 }
00882 }
00883 return $expiry;
00884 }
00885
00886 }