aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/ApiBundle/Controller/WallabagRestController.php31
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php40
-rw-r--r--src/Wallabag/CoreBundle/Resources/config/services.yml1
-rw-r--r--src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php106
-rw-r--r--src/Wallabag/ImportBundle/Import/PocketImport.php26
-rw-r--r--src/Wallabag/ImportBundle/Import/WallabagV1Import.php28
-rw-r--r--src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php18
7 files changed, 158 insertions, 92 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
index 84bc14a9..03990088 100644
--- a/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
+++ b/src/Wallabag/ApiBundle/Controller/WallabagRestController.php
@@ -15,31 +15,6 @@ use Wallabag\CoreBundle\Entity\Tag;
15 15
16class WallabagRestController extends FOSRestController 16class WallabagRestController extends FOSRestController
17{ 17{
18 /**
19 * @param Entry $entry
20 * @param string $tags
21 */
22 private function assignTagsToEntry(Entry $entry, $tags)
23 {
24 foreach (explode(',', $tags) as $label) {
25 $label = trim($label);
26 $tagEntity = $this
27 ->getDoctrine()
28 ->getRepository('WallabagCoreBundle:Tag')
29 ->findOneByLabel($label);
30
31 if (is_null($tagEntity)) {
32 $tagEntity = new Tag();
33 $tagEntity->setLabel($label);
34 }
35
36 // only add the tag on the entry if the relation doesn't exist
37 if (!$entry->getTags()->contains($tagEntity)) {
38 $entry->addTag($tagEntity);
39 }
40 }
41 }
42
43 private function validateAuthentication() 18 private function validateAuthentication()
44 { 19 {
45 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) { 20 if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
@@ -140,7 +115,7 @@ class WallabagRestController extends FOSRestController
140 115
141 $tags = $request->request->get('tags', ''); 116 $tags = $request->request->get('tags', '');
142 if (!empty($tags)) { 117 if (!empty($tags)) {
143 $this->assignTagsToEntry($entry, $tags); 118 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
144 } 119 }
145 120
146 $em = $this->getDoctrine()->getManager(); 121 $em = $this->getDoctrine()->getManager();
@@ -192,7 +167,7 @@ class WallabagRestController extends FOSRestController
192 167
193 $tags = $request->request->get('tags', ''); 168 $tags = $request->request->get('tags', '');
194 if (!empty($tags)) { 169 if (!empty($tags)) {
195 $this->assignTagsToEntry($entry, $tags); 170 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
196 } 171 }
197 172
198 $em = $this->getDoctrine()->getManager(); 173 $em = $this->getDoctrine()->getManager();
@@ -270,7 +245,7 @@ class WallabagRestController extends FOSRestController
270 245
271 $tags = $request->request->get('tags', ''); 246 $tags = $request->request->get('tags', '');
272 if (!empty($tags)) { 247 if (!empty($tags)) {
273 $this->assignTagsToEntry($entry, $tags); 248 $this->get('wallabag_core.content_proxy')->assignTagsToEntry($entry, $tags);
274 } 249 }
275 250
276 $em = $this->getDoctrine()->getManager(); 251 $em = $this->getDoctrine()->getManager();
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index bd8b993a..ba90b731 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -5,7 +5,9 @@ namespace Wallabag\CoreBundle\Helper;
5use Graby\Graby; 5use Graby\Graby;
6use Psr\Log\LoggerInterface as Logger; 6use Psr\Log\LoggerInterface as Logger;
7use Wallabag\CoreBundle\Entity\Entry; 7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\Tag;
8use Wallabag\CoreBundle\Tools\Utils; 9use Wallabag\CoreBundle\Tools\Utils;
10use Wallabag\CoreBundle\Repository\TagRepository;
9 11
10/** 12/**
11 * This kind of proxy class take care of getting the content from an url 13 * This kind of proxy class take care of getting the content from an url
@@ -16,12 +18,14 @@ class ContentProxy
16 protected $graby; 18 protected $graby;
17 protected $tagger; 19 protected $tagger;
18 protected $logger; 20 protected $logger;
21 protected $tagRepository;
19 22
20 public function __construct(Graby $graby, RuleBasedTagger $tagger, Logger $logger) 23 public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, Logger $logger)
21 { 24 {
22 $this->graby = $graby; 25 $this->graby = $graby;
23 $this->tagger = $tagger; 26 $this->tagger = $tagger;
24 $this->logger = $logger; 27 $this->logger = $logger;
28 $this->tagRepository = $tagRepository;
25 } 29 }
26 30
27 /** 31 /**
@@ -75,4 +79,38 @@ class ContentProxy
75 79
76 return $entry; 80 return $entry;
77 } 81 }
82
83 /**
84 * Assign some tags to an entry.
85 *
86 * @param Entry $entry
87 * @param array|string $tags An array of tag or a string coma separated of tag
88 */
89 public function assignTagsToEntry(Entry $entry, $tags)
90 {
91 if (!is_array($tags)) {
92 $tags = explode(',', $tags);
93 }
94
95 foreach ($tags as $label) {
96 $label = trim($label);
97
98 // avoid empty tag
99 if (0 === strlen($label)) {
100 continue;
101 }
102
103 $tagEntity = $this->tagRepository->findOneByLabel($label);
104
105 if (is_null($tagEntity)) {
106 $tagEntity = new Tag();
107 $tagEntity->setLabel($label);
108 }
109
110 // only add the tag on the entry if the relation doesn't exist
111 if (false === $entry->getTags()->contains($tagEntity)) {
112 $entry->addTag($tagEntity);
113 }
114 }
115 }
78} 116}
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml
index a8796fe4..1aa66be1 100644
--- a/src/Wallabag/CoreBundle/Resources/config/services.yml
+++ b/src/Wallabag/CoreBundle/Resources/config/services.yml
@@ -50,6 +50,7 @@ services:
50 arguments: 50 arguments:
51 - "@wallabag_core.graby" 51 - "@wallabag_core.graby"
52 - "@wallabag_core.rule_based_tagger" 52 - "@wallabag_core.rule_based_tagger"
53 - "@wallabag_core.tag_repository"
53 - "@logger" 54 - "@logger"
54 55
55 wallabag_core.rule_based_tagger: 56 wallabag_core.rule_based_tagger:
diff --git a/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php b/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php
index d29984e9..f58b5828 100644
--- a/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php
+++ b/src/Wallabag/CoreBundle/Tests/Helper/ContentProxyTest.php
@@ -3,8 +3,9 @@
3namespace Wallabag\CoreBundle\Tests\Helper; 3namespace Wallabag\CoreBundle\Tests\Helper;
4 4
5use Psr\Log\NullLogger; 5use Psr\Log\NullLogger;
6use Wallabag\CoreBundle\Entity\Entry;
7use Wallabag\CoreBundle\Helper\ContentProxy; 6use Wallabag\CoreBundle\Helper\ContentProxy;
7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\CoreBundle\Entity\Tag;
8use Wallabag\UserBundle\Entity\User; 9use Wallabag\UserBundle\Entity\User;
9 10
10class ContentProxyTest extends \PHPUnit_Framework_TestCase 11class ContentProxyTest extends \PHPUnit_Framework_TestCase
@@ -30,7 +31,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
30 'language' => '', 31 'language' => '',
31 )); 32 ));
32 33
33 $proxy = new ContentProxy($graby, $tagger, $this->getLogger()); 34 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
34 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); 35 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
35 36
36 $this->assertEquals('http://0.0.0.0', $entry->getUrl()); 37 $this->assertEquals('http://0.0.0.0', $entry->getUrl());
@@ -68,7 +69,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
68 ), 69 ),
69 )); 70 ));
70 71
71 $proxy = new ContentProxy($graby, $tagger, $this->getLogger()); 72 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
72 $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io'); 73 $entry = $proxy->updateEntry(new Entry(new User()), 'http://domain.io');
73 74
74 $this->assertEquals('http://domain.io', $entry->getUrl()); 75 $this->assertEquals('http://domain.io', $entry->getUrl());
@@ -107,7 +108,7 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
107 ), 108 ),
108 )); 109 ));
109 110
110 $proxy = new ContentProxy($graby, $tagger, $this->getLogger()); 111 $proxy = new ContentProxy($graby, $tagger, $this->getTagRepositoryMock(), $this->getLogger());
111 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0'); 112 $entry = $proxy->updateEntry(new Entry(new User()), 'http://0.0.0.0');
112 113
113 $this->assertEquals('http://1.1.1.1', $entry->getUrl()); 114 $this->assertEquals('http://1.1.1.1', $entry->getUrl());
@@ -120,6 +121,96 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
120 $this->assertEquals('1.1.1.1', $entry->getDomainName()); 121 $this->assertEquals('1.1.1.1', $entry->getDomainName());
121 } 122 }
122 123
124 public function testAssignTagsWithArrayAndExtraSpaces()
125 {
126 $graby = $this->getMockBuilder('Graby\Graby')
127 ->disableOriginalConstructor()
128 ->getMock();
129
130 $tagRepo = $this->getTagRepositoryMock();
131 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
132
133 $entry = new Entry(new User());
134
135 $proxy->assignTagsToEntry($entry, array(' tag1', 'tag2 '));
136
137 $this->assertCount(2, $entry->getTags());
138 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
139 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
140 }
141
142 public function testAssignTagsWithString()
143 {
144 $graby = $this->getMockBuilder('Graby\Graby')
145 ->disableOriginalConstructor()
146 ->getMock();
147
148 $tagRepo = $this->getTagRepositoryMock();
149 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
150
151 $entry = new Entry(new User());
152
153 $proxy->assignTagsToEntry($entry, 'tag1, tag2');
154
155 $this->assertCount(2, $entry->getTags());
156 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
157 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
158 }
159
160 public function testAssignTagsWithEmptyArray()
161 {
162 $graby = $this->getMockBuilder('Graby\Graby')
163 ->disableOriginalConstructor()
164 ->getMock();
165
166 $tagRepo = $this->getTagRepositoryMock();
167 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
168
169 $entry = new Entry(new User());
170
171 $proxy->assignTagsToEntry($entry, array());
172
173 $this->assertCount(0, $entry->getTags());
174 }
175
176 public function testAssignTagsWithEmptyString()
177 {
178 $graby = $this->getMockBuilder('Graby\Graby')
179 ->disableOriginalConstructor()
180 ->getMock();
181
182 $tagRepo = $this->getTagRepositoryMock();
183 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
184
185 $entry = new Entry(new User());
186
187 $proxy->assignTagsToEntry($entry, '');
188
189 $this->assertCount(0, $entry->getTags());
190 }
191
192 public function testAssignTagsAlreadyAssigned()
193 {
194 $graby = $this->getMockBuilder('Graby\Graby')
195 ->disableOriginalConstructor()
196 ->getMock();
197
198 $tagRepo = $this->getTagRepositoryMock();
199 $proxy = new ContentProxy($graby, $this->getTaggerMock(), $tagRepo, $this->getLogger());
200
201 $tagEntity = new Tag();
202 $tagEntity->setLabel('tag1');
203
204 $entry = new Entry(new User());
205 $entry->addTag($tagEntity);
206
207 $proxy->assignTagsToEntry($entry, 'tag1, tag2');
208
209 $this->assertCount(2, $entry->getTags());
210 $this->assertEquals('tag1', $entry->getTags()[0]->getLabel());
211 $this->assertEquals('tag2', $entry->getTags()[1]->getLabel());
212 }
213
123 private function getTaggerMock() 214 private function getTaggerMock()
124 { 215 {
125 return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger') 216 return $this->getMockBuilder('Wallabag\CoreBundle\Helper\RuleBasedTagger')
@@ -128,6 +219,13 @@ class ContentProxyTest extends \PHPUnit_Framework_TestCase
128 ->getMock(); 219 ->getMock();
129 } 220 }
130 221
222 private function getTagRepositoryMock()
223 {
224 return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
225 ->disableOriginalConstructor()
226 ->getMock();
227 }
228
131 private function getLogger() 229 private function getLogger()
132 { 230 {
133 return new NullLogger(); 231 return new NullLogger();
diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php
index 22932238..5dfd098c 100644
--- a/src/Wallabag/ImportBundle/Import/PocketImport.php
+++ b/src/Wallabag/ImportBundle/Import/PocketImport.php
@@ -9,7 +9,6 @@ use GuzzleHttp\Client;
9use GuzzleHttp\Exception\RequestException; 9use GuzzleHttp\Exception\RequestException;
10use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; 10use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
11use Wallabag\CoreBundle\Entity\Entry; 11use Wallabag\CoreBundle\Entity\Entry;
12use Wallabag\CoreBundle\Entity\Tag;
13use Wallabag\CoreBundle\Helper\ContentProxy; 12use Wallabag\CoreBundle\Helper\ContentProxy;
14use Craue\ConfigBundle\Util\Config; 13use Craue\ConfigBundle\Util\Config;
15 14
@@ -177,26 +176,6 @@ class PocketImport implements ImportInterface
177 $this->client = $client; 176 $this->client = $client;
178 } 177 }
179 178
180 private function assignTagsToEntry(Entry $entry, $tags)
181 {
182 foreach ($tags as $tag) {
183 $label = trim($tag['tag']);
184 $tagEntity = $this->em
185 ->getRepository('WallabagCoreBundle:Tag')
186 ->findOneByLabel($label);
187
188 if (is_object($tagEntity)) {
189 $entry->addTag($tagEntity);
190 } else {
191 $newTag = new Tag();
192 $newTag->setLabel($label);
193
194 $entry->addTag($newTag);
195 }
196 $this->em->flush();
197 }
198 }
199
200 /** 179 /**
201 * @see https://getpocket.com/developer/docs/v3/retrieve 180 * @see https://getpocket.com/developer/docs/v3/retrieve
202 * 181 *
@@ -246,7 +225,10 @@ class PocketImport implements ImportInterface
246 } 225 }
247 226
248 if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) { 227 if (isset($pocketEntry['tags']) && !empty($pocketEntry['tags'])) {
249 $this->assignTagsToEntry($entry, $pocketEntry['tags']); 228 $this->contentProxy->assignTagsToEntry(
229 $entry,
230 array_keys($pocketEntry['tags'])
231 );
250 } 232 }
251 233
252 $this->em->persist($entry); 234 $this->em->persist($entry);
diff --git a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php
index bbac6eaf..05bdb401 100644
--- a/src/Wallabag/ImportBundle/Import/WallabagV1Import.php
+++ b/src/Wallabag/ImportBundle/Import/WallabagV1Import.php
@@ -6,7 +6,6 @@ use Psr\Log\LoggerInterface;
6use Psr\Log\NullLogger; 6use Psr\Log\NullLogger;
7use Doctrine\ORM\EntityManager; 7use Doctrine\ORM\EntityManager;
8use Wallabag\CoreBundle\Entity\Entry; 8use Wallabag\CoreBundle\Entity\Entry;
9use Wallabag\CoreBundle\Entity\Tag;
10use Wallabag\UserBundle\Entity\User; 9use Wallabag\UserBundle\Entity\User;
11use Wallabag\CoreBundle\Tools\Utils; 10use Wallabag\CoreBundle\Tools\Utils;
12use Wallabag\CoreBundle\Helper\ContentProxy; 11use Wallabag\CoreBundle\Helper\ContentProxy;
@@ -144,6 +143,7 @@ class WallabagV1Import implements ImportInterface
144 // @see ContentProxy->updateEntry 143 // @see ContentProxy->updateEntry
145 $entry = new Entry($this->user); 144 $entry = new Entry($this->user);
146 $entry->setUrl($importedEntry['url']); 145 $entry->setUrl($importedEntry['url']);
146
147 if (in_array($importedEntry['title'], $untitled)) { 147 if (in_array($importedEntry['title'], $untitled)) {
148 $entry = $this->contentProxy->updateEntry($entry, $importedEntry['url']); 148 $entry = $this->contentProxy->updateEntry($entry, $importedEntry['url']);
149 } else { 149 } else {
@@ -152,10 +152,14 @@ class WallabagV1Import implements ImportInterface
152 $entry->setReadingTime(Utils::getReadingTime($importedEntry['content'])); 152 $entry->setReadingTime(Utils::getReadingTime($importedEntry['content']));
153 $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST)); 153 $entry->setDomainName(parse_url($importedEntry['url'], PHP_URL_HOST));
154 } 154 }
155
155 if (array_key_exists('tags', $importedEntry) && $importedEntry['tags'] != '') { 156 if (array_key_exists('tags', $importedEntry) && $importedEntry['tags'] != '') {
156 $tags = explode(',', $importedEntry['tags']); 157 $this->contentProxy->assignTagsToEntry(
157 $this->assignTagsToEntry($entry, $tags); 158 $entry,
159 $importedEntry['tags']
160 );
158 } 161 }
162
159 $entry->setArchived($importedEntry['is_read']); 163 $entry->setArchived($importedEntry['is_read']);
160 $entry->setStarred($importedEntry['is_fav']); 164 $entry->setStarred($importedEntry['is_fav']);
161 165
@@ -171,22 +175,4 @@ class WallabagV1Import implements ImportInterface
171 175
172 $this->em->flush(); 176 $this->em->flush();
173 } 177 }
174
175 private function assignTagsToEntry(Entry $entry, $tags)
176 {
177 foreach ($tags as $tag) {
178 $label = trim($tag);
179 $tagEntity = $this->em
180 ->getRepository('WallabagCoreBundle:Tag')
181 ->findOneByLabel($label);
182 if (is_object($tagEntity)) {
183 $entry->addTag($tagEntity);
184 } else {
185 $newTag = new Tag();
186 $newTag->setLabel($label);
187 $entry->addTag($newTag);
188 }
189 $this->em->flush();
190 }
191 }
192} 178}
diff --git a/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php b/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php
index 25359d56..f44786b1 100644
--- a/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php
+++ b/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php
@@ -260,24 +260,10 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase
260 ->method('findByUrlAndUserId') 260 ->method('findByUrlAndUserId')
261 ->will($this->onConsecutiveCalls(false, true)); 261 ->will($this->onConsecutiveCalls(false, true));
262 262
263 $tag = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Tag')
264 ->disableOriginalConstructor()
265 ->getMock();
266
267 $tagRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
268 ->disableOriginalConstructor()
269 ->getMock();
270
271 $tagRepo->expects($this->exactly(2))
272 // the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
273 // to magically call the `findOneBy` with ['label' => 'foo']
274 ->method('__call')
275 ->will($this->onConsecutiveCalls(false, $tag));
276
277 $this->em 263 $this->em
278 ->expects($this->any()) 264 ->expects($this->exactly(2))
279 ->method('getRepository') 265 ->method('getRepository')
280 ->will($this->onConsecutiveCalls($entryRepo, $tagRepo, $tagRepo, $entryRepo)); 266 ->willReturn($entryRepo);
281 267
282 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') 268 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
283 ->disableOriginalConstructor() 269 ->disableOriginalConstructor()