aboutsummaryrefslogtreecommitdiffhomepage
path: root/application
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-06-17 15:55:31 +0200
committerArthurHoaro <arthur@hoa.ro>2020-07-23 21:19:21 +0200
commite8a10f312a5c44314292402bb44e6ee2e71f3d5d (patch)
tree3f0c0bf3bc299eae3b379431d4a521f6904d5ee4 /application
parent3447d888d7881eed437117a6de2450abb96f6a76 (diff)
downloadShaarli-e8a10f312a5c44314292402bb44e6ee2e71f3d5d.tar.gz
Shaarli-e8a10f312a5c44314292402bb44e6ee2e71f3d5d.tar.zst
Shaarli-e8a10f312a5c44314292402bb44e6ee2e71f3d5d.zip
Use NetscapeBookmarkUtils object instance instead of static calls
Diffstat (limited to 'application')
-rw-r--r--application/container/ContainerBuilder.php5
-rw-r--r--application/container/ShaarliContainer.php2
-rw-r--r--application/netscape/NetscapeBookmarkUtils.php118
3 files changed, 71 insertions, 54 deletions
diff --git a/application/container/ContainerBuilder.php b/application/container/ContainerBuilder.php
index 72a85710..a4fd6a0c 100644
--- a/application/container/ContainerBuilder.php
+++ b/application/container/ContainerBuilder.php
@@ -11,6 +11,7 @@ use Shaarli\Feed\FeedBuilder;
11use Shaarli\Formatter\FormatterFactory; 11use Shaarli\Formatter\FormatterFactory;
12use Shaarli\History; 12use Shaarli\History;
13use Shaarli\Http\HttpAccess; 13use Shaarli\Http\HttpAccess;
14use Shaarli\Netscape\NetscapeBookmarkUtils;
14use Shaarli\Plugin\PluginManager; 15use Shaarli\Plugin\PluginManager;
15use Shaarli\Render\PageBuilder; 16use Shaarli\Render\PageBuilder;
16use Shaarli\Render\PageCacheManager; 17use Shaarli\Render\PageCacheManager;
@@ -118,6 +119,10 @@ class ContainerBuilder
118 return new HttpAccess(); 119 return new HttpAccess();
119 }; 120 };
120 121
122 $container['netscapeBookmarkUtils'] = function (ShaarliContainer $container): NetscapeBookmarkUtils {
123 return new NetscapeBookmarkUtils($container->bookmarkService, $container->conf, $container->history);
124 };
125
121 return $container; 126 return $container;
122 } 127 }
123} 128}
diff --git a/application/container/ShaarliContainer.php b/application/container/ShaarliContainer.php
index 4b97aae2..b08fa4cb 100644
--- a/application/container/ShaarliContainer.php
+++ b/application/container/ShaarliContainer.php
@@ -10,6 +10,7 @@ use Shaarli\Feed\FeedBuilder;
10use Shaarli\Formatter\FormatterFactory; 10use Shaarli\Formatter\FormatterFactory;
11use Shaarli\History; 11use Shaarli\History;
12use Shaarli\Http\HttpAccess; 12use Shaarli\Http\HttpAccess;
13use Shaarli\Netscape\NetscapeBookmarkUtils;
13use Shaarli\Plugin\PluginManager; 14use Shaarli\Plugin\PluginManager;
14use Shaarli\Render\PageBuilder; 15use Shaarli\Render\PageBuilder;
15use Shaarli\Render\PageCacheManager; 16use Shaarli\Render\PageCacheManager;
@@ -30,6 +31,7 @@ use Slim\Container;
30 * @property History $history 31 * @property History $history
31 * @property HttpAccess $httpAccess 32 * @property HttpAccess $httpAccess
32 * @property LoginManager $loginManager 33 * @property LoginManager $loginManager
34 * @property NetscapeBookmarkUtils $netscapeBookmarkUtils
33 * @property PageBuilder $pageBuilder 35 * @property PageBuilder $pageBuilder
34 * @property PageCacheManager $pageCacheManager 36 * @property PageCacheManager $pageCacheManager
35 * @property PluginManager $pluginManager 37 * @property PluginManager $pluginManager
diff --git a/application/netscape/NetscapeBookmarkUtils.php b/application/netscape/NetscapeBookmarkUtils.php
index d64eef7f..8557cca2 100644
--- a/application/netscape/NetscapeBookmarkUtils.php
+++ b/application/netscape/NetscapeBookmarkUtils.php
@@ -16,10 +16,24 @@ use Shaarli\NetscapeBookmarkParser\NetscapeBookmarkParser;
16 16
17/** 17/**
18 * Utilities to import and export bookmarks using the Netscape format 18 * Utilities to import and export bookmarks using the Netscape format
19 * TODO: Not static, use a container.
20 */ 19 */
21class NetscapeBookmarkUtils 20class NetscapeBookmarkUtils
22{ 21{
22 /** @var BookmarkServiceInterface */
23 protected $bookmarkService;
24
25 /** @var ConfigManager */
26 protected $conf;
27
28 /** @var History */
29 protected $history;
30
31 public function __construct(BookmarkServiceInterface $bookmarkService, ConfigManager $conf, History $history)
32 {
33 $this->bookmarkService = $bookmarkService;
34 $this->conf = $conf;
35 $this->history = $history;
36 }
23 37
24 /** 38 /**
25 * Filters bookmarks and adds Netscape-formatted fields 39 * Filters bookmarks and adds Netscape-formatted fields
@@ -28,18 +42,16 @@ class NetscapeBookmarkUtils
28 * - timestamp link addition date, using the Unix epoch format 42 * - timestamp link addition date, using the Unix epoch format
29 * - taglist comma-separated tag list 43 * - taglist comma-separated tag list
30 * 44 *
31 * @param BookmarkServiceInterface $bookmarkService Link datastore
32 * @param BookmarkFormatter $formatter instance 45 * @param BookmarkFormatter $formatter instance
33 * @param string $selection Which bookmarks to export: (all|private|public) 46 * @param string $selection Which bookmarks to export: (all|private|public)
34 * @param bool $prependNoteUrl Prepend note permalinks with the server's URL 47 * @param bool $prependNoteUrl Prepend note permalinks with the server's URL
35 * @param string $indexUrl Absolute URL of the Shaarli index page 48 * @param string $indexUrl Absolute URL of the Shaarli index page
36 * 49 *
37 * @return array The bookmarks to be exported, with additional fields 50 * @return array The bookmarks to be exported, with additional fields
38 *@throws Exception Invalid export selection
39 * 51 *
52 * @throws Exception Invalid export selection
40 */ 53 */
41 public static function filterAndFormat( 54 public function filterAndFormat(
42 $bookmarkService,
43 $formatter, 55 $formatter,
44 $selection, 56 $selection,
45 $prependNoteUrl, 57 $prependNoteUrl,
@@ -51,7 +63,7 @@ class NetscapeBookmarkUtils
51 } 63 }
52 64
53 $bookmarkLinks = array(); 65 $bookmarkLinks = array();
54 foreach ($bookmarkService->search([], $selection) as $bookmark) { 66 foreach ($this->bookmarkService->search([], $selection) as $bookmark) {
55 $link = $formatter->format($bookmark); 67 $link = $formatter->format($bookmark);
56 $link['taglist'] = implode(',', $bookmark->getTags()); 68 $link['taglist'] = implode(',', $bookmark->getTags());
57 if ($bookmark->isNote() && $prependNoteUrl) { 69 if ($bookmark->isNote() && $prependNoteUrl) {
@@ -65,52 +77,14 @@ class NetscapeBookmarkUtils
65 } 77 }
66 78
67 /** 79 /**
68 * Generates an import status summary
69 *
70 * @param string $filename name of the file to import
71 * @param int $filesize size of the file to import
72 * @param int $importCount how many bookmarks were imported
73 * @param int $overwriteCount how many bookmarks were overwritten
74 * @param int $skipCount how many bookmarks were skipped
75 * @param int $duration how many seconds did the import take
76 *
77 * @return string Summary of the bookmark import status
78 */
79 private static function importStatus(
80 $filename,
81 $filesize,
82 $importCount = 0,
83 $overwriteCount = 0,
84 $skipCount = 0,
85 $duration = 0
86 ) {
87 $status = sprintf(t('File %s (%d bytes) '), $filename, $filesize);
88 if ($importCount == 0 && $overwriteCount == 0 && $skipCount == 0) {
89 $status .= t('has an unknown file format. Nothing was imported.');
90 } else {
91 $status .= vsprintf(
92 t(
93 'was successfully processed in %d seconds: '
94 . '%d bookmarks imported, %d bookmarks overwritten, %d bookmarks skipped.'
95 ),
96 [$duration, $importCount, $overwriteCount, $skipCount]
97 );
98 }
99 return $status;
100 }
101
102 /**
103 * Imports Web bookmarks from an uploaded Netscape bookmark dump 80 * Imports Web bookmarks from an uploaded Netscape bookmark dump
104 * 81 *
105 * @param array $post Server $_POST parameters 82 * @param array $post Server $_POST parameters
106 * @param array $files Server $_FILES parameters 83 * @param array $files Server $_FILES parameters
107 * @param BookmarkServiceInterface $bookmarkService Loaded LinkDB instance
108 * @param ConfigManager $conf instance
109 * @param History $history History instance
110 * 84 *
111 * @return string Summary of the bookmark import status 85 * @return string Summary of the bookmark import status
112 */ 86 */
113 public static function import($post, $files, $bookmarkService, $conf, $history) 87 public function import($post, $files)
114 { 88 {
115 $start = time(); 89 $start = time();
116 $filename = $files['filetoupload']['name']; 90 $filename = $files['filetoupload']['name'];
@@ -141,11 +115,11 @@ class NetscapeBookmarkUtils
141 true, // nested tag support 115 true, // nested tag support
142 $defaultTags, // additional user-specified tags 116 $defaultTags, // additional user-specified tags
143 strval(1 - $defaultPrivacy), // defaultPub = 1 - defaultPrivacy 117 strval(1 - $defaultPrivacy), // defaultPub = 1 - defaultPrivacy
144 $conf->get('resource.data_dir') // log path, will be overridden 118 $this->conf->get('resource.data_dir') // log path, will be overridden
145 ); 119 );
146 $logger = new Logger( 120 $logger = new Logger(
147 $conf->get('resource.data_dir'), 121 $this->conf->get('resource.data_dir'),
148 !$conf->get('dev.debug') ? LogLevel::INFO : LogLevel::DEBUG, 122 !$this->conf->get('dev.debug') ? LogLevel::INFO : LogLevel::DEBUG,
149 [ 123 [
150 'prefix' => 'import.', 124 'prefix' => 'import.',
151 'extension' => 'log', 125 'extension' => 'log',
@@ -171,7 +145,7 @@ class NetscapeBookmarkUtils
171 $private = 0; 145 $private = 0;
172 } 146 }
173 147
174 $link = $bookmarkService->findByUrl($bkm['uri']); 148 $link = $this->bookmarkService->findByUrl($bkm['uri']);
175 $existingLink = $link !== null; 149 $existingLink = $link !== null;
176 if (! $existingLink) { 150 if (! $existingLink) {
177 $link = new Bookmark(); 151 $link = new Bookmark();
@@ -193,20 +167,21 @@ class NetscapeBookmarkUtils
193 } 167 }
194 168
195 $link->setTitle($bkm['title']); 169 $link->setTitle($bkm['title']);
196 $link->setUrl($bkm['uri'], $conf->get('security.allowed_protocols')); 170 $link->setUrl($bkm['uri'], $this->conf->get('security.allowed_protocols'));
197 $link->setDescription($bkm['note']); 171 $link->setDescription($bkm['note']);
198 $link->setPrivate($private); 172 $link->setPrivate($private);
199 $link->setTagsString($bkm['tags']); 173 $link->setTagsString($bkm['tags']);
200 174
201 $bookmarkService->addOrSet($link, false); 175 $this->bookmarkService->addOrSet($link, false);
202 $importCount++; 176 $importCount++;
203 } 177 }
204 178
205 $bookmarkService->save(); 179 $this->bookmarkService->save();
206 $history->importLinks(); 180 $this->history->importLinks();
207 181
208 $duration = time() - $start; 182 $duration = time() - $start;
209 return self::importStatus( 183
184 return $this->importStatus(
210 $filename, 185 $filename,
211 $filesize, 186 $filesize,
212 $importCount, 187 $importCount,
@@ -215,4 +190,39 @@ class NetscapeBookmarkUtils
215 $duration 190 $duration
216 ); 191 );
217 } 192 }
193
194 /**
195 * Generates an import status summary
196 *
197 * @param string $filename name of the file to import
198 * @param int $filesize size of the file to import
199 * @param int $importCount how many bookmarks were imported
200 * @param int $overwriteCount how many bookmarks were overwritten
201 * @param int $skipCount how many bookmarks were skipped
202 * @param int $duration how many seconds did the import take
203 *
204 * @return string Summary of the bookmark import status
205 */
206 protected function importStatus(
207 $filename,
208 $filesize,
209 $importCount = 0,
210 $overwriteCount = 0,
211 $skipCount = 0,
212 $duration = 0
213 ) {
214 $status = sprintf(t('File %s (%d bytes) '), $filename, $filesize);
215 if ($importCount == 0 && $overwriteCount == 0 && $skipCount == 0) {
216 $status .= t('has an unknown file format. Nothing was imported.');
217 } else {
218 $status .= vsprintf(
219 t(
220 'was successfully processed in %d seconds: '
221 . '%d bookmarks imported, %d bookmarks overwritten, %d bookmarks skipped.'
222 ),
223 [$duration, $importCount, $overwriteCount, $skipCount]
224 );
225 }
226 return $status;
227 }
218} 228}