2015-09-29 20:59:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\CoreBundle\Twig;
|
|
|
|
|
2016-04-21 20:30:50 +00:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
2022-12-19 09:37:22 +00:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2019-06-21 10:46:53 +00:00
|
|
|
use Twig\Extension\AbstractExtension;
|
|
|
|
use Twig\Extension\GlobalsInterface;
|
|
|
|
use Twig\TwigFilter;
|
|
|
|
use Twig\TwigFunction;
|
2016-04-21 20:30:50 +00:00
|
|
|
use Wallabag\CoreBundle\Repository\EntryRepository;
|
2016-09-04 18:53:28 +00:00
|
|
|
use Wallabag\CoreBundle\Repository\TagRepository;
|
2022-11-23 14:51:33 +00:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2016-04-21 20:30:50 +00:00
|
|
|
|
2019-06-21 10:46:53 +00:00
|
|
|
class WallabagExtension extends AbstractExtension implements GlobalsInterface
|
2015-09-29 20:59:44 +00:00
|
|
|
{
|
2016-04-21 20:30:50 +00:00
|
|
|
private $tokenStorage;
|
2016-09-04 18:53:28 +00:00
|
|
|
private $entryRepository;
|
|
|
|
private $tagRepository;
|
|
|
|
private $lifeTime;
|
2016-10-01 13:58:26 +00:00
|
|
|
private $translator;
|
2023-08-07 21:22:35 +00:00
|
|
|
private $projectDir;
|
2016-04-21 20:30:50 +00:00
|
|
|
|
2023-08-07 21:22:35 +00:00
|
|
|
public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator, string $projectDir)
|
2016-04-21 20:30:50 +00:00
|
|
|
{
|
2016-09-04 18:53:28 +00:00
|
|
|
$this->entryRepository = $entryRepository;
|
|
|
|
$this->tagRepository = $tagRepository;
|
2016-04-21 20:30:50 +00:00
|
|
|
$this->tokenStorage = $tokenStorage;
|
2016-09-04 18:53:28 +00:00
|
|
|
$this->lifeTime = $lifeTime;
|
2016-10-01 13:58:26 +00:00
|
|
|
$this->translator = $translator;
|
2023-08-07 21:22:35 +00:00
|
|
|
$this->projectDir = $projectDir;
|
2016-04-21 20:30:50 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 11:32:16 +00:00
|
|
|
public function getGlobals(): array
|
2019-06-21 10:46:53 +00:00
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:59:44 +00:00
|
|
|
public function getFilters()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
return [
|
2019-06-21 10:46:53 +00:00
|
|
|
new TwigFilter('removeWww', [$this, 'removeWww']),
|
|
|
|
new TwigFilter('removeScheme', [$this, 'removeScheme']),
|
|
|
|
new TwigFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']),
|
2016-04-12 09:36:01 +00:00
|
|
|
];
|
2015-09-29 20:59:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-03 17:09:28 +00:00
|
|
|
public function getFunctions()
|
|
|
|
{
|
2017-07-01 07:52:38 +00:00
|
|
|
return [
|
2019-06-21 10:46:53 +00:00
|
|
|
new TwigFunction('count_entries', [$this, 'countEntries']),
|
|
|
|
new TwigFunction('count_tags', [$this, 'countTags']),
|
|
|
|
new TwigFunction('display_stats', [$this, 'displayStats']),
|
2020-01-29 21:26:00 +00:00
|
|
|
new TwigFunction('asset_file_exists', [$this, 'assetFileExists']),
|
2020-11-08 14:04:29 +00:00
|
|
|
new TwigFunction('theme_class', [$this, 'themeClass']),
|
2017-07-01 07:52:38 +00:00
|
|
|
];
|
2016-09-03 17:09:28 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 20:59:44 +00:00
|
|
|
public function removeWww($url)
|
|
|
|
{
|
2015-10-01 07:26:52 +00:00
|
|
|
return preg_replace('/^www\./i', '', $url);
|
2015-09-29 20:59:44 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 16:48:10 +00:00
|
|
|
public function removeScheme($url)
|
|
|
|
{
|
|
|
|
return preg_replace('#^https?://#i', '', $url);
|
|
|
|
}
|
|
|
|
|
2017-09-09 17:34:41 +00:00
|
|
|
public function removeSchemeAndWww($url)
|
|
|
|
{
|
2019-04-25 12:12:56 +00:00
|
|
|
return $this->removeWww($this->removeScheme($url));
|
2017-09-09 17:34:41 +00:00
|
|
|
}
|
|
|
|
|
2016-09-03 17:09:28 +00:00
|
|
|
/**
|
2016-09-03 17:26:23 +00:00
|
|
|
* Return number of entries depending of the type (unread, archive, starred or all).
|
2016-09-03 17:09:28 +00:00
|
|
|
*
|
2016-09-03 17:26:23 +00:00
|
|
|
* @param string $type Type of entries to count
|
2016-09-03 17:09:28 +00:00
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function countEntries($type)
|
2016-04-21 20:30:50 +00:00
|
|
|
{
|
|
|
|
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
|
|
|
|
|
2022-11-23 14:51:33 +00:00
|
|
|
if (!$user instanceof User) {
|
2016-09-05 12:17:44 +00:00
|
|
|
return 0;
|
2016-04-21 20:30:50 +00:00
|
|
|
}
|
|
|
|
|
2016-09-03 17:09:28 +00:00
|
|
|
switch ($type) {
|
|
|
|
case 'starred':
|
2016-09-04 18:53:28 +00:00
|
|
|
$qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
|
2016-09-03 17:09:28 +00:00
|
|
|
break;
|
|
|
|
case 'archive':
|
2016-09-04 18:53:28 +00:00
|
|
|
$qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
|
2016-09-03 17:09:28 +00:00
|
|
|
break;
|
|
|
|
case 'unread':
|
2016-09-04 18:53:28 +00:00
|
|
|
$qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
|
2016-09-03 17:09:28 +00:00
|
|
|
break;
|
2020-04-26 12:09:16 +00:00
|
|
|
case 'annotated':
|
2020-04-20 17:00:58 +00:00
|
|
|
$qb = $this->entryRepository->getBuilderForAnnotationsByUser($user->getId());
|
|
|
|
break;
|
2016-09-03 17:09:28 +00:00
|
|
|
case 'all':
|
2016-09-04 18:53:28 +00:00
|
|
|
$qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
|
2016-09-03 17:09:28 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
|
|
|
|
}
|
|
|
|
|
|
|
|
// THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
|
|
|
|
// ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
|
|
|
|
$query = $qb
|
|
|
|
->select('e.id')
|
|
|
|
->groupBy('e.id')
|
|
|
|
->getQuery();
|
|
|
|
|
2016-09-25 09:21:13 +00:00
|
|
|
$query->useQueryCache(true);
|
2020-07-08 05:25:44 +00:00
|
|
|
$query->enableResultCache($this->lifeTime);
|
2016-09-03 17:09:28 +00:00
|
|
|
|
2018-09-05 12:25:32 +00:00
|
|
|
return \count($query->getArrayResult());
|
2016-04-21 20:30:50 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 18:53:28 +00:00
|
|
|
/**
|
|
|
|
* Return number of tags.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function countTags()
|
|
|
|
{
|
|
|
|
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
|
|
|
|
|
2022-11-23 14:51:33 +00:00
|
|
|
if (!$user instanceof User) {
|
2016-09-05 12:17:44 +00:00
|
|
|
return 0;
|
2016-09-04 18:53:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 10:23:44 +00:00
|
|
|
return $this->tagRepository->countAllTags($user->getId());
|
2016-09-04 18:53:28 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 13:58:26 +00: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 14:51:33 +00:00
|
|
|
if (!$user instanceof User) {
|
2016-10-01 13:58:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = $this->entryRepository->getBuilderForArchiveByUser($user->getId())
|
|
|
|
->select('e.id')
|
|
|
|
->groupBy('e.id')
|
|
|
|
->getQuery();
|
|
|
|
|
|
|
|
$query->useQueryCache(true);
|
2020-07-08 05:25:44 +00:00
|
|
|
$query->enableResultCache($this->lifeTime);
|
2016-10-01 13:58:26 +00:00
|
|
|
|
2018-09-05 12:25:32 +00:00
|
|
|
$nbArchives = \count($query->getArrayResult());
|
2016-10-01 13:58:26 +00:00
|
|
|
|
|
|
|
$interval = $user->getCreatedAt()->diff(new \DateTime('now'));
|
|
|
|
$nbDays = (int) $interval->format('%a') ?: 1;
|
|
|
|
|
2016-10-20 19:16:01 +00:00
|
|
|
// force setlocale for date translation
|
2021-01-23 20:15:29 +00:00
|
|
|
$locale = strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage()));
|
|
|
|
$intlDateFormatter = new \IntlDateFormatter($locale, \IntlDateFormatter::LONG, \IntlDateFormatter::NONE);
|
2016-10-20 19:16:01 +00:00
|
|
|
|
2016-10-01 13:58:26 +00:00
|
|
|
return $this->translator->trans('footer.stats', [
|
2021-01-23 20:15:29 +00:00
|
|
|
'%user_creation%' => $intlDateFormatter->format($user->getCreatedAt()),
|
2016-10-01 13:58:26 +00:00
|
|
|
'%nb_archives%' => $nbArchives,
|
|
|
|
'%per_day%' => round($nbArchives / $nbDays, 2),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-01-29 21:26:00 +00:00
|
|
|
public function assetFileExists($name)
|
|
|
|
{
|
2023-08-07 21:22:35 +00:00
|
|
|
return file_exists(realpath($this->projectDir . '/web/' . $name));
|
2020-01-29 21:26:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 14:04:29 +00:00
|
|
|
public function themeClass()
|
|
|
|
{
|
|
|
|
return isset($_COOKIE['theme']) && 'dark' === $_COOKIE['theme'] ? 'dark-theme' : '';
|
|
|
|
}
|
|
|
|
|
2015-09-29 20:59:44 +00:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'wallabag_extension';
|
|
|
|
}
|
|
|
|
}
|