3 namespace Wallabag\CoreBundle\Helper
;
6 use Psr\Log\LoggerInterface
as Logger
;
7 use Wallabag\CoreBundle\Entity\Entry
;
8 use Wallabag\CoreBundle\Entity\Tag
;
9 use Wallabag\CoreBundle\Tools\Utils
;
10 use Wallabag\CoreBundle\Repository\TagRepository
;
13 * This kind of proxy class take care of getting the content from an url
14 * and update the entry with what it found.
21 protected $tagRepository;
23 public function __construct(Graby
$graby, RuleBasedTagger
$tagger, TagRepository
$tagRepository, Logger
$logger)
25 $this->graby
= $graby;
26 $this->tagger
= $tagger;
27 $this->logger
= $logger;
28 $this->tagRepository
= $tagRepository;
32 * Fetch content using graby and hydrate given entry with results information.
33 * In case we couldn't find content, we'll try to use Open Graph data.
35 * @param Entry $entry Entry to update
36 * @param string $url Url to grab content for
40 public function updateEntry(Entry
$entry, $url)
42 $content = $this->graby
->fetchContent($url);
44 $title = $content['title'];
45 if (!$title && isset($content['open_graph']['og_title'])) {
46 $title = $content['open_graph']['og_title'];
49 $html = $content['html'];
50 if (false === $html) {
51 $html = '<p>Unable to retrieve readable content.</p>';
53 if (isset($content['open_graph']['og_description'])) {
54 $html .= '<p><i>But we found a short description: </i></p>';
55 $html .= $content['open_graph']['og_description'];
59 $entry->setUrl($content['url'] ?: $url);
60 $entry->setTitle($title);
61 $entry->setContent($html);
62 $entry->setLanguage($content['language']);
63 $entry->setMimetype($content['content_type']);
64 $entry->setReadingTime(Utils
::getReadingTime($html));
65 $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST
));
67 if (isset($content['open_graph']['og_image'])) {
68 $entry->setPreviewPicture($content['open_graph']['og_image']);
72 $this->tagger
->tag($entry);
73 } catch (\Exception
$e) {
74 $this->logger
->error('Error while trying to automatically tag an entry.', array(
76 'error_msg' => $e->getMessage(),
84 * Assign some tags to an entry.
87 * @param array|string $tags An array of tag or a string coma separated of tag
89 public function assignTagsToEntry(Entry
$entry, $tags)
91 if (!is_array($tags)) {
92 $tags = explode(',', $tags);
95 foreach ($tags as $label) {
96 $label = trim($label);
99 if (0 === strlen($label)) {
103 $tagEntity = $this->tagRepository
->findOneByLabel($label);
105 if (is_null($tagEntity)) {
106 $tagEntity = new Tag();
107 $tagEntity->setLabel($label);
110 // only add the tag on the entry if the relation doesn't exist
111 if (false === $entry->getTags()->contains($tagEntity)) {
112 $entry->addTag($tagEntity);