Merge pull request #622 from ArthurHoaro/update-date

Save link update dates and render it in templates and feeds
This commit is contained in:
Arthur 2016-10-12 14:51:37 +02:00 committed by GitHub
commit 0354257266
8 changed files with 74 additions and 16 deletions

View file

@ -154,17 +154,23 @@ protected function buildItem($link, $pageaddr)
} }
$link['description'] = format_description($link['description']) . PHP_EOL .'<br>&#8212; '. $permalink; $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) { // atom:entry elements MUST contain exactly one atom:updated element.
$link['iso_date'] = $date->format(DateTime::RSS); if (!empty($link['updated'])) {
$upDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['updated']);
$link['up_iso_date'] = $this->getIsoDate($upDate, DateTime::ATOM);
} else { } else {
$link['iso_date'] = $date->format(DateTime::ATOM); $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime::ATOM);;
} }
// Save the more recent item. // Save the more recent item.
if (empty($this->latestDate) || $this->latestDate < $date) { if (empty($this->latestDate) || $this->latestDate < $pubDate) {
$this->latestDate = $date; $this->latestDate = $pubDate;
}
if (!empty($upDate) && $this->latestDate < $upDate) {
$this->latestDate = $upDate;
} }
$taglist = array_filter(explode(' ', $link['tags']), 'strlen'); $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
@ -249,6 +255,26 @@ protected function getLatestDateFormatted()
return $this->latestDate->format($type); 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. * Returns the number of link to display according to 'nb' user input parameter.
* *

View file

@ -12,8 +12,9 @@
* *
* Available keys: * Available keys:
* - description: description of the entry * - description: description of the entry
* - linkdate: date of the creation of this entry, in the form YYYYMMDD_HHMMSS * - linkdate: creation date of this entry, format: YYYYMMDD_HHMMSS
* (e.g.'20110914_192317') * (e.g.'20110914_192317')
* - updated: last modification date of this entry, format: YYYYMMDD_HHMMSS
* - private: Is this link private? 0=no, other value=yes * - private: Is this link private? 0=no, other value=yes
* - tags: tags attached to this entry (separated by spaces) * - tags: tags attached to this entry (separated by spaces)
* - title Title of the link * - title Title of the link

View file

