From bcf7f9f36f37c10cdb8737ea88cce365522e37cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20=C5=A0koda?= Date: Sat, 30 Jun 2012 09:50:18 +0200 Subject: [PATCH] MDL-34057 improve styles_debug concurrency Please note style debugging is not intended for busy production sites! --- lib/outputlib.php | 48 +++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/outputlib.php b/lib/outputlib.php index 7d963f30198..0cf29d8fef8 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -626,30 +626,38 @@ class theme_config { if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) { define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php } - // Prepare the CSS optimiser if it is to be used - $optimiser = null; - $candidatesheet = "$CFG->cachedir/theme/$this->name/designer.ser"; - if (!empty($CFG->enablecssoptimiser) && $this->supportscssoptimisation) { - require_once($CFG->dirroot.'/lib/csslib.php'); - $optimiser = new css_optimiser; - } - if (!file_exists($candidatesheet)) { - $css = $this->css_content($optimiser); - check_dir_exists(dirname($candidatesheet)); - file_put_contents($candidatesheet, serialize($css)); - - } else if (filemtime($candidatesheet) > time() - THEME_DESIGNER_CACHE_LIFETIME) { + $candidatedir = "$CFG->cachedir/theme/$this->name"; + $candidatesheet = "$candidatedir/designer.ser"; + $rebuild = true; + if (file_exists($candidatesheet) and filemtime($candidatesheet) > time() - THEME_DESIGNER_CACHE_LIFETIME) { if ($css = file_get_contents($candidatesheet)) { $css = unserialize($css); - } else { - unlink($candidatesheet); - $css = $this->css_content($optimiser); + if (is_array($css)) { + $rebuild = false; + } + } + } + if ($rebuild) { + // Prepare the CSS optimiser if it is to be used, + // please note that it may be very slow and is therefore strongly discouraged in theme designer mode. + $optimiser = null; + if (!empty($CFG->enablecssoptimiser) && $this->supportscssoptimisation) { + require_once($CFG->dirroot.'/lib/csslib.php'); + $optimiser = new css_optimiser; } - - } else { - unlink($candidatesheet); $css = $this->css_content($optimiser); - file_put_contents($candidatesheet, serialize($css)); + + // We do not want any errors here because this may fail easily because of the concurrent access. + $prevabort = ignore_user_abort(true); + check_dir_exists($candidatedir); + $tempfile = tempnam($candidatedir, 'tmpdesigner'); + file_put_contents($tempfile, serialize($css)); + $reporting = error_reporting(0); + chmod($tempfile, $CFG->filepermissions); + unlink($candidatesheet); // Do not rely on rename() deleting original, they may decide to change it at any time as usually. + rename($tempfile, $candidatesheet); + error_reporting($reporting); + ignore_user_abort($prevabort); } $baseurl = $CFG->httpswwwroot.'/theme/styles_debug.php';