Merge pull request #2323 from wallabag/footer-stats

Add simple stats in footer
This commit is contained in:
Jeremy Benoist 2016-10-02 11:04:49 +02:00 committed by GitHub
commit bfb3ae01da
15 changed files with 82 additions and 5 deletions

View file

@ -21,6 +21,7 @@ services:
- "@wallabag_core.tag_repository"
- "@security.token_storage"
- "%wallabag_core.cache_lifetime%"
- "@translator"
tags:
- { name: twig.extension }

View file

@ -45,6 +45,7 @@ footer:
# social: 'Social'
# powered_by: 'powered by'
about: 'Om'
# stats: Since %user_creation% you read %nb_archives% articles. That is about %per_day% a day!
config:
page_title: 'Opsætning'

View file

@ -45,6 +45,7 @@ footer:
social: 'Soziales'
powered_by: 'angetrieben von'
about: 'Über'
# stats: Since %user_creation% you read %nb_archives% articles. That is about %per_day% a day!
config:
page_title: 'Einstellungen'

View file

@ -45,6 +45,7 @@ footer:
social: 'Social'
powered_by: 'powered by'
about: 'About'
stats: Since %user_creation% you read %nb_archives% articles. That is about %per_day% a day!
config:
page_title: 'Config'

View file

@ -45,6 +45,7 @@ footer:
social: 'Social'
powered_by: 'funciona por'
about: 'Acerca de'
# stats: Since %user_creation% you read %nb_archives% articles. That is about %per_day% a day!
config:
page_title: 'Configuración'

View file

@ -45,6 +45,7 @@ footer:
social: 'شبکه‌های اجتماعی'
powered_by: 'توانمند با'
about: 'درباره'
# stats: Since %user_creation% you read %nb_archives% articles. That is about %per_day% a day!
config:
page_title: 'پیکربندی'

View file

@ -45,6 +45,7 @@ footer:
social: 'Social'
powered_by: 'propulsé par'
about: 'À propos'
stats: Depuis le %user_creation% vous avez lu %nb_archives% articles. Ce qui fait %per_day% par jour !
config:
page_title: 'Configuration'

View file

@ -45,6 +45,7 @@ footer:
social: 'Social'
powered_by: 'powered by'
about: 'About'
# stats: Since %user_creation% you read %nb_archives% articles. That is about %per_day% a day!
config:
page_title: 'Configurazione'

View file

@ -45,9 +45,10 @@ footer:
social: 'Social'
powered_by: 'propulsat per'
about: 'A prepaus'
page_title: 'Configuracion'
# stats: Since %user_creation% you read %nb_archives% articles. That is about %per_day% a day!
config:
page_title: 'Configuracion'
tab_menu:
settings: 'Paramètres'
rss: 'RSS'

View file

@ -45,6 +45,7 @@ footer:
social: 'Społeczność'
powered_by: 'Kontrolowany przez'
about: 'O nas'
# stats: Since %user_creation% you read %nb_archives% articles. That is about %per_day% a day!
config:
page_title: 'Konfiguracja'

View file

@ -45,6 +45,7 @@ footer:
# social: 'Social'
# powered_by: 'powered by'
about: 'Despre'
# stats: Since %user_creation% you read %nb_archives% articles. That is about %per_day% a day!
config:
page_title: 'Configurație'

View file

@ -45,6 +45,7 @@ footer:
social: 'Sosyal'
powered_by: 'powered by'
about: 'Hakkımızda'
# stats: Since %user_creation% you read %nb_archives% articles. That is about %per_day% a day!
config:
page_title: 'Yapılandırma'

View file

@ -122,8 +122,19 @@
<footer class="page-footer cyan darken-2">
<div class="footer-copyright">
<div class="container">
<p>{{ 'footer.wallabag.powered_by'|trans }} <a target="_blank" href="https://wallabag.org" class="grey-text text-lighten-4">wallabag</a></p>
<a class="grey-text text-lighten-4 right" href="{{ path('about') }}">{{ 'footer.wallabag.about'|trans }}</a>
<div class="row">
<div class="col s8">
<p>
{{ display_stats() }}
</p>
</div>
<div class="col s4">
<p>
{{ 'footer.wallabag.powered_by'|trans }} <a target="_blank" href="https://wallabag.org" class="grey-text text-lighten-4">wallabag</a>
<a class="grey-text text-lighten-4" href="{{ path('about') }}">{{ 'footer.wallabag.about'|trans|lower }}</a>
</p>
</div>
</div>
</div>
</div>
</footer>

View file

@ -5,6 +5,7 @@ namespace Wallabag\CoreBundle\Twig;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Wallabag\CoreBundle\Repository\EntryRepository;
use Wallabag\CoreBundle\Repository\TagRepository;
use Symfony\Component\Translation\TranslatorInterface;
class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
{
@ -12,13 +13,15 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
private $entryRepository;
private $tagRepository;
private $lifeTime;
private $translator;
public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0)
public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator)
{
$this->entryRepository = $entryRepository;
$this->tagRepository = $tagRepository;
$this->tokenStorage = $tokenStorage;
$this->lifeTime = $lifeTime;
$this->translator = $translator;
}
public function getFilters()
@ -33,6 +36,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
return array(
new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
new \Twig_SimpleFunction('count_tags', [$this, 'countTags']),
new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']),
);
}
@ -107,6 +111,40 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
return $this->tagRepository->countAllTags($user->getId());
}
/**
* Display a single line about reading stats.
*
* @return string
*/
public function displayStats()
{
$user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
if (null === $user || !is_object($user)) {
return 0;
}
$query = $this->entryRepository->getBuilderForArchiveByUser($user->getId())
->select('e.id')
->groupBy('e.id')
->getQuery();
$query->useQueryCache(true);
$query->useResultCache(true);
$query->setResultCacheLifetime($this->lifeTime);
$nbArchives = count($query->getArrayResult());
$interval = $user->getCreatedAt()->diff(new \DateTime('now'));
$nbDays = (int) $interval->format('%a') ?: 1;
return $this->translator->trans('footer.stats', [
'%user_creation%' => $user->getCreatedAt()->format('F jS, Y'),
'%nb_archives%' => $nbArchives,
'%per_day%' => round($nbArchives / $nbDays, 2),
]);
}
public function getName()
{
return 'wallabag_extension';

View file

@ -8,7 +8,23 @@ class WallabagExtensionTest extends \PHPUnit_Framework_TestCase
{
public function testRemoveWww()
{
$extension = new WallabagExtension();
$entryRepository = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
->disableOriginalConstructor()
->getMock();
$tagRepository = $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
->disableOriginalConstructor()
->getMock();
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
->disableOriginalConstructor()
->getMock();
$translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface')
->disableOriginalConstructor()
->getMock();
$extension = new WallabagExtension($entryRepository, $tagRepository, $tokenStorage, 0, $translator);
$this->assertEquals('lemonde.fr', $extension->removeWww('www.lemonde.fr'));
$this->assertEquals('lemonde.fr', $extension->removeWww('lemonde.fr'));