]>
Commit | Line | Data |
---|---|---|
558d9aab JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | |
4 | ||
5 | use Graby\Graby; | |
6 | use Wallabag\CoreBundle\Entity\Entry; | |
da3d4998 | 7 | use Wallabag\CoreBundle\Tools\Utils; |
558d9aab JB |
8 | |
9 | /** | |
10 | * This kind of proxy class take care of getting the content from an url | |
f1e29e69 | 11 | * and update the entry with what it found. |
558d9aab JB |
12 | */ |
13 | class ContentProxy | |
14 | { | |
15 | protected $graby; | |
c3510620 | 16 | protected $tagger; |
558d9aab | 17 | |
c3510620 | 18 | public function __construct(Graby $graby, RuleBasedTagger $tagger) |
558d9aab | 19 | { |
c3510620 KG |
20 | $this->graby = $graby; |
21 | $this->tagger = $tagger; | |
558d9aab JB |
22 | } |
23 | ||
24 | /** | |
25 | * Fetch content using graby and hydrate given entry with results information. | |
f1e29e69 | 26 | * In case we couldn't find content, we'll try to use Open Graph data. |
558d9aab | 27 | * |
f1e29e69 JB |
28 | * @param Entry $entry Entry to update |
29 | * @param string $url Url to grab content for | |
558d9aab JB |
30 | * |
31 | * @return Entry | |
32 | */ | |
33 | public function updateEntry(Entry $entry, $url) | |
34 | { | |
35 | $content = $this->graby->fetchContent($url); | |
36 | ||
37 | $title = $content['title']; | |
38 | if (!$title && isset($content['open_graph']['og_title'])) { | |
39 | $title = $content['open_graph']['og_title']; | |
40 | } | |
41 | ||
42 | $html = $content['html']; | |
43 | if (false === $html) { | |
44 | $html = '<p>Unable to retrieve readable content.</p>'; | |
45 | ||
46 | if (isset($content['open_graph']['og_description'])) { | |
47 | $html .= '<p><i>But we found a short description: </i></p>'; | |
48 | $html .= $content['open_graph']['og_description']; | |
49 | } | |
50 | } | |
51 | ||
52 | $entry->setUrl($content['url'] ?: $url); | |
53 | $entry->setTitle($title); | |
54 | $entry->setContent($html); | |
98f0929f | 55 | $entry->setLanguage($content['language']); |
558d9aab | 56 | $entry->setMimetype($content['content_type']); |
da3d4998 JB |
57 | $entry->setReadingTime(Utils::getReadingTime($html)); |
58 | $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST)); | |
558d9aab JB |
59 | |
60 | if (isset($content['open_graph']['og_image'])) { | |
61 | $entry->setPreviewPicture($content['open_graph']['og_image']); | |
62 | } | |
63 | ||
c3510620 KG |
64 | $this->tagger->tag($entry); |
65 | ||
558d9aab JB |
66 | return $entry; |
67 | } | |
68 | } |