1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-06-27 16:36:00 +00:00
wallabag/src/Twig/WallabagExtension.php

175 lines
5.4 KiB
PHP
Raw Normal View History

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
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
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;
use Wallabag\Repository\AnnotationRepository;
2024-02-19 01:30:12 +01:00
use Wallabag\Repository\EntryRepository;
use Wallabag\Repository\TagRepository;
class WallabagExtension extends AbstractExtension implements GlobalsInterface
2015-09-29 22:59:44 +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,
private $lifeTime,
2025-04-05 13:59:36 +02:00
private readonly TranslatorInterface $translator,
private readonly string $projectDir,
) {
}
2022-12-15 12:32:16 +01:00
public function getGlobals(): array
{
return [];
}
2025-01-18 23:56:06 +01:00
public function getFilters(): array
2015-09-29 22:59:44 +02:00
{
return [
new TwigFilter('removeWww', [$this, 'removeWww']),
new TwigFilter('removeScheme', [$this, 'removeScheme']),
new TwigFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']),
];
2015-09-29 22:59:44 +02:00
}
2025-01-18 23:56:06 +01:00
public function getFunctions(): array
{
2017-07-01 09:52:38 +02:00
return [
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
];
}
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
}
public function removeScheme($url)
{
2023-08-10 00:37:25 +01:00
if (!\is_string($url)) {
return $url;
}
return preg_replace('#^https?://#i', '', $url);
}
public function removeSchemeAndWww($url)
{
2019-04-25 14:12:56 +02:00
return $this->removeWww($this->removeScheme($url));
}
/**
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:26:23 +02:00
* @param string $type Type of entries to count
*
* @return int
*/
public function countEntries($type)
{
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
2022-11-23 15:51:33 +01:00
if (!$user instanceof User) {
return 0;
}
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)),
};
$query = $qb->getQuery();
$query->useQueryCache(true);
2020-07-08 07:25:44 +02:00
$query->enableResultCache($this->lifeTime);
2023-08-31 12:13:15 +02:00
return $query->getSingleScalarResult();
}
/**
* 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) {
return 0;
}
2016-09-25 12:23:44 +02:00
return $this->tagRepository->countAllTags($user->getId());
}
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;
// force setlocale for date translation
$locale = strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage()));
$intlDateFormatter = new \IntlDateFormatter($locale, \IntlDateFormatter::LONG, \IntlDateFormatter::NONE);
2016-10-01 15:58:26 +02:00
return $this->translator->trans('footer.stats', [
'%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)
{
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';
}
}