From ad0e525edea703b48faca961ecfe4d386d236634 Mon Sep 17 00:00:00 2001 From: Happy-melon Date: Fri, 18 Mar 2011 16:35:22 +0000 Subject: [PATCH] First forays into the swamp of the Block.php backend: * Move Block::parseExpiryInput() to the frontend SpecialBlock::parseExpiryInput() * consolidate the several implementations of the MediaWiki:Ipblockoptions parsing into SpecialBlock::getSuggestedDurations() --- includes/Block.php | 35 ++++++++++------------- includes/api/ApiQueryLogEvents.php | 2 +- includes/specials/SpecialBlock.php | 45 +++++++++++++++++++++++------- languages/Language.php | 21 ++++---------- 4 files changed, 56 insertions(+), 47 deletions(-) diff --git a/includes/Block.php b/includes/Block.php index b8a4f80eba..422c12b1e1 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -885,8 +885,8 @@ class Block { } } - $expiry = Block::decodeExpiry( $encoded_expiry ); - if ( $expiry == 'infinity' ) { + $expiry = self::decodeExpiry( $encoded_expiry ); + if ( $expiry == self::infinity() ) { $expirystr = $msg['infiniteblock']; } else { global $wgLang; @@ -898,27 +898,20 @@ class Block { return $expirystr; } + # FIXME: everything above here is a mess, needs much cleaning up + /** - * Convert a typed-in expiry time into something we can put into the database. - * @param $expiry_input String: whatever was typed into the form - * @return String: more database friendly + * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute + * ("24 May 2034"), into an absolute timestamp we can put into the database. + * @param $expiry String: whatever was typed into the form + * @return String: timestamp or "infinity" string for th DB implementation + * @deprecated since 1.18 moved to SpecialBlock::parseExpiryInput() */ - public static function parseExpiryInput( $expiry_input ) { - if ( $expiry_input == 'infinite' || $expiry_input == 'indefinite' ) { - $expiry = 'infinity'; - } else { - $expiry = strtotime( $expiry_input ); - - if ( $expiry < 0 || $expiry === false ) { - return false; - } - } - - return $expiry; + public static function parseExpiryInput( $expiry ) { + wfDeprecated( __METHOD__ ); + return SpecialBlock::parseExpiryInput( $expiry ); } - # FIXME: everything above here is a mess, needs much cleaning up - /** * Given a target and the target's type, get an existing Block object if possible. * Note that passing an IP address will get an applicable rangeblock if the IP is @@ -1015,12 +1008,12 @@ class Block { } public function getType(){ - list( $target, $type ) = $this->getTargetAndType(); + list( /*...*/, $type ) = $this->getTargetAndType(); return $type; } public function getTarget(){ - list( $target, $type ) = $this->getTargetAndType(); + list( $target, /*...*/ ) = $this->getTargetAndType(); return $target; } } diff --git a/includes/api/ApiQueryLogEvents.php b/includes/api/ApiQueryLogEvents.php index 0ba070cb33..54f5ee93fc 100644 --- a/includes/api/ApiQueryLogEvents.php +++ b/includes/api/ApiQueryLogEvents.php @@ -239,7 +239,7 @@ class ApiQueryLogEvents extends ApiQueryBase { list( $vals2['duration'], $vals2['flags'] ) = $params; // Indefinite blocks have no expiry time - if ( Block::parseExpiryInput( $params[0] ) !== Block::infinity() ) { + if ( SpecialBlock::parseExpiryInput( $params[0] ) !== Block::infinity() ) { $vals2['expiry'] = wfTimestamp( TS_ISO_8601, strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) ); } diff --git a/includes/specials/SpecialBlock.php b/includes/specials/SpecialBlock.php index 3245883a76..17e59e0eb1 100644 --- a/includes/specials/SpecialBlock.php +++ b/includes/specials/SpecialBlock.php @@ -120,7 +120,7 @@ class SpecialBlock extends SpecialPage { 'validation-callback' => array( __CLASS__, 'validateTargetField' ), ), 'Expiry' => array( - 'type' => 'selectorother', + 'type' => self::getSuggestedDurations() === array() ? 'text' : 'selectorother', 'label-message' => 'ipbexpiry', 'required' => true, 'tabindex' => '2', @@ -139,10 +139,6 @@ class SpecialBlock extends SpecialPage { ), ); - if( wfMsgForContent( 'ipboptions' ) == '-' ){ - $a['Expiry']['type'] = 'text'; - } - if( self::canBlockEmail( $wgUser ) ) { $a['DisableEmail'] = array( 'type' => 'check', @@ -501,7 +497,7 @@ class SpecialBlock extends SpecialPage { } if( ( strlen( $data['Expiry'] ) == 0) || ( strlen( $data['Expiry'] ) > 50 ) - || !Block::parseExpiryInput( $data['Expiry'] ) ) + || !self::parseExpiryInput( $data['Expiry'] ) ) { return array( 'ipb_expiry_invalid' ); } @@ -548,7 +544,7 @@ class SpecialBlock extends SpecialPage { $data['Reason'][0], # Reason string wfTimestampNow(), # Block Timestamp 0, # Is this an autoblock (no) - Block::parseExpiryInput( $data['Expiry'] ), # Expiry time + self::parseExpiryInput( $data['Expiry'] ), # Expiry time !$data['HardBlock'], # Block anon only $data['CreateAccount'], $data['AutoBlock'], @@ -645,16 +641,45 @@ class SpecialBlock extends SpecialPage { * to the standard "**|" format? * @return Array */ - protected static function getSuggestedDurations(){ + public static function getSuggestedDurations( $lang = null ){ $a = array(); - foreach( explode( ',', wfMsgForContent( 'ipboptions' ) ) as $option ) { - if( strpos( $option, ':' ) === false ) $option = "$option:$option"; + $msg = $lang === null + ? wfMessage( 'ipboptions' )->inContentLanguage() + : wfMessage( 'ipboptions' )->inLanguage( $lang ); + + if( $msg == '-' ){ + return array(); + } + + foreach( explode( ',', $msg ) as $option ) { + if( strpos( $option, ':' ) === false ){ + $option = "$option:$option"; + } list( $show, $value ) = explode( ':', $option ); $a[htmlspecialchars( $show )] = htmlspecialchars( $value ); } return $a; } + /** + * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute + * ("24 May 2034", etc), into an absolute timestamp we can put into the database. + * @param $expiry String: whatever was typed into the form + * @return String: timestamp or "infinity" string for the DB implementation + */ + public static function parseExpiryInput( $expiry ) { + if ( $expiry == 'infinite' || $expiry == 'indefinite' ) { + $expiry = Block::infinity(); + } else { + $expiry = strtotime( $expiry ); + if ( $expiry < 0 || $expiry === false ) { + return false; + } + $expiry = wfTimestamp( TS_MW, $expiry ); + } + return $expiry; + } + /** * Can we do an email block? * @param $user User: the sysop wanting to make a block diff --git a/languages/Language.php b/languages/Language.php index de6bc395e5..6d971b5d06 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -2675,28 +2675,19 @@ class Language { } /** - * For translating of expiry times - * @param $str String: the validated block time in English - * @return Somehow translated block time + * Maybe translate block durations. Note that this function is somewhat misnamed: it + * deals with translating the *duration* ("1 week", "4 days", etc), not the expiry time + * (which is an absolute timestamp). + * @param $str String: the validated block duration in English + * @return Somehow translated block duration * @see LanguageFi.php for example implementation */ function translateBlockExpiry( $str ) { - $scBlockExpiryOptions = $this->getMessageFromDB( 'ipboptions' ); - - if ( $scBlockExpiryOptions == '-' ) { - return $str; - } - - foreach ( explode( ',', $scBlockExpiryOptions ) as $option ) { - if ( strpos( $option, ':' ) === false ) { - continue; - } - list( $show, $value ) = explode( ':', $option ); + foreach( SpecialBlock::getSuggestedDurations( $this ) as $show => $value ){ if ( strcmp( $str, $value ) == 0 ) { return htmlspecialchars( trim( $show ) ); } } - return $str; } -- 2.20.1