aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2016-02-17 22:46:50 +0100
committerVirtualTam <virtualtam@flibidi.net>2016-02-17 22:46:50 +0100
commit205a42778dff04f1552e34ac2b7ade9e407ab1a0 (patch)
treefd21ec30198e1da936c4923f24a037369da811bb
parentbfec695df1205864b46ca7175e1598b184602687 (diff)
downloadShaarli-205a42778dff04f1552e34ac2b7ade9e407ab1a0.tar.gz
Shaarli-205a42778dff04f1552e34ac2b7ade9e407ab1a0.tar.zst
Shaarli-205a42778dff04f1552e34ac2b7ade9e407ab1a0.zip
cleanup: use DateTime to format dates
Closes #270 Modifications: - replace custom date parsing by DateTime calls - use proper date formatting for RSS feeds Deletions: - linkdate2timestamp() - linkdate2rfc822 - linkdate2iso8601 Signed-off-by: VirtualTam <virtualtam@flibidi.net>
-rw-r--r--application/LinkDB.php3
-rw-r--r--index.php65
-rw-r--r--tpl/dailyrss.html4
3 files changed, 29 insertions, 43 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php
index 9f4d3e3c..9488ac45 100644
--- a/application/LinkDB.php
+++ b/application/LinkDB.php
@@ -32,6 +32,9 @@ class LinkDB implements Iterator, Countable, ArrayAccess
32 // Links are stored as a PHP serialized string 32 // Links are stored as a PHP serialized string
33 private $_datastore; 33 private $_datastore;
34 34
35 // Link date storage format
36 const LINK_DATE_FORMAT = 'Ymd_His';
37
35 // Datastore PHP prefix 38 // Datastore PHP prefix
36 protected static $phpPrefix = '<?php /* '; 39 protected static $phpPrefix = '<?php /* ';
37 40
diff --git a/index.php b/index.php
index 3a63319c..12b6e04d 100644
--- a/index.php
+++ b/index.php
@@ -551,33 +551,6 @@ function getMaxFileSize()
551 return $maxsize; 551 return $maxsize;
552} 552}
553 553
554/* Converts a linkdate time (YYYYMMDD_HHMMSS) of an article to a timestamp (Unix epoch)
555 (used to build the ADD_DATE attribute in Netscape-bookmarks file)
556 PS: I could have used strptime(), but it does not exist on Windows. I'm too kind. */
557function linkdate2timestamp($linkdate)
558{
559 if(strcmp($linkdate, '_000000') !== 0 || !$linkdate){
560 $Y=$M=$D=$h=$m=$s=0;
561 $r = sscanf($linkdate,'%4d%2d%2d_%2d%2d%2d',$Y,$M,$D,$h,$m,$s);
562 return mktime($h,$m,$s,$M,$D,$Y);
563 }
564 return time();
565}
566
567/* Converts a linkdate time (YYYYMMDD_HHMMSS) of an article to a RFC822 date.
568 (used to build the pubDate attribute in RSS feed.) */
569function linkdate2rfc822($linkdate)
570{
571 return date('r',linkdate2timestamp($linkdate)); // 'r' is for RFC822 date format.
572}
573
574/* Converts a linkdate time (YYYYMMDD_HHMMSS) of an article to a ISO 8601 date.
575 (used to build the updated tags in ATOM feed.) */
576function linkdate2iso8601($linkdate)
577{
578 return date('c',linkdate2timestamp($linkdate)); // 'c' is for ISO 8601 date format.
579}
580
581// ------------------------------------------------------------------------------------------ 554// ------------------------------------------------------------------------------------------
582// Token management for XSRF protection 555// Token management for XSRF protection
583// Token should be used in any form which acts on data (create,update,delete,import...). 556// Token should be used in any form which acts on data (create,update,delete,import...).
@@ -769,14 +742,16 @@ function showRSS()
769 { 742 {
770 $link = $linksToDisplay[$keys[$i]]; 743 $link = $linksToDisplay[$keys[$i]];
771 $guid = $pageaddr.'?'.smallHash($link['linkdate']); 744 $guid = $pageaddr.'?'.smallHash($link['linkdate']);
772 $rfc822date = linkdate2rfc822($link['linkdate']); 745 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
773 $absurl = $link['url']; 746 $absurl = $link['url'];
774 if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute 747 if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute
775 if ($usepermalinks===true) 748 if ($usepermalinks===true)
776 echo '<item><title>'.$link['title'].'</title><guid isPermaLink="true">'.$guid.'</guid><link>'.$guid.'</link>'; 749 echo '<item><title>'.$link['title'].'</title><guid isPermaLink="true">'.$guid.'</guid><link>'.$guid.'</link>';
777 else 750 else
778 echo '<item><title>'.$link['title'].'</title><guid isPermaLink="false">'.$guid.'</guid><link>'.$absurl.'</link>'; 751 echo '<item><title>'.$link['title'].'</title><guid isPermaLink="false">'.$guid.'</guid><link>'.$absurl.'</link>';
779 if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) echo '<pubDate>'.escape($rfc822date)."</pubDate>\n"; 752 if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) {
753 echo '<pubDate>'.escape($date->format(DateTime::RSS))."</pubDate>\n";
754 }
780 if ($link['tags']!='') // Adding tags to each RSS entry (as mentioned in RSS specification) 755 if ($link['tags']!='') // Adding tags to each RSS entry (as mentioned in RSS specification)
781 { 756 {
782 foreach(explode(' ',$link['tags']) as $tag) { echo '<category domain="'.$pageaddr.'">'.$tag.'</category>'."\n"; } 757 foreach(explode(' ',$link['tags']) as $tag) { echo '<category domain="'.$pageaddr.'">'.$tag.'</category>'."\n"; }
@@ -857,8 +832,9 @@ function showATOM()
857 { 832 {
858 $link = $linksToDisplay[$keys[$i]]; 833 $link = $linksToDisplay[$keys[$i]];
859 $guid = $pageaddr.'?'.smallHash($link['linkdate']); 834 $guid = $pageaddr.'?'.smallHash($link['linkdate']);
860 $iso8601date = linkdate2iso8601($link['linkdate']); 835 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
861 $latestDate = max($latestDate,$iso8601date); 836 $iso8601date = $date->format(DateTime::ISO8601);
837 $latestDate = max($latestDate, $iso8601date);
862 $absurl = $link['url']; 838 $absurl = $link['url'];
863 if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute 839 if (startsWith($absurl,'?')) $absurl=$pageaddr.$absurl; // make permalink URL absolute
864 $entries.='<entry><title>'.$link['title'].'</title>'; 840 $entries.='<entry><title>'.$link['title'].'</title>';
@@ -866,7 +842,10 @@ function showATOM()
866 $entries.='<link href="'.$guid.'" /><id>'.$guid.'</id>'; 842 $entries.='<link href="'.$guid.'" /><id>'.$guid.'</id>';
867 else 843 else
868 $entries.='<link href="'.$absurl.'" /><id>'.$guid.'</id>'; 844 $entries.='<link href="'.$absurl.'" /><id>'.$guid.'</id>';
869 if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) $entries.='<updated>'.escape($iso8601date).'</updated>'; 845
846 if (!$GLOBALS['config']['HIDE_TIMESTAMPS'] || isLoggedIn()) {
847 $entries.='<updated>'.escape($iso8601date).'</updated>';
848 }
870 849
871 // Add permalink in description 850 // Add permalink in description
872 $descriptionlink = '(<a href="'.$guid.'">Permalink</a>)'; 851 $descriptionlink = '(<a href="'.$guid.'">Permalink</a>)';
@@ -972,8 +951,7 @@ function showDailyRSS() {
972 951
973 // For each day. 952 // For each day.
974 foreach ($days as $day => $linkdates) { 953 foreach ($days as $day => $linkdates) {
975 $daydate = linkdate2timestamp($day.'_000000'); // Full text date 954 $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000');
976 $rfc822date = linkdate2rfc822($day.'_000000');
977 $absurl = escape(index_url($_SERVER).'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page. 955 $absurl = escape(index_url($_SERVER).'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page.
978 956
979 // Build the HTML body of this RSS entry. 957 // Build the HTML body of this RSS entry.
@@ -986,7 +964,8 @@ function showDailyRSS() {
986 $l = $LINKSDB[$linkdate]; 964 $l = $LINKSDB[$linkdate];
987 $l['formatedDescription'] = format_description($l['description'], $GLOBALS['redirector']); 965 $l['formatedDescription'] = format_description($l['description'], $GLOBALS['redirector']);
988 $l['thumbnail'] = thumbnail($l['url']); 966 $l['thumbnail'] = thumbnail($l['url']);
989 $l['timestamp'] = linkdate2timestamp($l['linkdate']); 967 $l_date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $l['linkdate']);
968 $l['timestamp'] = $l_date->getTimestamp();
990 if (startsWith($l['url'], '?')) { 969 if (startsWith($l['url'], '?')) {
991 $l['url'] = index_url($_SERVER) . $l['url']; // make permalink URL absolute 970 $l['url'] = index_url($_SERVER) . $l['url']; // make permalink URL absolute
992 } 971 }
@@ -996,10 +975,10 @@ function showDailyRSS() {
996 // Then build the HTML for this day: 975 // Then build the HTML for this day:
997 $tpl = new RainTPL; 976 $tpl = new RainTPL;
998 $tpl->assign('title', $GLOBALS['title']); 977 $tpl->assign('title', $GLOBALS['title']);
999 $tpl->assign('daydate', $daydate); 978 $tpl->assign('daydate', $dayDate->getTimestamp());
1000 $tpl->assign('absurl', $absurl); 979 $tpl->assign('absurl', $absurl);
1001 $tpl->assign('links', $links); 980 $tpl->assign('links', $links);
1002 $tpl->assign('rfc822date', escape($rfc822date)); 981 $tpl->assign('rssdate', escape($dayDate->format(DateTime::RSS)));
1003 $html = $tpl->draw('dailyrss', $return_string=true); 982 $html = $tpl->draw('dailyrss', $return_string=true);
1004 983
1005 echo $html . PHP_EOL; 984 echo $html . PHP_EOL;
@@ -1055,7 +1034,8 @@ function showDaily($pageBuilder)
1055 $linksToDisplay[$key]['taglist']=$taglist; 1034 $linksToDisplay[$key]['taglist']=$taglist;
1056 $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $GLOBALS['redirector']); 1035 $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $GLOBALS['redirector']);
1057 $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']); 1036 $linksToDisplay[$key]['thumbnail'] = thumbnail($link['url']);
1058 $linksToDisplay[$key]['timestamp'] = linkdate2timestamp($link['linkdate']); 1037 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
1038 $linksToDisplay[$key]['timestamp'] = $date->getTimestamp();
1059 } 1039 }
1060 1040
1061 /* We need to spread the articles on 3 columns. 1041 /* We need to spread the articles on 3 columns.
@@ -1080,11 +1060,12 @@ function showDaily($pageBuilder)
1080 $fill[$index]+=$length; 1060 $fill[$index]+=$length;
1081 } 1061 }
1082 1062
1063 $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000');
1083 $data = array( 1064 $data = array(
1084 'linksToDisplay' => $linksToDisplay, 1065 'linksToDisplay' => $linksToDisplay,
1085 'linkcount' => count($LINKSDB), 1066 'linkcount' => count($LINKSDB),
1086 'cols' => $columns, 1067 'cols' => $columns,
1087 'day' => linkdate2timestamp($day.'_000000'), 1068 'day' => $dayDate->getTimestamp(),
1088 'previousday' => $previousday, 1069 'previousday' => $previousday,
1089 'nextday' => $nextday, 1070 'nextday' => $nextday,
1090 ); 1071 );
@@ -1799,7 +1780,8 @@ HTML;
1799 ($exportWhat=='private' && $link['private']!=0) || 1780 ($exportWhat=='private' && $link['private']!=0) ||
1800 ($exportWhat=='public' && $link['private']==0)) 1781 ($exportWhat=='public' && $link['private']==0))
1801 { 1782 {
1802 echo '<DT><A HREF="'.$link['url'].'" ADD_DATE="'.linkdate2timestamp($link['linkdate']).'" PRIVATE="'.$link['private'].'"'; 1783 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
1784 echo '<DT><A HREF="'.$link['url'].'" ADD_DATE="'.$date->getTimestamp().'" PRIVATE="'.$link['private'].'"';
1803 if ($link['tags']!='') echo ' TAGS="'.str_replace(' ',',',$link['tags']).'"'; 1785 if ($link['tags']!='') echo ' TAGS="'.str_replace(' ',',',$link['tags']).'"';
1804 echo '>'.$link['title']."</A>\n"; 1786 echo '>'.$link['title']."</A>\n";
1805 if ($link['description']!='') echo '<DD>'.$link['description']."\n"; 1787 if ($link['description']!='') echo '<DD>'.$link['description']."\n";
@@ -2042,7 +2024,8 @@ function buildLinkList($PAGE,$LINKSDB)
2042 $link['description'] = format_description($link['description'], $GLOBALS['redirector']); 2024 $link['description'] = format_description($link['description'], $GLOBALS['redirector']);
2043 $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight'; 2025 $classLi = ($i % 2) != 0 ? '' : 'publicLinkHightLight';
2044 $link['class'] = $link['private'] == 0 ? $classLi : 'private'; 2026 $link['class'] = $link['private'] == 0 ? $classLi : 'private';
2045 $link['timestamp'] = linkdate2timestamp($link['linkdate']); 2027 $date = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
2028 $link['timestamp'] = $date->getTimestamp();
2046 $taglist = explode(' ', $link['tags']); 2029 $taglist = explode(' ', $link['tags']);
2047 uasort($taglist, 'strcasecmp'); 2030 uasort($taglist, 'strcasecmp');
2048 $link['taglist'] = $taglist; 2031 $link['taglist'] = $taglist;
diff --git a/tpl/dailyrss.html b/tpl/dailyrss.html
index d959d6be..4133ca3e 100644
--- a/tpl/dailyrss.html
+++ b/tpl/dailyrss.html
@@ -2,7 +2,7 @@
2 <title>{$title} - {function="strftime('%A %e %B %Y', $daydate)"}</title> 2 <title>{$title} - {function="strftime('%A %e %B %Y', $daydate)"}</title>
3 <guid>{$absurl}</guid> 3 <guid>{$absurl}</guid>
4 <link>{$absurl}</link> 4 <link>{$absurl}</link>
5 <pubDate>{$rfc822date}</pubDate> 5 <pubDate>{$rssdate}</pubDate>
6 <description><![CDATA[ 6 <description><![CDATA[
7 {loop="links"} 7 {loop="links"}
8 <h3><a href="{$value.url}">{$value.title}</a></h3> 8 <h3><a href="{$value.url}">{$value.title}</a></h3>
@@ -13,4 +13,4 @@
13 <br><br><hr> 13 <br><br><hr>
14 {/loop} 14 {/loop}
15 ]]></description> 15 ]]></description>
16</item> \ No newline at end of file 16</item>