aboutsummaryrefslogtreecommitdiff
path: root/model/Relive.php
blob: a76761cdc5de87fd1e18aeca4f0666749dfc3f48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php

class Relive extends ModelBase
{
	public function isEnabled()
	{
		// 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);

		usort($talks, function($a, $b) {
			$sort = array('live', 'recorded', 'released');
			return array_search($a['status'], $sort) > array_search($b['status'], $sort);
		});

		$talks_by_id = array();
		foreach ($talks as $talk)
		{
			if($talk['status'] == 'released')
				$talk['url'] = $talk['release_url'];
			else
				$talk['url'] = 'relive/'.rawurlencode($talk['id']).'/';

			$talks_by_id[$talk['id']] = $talk;
		}

		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;
	}
}