2020-05-17 11:06:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-05-22 13:20:31 +02:00
|
|
|
namespace Shaarli\Front\Controller\Visitor;
|
2020-05-17 11:06:39 +02:00
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use Shaarli\Bookmark\Bookmark;
|
2020-10-16 11:50:53 +02:00
|
|
|
use Shaarli\Helper\DailyPageHelper;
|
2020-07-06 08:04:35 +02:00
|
|
|
use Shaarli\Render\TemplatePage;
|
2020-05-17 11:06:39 +02:00
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class DailyController
|
|
|
|
*
|
|
|
|
* Slim controller used to render the daily page.
|
|
|
|
*/
|
2020-05-22 13:20:31 +02:00
|
|
|
class DailyController extends ShaarliVisitorController
|
2020-05-17 11:06:39 +02:00
|
|
|
{
|
2020-05-17 14:16:32 +02:00
|
|
|
public static $DAILY_RSS_NB_DAYS = 8;
|
|
|
|
|
2020-05-17 11:06:39 +02:00
|
|
|
/**
|
|
|
|
* Controller displaying all bookmarks published in a single day.
|
|
|
|
* It take a `day` date query parameter (format YYYYMMDD).
|
|
|
|
*/
|
|
|
|
public function index(Request $request, Response $response): Response
|
|
|
|
{
|
2020-10-16 11:50:53 +02:00
|
|
|
$type = DailyPageHelper::extractRequestedType($request);
|
|
|
|
$format = DailyPageHelper::getFormatByType($type);
|
|
|
|
$latestBookmark = $this->container->bookmarkService->getLatest();
|
|
|
|
$dateTime = DailyPageHelper::extractRequestedDateTime($type, $request->getQueryParam($type), $latestBookmark);
|
|
|
|
$start = DailyPageHelper::getStartDateTimeByType($type, $dateTime);
|
|
|
|
$end = DailyPageHelper::getEndDateTimeByType($type, $dateTime);
|
|
|
|
$dailyDesc = DailyPageHelper::getDescriptionByType($type, $dateTime);
|
|
|
|
|
|
|
|
$linksToDisplay = $this->container->bookmarkService->findByDate(
|
|
|
|
$start,
|
|
|
|
$end,
|
|
|
|
$previousDay,
|
|
|
|
$nextDay
|
|
|
|
);
|
2020-05-17 11:06:39 +02:00
|
|
|
|
|
|
|
$formatter = $this->container->formatterFactory->getFormatter();
|
2020-07-28 20:46:11 +02:00
|
|
|
$formatter->addContextData('base_path', $this->container->basePath);
|
2020-05-17 11:06:39 +02:00
|
|
|
// We pre-format some fields for proper output.
|
|
|
|
foreach ($linksToDisplay as $key => $bookmark) {
|
|
|
|
$linksToDisplay[$key] = $formatter->format($bookmark);
|
|
|
|
// This page is a bit specific, we need raw description to calculate the length
|
|
|
|
$linksToDisplay[$key]['formatedDescription'] = $linksToDisplay[$key]['description'];
|
|
|
|
$linksToDisplay[$key]['description'] = $bookmark->getDescription();
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'linksToDisplay' => $linksToDisplay,
|
2020-10-16 11:50:53 +02:00
|
|
|
'dayDate' => $start,
|
|
|
|
'day' => $start->getTimestamp(),
|
|
|
|
'previousday' => $previousDay ? $previousDay->format($format) : '',
|
|
|
|
'nextday' => $nextDay ? $nextDay->format($format) : '',
|
|
|
|
'dayDesc' => $dailyDesc,
|
|
|
|
'type' => $type,
|
|
|
|
'localizedType' => $this->translateType($type),
|
2020-05-17 11:06:39 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// Hooks are called before column construction so that plugins don't have to deal with columns.
|
2020-07-26 14:43:10 +02:00
|
|
|
$this->executePageHooks('render_daily', $data, TemplatePage::DAILY);
|
2020-05-17 11:06:39 +02:00
|
|
|
|
|
|
|
$data['cols'] = $this->calculateColumns($data['linksToDisplay']);
|
|
|
|
|
2020-07-26 14:43:10 +02:00
|
|
|
$this->assignAllView($data);
|
2020-05-17 11:06:39 +02:00
|
|
|
|
|
|
|
$mainTitle = $this->container->conf->get('general.title', 'Shaarli');
|
|
|
|
$this->assignView(
|
|
|
|
'pagetitle',
|
2020-10-16 11:50:53 +02:00
|
|
|
$data['localizedType'] . ' - ' . $data['dayDesc'] . ' - ' . $mainTitle
|
2020-05-17 11:06:39 +02:00
|
|
|
);
|
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
return $response->write($this->render(TemplatePage::DAILY));
|
2020-05-17 11:06:39 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:16:32 +02:00
|
|
|
/**
|
|
|
|
* Daily RSS feed: 1 RSS entry per day giving all the bookmarks on that day.
|
|
|
|
* Gives the last 7 days (which have bookmarks).
|
|
|
|
* This RSS feed cannot be filtered and does not trigger plugins yet.
|
|
|
|
*/
|
|
|
|
public function rss(Request $request, Response $response): Response
|
|
|
|
{
|
|
|
|
$response = $response->withHeader('Content-Type', 'application/rss+xml; charset=utf-8');
|
|
|
|
|
|
|
|
$pageUrl = page_url($this->container->environment);
|
|
|
|
$cache = $this->container->pageCacheManager->getCachePage($pageUrl);
|
|
|
|
|
|
|
|
$cached = $cache->cachedVersion();
|
|
|
|
if (!empty($cached)) {
|
|
|
|
return $response->write($cached);
|
|
|
|
}
|
|
|
|
|
|
|
|
$days = [];
|
2020-10-16 11:50:53 +02:00
|
|
|
$type = DailyPageHelper::extractRequestedType($request);
|
|
|
|
$format = DailyPageHelper::getFormatByType($type);
|
|
|
|
$length = DailyPageHelper::getRssLengthByType($type);
|
2020-05-17 14:16:32 +02:00
|
|
|
foreach ($this->container->bookmarkService->search() as $bookmark) {
|
2020-10-16 11:50:53 +02:00
|
|
|
$day = $bookmark->getCreated()->format($format);
|
2020-05-17 14:16:32 +02:00
|
|
|
|
|
|
|
// Stop iterating after DAILY_RSS_NB_DAYS entries
|
2020-10-16 11:50:53 +02:00
|
|
|
if (count($days) === $length && !isset($days[$day])) {
|
2020-05-17 14:16:32 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$days[$day][] = $bookmark;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the RSS feed.
|
|
|
|
$indexUrl = escape(index_url($this->container->environment));
|
|
|
|
|
|
|
|
$formatter = $this->container->formatterFactory->getFormatter();
|
|
|
|
$formatter->addContextData('index_url', $indexUrl);
|
|
|
|
|
|
|
|
$dataPerDay = [];
|
|
|
|
|
|
|
|
/** @var Bookmark[] $bookmarks */
|
|
|
|
foreach ($days as $day => $bookmarks) {
|
2020-10-16 11:50:53 +02:00
|
|
|
$dayDateTime = DailyPageHelper::extractRequestedDateTime($type, (string) $day);
|
|
|
|
$endDateTime = DailyPageHelper::getEndDateTimeByType($type, $dayDateTime);
|
|
|
|
|
|
|
|
// We only want the RSS entry to be published when the period is over.
|
|
|
|
if (new DateTime() < $endDateTime) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:16:32 +02:00
|
|
|
$dataPerDay[$day] = [
|
2020-10-16 11:50:53 +02:00
|
|
|
'date' => $endDateTime,
|
|
|
|
'date_rss' => $endDateTime->format(DateTime::RSS),
|
2020-11-15 12:05:08 +01:00
|
|
|
'date_human' => DailyPageHelper::getDescriptionByType($type, $dayDateTime, false),
|
2020-09-22 20:25:47 +02:00
|
|
|
'absolute_url' => $indexUrl . 'daily?' . $type . '=' . $day,
|
2020-05-17 14:16:32 +02:00
|
|
|
'links' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($bookmarks as $key => $bookmark) {
|
|
|
|
$dataPerDay[$day]['links'][$key] = $formatter->format($bookmark);
|
|
|
|
|
|
|
|
// Make permalink URL absolute
|
|
|
|
if ($bookmark->isNote()) {
|
2020-10-16 11:50:53 +02:00
|
|
|
$dataPerDay[$day]['links'][$key]['url'] = rtrim($indexUrl, '/') . $bookmark->getUrl();
|
2020-05-17 14:16:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:50:53 +02:00
|
|
|
$this->assignAllView([
|
|
|
|
'title' => $this->container->conf->get('general.title', 'Shaarli'),
|
|
|
|
'index_url' => $indexUrl,
|
|
|
|
'page_url' => $pageUrl,
|
|
|
|
'hide_timestamps' => $this->container->conf->get('privacy.hide_timestamps', false),
|
|
|
|
'days' => $dataPerDay,
|
|
|
|
'type' => $type,
|
|
|
|
'localizedType' => $this->translateType($type),
|
|
|
|
]);
|
2020-05-17 14:16:32 +02:00
|
|
|
|
2020-07-06 08:04:35 +02:00
|
|
|
$rssContent = $this->render(TemplatePage::DAILY_RSS);
|
2020-05-17 14:16:32 +02:00
|
|
|
|
|
|
|
$cache->cache($rssContent);
|
|
|
|
|
|
|
|
return $response->write($rssContent);
|
|
|
|
}
|
|
|
|
|
2020-05-17 11:06:39 +02:00
|
|
|
/**
|
|
|
|
* We need to spread the articles on 3 columns.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
protected function calculateColumns(array $links): array
|
|
|
|
{
|
|
|
|
// Entries to display, for each column.
|
|
|
|
$columns = [[], [], []];
|
|
|
|
// Rough estimate of columns fill.
|
|
|
|
$fill = [0, 0, 0];
|
|
|
|
foreach ($links as $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 (! empty($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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $columns;
|
|
|
|
}
|
2020-10-16 11:50:53 +02:00
|
|
|
|
|
|
|
protected function translateType($type): string
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
t('day') => t('Daily'),
|
|
|
|
t('week') => t('Weekly'),
|
|
|
|
t('month') => t('Monthly'),
|
|
|
|
][t($type)] ?? t('Daily');
|
|
|
|
}
|
2020-05-17 11:06:39 +02:00
|
|
|
}
|