aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php14
-rw-r--r--src/Wallabag/CoreBundle/Controller/EntryController.php10
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php60
-rw-r--r--src/Wallabag/CoreBundle/Resources/config/services.yml7
-rw-r--r--src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php84
5 files changed, 156 insertions, 19 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index 7452c82a..349229f3 100644
--- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
@@ -146,16 +146,10 @@ class WallabagRestController extends Controller
146 { 146 {
147 $url = $request->request->get('url'); 147 $url = $request->request->get('url');
148 148
149 $content = $this->get('wallabag_core.graby')->fetchContent($url); 149 $entry = $this->get('wallabag_core.content_proxy')->updateEntry(
150 150 new Entry($this->getUser()),
151 $entry = new Entry($this->getUser()); 151 $url
152 $entry->setUrl($content['url'] ?: $url); 152 );
153 $entry->setTitle($request->request->get('title') ?: $content['title']);
154 $entry->setContent($content['html']);
155 $entry->setMimetype($content['content_type']);
156 if (isset($content['open_graph']['og_image'])) {
157 $entry->setPreviewPicture($content['open_graph']['og_image']);
158 }
159 153
160 $tags = $request->request->get('tags', ''); 154 $tags = $request->request->get('tags', '');
161 if (!empty($tags)) { 155 if (!empty($tags)) {
diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php
index e6580a57..b9e4e67e 100644
--- a/src/Wallabag/CoreBundle/Controller/EntryController.php
+++ b/src/Wallabag/CoreBundle/Controller/EntryController.php
@@ -30,15 +30,7 @@ class EntryController extends Controller
30 $form->handleRequest($request); 30 $form->handleRequest($request);
31 31
32 if ($form->isValid()) { 32 if ($form->isValid()) {
33 $content = $this->get('wallabag_core.graby')->fetchContent($entry->getUrl()); 33 $entry = $this->get('wallabag_core.content_proxy')->updateEntry($entry, $entry->getUrl());
34
35 $entry->setUrl($content['url'] ?: $entry->getUrl());
36 $entry->setTitle($content['title']);
37 $entry->setContent($content['html']);
38 $entry->setMimetype($content['content_type']);
39 if (isset($content['open_graph']['og_image'])) {
40 $entry->setPreviewPicture($content['open_graph']['og_image']);
41 }
42 34
43 $em = $this->getDoctrine()->getManager(); 35 $em = $this->getDoctrine()->getManager();
44 $em->persist($entry); 36 $em->persist($entry);
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
new file mode 100644
index 00000000..2dd70e51
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -0,0 +1,60 @@
1<?php
2
3namespace Wallabag\CoreBundle\Helper;
4
5use Graby\Graby;
6use Wallabag\CoreBundle\Entity\Entry;
7
8/**
9 * This kind of proxy class take care of getting the content from an url
10 * and update the entry with what it found
11 */
12class ContentProxy
13{
14 protected $graby;
15
16 public function __construct(Graby $graby)
17 {
18 $this->graby = $graby;
19 }
20
21 /**
22 * Fetch content using graby and hydrate given entry with results information.
23 * In case we couldn't find content, we'll try to use Open Graph data
24 *
25 * @param Entry $entry Entry to update
26 * @param string $url Url to grab content for
27 *
28 * @return Entry
29 */
30 public function updateEntry(Entry $entry, $url)
31 {
32 $content = $this->graby->fetchContent($url);
33
34 $title = $content['title'];
35 if (!$title && isset($content['open_graph']['og_title'])) {
36 $title = $content['open_graph']['og_title'];
37 }
38
39 $html = $content['html'];
40 if (false === $html) {
41 $html = '<p>Unable to retrieve readable content.</p>';
42
43 if (isset($content['open_graph']['og_description'])) {
44 $html .= '<p><i>But we found a short description: </i></p>';
45 $html .= $content['open_graph']['og_description'];
46 }
47 }
48
49 $entry->setUrl($content['url'] ?: $url);
50 $entry->setTitle($title);
51 $entry->setContent($html);
52 $entry->setMimetype($content['content_type']);
53
54 if (isset($content['open_graph']['og_image'])) {
55 $entry->setPreviewPicture($content['open_graph']['og_image']);
56 }
57
58 return $entry;
59 }
60}
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml
index 6b8774f2..3beb5d0e 100644
--- a/src/Wallabag/CoreBundle/Resources/config/services.yml
+++ b/src/Wallabag/CoreBundle/Resources/config/services.yml
@@ -33,3 +33,10 @@ services:
33 33
34 wallabag_core.graby: 34 wallabag_core.graby:
35 class: Graby\Graby 35 class: Graby\Graby
36 arguments:
37 - { error_message: false }
38
39 wallabag_core.content_proxy:
40 class: Wallabag\CoreBundle\Helper\ContentProxy
41 arguments:
42 - @wallabag_core.graby
diff --git a/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php b/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php
new file mode 100644
index 00000000..7c93f460
--- /dev/null
+++ b/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php
@@ -0,0 +1,84 @@
1<?php
2
3namespace Wallabag\CoreBundle\Tests\Helper;
4
5use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6use Wallabag\CoreBundle\Entity\Entry;
7use Wallabag\CoreBundle\Entity\User;
8use Wallabag\CoreBundle\Helper\ContentProxy;
9
10class ContentProxyTest extends KernelTestCase
11{
12 public function testWithEmptyContent()
13 {
14 $graby = $this->getMockBuilder('Graby\Graby')
15 ->setMethods(array('fetchContent'))
16 ->disableOriginalConstructor()
17 ->getMock();
18
19 $graby->expects($this->any())
20 ->method('fetchContent')
21 ->willReturn(array('html' => false, 'title' => '', 'url' => '', 'content_type' => ''));
22
23 $proxy = new ContentProxy($graby);
24 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
25
26 $this->assertEquals('http://0.0.0.0', $entry->getUrl());
27 $this->assertEmpty($entry->getTitle());
28 $this->assertEquals('<p>Unable to retrieve readable content.</p>', $entry->getContent());
29 $this->assertEmpty($entry->getPreviewPicture());
30 $this->assertEmpty($entry->getMimetype());
31 }
32
33 public function testWithEmptyContentButOG()
34 {
35 $graby = $this->getMockBuilder('Graby\Graby')
36 ->setMethods(array('fetchContent'))
37 ->disableOriginalConstructor()
38 ->getMock();
39
40 $graby->expects($this->any())
41 ->method('fetchContent')
42 ->willReturn(array('html' => false, 'title' => '', 'url' => '', 'content_type' => '', 'open_graph' => array('og_title' => 'my title', 'og_description' => 'desc')));
43
44 $proxy = new ContentProxy($graby);
45 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
46
47 $this->assertEquals('http://0.0.0.0', $entry->getUrl());
48 $this->assertEquals('my title', $entry->getTitle());
49 $this->assertEquals('<p>Unable to retrieve readable content.</p><p><i>But we found a short description: </i></p>desc', $entry->getContent());
50 $this->assertEmpty($entry->getPreviewPicture());
51 $this->assertEmpty($entry->getMimetype());
52 }
53
54 public function testWithContent()
55 {
56 $graby = $this->getMockBuilder('Graby\Graby')
57 ->setMethods(array('fetchContent'))
58 ->disableOriginalConstructor()
59 ->getMock();
60
61 $graby->expects($this->any())
62 ->method('fetchContent')
63 ->willReturn(array(
64 'html' => 'this is my content',
65 'title' => 'this is my title',
66 'url' => 'http://1.1.1.1',
67 'content_type' => 'text/html',
68 'open_graph' => array(
69 'og_title' => 'my OG title',
70 'og_description' => 'OG desc',
71 'og_image' => 'http://3.3.3.3/cover.jpg'
72 )
73 ));
74
75 $proxy = new ContentProxy($graby);
76 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
77
78 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
79 $this->assertEquals('this is my title', $entry->getTitle());
80 $this->assertEquals('this is my content', $entry->getContent());
81 $this->assertEquals('http://3.3.3.3/cover.jpg', $entry->getPreviewPicture());
82 $this->assertEquals('text/html', $entry->getMimetype());
83 }
84}