First forays into the swamp of the Block.php backend:
authorHappy-melon <happy-melon@users.mediawiki.org>
Fri, 18 Mar 2011 16:35:22 +0000 (16:35 +0000)
committerHappy-melon <happy-melon@users.mediawiki.org>
Fri, 18 Mar 2011 16:35:22 +0000 (16:35 +0000)
* Move Block::parseExpiryInput() to the frontend SpecialBlock::parseExpiryInput()
* consolidate the several implementations of the MediaWiki:Ipblockoptions parsing into SpecialBlock::getSuggestedDurations()

includes/Block.php
includes/api/ApiQueryLogEvents.php
includes/specials/SpecialBlock.php
languages/Language.php

index b8a4f80..422c12b 100644 (file)
@@ -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;
        }
 }
index 0ba070c..54f5ee9 100644 (file)
@@ -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 ) ) );
                                }
index 3245883..17e59e0 100644 (file)
@@ -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 "**<duration>|<displayname>" 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
index de6bc39..6d971b5 100644 (file)
@@ -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;
        }