-/**
- * Daily RSS feed: 1 RSS entry per day giving all the links on that day.
- * Gives the last 7 days (which have links).
- * This RSS feed cannot be filtered.
- *
- * @param ConfigManager $conf Configuration Manager instance
- * @param LoginManager $loginManager LoginManager instance
- */
-function showDailyRSS($conf, $loginManager)
-{
- // Cache system
- $query = $_SERVER['QUERY_STRING'];
- $cache = new CachedPage(
- $conf->get('config.PAGE_CACHE'),
- page_url($_SERVER),
- startsWith($query, 'do=dailyrss') && !$loginManager->isLoggedIn()
- );
- $cached = $cache->cachedVersion();
- if (!empty($cached)) {
- echo $cached;
- exit;
- }
-
- // If cached was not found (or not usable), then read the database and build the response:
- // Read links from database (and filter private links if used it not logged in).
- $LINKSDB = new LinkDB(
- $conf->get('resource.datastore'),
- $loginManager->isLoggedIn(),
- $conf->get('privacy.hide_public_links'),
- $conf->get('redirector.url'),
- $conf->get('redirector.encode_url')
- );
-
- /* Some Shaarlies may have very few links, so we need to look
- back in time until we have enough days ($nb_of_days).
- */
- $nb_of_days = 7; // We take 7 days.
- $today = date('Ymd');
- $days = array();
-
- foreach ($LINKSDB as $link) {
- $day = $link['created']->format('Ymd'); // Extract day (without time)
- if (strcmp($day, $today) < 0) {
- if (empty($days[$day])) {
- $days[$day] = array();
- }
- $days[$day][] = $link;
- }
-
- if (count($days) > $nb_of_days) {
- break; // Have we collected enough days?
- }
- }
-
- // Build the RSS feed.
- header('Content-Type: application/rss+xml; charset=utf-8');
- $pageaddr = escape(index_url($_SERVER));
- echo '<?xml version="1.0" encoding="UTF-8"?><rss version="2.0">';
- echo '<channel>';
- echo '<title>Daily - '. $conf->get('general.title') . '</title>';
- echo '<link>'. $pageaddr .'</link>';
- echo '<description>Daily shared links</description>';
- echo '<language>en-en</language>';
- echo '<copyright>'. $pageaddr .'</copyright>'. PHP_EOL;
-
- // For each day.
- foreach ($days as $day => $links) {
- $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000');
- $absurl = escape(index_url($_SERVER).'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page.
-
- // We pre-format some fields for proper output.
- foreach ($links as &$link) {
- $link['formatedDescription'] = format_description(
- $link['description'],
- $conf->get('redirector.url'),
- $conf->get('redirector.encode_url')
- );
- $link['timestamp'] = $link['created']->getTimestamp();
- if (startsWith($link['url'], '?')) {
- $link['url'] = index_url($_SERVER) . $link['url']; // make permalink URL absolute
- }
- }
-
- // Then build the HTML for this day:
- $tpl = new RainTPL;
- $tpl->assign('title', $conf->get('general.title'));
- $tpl->assign('daydate', $dayDate->getTimestamp());
- $tpl->assign('absurl', $absurl);
- $tpl->assign('links', $links);
- $tpl->assign('rssdate', escape($dayDate->format(DateTime::RSS)));
- $tpl->assign('hide_timestamps', $conf->get('privacy.hide_timestamps', false));
- $tpl->assign('index_url', $pageaddr);
- $html = $tpl->draw('dailyrss', true);
-
- echo $html . PHP_EOL;
- }
- echo '</channel></rss><!-- Cached version of '. escape(page_url($_SERVER)) .' -->';
-
- $cache->cache(ob_get_contents());
- ob_end_flush();
- exit;
-}
-
-/**
- * Show the 'Daily' page.
- *
- * @param PageBuilder $pageBuilder Template engine wrapper.
- * @param LinkDB $LINKSDB LinkDB instance.
- * @param ConfigManager $conf Configuration Manager instance.
- * @param PluginManager $pluginManager Plugin Manager instance.
- * @param LoginManager $loginManager Login Manager instance
- */
-function showDaily($pageBuilder, $LINKSDB, $conf, $pluginManager, $loginManager)
-{
- $day = date('Ymd', strtotime('-1 day')); // Yesterday, in format YYYYMMDD.
- if (isset($_GET['day'])) {
- $day = $_GET['day'];
- }
-
- $days = $LINKSDB->days();
- $i = array_search($day, $days);
- if ($i === false && count($days)) {
- // no links for day, but at least one day with links
- $i = count($days) - 1;
- $day = $days[$i];
- }
- $previousday = '';
- $nextday = '';
-
- if ($i !== false) {
- if ($i >= 1) {
- $previousday=$days[$i - 1];
- }
- if ($i < count($days) - 1) {
- $nextday = $days[$i + 1];
- }
- }
- try {
- $linksToDisplay = $LINKSDB->filterDay($day);
- } catch (Exception $exc) {
- error_log($exc);
- $linksToDisplay = array();
- }
-
- // We pre-format some fields for proper output.
- foreach ($linksToDisplay as $key => $link) {
- $taglist = explode(' ', $link['tags']);
- uasort($taglist, 'strcasecmp');
- $linksToDisplay[$key]['taglist']=$taglist;
- $linksToDisplay[$key]['formatedDescription'] = format_description(
- $link['description'],
- $conf->get('redirector.url'),
- $conf->get('redirector.encode_url')
- );
- $linksToDisplay[$key]['timestamp'] = $link['created']->getTimestamp();
- }
-
- $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000');
- $data = array(
- 'pagetitle' => $conf->get('general.title') .' - '. format_date($dayDate, false),
- 'linksToDisplay' => $linksToDisplay,
- 'day' => $dayDate->getTimestamp(),
- 'dayDate' => $dayDate,
- 'previousday' => $previousday,
- 'nextday' => $nextday,
- );
-
- /* Hook is called before column construction so that plugins don't have
- to deal with columns. */
- $pluginManager->executeHooks('render_daily', $data, array('loggedin' => $loginManager->isLoggedIn()));
-
- /* We need to spread the articles on 3 columns.
- I did not want to use a JavaScript lib like http://masonry.desandro.com/
- so I manually spread entries with a simple method: I roughly evaluate the
- height of a div according to title and description length.
- */
- $columns = array(array(), array(), array()); // Entries to display, for each column.
- $fill = array(0, 0, 0); // Rough estimate of columns fill.
- foreach ($data['linksToDisplay'] as $key => $link) {
- // Roughly estimate length of entry (by counting characters)
- // Title: 30 chars = 1 line. 1 line is 30 pixels height.
- // Description: 836 characters gives roughly 342 pixel height.
- // This is not perfect, but it's usually OK.
- $length = strlen($link['title']) + (342 * strlen($link['description'])) / 836;
- if ($link['thumbnail']) {
- $length += 100; // 1 thumbnails roughly takes 100 pixels height.
- }
- // Then put in column which is the less filled:
- $smallest = min($fill); // find smallest value in array.
- $index = array_search($smallest, $fill); // find index of this smallest value.
- array_push($columns[$index], $link); // Put entry in this column.
- $fill[$index] += $length;
- }
-
- $data['cols'] = $columns;
-
- foreach ($data as $key => $value) {
- $pageBuilder->assign($key, $value);
- }
-
- $pageBuilder->assign('pagetitle', t('Daily') .' - '. $conf->get('general.title', 'Shaarli'));
- $pageBuilder->renderPage('daily');
- exit;
-}
-