ApiResult: Add ApiResult::formatExpiry()
authorBrad Jorsch <bjorsch@wikimedia.org>
Wed, 11 Jan 2017 19:04:41 +0000 (11:04 -0800)
committerBrad Jorsch <bjorsch@wikimedia.org>
Wed, 11 Jan 2017 19:11:19 +0000 (11:11 -0800)
This allows for removing $wgContLang from many API modules where it was
only used to call $wgContLang->formatExpiry() in a way in which the
results don't actually depend on the language.

Change-Id: Ib0f25f288b9b87d2e4131297c552e5971696db87

includes/api/ApiBlock.php
includes/api/ApiProtect.php
includes/api/ApiQueryBlocks.php
includes/api/ApiQueryInfo.php
includes/api/ApiQueryProtectedTitles.php
includes/api/ApiQueryUserInfo.php
includes/api/ApiResult.php

index 3774f09..c3aab88 100644 (file)
@@ -39,8 +39,6 @@ class ApiBlock extends ApiBase {
         * of success. If it fails, the result will specify the nature of the error.
         */
        public function execute() {
-               global $wgContLang;
-
                $this->checkUserRightsAny( 'block' );
 
                $user = $this->getUser();
@@ -118,7 +116,7 @@ class ApiBlock extends ApiBase {
 
                $block = Block::newFromTarget( $target, null, true );
                if ( $block instanceof Block ) {
-                       $res['expiry'] = $wgContLang->formatExpiry( $block->mExpiry, TS_ISO_8601, 'infinite' );
+                       $res['expiry'] = ApiResult::formatExpiry( $block->mExpiry, 'infinite' );
                        $res['id'] = $block->getId();
                } else {
                        # should be unreachable
index 746dc9a..c74f890 100644 (file)
@@ -29,8 +29,6 @@
  */
 class ApiProtect extends ApiBase {
        public function execute() {
-               global $wgContLang;
-
                $params = $this->extractRequestParams();
 
                $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
@@ -101,7 +99,7 @@ class ApiProtect extends ApiBase {
                        }
                        $resultProtections[] = [
                                $p[0] => $protections[$p[0]],
-                               'expiry' => $wgContLang->formatExpiry( $expiryarray[$p[0]], TS_ISO_8601, 'infinite' ),
+                               'expiry' => ApiResult::formatExpiry( $expiryarray[$p[0]], 'infinite' ),
                        ];
                }
 
index ef79efd..0040860 100644 (file)
@@ -36,8 +36,6 @@ class ApiQueryBlocks extends ApiQueryBase {
        }
 
        public function execute() {
-               global $wgContLang;
-
                $db = $this->getDB();
                $params = $this->extractRequestParams();
                $this->requireMaxOneParameter( $params, 'users', 'ip' );
@@ -204,7 +202,7 @@ class ApiQueryBlocks extends ApiQueryBase {
                                $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
                        }
                        if ( $fld_expiry ) {
-                               $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry, TS_ISO_8601 );
+                               $block['expiry'] = ApiResult::formatExpiry( $row->ipb_expiry );
                        }
                        if ( $fld_reason ) {
                                $block['reason'] = $row->ipb_reason;
index fd65038..e789dd4 100644 (file)
@@ -535,7 +535,6 @@ class ApiQueryInfo extends ApiQueryBase {
         * Get information about protections and put it in $protections
         */
        private function getProtectionInfo() {
-               global $wgContLang;
                $this->protections = [];
                $db = $this->getDB();
 
@@ -554,7 +553,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                $a = [
                                        'type' => $row->pr_type,
                                        'level' => $row->pr_level,
-                                       'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 )
+                                       'expiry' => ApiResult::formatExpiry( $row->pr_expiry )
                                ];
                                if ( $row->pr_cascade ) {
                                        $a['cascade'] = true;
@@ -614,7 +613,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                $this->protections[$row->pt_namespace][$row->pt_title][] = [
                                        'type' => 'create',
                                        'level' => $row->pt_create_perm,
-                                       'expiry' => $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 )
+                                       'expiry' => ApiResult::formatExpiry( $row->pt_expiry )
                                ];
                        }
                }
@@ -652,7 +651,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                $this->protections[$row->tl_namespace][$row->tl_title][] = [
                                        'type' => $row->pr_type,
                                        'level' => $row->pr_level,
-                                       'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
+                                       'expiry' => ApiResult::formatExpiry( $row->pr_expiry ),
                                        'source' => $source->getPrefixedText()
                                ];
                        }
@@ -675,7 +674,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                $this->protections[NS_FILE][$row->il_to][] = [
                                        'type' => $row->pr_type,
                                        'level' => $row->pr_level,
-                                       'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ),
+                                       'expiry' => ApiResult::formatExpiry( $row->pr_expiry ),
                                        'source' => $source->getPrefixedText()
                                ];
                        }
index e3c949b..62b2e42 100644 (file)
@@ -135,8 +135,7 @@ class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
                                }
 
                                if ( isset( $prop['expiry'] ) ) {
-                                       global $wgContLang;
-                                       $vals['expiry'] = $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 );
+                                       $vals['expiry'] = ApiResult::formatExpiry( $row->pt_expiry );
                                }
 
                                if ( isset( $prop['level'] ) ) {
index 60e122c..7bc00cb 100644 (file)
@@ -67,16 +67,13 @@ class ApiQueryUserInfo extends ApiQueryBase {
         *  - systemblocktype - system block type, if any
         */
        public static function getBlockInfo( Block $block ) {
-               global $wgContLang;
                $vals = [];
                $vals['blockid'] = $block->getId();
                $vals['blockedby'] = $block->getByName();
                $vals['blockedbyid'] = $block->getBy();
                $vals['blockreason'] = $block->mReason;
                $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $block->mTimestamp );
-               $vals['blockexpiry'] = $wgContLang->formatExpiry(
-                       $block->getExpiry(), TS_ISO_8601, 'infinite'
-               );
+               $vals['blockexpiry'] = ApiResult::formatExpiry( $block->getExpiry(), 'infinite' );
                if ( $block->getSystemBlockType() !== null ) {
                        $vals['systemblocktype'] = $block->getSystemBlockType();
                }
index 61a4394..e27cf7d 100644 (file)
@@ -1196,6 +1196,29 @@ class ApiResult implements ApiSerializable {
                }
        }
 
+       /**
+        * Format an expiry timestamp for API output
+        * @since 1.29
+        * @param string $expiry Expiry timestamp, likely from the database
+        * @param string $infinity Use this string for infinite expiry
+        *  (only use this to maintain backward compatibility with existing output)
+        * @return string Formatted expiry
+        */
+       public static function formatExpiry( $expiry, $infinity = 'infinity' ) {
+               static $dbInfinity;
+               if ( $dbInfinity === null ) {
+                       $dbInfinity = wfGetDB( DB_REPLICA )->getInfinity();
+               }
+
+               if ( $expiry === '' || $expiry === null || $expiry === false ||
+                       wfIsInfinity( $expiry ) || $expiry === $dbInfinity
+               ) {
+                       return $infinity;
+               } else {
+                       return wfTimestamp( TS_ISO_8601, $expiry );
+               }
+       }
+
        /**@}*/
 
 }