diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -2594,6 +2594,7 @@ 'PhabricatorInlineCommentPreviewController' => 'infrastructure/diff/PhabricatorInlineCommentPreviewController.php', 'PhabricatorInlineSummaryView' => 'infrastructure/diff/view/PhabricatorInlineSummaryView.php', 'PhabricatorInstructionsEditField' => 'applications/transactions/editfield/PhabricatorInstructionsEditField.php', + 'PhabricatorInternalSetting' => 'applications/settings/setting/PhabricatorInternalSetting.php', 'PhabricatorInternationalizationManagementExtractWorkflow' => 'infrastructure/internationalization/management/PhabricatorInternationalizationManagementExtractWorkflow.php', 'PhabricatorInternationalizationManagementWorkflow' => 'infrastructure/internationalization/management/PhabricatorInternationalizationManagementWorkflow.php', 'PhabricatorInvalidConfigSetupCheck' => 'applications/config/check/PhabricatorInvalidConfigSetupCheck.php', @@ -3353,6 +3354,7 @@ 'PhabricatorSearchResultBucketGroup' => 'applications/search/buckets/PhabricatorSearchResultBucketGroup.php', 'PhabricatorSearchResultView' => 'applications/search/view/PhabricatorSearchResultView.php', 'PhabricatorSearchSchemaSpec' => 'applications/search/storage/PhabricatorSearchSchemaSpec.php', + 'PhabricatorSearchScopeSetting' => 'applications/settings/setting/PhabricatorSearchScopeSetting.php', 'PhabricatorSearchSelectController' => 'applications/search/controller/PhabricatorSearchSelectController.php', 'PhabricatorSearchSelectField' => 'applications/search/field/PhabricatorSearchSelectField.php', 'PhabricatorSearchStringListField' => 'applications/search/field/PhabricatorSearchStringListField.php', @@ -3538,6 +3540,7 @@ 'PhabricatorTimeFormatSetting' => 'applications/settings/setting/PhabricatorTimeFormatSetting.php', 'PhabricatorTimeGuard' => 'infrastructure/time/PhabricatorTimeGuard.php', 'PhabricatorTimeTestCase' => 'infrastructure/time/__tests__/PhabricatorTimeTestCase.php', + 'PhabricatorTimezoneIgnoreOffsetSetting' => 'applications/settings/setting/PhabricatorTimezoneIgnoreOffsetSetting.php', 'PhabricatorTimezoneSetting' => 'applications/settings/setting/PhabricatorTimezoneSetting.php', 'PhabricatorTimezoneSetupCheck' => 'applications/config/check/PhabricatorTimezoneSetupCheck.php', 'PhabricatorTitleGlyphsSetting' => 'applications/settings/setting/PhabricatorTitleGlyphsSetting.php', @@ -7196,6 +7199,7 @@ 'PhabricatorInlineCommentPreviewController' => 'PhabricatorController', 'PhabricatorInlineSummaryView' => 'AphrontView', 'PhabricatorInstructionsEditField' => 'PhabricatorEditField', + 'PhabricatorInternalSetting' => 'PhabricatorSetting', 'PhabricatorInternationalizationManagementExtractWorkflow' => 'PhabricatorInternationalizationManagementWorkflow', 'PhabricatorInternationalizationManagementWorkflow' => 'PhabricatorManagementWorkflow', 'PhabricatorInvalidConfigSetupCheck' => 'PhabricatorSetupCheck', @@ -8103,6 +8107,7 @@ 'PhabricatorSearchResultBucketGroup' => 'Phobject', 'PhabricatorSearchResultView' => 'AphrontView', 'PhabricatorSearchSchemaSpec' => 'PhabricatorConfigSchemaSpec', + 'PhabricatorSearchScopeSetting' => 'PhabricatorInternalSetting', 'PhabricatorSearchSelectController' => 'PhabricatorSearchBaseController', 'PhabricatorSearchSelectField' => 'PhabricatorSearchField', 'PhabricatorSearchStringListField' => 'PhabricatorSearchField', @@ -8304,6 +8309,7 @@ 'PhabricatorTimeFormatSetting' => 'PhabricatorSelectSetting', 'PhabricatorTimeGuard' => 'Phobject', 'PhabricatorTimeTestCase' => 'PhabricatorTestCase', + 'PhabricatorTimezoneIgnoreOffsetSetting' => 'PhabricatorInternalSetting', 'PhabricatorTimezoneSetting' => 'PhabricatorOptionGroupSetting', 'PhabricatorTimezoneSetupCheck' => 'PhabricatorSetupCheck', 'PhabricatorTitleGlyphsSetting' => 'PhabricatorSelectSetting', diff --git a/src/aphront/response/AphrontAjaxResponse.php b/src/aphront/response/AphrontAjaxResponse.php --- a/src/aphront/response/AphrontAjaxResponse.php +++ b/src/aphront/response/AphrontAjaxResponse.php @@ -64,8 +64,8 @@ if ($request) { $viewer = $request->getViewer(); if ($viewer) { - $postprocessor_key = $viewer->getPreference( - PhabricatorUserPreferences::PREFERENCE_RESOURCE_POSTPROCESSOR); + $postprocessor_key = $viewer->getUserSetting( + PhabricatorAccessibilitySetting::SETTINGKEY); if (strlen($postprocessor_key)) { $response->setPostprocessorKey($postprocessor_key); } diff --git a/src/applications/people/storage/PhabricatorUser.php b/src/applications/people/storage/PhabricatorUser.php --- a/src/applications/people/storage/PhabricatorUser.php +++ b/src/applications/people/storage/PhabricatorUser.php @@ -66,7 +66,9 @@ private $authorities = array(); private $handlePool; private $csrfSalt; - private $timezoneOverride; + + private $settingCacheKeys = array(); + private $settingCache = array(); protected function readField($field) { switch ($field) { @@ -481,19 +483,50 @@ public function getUserSetting($key) { + // NOTE: We store available keys and cached values separately to make it + // faster to check for `null` in the cache, which is common. + if (isset($this->settingCacheKeys[$key])) { + return $this->settingCache[$key]; + } + $settings_key = PhabricatorUserPreferencesCacheType::KEY_PREFERENCES; $settings = $this->requireCacheData($settings_key); + $defaults = PhabricatorSetting::getAllEnabledSettings($this); + if (array_key_exists($key, $settings)) { - return $settings[$key]; + $value = $settings[$key]; + + // Make sure the value is valid before we return it. This makes things + // more robust when options are changed or removed. + if (isset($defaults[$key])) { + try { + id(clone $defaults[$key]) + ->setViewer($this) + ->assertValidValue($value); + + $this->settingCacheKeys[$key] = true; + $this->settingCache[$key] = $value; + + return $value; + } catch (Exception $ex) { + // Fall through below and return the default value. + } + } } - $defaults = PhabricatorSetting::getAllEnabledSettings($this); if (isset($defaults[$key])) { - return $defaults[$key]->getSettingDefaultValue(); + $value = id(clone $defaults[$key]) + ->setViewer($this) + ->getSettingDefaultValue(); + } else { + $value = null; } - return null; + $this->settingCacheKeys[$key] = true; + $this->settingCache[$key] = $value; + + return $value; } @@ -510,15 +543,25 @@ return ($actual == $value); } + + /** + * @task settings + */ + public function clearUserSettingCache() { + $this->settingCacheKeys = array(); + $this->settingCache = array(); + + $settings_key = PhabricatorUserPreferencesCacheType::KEY_PREFERENCES; + $this->clearCacheData($settings_key); + + return $this; + } + public function getTranslation() { return $this->getUserSetting(PhabricatorTranslationSetting::SETTINGKEY); } public function getTimezoneIdentifier() { - if ($this->timezoneOverride) { - return $this->timezoneOverride; - } - return $this->getUserSetting(PhabricatorTimezoneSetting::SETTINGKEY); } @@ -533,7 +576,9 @@ * @task settings */ public function overrideTimezoneIdentifier($identifier) { - $this->timezoneOverride = $identifier; + $timezone_key = PhabricatorTimezoneSetting::SETTINGKEY; + $this->settingCacheKeys[$timezone_key] = true; + $this->settingCache[$timezone_key] = $identifier; return $this; } @@ -578,17 +623,17 @@ $line, PhabricatorRepository $repository = null) { - $editor = $this->loadPreferences()->getPreference( - PhabricatorUserPreferences::PREFERENCE_EDITOR); + $editor = $this->getUserSetting(PhabricatorEditorSetting::SETTINGKEY); if (is_array($path)) { - $multiedit = $this->loadPreferences()->getPreference( - PhabricatorUserPreferences::PREFERENCE_MULTIEDIT); + $multi_key = PhabricatorEditorMultipleSetting::SETTINGKEY; + $multiedit = $this->getUserSetting($multi_key); switch ($multiedit) { - case '': + case PhabricatorEditorMultipleSetting::VALUE_SPACES: $path = implode(' ', $path); break; - case 'disable': + case PhabricatorEditorMultipleSetting::VALUE_SINGLE: + default: return null; } } @@ -848,48 +893,13 @@ $format = 'M j'; } else { // Same year, month and day so show a time of day. - $pref_time = PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT; - $format = $this->getPreference($pref_time); + $pref_time = PhabricatorTimeFormatSetting::SETTINGKEY; + $format = $this->getUserSetting($pref_time); } return $when->format($format); } - public function getPreference($key) { - $preferences = $this->loadPreferences(); - - // TODO: After T4103 and T7707 this should eventually be pushed down the - // stack into modular preference definitions and role profiles. This is - // just fixing T8601 and mildly anticipating those changes. - $value = $preferences->getPreference($key); - - $allowed_values = null; - switch ($key) { - case PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT: - $allowed_values = array( - 'g:i A', - 'H:i', - ); - break; - case PhabricatorUserPreferences::PREFERENCE_DATE_FORMAT: - $allowed_values = array( - 'Y-m-d', - 'n/j/Y', - 'd-m-Y', - ); - break; - } - - if ($allowed_values !== null) { - $allowed_values = array_fuse($allowed_values); - if (empty($allowed_values[$value])) { - $value = head($allowed_values); - } - } - - return $value; - } - public function __toString() { return $this->getUsername(); } diff --git a/src/applications/settings/controller/PhabricatorSettingsTimezoneController.php b/src/applications/settings/controller/PhabricatorSettingsTimezoneController.php --- a/src/applications/settings/controller/PhabricatorSettingsTimezoneController.php +++ b/src/applications/settings/controller/PhabricatorSettingsTimezoneController.php @@ -30,7 +30,7 @@ if ($request->isFormPost()) { $timezone = $request->getStr('timezone'); - $pref_ignore = PhabricatorUserPreferences::PREFERENCE_IGNORE_OFFSET; + $pref_ignore = PhabricatorTimezoneIgnoreOffsetSetting::SETTINGKEY; $pref_timezone = PhabricatorTimezoneSetting::SETTINGKEY; $preferences = $viewer->loadPreferences(); @@ -56,8 +56,7 @@ ->setPreference($pref_timezone, $timezone) ->save(); - $viewer->clearCacheData( - PhabricatorUserPreferencesCacheType::KEY_PREFERENCES); + $viewer->clearUserSettingCache(); } } diff --git a/src/applications/settings/panel/PhabricatorDateTimeSettingsPanel.php b/src/applications/settings/panel/PhabricatorDateTimeSettingsPanel.php --- a/src/applications/settings/panel/PhabricatorDateTimeSettingsPanel.php +++ b/src/applications/settings/panel/PhabricatorDateTimeSettingsPanel.php @@ -22,7 +22,7 @@ $pref_time = PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT; $pref_date = PhabricatorUserPreferences::PREFERENCE_DATE_FORMAT; $pref_week_start = PhabricatorUserPreferences::PREFERENCE_WEEK_START_DAY; - $pref_ignore = PhabricatorUserPreferences::PREFERENCE_IGNORE_OFFSET; + $pref_ignore = PhabricatorTimezoneIgnoreOffsetSetting::SETTINGKEY; $preferences = $user->loadPreferences(); $errors = array(); diff --git a/src/applications/settings/setting/PhabricatorInternalSetting.php b/src/applications/settings/setting/PhabricatorInternalSetting.php new file mode 100644 --- /dev/null +++ b/src/applications/settings/setting/PhabricatorInternalSetting.php @@ -0,0 +1,4 @@ +validateTransactionValue($value); + } + public function getTransactionNewValue($value) { return $value; } diff --git a/src/applications/settings/setting/PhabricatorTimezoneIgnoreOffsetSetting.php b/src/applications/settings/setting/PhabricatorTimezoneIgnoreOffsetSetting.php new file mode 100644 --- /dev/null +++ b/src/applications/settings/setting/PhabricatorTimezoneIgnoreOffsetSetting.php @@ -0,0 +1,12 @@ +formatTime($epoch, 'Y!m!d!g:i A'); + $readable = $this->formatTime($epoch, 'Y!m!d!'.$this->getTimeFormat()); $readable = explode('!', $readable, 4); $year = $readable[0]; @@ -140,13 +140,15 @@ } private function getTimeFormat() { - return $this->getViewer() - ->getPreference(PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT); + $viewer = $this->getViewer(); + $time_key = PhabricatorTimeFormatSetting::SETTINGKEY; + return $viewer->getUserSetting($time_key); } private function getDateFormat() { - return $this->getViewer() - ->getPreference(PhabricatorUserPreferences::PREFERENCE_DATE_FORMAT); + $viewer = $this->getViewer(); + $date_key = PhabricatorDateFormatSetting::SETTINGKEY; + return $viewer->getUserSetting($date_key); } private function getTimeInputValue() { @@ -241,7 +243,6 @@ 'format' => $this->getTimeFormat(), )); - $time_sel = javelin_tag( 'input', array( @@ -262,9 +263,9 @@ ), $time_sel); - $preferences = $this->getViewer()->loadPreferences(); - $pref_week_start = PhabricatorUserPreferences::PREFERENCE_WEEK_START_DAY; - $week_start = $preferences->getPreference($pref_week_start, 0); + $viewer = $this->getViewer(); + $week_key = PhabricatorWeekStartDaySetting::SETTINGKEY; + $week_start = $viewer->getUserSetting($week_key); Javelin::initBehavior('fancy-datepicker', array( 'format' => $this->getDateFormat(), diff --git a/src/view/form/control/AphrontFormDateControlValue.php b/src/view/form/control/AphrontFormDateControlValue.php --- a/src/view/form/control/AphrontFormDateControlValue.php +++ b/src/view/form/control/AphrontFormDateControlValue.php @@ -207,13 +207,15 @@ } private function getTimeFormat() { - return $this->getViewer() - ->getPreference(PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT); + $viewer = $this->getViewer(); + $time_key = PhabricatorTimeFormatSetting::SETTINGKEY; + return $viewer->getUserSetting($time_key); } private function getDateFormat() { - return $this->getViewer() - ->getPreference(PhabricatorUserPreferences::PREFERENCE_DATE_FORMAT); + $viewer = $this->getViewer(); + $date_key = PhabricatorDateFormatSetting::SETTINGKEY; + return $viewer->getUserSetting($date_key); } private function getFormattedDateFromDate($date, $time) { diff --git a/src/view/page/PhabricatorBarePageView.php b/src/view/page/PhabricatorBarePageView.php --- a/src/view/page/PhabricatorBarePageView.php +++ b/src/view/page/PhabricatorBarePageView.php @@ -130,8 +130,8 @@ if ($this->getRequest()) { $viewer = $this->getRequest()->getViewer(); if ($viewer) { - $postprocessor_key = $viewer->getPreference( - PhabricatorUserPreferences::PREFERENCE_RESOURCE_POSTPROCESSOR); + $postprocessor_key = $viewer->getUserSetting( + PhabricatorAccessibilitySetting::SETTINGKEY); if (strlen($postprocessor_key)) { $response->setPostProcessorKey($postprocessor_key); } diff --git a/src/view/page/PhabricatorStandardPageView.php b/src/view/page/PhabricatorStandardPageView.php --- a/src/view/page/PhabricatorStandardPageView.php +++ b/src/view/page/PhabricatorStandardPageView.php @@ -225,13 +225,8 @@ if ($user->isLoggedIn()) { $offset = $user->getTimeZoneOffset(); - $preferences = $user->loadPreferences(); - $ignore_key = PhabricatorUserPreferences::PREFERENCE_IGNORE_OFFSET; - - $ignore = $preferences->getPreference($ignore_key); - if (!strlen($ignore)) { - $ignore = null; - } + $ignore_key = PhabricatorTimezoneIgnoreOffsetSetting::SETTINGKEY; + $ignore = $user->getUserSetting($ignore_key); Javelin::initBehavior( 'detect-timezone', diff --git a/src/view/page/menu/PhabricatorMainMenuSearchView.php b/src/view/page/menu/PhabricatorMainMenuSearchView.php --- a/src/view/page/menu/PhabricatorMainMenuSearchView.php +++ b/src/view/page/menu/PhabricatorMainMenuSearchView.php @@ -50,7 +50,7 @@ ''); $search_datasource = new PhabricatorSearchDatasource(); - $scope_key = PhabricatorUserPreferences::PREFERENCE_SEARCH_SCOPE; + $scope_key = PhabricatorSearchScopeSetting::SETTINGKEY; Javelin::initBehavior( 'phabricator-search-typeahead', @@ -177,10 +177,8 @@ 'href' => PhabricatorEnv::getDoclink('Search User Guide'), ); - $scope_key = PhabricatorUserPreferences::PREFERENCE_SEARCH_SCOPE; - $current_value = $viewer->loadPreferences()->getPreference( - $scope_key, - 'all'); + $scope_key = PhabricatorSearchScopeSetting::SETTINGKEY; + $current_value = $viewer->getUserSetting($scope_key); $current_icon = 'fa-globe'; foreach ($items as $item) { diff --git a/src/view/phui/calendar/PHUICalendarListView.php b/src/view/phui/calendar/PHUICalendarListView.php --- a/src/view/phui/calendar/PHUICalendarListView.php +++ b/src/view/phui/calendar/PHUICalendarListView.php @@ -147,16 +147,18 @@ } private function getEventTooltip(AphrontCalendarEventView $event) { - $time_pref = $this->getUser() - ->getPreference(PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT); + $viewer = $this->getViewer(); + $time_key = PhabricatorTimeFormatSetting::SETTINGKEY; + $time_pref = $viewer->getUserSetting($time_key); Javelin::initBehavior('phabricator-tooltips'); $start = id(AphrontFormDateControlValue::newFromEpoch( - $this->getUser(), + $viewer, $event->getEpochStart())); + $end = id(AphrontFormDateControlValue::newFromEpoch( - $this->getUser(), + $viewer, $event->getEpochEnd())); $start_date = $start->getDateTime()->format('m d Y'); diff --git a/src/view/viewutils.php b/src/view/viewutils.php --- a/src/view/viewutils.php +++ b/src/view/viewutils.php @@ -31,21 +31,21 @@ } function phabricator_time($epoch, $user) { - $time_key = PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT; + $time_key = PhabricatorTimeFormatSetting::SETTINGKEY; return phabricator_format_local_time( $epoch, $user, - $user->getPreference($time_key)); + $user->getUserSetting($time_key)); } function phabricator_datetime($epoch, $user) { - $time_key = PhabricatorUserPreferences::PREFERENCE_TIME_FORMAT; + $time_key = PhabricatorTimeFormatSetting::SETTINGKEY; return phabricator_format_local_time( $epoch, $user, pht('%s, %s', phutil_date_format($epoch), - $user->getPreference($time_key))); + $user->getUserSetting($time_key))); } /**