]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\CoreBundle\Helper; | |
4 | ||
5 | use Graby\Graby; | |
6 | use Wallabag\CoreBundle\Entity\Entry; | |
7 | use Wallabag\CoreBundle\Tools\Utils; | |
8 | ||
9 | /** | |
10 | * This kind of proxy class take care of getting the content from an url | |
11 | * and update the entry with what it found. | |
12 | */ | |
13 | class ContentProxy | |
14 | { | |
15 | protected $graby; | |
16 | protected $tagger; | |
17 | ||
18 | public function __construct(Graby $graby, RuleBasedTagger $tagger) | |
19 | { | |
20 | $this->graby = $graby; | |
21 | $this->tagger = $tagger; | |
22 | } | |
23 | ||
24 | /** | |
25 | * Fetch content using graby and hydrate given entry with results information. | |
26 | * In case we couldn't find content, we'll try to use Open Graph data. | |
27 | * | |
28 | * @param Entry $entry Entry to update | |
29 | * @param string $url Url to grab content for | |
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); | |
55 | $entry->setLanguage($content['language']); | |
56 | $entry->setMimetype($content['content_type']); | |
57 | $entry->setReadingTime(Utils::getReadingTime($html)); | |
58 | $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST)); | |
59 | ||
60 | if (isset($content['open_graph']['og_image'])) { | |
61 | $entry->setPreviewPicture($content['open_graph']['og_image']); | |
62 | } | |
63 | ||
64 | $this->tagger->tag($entry); | |
65 | ||
66 | return $entry; | |
67 | } | |
68 | } |