forked from isaacrankin/silverstripe-social-feed
/
SocialFeedProvider.php
173 lines (151 loc) · 4.19 KB
/
SocialFeedProvider.php
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace IsaacRankin\SocialFeed\Providers;
use SilverStripe\ORM\DataObject;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\LiteralField;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\Exception;
use Psr\SimpleCache\CacheInterface;
use SilverStripe\Core\Injector\Injector;
use IsaacRankin\SocialFeed\SocialFeedCacheQueuedJob;
use Symbiote\QueuedJobs\Services\AbstractQueuedJob;
class SocialFeedProvider extends DataObject
{
private static $db = array(
'Label' => 'Varchar(100)',
'Enabled' => 'Boolean'
);
private static $summary_fields = array(
'Label',
'Enabled'
);
private static $table_name = 'SocialFeedProvider';
/**
* @return FieldList
*/
public function getCMSFields() {
if (Controller::has_curr())
{
if (isset($_GET['socialfeedclearcache']) && $_GET['socialfeedclearcache'] == 1 && $this->canEdit()) {
$this->clearFeedCache();
$url = Controller::curr()->getRequest()->getVar('url');
$urlAndParams = explode('?', $url);
Controller::curr()->redirect($urlAndParams[0]);
}
$this->beforeUpdateCMSFields(function($fields) {
$cache = $this->getFeedCache();
if ($cache !== null && $cache !== false) {
$url = Controller::curr()->getRequest()->getVar('url');
$url .= '?socialfeedclearcache=1';
$fields->addFieldToTab('Root.Main', LiteralField::create('cacheclear', '<a href="'.$url.'" class="field ss-ui-button ui-button" style="max-width: 100px;">Clear Cache</a>'));
}
});
}
$fields = parent::getCMSFields();
return $fields;
}
/**
* Get feed from provider, will automatically cache the result.
*
* If QueuedJobs module is installed, it will also create a job to update the cache
* 5 minutes before it expires.
*
* @return SS_List
*/
public function getFeed() {
$feed = $this->getFeedCache();
if (!$feed) {
$feed = $this->getFeedUncached();
$this->extend('updateFeedUncachedData', $feed);
$this->setFeedCache($feed);
if (class_exists(AbstractQueuedJob::class)) {
singleton(SocialFeedCacheQueuedJob::class)->createJob($this);
}
}
$data = array();
if ($feed) {
foreach ($feed as $post) {
$created = DBDatetime::create();
$timestamp = $this->getPostCreated($post);
if (!is_numeric($timestamp)) {
$timestamp = strtotime($this->getPostCreated($post));
}
$created->setValue($timestamp);
$data[] = array(
'Type' => $this->getType(),
'Content' => $this->getPostContent($post),
'Created' => $created,
'URL' => $this->getPostUrl($post),
'Data' => $post,
'UserName' => $this->getUserName($post),
'Image' => $this->getImage($post)
);
}
}
$result = ArrayList::create($data);
$result = $result->sort('Created', 'DESC');
return $result;
}
/**
* Retrieve the providers feed without checking the cache first.
* @throws Exception
*/
public function getFeedUncached() {
throw new Exception($this->class.' missing implementation for '.__FUNCTION__);
}
/**
* Get the providers feed from the cache. If there is no cache
* then return false.
*
* @return array
*/
public function getFeedCache() {
$cache = $this->getCacheFactory();
$feedStore = $cache->get($this->ID);
if (!$feedStore) {
return false;
}
$feed = unserialize($feedStore);
if (!$feed) {
return false;
}
return $feed;
}
/**
* Get the time() that the cache expires at.
*
* @return int
*/
public function getFeedCacheExpiry() {
$cache = $this->getCacheFactory();
$metadata = $cache->getMetadatas($this->ID);
if ($metadata && isset($metadata['expire'])) {
return $metadata['expire'];
}
return false;
}
/**
* Set the cache.
*/
public function setFeedCache(array $feed) {
$cache = $this->getCacheFactory();
$feedStore = serialize($feed);
$result = $cache->set($this->ID, $feedStore);
return $result;
}
/**
* Clear the cache that holds this providers feed.
*/
public function clearFeedCache() {
$cache = $this->getCacheFactory();
return $cache->remove($this->ID);
}
/**
* @return Zend_Cache_Frontend_Output
*/
public function getCacheFactory() {
$cache = Injector::inst()->get(CacheInterface::class . '.SocialFeedProvider');
return $cache;
}
}