2015-09-29 22:59:44 +02:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 01:30:12 +01:00
|
|
|
namespace Wallabag\Twig;
|
2015-09-29 22:59:44 +02:00
|
|
|
|
2016-04-21 22:30:50 +02:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
2022-12-19 10:37:22 +01:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2019-06-21 12:46:53 +02:00
|
|
|
use Twig\Extension\AbstractExtension;
|
|
|
|
use Twig\Extension\GlobalsInterface;
|
|
|
|
use Twig\TwigFilter;
|
|
|
|
use Twig\TwigFunction;
|
2024-02-19 01:30:12 +01:00
|
|
|
use Wallabag\Entity\User;
|
2025-03-18 20:59:39 +01:00
|
|
|
use Wallabag\Repository\AnnotationRepository;
|
2024-02-19 01:30:12 +01:00
|
|
|
use Wallabag\Repository\EntryRepository;
|
|
|
|
use Wallabag\Repository\TagRepository;
|
2016-04-21 22:30:50 +02:00
|
|
|
|
2019-06-21 12:46:53 +02:00
|
|
|
class WallabagExtension extends AbstractExtension implements GlobalsInterface
|
2015-09-29 22:59:44 +02:00
|
|
|
{
|
2025-04-05 13:54:27 +02:00
|
|
|
public function __construct(
|
2025-04-05 13:59:36 +02:00
|
|
|
private readonly EntryRepository $entryRepository,
|
|
|
|
private readonly AnnotationRepository $annotationRepository,
|
|
|
|
private readonly TagRepository $tagRepository,
|
|
|
|
private readonly TokenStorageInterface $tokenStorage,
|
2025-04-05 13:54:27 +02:00
|
|
|
private $lifeTime,
|
2025-04-05 13:59:36 +02:00
|
|
|
private readonly TranslatorInterface $translator,
|
|
|
|
private readonly string $projectDir,
|
2025-04-05 13:54:27 +02:00
|
|
|
) {
|
2016-04-21 22:30:50 +02:00
|
|
|
}
|
|
|
|
|
2022-12-15 12:32:16 +01:00
|
|
|
public function getGlobals(): array
|
2019-06-21 12:46:53 +02:00
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2025-01-18 23:56:06 +01:00
|
|
|
public function getFilters(): array
|
2015-09-29 22:59:44 +02:00
|
|
|
{
|
2016-04-12 11:36:01 +02:00
|
|
|
return [
|
2019-06-21 12:46:53 +02:00
|
|
|
new TwigFilter('removeWww', [$this, 'removeWww']),
|
|
|
|
new TwigFilter('removeScheme', [$this, 'removeScheme']),
|
|
|
|
new TwigFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']),
|
2016-04-12 11:36:01 +02:00
|
|
|
];
|
2015-09-29 22:59:44 +02:00
|
|
|
}
|
|
|
|
|
2025-01-18 23:56:06 +01:00
|
|
|
public function getFunctions(): array
|
2016-09-03 19:09:28 +02:00
|
|
|
{
|
2017-07-01 09:52:38 +02:00
|
|
|
return [
|
2019-06-21 12:46:53 +02:00
|
|
|
new TwigFunction('count_entries', [$this, 'countEntries']),
|
|
|
|
new TwigFunction('count_tags', [$this, 'countTags']),
|
|
|
|
new TwigFunction('display_stats', [$this, 'displayStats']),
|
2020-01-29 22:26:00 +01:00
|
|
|
new TwigFunction('asset_file_exists', [$this, 'assetFileExists']),
|
2020-11-08 15:04:29 +01:00
|
|
|
new TwigFunction('theme_class', [$this, 'themeClass']),
|
2017-07-01 09:52:38 +02:00
|
|
|
];
|
2016-09-03 19:09:28 +02:00
|
|
|
}
|
|
|
|
|
2015-09-29 22:59:44 +02:00
|
|
|
public function removeWww($url)
|
|
|
|
{
|
2023-08-10 00:37:25 +01:00
|
|
|
if (!\is_string($url)) {
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2015-10-01 09:26:52 +02:00
|
|
|
return preg_replace('/^www\./i', '', $url);
|
2015-09-29 22:59:44 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 18:48:10 +02:00
|
|
|
public function removeScheme($url)
|
|
|
|
{
|
2023-08-10 00:37:25 +01:00
|
|
|
if (!\is_string($url)) {
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2017-06-13 18:48:10 +02:00
|
|
|
return preg_replace('#^https?://#i', '', $url);
|
|
|
|
}
|
|
|
|
|
2017-09-09 19:34:41 +02:00
|
|
|
public function removeSchemeAndWww($url)
|
|
|
|
{
|
2019-04-25 14:12:56 +02:00
|
|
|
return $this->removeWww($this->removeScheme($url));
|
2017-09-09 19:34:41 +02:00
|
|
|
}
|
|
|
|
|
2016-09-03 19:09:28 +02:00
|
|
|
/**
|
2016-09-03 19:26:23 +02:00
|
|
|
* Return number of entries depending of the type (unread, archive, starred or all).
|
2016-09-03 19:09:28 +02:00
|
|
|
*
|
2016-09-03 19:26:23 +02:00
|
|
|
* @param string $type Type of entries to count
|
2016-09-03 19:09:28 +02:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function countEntries($type)
|
2016-04-21 22:30:50 +02:00
|
|
|
{
|
|
|
|
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
|
|
|
|
|
2022-11-23 15:51:33 +01:00
|
|
|
if (!$user instanceof User) {
|
2016-09-05 14:17:44 +02:00
|
|
|
return 0;
|
2016-04-21 22:30:50 +02:00
|
|
|
}
|
|
|
|
|
2025-04-05 13:56:56 +02:00
|
|
|
$qb = match ($type) {
|
|
|
|
'starred' => $this->entryRepository->getCountBuilderForStarredByUser($user->getId())->select('COUNT(e.id)'),
|
|
|
|
'archive' => $this->entryRepository->getCountBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)'),
|
|
|
|
'unread' => $this->entryRepository->getCountBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)'),
|
|
|
|
'annotated' => $this->annotationRepository->getCountBuilderByUser($user->getId())->select('COUNT(DISTINCT e.entry)'),
|
|
|
|
'all' => $this->entryRepository->getCountBuilderForAllByUser($user->getId())->select('COUNT(e.id)'),
|
|
|
|
default => throw new \InvalidArgumentException(\sprintf('Type "%s" is not implemented.', $type)),
|
|
|
|
};
|
2016-09-03 19:09:28 +02:00
|
|
|
|
2025-02-10 08:42:06 +01:00
|
|
|
$query = $qb->getQuery();
|
2016-09-25 11:21:13 +02:00
|
|
|
$query->useQueryCache(true);
|
2020-07-08 07:25:44 +02:00
|
|
|
$query->enableResultCache($this->lifeTime);
|
2016-09-03 19:09:28 +02:00
|
|
|
|
2023-08-31 12:13:15 +02:00
|
|
|
return $query->getSingleScalarResult();
|
2016-04-21 22:30:50 +02:00
|
|
|
}
|
|
|
|
|
2016-09-04 20:53:28 +02:00
|
|
|
/**
|
|
|
|
* Return number of tags.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function countTags()
|
|
|
|
{
|
|
|
|
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
|
|
|
|
|
2022-11-23 15:51:33 +01:00
|
|
|
if (!$user instanceof User) {
|
2016-09-05 14:17:44 +02:00
|
|
|
return 0;
|
2016-09-04 20:53:28 +02:00
|
|
|
}
|
|
|
|
|
2016-09-25 12:23:44 +02:00
|
|
|
return $this->tagRepository->countAllTags($user->getId());
|
2016-09-04 20:53:28 +02:00
|
|
|
}
|
|
|
|
|
2016-10-01 15:58:26 +02:00
|
|
|
/**
|
|
|
|
* Display a single line about reading stats.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function displayStats()
|
|
|
|
{
|
|
|
|
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
|
|
|
|
|
2022-11-23 15:51:33 +01:00
|
|
|
if (!$user instanceof User) {
|
2023-08-08 02:27:21 +01:00
|
|
|
return '';
|
2016-10-01 15:58:26 +02:00
|
|
|
}
|
|
|
|
|
2023-08-31 12:13:15 +02:00
|
|
|
$query = $this->entryRepository->getCountBuilderForArchiveByUser($user->getId())
|
|
|
|
->select('COUNT(e.id)')
|
2016-10-01 15:58:26 +02:00
|
|
|
->getQuery();
|
|
|
|
|
|
|
|
$query->useQueryCache(true);
|
2020-07-08 07:25:44 +02:00
|
|
|
$query->enableResultCache($this->lifeTime);
|
2016-10-01 15:58:26 +02:00
|
|
|
|
2023-08-31 12:13:15 +02:00
|
|
|
$nbArchives = $query->getSingleScalarResult();
|
2016-10-01 15:58:26 +02:00
|
|
|
|
|
|
|
$interval = $user->getCreatedAt()->diff(new \DateTime('now'));
|
|
|
|
$nbDays = (int) $interval->format('%a') ?: 1;
|
|
|
|
|
2016-10-20 21:16:01 +02:00
|
|
|
// force setlocale for date translation
|
2021-01-23 21:15:29 +01:00
|
|
|
$locale = strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage()));
|
|
|
|
$intlDateFormatter = new \IntlDateFormatter($locale, \IntlDateFormatter::LONG, \IntlDateFormatter::NONE);
|
2016-10-20 21:16:01 +02:00
|
|
|
|
2016-10-01 15:58:26 +02:00
|
|
|
return $this->translator->trans('footer.stats', [
|
2021-01-23 21:15:29 +01:00
|
|
|
'%user_creation%' => $intlDateFormatter->format($user->getCreatedAt()),
|
2016-10-01 15:58:26 +02:00
|
|
|
'%nb_archives%' => $nbArchives,
|
|
|
|
'%per_day%' => round($nbArchives / $nbDays, 2),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-01-29 22:26:00 +01:00
|
|
|
public function assetFileExists($name)
|
|
|
|
{
|
2023-08-07 22:22:35 +01:00
|
|
|
return file_exists(realpath($this->projectDir . '/web/' . $name));
|
2020-01-29 22:26:00 +01:00
|
|
|
}
|
|
|
|
|
2020-11-08 15:04:29 +01:00
|
|
|
public function themeClass()
|
|
|
|
{
|
|
|
|
return isset($_COOKIE['theme']) && 'dark' === $_COOKIE['theme'] ? 'dark-theme' : '';
|
|
|
|
}
|
|
|
|
|
2015-09-29 22:59:44 +02:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'wallabag_extension';
|
|
|
|
}
|
|
|
|
}
|