fibable/lib/ArticleManager.php

119 lines
4.7 KiB
PHP
Raw Normal View History

2023-09-04 21:13:29 +02:00
<?php
final class ArticleManager
{
const BASE_PATH = "./data/articles";
public static function get(string $fullref): Article
{
$serial_path = "./data/serial/$fullref.pobj";
return is_file($serial_path) ?
unserialize(file_get_contents($serial_path)) :
unserialize(file_get_contents("./data/serial/default.pobj"));
}
public static function getMultiple(array $fullrefs): array
{
$articles = [];
foreach ($fullrefs as $fullref) {
$articles[] = self::get($fullref);
}
return $articles;
}
public static function getNewest(): Article
{
$dot_dirs = [".", ".."];
$years = array_diff(scandir(self::BASE_PATH, SCANDIR_SORT_DESCENDING), $dot_dirs);
$year = $years[0];
$path = self::BASE_PATH . "/$year";
$months = array_diff(scandir($path, SCANDIR_SORT_DESCENDING), $dot_dirs);
$month = $months[0];
$path = "$path/$month";
$days = array_diff(scandir($path, SCANDIR_SORT_DESCENDING), $dot_dirs);
$day = $days[0];
$path = "$path/$day";
foreach (array_diff(scandir($path, SCANDIR_SORT_DESCENDING), $dot_dirs) as $file) {
$file_path = "$path/$file";
if (pathinfo($file_path, PATHINFO_EXTENSION) !== "txt") {
continue;
}
$ref = substr($file, 0, -4);
break;
}
return self::get("$year$month$day$ref");
}
public static function getLatestArticles(int $count): array
{
$dot_dirs = [".", ".."];
$articles = [];
foreach (array_diff(scandir(self::BASE_PATH, SCANDIR_SORT_DESCENDING), $dot_dirs) as $year) {
$year_path = self::BASE_PATH . "/$year";
foreach (array_diff(scandir($year_path, SCANDIR_SORT_DESCENDING), $dot_dirs) as $month) {
$month_path = "$year_path/$month";
foreach (array_diff(scandir($month_path, SCANDIR_SORT_DESCENDING), $dot_dirs) as $day) {
$day_path = "$month_path/$day";
foreach (array_diff(scandir($day_path, SCANDIR_SORT_DESCENDING), $dot_dirs) as $file) {
$file_path = "$day_path/$file";
if (pathinfo($file_path, PATHINFO_EXTENSION) !== "txt") {
continue;
}
$articles[] = self::getArticle($file_path);
if (count($articles) === $count) {
return $articles;
}
}
}
}
}
return $articles;
}
public static function getAll(): array
{
$dot_dirs = [".", ".."];
$dates = [];
foreach (array_diff(scandir(self::BASE_PATH, SCANDIR_SORT_DESCENDING), $dot_dirs) as $year) {
$year_path = self::BASE_PATH . "/$year";
foreach (array_diff(scandir($year_path, SCANDIR_SORT_DESCENDING), $dot_dirs) as $month) {
$month_path = "$year_path/$month";
foreach (array_diff(scandir($month_path, SCANDIR_SORT_DESCENDING), $dot_dirs) as $day) {
$day_path = "$month_path/$day";
$date = "$year$month$day";
foreach (array_diff(scandir($day_path, SCANDIR_SORT_DESCENDING), $dot_dirs) as $file) {
$file_path = "$day_path/$file";
if (pathinfo($file_path, PATHINFO_EXTENSION) !== "txt") {
continue;
}
if (!isset($dates["$date"])) {
$dates["$date"] = new ArticleDate("$day/$month/$year");
}
$dates["$date"]->articles[] = self::getArticle($file_path);
}
}
}
}
return $dates;
}
private static function getArticle(string $file_path): Article
{
$pattern = "|(./data)/articles/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)\.txt|";
$replacement = "$1/serial/$2$3$4$5.pobj";
$serial_path = preg_replace($pattern, $replacement, $file_path);
return file_exists($serial_path) ?
unserialize(file_get_contents($serial_path)) :
self::createArticle($file_path, $serial_path);
}
private static function createArticle(string $file_path, string $serial_path): Article
{
$parser = new ArticleParser();
$article = $parser->parse($file_path);
file_put_contents($serial_path, serialize($article));
TagManager::add($article->tags, substr(basename($serial_path), 0, -5));
return $article;
}
}