From 9cb6b2cfcc59afc16f071982ffb2477823577a59 Mon Sep 17 00:00:00 2001 From: James Rhodes Date: Sun, 13 Jul 2014 12:03:17 +1000 Subject: [PATCH] Remove user-independent date and time functions from Phabricator Summary: These have been moved into libphutil. Test Plan: Browsed Phabricator, didn't see a crash. Reviewers: #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: epriestley, Korvin Differential Revision: https://secure.phabricator.com/D9907 --- src/__phutil_library_map__.php | 6 - .../ConduitAPI_conduit_connect_Method.php | 2 +- .../PhabricatorDaemonLogViewController.php | 14 +- .../PhabricatorWorkerTaskDetailController.php | 4 +- .../PhabricatorFileInfoController.php | 2 +- .../PhabricatorFileUploadController.php | 4 +- .../query/PhabricatorFileSearchEngine.php | 2 +- .../files/storage/PhabricatorFile.php | 2 +- ...habricatorNotificationStatusController.php | 2 +- .../customfield/PhabricatorUserSinceField.php | 2 +- .../event/PhrequentUIEventListener.php | 2 +- .../phrequent/query/PhrequentSearchEngine.php | 2 +- .../request/ReleephRequestEditController.php | 2 +- .../ReleephRequestTypeaheadController.php | 2 +- ...habricatorRepositoryCommitHeraldWorker.php | 4 +- .../PhabricatorSettingsPanelSessions.php | 2 +- .../__tests__/PhabricatorUnitsTestCase.php | 22 +-- src/view/page/PhabricatorStandardPageView.php | 2 +- src/view/viewutils.php | 172 +----------------- 19 files changed, 38 insertions(+), 212 deletions(-) diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 9811c0d1ef..b3385d487b 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -2709,7 +2709,6 @@ phutil_register_library_map(array( ), 'function' => array( - '_phabricator_date_format' => 'view/viewutils.php', '_phabricator_time_format' => 'view/viewutils.php', 'celerity_generate_unique_node_id' => 'infrastructure/celerity/api.php', 'celerity_get_resource_uri' => 'infrastructure/celerity/api.php', @@ -2718,13 +2717,8 @@ phutil_register_library_map(array( 'phabricator_date' => 'view/viewutils.php', 'phabricator_datetime' => 'view/viewutils.php', 'phabricator_form' => 'infrastructure/javelin/markup.php', - 'phabricator_format_bytes' => 'view/viewutils.php', 'phabricator_format_local_time' => 'view/viewutils.php', - 'phabricator_format_relative_time' => 'view/viewutils.php', - 'phabricator_format_relative_time_detailed' => 'view/viewutils.php', - 'phabricator_format_units_generic' => 'view/viewutils.php', 'phabricator_on_relative_date' => 'view/viewutils.php', - 'phabricator_parse_bytes' => 'view/viewutils.php', 'phabricator_relative_date' => 'view/viewutils.php', 'phabricator_time' => 'view/viewutils.php', 'phid_get_subtype' => 'applications/phid/utils.php', diff --git a/src/applications/conduit/method/ConduitAPI_conduit_connect_Method.php b/src/applications/conduit/method/ConduitAPI_conduit_connect_Method.php index db341da764..4d9b705b0c 100644 --- a/src/applications/conduit/method/ConduitAPI_conduit_connect_Method.php +++ b/src/applications/conduit/method/ConduitAPI_conduit_connect_Method.php @@ -125,7 +125,7 @@ final class ConduitAPI_conduit_connect_Method extends ConduitAPIMethod { 'timestamp must differ from the server time by no more than '. '%s seconds. Your client or server clock may not be set '. 'correctly.', - phabricator_format_relative_time($threshold), + phutil_format_relative_time($threshold), $token, date('r', $token), $now, diff --git a/src/applications/daemon/controller/PhabricatorDaemonLogViewController.php b/src/applications/daemon/controller/PhabricatorDaemonLogViewController.php index c70cf83dfc..01216d5ffb 100644 --- a/src/applications/daemon/controller/PhabricatorDaemonLogViewController.php +++ b/src/applications/daemon/controller/PhabricatorDaemonLogViewController.php @@ -109,14 +109,14 @@ final class PhabricatorDaemonLogViewController $details = pht( 'This daemon is running normally and reported a status update '. 'recently (within %s).', - phabricator_format_relative_time($unknown_time)); + phutil_format_relative_time($unknown_time)); break; case PhabricatorDaemonLog::STATUS_UNKNOWN: $details = pht( 'This daemon has not reported a status update recently (within %s). '. 'It may have exited abruptly. After %s, it will be presumed dead.', - phabricator_format_relative_time($unknown_time), - phabricator_format_relative_time($dead_time)); + phutil_format_relative_time($unknown_time), + phutil_format_relative_time($dead_time)); break; case PhabricatorDaemonLog::STATUS_DEAD: $details = pht( @@ -124,7 +124,7 @@ final class PhabricatorDaemonLogViewController 'presumed dead. Usually, this indicates that the daemon was '. 'killed or otherwise exited abruptly with an error. You may '. 'need to restart it.', - phabricator_format_relative_time($dead_time)); + phutil_format_relative_time($dead_time)); break; case PhabricatorDaemonLog::STATUS_WAIT: $details = pht( @@ -133,8 +133,8 @@ final class PhabricatorDaemonLogViewController 'doing work and is waiting a little while (%s) to resume '. 'processing. After encountering an error, daemons wait before '. 'resuming work to avoid overloading services.', - phabricator_format_relative_time($unknown_time), - phabricator_format_relative_time($wait_time)); + phutil_format_relative_time($unknown_time), + phutil_format_relative_time($wait_time)); break; case PhabricatorDaemonLog::STATUS_EXITED: $details = pht( @@ -152,7 +152,7 @@ final class PhabricatorDaemonLogViewController pht('Seen'), pht( '%s ago (%s)', - phabricator_format_relative_time(time() - $u_epoch), + phutil_format_relative_time(time() - $u_epoch), phabricator_datetime($u_epoch, $viewer))); $argv = $daemon->getArgv(); diff --git a/src/applications/daemon/controller/PhabricatorWorkerTaskDetailController.php b/src/applications/daemon/controller/PhabricatorWorkerTaskDetailController.php index f0bdc813e0..fafd112247 100644 --- a/src/applications/daemon/controller/PhabricatorWorkerTaskDetailController.php +++ b/src/applications/daemon/controller/PhabricatorWorkerTaskDetailController.php @@ -174,7 +174,7 @@ final class PhabricatorWorkerTaskDetailController if ($task->getLeaseExpires() && $task->getLeaseOwner()) { $expires = ($task->getLeaseExpires() - time()); - $expires = phabricator_format_relative_time_detailed($expires); + $expires = phutil_format_relative_time_detailed($expires); } else { $expires = phutil_tag('em', array(), pht('None')); } @@ -247,7 +247,7 @@ final class PhabricatorWorkerTaskDetailController $duration = 60; } $cumulative += $duration; - $next[$key] = phabricator_format_relative_time($cumulative); + $next[$key] = phutil_format_relative_time($cumulative); } if ($ii != $retry_count) { $next[] = '...'; diff --git a/src/applications/files/controller/PhabricatorFileInfoController.php b/src/applications/files/controller/PhabricatorFileInfoController.php index 98268a515d..5c03d05b4d 100644 --- a/src/applications/files/controller/PhabricatorFileInfoController.php +++ b/src/applications/files/controller/PhabricatorFileInfoController.php @@ -179,7 +179,7 @@ final class PhabricatorFileInfoController extends PhabricatorFileController { $finfo->addProperty( pht('Size'), - phabricator_format_bytes($file->getByteSize())); + phutil_format_bytes($file->getByteSize())); $finfo->addProperty( pht('Mime Type'), diff --git a/src/applications/files/controller/PhabricatorFileUploadController.php b/src/applications/files/controller/PhabricatorFileUploadController.php index 9ca0a702f1..d7a6247a51 100644 --- a/src/applications/files/controller/PhabricatorFileUploadController.php +++ b/src/applications/files/controller/PhabricatorFileUploadController.php @@ -86,9 +86,9 @@ final class PhabricatorFileUploadController extends PhabricatorFileController { private function renderUploadLimit() { $limit = PhabricatorEnv::getEnvConfig('storage.upload-size-limit'); - $limit = phabricator_parse_bytes($limit); + $limit = phutil_parse_bytes($limit); if ($limit) { - $formatted = phabricator_format_bytes($limit); + $formatted = phutil_format_bytes($limit); return 'Maximum file size: '.$formatted; } diff --git a/src/applications/files/query/PhabricatorFileSearchEngine.php b/src/applications/files/query/PhabricatorFileSearchEngine.php index 51d2188a21..afde191e62 100644 --- a/src/applications/files/query/PhabricatorFileSearchEngine.php +++ b/src/applications/files/query/PhabricatorFileSearchEngine.php @@ -166,7 +166,7 @@ final class PhabricatorFileSearchEngine ->setHeader($name) ->setHref($file_uri) ->addAttribute($uploaded) - ->addIcon('none', phabricator_format_bytes($file->getByteSize())); + ->addIcon('none', phutil_format_bytes($file->getByteSize())); $ttl = $file->getTTL(); if ($ttl !== null) { diff --git a/src/applications/files/storage/PhabricatorFile.php b/src/applications/files/storage/PhabricatorFile.php index edf288935e..8ae6424a66 100644 --- a/src/applications/files/storage/PhabricatorFile.php +++ b/src/applications/files/storage/PhabricatorFile.php @@ -108,7 +108,7 @@ final class PhabricatorFile extends PhabricatorFileDAO return; } - $limit = phabricator_parse_bytes($limit); + $limit = phutil_parse_bytes($limit); if ($size > $limit) { throw new PhabricatorFileUploadException(-1000); } diff --git a/src/applications/notification/controller/PhabricatorNotificationStatusController.php b/src/applications/notification/controller/PhabricatorNotificationStatusController.php index 3e6c55f605..d3585876b6 100644 --- a/src/applications/notification/controller/PhabricatorNotificationStatusController.php +++ b/src/applications/notification/controller/PhabricatorNotificationStatusController.php @@ -41,7 +41,7 @@ final class PhabricatorNotificationStatusController switch ($key) { case 'uptime': $value /= 1000; - $value = phabricator_format_relative_time_detailed($value); + $value = phutil_format_relative_time_detailed($value); break; case 'log': break; diff --git a/src/applications/people/customfield/PhabricatorUserSinceField.php b/src/applications/people/customfield/PhabricatorUserSinceField.php index eff207b81b..7b15359ca6 100644 --- a/src/applications/people/customfield/PhabricatorUserSinceField.php +++ b/src/applications/people/customfield/PhabricatorUserSinceField.php @@ -26,7 +26,7 @@ final class PhabricatorUserSinceField $this->getObject()->getDateCreated(), $this->getViewer()); - $relative = phabricator_format_relative_time_detailed( + $relative = phutil_format_relative_time_detailed( time() - $this->getObject()->getDateCreated(), $levels = 2); diff --git a/src/applications/phrequent/event/PhrequentUIEventListener.php b/src/applications/phrequent/event/PhrequentUIEventListener.php index 4778686b9c..9b0bdace6b 100644 --- a/src/applications/phrequent/event/PhrequentUIEventListener.php +++ b/src/applications/phrequent/event/PhrequentUIEventListener.php @@ -138,7 +138,7 @@ final class PhrequentUIEventListener $block = new PhrequentTimeBlock($event_group); $item->setNote( - phabricator_format_relative_time( + phutil_format_relative_time( $block->getTimeSpentOnObject( $object->getPHID(), time()))); diff --git a/src/applications/phrequent/query/PhrequentSearchEngine.php b/src/applications/phrequent/query/PhrequentSearchEngine.php index ddd5108468..1a90463bd2 100644 --- a/src/applications/phrequent/query/PhrequentSearchEngine.php +++ b/src/applications/phrequent/query/PhrequentSearchEngine.php @@ -166,7 +166,7 @@ final class PhrequentSearchEngine } $time_spent = $time_spent == 0 ? 'none' : - phabricator_format_relative_time_detailed($time_spent); + phutil_format_relative_time_detailed($time_spent); if ($usertime->getDateEnded() !== null) { $item->addAttribute( diff --git a/src/applications/releeph/controller/request/ReleephRequestEditController.php b/src/applications/releeph/controller/request/ReleephRequestEditController.php index 9221170ce0..d18894d70c 100644 --- a/src/applications/releeph/controller/request/ReleephRequestEditController.php +++ b/src/applications/releeph/controller/request/ReleephRequestEditController.php @@ -195,7 +195,7 @@ final class ReleephRequestEditController extends ReleephBranchController { $age_string = ''; if ($is_edit) { - $age_string = phabricator_format_relative_time( + $age_string = phutil_format_relative_time( time() - $pull->getDateCreated()).' ago'; } diff --git a/src/applications/releeph/controller/request/ReleephRequestTypeaheadController.php b/src/applications/releeph/controller/request/ReleephRequestTypeaheadController.php index c86060c4b0..ca49a52858 100644 --- a/src/applications/releeph/controller/request/ReleephRequestTypeaheadController.php +++ b/src/applications/releeph/controller/request/ReleephRequestTypeaheadController.php @@ -56,7 +56,7 @@ final class ReleephRequestTypeaheadController $full_commit_id, $short_commit_id, $row['authorName'], - phabricator_format_relative_time($now - $row['epoch']), + phutil_format_relative_time($now - $row['epoch']), $first_line, ); } diff --git a/src/applications/repository/worker/PhabricatorRepositoryCommitHeraldWorker.php b/src/applications/repository/worker/PhabricatorRepositoryCommitHeraldWorker.php index e86ca99550..355ec5731e 100644 --- a/src/applications/repository/worker/PhabricatorRepositoryCommitHeraldWorker.php +++ b/src/applications/repository/worker/PhabricatorRepositoryCommitHeraldWorker.php @@ -469,8 +469,8 @@ final class PhabricatorRepositoryCommitHeraldWorker $size = strlen($raw_diff); if ($byte_limit && $size > $byte_limit) { - $pretty_size = phabricator_format_bytes($size); - $pretty_limit = phabricator_format_bytes($byte_limit); + $pretty_size = phutil_format_bytes($size); + $pretty_limit = phutil_format_bytes($byte_limit); throw new Exception( "Patch size of {$pretty_size} exceeds configured byte size limit of ". "{$pretty_limit}."); diff --git a/src/applications/settings/panel/PhabricatorSettingsPanelSessions.php b/src/applications/settings/panel/PhabricatorSettingsPanelSessions.php index 4103f6c9ab..2d3780107f 100644 --- a/src/applications/settings/panel/PhabricatorSettingsPanelSessions.php +++ b/src/applications/settings/panel/PhabricatorSettingsPanelSessions.php @@ -78,7 +78,7 @@ final class PhabricatorSettingsPanelSessions substr($session->getSessionKey(), 0, 6), $session->getType(), ($hisec > 0) - ? phabricator_format_relative_time($hisec) + ? phutil_format_relative_time($hisec) : null, phabricator_datetime($session->getSessionStart(), $viewer), phabricator_date($session->getSessionExpires(), $viewer), diff --git a/src/view/__tests__/PhabricatorUnitsTestCase.php b/src/view/__tests__/PhabricatorUnitsTestCase.php index 02e01305a2..4671532a3b 100644 --- a/src/view/__tests__/PhabricatorUnitsTestCase.php +++ b/src/view/__tests__/PhabricatorUnitsTestCase.php @@ -19,8 +19,8 @@ final class PhabricatorUnitsTestCase extends PhabricatorTestCase { foreach ($tests as $input => $expect) { $this->assertEqual( $expect, - phabricator_format_bytes($input), - 'phabricator_format_bytes('.$input.')'); + phutil_format_bytes($input), + 'phutil_format_bytes('.$input.')'); } } @@ -42,14 +42,14 @@ final class PhabricatorUnitsTestCase extends PhabricatorTestCase { foreach ($tests as $input => $expect) { $this->assertEqual( $expect, - phabricator_parse_bytes($input), - 'phabricator_parse_bytes('.$input.')'); + phutil_parse_bytes($input), + 'phutil_parse_bytes('.$input.')'); } $this->tryTestCases( array('string' => 'string'), array(false), - 'phabricator_parse_bytes'); + 'phutil_parse_bytes'); } public function testDetailedDurationFormatting() { @@ -68,8 +68,8 @@ final class PhabricatorUnitsTestCase extends PhabricatorTestCase { foreach ($tests as $duration => $expect) { $this->assertEqual( $expect, - phabricator_format_relative_time_detailed($duration), - 'phabricator_format_relative_time_detailed('.$duration.')'); + phutil_format_relative_time_detailed($duration), + 'phutil_format_relative_time_detailed('.$duration.')'); } @@ -113,16 +113,16 @@ final class PhabricatorUnitsTestCase extends PhabricatorTestCase { foreach ($sub_tests as $levels => $expect) { $this->assertEqual( $expect, - phabricator_format_relative_time_detailed($duration, $levels), - 'phabricator_format_relative_time_detailed('.$duration.', + phutil_format_relative_time_detailed($duration, $levels), + 'phutil_format_relative_time_detailed('.$duration.', '.$levels.')'); } } else { $expect = $sub_tests; $this->assertEqual( $expect, - phabricator_format_relative_time_detailed($duration), - 'phabricator_format_relative_time_detailed('.$duration.')'); + phutil_format_relative_time_detailed($duration), + 'phutil_format_relative_time_detailed('.$duration.')'); } } diff --git a/src/view/page/PhabricatorStandardPageView.php b/src/view/page/PhabricatorStandardPageView.php index 4c8817fa59..82d30875ad 100644 --- a/src/view/page/PhabricatorStandardPageView.php +++ b/src/view/page/PhabricatorStandardPageView.php @@ -171,7 +171,7 @@ final class PhabricatorStandardPageView extends PhabricatorBarePageView { if ($user->hasSession()) { $hisec = ($user->getSession()->getHighSecurityUntil() - time()); if ($hisec > 0) { - $remaining_time = phabricator_format_relative_time($hisec); + $remaining_time = phutil_format_relative_time($hisec); Javelin::initBehavior( 'high-security-warning', array( diff --git a/src/view/viewutils.php b/src/view/viewutils.php index b7fc259dfe..b29e453835 100644 --- a/src/view/viewutils.php +++ b/src/view/viewutils.php @@ -4,7 +4,7 @@ function phabricator_date($epoch, PhabricatorUser $user) { return phabricator_format_local_time( $epoch, $user, - _phabricator_date_format($epoch)); + _phutil_date_format($epoch)); } function phabricator_on_relative_date($epoch, $user) { @@ -46,21 +46,10 @@ function phabricator_datetime($epoch, $user) { $epoch, $user, pht('%s, %s', - _phabricator_date_format($epoch), + _phutil_date_format($epoch), _phabricator_time_format($user))); } -function _phabricator_date_format($epoch) { - $now = time(); - $shift = 30 * 24 * 60 * 60; - if ($epoch < $now + $shift && $epoch > $now - $shift) { - $format = pht('D, M j'); - } else { - $format = pht('M j Y'); - } - return $format; -} - function _phabricator_time_format($user) { $prefs = $user->loadPreferences(); @@ -119,161 +108,4 @@ function phabricator_format_local_time($epoch, $user, $format) { return PhutilTranslator::getInstance()->translateDate($format, $date); } -function phabricator_format_relative_time($duration) { - return phabricator_format_units_generic( - $duration, - array(60, 60, 24, 7), - array('s', 'm', 'h', 'd', 'w'), - $precision = 0); -} -/** - * Format a relative time (duration) into weeks, days, hours, minutes, - * seconds, but unlike phabricator_format_relative_time, does so for more than - * just the largest unit. - * - * @param int Duration in seconds. - * @param int Levels to render - will render the three highest levels, ie: - * 5 h, 37 m, 1 s - * @return string Human-readable description. - */ -function phabricator_format_relative_time_detailed($duration, $levels = 2) { - if ($duration == 0) { - return 'now'; - } - $levels = max(1, min($levels, 5)); - $remainder = 0; - - $is_negative = false; - if ($duration < 0) { - $is_negative = true; - $duration = abs($duration); - } - - $this_level = 1; - $detailed_relative_time = phabricator_format_units_generic( - $duration, - array(60, 60, 24, 7), - array('s', 'm', 'h', 'd', 'w'), - $precision = 0, - $remainder); - $duration = $remainder; - - while ($remainder > 0 && $this_level < $levels) { - $detailed_relative_time .= ', '.phabricator_format_units_generic( - $duration, - array(60, 60, 24, 7), - array('s', 'm', 'h', 'd', 'w'), - $precision = 0, - $remainder); - $duration = $remainder; - $this_level++; - }; - - if ($is_negative) { - $detailed_relative_time .= ' ago'; - } - - return $detailed_relative_time; -} - -/** - * Format a byte count for human consumption, e.g. "10MB" instead of - * "10000000". - * - * @param int Number of bytes. - * @return string Human-readable description. - */ -function phabricator_format_bytes($bytes) { - return phabricator_format_units_generic( - $bytes, - // NOTE: Using the SI version of these units rather than the 1024 version. - array(1000, 1000, 1000, 1000, 1000), - array('B', 'KB', 'MB', 'GB', 'TB', 'PB'), - $precision = 0); -} - - -/** - * Parse a human-readable byte description (like "6MB") into an integer. - * - * @param string Human-readable description. - * @return int Number of represented bytes. - */ -function phabricator_parse_bytes($input) { - $bytes = trim($input); - if (!strlen($bytes)) { - return null; - } - - // NOTE: Assumes US-centric numeral notation. - $bytes = preg_replace('/[ ,]/', '', $bytes); - - $matches = null; - if (!preg_match('/^(?:\d+(?:[.]\d+)?)([kmgtp]?)b?$/i', $bytes, $matches)) { - throw new Exception("Unable to parse byte size '{$input}'!"); - } - - $scale = array( - 'k' => 1000, - 'm' => 1000 * 1000, - 'g' => 1000 * 1000 * 1000, - 't' => 1000 * 1000 * 1000 * 1000, - 'p' => 1000 * 1000 * 1000 * 1000 * 1000, - ); - - $bytes = (float)$bytes; - if ($matches[1]) { - $bytes *= $scale[strtolower($matches[1])]; - } - - return (int)$bytes; -} - - -function phabricator_format_units_generic( - $n, - array $scales, - array $labels, - $precision = 0, - &$remainder = null) { - - $is_negative = false; - if ($n < 0) { - $is_negative = true; - $n = abs($n); - } - - $remainder = 0; - $accum = 1; - - $scale = array_shift($scales); - $label = array_shift($labels); - while ($n >= $scale && count($labels)) { - $remainder += ($n % $scale) * $accum; - $n /= $scale; - $accum *= $scale; - $label = array_shift($labels); - if (!count($scales)) { - break; - } - $scale = array_shift($scales); - } - - if ($is_negative) { - $n = -$n; - $remainder = -$remainder; - } - - if ($precision) { - $num_string = number_format($n, $precision); - } else { - $num_string = (int)floor($n); - } - - if ($label) { - $num_string .= ' '.$label; - } - - return $num_string; -}