aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/ContentProxy.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/ContentProxy.php')
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php89
1 files changed, 52 insertions, 37 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index ddecd6f4..2a650e97 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -75,9 +75,17 @@ class ContentProxy
75 */ 75 */
76 private function stockEntry(Entry $entry, array $content) 76 private function stockEntry(Entry $entry, array $content)
77 { 77 {
78 $title = $content['title']; 78 $entry->setUrl($content['url']);
79 if (!$title && !empty($content['open_graph']['og_title'])) { 79
80 $title = $content['open_graph']['og_title']; 80 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
81 if (false !== $domainName) {
82 $entry->setDomainName($domainName);
83 }
84
85 if (!empty($content['title'])) {
86 $entry->setTitle($content['title']);
87 } elseif (!empty($content['open_graph']['og_title'])) {
88 $entry->setTitle($content['open_graph']['og_title']);
81 } 89 }
82 90
83 $html = $content['html']; 91 $html = $content['html'];
@@ -90,24 +98,11 @@ class ContentProxy
90 } 98 }
91 } 99 }
92 100
93 $entry->setUrl($content['url']);
94 $entry->setTitle($title);
95 $entry->setContent($html); 101 $entry->setContent($html);
96 $entry->setHttpStatus(isset($content['status']) ? $content['status'] : ''); 102 $entry->setReadingTime(Utils::getReadingTime($html));
97
98 if (!empty($content['date'])) {
99 $date = $content['date'];
100
101 // is it a timestamp?
102 if (filter_var($date, FILTER_VALIDATE_INT) !== false) {
103 $date = '@' . $content['date'];
104 }
105 103
106 try { 104 if (!empty($content['status'])) {
107 $entry->setPublishedAt(new \DateTime($date)); 105 $entry->setHttpStatus($content['status']);
108 } catch (\Exception $e) {
109 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $content['url'], 'date' => $content['date']]);
110 }
111 } 106 }
112 107
113 if (!empty($content['authors']) && is_array($content['authors'])) { 108 if (!empty($content['authors']) && is_array($content['authors'])) {
@@ -118,15 +113,17 @@ class ContentProxy
118 $entry->setHeaders($content['all_headers']); 113 $entry->setHeaders($content['all_headers']);
119 } 114 }
120 115
121 $this->validateAndSetLanguage( 116 if (!empty($content['date'])) {
122 $entry, 117 $this->updatePublishedAt($entry, $content['date']);
123 isset($content['language']) ? $content['language'] : null 118 }
124 );
125 119
126 $this->validateAndSetPreviewPicture( 120 if (!empty($content['language'])) {
127 $entry, 121 $this->updateLanguage($entry, $content['language']);
128 isset($content['open_graph']['og_image']) ? $content['open_graph']['og_image'] : null 122 }
129 ); 123
124 if (!empty($content['open_graph']['og_image'])) {
125 $this->updatePreviewPicture($entry, $content['open_graph']['og_image']);
126 }
130 127
131 // if content is an image, define it as a preview too 128 // if content is an image, define it as a preview too
132 if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) { 129 if (!empty($content['content_type']) && in_array($this->mimeGuesser->guess($content['content_type']), ['jpeg', 'jpg', 'gif', 'png'], true)) {
@@ -136,12 +133,8 @@ class ContentProxy
136 ); 133 );
137 } 134 }
138 135
139 $entry->setMimetype(isset($content['content_type']) ? $content['content_type'] : ''); 136 if (!empty($content['content_type'])) {
140 $entry->setReadingTime(Utils::getReadingTime($html)); 137 $entry->setMimetype($content['content_type']);
141
142 $domainName = parse_url($entry->getUrl(), PHP_URL_HOST);
143 if (false !== $domainName) {
144 $entry->setDomainName($domainName);
145 } 138 }
146 139
147 try { 140 try {
@@ -170,9 +163,9 @@ class ContentProxy
170 * Use a Symfony validator to ensure the language is well formatted. 163 * Use a Symfony validator to ensure the language is well formatted.
171 * 164 *
172 * @param Entry $entry 165 * @param Entry $entry
173 * @param string $value Language to validate 166 * @param string $value Language to validate and save
174 */ 167 */
175 private function validateAndSetLanguage($entry, $value) 168 public function updateLanguage(Entry $entry, $value)
176 { 169 {
177 // some lang are defined as fr-FR, es-ES. 170 // some lang are defined as fr-FR, es-ES.
178 // replacing - by _ might increase language support 171 // replacing - by _ might increase language support
@@ -196,9 +189,9 @@ class ContentProxy
196 * Use a Symfony validator to ensure the preview picture is a real url. 189 * Use a Symfony validator to ensure the preview picture is a real url.
197 * 190 *
198 * @param Entry $entry 191 * @param Entry $entry
199 * @param string $value URL to validate 192 * @param string $value URL to validate and save
200 */ 193 */
201 private function validateAndSetPreviewPicture($entry, $value) 194 public function updatePreviewPicture(Entry $entry, $value)
202 { 195 {
203 $errors = $this->validator->validate( 196 $errors = $this->validator->validate(
204 $value, 197 $value,
@@ -213,4 +206,26 @@ class ContentProxy
213 206
214 $this->logger->warning('PreviewPicture validation failed. ' . (string) $errors); 207 $this->logger->warning('PreviewPicture validation failed. ' . (string) $errors);
215 } 208 }
209
210 /**
211 * Update date.
212 *
213 * @param Entry $entry
214 * @param string $value Date to validate and save
215 */
216 public function updatePublishedAt(Entry $entry, $value)
217 {
218 $date = $value;
219
220 // is it a timestamp?
221 if (filter_var($date, FILTER_VALIDATE_INT) !== false) {
222 $date = '@'.$value;
223 }
224
225 try {
226 $entry->setPublishedAt(new \DateTime($date));
227 } catch (\Exception $e) {
228 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $entry->getUrl(), 'date' => $value]);
229 }
230 }
216} 231}