Problem/Motivation

Working on #3239552-9: Improve hash() calculation for PHP 8.1 faced that the source_string property undeclared in entity class

Steps to reproduce

See usage in \Drupal\aggregator\ItemsImporter::refresh()

...
    // We store the hash of feed data in the database. When refreshing a
    // feed we compare stored hash and new hash calculated from downloaded
    // data. If both are equal we say that feed is not updated.
    $hash = $success ? hash('sha256', $feed->source_string) : '';
...

But in \Drupal\aggregator\Plugin\aggregator\fetcher\DefaultFetcher::fetch() the variable is set to FALSE

  public function fetch(FeedInterface $feed) {
    $request = new Request('GET', $feed->getUrl());
    $feed->source_string = FALSE;
...
       if ($response->hasHeader('Last-Modified')) {
         $feed->setLastModified(strtotime($response->getHeaderLine('Last-Modified')));
       }
       $feed->http_headers = $response->getHeaders();

Proposed resolution

- Declare property as string and fix initial value.
- remove usage of http_headers as no core/contrib usage found

Remaining tasks

- consensus
- review
- commit

User interface changes

no

API changes

property is declared

Data model changes

no

Release notes snippet

no

Comments

andypost created an issue. See original summary.

andypost’s picture

Status: Active » Needs review
StatusFileSize
new1.19 KB

MVP

andypost’s picture

Title: Add the undeclared variable $source_string to the Feed entity class » Add the undeclared variables $source_string and $http_headers to the Feed entity class

There's also $http_headers undeclared

andypost’s picture

Issue summary: View changes
StatusFileSize
new742 bytes
new1.57 KB

There's no usage in contrib of headers http://grep.xnddx.ru/search?text=-%3Ehttp_headers&filename=

So I thing better to remove unused property

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.

quietone’s picture

Project: Drupal core » Aggregator
Version: 9.4.x-dev » 1.x-dev
Component: aggregator.module » Code

The aggregator module has been removed from Core in 10.0.x-dev and now lives on as a contrib module. Issues in the Core queue about the aggregator module, like this one, have been moved to the contrib module queue.

larowlan credited Risse.

larowlan’s picture

larowlan’s picture

Version: 1.x-dev » 2.x-dev
Issue tags: -Aggregator 2.x
larowlan’s picture

+++ b/core/modules/aggregator/src/Entity/Feed.php
@@ -55,6 +55,14 @@
+  /**
+   * The last fetched content of the feed.
+   *
+   * @var string
+   *   The content of feed response.
+   */
+  public $source_string = '';

should we make this protected and then add a __get and trigger a deprecation warning?

rajandro’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

As per #6

The aggregator module has been removed from Core in 10.0.x-dev and now lives on as a contrib module.

- this patch needs reroll.

immaculatexavier’s picture

Status: Needs work » Needs review
StatusFileSize
new1.46 KB
new2.18 KB

Addressed #13
Rerolled patch against #4 for 2.x-dev in in 10.0.x-dev.

Kindly review.

larowlan’s picture

Status: Needs review » Needs work
Issue tags: -Needs reroll

Needs work for #12

Tests are fixed in HEAD for the 2.x branch

dcam’s picture

Title: Add the undeclared variables $source_string and $http_headers to the Feed entity class » Add undeclared variables to the Feed entity class

RE: #12

This also applies to the undeclared $feed->items property.

I'm sensing some code smell from these undeclared variables. It's more than just a bad practice in general. They obfuscate dependencies of the parser and processor plugins. Why doesn't fetch() return the body of the response instead of assigning it to $feed->source_string? Why doesn't parse() return the items instead of assigning it to $feed->items? I do not like using an entity class like this.

I want to remove the use of these variables altogether. But doing that would break backward compatibility by changing the plugin APIs. I don't know that I want to bother with releasing version 3 right now. Also, I feel like we should get a version that doesn't trigger deprecation notices in PHP 8.2 out sooner rather than later.

Suggested plan:

  • Add the three variables as protected to the Feed class.
  • Add the __get function to return the variables. Have the function trigger a deprecation notice stating that they will be removed in version 3.0.0.
  • Open issues for removing the three variables and changing the plugin APIs to not use them. I suggest keeping the issues for each variable separate.

Please provide feedback.

dcam’s picture

Status: Needs work » Needs review
StatusFileSize
new4.38 KB
new3.4 KB

Here's a patch for the plan I outlined in #16. Though I decided to not do anything with $http_headers and just let it be deleted as in the original patch. This may not be the correct thing to do. Feel free to comment.

Status: Needs review » Needs work

The last submitted patch, 17: 3239690-17.patch, failed testing. View results

dcam’s picture

StatusFileSize
new5.65 KB
new1.56 KB

An empty($feed->items); call in the ItemsImporter caused the failures above. empty() calls the __isset() magic method, which hadn't been implemented. I could have rewritten that line to check for the existence of items in the array a different way, but I figured a more complete solution is to implement __isset().

I also removed a couple of other dynamic variable assignments in AggregatorTestBase. One was for $feed->items, but used it in a completely different way than the importers. The other was for yet another new dynamic variable. Since it's only in a test I've chosen to not add it to the Feed class.

dcam’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 19: 3239690-19.patch, failed testing. View results

dcam’s picture

Status: Needs work » Needs review
StatusFileSize
new7.8 KB
new2.71 KB

My change to remove those additional dynamic variables in the test base broke two of the tests. They were actually using that badly-assigned items variable. So I had to write it out of those other tests.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/src/Entity/Feed.php
@@ -55,6 +55,67 @@ use Drupal\aggregator\FeedInterface;
+    if ($name == 'items') {
+      @trigger_error('The $items property is deprecated in 2.1.0 and will be removed from 3.0.0. See https://www.drupal.org/project/aggregator/issues/3239690.', E_USER_DEPRECATED);
+      $this->items = $value;
+    }
+    elseif ($name == 'source_string') {
+      @trigger_error('The $source_string property is deprecated in 2.1.0 and will be removed from 3.0.0. See https://www.drupal.org/project/aggregator/issues/3239690.', E_USER_DEPRECATED);
+      $this->source_string = $value;
+    }
+    else {
+      parent::__set($name, $value);
+    }

Ubernit, but personally I see elseif (and else) as a code smell.

We can return early here and just use if.

I think we're supposed to link to a change record rather than an issue node - we can create one here: https://www.drupal.org/node/add/changenotice?field_project=3262413&field...

Not sure this is worth fixing, feel free to ignore on both counts

dcam’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new7.67 KB
new3.47 KB

Here's the change record: https://www.drupal.org/node/3386012.

I also edited the __set() function as suggested.

  • dcam committed 5424777f on 2.x
    Issue #3239690 by dcam, andypost, immaculatexavier, ParisLiakos,...

  • dcam committed c00d0077 on 1.x
    Issue #3239690 by dcam, andypost, immaculatexavier, ParisLiakos,...
dcam’s picture

Status: Needs review » Fixed

Thank you to everyone who worked on this issue and the one that was closed as a duplicate!

Status: Fixed » Closed (fixed)

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