1
|
|
|
<?php |
2
|
|
|
namespace Elgg\Cache; |
3
|
|
|
|
4
|
|
|
use Elgg\Config; |
5
|
|
|
use Elgg\ViewsService; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* WARNING: API IN FLUX. DO NOT USE DIRECTLY. |
10
|
|
|
* |
11
|
|
|
* @access private |
12
|
|
|
* |
13
|
|
|
* @since 1.10.0 |
14
|
|
|
*/ |
15
|
|
|
class SimpleCache { |
16
|
|
|
|
17
|
|
|
/** @var Config */ |
18
|
|
|
private $config; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructor |
22
|
|
|
* |
23
|
|
|
* @param Config $config Elgg's global configuration |
24
|
|
|
*/ |
25
|
151 |
|
public function __construct(Config $config) { |
26
|
151 |
|
$this->config = $config; |
27
|
151 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Registers a view to simple cache. |
31
|
|
|
* |
32
|
|
|
* Simple cache is a caching mechanism that saves the output of |
33
|
|
|
* a view and its extensions into a file. |
34
|
|
|
* |
35
|
|
|
* @warning Simple cached views must take no parameters and return |
36
|
|
|
* the same content no matter who is logged in. |
37
|
|
|
* |
38
|
|
|
* @param string $view_name View name |
39
|
|
|
* |
40
|
|
|
* @return void |
41
|
|
|
* @see elgg_get_simplecache_url() |
42
|
|
|
*/ |
43
|
37 |
|
function registerView($view_name) { |
|
|
|
|
44
|
37 |
|
$view_name = ViewsService::canonicalizeViewName($view_name); |
45
|
37 |
|
elgg_register_external_view($view_name, true); |
46
|
37 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the URL for the cached view. |
50
|
|
|
* |
51
|
|
|
* Recommended usage is to just pass the entire view name as the first and only arg: |
52
|
|
|
* |
53
|
|
|
* ``` |
54
|
|
|
* $blog_js = $simpleCache->getUrl('blog/save_draft.js'); |
55
|
|
|
* $favicon = $simpleCache->getUrl('graphics/favicon.ico'); |
56
|
|
|
* ``` |
57
|
|
|
* |
58
|
|
|
* For backwards compatibility with older versions of Elgg, you can also pass |
59
|
|
|
* "js" or "css" as the first arg, with the rest of the view name as the second arg: |
60
|
|
|
* |
61
|
|
|
* ``` |
62
|
|
|
* $blog_js = $simpleCache->getUrl('js', 'blog/save_draft.js'); |
63
|
|
|
* ``` |
64
|
|
|
* |
65
|
|
|
* This automatically registers the view with Elgg's simplecache. |
66
|
|
|
* |
67
|
|
|
* @param string $view The full view name |
68
|
|
|
* @param string $subview If the first arg is "css" or "js", the rest of the view name |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
37 |
|
function getUrl($view, $subview = '') { |
|
|
|
|
73
|
|
|
// handle `getUrl('js', 'js/blog/save_draft')` |
74
|
37 |
|
if (($view === 'js' || $view === 'css') && 0 === strpos($subview, $view . '/')) { |
75
|
|
|
$view = $subview; |
76
|
|
|
$subview = ''; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// handle `getUrl('js', 'blog/save_draft')` |
80
|
37 |
|
if (!empty($subview)) { |
81
|
|
|
$view = "$view/$subview"; |
82
|
|
|
} |
83
|
|
|
|
84
|
37 |
|
$view = ViewsService::canonicalizeViewName($view); |
85
|
|
|
|
86
|
|
|
// should be normalized to canonical form by now: `getUrl('blog/save_draft.js')` |
87
|
37 |
|
$this->registerView($view); |
88
|
37 |
|
return $this->getRoot() . $view; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the base url for simple cache requests |
93
|
|
|
* |
94
|
|
|
* @return string The simplecache root url for the current viewtype. |
95
|
|
|
* @access private |
96
|
|
|
*/ |
97
|
168 |
|
function getRoot() { |
|
|
|
|
98
|
168 |
|
$viewtype = elgg_get_viewtype(); |
99
|
168 |
|
if ($this->isEnabled()) { |
100
|
13 |
|
$lastcache = (int) $this->config->lastcache; |
101
|
|
|
} else { |
102
|
155 |
|
$lastcache = 0; |
103
|
|
|
} |
104
|
|
|
|
105
|
168 |
|
return elgg_normalize_url("/cache/$lastcache/$viewtype/"); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Is simple cache enabled |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
169 |
|
function isEnabled() { |
|
|
|
|
114
|
169 |
|
return (bool) $this->config->simplecache_enabled; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Enables the simple cache. |
119
|
|
|
* |
120
|
|
|
* @see elgg_register_simplecache_view() |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
1 |
|
function enable() { |
|
|
|
|
124
|
1 |
|
$this->config->save('simplecache_enabled', 1); |
125
|
1 |
|
$this->invalidate(); |
126
|
1 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Disables the simple cache. |
130
|
|
|
* |
131
|
|
|
* @warning Simplecache is also purged when disabled. |
132
|
|
|
* |
133
|
|
|
* @see elgg_register_simplecache_view() |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
1 |
|
function disable() { |
|
|
|
|
137
|
1 |
|
if ($this->config->simplecache_enabled) { |
138
|
|
|
$this->config->save('simplecache_enabled', 0); |
139
|
|
|
|
140
|
|
|
$this->invalidate(); |
141
|
|
|
} |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns the path to where views are simplecached. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
2 |
|
private function getPath() { |
150
|
2 |
|
$realpath = realpath($this->config->cacheroot); |
151
|
2 |
|
return rtrim($realpath, DIRECTORY_SEPARATOR) . "/views_simplecache"; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Deletes all cached views in the simplecache and sets the lastcache and |
156
|
|
|
* lastupdate time to 0 for every valid viewtype. |
157
|
|
|
* |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
2 |
|
function invalidate() { |
|
|
|
|
161
|
2 |
|
_elgg_rmdir($this->getPath(), true); |
162
|
|
|
|
163
|
2 |
|
$time = time(); |
164
|
2 |
|
$this->config->save("simplecache_lastupdate", $time); |
165
|
2 |
|
$this->config->lastcache = $time; |
166
|
|
|
|
167
|
2 |
|
return true; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Set up config appropriately on engine boot. |
172
|
|
|
* |
173
|
|
|
* @return void |
174
|
|
|
*/ |
175
|
18 |
|
function init() { |
|
|
|
|
176
|
18 |
|
$lastcache = $this->config->lastcache; |
177
|
18 |
|
if (!defined('UPGRADING') && empty($lastcache)) { |
178
|
5 |
|
$this->config->lastcache = (int) $this->config->simplecache_lastupdate; |
179
|
|
|
} |
180
|
18 |
|
} |
181
|
|
|
} |
182
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.