Skip to content

Commit 992ba79

Browse files
committed
feat: added seo friendly URLs for bookmarks (#2552)
1 parent 386f256 commit 992ba79

File tree

7 files changed

+52
-22
lines changed

7 files changed

+52
-22
lines changed

nginx.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ server {
8080
# a solution id page
8181
rewrite ^/solution_id_([0-9]+).html$ /index.php?solution_id=$1 last;
8282

83+
# the bookmarks page
84+
rewrite ^/bookmarks.html$ /index.php?action=bookmarks last;
85+
8386
# PMF faq record page
8487
# * http://[...]/content/1/1/<LANGUAGE CODE>/<FAQ TOPIC>.htm
8588
# * http://[...]/content/1/1/<LANGUAGE CODE>/<FAQ TOPIC>.html

phpmyfaq/.htaccess

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ RewriteRule ^index.html$ index.php [PT]
9191
RewriteCond %{REQUEST_URI} solution_id_([0-9]+)\.html$ [NC]
9292
RewriteRule ^solution_id_(.*)\.html$ index.php?solution_id=$1 [L,QSA]
9393

94+
# the bookmarks page
95+
RewriteCond %{REQUEST_URI} /bookmarks\.html$ [NC]
96+
RewriteRule ^(.*)$ index.php?action=bookmarks [L,QSA]
97+
9498
# PMF faq record page
9599
# * http://[...]/content/1/1/<LANGUAGE CODE>/<FAQ TOPIC>.htm
96100
# * http://[...]/content/1/1/<LANGUAGE CODE>/<FAQ TOPIC>.html

phpmyfaq/bookmarks.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616
* @since 2023-07-20
1717
*/
1818

19-
use Symfony\Component\HttpFoundation\Request;
19+
use phpMyFAQ\Configuration;
2020
use phpMyFAQ\Translation;
2121
use phpMyFAQ\Bookmark;
22+
use phpMyFAQ\User\CurrentUser;
2223

2324
if (!defined('IS_VALID_PHPMYFAQ')) {
2425
http_response_code(400);
2526
exit();
2627
}
2728

28-
$request = Request::createFromGlobals();
29+
$faqConfig = Configuration::getConfigurationInstance();
30+
$user = CurrentUser::getCurrentUser($faqConfig);
2931

3032
$bookmark = new Bookmark($faqConfig, $user);
3133

phpmyfaq/faq.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
$bookmarkAlert = $alert->success('msgBookmarkAdded');
9191
}
9292
if ($bookmarkAction === 'remove' && isset($faqId)) {
93-
$bookmark->removeBookmark($faqId);
93+
$bookmark->remove($faqId);
9494
$alert = new Alert();
9595
$bookmarkAlert = $alert->success('msgBookmarkRemoved');
9696
}
@@ -303,7 +303,7 @@
303303
$response->setStatusCode(Response::HTTP_NOT_FOUND);
304304
}
305305

306-
// Check if author name should be visible according to GDPR option
306+
// Check if the author name should be visible, according to the GDPR option
307307
if ($user->getUserVisibilityByEmail($faq->faqRecord['email'])) {
308308
$author = $faq->faqRecord['author'];
309309
} else {
@@ -349,12 +349,23 @@
349349
'msgNewContentSubmit' => Translation::get('msgNewContentSubmit'),
350350
'csrfInput' => Token::getInstance()->getTokenInput('add-comment'),
351351
'captchaFieldset' =>
352-
$captchaHelper->renderCaptcha($captcha, 'writecomment', Translation::get('msgCaptcha'), $user->isLoggedIn()),
352+
$captchaHelper->renderCaptcha(
353+
$captcha,
354+
'writecomment',
355+
Translation::get('msgCaptcha'),
356+
$user->isLoggedIn()
357+
),
353358
'renderComments' => $comment->getComments($faqId, CommentType::FAQ),
354359
'msg_about_faq' => Translation::get('msg_about_faq'),
355-
'bookmarkIcon' => ($bookmark->isFaqBookmark($faqId)===true) ? 'fa fa-bookmark' : 'fa fa-bookmark-o',
356-
'bookmarkLink' => ($bookmark->isFaqBookmark($faqId)===true) ? sprintf('index.php?action=faq&bookmark_action=remove&id=%s', $faqId) : sprintf('index.php?action=faq&bookmark_action=add&id=%s', $faqId),
357-
'msgAddBookmark' => ($bookmark->isFaqBookmark($faqId)===true) ? Translation::get('removeBookmark') : Translation::get('msgAddBookmark'),
360+
'bookmarkIcon' => $bookmark->isFaqBookmark($faqId) ? 'fa fa-bookmark' : 'fa fa-bookmark-o',
361+
'bookmarkLink' =>
362+
$bookmark->isFaqBookmark($faqId)
363+
?
364+
sprintf('index.php?action=faq&bookmark_action=remove&id=%d', $faqId)
365+
:
366+
sprintf('index.php?action=faq&bookmark_action=add&id=%d', $faqId),
367+
'msgAddBookmark' =>
368+
$bookmark->isFaqBookmark($faqId) ? Translation::get('removeBookmark') : Translation::get('msgAddBookmark'),
358369
'alert' => (isset($bookmarkAlert)) ? $bookmarkAlert : '',
359370
]
360371
);

