Permalink
Browse files

feature(cache): allow admin to attempt an automatic symlink to cache

This adds an option to Advanced settings to attempt symlinking cache to
views_simplecache. The action makes an HTTP request to a simplecache view
and validates the 200 response. If response was different, removes the symlink
and advises to refer to documentation for manual symlinking.

Fixes #8639
Fixes #8638
  • Loading branch information...
hypeJunction committed Apr 13, 2016
1 parent eaee828 commit b06a1cb3e799d3863126bc279d202e25ed96aea9
@@ -45,6 +45,20 @@
elgg_disable_simplecache();
}
$cache_symlinked = _elgg_is_cache_symlinked();
if ('on' === get_input('cache_symlink_enabled') && !$cache_symlinked) {
if (!is_dir(elgg_get_root_path() . 'cache/')) {
$cache_symlinked = symlink(elgg_get_cache_path() . 'views_simplecache/', elgg_get_root_path() . 'cache/');
}
if (!_elgg_is_cache_symlinked()) {
unlink(elgg_get_root_path() . 'cache/');
$cache_symlinked = false;
}
if (!$cache_symlinked) {
register_error(elgg_echo('installation:cache_symlink:error'));
}
}
set_config('simplecache_minify_js', 'on' === get_input('simplecache_minify_js'), $site->getGUID());
set_config('simplecache_minify_css', 'on' === get_input('simplecache_minify_css'), $site->getGUID());
View
@@ -226,6 +226,20 @@ function elgg_flush_caches() {
_elgg_services()->events->trigger('cache:flush', 'system');
}
/**
* Checks if /cache has been symlinked to views simplecache directory,
* and if HTTP requests to /cache are are successful
* @return bool
* @access private
*/
function _elgg_is_cache_symlinked() {
if (!is_dir(elgg_get_root_path() . 'cache/')) {
return false;
}
$headers = get_headers(elgg_get_simplecache_url('elgg.js'));
return substr($headers[0], 9, 3) == 200;
}
/**
* Initializes the simplecache lastcache variable and creates system cache files
* when appropriate.
View
@@ -1109,6 +1109,11 @@
'installation:simplecache:description' => "The simple cache increases performance by caching static content including some CSS and JavaScript files.",
'installation:simplecache:label' => "Use simple cache (recommended)",
'installation:cache_symlink:description' => "The symbolic link to the simple cache directory allows the server to serve static views bypassing the engine, which considerably improves performance and reduces the server load",
'installation:cache_symlink:label' => "Use symbolic link to simple cache directory (recommended)",
'installation:cache_symlink:warning' => "Symbolic link has been established. If, for some reason, you want to remove the link, delete the symbolic link directory from your server",
'installation:cache_symlink:error' => "Due to your server configuration the symbolic link can not be established automatically. Please refer to the documentation and establish the symbolic link manually.",
'installation:minify:description' => "The simple cache can also improve performance by compressing JavaScript and CSS files. (Requires that simple cache is enabled.)",
'installation:minify_js:label' => "Compress JavaScript (recommended)",
'installation:minify_css:label' => "Compress CSS (recommended)",
View
@@ -168,4 +168,4 @@ function profile_pagesetup() {
'link_class' => 'elgg-topbar-avatar',
'item_class' => 'elgg-avatar elgg-avatar-topbar',
));
}
}
@@ -226,7 +226,7 @@ define(function(require) {
function simplecacheToggle () {
// when the checkbox is disabled, do not toggle the compression checkboxes
if (!$(this).hasClass('elgg-state-disabled')) {
var names = ['simplecache_minify_js', 'simplecache_minify_css'];
var names = ['simplecache_minify_js', 'simplecache_minify_css', 'cache_symlink_enabled'];
for (var i = 0; i < names.length; i++) {
var $input = $('input[type!=hidden][name="' + names[i] + '"]');
if ($input.length) {
@@ -24,6 +24,24 @@
$simple_cache_warning .= "<span class=\"elgg-text-help\">$warning</span>";
}
$cache_symlinked = _elgg_is_cache_symlinked();
$params = array(
'label' => elgg_echo('installation:cache_symlink:label'),
'name' => 'cache_symlink_enabled',
'checked' => $cache_symlinked,
'class' => $simple_cache_disabled_class,
'label_class' => $simple_cache_disabled_class,
);
$symlink_warning = '';
if ($cache_symlinked) {
$params['class'] = 'elgg-state-disabled';
$params['label_class'] = 'elgg-state-disabled';
}
$symlink_input .= elgg_view("input/checkbox", $params);
if ($cache_symlinked) {
$symlink_warning .= elgg_format_element('span', ['class' => 'elgg-text-help'], elgg_echo('installation:cache_symlink:warning'));
}
// minify
$minify_js_input = elgg_view("input/checkbox", array(
'label' => elgg_echo('installation:minify_js:label'),
@@ -53,9 +71,12 @@
<?php echo $simple_cache_input; ?>
<p class="elgg-text-help"><?php echo elgg_echo('installation:simplecache:description'); ?></p>
<?php echo $simple_cache_warning; ?>
</div>
<div>
<?php echo $symlink_input; ?>
<p class="elgg-text-help"><?php echo elgg_echo('installation:cache_symlink:description'); ?></p>
<?php echo $symlink_warning; ?>
</div>
<div>
<p><?php echo elgg_echo('installation:minify:description'); ?></p>
<?php echo $minify_js_input; ?><br />

0 comments on commit b06a1cb

Please sign in to comment.