From fda57f3b7c52ea9747e8c132c4571772ab913752 Mon Sep 17 00:00:00 2001 From: Romain d'Alverny Date: Sat, 15 Jan 2022 16:41:25 +0100 Subject: Refactor main controller and templates Templates for the public part: - archive/default views are merged into a single set - becomes HTML5 Controller code is reorganized for clarity. Compiled template caching is also fixed and activated when config cache value is > 0. Caching is also given new default larger values. --- app/classes/Planet.php | 13 ++++ app/classes/PlanetConfig.php | 7 +-- atom.php | 1 - custom/views/archive/footer.tpl.php | 3 - custom/views/archive/head.tpl.php | 6 -- custom/views/archive/index.tpl.php | 114 ----------------------------------- custom/views/archive/sidebar.tpl.php | 34 ----------- custom/views/archive/top.tpl.php | 4 -- custom/views/default/archive.tpl.php | 105 ++++++++++++++++++++++++++++++++ custom/views/default/footer.tpl.php | 8 +-- custom/views/default/head.tpl.php | 9 ++- custom/views/default/index.tpl.php | 19 +----- custom/views/default/top.tpl.php | 11 +++- index.php | 38 ++++-------- tests/InstallTest.php | 2 +- 15 files changed, 157 insertions(+), 217 deletions(-) delete mode 100755 custom/views/archive/footer.tpl.php delete mode 100644 custom/views/archive/head.tpl.php delete mode 100755 custom/views/archive/index.tpl.php delete mode 100755 custom/views/archive/sidebar.tpl.php delete mode 100644 custom/views/archive/top.tpl.php create mode 100644 custom/views/default/archive.tpl.php diff --git a/app/classes/Planet.php b/app/classes/Planet.php index 402d2d0..1555c25 100644 --- a/app/classes/Planet.php +++ b/app/classes/Planet.php @@ -80,6 +80,19 @@ class Planet return hash_equals($known, $supplied); } + /** + * @return array $items + */ + public function getFeedsItems() + { + $items = []; + if (0 < $this->loadOpml($this->config->getOpmlFile())) { + $this->loadFeeds(); + $items = $this->getItems(); + } + return $items; + } + /** * Getters */ diff --git a/app/classes/PlanetConfig.php b/app/classes/PlanetConfig.php index a13179b..b8233e0 100644 --- a/app/classes/PlanetConfig.php +++ b/app/classes/PlanetConfig.php @@ -16,11 +16,8 @@ class PlanetConfig 'name' => '', 'locale' => 'en', 'items' => 10, - 'shuffle' => 0, - 'refresh' => 240, - 'cache' => 10, - 'nohtml' => 0, - 'postmaxlength' => 0, + 'refresh' => 3600, + 'cache' => 1800, 'categories' => '', 'cachedir' => './cache', 'debug' => false, diff --git a/atom.php b/atom.php index 3b759e9..ddeebc0 100644 --- a/atom.php +++ b/atom.php @@ -24,7 +24,6 @@ echo ''; getName())?> - <?=htmlspecialchars($item->get_feed()->getName())?> : <?=htmlspecialchars($item->get_title())?> get_id())?> diff --git a/custom/views/archive/footer.tpl.php b/custom/views/archive/footer.tpl.php deleted file mode 100755 index c9c84b1..0000000 --- a/custom/views/archive/footer.tpl.php +++ /dev/null @@ -1,3 +0,0 @@ - diff --git a/custom/views/archive/head.tpl.php b/custom/views/archive/head.tpl.php deleted file mode 100644 index 005a75d..0000000 --- a/custom/views/archive/head.tpl.php +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - diff --git a/custom/views/archive/index.tpl.php b/custom/views/archive/index.tpl.php deleted file mode 100755 index 679ef71..0000000 --- a/custom/views/archive/index.tpl.php +++ /dev/null @@ -1,114 +0,0 @@ -get_date('U')) / (60*60*24); - if ($age < 1) { - $today[] = $item; - } elseif ($age < 7) { - $week[] = $item; - } elseif ($age < 30) { - $month[] = $item; - } else { - $older[] = $item; - } -} - -header('Content-type: text/html; charset=UTF-8'); -?> - - - - - - - <?php echo $PlanetConfig->getName(); ?> - - - - -
- - -
- -
-

- -

-

-
- - -
-

- -
- - - -
-

- -
- - - -
-

- -
- - - -
-

- -
- -
- - - - -
- - diff --git a/custom/views/archive/sidebar.tpl.php b/custom/views/archive/sidebar.tpl.php deleted file mode 100755 index 7a5d080..0000000 --- a/custom/views/archive/sidebar.tpl.php +++ /dev/null @@ -1,34 +0,0 @@ -getPeople(); -usort($all_people, array('PlanetFeed', 'compare')); -?> - diff --git a/custom/views/archive/top.tpl.php b/custom/views/archive/top.tpl.php deleted file mode 100644 index 7a818e1..0000000 --- a/custom/views/archive/top.tpl.php +++ /dev/null @@ -1,4 +0,0 @@ - - \ No newline at end of file diff --git a/custom/views/default/archive.tpl.php b/custom/views/default/archive.tpl.php new file mode 100644 index 0000000..f775971 --- /dev/null +++ b/custom/views/default/archive.tpl.php @@ -0,0 +1,105 @@ +getName() . ' · ' . _g('All Headlines'); +$count = 0; +$today = array(); +$week = array(); +$month = array(); +$older = array(); +$now = time(); + +foreach ($items as $item) { + $age = ($now - $item->get_date('U')) / (60*60*24); + if ($age < 1) { + $today[] = $item; + } elseif ($age < 7) { + $week[] = $item; + } elseif ($age < 30) { + $month[] = $item; + } else { + $older[] = $item; + } +} + +header('Content-type: text/html; charset=UTF-8'); +?> + + + + + +
+ +
+ +
+

