]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/CoreBundle/Helper/ContentProxy.php
changed table name for tagging rule
[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;
da3d4998 8use Wallabag\CoreBundle\Tools\Utils;
558d9aab
JB
9
10/**
11 * This kind of proxy class take care of getting the content from an url
f1e29e69 12 * and update the entry with what it found.
558d9aab
JB
13 */
14class ContentProxy
15{
16 protected $graby;
c3510620 17 protected $tagger;
1c9cd2a7 18 protected $logger;
558d9aab 19
1c9cd2a7 20 public function __construct(Graby $graby, RuleBasedTagger $tagger, Logger $logger)
558d9aab 21 {
c3510620
KG
22 $this->graby = $graby;
23 $this->tagger = $tagger;
1c9cd2a7 24 $this->logger = $logger;
558d9aab
JB
25 }
26
27 /**
28 * Fetch content using graby and hydrate given entry with results information.
f1e29e69 29 * In case we couldn't find content, we'll try to use Open Graph data.
558d9aab 30 *
f1e29e69
JB
31 * @param Entry $entry Entry to update
32 * @param string $url Url to grab content for
558d9aab
JB
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);
98f0929f 58 $entry->setLanguage($content['language']);
558d9aab 59 $entry->setMimetype($content['content_type']);
da3d4998
JB
60 $entry->setReadingTime(Utils::getReadingTime($html));
61 $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST));
558d9aab
JB
62
63 if (isset($content['open_graph']['og_image'])) {
64 $entry->setPreviewPicture($content['open_graph']['og_image']);
65 }
66
1c9cd2a7
KG
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 }
c3510620 75
558d9aab
JB
76 return $entry;
77 }
78}