diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/ContentProxy.php | 137 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/DownloadImages.php | 9 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/HttpClientFactory.php | 29 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php | 5 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/Redirect.php | 10 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php | 10 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/TagsAssigner.php | 75 |
7 files changed, 179 insertions, 96 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php index f222dd88..bfaa1976 100644 --- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php | |||
@@ -5,9 +5,7 @@ namespace Wallabag\CoreBundle\Helper; | |||
5 | use Graby\Graby; | 5 | use Graby\Graby; |
6 | use Psr\Log\LoggerInterface; | 6 | use Psr\Log\LoggerInterface; |
7 | use Wallabag\CoreBundle\Entity\Entry; | 7 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\CoreBundle\Entity\Tag; | ||
9 | use Wallabag\CoreBundle\Tools\Utils; | 8 | use Wallabag\CoreBundle\Tools\Utils; |
10 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
11 | use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser; | 9 | use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeExtensionGuesser; |
12 | 10 | ||
13 | /** | 11 | /** |
@@ -19,37 +17,34 @@ class ContentProxy | |||
19 | protected $graby; | 17 | protected $graby; |
20 | protected $tagger; | 18 | protected $tagger; |
21 | protected $logger; | 19 | protected $logger; |
22 | protected $tagRepository; | ||
23 | protected $mimeGuesser; | 20 | protected $mimeGuesser; |
24 | protected $fetchingErrorMessage; | 21 | protected $fetchingErrorMessage; |
22 | protected $eventDispatcher; | ||
25 | 23 | ||
26 | public function __construct(Graby $graby, RuleBasedTagger $tagger, TagRepository $tagRepository, LoggerInterface $logger, $fetchingErrorMessage) | 24 | public function __construct(Graby $graby, RuleBasedTagger $tagger, LoggerInterface $logger, $fetchingErrorMessage) |
27 | { | 25 | { |
28 | $this->graby = $graby; | 26 | $this->graby = $graby; |
29 | $this->tagger = $tagger; | 27 | $this->tagger = $tagger; |
30 | $this->logger = $logger; | 28 | $this->logger = $logger; |
31 | $this->tagRepository = $tagRepository; | ||
32 | $this->mimeGuesser = new MimeTypeExtensionGuesser(); | 29 | $this->mimeGuesser = new MimeTypeExtensionGuesser(); |
33 | $this->fetchingErrorMessage = $fetchingErrorMessage; | 30 | $this->fetchingErrorMessage = $fetchingErrorMessage; |
34 | } | 31 | } |
35 | 32 | ||
36 | /** | 33 | /** |
37 | * Fetch content using graby and hydrate given entry with results information. | 34 | * Update entry using either fetched or provided content. |
38 | * In case we couldn't find content, we'll try to use Open Graph data. | ||
39 | * | 35 | * |
40 | * We can also force the content, in case of an import from the v1 for example, so the function won't | 36 | * @param Entry $entry Entry to update |
41 | * fetch the content from the website but rather use information given with the $content parameter. | 37 | * @param string $url Url of the content |
42 | * | 38 | * @param array $content Array with content provided for import with AT LEAST keys title, html, url to skip the fetchContent from the url |
43 | * @param Entry $entry Entry to update | 39 | * @param bool $disableContentUpdate Whether to skip trying to fetch content using Graby |
44 | * @param string $url Url to grab content for | ||
45 | * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url | ||
46 | * | ||
47 | * @return Entry | ||
48 | */ | 40 | */ |
49 | public function updateEntry(Entry $entry, $url, array $content = []) | 41 | public function updateEntry(Entry $entry, $url, array $content = [], $disableContentUpdate = false) |
50 | { | 42 | { |
51 | // do we have to fetch the content or the provided one is ok? | 43 | if (!empty($content['html'])) { |
52 | if (empty($content) || false === $this->validateContent($content)) { | 44 | $content['html'] = $this->graby->cleanupHtml($content['html'], $url); |
45 | } | ||
46 | |||
47 | if ((empty($content) || false === $this->validateContent($content)) && false === $disableContentUpdate) { | ||
53 | $fetchedContent = $this->graby->fetchContent($url); | 48 | $fetchedContent = $this->graby->fetchContent($url); |
54 | 49 | ||
55 | // when content is imported, we have information in $content | 50 | // when content is imported, we have information in $content |
@@ -59,8 +54,24 @@ class ContentProxy | |||
59 | } | 54 | } |
60 | } | 55 | } |
61 | 56 | ||
57 | // be sure to keep the url in case of error | ||
58 | // so we'll be able to refetch it in the future | ||
59 | $content['url'] = !empty($content['url']) ? $content['url'] : $url; | ||
60 | |||
61 | $this->stockEntry($entry, $content); | ||
62 | } | ||
63 | |||
64 | /** | ||
65 | * Stock entry with fetched or imported content. | ||
66 | * Will fall back to OpenGraph data if available. | ||
67 | * | ||
68 | * @param Entry $entry Entry to stock | ||
69 | * @param array $content Array with at least title, url & html | ||
70 | */ | ||
71 | private function stockEntry(Entry $entry, array $content) | ||
72 | { | ||
62 | $title = $content['title']; | 73 | $title = $content['title']; |
63 | if (!$title && isset($content['open_graph']['og_title'])) { | 74 | if (!$title && !empty($content['open_graph']['og_title'])) { |
64 | $title = $content['open_graph']['og_title']; | 75 | $title = $content['open_graph']['og_title']; |
65 | } | 76 | } |
66 | 77 | ||
@@ -68,17 +79,40 @@ class ContentProxy | |||
68 | if (false === $html) { | 79 | if (false === $html) { |
69 | $html = $this->fetchingErrorMessage; | 80 | $html = $this->fetchingErrorMessage; |
70 | 81 | ||
71 | if (isset($content['open_graph']['og_description'])) { | 82 | if (!empty($content['open_graph']['og_description'])) { |
72 | $html .= '<p><i>But we found a short description: </i></p>'; | 83 | $html .= '<p><i>But we found a short description: </i></p>'; |
73 | $html .= $content['open_graph']['og_description']; | 84 | $html .= $content['open_graph']['og_description']; |
74 | } | 85 | } |
75 | } | 86 | } |
76 | 87 | ||
77 | $entry->setUrl($content['url'] ?: $url); | 88 | $entry->setUrl($content['url']); |
78 | $entry->setTitle($title); | 89 | $entry->setTitle($title); |
79 | $entry->setContent($html); | 90 | $entry->setContent($html); |
80 | $entry->setHttpStatus(isset($content['status']) ? $content['status'] : ''); | 91 | $entry->setHttpStatus(isset($content['status']) ? $content['status'] : ''); |
81 | 92 | ||
93 | if (!empty($content['date'])) { | ||
94 | $date = $content['date']; | ||
95 | |||
96 | // is it a timestamp? | ||
97 | if (filter_var($date, FILTER_VALIDATE_INT) !== false) { | ||
98 | $date = '@'.$content['date']; | ||
99 | } | ||
100 | |||
101 | try { | ||
102 | $entry->setPublishedAt(new \DateTime($date)); | ||
103 | } catch (\Exception $e) { | ||
104 | $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $content['url'], 'date' => $content['date']]); | ||
105 | } | ||
106 | } | ||
107 | |||
108 | if (!empty($content['authors'])) { | ||
109 | $entry->setPublishedBy($content['authors']); | ||
110 | } | ||
111 | |||
112 | if (!empty($content['all_headers'])) { | ||
113 | $entry->setHeaders($content['all_headers']); | ||
114 | } | ||
115 | |||
82 | $entry->setLanguage(isset($content['language']) ? $content['language'] : ''); | 116 | $entry->setLanguage(isset($content['language']) ? $content['language'] : ''); |
83 | $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : ''); | 117 | $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : ''); |
84 | $entry->setReadingTime(Utils::getReadingTime($html)); | 118 | $entry->setReadingTime(Utils::getReadingTime($html)); |
@@ -88,12 +122,12 @@ class ContentProxy | |||
88 | $entry->setDomainName($domainName); | 122 | $entry->setDomainName($domainName); |
89 | } | 123 | } |
90 | 124 | ||
91 | if (isset($content['open_graph']['og_image']) && $content['open_graph']['og_image']) { | 125 | if (!empty($content['open_graph']['og_image'])) { |
92 | $entry->setPreviewPicture($content['open_graph']['og_image']); | 126 | $entry->setPreviewPicture($content['open_graph']['og_image']); |
93 | } | 127 | } |
94 | 128 | ||
95 | // if content is an image define as a preview too | 129 | // if content is an image define as a preview too |
96 | if (isset($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { | 130 | if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { |
97 | $entry->setPreviewPicture($content['url']); | 131 | $entry->setPreviewPicture($content['url']); |
98 | } | 132 | } |
99 | 133 | ||
@@ -101,65 +135,14 @@ class ContentProxy | |||
101 | $this->tagger->tag($entry); | 135 | $this->tagger->tag($entry); |
102 | } catch (\Exception $e) { | 136 | } catch (\Exception $e) { |
103 | $this->logger->error('Error while trying to automatically tag an entry.', [ | 137 | $this->logger->error('Error while trying to automatically tag an entry.', [ |
104 | 'entry_url' => $url, | 138 | 'entry_url' => $content['url'], |
105 | 'error_msg' => $e->getMessage(), | 139 | 'error_msg' => $e->getMessage(), |
106 | ]); | 140 | ]); |
107 | } | 141 | } |
108 | |||
109 | return $entry; | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * Assign some tags to an entry. | ||
114 | * | ||
115 | * @param Entry $entry | ||
116 | * @param array|string $tags An array of tag or a string coma separated of tag | ||
117 | * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed | ||
118 | * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101 | ||
119 | */ | ||
120 | public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = []) | ||
121 | { | ||
122 | if (!is_array($tags)) { | ||
123 | $tags = explode(',', $tags); | ||
124 | } | ||
125 | |||
126 | // keeps only Tag entity from the "not yet flushed entities" | ||
127 | $tagsNotYetFlushed = []; | ||
128 | foreach ($entitiesReady as $entity) { | ||
129 | if ($entity instanceof Tag) { | ||
130 | $tagsNotYetFlushed[$entity->getLabel()] = $entity; | ||
131 | } | ||
132 | } | ||
133 | |||
134 | foreach ($tags as $label) { | ||
135 | $label = trim($label); | ||
136 | |||
137 | // avoid empty tag | ||
138 | if (0 === strlen($label)) { | ||
139 | continue; | ||
140 | } | ||
141 | |||
142 | if (isset($tagsNotYetFlushed[$label])) { | ||
143 | $tagEntity = $tagsNotYetFlushed[$label]; | ||
144 | } else { | ||
145 | $tagEntity = $this->tagRepository->findOneByLabel($label); | ||
146 | |||
147 | if (is_null($tagEntity)) { | ||
148 | $tagEntity = new Tag(); | ||
149 | $tagEntity->setLabel($label); | ||
150 | } | ||
151 | } | ||
152 | |||
153 | // only add the tag on the entry if the relation doesn't exist | ||
154 | if (false === $entry->getTags()->contains($tagEntity)) { | ||
155 | $entry->addTag($tagEntity); | ||
156 | } | ||
157 | } | ||
158 | } | 142 | } |
159 | 143 | ||
160 | /** | 144 | /** |
161 | * Validate that the given content as enough value to be used | 145 | * Validate that the given content has at least a title, an html and a url. |
162 | * instead of fetch the content from the url. | ||
163 | * | 146 | * |
164 | * @param array $content | 147 | * @param array $content |
165 | * | 148 | * |
@@ -167,6 +150,6 @@ class ContentProxy | |||
167 | */ | 150 | */ |
168 | private function validateContent(array $content) | 151 | private function validateContent(array $content) |
169 | { | 152 | { |
170 | return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']); | 153 | return !empty($content['title']) && !empty($content['html']) && !empty($content['url']); |
171 | } | 154 | } |
172 | } | 155 | } |
diff --git a/src/Wallabag/CoreBundle/Helper/DownloadImages.php b/src/Wallabag/CoreBundle/Helper/DownloadImages.php index 0d330d2a..54e23a05 100644 --- a/src/Wallabag/CoreBundle/Helper/DownloadImages.php +++ b/src/Wallabag/CoreBundle/Helper/DownloadImages.php | |||
@@ -54,7 +54,7 @@ class DownloadImages | |||
54 | $crawler = new Crawler($html); | 54 | $crawler = new Crawler($html); |
55 | $result = $crawler | 55 | $result = $crawler |
56 | ->filterXpath('//img') | 56 | ->filterXpath('//img') |
57 | ->extract(array('src')); | 57 | ->extract(['src']); |
58 | 58 | ||
59 | $relativePath = $this->getRelativePath($entryId); | 59 | $relativePath = $this->getRelativePath($entryId); |
60 | 60 | ||
@@ -66,6 +66,11 @@ class DownloadImages | |||
66 | continue; | 66 | continue; |
67 | } | 67 | } |
68 | 68 | ||
69 | // if image contains "&" and we can't find it in the html it might be because it's encoded as & | ||
70 | if (false !== stripos($image, '&') && false === stripos($html, $image)) { | ||
71 | $image = str_replace('&', '&', $image); | ||
72 | } | ||
73 | |||
69 | $html = str_replace($image, $imagePath, $html); | 74 | $html = str_replace($image, $imagePath, $html); |
70 | } | 75 | } |
71 | 76 | ||
@@ -114,7 +119,7 @@ class DownloadImages | |||
114 | $ext = $this->mimeGuesser->guess($res->getHeader('content-type')); | 119 | $ext = $this->mimeGuesser->guess($res->getHeader('content-type')); |
115 | $this->logger->debug('DownloadImages: Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]); | 120 | $this->logger->debug('DownloadImages: Checking extension', ['ext' => $ext, 'header' => $res->getHeader('content-type')]); |
116 | if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) { | 121 | if (!in_array($ext, ['jpeg', 'jpg', 'gif', 'png'], true)) { |
117 | $this->logger->error('DownloadImages: Processed image with not allowed extension. Skipping '.$imagePath); | 122 | $this->logger->error('DownloadImages: Processed image with not allowed extension. Skipping: '.$imagePath); |
118 | 123 | ||
119 | return false; | 124 | return false; |
120 | } | 125 | } |
diff --git a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php index 1ac8feb1..43f5b119 100644 --- a/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php +++ b/src/Wallabag/CoreBundle/Helper/HttpClientFactory.php | |||
@@ -13,8 +13,8 @@ use Psr\Log\LoggerInterface; | |||
13 | */ | 13 | */ |
14 | class HttpClientFactory | 14 | class HttpClientFactory |
15 | { | 15 | { |
16 | /** @var \GuzzleHttp\Event\SubscriberInterface */ | 16 | /** @var [\GuzzleHttp\Event\SubscriberInterface] */ |
17 | private $authenticatorSubscriber; | 17 | private $subscribers = []; |
18 | 18 | ||
19 | /** @var \GuzzleHttp\Cookie\CookieJar */ | 19 | /** @var \GuzzleHttp\Cookie\CookieJar */ |
20 | private $cookieJar; | 20 | private $cookieJar; |
@@ -25,14 +25,12 @@ class HttpClientFactory | |||
25 | /** | 25 | /** |
26 | * HttpClientFactory constructor. | 26 | * HttpClientFactory constructor. |
27 | * | 27 | * |
28 | * @param \GuzzleHttp\Event\SubscriberInterface $authenticatorSubscriber | 28 | * @param \GuzzleHttp\Cookie\CookieJar $cookieJar |
29 | * @param \GuzzleHttp\Cookie\CookieJar $cookieJar | 29 | * @param string $restrictedAccess This param is a kind of boolean. Values: 0 or 1 |
30 | * @param string $restrictedAccess this param is a kind of boolean. Values: 0 or 1 | 30 | * @param LoggerInterface $logger |
31 | * @param LoggerInterface $logger | ||
32 | */ | 31 | */ |
33 | public function __construct(SubscriberInterface $authenticatorSubscriber, CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger) | 32 | public function __construct(CookieJar $cookieJar, $restrictedAccess, LoggerInterface $logger) |
34 | { | 33 | { |
35 | $this->authenticatorSubscriber = $authenticatorSubscriber; | ||
36 | $this->cookieJar = $cookieJar; | 34 | $this->cookieJar = $cookieJar; |
37 | $this->restrictedAccess = $restrictedAccess; | 35 | $this->restrictedAccess = $restrictedAccess; |
38 | $this->logger = $logger; | 36 | $this->logger = $logger; |
@@ -53,8 +51,21 @@ class HttpClientFactory | |||
53 | $this->cookieJar->clear(); | 51 | $this->cookieJar->clear(); |
54 | // need to set the (shared) cookie jar | 52 | // need to set the (shared) cookie jar |
55 | $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); | 53 | $client = new Client(['handler' => new SafeCurlHandler(), 'defaults' => ['cookies' => $this->cookieJar]]); |
56 | $client->getEmitter()->attach($this->authenticatorSubscriber); | 54 | |
55 | foreach ($this->subscribers as $subscriber) { | ||
56 | $client->getEmitter()->attach($subscriber); | ||
57 | } | ||
57 | 58 | ||
58 | return $client; | 59 | return $client; |
59 | } | 60 | } |
61 | |||
62 | /** | ||
63 | * Adds a subscriber to the HTTP client. | ||
64 | * | ||
65 | * @param SubscriberInterface $subscriber | ||
66 | */ | ||
67 | public function addSubscriber(SubscriberInterface $subscriber) | ||
68 | { | ||
69 | $this->subscribers[] = $subscriber; | ||
70 | } | ||
60 | } | 71 | } |
diff --git a/src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php b/src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php index 7d3798b9..df579ebd 100644 --- a/src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php +++ b/src/Wallabag/CoreBundle/Helper/PreparePagerForEntries.php | |||
@@ -20,16 +20,15 @@ class PreparePagerForEntries | |||
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @param AdapterInterface $adapter | 22 | * @param AdapterInterface $adapter |
23 | * @param int $page | ||
24 | * | 23 | * |
25 | * @return null|Pagerfanta | 24 | * @return null|Pagerfanta |
26 | */ | 25 | */ |
27 | public function prepare(AdapterInterface $adapter, $page = 1) | 26 | public function prepare(AdapterInterface $adapter) |
28 | { | 27 | { |
29 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | 28 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; |
30 | 29 | ||
31 | if (null === $user || !is_object($user)) { | 30 | if (null === $user || !is_object($user)) { |
32 | return null; | 31 | return; |
33 | } | 32 | } |
34 | 33 | ||
35 | $entries = new Pagerfanta($adapter); | 34 | $entries = new Pagerfanta($adapter); |
diff --git a/src/Wallabag/CoreBundle/Helper/Redirect.php b/src/Wallabag/CoreBundle/Helper/Redirect.php index f78b7fe0..abc84d08 100644 --- a/src/Wallabag/CoreBundle/Helper/Redirect.php +++ b/src/Wallabag/CoreBundle/Helper/Redirect.php | |||
@@ -21,12 +21,13 @@ class Redirect | |||
21 | } | 21 | } |
22 | 22 | ||
23 | /** | 23 | /** |
24 | * @param string $url URL to redirect | 24 | * @param string $url URL to redirect |
25 | * @param string $fallback Fallback URL if $url is null | 25 | * @param string $fallback Fallback URL if $url is null |
26 | * @param bool $ignoreActionMarkAsRead Ignore configured action when mark as read | ||
26 | * | 27 | * |
27 | * @return string | 28 | * @return string |
28 | */ | 29 | */ |
29 | public function to($url, $fallback = '') | 30 | public function to($url, $fallback = '', $ignoreActionMarkAsRead = false) |
30 | { | 31 | { |
31 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | 32 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; |
32 | 33 | ||
@@ -34,7 +35,8 @@ class Redirect | |||
34 | return $url; | 35 | return $url; |
35 | } | 36 | } |
36 | 37 | ||
37 | if (Config::REDIRECT_TO_HOMEPAGE === $user->getConfig()->getActionMarkAsRead()) { | 38 | if (!$ignoreActionMarkAsRead && |
39 | Config::REDIRECT_TO_HOMEPAGE === $user->getConfig()->getActionMarkAsRead()) { | ||
38 | return $this->router->generate('homepage'); | 40 | return $this->router->generate('homepage'); |
39 | } | 41 | } |
40 | 42 | ||
diff --git a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php index b490e209..509d0dec 100644 --- a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php +++ b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php | |||
@@ -8,18 +8,21 @@ use Wallabag\CoreBundle\Entity\Tag; | |||
8 | use Wallabag\CoreBundle\Repository\EntryRepository; | 8 | use Wallabag\CoreBundle\Repository\EntryRepository; |
9 | use Wallabag\CoreBundle\Repository\TagRepository; | 9 | use Wallabag\CoreBundle\Repository\TagRepository; |
10 | use Wallabag\UserBundle\Entity\User; | 10 | use Wallabag\UserBundle\Entity\User; |
11 | use Psr\Log\LoggerInterface; | ||
11 | 12 | ||
12 | class RuleBasedTagger | 13 | class RuleBasedTagger |
13 | { | 14 | { |
14 | private $rulerz; | 15 | private $rulerz; |
15 | private $tagRepository; | 16 | private $tagRepository; |
16 | private $entryRepository; | 17 | private $entryRepository; |
18 | private $logger; | ||
17 | 19 | ||
18 | public function __construct(RulerZ $rulerz, TagRepository $tagRepository, EntryRepository $entryRepository) | 20 | public function __construct(RulerZ $rulerz, TagRepository $tagRepository, EntryRepository $entryRepository, LoggerInterface $logger) |
19 | { | 21 | { |
20 | $this->rulerz = $rulerz; | 22 | $this->rulerz = $rulerz; |
21 | $this->tagRepository = $tagRepository; | 23 | $this->tagRepository = $tagRepository; |
22 | $this->entryRepository = $entryRepository; | 24 | $this->entryRepository = $entryRepository; |
25 | $this->logger = $logger; | ||
23 | } | 26 | } |
24 | 27 | ||
25 | /** | 28 | /** |
@@ -36,6 +39,11 @@ class RuleBasedTagger | |||
36 | continue; | 39 | continue; |
37 | } | 40 | } |
38 | 41 | ||
42 | $this->logger->info('Matching rule.', [ | ||
43 | 'rule' => $rule->getRule(), | ||
44 | 'tags' => $rule->getTags(), | ||
45 | ]); | ||
46 | |||
39 | foreach ($rule->getTags() as $label) { | 47 | foreach ($rule->getTags() as $label) { |
40 | $tag = $this->getTag($label); | 48 | $tag = $this->getTag($label); |
41 | 49 | ||
diff --git a/src/Wallabag/CoreBundle/Helper/TagsAssigner.php b/src/Wallabag/CoreBundle/Helper/TagsAssigner.php new file mode 100644 index 00000000..a2fb0b9a --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/TagsAssigner.php | |||
@@ -0,0 +1,75 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Wallabag\CoreBundle\Entity\Entry; | ||
6 | use Wallabag\CoreBundle\Entity\Tag; | ||
7 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
8 | |||
9 | class TagsAssigner | ||
10 | { | ||
11 | /** | ||
12 | * @var TagRepository | ||
13 | */ | ||
14 | protected $tagRepository; | ||
15 | |||
16 | public function __construct(TagRepository $tagRepository) | ||
17 | { | ||
18 | $this->tagRepository = $tagRepository; | ||
19 | } | ||
20 | |||
21 | /** | ||
22 | * Assign some tags to an entry. | ||
23 | * | ||
24 | * @param Entry $entry | ||
25 | * @param array|string $tags An array of tag or a string coma separated of tag | ||
26 | * @param array $entitiesReady Entities from the EntityManager which are persisted but not yet flushed | ||
27 | * It is mostly to fix duplicate tag on import @see http://stackoverflow.com/a/7879164/569101 | ||
28 | * | ||
29 | * @return Tag[] | ||
30 | */ | ||
31 | public function assignTagsToEntry(Entry $entry, $tags, array $entitiesReady = []) | ||
32 | { | ||
33 | $tagsEntities = []; | ||
34 | |||
35 | if (!is_array($tags)) { | ||
36 | $tags = explode(',', $tags); | ||
37 | } | ||
38 | |||
39 | // keeps only Tag entity from the "not yet flushed entities" | ||
40 | $tagsNotYetFlushed = []; | ||
41 | foreach ($entitiesReady as $entity) { | ||
42 | if ($entity instanceof Tag) { | ||
43 | $tagsNotYetFlushed[$entity->getLabel()] = $entity; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | foreach ($tags as $label) { | ||
48 | $label = trim($label); | ||
49 | |||
50 | // avoid empty tag | ||
51 | if (0 === strlen($label)) { | ||
52 | continue; | ||
53 | } | ||
54 | |||
55 | if (isset($tagsNotYetFlushed[$label])) { | ||
56 | $tagEntity = $tagsNotYetFlushed[$label]; | ||
57 | } else { | ||
58 | $tagEntity = $this->tagRepository->findOneByLabel($label); | ||
59 | |||
60 | if (null === $tagEntity) { | ||
61 | $tagEntity = new Tag(); | ||
62 | $tagEntity->setLabel($label); | ||
63 | } | ||
64 | } | ||
65 | |||
66 | // only add the tag on the entry if the relation doesn't exist | ||
67 | if (false === $entry->getTags()->contains($tagEntity)) { | ||
68 | $entry->addTag($tagEntity); | ||
69 | $tagsEntities[] = $tagEntity; | ||
70 | } | ||
71 | } | ||
72 | |||
73 | return $tagsEntities; | ||
74 | } | ||
75 | } | ||