+ +

+

+
+ + + + + + +
+

+ +
+ + + +
+

+ +
+ + + +
+

+ +
+ +
+ + +
+ + diff --git a/custom/views/default/footer.tpl.php b/custom/views/default/footer.tpl.php index d7d6c1a..eeff62b 100755 --- a/custom/views/default/footer.tpl.php +++ b/custom/views/default/footer.tpl.php @@ -1,4 +1,4 @@ - +
+

moonmoon'))?> + |

+
diff --git a/custom/views/default/head.tpl.php b/custom/views/default/head.tpl.php index 5773aa6..dbaf65c 100644 --- a/custom/views/default/head.tpl.php +++ b/custom/views/default/head.tpl.php @@ -1,3 +1,10 @@ + + + <?php echo $pageTitle; ?> - + + \ No newline at end of file diff --git a/custom/views/default/index.tpl.php b/custom/views/default/index.tpl.php index 5e68161..cad2a6f 100644 --- a/custom/views/default/index.tpl.php +++ b/custom/views/default/index.tpl.php @@ -1,26 +1,15 @@ getName(); $limit = $PlanetConfig->getMaxDisplay(); $count = 0; header('Content-type: text/html; charset=UTF-8'); -?> - +?> + - - - - - - - <?php echo $PlanetConfig->getName(); ?> - -
@@ -67,9 +56,7 @@ header('Content-type: text/html; charset=UTF-8');
- - diff --git a/custom/views/default/top.tpl.php b/custom/views/default/top.tpl.php index 6db425c..ed6e19c 100644 --- a/custom/views/default/top.tpl.php +++ b/custom/views/default/top.tpl.php @@ -1,3 +1,8 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/index.php b/index.php index 75c1bc6..0d149bd 100755 --- a/index.php +++ b/index.php @@ -2,28 +2,17 @@ include_once(__DIR__.'/app/app.php'); include_once(__DIR__.'/app/classes/Cache.php'); -//Installed ? if (!$PlanetConfig::isInstalled()) { - echo '

' . _g('You might want to install moonmoon.') . '

'; - exit; + die('

' . _g('You might want to install moonmoon.') . '

'); } -//Load from cache -$items = array(); -if (0 < $Planet->loadOpml($PlanetConfig->getOpmlFile())) { - $Planet->loadFeeds(); - $items = $Planet->getItems(); +$pageRole = $_GET['type'] ?? 'index'; +$pageTheme = 'default'; +if (!in_array($pageRole, ['index', 'archive', 'atom10'])) { + $pageRole = 'index'; } -//Prepare output cache -Cache::$enabled = false; -$cache_key = (count($items)) ? $items[0]->get_id() : ''; -$last_modified = (count($items)) ? $items[0]->get_date() : ''; -$cache_duration = $PlanetConfig->getOutputTimeout() * 60; - -Cache::setStore($PlanetConfig->getCacheDir()); - -if (isset($_GET['type']) && $_GET['type'] == 'atom10') { +if ($pageRole == 'atom10') { /* XXX: Redirect old ATOM feeds to new url to make sure our users don't * loose subscribers upon upgrading their moonmoon installation. * Remove this check in a more distant future. @@ -33,15 +22,14 @@ if (isset($_GET['type']) && $_GET['type'] == 'atom10') { exit; } -//Go display -if (!isset($_GET['type']) || - !is_file(__DIR__.'/custom/views/'.$_GET['type'].'/index.tpl.php') || - strpos($_GET['type'], DIRECTORY_SEPARATOR) || strpos($_GET['type'], '..')) { - $_GET['type'] = 'default'; -} +$cache_duration = $PlanetConfig->getOutputTimeout(); +Cache::$enabled = ($cache_duration > 0); +Cache::setStore($PlanetConfig->getCacheDir() . '/'); -if (!OutputCache::Start($_GET['type'], $cache_key, $cache_duration)) { - include_once(__DIR__.'/custom/views/'.$_GET['type'].'/index.tpl.php'); +if (!OutputCache::Start('html', $pageRole, $cache_duration)) { + $items = $Planet->getFeedsItems(); + $last_modified = (count($items)) ? $items[0]->get_date() : ''; + include_once(__DIR__.'/custom/views/'.$pageTheme.'/'.$pageRole.'.tpl.php'); OutputCache::End(); } diff --git a/tests/InstallTest.php b/tests/InstallTest.php index fe5dc4e..b230d74 100644 --- a/tests/InstallTest.php +++ b/tests/InstallTest.php @@ -48,7 +48,7 @@ class InstallTest extends GuzzleHarness { $data = [ 'url' => 'http://127.0.0.1:8081/', - 'title' => 'My website', + 'title' => 'My website', 'password' => 'admin', 'locale' => 'en', ]; -- cgit v1.2.1