From 301c7ab1a079d937ab41c6f52b8804e5731008e6 Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Tue, 28 Jul 2020 20:46:11 +0200 Subject: Better support for notes permalink --- application/api/ApiUtils.php | 2 +- application/bookmark/Bookmark.php | 4 ++-- application/container/ContainerBuilder.php | 5 ++++- application/feed/FeedBuilder.php | 2 +- application/formatter/BookmarkDefaultFormatter.php | 22 ++++++++++++++-------- application/formatter/BookmarkFormatter.php | 6 ++++-- application/formatter/FormatterFactory.php | 2 +- .../controller/visitor/BookmarkListController.php | 6 +++++- .../front/controller/visitor/DailyController.php | 1 + application/netscape/NetscapeBookmarkUtils.php | 2 +- assets/default/js/base.js | 8 ++++++-- tests/api/controllers/links/PostLinkTest.php | 4 ++-- tests/api/controllers/links/PutLinkTest.php | 4 ++-- tests/bookmark/BookmarkFileServiceTest.php | 4 ++-- tests/bookmark/BookmarkTest.php | 4 ++-- tpl/default/daily.html | 2 +- 16 files changed, 49 insertions(+), 29 deletions(-) diff --git a/application/api/ApiUtils.php b/application/api/ApiUtils.php index 5156a5f7..faebb8f5 100644 --- a/application/api/ApiUtils.php +++ b/application/api/ApiUtils.php @@ -67,7 +67,7 @@ class ApiUtils if (! $bookmark->isNote()) { $out['url'] = $bookmark->getUrl(); } else { - $out['url'] = $indexUrl . $bookmark->getUrl(); + $out['url'] = rtrim($indexUrl, '/') . '/' . ltrim($bookmark->getUrl(), '/'); } $out['shorturl'] = $bookmark->getShortUrl(); $out['title'] = $bookmark->getTitle(); diff --git a/application/bookmark/Bookmark.php b/application/bookmark/Bookmark.php index 90ff5b16..c6f2c515 100644 --- a/application/bookmark/Bookmark.php +++ b/application/bookmark/Bookmark.php @@ -106,7 +106,7 @@ class Bookmark throw new InvalidBookmarkException($this); } if (empty($this->url)) { - $this->url = '?'. $this->shortUrl; + $this->url = '/shaare/'. $this->shortUrl; } if (empty($this->title)) { $this->title = $this->url; @@ -406,7 +406,7 @@ class Bookmark public function isNote() { // We check empty value to get a valid result if the link has not been saved yet - return empty($this->url) || $this->url[0] === '?'; + return empty($this->url) || startsWith($this->url, '/shaare/') || $this->url[0] === '?'; } /** diff --git a/application/container/ContainerBuilder.php b/application/container/ContainerBuilder.php index bfe93501..2e8c1ee3 100644 --- a/application/container/ContainerBuilder.php +++ b/application/container/ContainerBuilder.php @@ -105,7 +105,10 @@ class ContainerBuilder }; $container['formatterFactory'] = function (ShaarliContainer $container): FormatterFactory { - return new FormatterFactory($container->conf, $container->loginManager->isLoggedIn()); + return new FormatterFactory( + $container->conf, + $container->loginManager->isLoggedIn() + ); }; $container['pageCacheManager'] = function (ShaarliContainer $container): PageCacheManager { diff --git a/application/feed/FeedBuilder.php b/application/feed/FeedBuilder.php index c97ae1ea..269ad877 100644 --- a/application/feed/FeedBuilder.php +++ b/application/feed/FeedBuilder.php @@ -174,7 +174,7 @@ class FeedBuilder protected function buildItem(string $feedType, $link, $pageaddr) { $data = $this->formatter->format($link); - $data['guid'] = $pageaddr . '?' . $data['shorturl']; + $data['guid'] = rtrim($pageaddr, '/') . '/shaare/' . $data['shorturl']; if ($this->usePermalinks === true) { $permalink = ''. t('Direct link') .''; } else { diff --git a/application/formatter/BookmarkDefaultFormatter.php b/application/formatter/BookmarkDefaultFormatter.php index c6c59064..08e710eb 100644 --- a/application/formatter/BookmarkDefaultFormatter.php +++ b/application/formatter/BookmarkDefaultFormatter.php @@ -50,11 +50,10 @@ class BookmarkDefaultFormatter extends BookmarkFormatter */ public function formatUrl($bookmark) { - if (! empty($this->contextData['index_url']) && ( - startsWith($bookmark->getUrl(), '?') || startsWith($bookmark->getUrl(), '/') - )) { - return $this->contextData['index_url'] . escape($bookmark->getUrl()); + if ($bookmark->isNote() && !empty($this->contextData['index_url'])) { + return rtrim($this->contextData['index_url'], '/') . '/' . escape(ltrim($bookmark->getUrl(), '/')); } + return escape($bookmark->getUrl()); } @@ -63,11 +62,18 @@ class BookmarkDefaultFormatter extends BookmarkFormatter */ protected function formatRealUrl($bookmark) { - if (! empty($this->contextData['index_url']) && ( - startsWith($bookmark->getUrl(), '?') || startsWith($bookmark->getUrl(), '/') - )) { - return $this->contextData['index_url'] . escape($bookmark->getUrl()); + if ($bookmark->isNote()) { + if (!empty($this->contextData['index_url'])) { + $prefix = rtrim($this->contextData['index_url'], '/') . '/'; + } + + if (!empty($this->contextData['base_path'])) { + $prefix = rtrim($this->contextData['base_path'], '/') . '/'; + } + + return escape($prefix ?? '') . escape(ltrim($bookmark->getUrl(), '/')); } + return escape($bookmark->getUrl()); } diff --git a/application/formatter/BookmarkFormatter.php b/application/formatter/BookmarkFormatter.php index a80d83fc..22ba7aae 100644 --- a/application/formatter/BookmarkFormatter.php +++ b/application/formatter/BookmarkFormatter.php @@ -3,8 +3,8 @@ namespace Shaarli\Formatter; use DateTime; -use Shaarli\Config\ConfigManager; use Shaarli\Bookmark\Bookmark; +use Shaarli\Config\ConfigManager; /** * Class BookmarkFormatter @@ -80,6 +80,8 @@ abstract class BookmarkFormatter public function addContextData($key, $value) { $this->contextData[$key] = $value; + + return $this; } /** @@ -128,7 +130,7 @@ abstract class BookmarkFormatter */ protected function formatRealUrl($bookmark) { - return $bookmark->getUrl(); + return $this->formatUrl($bookmark); } /** diff --git a/application/formatter/FormatterFactory.php b/application/formatter/FormatterFactory.php index 5f282f68..a029579f 100644 --- a/application/formatter/FormatterFactory.php +++ b/application/formatter/FormatterFactory.php @@ -38,7 +38,7 @@ class FormatterFactory * * @return BookmarkFormatter instance. */ - public function getFormatter(string $type = null) + public function getFormatter(string $type = null): BookmarkFormatter { $type = $type ? $type : $this->conf->get('formatter', 'default'); $className = '\\Shaarli\\Formatter\\Bookmark'. ucfirst($type) .'Formatter'; diff --git a/application/front/controller/visitor/BookmarkListController.php b/application/front/controller/visitor/BookmarkListController.php index 23c4fbae..2988bee6 100644 --- a/application/front/controller/visitor/BookmarkListController.php +++ b/application/front/controller/visitor/BookmarkListController.php @@ -32,6 +32,7 @@ class BookmarkListController extends ShaarliVisitorController } $formatter = $this->container->formatterFactory->getFormatter(); + $formatter->addContextData('base_path', $this->container->basePath); $searchTags = escape(normalize_spaces($request->getParam('searchtags') ?? '')); $searchTerm = escape(normalize_spaces($request->getParam('searchterm') ?? ''));; @@ -145,11 +146,14 @@ class BookmarkListController extends ShaarliVisitorController $this->updateThumbnail($bookmark); + $formatter = $this->container->formatterFactory->getFormatter(); + $formatter->addContextData('base_path', $this->container->basePath); + $data = array_merge( $this->initializeTemplateVars(), [ 'pagetitle' => $bookmark->getTitle() .' - '. $this->container->conf->get('general.title', 'Shaarli'), - 'links' => [$this->container->formatterFactory->getFormatter()->format($bookmark)], + 'links' => [$formatter->format($bookmark)], ] ); diff --git a/application/front/controller/visitor/DailyController.php b/application/front/controller/visitor/DailyController.php index 808ca5f7..54a4778f 100644 --- a/application/front/controller/visitor/DailyController.php +++ b/application/front/controller/visitor/DailyController.php @@ -54,6 +54,7 @@ class DailyController extends ShaarliVisitorController } $formatter = $this->container->formatterFactory->getFormatter(); + $formatter->addContextData('base_path', $this->container->basePath); // We pre-format some fields for proper output. foreach ($linksToDisplay as $key => $bookmark) { $linksToDisplay[$key] = $formatter->format($bookmark); diff --git a/application/netscape/NetscapeBookmarkUtils.php b/application/netscape/NetscapeBookmarkUtils.php index b150f649..b83f16f8 100644 --- a/application/netscape/NetscapeBookmarkUtils.php +++ b/application/netscape/NetscapeBookmarkUtils.php @@ -68,7 +68,7 @@ class NetscapeBookmarkUtils $link = $formatter->format($bookmark); $link['taglist'] = implode(',', $bookmark->getTags()); if ($bookmark->isNote() && $prependNoteUrl) { - $link['url'] = $indexUrl . $link['url']; + $link['url'] = rtrim($indexUrl, '/') . '/' . ltrim($link['url'], '/'); } $bookmarkLinks[] = $link; diff --git a/assets/default/js/base.js b/assets/default/js/base.js index 76e4fe2a..0f29799d 100644 --- a/assets/default/js/base.js +++ b/assets/default/js/base.js @@ -26,11 +26,15 @@ function findParent(element, tagName, attributes) { * Ajax request to refresh the CSRF token. */ function refreshToken(basePath) { + console.log('refresh'); const xhr = new XMLHttpRequest(); xhr.open('GET', `${basePath}/admin/token`); xhr.onload = () => { - const token = document.getElementById('token'); - token.setAttribute('value', xhr.responseText); + const elements = document.querySelectorAll('input[name="token"]'); + [...elements].forEach((element) => { + console.log(element); + element.setAttribute('value', xhr.responseText); + }); }; xhr.send(); } diff --git a/tests/api/controllers/links/PostLinkTest.php b/tests/api/controllers/links/PostLinkTest.php index b2dd09eb..4e791a04 100644 --- a/tests/api/controllers/links/PostLinkTest.php +++ b/tests/api/controllers/links/PostLinkTest.php @@ -131,8 +131,8 @@ class PostLinkTest extends TestCase $this->assertEquals(self::NB_FIELDS_LINK, count($data)); $this->assertEquals(43, $data['id']); $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']); - $this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']); - $this->assertEquals('?' . $data['shorturl'], $data['title']); + $this->assertEquals('http://domain.tld/shaare/' . $data['shorturl'], $data['url']); + $this->assertEquals('/shaare/' . $data['shorturl'], $data['title']); $this->assertEquals('', $data['description']); $this->assertEquals([], $data['tags']); $this->assertEquals(true, $data['private']); diff --git a/tests/api/controllers/links/PutLinkTest.php b/tests/api/controllers/links/PutLinkTest.php index cb63742e..302cac0f 100644 --- a/tests/api/controllers/links/PutLinkTest.php +++ b/tests/api/controllers/links/PutLinkTest.php @@ -114,8 +114,8 @@ class PutLinkTest extends \PHPUnit\Framework\TestCase $this->assertEquals(self::NB_FIELDS_LINK, count($data)); $this->assertEquals($id, $data['id']); $this->assertEquals('WDWyig', $data['shorturl']); - $this->assertEquals('http://domain.tld/?WDWyig', $data['url']); - $this->assertEquals('?WDWyig', $data['title']); + $this->assertEquals('http://domain.tld/shaare/WDWyig', $data['url']); + $this->assertEquals('/shaare/WDWyig', $data['title']); $this->assertEquals('', $data['description']); $this->assertEquals([], $data['tags']); $this->assertEquals(true, $data['private']); diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php index b19c8250..a8bf47cb 100644 --- a/tests/bookmark/BookmarkFileServiceTest.php +++ b/tests/bookmark/BookmarkFileServiceTest.php @@ -200,7 +200,7 @@ class BookmarkFileServiceTest extends TestCase $bookmark = $this->privateLinkDB->get(43); $this->assertEquals(43, $bookmark->getId()); - $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); + $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl()); $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); $this->assertEmpty($bookmark->getDescription()); @@ -216,7 +216,7 @@ class BookmarkFileServiceTest extends TestCase $bookmark = $this->privateLinkDB->get(43); $this->assertEquals(43, $bookmark->getId()); - $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); + $this->assertRegExp('#/shaare/[\w\-]{6}#', $bookmark->getUrl()); $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); $this->assertEmpty($bookmark->getDescription()); diff --git a/tests/bookmark/BookmarkTest.php b/tests/bookmark/BookmarkTest.php index 9a3bbbfc..4b6a3c07 100644 --- a/tests/bookmark/BookmarkTest.php +++ b/tests/bookmark/BookmarkTest.php @@ -124,8 +124,8 @@ class BookmarkTest extends TestCase $this->assertEquals(1, $bookmark->getId()); $this->assertEquals('abc', $bookmark->getShortUrl()); $this->assertEquals($date, $bookmark->getCreated()); - $this->assertEquals('?abc', $bookmark->getUrl()); - $this->assertEquals('?abc', $bookmark->getTitle()); + $this->assertEquals('/shaare/abc', $bookmark->getUrl()); + $this->assertEquals('/shaare/abc', $bookmark->getTitle()); $this->assertEquals('', $bookmark->getDescription()); $this->assertEquals([], $bookmark->getTags()); $this->assertEquals('', $bookmark->getTagsString()); diff --git a/tpl/default/daily.html b/tpl/default/daily.html index aa34bd1c..3ab8053f 100644 --- a/tpl/default/daily.html +++ b/tpl/default/daily.html @@ -85,7 +85,7 @@ {if="$link.tags"}
{loop="link.taglist"} - + {$value} {/loop} -- cgit v1.2.3