More cleanup in Block.php. Push Block::encodeExpiry() and Block::decodeExpiry()...
authorHappy-melon <happy-melon@users.mediawiki.org>
Fri, 18 Mar 2011 19:15:56 +0000 (19:15 +0000)
committerHappy-melon <happy-melon@users.mediawiki.org>
Fri, 18 Mar 2011 19:15:56 +0000 (19:15 +0000)
includes/Article.php
includes/Block.php
includes/OutputPage.php
includes/Title.php
includes/api/ApiQueryBlocks.php
includes/api/ApiQueryInfo.php
includes/api/ApiQueryProtectedTitles.php
includes/db/Database.php
includes/specials/SpecialProtectedpages.php
includes/specials/SpecialProtectedtitles.php
languages/Language.php

index 254403c..ad75953 100644 (file)
@@ -2617,7 +2617,7 @@ class Article {
                                        if ( !isset( $expiry[$action] ) )
                                                $expiry[$action] = Block::infinity();
 
-                                       $encodedExpiry[$action] = Block::encodeExpiry( $expiry[$action], $dbw );
+                                       $encodedExpiry[$action] = $dbw->encodeExpiry( $expiry[$action] );
                                        if ( $restrictions != '' ) {
                                                $protect_description .= "[$action=$restrictions] (";
                                                if ( $encodedExpiry[$action] != 'infinity' ) {
index 422c12b..8314c6f 100644 (file)
@@ -45,7 +45,7 @@ class Block {
                $this->mAuto = $auto;
                $this->mAnonOnly = $anonOnly;
                $this->mCreateAccount = $createAccount;
-               $this->mExpiry = self::decodeExpiry( $expiry );
+               $this->mExpiry = $expiry;
                $this->mEnableAutoblock = $enableAutoblock;
                $this->mHideName = $hideName;
                $this->mBlockEmail = $blockEmail;
@@ -342,7 +342,7 @@ class Block {
                $this->mAllowUsertalk = $row->ipb_allow_usertalk;
                $this->mHideName = $row->ipb_deleted;
                $this->mId = $row->ipb_id;
-               $this->mExpiry = self::decodeExpiry( $row->ipb_expiry );
+               $this->mExpiry = $row->ipb_expiry;
 
                if ( isset( $row->user_name ) ) {
                        $this->mByName = $row->user_name;
@@ -420,7 +420,7 @@ class Block {
                                'ipb_anon_only' => $this->mAnonOnly,
                                'ipb_create_account' => $this->mCreateAccount,
                                'ipb_enable_autoblock' => $this->mEnableAutoblock,
-                               'ipb_expiry' => self::encodeExpiry( $this->mExpiry, $dbw ),
+                               'ipb_expiry' => $dbw->encodeExpiry( $this->mExpiry ),
                                'ipb_range_start' => $this->mRangeStart,
                                'ipb_range_end' => $this->mRangeEnd,
                                'ipb_deleted'   => intval( $this->mHideName ), // typecast required for SQLite
@@ -460,7 +460,7 @@ class Block {
                                'ipb_anon_only' => $this->mAnonOnly,
                                'ipb_create_account' => $this->mCreateAccount,
                                'ipb_enable_autoblock' => $this->mEnableAutoblock,
-                               'ipb_expiry' => self::encodeExpiry( $this->mExpiry, $dbw ),
+                               'ipb_expiry' => $dbw->encodeExpiry( $this->mExpiry ),
                                'ipb_range_start' => $this->mRangeStart,
                                'ipb_range_end' => $this->mRangeEnd,
                                'ipb_deleted'   => $this->mHideName,
@@ -778,6 +778,7 @@ class Block {
         * @param $expiry String: timestamp for expiry, or
         * @param $db Database object
         * @return String
+        * @deprecated since 1.18; use $dbw->encodeExpiry() instead
         */
        public static function encodeExpiry( $expiry, $db ) {
                if ( $expiry == '' || $expiry == Block::infinity() ) {
@@ -793,13 +794,11 @@ class Block {
         * @param $expiry String: Database expiry format
         * @param $timestampType Requested timestamp format
         * @return String
+        * @deprecated since 1.18; use $wgLang->decodeExpiry() instead
         */
        public static function decodeExpiry( $expiry, $timestampType = TS_MW ) {
-               if ( $expiry == '' || $expiry == Block::infinity() ) {
-                       return Block::infinity();
-               } else {
-                       return wfTimestamp( $timestampType, $expiry );
-               }
+               global $wgContLang;
+               return $wgContLang->formatExpiry( $expiry, $timestampType );
        }
 
        /**
@@ -872,8 +871,10 @@ class Block {
         *
         * @param $encoded_expiry String: Database encoded expiry time
         * @return Html-escaped String
+        * @deprecated since 1.18; use $wgLang->formatExpiry() instead
         */
        public static function formatExpiry( $encoded_expiry ) {
+               global $wgContLang;
                static $msg = null;
 
                if ( is_null( $msg ) ) {
@@ -885,7 +886,7 @@ class Block {
                        }
                }
 
-               $expiry = self::decodeExpiry( $encoded_expiry );
+               $expiry = $wgContLang->formatExpiry( $encoded_expiry, TS_MW );
                if ( $expiry == self::infinity() ) {
                        $expirystr = $msg['infiniteblock'];
                } else {
index b4059fe..4471d77 100644 (file)
@@ -1910,20 +1910,8 @@ class OutputPage {
                $blockid = $wgUser->mBlock->mId;
 
                $blockExpiry = $wgUser->mBlock->mExpiry;
-               if ( $blockExpiry == 'infinity' ) {
-                       // Entry in database (table ipblocks) is 'infinity' but 'ipboptions' uses 'infinite' or 'indefinite'
-                       // Search for localization in 'ipboptions'
-                       $scBlockExpiryOptions = wfMsg( 'ipboptions' );
-                       foreach ( explode( ',', $scBlockExpiryOptions ) as $option ) {
-                               if ( strpos( $option, ':' ) === false ) {
-                                       continue;
-                               }
-                               list( $show, $value ) = explode( ':', $option );
-                               if ( $value == 'infinite' || $value == 'indefinite' ) {
-                                       $blockExpiry = $show;
-                                       break;
-                               }
-                       }
+               if ( $blockExpiry == Block::infinity() ) {
+                       $blockExpiry = wfMessage( 'infiniteblock' );
                } else {
                        $blockExpiry = $wgLang->timeanddate(
                                wfTimestamp( TS_MW, $blockExpiry ),
index 9141474..d878642 100644 (file)
@@ -1566,20 +1566,7 @@ class Title {
                        $blockExpiry = $user->mBlock->mExpiry;
                        $blockTimestamp = $wgLang->timeanddate( wfTimestamp( TS_MW, $user->mBlock->mTimestamp ), true );
                        if ( $blockExpiry == 'infinity' ) {
-                               // Entry in database (table ipblocks) is 'infinity' but 'ipboptions' uses 'infinite' or 'indefinite'
-                               $scBlockExpiryOptions = wfMsg( 'ipboptions' );
-
-                               foreach ( explode( ',', $scBlockExpiryOptions ) as $option ) {
-                                       if ( !strpos( $option, ':' ) )
-                                               continue;
-
-                                       list( $show, $value ) = explode( ':', $option );
-
-                                       if ( $value == 'infinite' || $value == 'indefinite' ) {
-                                               $blockExpiry = $show;
-                                               break;
-                                       }
-                               }
+                               $blockExpiry = wfMessage( 'infiniteblock' );
                        } else {
                                $blockExpiry = $wgLang->timeanddate( wfTimestamp( TS_MW, $blockExpiry ), true );
                        }
@@ -1680,10 +1667,10 @@ class Title {
 
                $dbw = wfGetDB( DB_MASTER );
 
-               $encodedExpiry = Block::encodeExpiry( $expiry, $dbw );
+               $encodedExpiry = $dbw->encodeExpiry( $expiry );
 
                $expiry_description = '';
-               if ( $encodedExpiry != 'infinity' ) {
+               if ( $encodedExpiry != $dbw->getInfinity() ) {
                        $expiry_description = ' (' . wfMsgForContent( 'protect-expiring', $wgContLang->timeanddate( $expiry ),
                                $wgContLang->date( $expiry ) , $wgContLang->time( $expiry ) ) . ')';
                } else {
@@ -1696,7 +1683,7 @@ class Title {
                                        'pt_namespace' => $namespace,
                                        'pt_title' => $title,
                                        'pt_create_perm' => $create_perm,
-                                       'pt_timestamp' => Block::encodeExpiry( wfTimestampNow(), $dbw ),
+                                       'pt_timestamp' => $dbw->encodeExpiry( wfTimestampNow() ),
                                        'pt_expiry' => $encodedExpiry,
                                        'pt_user' => $wgUser->getId(),
                                        'pt_reason' => $reason,
@@ -2037,6 +2024,7 @@ class Title {
         *     contains a array of unique groups.
         */
        public function getCascadeProtectionSources( $getPages = true ) {
+               global $wgContLang;
                $pagerestrictions = array();
 
                if ( isset( $this->mCascadeSources ) && $getPages ) {
@@ -2082,7 +2070,7 @@ class Title {
                $purgeExpired = false;
 
                foreach ( $res as $row ) {
-                       $expiry = Block::decodeExpiry( $row->pr_expiry );
+                       $expiry = $wgContLang->formatExpiry( $row->pr_expiry, TS_MW );
                        if ( $expiry > $now ) {
                                if ( $getPages ) {
                                        $page_id = $row->pr_page;
@@ -2163,13 +2151,14 @@ class Title {
         *        restrictions from page table (pre 1.10)
         */
        public function loadRestrictionsFromRows( $rows, $oldFashionedRestrictions = null ) {
+               global $wgContLang;
                $dbr = wfGetDB( DB_SLAVE );
 
                $restrictionTypes = $this->getRestrictionTypes();
 
                foreach ( $restrictionTypes as $type ) {
                        $this->mRestrictions[$type] = array();
-                       $this->mRestrictionsExpiry[$type] = Block::decodeExpiry( '' );
+                       $this->mRestrictionsExpiry[$type] = $wgContLang->formatExpiry( '', TS_MW );
                }
 
                $this->mCascadeRestriction = false;
@@ -2212,7 +2201,7 @@ class Title {
 
                                // This code should be refactored, now that it's being used more generally,
                                // But I don't really see any harm in leaving it in Block for now -werdna
-                               $expiry = Block::decodeExpiry( $row->pr_expiry );
+                               $expiry = $wgContLang->formatExpiry( $row->pr_expiry, TS_MW );
 
                                // Only apply the restrictions if they haven't expired!
                                if ( !$expiry || $expiry > $now ) {
@@ -2241,12 +2230,17 @@ class Title {
         *        restrictions from page table (pre 1.10)
         */
        public function loadRestrictions( $oldFashionedRestrictions = null ) {
+               global $wgContLang;
                if ( !$this->mRestrictionsLoaded ) {
                        if ( $this->exists() ) {
                                $dbr = wfGetDB( DB_SLAVE );
 
-                               $res = $dbr->select( 'page_restrictions', '*',
-                                       array( 'pr_page' => $this->getArticleId() ), __METHOD__ );
+                               $res = $dbr->select(
+                                       'page_restrictions',
+                                       '*',
+                                       array( 'pr_page' => $this->getArticleId() ),
+                                       __METHOD__
+                               );
 
                                $this->loadRestrictionsFromResultWrapper( $res, $oldFashionedRestrictions );
                        } else {
@@ -2254,7 +2248,7 @@ class Title {
 
                                if ( $title_protection ) {
                                        $now = wfTimestampNow();
-                                       $expiry = Block::decodeExpiry( $title_protection['pt_expiry'] );
+                                       $expiry = $wgContLang->formatExpiry( $title_protection['pt_expiry'], TS_MW );
 
                                        if ( !$expiry || $expiry > $now ) {
                                                // Apply the restrictions
@@ -2265,7 +2259,7 @@ class Title {
                                                $this->mTitleProtection = false;
                                        }
                                } else {
-                                       $this->mRestrictionsExpiry['create'] = Block::decodeExpiry( '' );
+                                       $this->mRestrictionsExpiry['create'] = $wgContLang->formatExpiry( '', TS_MW );
                                }
                                $this->mRestrictionsLoaded = true;
                        }
index 584a049..9cea45f 100644 (file)
@@ -46,7 +46,7 @@ class ApiQueryBlocks extends ApiQueryBase {
        }
 
        public function execute() {
-               global $wgUser;
+               global $wgUser, $wgContLang;
 
                $params = $this->extractRequestParams();
                if ( isset( $params['users'] ) && isset( $params['ip'] ) ) {
@@ -170,7 +170,7 @@ class ApiQueryBlocks extends ApiQueryBase {
                                $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
                        }
                        if ( $fld_expiry ) {
-                               $block['expiry'] = Block::decodeExpiry( $row->ipb_expiry, TS_ISO_8601 );
+                               $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry, TS_ISO_8601 );
                        }
                        if ( $fld_reason ) {
                                $block['reason'] = $row->ipb_reason;
index d96808f..5b0d325 100644 (file)
@@ -397,6 +397,7 @@ class ApiQueryInfo extends ApiQueryBase {
         * Get information about protections and put it in $protections
         */
        private function getProtectionInfo() {
+               global $wgContLang;
                $this->protections = array();
                $db = $this->getDB();
 
@@ -415,7 +416,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                $a = array(
                                        'type' => $row->pr_type,
                                        'level' => $row->pr_level,
-                                       'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 )
+                                       'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 )
                                );
                                if ( $row->pr_cascade ) {
                                        $a['cascade'] = '';
@@ -472,7 +473,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                $this->protections[$row->pt_namespace][$row->pt_title][] = array(
                                        'type' => 'create',
                                        'level' => $row->pt_create_perm,
-                                       'expiry' => Block::decodeExpiry( $row->pt_expiry, TS_ISO_8601 )
+                                       'expiry' => $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 )
                                );
                        }
                }
@@ -506,7 +507,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                $this->protections[$row->tl_namespace][$row->tl_title][] = array(
                                        'type' => $row->pr_type,
                                        'level' => $row->pr_level,
-                                       'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
+                                       'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
                                        'source' => $source->getPrefixedText()
                                );
                        }
@@ -529,7 +530,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                $this->protections[NS_FILE][$row->il_to][] = array(
                                        'type' => $row->pr_type,
                                        'level' => $row->pr_level,
-                                       'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ),
+                                       'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
                                        'source' => $source->getPrefixedText()
                                );
                        }
index 32249a7..6c54dd6 100644 (file)
@@ -117,7 +117,8 @@ class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
                                }
 
                                if ( isset( $prop['expiry'] ) ) {
-                                       $vals['expiry'] = Block::decodeExpiry( $row->pt_expiry, TS_ISO_8601 );
+                                       global $wgContLang;
+                                       $vals['expiry'] = $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 );
                                }
 
                                if ( isset( $prop['level'] ) ) {
index d78f405..2a8c198 100644 (file)
@@ -2747,6 +2747,20 @@ abstract class DatabaseBase implements DatabaseType {
                return 'infinity';
        }
 
+       /**
+        * Encode an expiry time
+        *
+        * @param $expiry String: timestamp for expiry, or the 'infinity' string
+        * @return String
+        */
+       public function encodeExpiry( $expiry ) {
+               if ( $expiry == '' || $expiry == $this->getInfinity() ) {
+                       return $this->getInfinity();
+               } else {
+                       return $this->timestamp( $expiry );
+               }
+       }
+
        /**
         * Allow or deny "big selects" for this session only. This is done by setting
         * the sql_big_selects session variable.
index c676aa0..5d23788 100644 (file)
@@ -80,10 +80,12 @@ class SpecialProtectedpages extends SpecialPage {
 
                wfProfileIn( __METHOD__ );
 
-               static $skin=null;
+               static $skin = null, $dbr = null;
 
-               if( is_null( $skin ) )
+               if( is_null( $skin ) ){
                        $skin = $wgUser->getSkin();
+                       $dbr = wfGetDB( DB_READ );
+               }
 
                $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title );
                $link = $skin->link( $title );
@@ -100,11 +102,15 @@ class SpecialProtectedpages extends SpecialPage {
 
                $stxt = '';
 
-               if( $row->pr_expiry != 'infinity' && strlen($row->pr_expiry) ) {
-                       $expiry = Block::decodeExpiry( $row->pr_expiry );
+               $expiry = $wgLang->formatExpiry( $row->pr_expiry, TS_MW );
+               if( $expiry != $dbr->getInfinity() ) {
 
-                       $expiry_description = wfMsg( 'protect-expiring' , $wgLang->timeanddate( $expiry ) , 
-                               $wgLang->date( $expiry ) , $wgLang->time( $expiry ) );
+                       $expiry_description = wfMsg(
+                               'protect-expiring',
+                               $wgLang->timeanddate( $expiry ),
+                               $wgLang->date( $expiry ),
+                               $wgLang->time( $expiry )
+                       );
 
                        $description_items[] = htmlspecialchars($expiry_description);
                }
index 5b18d87..51528bc 100644 (file)
@@ -93,7 +93,7 @@ class SpecialProtectedtitles extends SpecialPage {
                $stxt = '';
 
                if ( $row->pt_expiry != 'infinity' && strlen($row->pt_expiry) ) {
-                       $expiry = Block::decodeExpiry( $row->pt_expiry );
+                       $expiry = $wgLang->formatExpiry( $row->pt_expiry );
 
                        $expiry_description = wfMsg( 'protect-expiring', $wgLang->timeanddate( $expiry ) , $wgLang->date( $expiry ) , $wgLang->time( $expiry ) );
 
index 6d971b5..504f882 100644 (file)
@@ -2970,6 +2970,37 @@ class Language {
                return array( $wikiUpperChars, $wikiLowerChars );
        }
 
+       /**
+        * Decode an expiry (block, protection, etc) which has come from the DB
+        *
+        * @param $expiry String: Database expiry String
+        * @param $format Bool|Int true to process using language functions, or TS_ constant
+        *     to return the expiry in a given timestamp
+        * @return String
+        */
+       public function formatExpiry( $expiry, $format = true ) {
+               static $dbr, $msg;
+               if( !$dbr ){
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $msg = wfMessage( 'infiniteblock' );
+               }
+
+               if ( $expiry == '' || $expiry == $dbr->getInfinity() ) {
+                       return $format === true
+                               ? $msg
+                               : $dbr->getInfinity();
+               } else {
+                       return $format === true
+                               ? $this->timeanddate( $expiry )
+                               : wfTimestamp( $format, $expiry );
+               }
+       }
+
+       /**
+        * @todo Document
+        * @param  $seconds String
+        * @return string
+        */
        function formatTimePeriod( $seconds ) {
                if ( round( $seconds * 10 ) < 100 ) {
                        return $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) ) . $this->getMessageFromDB( 'seconds-abbrev' );