]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Helper/ContentProxy.php
Merge pull request #1529 from wallabag/v2-taggingrule-tablename
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Helper / ContentProxy.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Helper;
4
5 use Graby\Graby;
6 use Psr\Log\LoggerInterface as Logger;
7 use Wallabag\CoreBundle\Entity\Entry;
8 use Wallabag\CoreBundle\Tools\Utils;
9
10 /**
11 * This kind of proxy class take care of getting the content from an url
12 * and update the entry with what it found.
13 */
14 class ContentProxy
15 {
16 protected $graby;
17 protected $tagger;
18 protected $logger;
19
20 public function __construct(Graby $graby, RuleBasedTagger $tagger, Logger $logger)
21 {
22 $this->graby = $graby;
23 $this->tagger = $tagger;
24 $this->logger = $logger;
25 }
26
27 /**
28 * Fetch content using graby and hydrate given entry with results information.
29 * In case we couldn't find content, we'll try to use Open Graph data.
30 *
31 * @param Entry $entry Entry to update
32 * @param string $url Url to grab content for
33 *
34 * @return Entry
35 */
36 public function updateEntry(Entry $entry, $url)
37 {
38 $content = $this->graby->fetchContent($url);
39
40 $title = $content['title'];
41 if (!$title && isset($content['open_graph']['og_title'])) {
42 $title = $content['open_graph']['og_title'];
43 }
44
45 $html = $content['html'];
46 if (false === $html) {
47 $html = '<p>Unable to retrieve readable content.</p>';
48
49 if (isset($content['open_graph']['og_description'])) {
50 $html .= '<p><i>But we found a short description: </i></p>';
51 $html .= $content['open_graph']['og_description'];
52 }
53 }
54
55 $entry->setUrl($content['url'] ?: $url);
56 $entry->setTitle($title);
57 $entry->setContent($html);
58 $entry->setLanguage($content['language']);
59 $entry->setMimetype($content['content_type']);
60 $entry->setReadingTime(Utils::getReadingTime($html));
61 $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST));
62
63 if (isset($content['open_graph']['og_image'])) {
64 $entry->setPreviewPicture($content['open_graph']['og_image']);
65 }
66
67 try {
68 $this->tagger->tag($entry);
69 } catch (\Exception $e) {
70 $this->logger->error('Error while trying to automatically tag an entry.', array(
71 'entry_url' => $url,
72 'error_msg' => $e->getMessage(),
73 ));
74 }
75
76 return $entry;
77 }
78 }