From 6c267497cce0dc6b7d0102517d4a3f311535e76b Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Sat, 18 Jun 2022 21:17:33 -0500 Subject: [PATCH] =?UTF-8?q?Adjust=20how=20tags=20URLs=20are=20calculated?= =?UTF-8?q?=20to=20support=20tags=20with=20non-ASCII=20characters=20like?= =?UTF-8?q?=20'children=E2=80=99s'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Ebook.php | 2 +- lib/Library.php | 18 ++++++++---------- lib/Tag.php | 4 +++- templates/SearchForm.php | 5 ++--- www/ebooks/index.php | 5 ----- 5 files changed, 14 insertions(+), 20 deletions(-) diff --git a/lib/Ebook.php b/lib/Ebook.php index 8f72a9aa..28a966d2 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -728,7 +728,7 @@ class Ebook{ public function HasTag(string $tag): bool{ foreach($this->Tags as $t){ - if(strtolower($t->Name) == strtolower($tag)){ + if(strtolower($t->UrlName) == strtolower($tag)){ return true; } } diff --git a/lib/Library.php b/lib/Library.php index 46ee082f..e7ad9e2e 100644 --- a/lib/Library.php +++ b/lib/Library.php @@ -200,7 +200,7 @@ class Library{ $ebooks = []; $collections = []; - $tags = []; + $ebooksByTag = []; $authors = []; $tagsByName = []; @@ -236,13 +236,12 @@ class Library{ // Create the tags cache foreach($ebook->Tags as $tag){ - $tagsByName[] = $tag->Name; - $lcTag = strtolower($tag->Name); - if(!array_key_exists($lcTag, $tags)){ - $tags[$lcTag] = []; + $tagsByName[$tag->UrlName] = $tag; + if(!array_key_exists($tag->UrlName, $ebooksByTag)){ + $ebooksByTag[$tag->UrlName] = []; } - $tags[$lcTag][] = $ebook; + $ebooksByTag[$tag->UrlName][] = $ebook; } // Create the authors cache @@ -288,13 +287,12 @@ class Library{ } apcu_delete(new APCUIterator('/^tag-/')); - foreach($tags as $tag => $ebooks){ - apcu_store('tag-' . $tag, $ebooks); + foreach($ebooksByTag as $tagName => $ebooks){ + apcu_store('tag-' . $tagName, $ebooks); } + ksort($tagsByName); apcu_delete('tags'); - $tagsByName = array_unique($tagsByName, SORT_STRING); - natsort($tagsByName); apcu_store('tags', $tagsByName); apcu_delete(new APCUIterator('/^author-/')); diff --git a/lib/Tag.php b/lib/Tag.php index 9e3fcbb6..024b3fb4 100644 --- a/lib/Tag.php +++ b/lib/Tag.php @@ -2,9 +2,11 @@ class Tag{ public $Name; public $Url; + public $UrlName; public function __construct(string $name){ $this->Name = $name; - $this->Url = '/tags/' . strtolower(str_replace(' ', '-', Formatter::ToPlainText($this->Name))); + $this->UrlName = Formatter::MakeUrlSafe($this->Name); + $this->Url = '/tags/' . $this->UrlName; } } diff --git a/templates/SearchForm.php b/templates/SearchForm.php index 7662a41e..2463f9f5 100644 --- a/templates/SearchForm.php +++ b/templates/SearchForm.php @@ -5,9 +5,8 @@ $allSelected = sizeof($tags) == 0 || in_array('all', $tags); diff --git a/www/ebooks/index.php b/www/ebooks/index.php index 7e3f8b71..ad7c0a55 100644 --- a/www/ebooks/index.php +++ b/www/ebooks/index.php @@ -49,11 +49,6 @@ try{ $tags = []; } - // Replace dashes passed in from URLs like /tags/science-fiction - foreach($tags as $key => $tag){ - $tags[$key] = str_replace('-', ' ', $tag); - } - // Are we looking at a collection? if($collection !== null){ $ebooks = Library::GetEbooksByCollection($collection);