Refactor feed functions out of Library and add some enums

This commit is contained in:
Alex Cabal 2024-11-15 21:09:42 -06:00
parent 66c44cbdbe
commit 90b70b3235
8 changed files with 117 additions and 101 deletions

View file

@ -4,6 +4,7 @@ use Safe\DateTimeImmutable;
use function Safe\exec;
use function Safe\file_get_contents;
use function Safe\file_put_contents;
use function Safe\glob;
use function Safe\tempnam;
use function Safe\unlink;
@ -64,4 +65,55 @@ abstract class Feed{
file_put_contents($this->Path, $feed);
}
/**
* @return ?array<stdClass>
*
* @throws Exceptions\AppException
*/
public static function RebuildFeedsCache(?Enums\FeedType $returnType = null, ?Enums\FeedCollectionType $returnCollectionType = null): ?array{
$retval = null;
$collator = Collator::create('en_US'); // Used for sorting letters with diacritics like in author names
if($collator === null){
throw new Exceptions\AppException('Couldn\'t create collator object when rebuilding feeds cache.');
}
foreach(Enums\FeedType::cases() as $type){
foreach(Enums\FeedCollectionType::cases() as $collectionType){
$files = glob(WEB_ROOT . '/feeds/' . $type->value . '/' . $collectionType->value . '/*.xml');
$feeds = [];
foreach($files as $file){
$obj = new stdClass();
$obj->Url = '/feeds/' . $type->value . '/' . $collectionType->value . '/' . basename($file, '.xml');
$obj->Label = exec('attr -g se-label ' . escapeshellarg($file)) ?: null;
if($obj->Label == null){
$obj->Label = basename($file, '.xml');
}
$obj->LabelSort = exec('attr -g se-label-sort ' . escapeshellarg($file)) ?: null;
if($obj->LabelSort == null){
$obj->LabelSort = basename($file, '.xml');
}
$feeds[] = $obj;
}
usort($feeds, function(stdClass $a, stdClass $b) use($collator): int{
$result = $collator->compare($a->LabelSort, $b->LabelSort);
return $result === false ? 0 : $result;
});
if($type == $returnType && $collectionType == $returnCollectionType){
$retval = $feeds;
}
apcu_store('feeds-index-' . $type->value . '-' . $collectionType->value, $feeds);
}
}
return $retval;
}
}