MDL-32683 fine tune yui resource caching
[moodle.git] / theme / javascript.php
CommitLineData
38aafea2
PS
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * This file is responsible for serving the one huge CSS of each theme.
20 *
21 * @package moodlecore
22 * @copyright 2009 Petr Skoda (skodak) {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
e68c5f36 26
c198390a
PS
27// disable moodle specific debug messages and any errors in output,
28// comment out when debugging or better look into error log!
29define('NO_DEBUG_DISPLAY', true);
30
e68c5f36
PS
31// we need just the values from config.php and minlib.php
32define('ABORT_AFTER_CONFIG', true);
33require('../config.php'); // this stops immediately at the beginning of lib/setup.php
34
ecbad2ad
PS
35if ($slashargument = min_get_slash_argument()) {
36 $slashargument = ltrim($slashargument, '/');
37 if (substr_count($slashargument, '/') < 2) {
38 image_not_found();
39 }
40 // image must be last because it may contain "/"
41 list($themename, $rev, $type) = explode('/', $slashargument, 3);
42 $themename = min_clean_param($themename, 'SAFEDIR');
43 $rev = min_clean_param($rev, 'INT');
44 $type = min_clean_param($type, 'SAFEDIR');
45
46} else {
47 $themename = min_optional_param('theme', 'standard', 'SAFEDIR');
48 $rev = min_optional_param('rev', 0, 'INT');
49 $type = min_optional_param('type', 'head', 'RAW');
50}
04c01408 51
baeb20bb 52if ($type !== 'head' and $type !== 'footer') {
04c01408
PS
53 header('HTTP/1.0 404 not found');
54 die('Theme was not found, sorry.');
55}
56
73e504bc
PS
57if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
58 // exists
59} else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
60 // exists
61} else {
e68c5f36
PS
62 header('HTTP/1.0 404 not found');
63 die('Theme was not found, sorry.');
64}
65
365bec4c 66$candidate = "$CFG->cachedir/theme/$themename/javascript_$type.js";
8475b970 67$etag = sha1("$themename/$rev/$type");
e68c5f36
PS
68
69if ($rev > -1 and file_exists($candidate)) {
aa603b14 70 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
e68c5f36
PS
71 // we do not actually need to verify the etag value because our files
72 // never change in cache because we increment the rev parameter
8475b970 73 $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
aa603b14 74 header('HTTP/1.1 304 Not Modified');
ccc0fff9 75 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
8c672cf9 76 header('Cache-Control: public, max-age='.$lifetime);
ccc0fff9 77 header('Content-Type: application/javascript; charset=utf-8');
8475b970 78 header('Etag: '.$etag);
e68c5f36
PS
79 die;
80 }
8475b970 81 send_cached_js($candidate, $etag);
e68c5f36
PS
82}
83
84//=================================================================================
85// ok, now we need to start normal moodle script, we need to load all libs and $DB
86define('ABORT_AFTER_CONFIG_CANCEL', true);
87
88define('NO_MOODLE_COOKIES', true); // Session not used here
89define('NO_UPGRADE_CHECK', true); // Ignore upgrade check
90
91require("$CFG->dirroot/lib/setup.php");
045f492c
SH
92// setup include path
93set_include_path($CFG->libdir . '/minify/lib' . PATH_SEPARATOR . get_include_path());
94require_once('Minify.php');
e68c5f36
PS
95
96$theme = theme_config::load($themename);
97
8475b970
PS
98$rev = theme_get_revision();
99$etag = sha1("$themename/$rev/$type");
100
e68c5f36 101if ($rev > -1) {
e164a0df
PS
102 // note: cache reset might have purged our cache dir structure,
103 // make sure we do not use stale file stat cache in the next check_dir_exists()
104 clearstatcache();
c426ef3a 105 check_dir_exists(dirname($candidate));
e68c5f36 106 $fp = fopen($candidate, 'w');
045f492c 107 fwrite($fp, minify($theme->javascript_files($type)));
e68c5f36 108 fclose($fp);
8475b970 109 send_cached_js($candidate, $etag);
e68c5f36 110} else {
045f492c 111 send_uncached_js($theme->javascript_content($type));
e68c5f36
PS
112}
113
114//=================================================================================
115//=== utility functions ==
116// we are not using filelib because we need to fine tune all header
117// parameters to get the best performance.
118
8475b970 119function send_cached_js($jspath, $etag) {
7e9f1b63
PS
120 global $CFG;
121 require("$CFG->dirroot/lib/xsendfilelib.php");
122
8475b970 123 $lifetime = 60*60*24*60; // 60 days only - the revision may get incremented quite often
e68c5f36 124
8475b970 125 header('Etag: '.$etag);
baeb20bb 126 header('Content-Disposition: inline; filename="javascript.php"');
e68c5f36
PS
127 header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT');
128 header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
129 header('Pragma: ');
8c672cf9 130 header('Cache-Control: public, max-age='.$lifetime);
e68c5f36 131 header('Accept-Ranges: none');
8a7703ce 132 header('Content-Type: application/javascript; charset=utf-8');
7e9f1b63
PS
133
134 if (xsendfile($jspath)) {
135 die;
136 }
137
7c986f04
PS
138 if (!min_enable_zlib_compression()) {
139 header('Content-Length: '.filesize($jspath));
140 }
e68c5f36 141
e68c5f36
PS
142 readfile($jspath);
143 die;
144}
145
146function send_uncached_js($js) {
baeb20bb 147 header('Content-Disposition: inline; filename="javascript.php"');
e68c5f36
PS
148 header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT');
149 header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT');
150 header('Pragma: ');
151 header('Accept-Ranges: none');
8a7703ce 152 header('Content-Type: application/javascript; charset=utf-8');
e68c5f36
PS
153 header('Content-Length: '.strlen($js));
154
e68c5f36
PS
155 echo $js;
156 die;
157}
045f492c
SH
158
159function minify($files) {
d1f582d9
PS
160 if (empty($files)) {
161 return '';
162 }
163
045f492c
SH
164 if (0 === stripos(PHP_OS, 'win')) {
165 Minify::setDocRoot(); // IIS may need help
166 }
e1c2a211
PS
167 // disable all caching, we do it in moodle
168 Minify::setCache(null, false);
045f492c
SH
169
170 $options = array(
171 'bubbleCssImports' => false,
172 // Don't gzip content we just want text for storage
173 'encodeOutput' => false,
174 // Maximum age to cache, not used but required
175 'maxAge' => 1800,
176 // The files to minify
177 'files' => $files,
178 // Turn orr URI rewriting
179 'rewriteCssUris' => false,
180 // This returns the CSS rather than echoing it for display
181 'quiet' => true
182 );
183
d1f582d9
PS
184 $error = 'unknown';
185 try {
186 $result = Minify::serve('Files', $options);
187 if ($result['success']) {
188 return $result['content'];
189 }
190 } catch (Exception $e) {
191 $error = $e->getMessage();
192 $error = str_replace("\r", ' ', $error);
193 $error = str_replace("\n", ' ', $error);
194 }
195
196 // minification failed - try to inform the theme developer and include the non-minified version
197 $js = <<<EOD
198try {console.log('Error: Minimisation of theme javascript failed!');} catch (e) {}
199
200// Error: $error
201// Problem detected during javascript minimisation, please review the following code
202// =================================================================================
203
204
205EOD;
206 foreach ($files as $jsfile) {
207 $js .= file_get_contents($jsfile)."\n";
208 }
209 return $js;
045f492c 210}