From: Brad Jorsch Date: Wed, 11 Jan 2017 19:04:41 +0000 (-0800) Subject: ApiResult: Add ApiResult::formatExpiry() X-Git-Tag: 1.31.0-rc.0~4345^2 X-Git-Url: http://git.cyclocoop.org/%24action?a=commitdiff_plain;h=a0664196282614ddd033737edd39b4bd9d2e056f;p=lhc%2Fweb%2Fwiklou.git ApiResult: Add ApiResult::formatExpiry() 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 --- diff --git a/includes/api/ApiBlock.php b/includes/api/ApiBlock.php index 3774f099a7..c3aab88190 100644 --- a/includes/api/ApiBlock.php +++ b/includes/api/ApiBlock.php @@ -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 diff --git a/includes/api/ApiProtect.php b/includes/api/ApiProtect.php index 746dc9a16b..c74f890a57 100644 --- a/includes/api/ApiProtect.php +++ b/includes/api/ApiProtect.php @@ -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' ), ]; } diff --git a/includes/api/ApiQueryBlocks.php b/includes/api/ApiQueryBlocks.php index ef79efd358..004086059c 100644 --- a/includes/api/ApiQueryBlocks.php +++ b/includes/api/ApiQueryBlocks.php @@ -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; diff --git a/includes/api/ApiQueryInfo.php b/includes/api/ApiQueryInfo.php index fd6503801f..e789dd4fb5 100644 --- a/includes/api/ApiQueryInfo.php +++ b/includes/api/ApiQueryInfo.php @@ -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() ]; } diff --git a/includes/api/ApiQueryProtectedTitles.php b/includes/api/ApiQueryProtectedTitles.php index e3c949bd3b..62b2e42e03 100644 --- a/includes/api/ApiQueryProtectedTitles.php +++ b/includes/api/ApiQueryProtectedTitles.php @@ -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'] ) ) { diff --git a/includes/api/ApiQueryUserInfo.php b/includes/api/ApiQueryUserInfo.php index 60e122cdd1..7bc00cb158 100644 --- a/includes/api/ApiQueryUserInfo.php +++ b/includes/api/ApiQueryUserInfo.php @@ -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(); } diff --git a/includes/api/ApiResult.php b/includes/api/ApiResult.php index 61a4394e74..e27cf7dd64 100644 --- a/includes/api/ApiResult.php +++ b/includes/api/ApiResult.php @@ -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 ); + } + } + /**@}*/ }