Issue #3112290 by Spokje, mondrake, mpdonadio, daffie, andypost, Kristen Pol, quietone, mallezie, alexpott: Replace REQUEST_TIME in procedural code

merge-requests/2475/head
Alex Pott 2022-07-04 15:34:45 +01:00
parent e29436562f
commit a15d0efd16
No known key found for this signature in database
GPG Key ID: BDA67E7EE836E5CE
18 changed files with 52 additions and 40 deletions

View File

@ -163,12 +163,13 @@ function aggregator_entity_extra_field_info() {
*/
function aggregator_cron() {
$queue = \Drupal::queue('aggregator_feeds');
$request_time = \Drupal::time()->getRequestTime();
$ids = \Drupal::entityTypeManager()->getStorage('aggregator_feed')->getFeedIdsToRefresh();
foreach (Feed::loadMultiple($ids) as $feed) {
if ($queue->createItem($feed)) {
// Add timestamp to avoid queueing item more than once.
$feed->setQueuedTime(REQUEST_TIME);
$feed->setQueuedTime($request_time);
$feed->save();
}
}
@ -176,7 +177,7 @@ function aggregator_cron() {
// Delete queued timestamp after 6 hours assuming the update has failed.
$ids = \Drupal::entityQuery('aggregator_feed')
->accessCheck(FALSE)
->condition('queued', REQUEST_TIME - (3600 * 6), '<')
->condition('queued', $request_time - (3600 * 6), '<')
->execute();
if ($ids) {

View File

@ -35,9 +35,9 @@ use Drupal\user\UserInterface;
* Comments changed after this time may be marked new, updated, or read,
* depending on their state for the current user. Defaults to 30 days ago.
*
* @todo Remove when https://www.drupal.org/node/1029708 lands.
* @todo Remove when https://www.drupal.org/node/2006632 lands.
*/
define('COMMENT_NEW_LIMIT', REQUEST_TIME - 30 * 24 * 60 * 60);
define('COMMENT_NEW_LIMIT', ((int) $_SERVER['REQUEST_TIME']) - 30 * 24 * 60 * 60);
/**
* Implements hook_help().

View File

@ -648,7 +648,7 @@ function file_cron() {
$fids = Drupal::entityQuery('file')
->accessCheck(FALSE)
->condition('status', FileInterface::STATUS_PERMANENT, '<>')
->condition('changed', REQUEST_TIME - $age, '<')
->condition('changed', \Drupal::time()->getRequestTime() - $age, '<')
->range(0, 100)
->execute();
$files = $file_storage->loadMultiple($fids);

View File

@ -20,8 +20,10 @@ use Drupal\user\UserInterface;
*
* Entities changed within this time may be marked as new, updated, or read,
* depending on their state for the current user. Defaults to 30 days ago.
*
* @todo Remove when https://www.drupal.org/node/2006632 lands.
*/
define('HISTORY_READ_LIMIT', REQUEST_TIME - 30 * 24 * 60 * 60);
define('HISTORY_READ_LIMIT', ((int) $_SERVER['REQUEST_TIME']) - 30 * 24 * 60 * 60);
/**
* Implements hook_help().
@ -109,16 +111,17 @@ function history_write($nid, $account = NULL) {
}
if ($account->isAuthenticated()) {
$request_time = \Drupal::time()->getRequestTime();
\Drupal::database()->merge('history')
->keys([
'uid' => $account->id(),
'nid' => $nid,
])
->fields(['timestamp' => REQUEST_TIME])
->fields(['timestamp' => $request_time])
->execute();
// Update static cache.
$history = &drupal_static('history_read_multiple', []);
$history[$nid] = REQUEST_TIME;
$history[$nid] = $request_time;
}
}

View File

@ -27,7 +27,7 @@ function template_preprocess_image_style_preview(&$variables) {
$variables['style_name'] = $style->label();
// Cache bypass token.
$variables['cache_bypass'] = REQUEST_TIME;
$variables['cache_bypass'] = \Drupal::time()->getRequestTime();
// Sample image info.
$sample_width = 160;

View File

@ -119,7 +119,7 @@ function locale_translation_batch_status_finished($success, $results) {
if (!isset($results['failed_files']) && !isset($results['files'])) {
\Drupal::messenger()->addStatus(t('Nothing to check.'));
}
\Drupal::state()->set('locale.translation_last_checked', REQUEST_TIME);
\Drupal::state()->set('locale.translation_last_checked', \Drupal::time()->getRequestTime());
}
else {
\Drupal::messenger()->addError(t('An error occurred trying to check available interface translation updates.'));
@ -219,7 +219,7 @@ function locale_translation_batch_fetch_import($project, $langcode, $options, &$
function locale_translation_batch_fetch_finished($success, $results) {
\Drupal::moduleHandler()->loadInclude('locale', 'inc', 'locale.bulk');
if ($success) {
\Drupal::state()->set('locale.translation_last_checked', REQUEST_TIME);
\Drupal::state()->set('locale.translation_last_checked', \Drupal::time()->getRequestTime());
}
return locale_translate_batch_finished($success, $results);
}

View File

@ -275,6 +275,7 @@ function locale_translate_batch_import($file, array $options, &$context) {
*/
function locale_translate_batch_import_save($context) {
if (isset($context['results']['files'])) {
$request_time = \Drupal::time()->getRequestTime();
foreach ($context['results']['files'] as $file) {
// Update the file history if both project and version are known. This
// table is used by the automated translation update function which tracks
@ -282,7 +283,7 @@ function locale_translate_batch_import_save($context) {
// translation files are not tracked and are therefore not stored in this
// table.
if ($file->project && $file->version) {
$file->last_checked = REQUEST_TIME;
$file->last_checked = $request_time;
locale_translation_update_file_history($file);
}
}

View File

@ -191,7 +191,7 @@ function locale_translation_check_projects($projects = [], $langcodes = []) {
else {
// Retrieve and save the status of local translations only.
locale_translation_check_projects_local($projects, $langcodes);
\Drupal::state()->set('locale.translation_last_checked', REQUEST_TIME);
\Drupal::state()->set('locale.translation_last_checked', \Drupal::time()->getRequestTime());
}
}

View File

@ -912,6 +912,7 @@ function locale_translation_status_save($project, $langcode, $type, $data) {
// Merge the new status data with the existing status.
if (isset($status[$project][$langcode])) {
$request_time = \Drupal::time()->getRequestTime();
switch ($type) {
case LOCALE_TRANSLATION_REMOTE:
case LOCALE_TRANSLATION_LOCAL:
@ -923,14 +924,14 @@ function locale_translation_status_save($project, $langcode, $type, $data) {
if (isset($data->timestamp) && $data->timestamp) {
if ($data->timestamp > $status[$project][$langcode]->timestamp) {
$status[$project][$langcode]->timestamp = $data->timestamp;
$status[$project][$langcode]->last_checked = REQUEST_TIME;
$status[$project][$langcode]->last_checked = $request_time;
$status[$project][$langcode]->type = $type;
}
}
break;
case LOCALE_TRANSLATION_CURRENT:
$data->last_checked = REQUEST_TIME;
$data->last_checked = $request_time;
$status[$project][$langcode]->timestamp = $data->timestamp;
$status[$project][$langcode]->last_checked = $data->last_checked;
$status[$project][$langcode]->type = $type;
@ -939,7 +940,7 @@ function locale_translation_status_save($project, $langcode, $type, $data) {
}
\Drupal::keyValue('locale.translation_status')->set($project, $status[$project]);
\Drupal::state()->set('locale.translation_last_checked', REQUEST_TIME);
\Drupal::state()->set('locale.translation_last_checked', $request_time);
}
}

View File

@ -331,7 +331,8 @@ function locale_cron_fill_queue() {
$config = \Drupal::config('locale.settings');
// Determine which project+language should be updated.
$last = REQUEST_TIME - $config->get('translation.update_interval_days') * 3600 * 24;
$request_time = \Drupal::time()->getRequestTime();
$last = $request_time - $config->get('translation.update_interval_days') * 3600 * 24;
$projects = \Drupal::service('locale.project')->getAll();
$projects = array_filter($projects, function ($project) {
return $project['status'] == 1;
@ -348,7 +349,7 @@ function locale_cron_fill_queue() {
// Update the last_checked timestamp of the project+language that will
// be checked for updates.
$connection->update('locale_file')
->fields(['last_checked' => REQUEST_TIME])
->fields(['last_checked' => $request_time])
->condition('project', $file->project)
->condition('langcode', $file->langcode)
->execute();

View File

@ -726,10 +726,11 @@ function system_requirements($phase) {
// Determine severity based on time since cron last ran.
$severity = REQUIREMENT_INFO;
if (REQUEST_TIME - $cron_last > $threshold_error) {
$request_time = \Drupal::time()->getRequestTime();
if ($request_time - $cron_last > $threshold_error) {
$severity = REQUIREMENT_ERROR;
}
elseif (REQUEST_TIME - $cron_last > $threshold_warning) {
elseif ($request_time - $cron_last > $threshold_warning) {
$severity = REQUIREMENT_WARNING;
}

View File

@ -54,17 +54,18 @@ function system_token_info() {
$date_formatter = \Drupal::service('date.formatter');
// Date related tokens.
$request_time = \Drupal::time()->getRequestTime();
$date['short'] = [
'name' => t("Short format"),
'description' => t("A date in 'short' format. (%date)", ['%date' => $date_formatter->format(REQUEST_TIME, 'short')]),
'description' => t("A date in 'short' format. (%date)", ['%date' => $date_formatter->format($request_time, 'short')]),
];
$date['medium'] = [
'name' => t("Medium format"),
'description' => t("A date in 'medium' format. (%date)", ['%date' => $date_formatter->format(REQUEST_TIME, 'medium')]),
'description' => t("A date in 'medium' format. (%date)", ['%date' => $date_formatter->format($request_time, 'medium')]),
];
$date['long'] = [
'name' => t("Long format"),
'description' => t("A date in 'long' format. (%date)", ['%date' => $date_formatter->format(REQUEST_TIME, 'long')]),
'description' => t("A date in 'long' format. (%date)", ['%date' => $date_formatter->format($request_time, 'long')]),
];
$date['custom'] = [
'name' => t("Custom format"),
@ -72,11 +73,11 @@ function system_token_info() {
];
$date['since'] = [
'name' => t("Time-since"),
'description' => t("A date in 'time-since' format. (%date)", ['%date' => $date_formatter->formatTimeDiffSince(REQUEST_TIME - 360)]),
'description' => t("A date in 'time-since' format. (%date)", ['%date' => $date_formatter->formatTimeDiffSince($request_time - 360)]),
];
$date['raw'] = [
'name' => t("Raw timestamp"),
'description' => t("A date in UNIX timestamp format (%date)", ['%date' => REQUEST_TIME]),
'description' => t("A date in UNIX timestamp format (%date)", ['%date' => $request_time]),
];
return [
@ -157,7 +158,7 @@ function system_tokens($type, $tokens, array $data, array $options, BubbleableMe
elseif ($type == 'date') {
if (empty($data['date'])) {
$date = REQUEST_TIME;
$date = \Drupal::time()->getRequestTime();
// We depend on the current request time, so the tokens are not cacheable
// at all.
$bubbleable_metadata->setCacheMaxAge(0);

View File

@ -33,6 +33,7 @@ function _update_cron_notify() {
$notify_list = $update_config->get('notification.emails');
if (!empty($notify_list)) {
$default_langcode = \Drupal::languageManager()->getDefaultLanguage()->getId();
$request_time = \Drupal::time()->getRequestTime();
foreach ($notify_list as $target) {
if ($target_user = user_load_by_mail($target)) {
$target_langcode = $target_user->getPreferredLangcode();
@ -44,7 +45,7 @@ function _update_cron_notify() {
// Track when the last mail was successfully sent to avoid sending
// too many emails.
if ($message['result']) {
\Drupal::state()->set('update.last_email_notification', REQUEST_TIME);
\Drupal::state()->set('update.last_email_notification', $request_time);
}
}
}

View File

@ -177,7 +177,8 @@ function update_cron() {
$frequency = $update_config->get('check.interval_days');
$interval = 60 * 60 * 24 * $frequency;
$last_check = \Drupal::state()->get('update.last_check', 0);
if ((REQUEST_TIME - $last_check) > $interval) {
$request_time = \Drupal::time()->getRequestTime();
if (($request_time - $last_check) > $interval) {
// If the configured update interval has elapsed, we want to invalidate
// the data for all projects, attempt to re-fetch, and trigger any
// configured notifications about the new status.
@ -190,7 +191,7 @@ function update_cron() {
update_get_available(TRUE);
}
$last_email_notice = \Drupal::state()->get('update.last_email_notification', 0);
if ((REQUEST_TIME - $last_email_notice) > $interval) {
if (($request_time - $last_email_notice) > $interval) {
// If configured time between notifications elapsed, send email about
// updates possibly available.
\Drupal::moduleHandler()->loadInclude('update', 'inc', 'update.fetch');
@ -702,8 +703,8 @@ function update_delete_file_if_stale($path) {
if (file_exists($path)) {
$filectime = filectime($path);
$max_age = \Drupal::config('system.file')->get('temporary_maximum_age');
if (REQUEST_TIME - $filectime > $max_age || (preg_match('/.*-dev\.(tar\.gz|zip)/i', $path) && REQUEST_TIME - $filectime > 300)) {
$request_time = \Drupal::time()->getRequestTime();
if ($request_time - $filectime > $max_age || (preg_match('/.*-dev\.(tar\.gz|zip)/i', $path) && $request_time - $filectime > 300)) {
try {
\Drupal::service('file_system')->deleteRecursive($path);
return TRUE;

View File

@ -459,7 +459,7 @@ function user_login_finalize(UserInterface $account) {
\Drupal::logger('user')->notice('Session opened for %name.', ['%name' => $account->getAccountName()]);
// Update the user table timestamp noting user has logged in.
// This is also used to invalidate one-time login links.
$account->setLastLoginTime(REQUEST_TIME);
$account->setLastLoginTime(\Drupal::time()->getRequestTime());
\Drupal::entityTypeManager()
->getStorage('user')
->updateLastLoginTimestamp($account);
@ -521,7 +521,7 @@ function user_user_logout(AccountInterface $account) {
* they can change their password.
*/
function user_pass_reset_url($account, $options = []) {
$timestamp = REQUEST_TIME;
$timestamp = \Drupal::time()->getRequestTime();
$langcode = $options['langcode'] ?? $account->getPreferredLangcode();
return Url::fromRoute('user.reset',
[
@ -554,7 +554,7 @@ function user_pass_reset_url($account, $options = []) {
* @see \Drupal\user\Controller\UserController::confirmCancel()
*/
function user_cancel_url(UserInterface $account, $options = []) {
$timestamp = REQUEST_TIME;
$timestamp = \Drupal::time()->getRequestTime();
$langcode = $options['langcode'] ?? $account->getPreferredLangcode();
$url_options = ['absolute' => TRUE, 'language' => \Drupal::languageManager()->getLanguage($langcode)];
return Url::fromRoute('user.cancel_confirm', [
@ -579,7 +579,7 @@ function user_cancel_url(UserInterface $account, $options = []) {
* @param \Drupal\user\UserInterface $account
* An object containing the user account.
* @param int $timestamp
* A UNIX timestamp, typically REQUEST_TIME.
* A UNIX timestamp, typically \Drupal::time()->getRequestTime().
*
* @return string
* A string that is safe for use in URLs and SQL statements.
@ -1141,9 +1141,10 @@ function user_modules_uninstalled($modules) {
* An array of key/value pairs to be saved into a cookie.
*/
function user_cookie_save(array $values) {
$request_time = \Drupal::time()->getRequestTime();
foreach ($values as $field => $value) {
// Set cookie for 365 days.
setrawcookie('Drupal.visitor.' . $field, rawurlencode($value), REQUEST_TIME + 31536000, '/');
setrawcookie('Drupal.visitor.' . $field, rawurlencode($value), $request_time + 31536000, '/');
}
}
@ -1154,7 +1155,7 @@ function user_cookie_save(array $values) {
* A cookie name such as 'homepage'.
*/
function user_cookie_delete($cookie_name) {
setrawcookie('Drupal.visitor.' . $cookie_name, '', REQUEST_TIME - 3600, '/');
setrawcookie('Drupal.visitor.' . $cookie_name, '', \Drupal::time()->getRequestTime() - 3600, '/');
}
/**

View File

@ -661,7 +661,7 @@ function hook_views_query_substitutions(ViewExecutable $view) {
// Example from views_views_query_substitutions().
return [
'***CURRENT_VERSION***' => \Drupal::VERSION,
'***CURRENT_TIME***' => REQUEST_TIME,
'***CURRENT_TIME***' => \Drupal::time()->getRequestTime(),
'***LANGUAGE_language_content***' => \Drupal::languageManager()->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(),
PluginBase::VIEWS_QUERY_LANGUAGE_SITE_DEFAULT => \Drupal::languageManager()->getDefaultLanguage()->getId(),
];

View File

@ -956,7 +956,7 @@ function template_preprocess_views_view_opml(&$variables) {
}
$variables['title'] = $title;
$variables['items'] = $items;
$variables['updated'] = gmdate(DATE_RFC2822, REQUEST_TIME);
$variables['updated'] = gmdate(DATE_RFC2822, \Drupal::time()->getRequestTime());
// During live preview we don't want to output the header since the contents
// of the feed are being displayed inside a normal HTML page.

View File

@ -20,7 +20,7 @@ use Drupal\views\Plugin\views\PluginBase;
function views_views_query_substitutions(ViewExecutable $view) {
$substitutions = [
'***CURRENT_VERSION***' => \Drupal::VERSION,
'***CURRENT_TIME***' => REQUEST_TIME,
'***CURRENT_TIME***' => \Drupal::time()->getRequestTime(),
] + PluginBase::queryLanguageSubstitutions();
return $substitutions;