@ -1245,6 +1245,9 @@ function renderPage($conf, $pluginManager)
// -------- User clicked the "Save" button when editing a link: Save link to database. // -------- User clicked the "Save" button when editing a link: Save link to database.
if (isset($_POST['save_edit'])) if (isset($_POST['save_edit']))
{ {
$linkdate = $_POST['lf_linkdate'];
$updated = isset($LINKSDB[$linkdate]) ? strval(date('Ymd_His')) : false;
// Go away! // Go away!
if (! tokenOk($_POST['token'])) { if (! tokenOk($_POST['token'])) {
die('Wrong token.'); die('Wrong token.');
@ -1255,7 +1258,7 @@ function renderPage($conf, $pluginManager)
$tags = preg_replace('/(^| )\-/', '$1', $tags); $tags = preg_replace('/(^| )\-/', '$1', $tags);
// Remove duplicates. // Remove duplicates.
$tags = implode(' ', array_unique(explode(' ', $tags))); $tags = implode(' ', array_unique(explode(' ', $tags)));
$linkdate = $_POST['lf_linkdate'];
$url = trim($_POST['lf_url']); $url = trim($_POST['lf_url']);
if (! startsWith($url, 'http:') && ! startsWith($url, 'https:') if (! startsWith($url, 'http:') && ! startsWith($url, 'https:')
&& ! startsWith($url, 'ftp:') && ! startsWith($url, 'magnet:') && ! startsWith($url, 'ftp:') && ! startsWith($url, 'magnet:')
@ -1270,6 +1273,7 @@ function renderPage($conf, $pluginManager)
'description' => $_POST['lf_description'], 'description' => $_POST['lf_description'],
'private' => (isset($_POST['lf_private']) ? 1 : 0), 'private' => (isset($_POST['lf_private']) ? 1 : 0),
'linkdate' => $linkdate, 'linkdate' => $linkdate,
'updated' => $updated,
'tags' => str_replace(',', ' ', $tags) 'tags' => str_replace(',', ' ', $tags)
); );
// If title is empty, use the URL as title. // If title is empty, use the URL as title.
@ -1633,6 +1637,12 @@ function buildLinkList($PAGE,$LINKSDB, $conf, $pluginManager)
$link['class'] = $link['private'] == 0 ? $classLi : 'private'; $link['class'] = $link['private'] == 0 ? $classLi : 'private';
$date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']); $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
$link['timestamp'] = $date->getTimestamp(); $link['timestamp'] = $date->getTimestamp();
if (! empty($link['updated'])) {
$date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['updated']);
$link['updated_timestamp'] = $date->getTimestamp();
} else {
$link['updated_timestamp'] = '';
}
$taglist = explode(' ', $link['tags']); $taglist = explode(' ', $link['tags']);
uasort($taglist, 'strcasecmp'); uasort($taglist, 'strcasecmp');
$link['taglist'] = $taglist; $link['taglist'] = $taglist;

View file

@ -76,7 +76,7 @@ public function testRSSBuildData()
// Test headers (RSS) // Test headers (RSS)
$this->assertEquals(self::$RSS_LANGUAGE, $data['language']); $this->assertEquals(self::$RSS_LANGUAGE, $data['language']);
$this->assertEmpty($data['pubsubhub_url']); $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(true, $data['show_dates']);
$this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']); $this->assertEquals('http://host.tld/index.php?do=feed', $data['self_link']);
$this->assertEquals('http://host.tld/', $data['index_url']); $this->assertEquals('http://host.tld/', $data['index_url']);
@ -88,7 +88,10 @@ public function testRSSBuildData()
$this->assertEquals('20150310_114651', $link['linkdate']); $this->assertEquals('20150310_114651', $link['linkdate']);
$this->assertEquals('http://host.tld/?WDWyig', $link['guid']); $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
$this->assertEquals('http://host.tld/?WDWyig', $link['url']); $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('Stallman has a beard', $link['description']);
$this->assertContains('Permalink', $link['description']); $this->assertContains('Permalink', $link['description']);
$this->assertContains('http://host.tld/?WDWyig', $link['description']); $this->assertContains('http://host.tld/?WDWyig', $link['description']);
@ -101,6 +104,9 @@ public function testRSSBuildData()
// Test multitags. // Test multitags.
$this->assertEquals(5, count($data['links']['20141125_084734']['taglist'])); $this->assertEquals(5, count($data['links']['20141125_084734']['taglist']));
$this->assertEquals('css', $data['links']['20141125_084734']['taglist'][0]); $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 @@ public function testAtomBuildData()
$feedBuilder->setLocale(self::$LOCALE); $feedBuilder->setLocale(self::$LOCALE);
$data = $feedBuilder->buildData(); $data = $feedBuilder->buildData();
$this->assertEquals(ReferenceLinkDB::$NB_LINKS_TOTAL, count($data['links'])); $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']); $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']);
} }
/** /**

View file

@ -30,7 +30,8 @@ function __construct()
'Richard Stallman and the Free Software Revolution. Read this. #hashtag', 'Richard Stallman and the Free Software Revolution. Read this. #hashtag',
0, 0,
'20150310_114633', '20150310_114633',
'free gnu software stallman -exclude stuff hashtag' 'free gnu software stallman -exclude stuff hashtag',
'20160803_093033'
); );
$this->addLink( $this->addLink(
@ -82,7 +83,7 @@ function __construct()
/** /**
* Adds a new link * 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( $link = array(
'title' => $title, 'title' => $title,
@ -91,6 +92,7 @@ protected function addLink($title, $url, $description, $private, $date, $tags)
'private' => $private, 'private' => $private,
'linkdate' => $date, 'linkdate' => $date,
'tags' => $tags, 'tags' => $tags,
'updated' => $updated,
); );
$this->_links[$date] = $link; $this->_links[$date] = $link;

View file

@ -27,7 +27,8 @@
{/if} {/if}
<id>{$value.guid}</id> <id>{$value.guid}</id>
{if="$show_dates"} {if="$show_dates"}
<updated>{$value.iso_date}</updated> <published>{$value.pub_iso_date}</published>
<updated>{$value.up_iso_date}</updated>
{/if} {/if}
<content type="html" xml:lang="{$language}"> <content type="html" xml:lang="{$language}">
<![CDATA[{$value.description}]]> <![CDATA[{$value.description}]]>

View file

@ -22,7 +22,8 @@
<link>{$value.url}</link> <link>{$value.url}</link>
{/if} {/if}
{if="$show_dates"} {if="$show_dates"}
<pubDate>{$value.iso_date}</pubDate> <pubDate>{$value.pub_iso_date}</pubDate>
<atom:modified>{$value.up_iso_date}</atom:modified>
{/if} {/if}
<description><![CDATA[{$value.description}]]></description> <description><![CDATA[{$value.description}]]></description>
{loop="$value.taglist"} {loop="$value.taglist"}

View file

@ -89,7 +89,16 @@
<br> <br>
{if="$value.description"}<div class="linkdescription">{$value.description}</div>{/if} {if="$value.description"}<div class="linkdescription">{$value.description}</div>{/if}
{if="!$hide_timestamps || isLoggedIn()"} {if="!$hide_timestamps || isLoggedIn()"}
<span class="linkdate" title="Permalink"><a href="?{$value.linkdate|smallHash}">{function="strftime('%c', $value.timestamp)"} - permalink</a> - </span> {$updated=$value.updated_timestamp ? 'Edited: '. strftime('%c', $value.updated_timestamp) : 'Permalink'}
<span class="linkdate" title="Permalink">
<a href="?{$value.linkdate|smallHash}">
<span title="{$updated}">
{function="strftime('%c', $value.timestamp)"}
{if="$value.updated_timestamp"}*{/if}
</span>
- permalink
</a> -
</span>
{else} {else}
<span class="linkdate" title="Short link here"><a href="?{$value.shorturl}">permalink</a> - </span> <span class="linkdate" title="Short link here"><a href="?{$value.shorturl}">permalink</a> - </span>
{/if} {/if}