Adjust how tags URLs are calculated to support tags with non-ASCII characters like 'children’s'

This commit is contained in:
Alex Cabal 2022-06-18 21:17:33 -05:00
parent c5b686636b
commit 6c267497cc
5 changed files with 14 additions and 20 deletions

View file

@ -728,7 +728,7 @@ class Ebook{
public function HasTag(string $tag): bool{ public function HasTag(string $tag): bool{
foreach($this->Tags as $t){ foreach($this->Tags as $t){
if(strtolower($t->Name) == strtolower($tag)){ if(strtolower($t->UrlName) == strtolower($tag)){
return true; return true;
} }
} }

View file

@ -200,7 +200,7 @@ class Library{
$ebooks = []; $ebooks = [];
$collections = []; $collections = [];
$tags = []; $ebooksByTag = [];
$authors = []; $authors = [];
$tagsByName = []; $tagsByName = [];
@ -236,13 +236,12 @@ class Library{
// Create the tags cache // Create the tags cache
foreach($ebook->Tags as $tag){ foreach($ebook->Tags as $tag){
$tagsByName[] = $tag->Name; $tagsByName[$tag->UrlName] = $tag;
$lcTag = strtolower($tag->Name); if(!array_key_exists($tag->UrlName, $ebooksByTag)){
if(!array_key_exists($lcTag, $tags)){ $ebooksByTag[$tag->UrlName] = [];
$tags[$lcTag] = [];
} }
$tags[$lcTag][] = $ebook; $ebooksByTag[$tag->UrlName][] = $ebook;
} }
// Create the authors cache // Create the authors cache
@ -288,13 +287,12 @@ class Library{
} }
apcu_delete(new APCUIterator('/^tag-/')); apcu_delete(new APCUIterator('/^tag-/'));
foreach($tags as $tag => $ebooks){ foreach($ebooksByTag as $tagName => $ebooks){
apcu_store('tag-' . $tag, $ebooks); apcu_store('tag-' . $tagName, $ebooks);
} }
ksort($tagsByName);
apcu_delete('tags'); apcu_delete('tags');
$tagsByName = array_unique($tagsByName, SORT_STRING);
natsort($tagsByName);
apcu_store('tags', $tagsByName); apcu_store('tags', $tagsByName);
apcu_delete(new APCUIterator('/^author-/')); apcu_delete(new APCUIterator('/^author-/'));

View file

@ -2,9 +2,11 @@
class Tag{ class Tag{
public $Name; public $Name;
public $Url; public $Url;
public $UrlName;
public function __construct(string $name){ public function __construct(string $name){
$this->Name = $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;
} }
} }

View file

@ -5,9 +5,8 @@ $allSelected = sizeof($tags) == 0 || in_array('all', $tags);
<label class="tags">Subjects <label class="tags">Subjects
<select multiple="multiple" name="tags[]" size="1"> <select multiple="multiple" name="tags[]" size="1">
<option value="all">All</option> <option value="all">All</option>
<? foreach(Library::GetTags() as $tag){ <? foreach(Library::GetTags() as $tag){ ?>
$lcTag = mb_strtolower($tag); ?> <option value="<?= $tag->UrlName ?>"<? if(!$allSelected && in_array($tag->UrlName, $tags)){ ?> selected="selected"<? } ?>><?= Formatter::ToPlainText($tag->Name) ?></option>
<option value="<?= Formatter::ToPlainText($lcTag) ?>"<? if(!$allSelected && in_array($lcTag, $tags)){ ?> selected="selected"<? } ?>><?= Formatter::ToPlainText($tag) ?></option>
<? } ?> <? } ?>
</select> </select>
</label> </label>

View file

@ -49,11 +49,6 @@ try{
$tags = []; $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? // Are we looking at a collection?
if($collection !== null){ if($collection !== null){
$ebooks = Library::GetEbooksByCollection($collection); $ebooks = Library::GetEbooksByCollection($collection);