diff options
author | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-03-28 09:41:49 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas@loeuillet.org> | 2016-03-28 09:41:49 +0200 |
commit | cc8c2d315f6868201ff32cc4895844c800624a87 (patch) | |
tree | 9a59669aba09a08751aecc0dcf8ccd1e20aef6c6 /src/Wallabag/CoreBundle/Helper | |
parent | a2d6f6e9d806c727b20a08f37ef7ede1e0dad3cf (diff) | |
parent | 8f336fda649c064cabfa692793334067ece780f9 (diff) | |
download | wallabag-cc8c2d315f6868201ff32cc4895844c800624a87.tar.gz wallabag-cc8c2d315f6868201ff32cc4895844c800624a87.tar.zst wallabag-cc8c2d315f6868201ff32cc4895844c800624a87.zip |
Merge pull request #1819 from wallabag/cleanup
Some cleanup
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/ContentProxy.php | 34 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/EntriesExport.php | 34 |
2 files changed, 37 insertions, 31 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php index ba90b731..ed4a220d 100644 --- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php | |||
@@ -32,14 +32,21 @@ class ContentProxy | |||
32 | * Fetch content using graby and hydrate given entry with results information. | 32 | * Fetch content using graby and hydrate given entry with results information. |
33 | * In case we couldn't find content, we'll try to use Open Graph data. | 33 | * In case we couldn't find content, we'll try to use Open Graph data. |
34 | * | 34 | * |
35 | * @param Entry $entry Entry to update | 35 | * We can also force the content, in case of an import from the v1 for example, so the function won't |
36 | * @param string $url Url to grab content for | 36 | * fetch the content from the website but rather use information given with the $content parameter. |
37 | * | ||
38 | * @param Entry $entry Entry to update | ||
39 | * @param string $url Url to grab content for | ||
40 | * @param array $content An array with AT LEAST keys title, html, url, language & content_type to skip the fetchContent from the url | ||
37 | * | 41 | * |
38 | * @return Entry | 42 | * @return Entry |
39 | */ | 43 | */ |
40 | public function updateEntry(Entry $entry, $url) | 44 | public function updateEntry(Entry $entry, $url, array $content = []) |
41 | { | 45 | { |
42 | $content = $this->graby->fetchContent($url); | 46 | // do we have to fetch the content or the provided one is ok? |
47 | if (empty($content) || false === $this->validateContent($content)) { | ||
48 | $content = $this->graby->fetchContent($url); | ||
49 | } | ||
43 | 50 | ||
44 | $title = $content['title']; | 51 | $title = $content['title']; |
45 | if (!$title && isset($content['open_graph']['og_title'])) { | 52 | if (!$title && isset($content['open_graph']['og_title'])) { |
@@ -62,7 +69,11 @@ class ContentProxy | |||
62 | $entry->setLanguage($content['language']); | 69 | $entry->setLanguage($content['language']); |
63 | $entry->setMimetype($content['content_type']); | 70 | $entry->setMimetype($content['content_type']); |
64 | $entry->setReadingTime(Utils::getReadingTime($html)); | 71 | $entry->setReadingTime(Utils::getReadingTime($html)); |
65 | $entry->setDomainName(parse_url($entry->getUrl(), PHP_URL_HOST)); | 72 | |
73 | $domainName = parse_url($entry->getUrl(), PHP_URL_HOST); | ||
74 | if (false !== $domainName) { | ||
75 | $entry->setDomainName($domainName); | ||
76 | } | ||
66 | 77 | ||
67 | if (isset($content['open_graph']['og_image'])) { | 78 | if (isset($content['open_graph']['og_image'])) { |
68 | $entry->setPreviewPicture($content['open_graph']['og_image']); | 79 | $entry->setPreviewPicture($content['open_graph']['og_image']); |
@@ -113,4 +124,17 @@ class ContentProxy | |||
113 | } | 124 | } |
114 | } | 125 | } |
115 | } | 126 | } |
127 | |||
128 | /** | ||
129 | * Validate that the given content as enough value to be used | ||
130 | * instead of fetch the content from the url. | ||
131 | * | ||
132 | * @param array $content | ||
133 | * | ||
134 | * @return bool true if valid otherwise false | ||
135 | */ | ||
136 | private function validateContent(array $content) | ||
137 | { | ||
138 | return isset($content['title']) && isset($content['html']) && isset($content['url']) && isset($content['language']) && isset($content['content_type']); | ||
139 | } | ||
116 | } | 140 | } |
diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index 6ecdf019..82004a6d 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php | |||
@@ -81,27 +81,9 @@ class EntriesExport | |||
81 | */ | 81 | */ |
82 | public function exportAs($format) | 82 | public function exportAs($format) |
83 | { | 83 | { |
84 | switch ($format) { | 84 | $functionName = 'produce'.ucfirst($format); |
85 | case 'epub': | 85 | if (method_exists($this, $functionName)) { |
86 | return $this->produceEpub(); | 86 | return $this->$functionName(); |
87 | |||
88 | case 'mobi': | ||
89 | return $this->produceMobi(); | ||
90 | |||
91 | case 'pdf': | ||
92 | return $this->producePDF(); | ||
93 | |||
94 | case 'csv': | ||
95 | return $this->produceCSV(); | ||
96 | |||
97 | case 'json': | ||
98 | return $this->produceJSON(); | ||
99 | |||
100 | case 'xml': | ||
101 | return $this->produceXML(); | ||
102 | |||
103 | case 'txt': | ||
104 | return $this->produceTXT(); | ||
105 | } | 87 | } |
106 | 88 | ||
107 | throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format)); | 89 | throw new \InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format)); |
@@ -242,7 +224,7 @@ class EntriesExport | |||
242 | /** | 224 | /** |
243 | * Use TCPDF to dump a .pdf file. | 225 | * Use TCPDF to dump a .pdf file. |
244 | */ | 226 | */ |
245 | private function producePDF() | 227 | private function producePdf() |
246 | { | 228 | { |
247 | $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); | 229 | $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); |
248 | 230 | ||
@@ -296,7 +278,7 @@ class EntriesExport | |||
296 | /** | 278 | /** |
297 | * Inspired from CsvFileDumper. | 279 | * Inspired from CsvFileDumper. |
298 | */ | 280 | */ |
299 | private function produceCSV() | 281 | private function produceCsv() |
300 | { | 282 | { |
301 | $delimiter = ';'; | 283 | $delimiter = ';'; |
302 | $enclosure = '"'; | 284 | $enclosure = '"'; |
@@ -336,7 +318,7 @@ class EntriesExport | |||
336 | ); | 318 | ); |
337 | } | 319 | } |
338 | 320 | ||
339 | private function produceJSON() | 321 | private function produceJson() |
340 | { | 322 | { |
341 | return Response::create( | 323 | return Response::create( |
342 | $this->prepareSerializingContent('json'), | 324 | $this->prepareSerializingContent('json'), |
@@ -349,7 +331,7 @@ class EntriesExport | |||
349 | ); | 331 | ); |
350 | } | 332 | } |
351 | 333 | ||
352 | private function produceXML() | 334 | private function produceXml() |
353 | { | 335 | { |
354 | return Response::create( | 336 | return Response::create( |
355 | $this->prepareSerializingContent('xml'), | 337 | $this->prepareSerializingContent('xml'), |
@@ -362,7 +344,7 @@ class EntriesExport | |||
362 | ); | 344 | ); |
363 | } | 345 | } |
364 | 346 | ||
365 | private function produceTXT() | 347 | private function produceTxt() |
366 | { | 348 | { |
367 | $content = ''; | 349 | $content = ''; |
368 | $bar = str_repeat('=', 100); | 350 | $bar = str_repeat('=', 100); |