WEB: Fix loading localized data for news and articles

This commit is contained in:
Mataniko
2020-10-09 09:10:18 -04:00
committed by Matan Bareket
parent 0a394a6260
commit fdc6d5ff6e
7 changed files with 48 additions and 41 deletions
-3
View File
@@ -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');
+3 -3
View File
@@ -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);
+2 -1
View File
@@ -25,7 +25,8 @@ abstract class BasicModel
}
}
protected function getLocalizedFile($filename) {
protected function getLocalizedFile($filename)
{
global $lang;
if (!$lang) {
$lang = DEFAULT_LOCALE;
+2 -1
View File
@@ -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"));
}
+8 -17
View File
@@ -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);
}
+33 -16
View File
@@ -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,
)
);
]);
}
}