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 * We can also force the content, in case of an import from the v1 for example, so the function won't
36 * fetch the content from the website but rather use information given with the $content parameter.
38 * @param Entry $entry Entry to update
39 * @param string $url Url to grab content for
40 * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url
44 public function updateEntry(Entry
$entry, $url, array $content = [])
46 // do we have to fetch the content or the provided one is ok?
47 if (empty($content) || false === $this->validateContent($content)) {
48 $content = $this->graby
->fetchContent($url);
51 $title = $content['title'];
52 if (!$title && isset($content['open_graph']['og_title'])) {
53 $title = $content['open_graph']['og_title'];
56 $html = $content['html'];
57 if (false === $html) {
58 $html = '<p>Unable to retrieve readable content.</p>';
60 if (isset($content['open_graph']['og_description'])) {
61 $html .= '<p><i>But we found a short description: </i></p>';
62 $html .= $content['open_graph']['og_description'];
66 $entry->setUrl($content['url'] ?: $url);
67 $entry->setTitle($title);
68 $entry->setContent($html);
69 $entry->setLanguage($content['language']);
70 $entry->setMimetype($content['content_type']);
71 $entry->setReadingTime(Utils
::getReadingTime($html));
73 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST
);
74 if (false !== $domainName) {
75 $entry->setDomainName($domainName);
78 if (isset($content['open_graph']['og_image'])) {
79 $entry->setPreviewPicture($content['open_graph']['og_image']);
83 $this->tagger
->tag($entry);
84 } catch (\Exception
$e) {
85 $this->logger
->error('Error while trying to automatically tag an entry.', [
87 'error_msg' => $e->getMessage(),
95 * Assign some tags to an entry.
98 * @param array|string $tags An array of tag or a string coma separated of tag
99 * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed
100 * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101
102 public function assignTagsToEntry(Entry
$entry, $tags, array $entitiesReady = [])
104 if (!is_array($tags)) {
105 $tags = explode(',', $tags);
108 // keeps only Tag entity from the "not yet flushed entities"
109 $tagsNotYetFlushed = [];
110 foreach ($entitiesReady as $entity) {
111 if ($entity instanceof Tag
) {
112 $tagsNotYetFlushed[$entity->getLabel()] = $entity;
116 foreach ($tags as $label) {
117 $label = trim($label);
120 if (0 === strlen($label)) {
124 if (isset($tagsNotYetFlushed[$label])) {
125 $tagEntity = $tagsNotYetFlushed[$label];
127 $tagEntity = $this->tagRepository
->findOneByLabel($label);
129 if (is_null($tagEntity)) {
130 $tagEntity = new Tag();
131 $tagEntity->setLabel($label);
135 // only add the tag on the entry if the relation doesn't exist
136 if (false === $entry->getTags()->contains($tagEntity)) {
137 $entry->addTag($tagEntity);
143 * Validate that the given content as enough value to be used
144 * instead of fetch the content from the url.
146 * @param array $content
148 * @return bool true if valid otherwise false
150 private function validateContent(array $content)
152 return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']);