phpmyfaq/index.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,10 @@
721721
'<a class="dropdown-item" href="?action=logout&csrf=%s">%s</a>',
722722
Token::getInstance()->getTokenString('logout'),
723723
Translation::get('ad_menu_logout'),
724-
'msgBookmarks' => sprintf('<a class="dropdown-item" href="?action=bookmarks">%s</a>',
725-
Translation::get('msgBookmarks'))
724+
),
725+
'msgBookmarks' => sprintf(
726+
'<a class="dropdown-item" href="?action=bookmarks">%s</a>',
727+
Translation::get('msgBookmarks')
726728
)
727729
]
728730
);

phpmyfaq/src/phpMyFAQ/Bookmark.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
*
1010
* @package phpMyFAQ
1111
* @author Jan Harms <model_railroader@gmx-topmail.de>
12-
* @copyright 2004-2023 phpMyFAQ Team
12+
* @copyright 2023 phpMyFAQ Team
1313
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
1414
* @link https://www.phpmyfaq.de
1515
* @since 2023-07-19
1616
*/
1717

1818
namespace phpMyFAQ;
1919

20-
use phpMyFAQ\Configuration;
20+
use phpMyFAQ\Language\Plurals;
2121
use phpMyFAQ\User\CurrentUser;
2222

2323
/**
@@ -33,7 +33,7 @@ class Bookmark
3333
* @param Configuration $config Configuration object
3434
* @param CurrentUser $user CurrentUser object
3535
*/
36-
public function __construct(private Configuration $config, private CurrentUser $user)
36+
public function __construct(private readonly Configuration $config, private readonly CurrentUser $user)
3737
{
3838
}
3939

@@ -59,7 +59,7 @@ public function isFaqBookmark(int $faqId): bool
5959
/**
6060
* Saves a given Faq to the bookmark collection of the current user.
6161
*
62-
* @param int $faqId Id of the Faq
62+
* @param int $faqId ID of the Faq
6363
*/
6464
public function saveFaqAsBookmarkById(int $faqId)
6565
{
@@ -78,7 +78,7 @@ public function saveFaqAsBookmarkById(int $faqId)
7878
public function getAll()
7979
{
8080
$query = sprintf(
81-
'SELECT faqid FROM %sfaqbookmarks WHERE userid=%d',
81+
'SELECT faqid FROM %sfaqbookmarks WHERE userid = %d',
8282
Database::getTablePrefix(),
8383
$this->user->getUserId()
8484
);
@@ -89,21 +89,22 @@ public function getAll()
8989
/**
9090
* Removes a bookmark from the current user.
9191
*
92-
* @param int $faqId Id of the Faq
92+
* @param int $faqId ID of the Faq
9393
*/
94-
public function removeBookmark(int $faqId)
94+
public function remove(int $faqId): bool
9595
{
9696
$query = sprintf(
97-
'DELETE FROM %sfaqbookmarks WHERE userid=%d AND faqid=%d',
97+
'DELETE FROM %sfaqbookmarks WHERE userid = %d AND faqid = %d',
9898
Database::getTablePrefix(),
9999
$this->user->getUserId(),
100100
$faqId
101101
);
102-
return $this->config->getDb()->query($query);
102+
103+
return (bool) $this->config->getDb()->query($query);
103104
}
104105

105106
/**
106-
* Renders the bookmark tree for the personally bookmark list.
107+
* Renders the bookmark tree for the personal bookmark list.
107108
*
108109
* @return string
109110
*/
@@ -122,7 +123,8 @@ public function renderBookmarkTree(): string
122123
*/
123124
public function buildBookmarkList(): string
124125
{
125-
$bookmarks = $this->getAllBookmarks();
126+
$bookmarks = $this->getAll();
127+
$plurals = new Plurals();
126128
$html = '';
127129
foreach ($bookmarks as $object => $key) {
128130
$faq = new Faq($this->config);
@@ -139,7 +141,7 @@ public function buildBookmarkList(): string
139141
$link->itemTitle = $faqData['title'];
140142
$link->tooltip = $faqData['title'];
141143

142-
$plurals = new Plurals();
144+
143145
$visits = new Visits($this->config);
144146
foreach ($visits->getAllData() as $item) {
145147
if ((string) $key->faqid === $item['id']) {

phpmyfaq/src/phpMyFAQ/Link.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class Link
6767
private const LINK_GET_ACTION_SEARCH = 'search';
6868
private const LINK_GET_ACTION_SITEMAP = 'sitemap';
6969
private const LINK_GET_ACTION_SHOW = 'show';
70+
private const LINK_GET_ACTION_BOOKMARKS = 'bookmarks';
7071
private const LINK_HTML_EXTENSION = '.html';
7172

7273
private const LINK_HTML_ADDCONTENT = 'addcontent.html';
@@ -78,6 +79,7 @@ class Link
7879
private const LINK_HTML_OPEN = 'open-questions.html';
7980
private const LINK_HTML_SEARCH = 'search.html';
8081
private const LINK_HTML_SHOWCAT = 'show-categories.html';
82+
private const LINK_HTML_BOOKMARKS = 'bookmarks.html';
8183

8284
/**
8385
* @var int[] List of allowed action parameters
@@ -363,6 +365,10 @@ public function toString(bool $removeSessionFromUrl = false): string
363365
$url .= self::LINK_HTML_LOGIN;
364366
break;
365367

368+
case self::LINK_GET_ACTION_BOOKMARKS:
369+
$url .= self::LINK_HTML_BOOKMARKS;
370+
break;
371+
366372
case self::LINK_GET_ACTION_SEARCH:
367373
if (
368374
!isset($getParams[self::LINK_GET_ACTION_SEARCH])

0 commit comments

Comments
 (0)