From fdc6d5ff6e433b8e7835beeebe2526e16a4fddf5 Mon Sep 17 00:00:00 2001 From: Mataniko Date: Fri, 9 Oct 2020 09:10:18 -0400 Subject: [PATCH] WEB: Fix loading localized data for news and articles --- .../{article => articles}/snowberry.markdown | 0 include/Constants.php | 3 -- include/LocalizationUtils.php | 6 +-- include/Models/BasicModel.php | 3 +- include/Models/CompatibilityModel.php | 3 +- include/Models/NewsModel.php | 25 +++------- include/Pages/ArticlePage.php | 49 +++++++++++++------ 7 files changed, 48 insertions(+), 41 deletions(-) rename data/en/{article => articles}/snowberry.markdown (100%) diff --git a/data/en/article/snowberry.markdown b/data/en/articles/snowberry.markdown similarity index 100% rename from data/en/article/snowberry.markdown rename to data/en/articles/snowberry.markdown diff --git a/include/Constants.php b/include/Constants.php index 4e6c7f0e..bb088850 100644 --- a/include/Constants.php +++ b/include/Constants.php @@ -38,9 +38,6 @@ class Constants /* Paths */ define('DIR_BASE', __DIR__ . '/..'); define('DIR_DATA', DIR_BASE . '/data'); - define('DIR_NEWS', DIR_DATA . '/news'); - define('DIR_ARTICLE', DIR_DATA . '/article'); - define('DIR_COMPAT', DIR_DATA . '/compatibility'); define('DIR_DOWNLOADS', '/downloads'); define('DIR_SCREENSHOTS', '/data/screenshots'); define('DIR_FRS', '/.0/frs'); diff --git a/include/LocalizationUtils.php b/include/LocalizationUtils.php index 7236506d..10ce6a85 100644 --- a/include/LocalizationUtils.php +++ b/include/LocalizationUtils.php @@ -58,7 +58,7 @@ class LocalizationUtils $l10n = json_decode(file_get_contents($newsFile)); foreach ($l10n as $key => $translatedArticle) { - $englishArticle = YamlFrontMatter::parse(file_get_contents(join(DIRECTORY_SEPARATOR, [DIR_NEWS, DEFAULT_LOCALE,"/{$key}.markdown"]))); + $englishArticle = YamlFrontMatter::parse(file_get_contents(join(DIRECTORY_SEPARATOR, [DIR_DATA, DEFAULT_LOCALE, 'news', "/{$key}.markdown"]))); $date = $this->purifier->purify($englishArticle->date); $author = $this->purifier->purify($englishArticle->author); @@ -87,7 +87,7 @@ class LocalizationUtils $yaml = "---\ntitle: \"$title\"\ndate: $date\nauthor: $author\n---\n\n$content\n"; file_put_contents( - DIR_NEWS . "/{$lang}/{$key}.markdown", + DIR_DATA . "/{$lang}/news/{$key}.markdown", $yaml ); } @@ -106,7 +106,7 @@ class LocalizationUtils private function getAllNews($lang) { - $dir = join(DIRECTORY_SEPARATOR, [DIR_NEWS, $lang]); + $dir = join(DIRECTORY_SEPARATOR, [DIR_DATA, $lang, 'news']); if (!($files = scandir($dir))) { throw new \ErrorException(self::NO_FILES); diff --git a/include/Models/BasicModel.php b/include/Models/BasicModel.php index 4e4485c2..bc9c4255 100644 --- a/include/Models/BasicModel.php +++ b/include/Models/BasicModel.php @@ -25,7 +25,8 @@ abstract class BasicModel } } - protected function getLocalizedFile($filename) { + protected function getLocalizedFile($filename) + { global $lang; if (!$lang) { $lang = DEFAULT_LOCALE; diff --git a/include/Models/CompatibilityModel.php b/include/Models/CompatibilityModel.php index 6f256e8f..13d0956d 100644 --- a/include/Models/CompatibilityModel.php +++ b/include/Models/CompatibilityModel.php @@ -24,7 +24,8 @@ class CompatibilityModel extends BasicModel $this->platformsModel = new SimpleModel("Platform", "platforms.yaml"); } - public function getLastUpdated() { + public function getLastUpdated() + { return filemtime($this->getLocalizedFile("compatibility.yaml")); } diff --git a/include/Models/NewsModel.php b/include/Models/NewsModel.php index cbf5aacd..94e58114 100644 --- a/include/Models/NewsModel.php +++ b/include/Models/NewsModel.php @@ -15,7 +15,7 @@ class NewsModel extends BasicModel /* Get a list of all the available news files. */ private function getListOfNewsFilenames() { - if (!($files = scandir(join(DIRECTORY_SEPARATOR, [DIR_NEWS, DEFAULT_LOCALE])))) { + if (!($files = scandir(join(DIRECTORY_SEPARATOR, [DIR_DATA, DEFAULT_LOCALE, 'news'])))) { throw new \ErrorException(self::NO_FILES); } $filenames = array(); @@ -34,19 +34,19 @@ class NewsModel extends BasicModel { $news = $this->getFromCache(); if (is_null($news)) { - if (!($files = scandir(join(DIRECTORY_SEPARATOR, [DIR_NEWS, DEFAULT_LOCALE])))) { + if (!($files = scandir(join(DIRECTORY_SEPARATOR, [DIR_DATA, DEFAULT_LOCALE, 'news'])))) { throw new \ErrorException(self::NO_FILES); } global $lang; - $news = array(); + $news = []; foreach ($files as $filename) { if (substr($filename, -9) != '.markdown') { continue; } - if (!is_file(($fname = join(DIRECTORY_SEPARATOR, [DIR_NEWS,$lang,basename($filename)]))) + if (!is_file(($fname = join(DIRECTORY_SEPARATOR, [DIR_DATA, $lang, 'news', basename($filename)]))) || !is_readable($fname) || !($data = @file_get_contents($fname)) ) { - if (!($data = @file_get_contents(join(DIRECTORY_SEPARATOR, [DIR_NEWS, DEFAULT_LOCALE, $filename])))) { + if (!($data = @file_get_contents(join(DIRECTORY_SEPARATOR, [DIR_DATA, DEFAULT_LOCALE, 'news', $filename])))) { continue; } } @@ -69,7 +69,7 @@ class NewsModel extends BasicModel } rsort($newslist, SORT_STRING); $newslist = array_slice($newslist, 0, $num); - $news = array(); + $news = []; foreach ($newslist as $filename) { $news[] = $this->getOneByFilename($filename, $processContent); } @@ -83,17 +83,8 @@ class NewsModel extends BasicModel if (is_null($filename) || !preg_match('/^\d{8,12}[a-z]?$/', $filename)) { throw new \ErrorException(self::INVALID_DATE); } - global $lang; - - if (!is_file(($fname = join(DIRECTORY_SEPARATOR, [DIR_NEWS, $lang, "{$filename}.markdown"]))) - || !is_readable($fname) || !($data = @file_get_contents($fname)) - ) { - if (!is_file(($fname = join(DIRECTORY_SEPARATOR, [DIR_NEWS, DEFAULT_LOCALE, "/{$filename}.markdown"]))) - || !is_readable($fname) || !($data = @file_get_contents($fname)) - ) { - throw new \ErrorException(self::FILE_NOT_FOUND); - } - } + $fname = $this->getLocalizedFile("news/$filename.markdown"); + $data = @file_get_contents($fname); return new News($data, $fname, $processContent); } diff --git a/include/Pages/ArticlePage.php b/include/Pages/ArticlePage.php index 9e9debd2..a9473bc8 100644 --- a/include/Pages/ArticlePage.php +++ b/include/Pages/ArticlePage.php @@ -7,42 +7,59 @@ use Erusev\Parsedown; class ArticlePage extends Controller { - private $purifier; + const FILE_NOT_FOUND = 'The filename %s could not be found'; + const ARTICLE_NAME_MISSING = 'An article name is missing'; /* Constructor. */ public function __construct() { parent::__construct(); $this->template = 'pages/article.tpl'; - $config = \HTMLPurifier_Config::createDefault(); - $this->purifier = new \HTMLPurifier($config); + } + + private function getArticle($filename) + { + global $lang; + if (!$lang) { + $lang = DEFAULT_LOCALE; + } + $localizedFilename = join('/', [DIR_DATA, $lang, 'articles', $filename]); + $defaultFilename = join('/', [DIR_DATA, DEFAULT_LOCALE, 'articles', $filename]); + if (is_file($localizedFilename) && is_readable($localizedFilename)) { + $fname = $localizedFilename; + } elseif (is_file($defaultFilename) && is_readable($defaultFilename)) { + $fname = $defaultFilename; + } else { + throw new \ErrorException(\sprintf(self::FILE_NOT_FOUND, $filename)); + } + + return YamlFrontMatter::parse(file_get_contents($fname)); } /* Display the index page. */ public function index($params) { - $articleFile = DIR_ARTICLE . "/" . $params['article'] . ".markdown"; - if (!is_file($articleFile) || !is_readable($articleFile)) { - $page = new \ScummVM\Pages\NewsPage(); - return $page->index(array()); + if (!$params['article']) { + throw new \ErrorException(self::ARTICLE_NAME_MISSING); } + $filename = $params['article'] . '.markdown'; - $article = YamlFrontMatter::parse(file_get_contents($articleFile)); + $article = $this->getArticle($filename); + + $purifier = new \HTMLPurifier(\HTMLPurifier_Config::createDefault()); $Parsedown = new \Parsedown(); $Parsedown->setBreaksEnabled(true); - $date = $this->purifier->purify($article->date); - $title = $this->purifier->purify($article->title); - $author = $this->purifier->purify($article->author); - $content = $this->purifier->purify($Parsedown->text($article->body())); + $date = $purifier->purify($article->date); + $title = $purifier->purify($article->title); + $author = $purifier->purify($article->author); + $content = $purifier->purify($Parsedown->text($article->body())); - return $this->renderPage( - array( + return $this->renderPage([ 'content_title' => $title, 'date' => $date, 'author' => $author, 'content' => $content, - ) - ); + ]); } }