|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Elgg cache |
|
4
|
|
|
* Cache file interface for caching data. |
|
5
|
|
|
* |
|
6
|
|
|
* @package Elgg.Core |
|
7
|
|
|
* @subpackage Cache |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Returns an \ElggCache object suitable for caching system information |
|
12
|
|
|
* |
|
13
|
|
|
* @return ElggCache |
|
14
|
|
|
*/ |
|
15
|
|
|
function elgg_get_system_cache() { |
|
16
|
1 |
|
return _elgg_services()->fileCache; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Reset the system cache by deleting the caches |
|
21
|
|
|
* |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
function elgg_reset_system_cache() { |
|
25
|
4 |
|
_elgg_services()->systemCache->reset(); |
|
26
|
4 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Saves a system cache. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $type The type or identifier of the cache |
|
32
|
|
|
* @param string $data The data to be saved |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
|
|
function elgg_save_system_cache($type, $data) { |
|
36
|
2 |
|
return _elgg_services()->systemCache->save($type, $data); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Retrieve the contents of a system cache. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $type The type of cache to load |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
function elgg_load_system_cache($type) { |
|
46
|
17 |
|
return _elgg_services()->systemCache->load($type); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Is system cache enabled |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
* @since 2.2.0 |
|
54
|
|
|
*/ |
|
55
|
|
|
function elgg_is_system_cache_enabled() { |
|
56
|
4778 |
|
return _elgg_services()->systemCache->isEnabled(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Enables the system disk cache. |
|
61
|
|
|
* |
|
62
|
|
|
* Uses the 'system_cache_enabled' config with a boolean value. |
|
63
|
|
|
* Resets the system cache. |
|
64
|
|
|
* |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
|
function elgg_enable_system_cache() { |
|
68
|
4 |
|
_elgg_services()->systemCache->enable(); |
|
69
|
4 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Disables the system disk cache. |
|
73
|
|
|
* |
|
74
|
|
|
* Uses the 'system_cache_enabled' config with a boolean value. |
|
75
|
|
|
* Resets the system cache. |
|
76
|
|
|
* |
|
77
|
|
|
* @return void |
|
78
|
|
|
*/ |
|
79
|
|
|
function elgg_disable_system_cache() { |
|
80
|
2 |
|
_elgg_services()->systemCache->disable(); |
|
81
|
2 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/* Simplecache */ |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Registers a view to simple cache. |
|
87
|
|
|
* |
|
88
|
|
|
* Simple cache is a caching mechanism that saves the output of |
|
89
|
|
|
* a view and its extensions into a file. |
|
90
|
|
|
* |
|
91
|
|
|
* @warning Simple cached views must take no parameters and return |
|
92
|
|
|
* the same content no matter who is logged in. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $view_name View name |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
* @see elgg_get_simplecache_url() |
|
98
|
|
|
* @since 1.8.0 |
|
99
|
|
|
*/ |
|
100
|
|
|
function elgg_register_simplecache_view($view_name) { |
|
101
|
31 |
|
_elgg_services()->simpleCache->registerView($view_name); |
|
102
|
31 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the URL for the cached view. |
|
106
|
|
|
* |
|
107
|
|
|
* Recommended usage is to just pass the entire view name as the first and only arg: |
|
108
|
|
|
* |
|
109
|
|
|
* ``` |
|
110
|
|
|
* $blog_js = elgg_get_simplecache_url('elgg/blog/save_draft.js'); |
|
111
|
|
|
* $favicon = elgg_get_simplecache_url('favicon.ico'); |
|
112
|
|
|
* ``` |
|
113
|
|
|
* |
|
114
|
|
|
* For backwards compatibility with older versions of Elgg, this function supports |
|
115
|
|
|
* "js" or "css" as the first arg, with the rest of the view name as the second arg: |
|
116
|
|
|
* |
|
117
|
|
|
* ``` |
|
118
|
|
|
* $blog_js = elgg_get_simplecache_url('js', 'elgg/blog/save_draft.js'); |
|
119
|
|
|
* ``` |
|
120
|
|
|
* |
|
121
|
|
|
* This automatically registers the view with Elgg's simplecache. |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $view The full view name |
|
124
|
|
|
* @param string $subview If the first arg is "css" or "js", the rest of the view name |
|
125
|
|
|
* @return string |
|
126
|
|
|
* @since 1.8.0 |
|
127
|
|
|
*/ |
|
128
|
|
|
function elgg_get_simplecache_url($view, $subview = '') { |
|
129
|
37 |
|
return _elgg_services()->simpleCache->getUrl($view, $subview); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Is simple cache enabled |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
* @since 1.8.0 |
|
137
|
|
|
*/ |
|
138
|
|
|
function elgg_is_simplecache_enabled() { |
|
139
|
1 |
|
return _elgg_services()->simpleCache->isEnabled(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Enables the simple cache. |
|
144
|
|
|
* |
|
145
|
|
|
* @see elgg_register_simplecache_view() |
|
146
|
|
|
* @return void |
|
147
|
|
|
* @since 1.8.0 |
|
148
|
|
|
*/ |
|
149
|
|
|
function elgg_enable_simplecache() { |
|
150
|
1 |
|
_elgg_services()->simpleCache->enable(); |
|
151
|
1 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Disables the simple cache. |
|
155
|
|
|
* |
|
156
|
|
|
* @warning Simplecache is also purged when disabled. |
|
157
|
|
|
* |
|
158
|
|
|
* @see elgg_register_simplecache_view() |
|
159
|
|
|
* @return void |
|
160
|
|
|
* @since 1.8.0 |
|
161
|
|
|
*/ |
|
162
|
|
|
function elgg_disable_simplecache() { |
|
163
|
1 |
|
_elgg_services()->simpleCache->disable(); |
|
164
|
1 |
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Recursively deletes a directory, including all hidden files. |
|
168
|
|
|
* |
|
169
|
|
|
* TODO(ewinslow): Move to filesystem package |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $dir The directory |
|
172
|
|
|
* @param bool $empty If true, we just empty the directory |
|
173
|
|
|
* |
|
174
|
|
|
* @return boolean Whether the dir was successfully deleted. |
|
175
|
|
|
* @access private |
|
176
|
|
|
*/ |
|
177
|
|
|
function _elgg_rmdir($dir, $empty = false) { |
|
178
|
52 |
|
if (!$dir) { |
|
179
|
|
|
// realpath can return false |
|
180
|
|
|
_elgg_services()->logger->warn(__FUNCTION__ . ' called with empty $dir'); |
|
181
|
|
|
return true; |
|
182
|
|
|
} |
|
183
|
52 |
|
if (!is_dir($dir)) { |
|
184
|
2 |
|
return true; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
51 |
|
$files = array_diff(scandir($dir), ['.', '..']); |
|
188
|
|
|
|
|
189
|
51 |
|
foreach ($files as $file) { |
|
190
|
49 |
|
if (is_dir("$dir/$file")) { |
|
191
|
46 |
|
_elgg_rmdir("$dir/$file"); |
|
192
|
|
|
} else { |
|
193
|
49 |
|
unlink("$dir/$file"); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
51 |
|
if ($empty) { |
|
198
|
1 |
|
return true; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
50 |
|
return rmdir($dir); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Deletes all cached views in the simplecache and sets the lastcache and |
|
206
|
|
|
* lastupdate time to 0 for every valid viewtype. |
|
207
|
|
|
* |
|
208
|
|
|
* @return bool |
|
209
|
|
|
* @since 1.7.4 |
|
210
|
|
|
*/ |
|
211
|
|
|
function elgg_invalidate_simplecache() { |
|
212
|
1 |
|
_elgg_services()->simpleCache->invalidate(); |
|
213
|
1 |
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Flush all the registered caches |
|
217
|
|
|
* |
|
218
|
|
|
* @return void |
|
219
|
|
|
* @since 1.11 |
|
220
|
|
|
*/ |
|
221
|
|
|
function elgg_flush_caches() { |
|
222
|
1 |
|
_elgg_services()->hooks->getEvents()->trigger('cache:flush', 'system'); |
|
223
|
1 |
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Checks if /cache directory has been symlinked to views simplecache directory |
|
227
|
|
|
* |
|
228
|
|
|
* @return bool |
|
229
|
|
|
* @access private |
|
230
|
|
|
*/ |
|
231
|
|
|
function _elgg_is_cache_symlinked() { |
|
232
|
1 |
|
$root_path = elgg_get_root_path(); |
|
233
|
1 |
|
$cache_path = elgg_get_cache_path(); |
|
234
|
|
|
|
|
235
|
1 |
|
$simplecache_path = "{$cache_path}views_simplecache"; |
|
236
|
1 |
|
$symlink_path = "{$root_path}cache"; |
|
237
|
|
|
|
|
238
|
1 |
|
if (!is_dir($simplecache_path)) { |
|
239
|
1 |
|
return false; |
|
240
|
|
|
} |
|
241
|
1 |
|
return is_dir($symlink_path) && realpath($simplecache_path) == realpath($symlink_path); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Symlinks /cache directory to views simplecache directory |
|
246
|
|
|
* |
|
247
|
|
|
* @return bool |
|
248
|
|
|
* @access private |
|
249
|
|
|
*/ |
|
250
|
|
|
function _elgg_symlink_cache() { |
|
251
|
|
|
|
|
252
|
1 |
|
if (_elgg_is_cache_symlinked()) { |
|
253
|
|
|
// Symlink exists, no need to proceed |
|
254
|
|
|
return true; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
1 |
|
$root_path = elgg_get_root_path(); |
|
258
|
1 |
|
$cache_path = elgg_get_cache_path(); |
|
259
|
|
|
|
|
260
|
1 |
|
$simplecache_path = "{$cache_path}views_simplecache"; |
|
261
|
1 |
|
$symlink_path = "{$root_path}cache"; |
|
262
|
|
|
|
|
263
|
1 |
|
if (is_dir($symlink_path)) { |
|
264
|
|
|
// Cache directory already exists |
|
265
|
|
|
// We can not proceed without overwriting files |
|
266
|
|
|
return false; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
1 |
|
if (!is_dir($simplecache_path)) { |
|
270
|
|
|
// Views simplecache directory has not yet been created |
|
271
|
1 |
|
mkdir($simplecache_path, 0700, true); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
1 |
|
symlink($simplecache_path, $symlink_path); |
|
275
|
|
|
|
|
276
|
1 |
|
if (_elgg_is_cache_symlinked()) { |
|
277
|
1 |
|
return true; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
if (is_dir($symlink_path)) { |
|
281
|
|
|
unlink($symlink_path); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
return false; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Initializes the simplecache lastcache variable and creates system cache files |
|
289
|
|
|
* when appropriate. |
|
290
|
|
|
* |
|
291
|
|
|
* @return void |
|
292
|
|
|
* |
|
293
|
|
|
* @access private |
|
294
|
|
|
*/ |
|
295
|
|
|
function _elgg_cache_init() { |
|
296
|
18 |
|
_elgg_services()->simpleCache->init(); |
|
297
|
18 |
|
_elgg_services()->systemCache->init(); |
|
298
|
18 |
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* @see \Elgg\Application::loadCore Do not do work here. Just register for events. |
|
302
|
|
|
*/ |
|
303
|
|
|
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) { |
|
|
|
|
|
|
304
|
18 |
|
$events->registerHandler('ready', 'system', '_elgg_cache_init'); |
|
305
|
|
|
|
|
306
|
|
|
// register plugin hooks for cache reset |
|
307
|
18 |
|
$events->registerHandler('cache:flush', 'system', 'elgg_reset_system_cache'); |
|
308
|
18 |
|
$events->registerHandler('cache:flush', 'system', 'elgg_invalidate_simplecache'); |
|
309
|
|
|
}; |
|
310
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.