]> git.immae.eu Git - github/wallabag/wallabag.git/blame_incremental - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Enhance documentation and create a form to create a new client
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / ContentProxy.php
... / ...
CommitLineData
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use Graby\Graby;
6use Psr\Log\LoggerInterface as Logger;
7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\Tag;
9use Wallabag\CoreBundle\Tools\Utils;
10use Wallabag\CoreBundle\Repository\TagRepository;
11
12/**
13 * This kind of proxy class take care of getting the content from an url
14 * and update the entry with what it found.
15 */
16class ContentProxy
17{
18 protected $graby;
19 protected $tagger;
20 protected $logger;
21 protected $tagRepository;
22
23 public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, Logger $logger)
24 {
25 $this->graby = $graby;
26 $this->tagger = $tagger;
27 $this->logger = $logger;
28 $this->tagRepository = $tagRepository;
29 }
30
31 /**
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.
34 *
35 * @param Entry $entry Entry to update
36 * @param string $url Url to grab content for
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);
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));
66
67 if (isset($content['open_graph']['og_image'])) {
68 $entry->setPreviewPicture($content['open_graph']['og_image']);
69 }
70
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 }
79
80 return $entry;
81 }
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 }
116}