]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Some cleanup
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / ContentProxy.php
CommitLineData
558d9aab
JB
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use Graby\Graby;
1c9cd2a7 6use Psr\Log\LoggerInterface as Logger;
558d9aab 7use Wallabag\CoreBundle\Entity\Entry;
c2656f96 8use Wallabag\CoreBundle\Entity\Tag;
da3d4998 9use Wallabag\CoreBundle\Tools\Utils;
c2656f96 10use Wallabag\CoreBundle\Repository\TagRepository;
558d9aab
JB
11
12/**
13 * This kind of proxy class take care of getting the content from an url
f1e29e69 14 * and update the entry with what it found.
558d9aab
JB
15 */
16class ContentProxy
17{
18 protected $graby;
c3510620 19 protected $tagger;
1c9cd2a7 20 protected $logger;
c2656f96 21 protected $tagRepository;
558d9aab 22
c2656f96 23 public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, Logger $logger)
558d9aab 24 {
347fa6be 25 $this->graby = $graby;
c3510620 26 $this->tagger = $tagger;
1c9cd2a7 27 $this->logger = $logger;
c2656f96 28 $this->tagRepository = $tagRepository;
558d9aab
JB
29 }
30
31 /**
32 * Fetch content using graby and hydrate given entry with results information.
f1e29e69 33 * In case we couldn't find content, we'll try to use Open Graph data.
558d9aab 34 *
f1e29e69
JB
35 * @param Entry $entry Entry to update
36 * @param string $url Url to grab content for
558d9aab
JB
37 *
38 * @return Entry
39 */
40 public function updateEntry(Entry $entry, $url)
41 {
42 $content = $this->graby->fetchContent($url);
43
44 $title = $content['title'];
45 if (!$title && isset($content['open_graph']['og_title'])) {
46 $title = $content['open_graph']['og_title'];
47 }
48
49 $html = $content['html'];
50 if (false === $html) {
51 $html = '<p>Unable to retrieve readable content.</p>';
52
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'];
56 }
57 }
58
59 $entry->setUrl($content['url'] ?: $url);
60 $entry->setTitle($title);
61 $entry->setContent($html);
98f0929f 62 $entry->setLanguage($content['language']);
558d9aab 63 $entry->setMimetype($content['content_type']);
da3d4998
JB
64 $entry->setReadingTime(Utils::getReadingTime($html));
65 $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST));
558d9aab
JB
66
67 if (isset($content['open_graph']['og_image'])) {
68 $entry->setPreviewPicture($content['open_graph']['og_image']);
69 }
70
1c9cd2a7
KG
71 try {
72 $this->tagger->tag($entry);
73 } catch (\Exception $e) {
74 $this->logger->error('Error while trying to automatically tag an entry.', array(
75 'entry_url' => $url,
76 'error_msg' => $e->getMessage(),
77 ));
78 }
c3510620 79
558d9aab
JB
80 return $entry;
81 }
c2656f96
JB
82
83 /**
84 * Assign some tags to an entry.
85 *
86 * @param Entry $entry
87 * @param array|string $tags An array of tag or a string coma separated of tag
88 */
89 public function assignTagsToEntry(Entry $entry, $tags)
90 {
91 if (!is_array($tags)) {
92 $tags = explode(',', $tags);
93 }
94
95 foreach ($tags as $label) {
96 $label = trim($label);
97
98 // avoid empty tag
99 if (0 === strlen($label)) {
100 continue;
101 }
102
103 $tagEntity = $this->tagRepository->findOneByLabel($label);
104
105 if (is_null($tagEntity)) {
106 $tagEntity = new Tag();
107 $tagEntity->setLabel($label);
108 }
109
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);
113 }
114 }
115 }
558d9aab 116}