[Scummvm-git-logs] scummvm-web master -> 018c78010abeeef9800184766bdf984e2cfb0f2a
Mataniko
mataniko at gmail.com
Sun Nov 18 13:47:58 CET 2018
This automated email contains information about 1 new commit which have been
pushed to the 'scummvm-web' repo located at https://github.com/scummvm/scummvm-web .
Summary:
018c78010a NEWS: Add support for news filenames in either yyyyMMdd or yyyyMMddHHmm format
Commit: 018c78010abeeef9800184766bdf984e2cfb0f2a
https://github.com/scummvm/scummvm-web/commit/018c78010abeeef9800184766bdf984e2cfb0f2a
Author: Matan Bareket (mataniko at gmail.com)
Date: 2018-11-18T07:46:22-05:00
Commit Message:
NEWS: Add support for news filenames in either yyyyMMdd or yyyyMMddHHmm format
This also changes the behavior that when a user clicks on a news
article, he will get all news articles from the same day. The reasoning
is a) we barely have more than 1 article a day. b) if a user wanted a
hotlink to a particular news article, he might get two different ones
which is odd.
Changed paths:
.htaccess
include/Models/NewsModel.php
include/Objects/News.php
include/Pages/NewsPage.php
diff --git a/.htaccess b/.htaccess
index 9f42817..4def23b 100644
--- a/.htaccess
+++ b/.htaccess
@@ -130,7 +130,7 @@ RewriteRule ^(feeds)/(atom|rss)/? ?p=$1&f=$2 [L,QSA]
##
# News
##
-RewriteRule ^(news)(/([0-9]{8}[a-z]?|archive))?/?$ ?p=$1&d=$3 [L,QSA]
+RewriteRule ^(news)(/([0-9]{8,12}[a-z]?|archive))?/?$ ?p=$1&d=$3 [L,QSA]
##
# Screenshots
diff --git a/include/Models/NewsModel.php b/include/Models/NewsModel.php
index 3de79b3..0e90c4c 100644
--- a/include/Models/NewsModel.php
+++ b/include/Models/NewsModel.php
@@ -6,23 +6,23 @@ require_once('Objects/News.php');
*/
abstract class NewsModel extends BasicModel {
const NO_FILES = 'No news files found.';
- const INVALID_DATE = 'Invalid date, use YYYYMMDD.';
+ const INVALID_DATE = 'Invalid date, use yyyyMMdd. or yyyyMMddHHmm';
const FILE_NOT_FOUND = 'The requested news file doesn\'t exist.';
- /* Get a list of all the available news dates. */
- static public function getListOfNewsDates() {
+ /* Get a list of all the available news files. */
+ static public function getListOfNewsFilenames() {
if (!($files = scandir(DIR_NEWS))) {
throw new ErrorException(self::NO_FILES);
}
- $dates = array();
+ $filenames = array();
foreach ($files as $file) {
if (substr($file, -5) != '.json') {
continue;
}
- $dates[] = substr($file, 0, -5);
+ $filenames[] = substr($file, 0, -5);
}
- sort($dates, SORT_STRING);
- return $dates;
+ sort($filenames, SORT_STRING);
+ return $filenames;
}
/* Get all news items ordered by date, descending. */
@@ -52,59 +52,33 @@ abstract class NewsModel extends BasicModel {
if ($num == -1) {
return NewsModel::getAllNews($processContent);
} else {
- if (!($newslist = NewsModel::getListOfNewsDates())) {
+ if (!($newslist = NewsModel::getListOfNewsFilenames())) {
throw new ErrorException(self::NO_FILES);
}
rsort($newslist, SORT_STRING);
$newslist = array_slice($newslist, 0, $num);
$news = array();
- foreach ($newslist as $date) {
- $news[] = NewsModel::getOneByDate($date, $processContent);
+ foreach ($newslist as $filename) {
+ $news[] = NewsModel::getOneByFilename($filename, $processContent);
}
return $news;
}
}
/* Get the news item that was posted on a specific date. */
- static public function getOneByDate($date, $processContent = false) {
- if (is_null($date) || !preg_match('/^\d{8}[a-z]?$/', $date)) {
+ static public function getOneByFilename($filename, $processContent = false) {
+ if (is_null($filename) || !preg_match('/^\d{8,12}[a-z]?$/', $filename)) {
throw new ErrorException(self::INVALID_DATE);
}
global $lang;
- if (!is_file(($fname = DIR_NEWS . "/$lang/{$date}.json"))
+ if (!is_file(($fname = DIR_NEWS . "/$lang/{$filename}.json"))
|| !is_readable($fname) || !($data = @file_get_contents($fname))) {
- if (!is_file(($fname = DIR_NEWS . "/{$date}.json"))
+ if (!is_file(($fname = DIR_NEWS . "/{$filename}.json"))
|| !is_readable($fname) || !($data = @file_get_contents($fname))) {
throw new ErrorException(self::FILE_NOT_FOUND);
}
}
return new News(json_decode($data), $fname, $processContent);
}
-
- /* Get the news item that was posted on a specific date. */
- static public function getAllByDate($date, $processContent = false) {
- if ($date == null || !is_numeric($date) || strlen($date) != 8) {
- throw new ErrorException(self::INVALID_DATE);
- }
- $files = glob(DIR_NEWS . "/{$date}*.json");
- if (!is_array($files) || count($files) == 0) {
- throw new ErrorException(self::FILE_NOT_FOUND);
- }
- natsort($files);
- global $lang;
- $files = array_reverse($files);
- $news = array();
- foreach ($files as $filename) {
- if (!is_file(($fname = DIR_NEWS . "/$lang/" . basename($filename)))
- || !is_readable($fname) || !($data = @file_get_contents($fname))) {
- if (($data = @file_get_contents($filename))) {
- $news[] = new News(json_decode($data), $filename, $processContent);
- }
- } else {
- $news[] = new News(json_decode($data), $fname, $processContent);
- }
- }
- return $news;
- }
}
?>
diff --git a/include/Objects/News.php b/include/Objects/News.php
index fc49f7e..430d4a3 100644
--- a/include/Objects/News.php
+++ b/include/Objects/News.php
@@ -12,19 +12,20 @@ class News extends BasicObject {
private $_filename;
/**
- * News object constructor that extracts the data from the pseudo-XML scheme
+ * News object constructor that extracts the data from the JSON scheme
* used. The format looks like this:
*
- * <NAME>Schwag</NAME>
- * <DATE>1st january, 1770</DATE>
- * <AUTHOR>Guybrush Threepwood</AUTHOR>
- * <BODY>All I got was this lousy t-shirt!</BODY>
+ * {
+ * "title": "Article Title",
+ * "content": "Your article content goes here",
+ * "date": "UNIX timestamp",
+ * "author": "Name"
+ * }
*
*
*/
- public function __construct($data, $filename, $processContent = false) {
+ public function __construct($data, $filename, $processContent = false) {
$this->_title = $processContent ? $this->processText($data->title) : $data->title;
- /* Store the date as an unix timestamp*/
$this->_date = $data->date;
$this->_author = $data->author;
$this->_content = $processContent ? $this->processText($data->content) : $data->content;
@@ -71,7 +72,7 @@ class News extends BasicObject {
/* Get the News link. */
public function getLink() {
- return URL_BASE . 'news/' . rtrim($this->_filename, "a..z.");
+ return URL_BASE . 'news/' . substr( $this->_filename, 0, -5);
}
}
?>
diff --git a/include/Pages/NewsPage.php b/include/Pages/NewsPage.php
index e0ff5ef..e29fa65 100644
--- a/include/Pages/NewsPage.php
+++ b/include/Pages/NewsPage.php
@@ -14,32 +14,28 @@ class NewsPage extends Controller {
/* Display the index page. */
public function index() {
- $date = isset($_GET['d']) ? $_GET['d'] : null;
+ $filename = isset($_GET['d']) ? $_GET['d'] : null;
- if ($date != null) {
- if (strtolower($date) == 'archive' || $date == '') {
- $date = null;
+ if ($filename != null) {
+ if (strtolower($filename) == 'archive' || $filename == '') {
+ $filename = null;
}
- return $this->getNews($date);
+ return $this->getNews($filename);
}
return $this->getNewsIntro();
}
/* Display a specific news item, or all news items. */
- public function getNews($date = null) {
+ public function getNews($filename = null) {
global $Smarty;
- if ($date == null) {
+ if ($filename == null) {
$news_items = NewsModel::getAllNews();
- $date = 'archive';
+ $filename = 'archive';
} else {
- if (strlen($date) == 8) {
- $news_items = NewsModel::getAllByDate($date);
- } else {
- $news_items = array(NewsModel::getOneByDate($date));
- }
+ $news_items = array(NewsModel::getOneByFilename($filename));
}
-
+
return $this->renderPage(
array(
'title' => $Smarty->getConfigVars('newsTitle'),
@@ -47,7 +43,6 @@ class NewsPage extends Controller {
'show_intro' => false,
'news_items' => $news_items,
'news_archive_link' => false,
- 'date' => $date,
),
$this->_template
);
More information about the Scummvm-git-logs
mailing list