]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Set updated date for items in feeds
authorArthurHoaro <arthur@hoa.ro>
Wed, 3 Aug 2016 07:45:28 +0000 (09:45 +0200)
committerArthurHoaro <arthur@hoa.ro>
Sat, 5 Nov 2016 13:29:53 +0000 (14:29 +0100)
RSS doesn't support updated date for items, so we use the ATOM extension.
Updated dates also bump the global update

application/FeedBuilder.php
tests/FeedBuilderTest.php
tests/utils/ReferenceLinkDB.php
tpl/feed.atom.html
tpl/feed.rss.html

index ddefe6ce93a83ed8f6869d1eee57941264731e72..58c6bb179a636688a648bab720e43930306d5f1a 100644 (file)
@@ -154,17 +154,23 @@ class FeedBuilder
         }
         $link['description'] = format_description($link['description']) . PHP_EOL .'<br>&#8212; '. $permalink;
 
-        $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
+        $pubDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
+        $link['pub_iso_date'] = $this->getIsoDate($pubDate);
 
-        if ($this->feedType == self::$FEED_RSS) {
-            $link['iso_date'] = $date->format(DateTime::RSS);
+        // atom:entry elements MUST contain exactly one atom:updated element.
+        if (!empty($link['updated'])) {
+            $upDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['updated']);
+            $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime::ATOM);
         } else {
-            $link['iso_date'] = $date->format(DateTime::ATOM);
+            $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime::ATOM);;
         }
 
         // Save the more recent item.
-        if (empty($this->latestDate) || $this->latestDate < $date) {
-            $this->latestDate = $date;
+        if (empty($this->latestDate) || $this->latestDate < $pubDate) {
+            $this->latestDate = $pubDate;
+        }
+        if (!empty($upDate) && $this->latestDate < $upDate) {
+            $this->latestDate = $upDate;
         }
 
         $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
@@ -249,6 +255,26 @@ class FeedBuilder
         return $this->latestDate->format($type);
     }
 
+    /**
+     * Get ISO date from DateTime according to feed type.
+     *
+     * @param DateTime    $date   Date to format.
+     * @param string|bool $format Force format.
+     *
+     * @return string Formatted date.
+     */
+    protected function getIsoDate(DateTime $date, $format = false)
+    {
+        if ($format !== false) {
+            return $date->format($format);
+        }
+        if ($this->feedType == self::$FEED_RSS) {
+            return $date->format(DateTime::RSS);
+
+        }
+        return $date->format(DateTime::ATOM);
+    }
+
     /**
      * Returns the number of link to display according to 'nb' user input parameter.
      *
index 460fb0c5a06299e45d8893895a6aa65c821b4344..c9ff397d153e928c87ef1276f22307af62b63607 100644 (file)
@@ -76,7 +76,7 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase
         // Test headers (RSS)
         $this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
         $this->assertEmpty($data['pubsubhub_url']);
-        $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $data['last_update']);
+        $this->assertRegExp('/Wed, 03 Aug 2016 09:30:33 \+\d{4}/', $data['last_update']);
         $this->assertEquals(true, $data['show_dates']);
         $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
         $this->assertEquals('http://host.tld/', $data['index_url']);
@@ -88,7 +88,10 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('20150310_114651', $link['linkdate']);
         $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
         $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
-        $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['iso_date']);
+        $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
+        $pub = DateTime::createFromFormat(DateTime::RSS, $link['pub_iso_date']);
+        $up  = DateTime::createFromFormat(DateTime::ATOM, $link['up_iso_date']);
+        $this->assertEquals($pub, $up);
         $this->assertContains('Stallman has a beard', $link['description']);
         $this->assertContains('Permalink', $link['description']);
         $this->assertContains('http://host.tld/?WDWyig', $link['description']);
@@ -101,6 +104,9 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase
         // Test multitags.
         $this->assertEquals(5, count($data['links']['20141125_084734']['taglist']));
         $this->assertEquals('css', $data['links']['20141125_084734']['taglist'][0]);
+
+        // Test update date
+        $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links']['20150310_114633']['up_iso_date']);
     }
 
     /**
@@ -112,8 +118,10 @@ class FeedBuilderTest extends PHPUnit_Framework_TestCase
         $feedBuilder->setLocale(self::$LOCALE);
         $data = $feedBuilder->buildData();
         $this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links']));
+        $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['last_update']);
         $link = array_shift($data['links']);
-        $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:+\d{2}/', $link['iso_date']);
+        $this->assertRegExp('/2015-03-10T11:46:51\+\d{2}:\d{2}/', $link['pub_iso_date']);
+        $this->assertRegExp('/2016-08-03T09:30:33\+\d{2}:\d{2}/', $data['links']['20150310_114633']['up_iso_date']);
     }
 
     /**
index fcc7a4f9f753ba3c4463fa798c0877932132a642..937961c8e3b488185f53f96380e44c5144ec8a78 100644 (file)
@@ -30,7 +30,8 @@ class ReferenceLinkDB
             'Richard Stallman and the Free Software Revolution. Read this. #hashtag',
             0,
             '20150310_114633',
-            'free gnu software stallman -exclude stuff hashtag'
+            'free gnu software stallman -exclude stuff hashtag',
+            '20160803_093033'
         );
 
         $this->addLink(
@@ -82,7 +83,7 @@ class ReferenceLinkDB
     /**
      * Adds a new link
      */
-    protected function addLink($title, $url, $description, $private, $date, $tags)
+    protected function addLink($title, $url, $description, $private, $date, $tags, $updated = '')
     {
         $link = array(
             'title' => $title,
@@ -91,6 +92,7 @@ class ReferenceLinkDB
             'private' => $private,
             'linkdate' => $date,
             'tags' => $tags,
+            'updated' => $updated,
         );
         $this->_links[$date] = $link;
 
index 2ebb162a2139439cd09b60267fcd2ea4e5c6f2b3..1932f507cf614178c605ab3ea6f236b3609e1037 100644 (file)
@@ -27,7 +27,8 @@
       {/if}
       <id>{$value.guid}</id>
       {if="$show_dates"}
-        <updated>{$value.iso_date}</updated>
+        <published>{$value.pub_iso_date}</published>
+        <updated>{$value.up_iso_date}</updated>
       {/if}
       <content type="html" xml:lang="{$language}">
         <![CDATA[{$value.description}]]>
index 26de7f19136f4d3f9b4bdc061377319d374bb5d0..4bfe4196c24abfdc6b4c94bda104adce5fe99824 100644 (file)
@@ -22,7 +22,8 @@
           <link>{$value.url}</link>
         {/if}
         {if="$show_dates"}
-          <pubDate>{$value.iso_date}</pubDate>
+          <pubDate>{$value.pub_iso_date}</pubDate>
+          <atom:modified>{$value.up_iso_date}</atom:modified>
         {/if}
         <description><![CDATA[{$value.description}]]></description>
         {loop="$value.taglist"}