From 024d632396def58c335944cae24371a10a3306b9 Mon Sep 17 00:00:00 2001 From: MaZderMind Date: Tue, 31 Mar 2015 23:17:01 +0200 Subject: Implement Relive based on the MVT Pattern --- config.php | 7 ++++++ index.php | 7 +----- model/Relive.php | 66 ++++++++++++++++++++++++++++++++++++++++++++++---- template/relive.phtml | 5 ++-- view/relive-player.php | 8 +++--- view/relive.php | 6 ++++- 6 files changed, 80 insertions(+), 19 deletions(-) diff --git a/config.php b/config.php index af427d7..7acfe4d 100644 --- a/config.php +++ b/config.php @@ -82,6 +82,13 @@ $GLOBALS['CONFIG']['CONFERENCE'] = array( * Wird beides auskommentiert, wird der Link nicht angezeigt */ 'RELIVE_JSON' => 'http://vod.c3voc.de/index.json', + + /** + * APCU-Cache-Zeit in Sekunden + * Wird diese Zeile auskommentiert, werden die apc_*-Methoden nicht verwendet und + * das Relive-Json bei jedem Request von der Quelle geladen und geparst + */ + 'RELIVE_JSON_CACHE' => 30*60, ); /** diff --git a/index.php b/index.php index 5292f82..0e5117f 100644 --- a/index.php +++ b/index.php @@ -15,6 +15,7 @@ require_once('model/Room.php'); require_once('model/RoomTab.php'); require_once('model/RoomSelection.php'); require_once('model/Stream.php'); +require_once('model/Relive.php'); $route = @$_GET['route']; $route = rtrim($route, '/'); @@ -61,9 +62,6 @@ try { else if(preg_match('@^relive/([0-9]+)$@', $route, $m)) { - if(!has('OVERVIEW.RELIVE_JSON')) - return require('view/404.php'); - $_GET = array( 'id' => $m[1], ); @@ -72,9 +70,6 @@ try { else if(preg_match('@^relive$@', $route, $m)) { - if(!has('OVERVIEW.RELIVE_JSON')) - return require('view/404.php'); - require('view/relive.php'); } diff --git a/model/Relive.php b/model/Relive.php index 3ffdc1b..a76761c 100644 --- a/model/Relive.php +++ b/model/Relive.php @@ -2,9 +2,18 @@ class Relive extends ModelBase { - function relive_talks() + public function isEnabled() { - $talks = @file_get_contents(get('OVERVIEW.RELIVE_JSON')); + // having CONFERENCE.RELIVE is not enough! + return $this->has('CONFERENCE.RELIVE_JSON'); + } + + public function getTalks() + { + if($talks_by_id = $this->getCached()) + return $talks_by_id; + + $talks = file_get_contents($this->get('CONFERENCE.RELIVE_JSON')); $talks = utf8_decode($talks); $talks = (array)json_decode($talks, true); @@ -14,11 +23,58 @@ class Relive extends ModelBase }); $talks_by_id = array(); - foreach ($talks as $value) + foreach ($talks as $talk) { - $talks_by_id[$value['id']] = $value; + if($talk['status'] == 'released') + $talk['url'] = $talk['release_url']; + else + $talk['url'] = 'relive/'.rawurlencode($talk['id']).'/'; + + $talks_by_id[$talk['id']] = $talk; } - return $talks_by_id; + return $this->doCache($talks_by_id); + } + + public function getTalk($id) + { + $talks = $this->getTalks(); + if(!isset($talks[$id])) + throw new NotFoundException('Relive-Talk id '.$id); + + return $talks[$id]; + } + + private function isCacheEnabled() + { + return $this->has('CONFERENCE.RELIVE_JSON_CACHE') && function_exists('apc_fetch') && function_exists('apc_store'); + } + + private function getCacheDuration() + { + return $this->get('CONFERENCE.RELIVE_JSON_CACHE', 60*10 /* 10 minutes */); + } + + private $localCache = null; + private function getCached() + { + if($this->localCache) + return $this->localCache; + + if(!$this->isCacheEnabled()) + return null; + + return apc_fetch('RELIVE.CACHE'); + } + + private function doCache($value) + { + $this->localCache = $value; + + if(!$this->isCacheEnabled()) + return $value; + + apc_store('RELIVE.CACHE', $value, $this->getCacheDuration()); + return $value; } } diff --git a/template/relive.phtml b/template/relive.phtml index b26dd20..94e4acf 100644 --- a/template/relive.phtml +++ b/template/relive.phtml @@ -21,20 +21,19 @@ -
- + <?=h($talk['title'])?> diff --git a/view/relive-player.php b/view/relive-player.php index bc8bb57..587286b 100644 --- a/view/relive-player.php +++ b/view/relive-player.php @@ -1,10 +1,10 @@ isEnabled()) + throw new NotFoundException('Internal Relive is disabled'); -if(!$talk) - return require('page/404.php'); +$talk = $relive->getTalk(intval($_GET['id'])); echo $tpl->render(array( 'page' => 'relive-player', diff --git a/view/relive.php b/view/relive.php index 0608423..686d61e 100644 --- a/view/relive.php +++ b/view/relive.php @@ -1,7 +1,11 @@ isEnabled()) + throw new NotFoundException('Internal Relive is disabled'); + echo $tpl->render(array( 'page' => 'relive', 'title' => 'Relive!', - 'talks' => relive_talks(), + 'talks' => $relive->getTalks(), )); -- cgit v1.2.3