diff --git a/include/Models/BasicModel.php b/include/Models/BasicModel.php index e6ea3147..4e4485c2 100644 --- a/include/Models/BasicModel.php +++ b/include/Models/BasicModel.php @@ -7,6 +7,8 @@ use Phpfastcache\Exceptions\PhpfastcacheDriverException; abstract class BasicModel { + const FILE_NOT_FOUND = 'The filename %s could not be found'; + protected static $cache; public function __construct() @@ -23,6 +25,22 @@ abstract class BasicModel } } + protected function getLocalizedFile($filename) { + global $lang; + if (!$lang) { + $lang = DEFAULT_LOCALE; + } + $localizedFilename = DIR_DATA . "/$lang/$filename"; + $defaultFilename = DIR_DATA . "/" . DEFAULT_LOCALE . "/$filename"; + if (is_file($localizedFilename) && is_readable($localizedFilename)) { + return $localizedFilename; + } elseif (is_file($defaultFilename) && is_readable($defaultFilename)) { + return $defaultFilename; + } else { + throw new \ErrorException(\sprintf(self::FILE_NOT_FOUND, $filename)); + } + } + protected function saveToCache($data, $key = '') { if ($key) { diff --git a/include/Models/CompatibilityModel.php b/include/Models/CompatibilityModel.php index d2ff4dc7..6f256e8f 100644 --- a/include/Models/CompatibilityModel.php +++ b/include/Models/CompatibilityModel.php @@ -24,12 +24,16 @@ class CompatibilityModel extends BasicModel $this->platformsModel = new SimpleModel("Platform", "platforms.yaml"); } + public function getLastUpdated() { + return filemtime($this->getLocalizedFile("compatibility.yaml")); + } + /* Get all the groups and the respectively demos for the specified ScummVM version. */ public function getAllData($version) { $data = $this->getFromCache($version); if (is_null($data)) { - $fname = DIR_DATA . "/compatibility.yaml"; + $fname = $this->getLocalizedFile("compatibility.yaml"); $compatibilityEntries = \yaml_parse_file($fname); $games = $this->gameModel->getAllGames(); $platforms = $this->platformsModel->getAllData(); diff --git a/include/Models/DownloadsModel.php b/include/Models/DownloadsModel.php index 263b0d27..0f16cfcf 100644 --- a/include/Models/DownloadsModel.php +++ b/include/Models/DownloadsModel.php @@ -15,7 +15,7 @@ class DownloadsModel extends BasicModel { $sections = $this->getFromCache(); if (is_null($sections)) { - $fname = DIR_DATA . '/downloads.xml'; + $fname = $this->getLocalizedFile('downloads.xml'); /* Now parse the data. */ $parser = new XMLParser(); $parsedData = $parser->parseByFilename($fname); diff --git a/include/Models/FAQModel.php b/include/Models/FAQModel.php index f1d67340..d29b31df 100644 --- a/include/Models/FAQModel.php +++ b/include/Models/FAQModel.php @@ -19,22 +19,7 @@ class FAQModel extends BasicModel /* Get the full path and filename for the F.A.Q. XML-file. */ public function getFilename() { - global $lang; - - if ($lang !== DEFAULT_LOCALE) { - $localized = DIR_DATA . "/faq-xml." . $lang . ".xml"; - - if (is_file($localized)) { - if (!is_readable($localized)) { - $file = "\n\nFilename: " . basename($localized) . "\n"; - throw new \ErrorException(self::FILE_NOT_FOUND . $file); - } else { - return $localized; - } - } - } - - return DIR_DATA . '/faq-xml.xml'; + return $this->getLocalizedFile('faq.xml'); } /* Get last modification time. */ diff --git a/include/Models/GameDemosModel.php b/include/Models/GameDemosModel.php index 9b244307..1ea53d51 100644 --- a/include/Models/GameDemosModel.php +++ b/include/Models/GameDemosModel.php @@ -23,7 +23,7 @@ class GameDemosModel extends BasicModel { $groupedData = $this->getFromCache(); if (is_null($groupedData)) { - $fname = DIR_DATA . '/game_demos.yaml'; + $fname = $this->getLocalizedFile('game_demos.yaml'); $demos = \yaml_parse_file($fname); $games = $this->gameModel->getAllGames(); $platforms = $this->platformsModel->getAllData(); diff --git a/include/Models/GameDownloadsModel.php b/include/Models/GameDownloadsModel.php index 756d3de4..c61fd339 100644 --- a/include/Models/GameDownloadsModel.php +++ b/include/Models/GameDownloadsModel.php @@ -12,7 +12,7 @@ class GameDownloadsModel extends BasicModel /* Get all download entries. */ public function getAllDownloads() { - $fname = DIR_DATA . '/games.xml'; + $fname = $this->getLocalizedFile('games.xml'); /* Now parse the data. */ $parser = new XMLParser(); $parsedData = $parser->parseByFilename($fname); diff --git a/include/Models/GameModel.php b/include/Models/GameModel.php index aac7a9b8..dddf3462 100644 --- a/include/Models/GameModel.php +++ b/include/Models/GameModel.php @@ -28,7 +28,7 @@ class GameModel extends BasicModel $companies = $this->companiesModel->getAllData(); $engines = $this->enginesModel->getAllData(); $series = $this->seriesModel->getAllData(); - $fname = DIR_DATA . '/games.yaml'; + $fname = $this->getLocalizedFile('games.yaml'); $games = \yaml_parse_file($fname); $data = []; foreach ($games as $game) { diff --git a/include/Models/LinksModel.php b/include/Models/LinksModel.php index ac1e898b..15e7e27e 100644 --- a/include/Models/LinksModel.php +++ b/include/Models/LinksModel.php @@ -14,7 +14,7 @@ class LinksModel extends BasicModel { $entries = $this->getFromCache(); if (is_null($entries)) { - $fname = DIR_DATA . '/links.yaml'; + $fname = $this->getLocalizedFile('links.yaml'); $parsedData = \yaml_parse_file($fname); $entries = []; foreach ($parsedData as $value) { diff --git a/include/Models/ScreenshotsModel.php b/include/Models/ScreenshotsModel.php index 38fbeb74..08d059b0 100644 --- a/include/Models/ScreenshotsModel.php +++ b/include/Models/ScreenshotsModel.php @@ -26,7 +26,7 @@ class ScreenshotsModel extends BasicModel /* Get all screenshots. */ public function getAllScreenshots() { - $fname = DIR_DATA . '/screenshots.yaml'; + $fname = $this->getLocalizedFile('screenshots.yaml'); $screenshots = \yaml_parse_file($fname); $platforms = $this->platformsModel->getAllData(); $games = $this->gameModel->getAllGames(); diff --git a/include/Models/SimpleModel.php b/include/Models/SimpleModel.php index d52e3ff6..6c7fdd4b 100644 --- a/include/Models/SimpleModel.php +++ b/include/Models/SimpleModel.php @@ -16,10 +16,7 @@ class SimpleModel extends BasicModel public function __construct($type, $filename) { parent::__construct(); - $this->filename = DIR_DATA . "/$filename"; - if (!is_file($this->filename) || !is_readable($this->filename)) { - throw new \ErrorException(\sprintf(self::FILE_NOT_FOUND, $this->filename)); - } + $this->filename = $this->getLocalizedFile($filename); $this->type = "ScummVM\Objects\\$type"; } diff --git a/include/Models/VersionsModel.php b/include/Models/VersionsModel.php index d8bab600..3efafba1 100644 --- a/include/Models/VersionsModel.php +++ b/include/Models/VersionsModel.php @@ -13,7 +13,7 @@ class VersionsModel extends BasicModel { $data = $this->getFromCache(); if (is_null($data)) { - $fname = DIR_DATA . '/versions.yaml'; + $fname = $this->getLocalizedFile('versions.yaml'); $versions = \yaml_parse_file($fname); $data = [];