diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-09-25 13:29:36 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-15 09:08:46 +0200 |
commit | 4cf3564d28dc8e4d08a3e64f09ad045ffbde97ae (patch) | |
tree | 8f8ef095cdfea3b35953417fd3d8bb6cdbc7cb46 /application/front/controller/admin | |
parent | f34554c6c2cd8fe99fe2e8907bfc196a4884416a (diff) | |
download | Shaarli-4cf3564d28dc8e4d08a3e64f09ad045ffbde97ae.tar.gz Shaarli-4cf3564d28dc8e4d08a3e64f09ad045ffbde97ae.tar.zst Shaarli-4cf3564d28dc8e4d08a3e64f09ad045ffbde97ae.zip |
Add a setting to retrieve bookmark metadata asynchrounously
- There is a new standalone script (metadata.js) which requests
a new controller to get bookmark metadata and fill the form async
- This feature is enabled with the new setting: general.enable_async_metadata
(enabled by default)
- general.retrieve_description is now enabled by default
- A small rotating loader animation has a been added to bookmark inputs
when metadata is being retrieved (default template)
- Custom JS htmlentities has been removed and mathiasbynens/he
library is used instead
Fixes #1563
Diffstat (limited to 'application/front/controller/admin')
-rw-r--r-- | application/front/controller/admin/ManageShaareController.php | 36 | ||||
-rw-r--r-- | application/front/controller/admin/MetadataController.php | 29 |
2 files changed, 41 insertions, 24 deletions
diff --git a/application/front/controller/admin/ManageShaareController.php b/application/front/controller/admin/ManageShaareController.php index bb083486..df2f1631 100644 --- a/application/front/controller/admin/ManageShaareController.php +++ b/application/front/controller/admin/ManageShaareController.php | |||
@@ -53,36 +53,22 @@ class ManageShaareController extends ShaarliAdminController | |||
53 | 53 | ||
54 | // If this is an HTTP(S) link, we try go get the page to extract | 54 | // If this is an HTTP(S) link, we try go get the page to extract |
55 | // the title (otherwise we will to straight to the edit form.) | 55 | // the title (otherwise we will to straight to the edit form.) |
56 | if (empty($title) && strpos(get_url_scheme($url) ?: '', 'http') !== false) { | 56 | if (true !== $this->container->conf->get('general.enable_async_metadata', true) |
57 | $retrieveDescription = $this->container->conf->get('general.retrieve_description'); | 57 | && empty($title) |
58 | // Short timeout to keep the application responsive | 58 | && strpos(get_url_scheme($url) ?: '', 'http') !== false |
59 | // The callback will fill $charset and $title with data from the downloaded page. | 59 | ) { |
60 | $this->container->httpAccess->getHttpResponse( | 60 | $metadata = $this->container->metadataRetriever->retrieve($url); |
61 | $url, | ||
62 | $this->container->conf->get('general.download_timeout', 30), | ||
63 | $this->container->conf->get('general.download_max_size', 4194304), | ||
64 | $this->container->httpAccess->getCurlDownloadCallback( | ||
65 | $charset, | ||
66 | $title, | ||
67 | $description, | ||
68 | $tags, | ||
69 | $retrieveDescription | ||
70 | ) | ||
71 | ); | ||
72 | if (! empty($title) && strtolower($charset) !== 'utf-8' && mb_check_encoding($charset)) { | ||
73 | $title = mb_convert_encoding($title, 'utf-8', $charset); | ||
74 | } | ||
75 | } | 61 | } |
76 | 62 | ||
77 | if (empty($url) && empty($title)) { | 63 | if (empty($url)) { |
78 | $title = $this->container->conf->get('general.default_note_title', t('Note: ')); | 64 | $metadata['title'] = $this->container->conf->get('general.default_note_title', t('Note: ')); |
79 | } | 65 | } |
80 | 66 | ||
81 | $link = [ | 67 | $link = [ |
82 | 'title' => $title, | 68 | 'title' => $title ?? $metadata['title'] ?? '', |
83 | 'url' => $url ?? '', | 69 | 'url' => $url ?? '', |
84 | 'description' => $description ?? '', | 70 | 'description' => $description ?? $metadata['description'] ?? '', |
85 | 'tags' => $tags ?? '', | 71 | 'tags' => $tags ?? $metadata['tags'] ?? '', |
86 | 'private' => $private, | 72 | 'private' => $private, |
87 | ]; | 73 | ]; |
88 | } else { | 74 | } else { |
@@ -352,6 +338,8 @@ class ManageShaareController extends ShaarliAdminController | |||
352 | 'source' => $request->getParam('source') ?? '', | 338 | 'source' => $request->getParam('source') ?? '', |
353 | 'tags' => $tags, | 339 | 'tags' => $tags, |
354 | 'default_private_links' => $this->container->conf->get('privacy.default_private_links', false), | 340 | 'default_private_links' => $this->container->conf->get('privacy.default_private_links', false), |
341 | 'async_metadata' => $this->container->conf->get('general.enable_async_metadata', true), | ||
342 | 'retrieve_description' => $this->container->conf->get('general.retrieve_description', false), | ||
355 | ]); | 343 | ]); |
356 | 344 | ||
357 | $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK); | 345 | $this->executePageHooks('render_editlink', $data, TemplatePage::EDIT_LINK); |
diff --git a/application/front/controller/admin/MetadataController.php b/application/front/controller/admin/MetadataController.php new file mode 100644 index 00000000..ff845944 --- /dev/null +++ b/application/front/controller/admin/MetadataController.php | |||
@@ -0,0 +1,29 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Front\Controller\Admin; | ||
6 | |||
7 | use Slim\Http\Request; | ||
8 | use Slim\Http\Response; | ||
9 | |||
10 | /** | ||
11 | * Controller used to retrieve/update bookmark's metadata. | ||
12 | */ | ||
13 | class MetadataController extends ShaarliAdminController | ||
14 | { | ||
15 | /** | ||
16 | * GET /admin/metadata/{url} - Attempt to retrieve the bookmark title from provided URL. | ||
17 | */ | ||
18 | public function ajaxRetrieveTitle(Request $request, Response $response): Response | ||
19 | { | ||
20 | $url = $request->getParam('url'); | ||
21 | |||
22 | // Only try to extract metadata from URL with HTTP(s) scheme | ||
23 | if (!empty($url) && strpos(get_url_scheme($url) ?: '', 'http') !== false) { | ||
24 | return $response->withJson($this->container->metadataRetriever->retrieve($url)); | ||
25 | } | ||
26 | |||
27 | return $response->withJson([]); | ||
28 | } | ||
29 | } | ||