Problem/Motivation

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

mpdonadio created an issue. See original summary.

mpdonadio’s picture

Title: Replace REQUEST_TIME in include (and similar) files that need to directly access $_SERVER » Replace REQUEST_TIME in non-OO and non-module code
Status: Active » Needs review
StatusFileSize
new4.19 KB

Took the patch from 2902895-43.patch and

git checkout 8.9.x core/modules core/lib core/tests
git diff 8.9.x > 3112283-02.patch

which just leaves us with the changes to (git diff --name-only 8.9.x)

core/core.api.php
core/includes/bootstrap.inc
core/includes/common.inc
core/includes/install.core.inc
core/rebuild.php

So, instead of

1. include files that need the $_SERVER thing.

I think a better description of the scope is

1. replace REQUEST_TIME in non-OO and non-module code

mpdonadio’s picture

StatusFileSize
new4.19 KB

Reroll b/c change in UserBlocksTest.

mpdonadio credited JeroenT.

mpdonadio credited Vlad Bo.

mpdonadio credited pifagor.

mpdonadio credited voleger.

mpdonadio’s picture

Commit credits.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

All changes look good to me.
For me it is RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/includes/bootstrap.inc
@@ -688,7 +688,7 @@ function drupal_valid_test_ua($new_prefix = NULL) {
-    $time_diff = REQUEST_TIME - $time;
+    $time_diff = (int) $_SERVER['REQUEST_TIME'] - $time;

Can this use \Drupal::time() now? Also if we can't I think using time() is better than doing $_SERVER['REQUEST_TIME'] - also there is no need to cast to int. it is an int.

ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new4.17 KB
new709 bytes

Here I have made changes as suggested in comment #10

daffie’s picture

@ravi.shankar: Did you try the change that @alexpott suggested in comment #10 with \Drupal::time()->getCurrentTime() instead of time(). If you did and it did not work then it is for me RTBC.

ravi.shankar’s picture

Hi @daffie,

Unfortunately, I didn't try \Drupal::time()

I'll give it a try now.

ravi.shankar’s picture

StatusFileSize
new4.2 KB
new712 bytes

Here I have added a new patch.

daffie’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new4.17 KB

The patch with \Drupal::time()->getCurrentTime() instead of time() did not work. Re uploading the patch from comment #11. That patch addresses the change that @alexpott would like. Therefor is that patch for me RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/includes/bootstrap.inc
@@ -688,7 +688,7 @@ function drupal_valid_test_ua($new_prefix = NULL) {
-    $time_diff = REQUEST_TIME - $time;
+    $time_diff = time() - $time;

This needs to be the request time. I'm guessing that because \Drupal is not working this method is being called when there is no container. Changing this to time() still doesn't buy us the test-ability that something \Drupal::time() would. Is time() better than $_SERVER['REQUEST_TIME'] - i dunno - this feels like it is normally about validating a request made during a test so request time is more logical.

All this said another issue converting to the \Drupal::time() service caused a lot of random fails so I'm not sure that the plan to do the conversions piecemeal is a good one because there are situations (especially another kernel tests) where $_SERVER['REQUEST_TIME'] !== \Drupal::time()->getRequestTime() - see #2902895-58: [meta][no patch] Replace uses of REQUEST_TIME and time() with time service

alexpott’s picture

If we want to resolve the problem of testability for drupal_valid_test_ua() then we need to pass the request into this method. If we did that we should get the request time form that object. So let's file another issue to do that.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

kristen pol’s picture

ptmkenny’s picture

For anyone looking for an explanation of why there are several issues on this, see the discussion in the parent issue.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
ravi.shankar’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new4.17 KB

Added reroll of patch #15 on Drupal 9.4.x.

mondrake’s picture

Issue tags: +PHPStan-0
andypost’s picture

Assigned: mpdonadio » Unassigned
Status: Needs review » Needs work
Issue tags: +ContributionWeekend2022
  1. +++ b/core/core.api.php
    @@ -1935,13 +1935,13 @@ function hook_cron() {
       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(\Drupal::time()->getRequestTime());
    

    should be moved out of loop

  2. +++ b/core/includes/install.core.inc
    @@ -1058,9 +1058,9 @@ function install_display_output($output, $install_state) {
    -    'Last-Modified' => gmdate(DATE_RFC1123, REQUEST_TIME),
    +    'Last-Modified' => gmdate(DATE_RFC1123, \Drupal::time()->getRequestTime()),
         'Cache-Control' => 'no-cache, must-revalidate',
    -    'ETag' => '"' . REQUEST_TIME . '"',
    +    'ETag' => '"' . \Drupal::time()->getRequestTime() . '"',
    

    should use a variable to not call service twice

  3. +++ b/core/rebuild.php
    @@ -38,7 +38,7 @@
    -    ((REQUEST_TIME - $request->query->get('timestamp')) < 300) &&
    +    ((\Drupal::time()->getRequestTime() - $request->query->get('timestamp')) < 300) &&
    

    I think this place should use request->server->getInt(REQUEST_TIME)

ravi.shankar’s picture

Status: Needs work » Needs review
StatusFileSize
new4.43 KB
new2.97 KB

Made changes as per comment #27, please review.

cliddell’s picture

Status: Needs review » Needs work

Thanks for working on this ravi! I think there's still a few more changes which need to be made.

  1. +++ b/core/rebuild.php
    @@ -38,7 +38,7 @@
    -    ((REQUEST_TIME - $request->query->get('timestamp')) < 300) &&
    +    ((\Drupal::time()->getRequestTime() - $request->query->get('timestamp')) < 300) &&
    

    You need to pass REQUEST_TIME as a string, not a constant since the constant is being deprecated. Passing 'REQUEST_TIME' tells the request object, "Hey, I want the time of this request as an integer". So, this should really be $request->server->getInt('REQUEST_TIME').

  2. core/includes/install.core.inc
    @@ -1884,7 +1884,7 @@ function install_finished(&$install_state) {
       \Drupal::messenger()->addStatus($success_message);
     
       // Record when this install ran.
       \Drupal::state()->set('install_time', $_SERVER['REQUEST_TIME']);
     }
    

    This should really be updated as well with \Drupal::time()->getRequestTime());.

  3. core/scripts/generate-d7-content.sh
    @@ -237,7 +238,7 @@
       $node->status = intval($i / 2) % 2;
       $node->revision = 1;
       $node->promote = $i % 2;
       $node->created = REQUEST_TIME + $i * 43200;
    

    This should be updated with either $_SERVER['REQUEST_TIME'] or maybe time(). If time() is used, I'd recommend setting the variable outside of the loop to prevent repeated calls to time().

andregp’s picture

I'm gonna work on @cliddell suggestions at #29. :)

andregp’s picture

Status: Needs work » Needs review
StatusFileSize
new5.23 KB
new1.4 KB

Patch updated with @cliddell suggestions.

jhedstrom’s picture

+++ b/core/scripts/generate-d7-content.sh
@@ -237,7 +237,7 @@
+  $node->created = $_SERVER['REQUEST_TIME'] + $i * 43200;

I'm not sure $_SERVER['REQUEST_TIME'] is ever set in this script? It might be safer to use time() as noted above, or explicitly set $_SERVER['REQUEST_TIME'] = time() at the top of the script where other $_SERVER values are set...

jhedstrom’s picture

Actually, I think PHP always sets $_SERVER['REQUEST_TIME'), so #32 can be ignored.

jhedstrom’s picture

Status: Needs review » Reviewed & tested by the community

I think #31 is good to go.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 10dce7b and pushed to 10.0.x. Thanks!
Committed and pushed debf702718 to 9.5.x and 960480f092 to 9.4.x. Thanks!

diff --git a/core/phpstan-baseline.neon b/core/phpstan-baseline.neon
index 883c95f1d7..e7bcbbb205 100644
--- a/core/phpstan-baseline.neon
+++ b/core/phpstan-baseline.neon
@@ -1,25 +1,10 @@
 parameters:
 	ignoreErrors:
-		-
-			message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:10\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#"
-			count: 1
-			path: includes/bootstrap.inc
-
-		-
-			message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:10\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#"
-			count: 1
-			path: includes/common.inc
-
 		-
 			message: "#^Function format_size\\(\\) should return Drupal\\\\Core\\\\StringTranslation\\\\TranslatableMarkup but return statement is missing\\.$#"
 			count: 1
 			path: includes/common.inc
 
-		-
-			message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:10\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#"
-			count: 2
-			path: includes/install.core.inc
-
 		-
 			message: "#^Function install_config_download_translations\\(\\) should return string but return statement is missing\\.$#"
 			count: 1
@@ -2618,7 +2603,6 @@ parameters:
 			count: 1
 			path: modules/system/tests/modules/service_provider_test/src/TestFileUsage.php
 
-
 		-
 			message: "#^Access to an undefined property Drupal\\\\Tests\\\\system\\\\Functional\\\\Entity\\\\EntityListBuilderTest\\:\\:\\$webUser\\.$#"
 			count: 1
@@ -3524,7 +3508,6 @@ parameters:
 			count: 1
 			path: modules/workspaces/src/WorkspacePublisher.php
 
-
 		-
 			message: "#^Access to an undefined property Drupal\\\\Tests\\\\workspaces\\\\Kernel\\\\EntityReferenceSupportedNewEntitiesConstraintValidatorTest\\:\\:\\$entityTypeManager\\.$#"
 			count: 2
@@ -3540,11 +3523,6 @@ parameters:
 			count: 5
 			path: profiles/demo_umami/modules/demo_umami_content/src/InstallHelper.php
 
-		-
-			message: "#^Call to deprecated constant REQUEST_TIME\\: Deprecated in drupal\\:8\\.3\\.0 and is removed from drupal\\:10\\.0\\.0\\. Use \\\\Drupal\\:\\:time\\(\\)\\-\\>getRequestTime\\(\\); $#"
-			count: 1
-			path: rebuild.php
-
 		-
 			message: "#^Access to an undefined property Drupal\\\\BuildTests\\\\Framework\\\\BuildTestBase\\:\\:\\$phpFinder\\.$#"
 			count: 1
diff --git a/core/phpstan.neon.dist b/core/phpstan.neon.dist
index 543c5d50ef..41bedcd7d3 100644
--- a/core/phpstan.neon.dist
+++ b/core/phpstan.neon.dist
@@ -18,6 +18,9 @@ parameters:
     - ../*/node_modules/*
     - */tests/fixtures/*.php
     - */tests/fixtures/*.php.gz
+    # Skip Drupal 6 & 7 code.
+    - core/scripts/generate-d6-content.sh
+    - core/scripts/generate-d7-content.sh
     # Skip data files.
     - lib/Drupal/Component/Transliteration/data/*.php
     # Below extends on purpose a non existing class for testing.

On Drupal 10 has to make the above changes to commit this one. Given that core/phpstan-baseline.neon is automatically generated and the skips are obvious I think it is fine to do on commit.

  • alexpott committed 10dce7b on 10.0.x
    Issue #3112283 by ravi.shankar, mpdonadio, andregp, daffie, jhedstrom,...

  • alexpott committed debf702 on 9.5.x
    Issue #3112283 by ravi.shankar, mpdonadio, andregp, daffie, jhedstrom,...

  • alexpott committed 960480f on 9.4.x
    Issue #3112283 by ravi.shankar, mpdonadio, andregp, daffie, jhedstrom,...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.