From 1c0147a7894fd25c5b69ae83e87784de66c7edbb Mon Sep 17 00:00:00 2001 From: Herb Date: Mon, 11 Jul 2022 16:47:07 -0400 Subject: [PATCH] Issue #112: remove deprecated FeedsDateTimeElement and FeedsDateTime classes (#113) --- feeds.module | 2 - mappers/date.inc | 2 +- plugins/FeedsParser.inc | 394 +------------------------------------ tests/feeds.tests.info | 6 - tests/feeds_date_time.test | 61 ------ 5 files changed, 3 insertions(+), 462 deletions(-) delete mode 100644 tests/feeds_date_time.test diff --git a/feeds.module b/feeds.module index 2bb3530..229e9c5 100644 --- a/feeds.module +++ b/feeds.module @@ -1687,8 +1687,6 @@ function feeds_autoload_info() { 'FeedsTermElement' => 'plugins/FeedsParser.inc', 'FeedsGeoTermElement' => 'plugins/FeedsParser.inc', 'FeedsEnclosure' => 'plugins/FeedsParser.inc', - 'FeedsDateTimeElement' => 'plugins/FeedsParser.inc', - 'FeedsDateTime' => 'plugins/FeedsParser.inc', 'FeedsPlugin' => 'plugins/FeedsPlugin.inc', 'FeedsMissingPlugin' => 'plugins/FeedsPlugin.inc', 'FeedsProcessor' => 'plugins/FeedsProcessor.inc', diff --git a/mappers/date.inc b/mappers/date.inc index 11bed06..be5e312 100644 --- a/mappers/date.inc +++ b/mappers/date.inc @@ -183,7 +183,7 @@ function _date_feeds_get_date_object($value, DateTimeZone $default_tz) { return $value; } - // Convert DateTime and FeedsDateTime. + // Convert DateTime. if ($value instanceof DateTime) { if (!$value->getTimezone() || !preg_match('/[a-zA-Z]/', $value->getTimezone()->getName())) { $value->setTimezone($default_tz); diff --git a/plugins/FeedsParser.inc b/plugins/FeedsParser.inc index 62b72dc..d6e5192 100644 --- a/plugins/FeedsParser.inc +++ b/plugins/FeedsParser.inc @@ -529,397 +529,11 @@ class FeedsEnclosure extends FeedsElement { } -/** - * Defines a date element of a parsed result (including ranges, repeat). - * - * @deprecated This is no longer in use and will not be maintained. - */ -class FeedsDateTimeElement extends FeedsElement { - - /** - * Start date and end date. - */ - public $start; - public $end; - - /** - * Constructor. - * - * @param $start - * A FeedsDateTime object or a date as accepted by FeedsDateTime. - * @param $end - * A FeedsDateTime object or a date as accepted by FeedsDateTime. - * @param $tz - * A PHP DateTimeZone object. - */ - public function __construct($start = NULL, $end = NULL, $tz = NULL) { - $this->start = (!isset($start) || ($start instanceof FeedsDateTime)) ? $start : new FeedsDateTime($start, $tz); - $this->end = (!isset($end) || ($end instanceof FeedsDateTime)) ? $end : new FeedsDateTime($end, $tz); - } - - /** - * Override FeedsElement::getValue(). - * - * @return - * The UNIX timestamp of this object's start date. Return value is - * technically a string but will only contain numeric values. - */ - public function getValue() { - if ($this->start) { - return $this->start->format('U'); - } - return '0'; - } - - /** - * Merge this field with another. Most stuff goes down when merging the two - * sub-dates. - * - * @see FeedsDateTime - */ - public function merge(FeedsDateTimeElement $other) { - $this2 = clone $this; - if ($this->start && $other->start) { - $this2->start = $this->start->merge($other->start); - } - elseif ($other->start) { - $this2->start = clone $other->start; - } - elseif ($this->start) { - $this2->start = clone $this->start; - } - - if ($this->end && $other->end) { - $this2->end = $this->end->merge($other->end); - } - elseif ($other->end) { - $this2->end = clone $other->end; - } - elseif ($this->end) { - $this2->end = clone $this->end; - } - return $this2; - } - - /** - * Helper method for buildDateField(). Build a FeedsDateTimeElement object - * from a standard formatted node. - */ - protected static function readDateField($entity, $field_name, $delta = 0, $language = LANGUAGE_NONE) { - $ret = new FeedsDateTimeElement(); - if (isset($entity->{$field_name}[$language][$delta]['date']) && $entity->{$field_name}[$language][$delta]['date'] instanceof FeedsDateTime) { - $ret->start = $entity->{$field_name}[$language][$delta]['date']; - } - if (isset($entity->{$field_name}[$language][$delta]['date2']) && $entity->{$field_name}[$language][$delta]['date2'] instanceof FeedsDateTime) { - $ret->end = $entity->{$field_name}[$language][$delta]['date2']; - } - return $ret; - } - - /** - * Build a entity's date field from our object. - * - * @param object $entity - * The entity to build the date field on. - * @param string $field_name - * The name of the field to build. - * @param int $delta - * The delta in the field. - */ - public function buildDateField($entity, $field_name, $delta = 0, $language = LANGUAGE_NONE) { - $info = field_info_field($field_name); - - $oldfield = FeedsDateTimeElement::readDateField($entity, $field_name, $delta, $language); - // Merge with any preexisting objects on the field; we take precedence. - $oldfield = $this->merge($oldfield); - $use_start = $oldfield->start; - $use_end = $oldfield->end; - - // Set timezone if not already in the FeedsDateTime object. - $to_tz = date_get_timezone($info['settings']['tz_handling'], date_default_timezone()); - $temp = new FeedsDateTime(NULL, new DateTimeZone($to_tz)); - - $db_tz = ''; - if ($use_start) { - $use_start = $use_start->merge($temp); - if (!date_timezone_is_valid($use_start->getTimezone()->getName())) { - $use_start->setTimezone(new DateTimeZone("UTC")); - } - $db_tz = date_get_timezone_db($info['settings']['tz_handling'], $use_start->getTimezone()->getName()); - } - if ($use_end) { - $use_end = $use_end->merge($temp); - if (!date_timezone_is_valid($use_end->getTimezone()->getName())) { - $use_end->setTimezone(new DateTimeZone("UTC")); - } - if (!$db_tz) { - $db_tz = date_get_timezone_db($info['settings']['tz_handling'], $use_end->getTimezone()->getName()); - } - } - if (!$db_tz) { - return; - } - - $db_tz = new DateTimeZone($db_tz); - if (!isset($entity->{$field_name})) { - $entity->{$field_name} = array($language => array()); - } - if ($use_start) { - $entity->{$field_name}[$language][$delta]['timezone'] = $use_start->getTimezone()->getName(); - $entity->{$field_name}[$language][$delta]['offset'] = $use_start->getOffset(); - $use_start->setTimezone($db_tz); - $entity->{$field_name}[$language][$delta]['date'] = $use_start; - /** - * @todo the date_type_format line could be simplified based upon a patch - * DO issue #259308 could affect this, follow up on at some point. - * Without this, all granularity info is lost. - * $use_start->format(date_type_format($field['type'], $use_start->granularity)); - */ - $entity->{$field_name}[$language][$delta]['value'] = $use_start->format(date_type_format($info['type'])); - } - if ($use_end) { - // Don't ever use end to set timezone (for now) - $entity->{$field_name}[$language][$delta]['offset2'] = $use_end->getOffset(); - $use_end->setTimezone($db_tz); - $entity->{$field_name}[$language][$delta]['date2'] = $use_end; - $entity->{$field_name}[$language][$delta]['value2'] = $use_end->format(date_type_format($info['type'])); - } - } - -} - -/** - * Extend PHP DateTime class with granularity handling, merge functionality and - * slightly more flexible initialization parameters. - * - * This class is a Backdrop independent extension of the >= PHP 5.2 DateTime - * class. - * - * @see FeedsDateTimeElement - * - * @deprecated Use BackdropDateTime instead. - */ -class FeedsDateTime extends DateTime { - public $granularity = array(); - protected static $allgranularity = array('year', 'month', 'day', 'hour', 'minute', 'second', 'zone'); - private $_serialized_time; - private $_serialized_timezone; - - /** - * The original time value passed into the constructor. - * - * @var mixed - */ - protected $originalValue; - - /** - * Overridden constructor. - * - * @param $time - * time string, flexible format including timestamp. Invalid formats will - * fall back to 'now'. - * @param $tz - * PHP DateTimeZone object, NULL allowed - */ - public function __construct($time = '', $tz = NULL) { - $this->originalValue = $time; - - if (is_numeric($time)) { - // Assume UNIX timestamp if it doesn't look like a simple year. - if (strlen($time) > 4) { - $time = "@" . $time; - } - // If it's a year, add a default month too, because PHP's date functions - // won't parse standalone years after 2000 correctly (see explanation at - // http://aaronsaray.com/blog/2007/07/11/helpful-strtotime-reminders/#comment-47). - else { - $time = 'January ' . $time; - } - } - - // PHP < 5.3 doesn't like the GMT- notation for parsing timezones. - $time = str_replace("GMT-", "-", $time); - $time = str_replace("GMT+", "+", $time); - - // Some PHP 5.2 version's DateTime class chokes on invalid dates. - if (!date_create($time)) { - $time = 'now'; - } - - // Create and set time zone separately, PHP 5.2.6 does not respect time zone - // argument in __construct(). - parent::__construct($time); - $tz = $tz ? $tz : new DateTimeZone("UTC"); - $this->setTimeZone($tz); - - // Verify that timezone has not been specified as an offset. - if (!preg_match('/[a-zA-Z]/', $this->getTimezone()->getName())) { - $this->setTimezone(new DateTimeZone("UTC")); - } - - // Finally set granularity. - $this->setGranularityFromTime($time, $tz); - } - - /** - * Helper function to prepare the object during serialization. - * - * We are extending a core class and core classes cannot be serialized. - * - * Ref: http://bugs.php.net/41334, http://bugs.php.net/39821 - */ - public function __sleep() { - $this->_serialized_time = $this->format('c'); - $this->_serialized_timezone = $this->getTimezone()->getName(); - return array('_serialized_time', '_serialized_timezone'); - } - - /** - * Upon unserializing, we must re-build ourselves using local variables. - */ - public function __wakeup() { - $this->__construct($this->_serialized_time, new DateTimeZone($this->_serialized_timezone)); - } - - /** - * Returns the string representation. - * - * Will try to use the literal input, if that is a string. Falls back to - * ISO-8601. - * - * @return string - * The string version of this DateTime object. - */ - public function __toString() { - if (is_scalar($this->originalValue)) { - return (string) $this->originalValue; - } - - return $this->format('Y-m-d\TH:i:sO'); - } - - /** - * This function will keep this object's values by default. - */ - public function merge(FeedsDateTime $other) { - $other_tz = $other->getTimezone(); - $this_tz = $this->getTimezone(); - // Figure out which timezone to use for combination. - $use_tz = ($this->hasGranularity('zone') || !$other->hasGranularity('zone')) ? $this_tz : $other_tz; - - $this2 = clone $this; - $this2->setTimezone($use_tz); - $other->setTimezone($use_tz); - $val = $this2->toArray(); - $otherval = $other->toArray(); - foreach (self::$allgranularity as $g) { - if ($other->hasGranularity($g) && !$this2->hasGranularity($g)) { - // The other class has a property we don't; steal it. - $this2->addGranularity($g); - $val[$g] = $otherval[$g]; - } - } - $other->setTimezone($other_tz); - - $this2->setDate($val['year'], $val['month'], $val['day']); - $this2->setTime($val['hour'], $val['minute'], $val['second']); - return $this2; - } - - /** - * Overrides default DateTime function. Only changes output values if - * actually had time granularity. This should be used as a "converter" for - * output, to switch tzs. - * - * In order to set a timezone for a datetime that doesn't have such - * granularity, merge() it with one that does. - */ - public function setTimezone($tz, $force = FALSE) { - // PHP 5.2.6 has a fatal error when setting a date's timezone to itself. - // http://bugs.php.net/bug.php?id=45038 - if (version_compare(PHP_VERSION, '5.2.7', '<') && $tz == $this->getTimezone()) { - $tz = new DateTimeZone($tz->getName()); - } - - if (!$this->hasTime() || !$this->hasGranularity('zone') || $force) { - // This has no time or timezone granularity, so timezone doesn't mean much - // We set the timezone using the method, which will change the day/hour, but then we switch back. - $arr = $this->toArray(); - parent::setTimezone($tz); - $this->setDate($arr['year'], $arr['month'], $arr['day']); - $this->setTime($arr['hour'], $arr['minute'], $arr['second']); - return; - } - parent::setTimezone($tz); - } - - /** - * Safely adds a granularity entry to the array. - */ - public function addGranularity($g) { - $this->granularity[] = $g; - $this->granularity = array_unique($this->granularity); - } - - /** - * Removes a granularity entry from the array. - */ - public function removeGranularity($g) { - if ($key = array_search($g, $this->granularity)) { - unset($this->granularity[$key]); - } - } - - /** - * Checks granularity array for a given entry. - */ - public function hasGranularity($g) { - return in_array($g, $this->granularity); - } - - /** - * Returns whether this object has time set. Used primarily for timezone - * conversion and formatting. - * - * @todo currently very simplistic, but effective, see usage - */ - public function hasTime() { - return $this->hasGranularity('hour'); - } - - /** - * Protected function to find the granularity given by the arguments to the - * constructor. - */ - protected function setGranularityFromTime($time, $tz) { - $this->granularity = array(); - $temp = date_parse($time); - // This PHP method currently doesn't have resolution down to seconds, so if - // there is some time, all will be set. - foreach (self::$allgranularity as $g) { - if ((isset($temp[$g]) && is_numeric($temp[$g])) || ($g == 'zone' && (isset($temp['zone_type']) && $temp['zone_type'] > 0))) { - $this->granularity[] = $g; - } - } - if ($tz) { - $this->addGranularity('zone'); - } - } - - /** - * Helper to return all standard date parts in an array. - */ - protected function toArray() { - return array('year' => $this->format('Y'), 'month' => $this->format('m'), 'day' => $this->format('d'), 'hour' => $this->format('H'), 'minute' => $this->format('i'), 'second' => $this->format('s'), 'zone' => $this->format('e')); - } - -} - /** * Converts to UNIX time. * - * @param $date - * A date that is either a string, a FeedsDateTimeElement or a UNIX timestamp. + * @param string|BackdropDateTime $date + * A date that is either a string, a BackdropDateTime object, or a UNIX timestamp. * @param $default_value * A default UNIX timestamp to return if $date could not be parsed. * @@ -931,10 +545,6 @@ function feeds_to_unixtime($date, $default_value) { return $date; } - if ($date instanceof FeedsDateTimeElement) { - return $date->getValue(); - } - if (is_string($date) || is_object($date) && method_exists($date, '__toString')) { if ($date_object = date_create(trim($date))) { return $date_object->format('U'); diff --git a/tests/feeds.tests.info b/tests/feeds.tests.info index e09466b..9d56847 100644 --- a/tests/feeds.tests.info +++ b/tests/feeds.tests.info @@ -22,12 +22,6 @@ description = Tests behavior for when an importer is attached to a content type. group = Feeds file = feeds_content_type.test -[FeedsDateTimeTest] -name = FeedsDateTimeTest -description = TBD -group = Feeds -file = feeds_date_time.test - [FeedsFileFetcherTestCase] name = FeedsFileFetcherTestCase description = TBD diff --git a/tests/feeds_date_time.test b/tests/feeds_date_time.test deleted file mode 100644 index 415d642..0000000 --- a/tests/feeds_date_time.test +++ /dev/null @@ -1,61 +0,0 @@ - 'FeedsDateTime unit tests', - 'description' => 'Unit tests for Feeds date handling.', - 'group' => 'Feeds', - ); - } - - /** - * {@inheritdoc} - */ - public function setUp() { - parent::setUp(); - module_load_include('inc', 'feeds', 'plugins/FeedsParser'); - } - - /** - * Dispatch tests, only use one entry point method testX to save time. - */ - public function test() { - $date = new FeedsDateTime('2010-20-12'); - $this->assertTrue(is_numeric($date->format('U'))); - $date = new FeedsDateTime('created'); - $this->assertTrue(is_numeric($date->format('U'))); - $date = new FeedsDateTime('12/3/2009 20:00:10'); - $this->assertTrue(is_numeric($date->format('U'))); - - // Check that years above 2000 work correctly. - $date1 = new FeedsDateTime(2012); - $date2 = new FeedsDateTime('January 2012'); - $this->assertEqual($date1->format('U'), $date2->format('U')); - - // Check that years before 1902 work correctly. - $early_date_string = '01/02/1901'; - $date = new FeedsDateTime($early_date_string); - $this->assertEqual($date->format('m/d/Y'), $early_date_string); - } - -}