| 1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Functions for generating the HTML that Moodle should output.
|
|
|
19 |
*
|
|
|
20 |
* Please see http://docs.moodle.org/en/Developement:How_Moodle_outputs_HTML
|
|
|
21 |
* for an overview.
|
|
|
22 |
*
|
|
|
23 |
* @copyright 2009 Tim Hunt
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
* @package core
|
|
|
26 |
* @category output
|
|
|
27 |
*/
|
|
|
28 |
|
| 1441 |
ariadna |
29 |
/** General rendering target, usually normal browser page */
|
|
|
30 |
define('RENDERER_TARGET_GENERAL', 'general');
|
| 1 |
efrain |
31 |
|
| 1441 |
ariadna |
32 |
/** General rendering target, usually normal browser page, but with limited capacity to avoid API use */
|
|
|
33 |
define('RENDERER_TARGET_MAINTENANCE', 'maintenance');
|
| 1 |
efrain |
34 |
|
| 1441 |
ariadna |
35 |
/** Plain text rendering for CLI scripts and cron */
|
|
|
36 |
define('RENDERER_TARGET_CLI', 'cli');
|
|
|
37 |
|
|
|
38 |
/** Plain text rendering for Ajax scripts*/
|
|
|
39 |
define('RENDERER_TARGET_AJAX', 'ajax');
|
|
|
40 |
|
|
|
41 |
/** Plain text rendering intended for sending via email */
|
|
|
42 |
define('RENDERER_TARGET_TEXTEMAIL', 'textemail');
|
|
|
43 |
|
|
|
44 |
/** Rich text html rendering intended for sending via email */
|
|
|
45 |
define('RENDERER_TARGET_HTMLEMAIL', 'htmlemail');
|
|
|
46 |
|
| 1 |
efrain |
47 |
/**
|
|
|
48 |
* Returns current theme revision number.
|
|
|
49 |
*
|
|
|
50 |
* @return int
|
|
|
51 |
*/
|
|
|
52 |
function theme_get_revision() {
|
|
|
53 |
global $CFG;
|
|
|
54 |
|
|
|
55 |
if (empty($CFG->themedesignermode)) {
|
|
|
56 |
if (empty($CFG->themerev)) {
|
|
|
57 |
// This only happens during install. It doesn't matter what themerev we use as long as it's positive.
|
|
|
58 |
return 1;
|
|
|
59 |
} else {
|
|
|
60 |
return $CFG->themerev;
|
|
|
61 |
}
|
|
|
62 |
} else {
|
|
|
63 |
return -1;
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/**
|
|
|
68 |
* Returns current theme sub revision number. This is the revision for
|
|
|
69 |
* this theme exclusively, not the global theme revision.
|
|
|
70 |
*
|
|
|
71 |
* @param string $themename The non-frankenstyle name of the theme
|
|
|
72 |
* @return int
|
|
|
73 |
*/
|
|
|
74 |
function theme_get_sub_revision_for_theme($themename) {
|
|
|
75 |
global $CFG;
|
|
|
76 |
|
|
|
77 |
if (empty($CFG->themedesignermode)) {
|
|
|
78 |
$pluginname = "theme_{$themename}";
|
|
|
79 |
$revision = during_initial_install() ? null : get_config($pluginname, 'themerev');
|
|
|
80 |
|
|
|
81 |
if (empty($revision)) {
|
|
|
82 |
// This only happens during install. It doesn't matter what themerev we use as long as it's positive.
|
|
|
83 |
return 1;
|
|
|
84 |
} else {
|
|
|
85 |
return $revision;
|
|
|
86 |
}
|
|
|
87 |
} else {
|
|
|
88 |
return -1;
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Calculates and returns the next theme revision number.
|
|
|
94 |
*
|
|
|
95 |
* @return int
|
|
|
96 |
*/
|
|
|
97 |
function theme_get_next_revision() {
|
|
|
98 |
global $CFG;
|
|
|
99 |
|
|
|
100 |
$next = time();
|
| 1441 |
ariadna |
101 |
if (isset($CFG->themerev) && ($next <= $CFG->themerev) && (($CFG->themerev - $next) < 60 * 60)) {
|
| 1 |
efrain |
102 |
// This resolves problems when reset is requested repeatedly within 1s,
|
|
|
103 |
// the < 1h condition prevents accidental switching to future dates
|
|
|
104 |
// because we might not recover from it.
|
| 1441 |
ariadna |
105 |
$next = $CFG->themerev + 1;
|
| 1 |
efrain |
106 |
}
|
|
|
107 |
|
|
|
108 |
return $next;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Calculates and returns the next theme revision number.
|
|
|
113 |
*
|
|
|
114 |
* @param string $themename The non-frankenstyle name of the theme
|
|
|
115 |
* @return int
|
|
|
116 |
*/
|
|
|
117 |
function theme_get_next_sub_revision_for_theme($themename) {
|
|
|
118 |
global $CFG;
|
|
|
119 |
|
|
|
120 |
$next = time();
|
|
|
121 |
$current = theme_get_sub_revision_for_theme($themename);
|
| 1441 |
ariadna |
122 |
if ($next <= $current && $current - $next < 60 * 60) {
|
| 1 |
efrain |
123 |
// This resolves problems when reset is requested repeatedly within 1s,
|
|
|
124 |
// the < 1h condition prevents accidental switching to future dates
|
|
|
125 |
// because we might not recover from it.
|
|
|
126 |
$next = $current + 1;
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
return $next;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Sets the current theme revision number.
|
|
|
134 |
*
|
|
|
135 |
* @param int $revision The new theme revision number
|
|
|
136 |
*/
|
|
|
137 |
function theme_set_revision($revision) {
|
|
|
138 |
set_config('themerev', $revision);
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
/**
|
|
|
142 |
* Sets the current theme revision number for a specific theme.
|
|
|
143 |
* This does not affect the global themerev value.
|
|
|
144 |
*
|
|
|
145 |
* @param string $themename The non-frankenstyle name of the theme
|
|
|
146 |
* @param int $revision The new theme revision number
|
|
|
147 |
*/
|
|
|
148 |
function theme_set_sub_revision_for_theme($themename, $revision) {
|
|
|
149 |
set_config('themerev', $revision, "theme_{$themename}");
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Get the path to a theme config.php file.
|
|
|
154 |
*
|
|
|
155 |
* @param string $themename The non-frankenstyle name of the theme to check
|
|
|
156 |
*/
|
|
|
157 |
function theme_get_config_file_path($themename) {
|
|
|
158 |
global $CFG;
|
|
|
159 |
|
|
|
160 |
if (file_exists("{$CFG->dirroot}/theme/{$themename}/config.php")) {
|
|
|
161 |
return "{$CFG->dirroot}/theme/{$themename}/config.php";
|
| 1441 |
ariadna |
162 |
} else if (!empty($CFG->themedir) && file_exists("{$CFG->themedir}/{$themename}/config.php")) {
|
| 1 |
efrain |
163 |
return "{$CFG->themedir}/{$themename}/config.php";
|
|
|
164 |
} else {
|
|
|
165 |
return null;
|
|
|
166 |
}
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
/**
|
|
|
170 |
* Get the path to the local cached CSS file.
|
|
|
171 |
*
|
|
|
172 |
* @param string $themename The non-frankenstyle theme name.
|
|
|
173 |
* @param int $globalrevision The global theme revision.
|
|
|
174 |
* @param int $themerevision The theme specific revision.
|
|
|
175 |
* @param string $direction Either 'ltr' or 'rtl' (case sensitive).
|
|
|
176 |
*/
|
|
|
177 |
function theme_get_css_filename($themename, $globalrevision, $themerevision, $direction) {
|
|
|
178 |
global $CFG;
|
|
|
179 |
|
|
|
180 |
$path = "{$CFG->localcachedir}/theme/{$globalrevision}/{$themename}/css";
|
|
|
181 |
$filename = $direction == 'rtl' ? "all-rtl_{$themerevision}" : "all_{$themerevision}";
|
|
|
182 |
return "{$path}/{$filename}.css";
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* Generates and saves the CSS files for the given theme configs.
|
|
|
187 |
*
|
|
|
188 |
* @param theme_config[] $themeconfigs An array of theme_config instances.
|
|
|
189 |
* @param array $directions Must be a subset of ['rtl', 'ltr'].
|
|
|
190 |
* @param bool $cache Should the generated files be stored in local cache.
|
|
|
191 |
* @return array The built theme content in a multi-dimensional array of name => direction => content
|
|
|
192 |
*/
|
| 1441 |
ariadna |
193 |
function theme_build_css_for_themes(
|
|
|
194 |
$themeconfigs = [],
|
|
|
195 |
$directions = ['rtl', 'ltr'],
|
|
|
196 |
$cache = true,
|
|
|
197 |
$mtraceprogress = false
|
|
|
198 |
): array {
|
| 1 |
efrain |
199 |
global $CFG;
|
|
|
200 |
|
|
|
201 |
if (empty($themeconfigs)) {
|
|
|
202 |
return [];
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
require_once("{$CFG->libdir}/csslib.php");
|
|
|
206 |
|
|
|
207 |
$themescss = [];
|
|
|
208 |
$themerev = theme_get_revision();
|
|
|
209 |
// Make sure the local cache directory exists.
|
|
|
210 |
make_localcache_directory('theme');
|
|
|
211 |
|
|
|
212 |
foreach ($themeconfigs as $themeconfig) {
|
|
|
213 |
$themecss = [];
|
|
|
214 |
$oldrevision = theme_get_sub_revision_for_theme($themeconfig->name);
|
|
|
215 |
$newrevision = theme_get_next_sub_revision_for_theme($themeconfig->name);
|
|
|
216 |
|
|
|
217 |
// First generate all the new css.
|
|
|
218 |
foreach ($directions as $direction) {
|
|
|
219 |
if ($mtraceprogress) {
|
|
|
220 |
$timestart = microtime(true);
|
|
|
221 |
mtrace('Building theme CSS for ' . $themeconfig->name . ' [' .
|
|
|
222 |
$direction . '] ...', '');
|
|
|
223 |
}
|
|
|
224 |
// Lock it on. Technically we should build all themes for SVG and no SVG - but ie9 is out of support.
|
|
|
225 |
$themeconfig->force_svg_use(true);
|
|
|
226 |
$themeconfig->set_rtl_mode(($direction === 'rtl'));
|
|
|
227 |
|
|
|
228 |
$themecss[$direction] = $themeconfig->get_css_content();
|
|
|
229 |
if ($cache) {
|
|
|
230 |
$themeconfig->set_css_content_cache($themecss[$direction]);
|
|
|
231 |
$filename = theme_get_css_filename($themeconfig->name, $themerev, $newrevision, $direction);
|
|
|
232 |
css_store_css($themeconfig, $filename, $themecss[$direction]);
|
|
|
233 |
}
|
|
|
234 |
if ($mtraceprogress) {
|
|
|
235 |
mtrace(' done in ' . round(microtime(true) - $timestart, 2) . ' seconds.');
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
$themescss[$themeconfig->name] = $themecss;
|
|
|
239 |
|
|
|
240 |
if ($cache) {
|
|
|
241 |
// Only update the theme revision after we've successfully created the
|
|
|
242 |
// new CSS cache.
|
|
|
243 |
theme_set_sub_revision_for_theme($themeconfig->name, $newrevision);
|
|
|
244 |
|
|
|
245 |
// Now purge old files. We must purge all old files in the local cache
|
|
|
246 |
// because we've incremented the theme sub revision. This will leave any
|
|
|
247 |
// files with the old revision inaccessbile so we might as well removed
|
|
|
248 |
// them from disk.
|
|
|
249 |
foreach (['ltr', 'rtl'] as $direction) {
|
|
|
250 |
$oldcss = theme_get_css_filename($themeconfig->name, $themerev, $oldrevision, $direction);
|
|
|
251 |
if (file_exists($oldcss)) {
|
|
|
252 |
unlink($oldcss);
|
|
|
253 |
}
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
return $themescss;
|
|
|
259 |
}
|
|
|
260 |
|
|
|
261 |
/**
|
|
|
262 |
* Invalidate all server and client side caches.
|
|
|
263 |
*
|
|
|
264 |
* This method deletes the physical directory that is used to cache the theme
|
|
|
265 |
* files used for serving.
|
|
|
266 |
* Because it deletes the main theme cache directory all themes are reset by
|
|
|
267 |
* this function.
|
|
|
268 |
*/
|
|
|
269 |
function theme_reset_all_caches() {
|
|
|
270 |
global $CFG, $PAGE;
|
|
|
271 |
require_once("{$CFG->libdir}/filelib.php");
|
|
|
272 |
|
|
|
273 |
$next = theme_get_next_revision();
|
|
|
274 |
theme_set_revision($next);
|
|
|
275 |
|
|
|
276 |
if (!empty($CFG->themedesignermode)) {
|
|
|
277 |
$cache = cache::make_from_params(cache_store::MODE_APPLICATION, 'core', 'themedesigner');
|
|
|
278 |
$cache->purge();
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
// Purge compiled post processed css.
|
|
|
282 |
cache::make('core', 'postprocessedcss')->purge();
|
|
|
283 |
|
|
|
284 |
// Delete all old theme localcaches.
|
|
|
285 |
$themecachedirs = glob("{$CFG->localcachedir}/theme/*", GLOB_ONLYDIR);
|
|
|
286 |
foreach ($themecachedirs as $localcachedir) {
|
|
|
287 |
fulldelete($localcachedir);
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
if ($PAGE) {
|
|
|
291 |
$PAGE->reload_theme();
|
|
|
292 |
}
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
/**
|
|
|
296 |
* Reset static caches.
|
|
|
297 |
*
|
|
|
298 |
* This method indicates that all running cron processes should exit at the
|
|
|
299 |
* next opportunity.
|
|
|
300 |
*/
|
|
|
301 |
function theme_reset_static_caches() {
|
|
|
302 |
\core\task\manager::clear_static_caches();
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
/**
|
|
|
306 |
* Enable or disable theme designer mode.
|
|
|
307 |
*
|
|
|
308 |
* @param bool $state
|
|
|
309 |
*/
|
|
|
310 |
function theme_set_designer_mod($state) {
|
|
|
311 |
set_config('themedesignermode', (int)!empty($state));
|
|
|
312 |
// Reset caches after switching mode so that any designer mode caches get purged too.
|
|
|
313 |
theme_reset_all_caches();
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
/**
|
|
|
317 |
* Purge theme used in context caches.
|
|
|
318 |
*/
|
|
|
319 |
function theme_purge_used_in_context_caches() {
|
|
|
320 |
\cache::make('core', 'theme_usedincontext')->purge();
|
|
|
321 |
}
|
|
|
322 |
|
|
|
323 |
/**
|
|
|
324 |
* Delete theme used in context cache for a particular theme.
|
|
|
325 |
*
|
|
|
326 |
* When switching themes, both old and new theme caches are deleted.
|
|
|
327 |
* This gives the query the opportunity to recache accurate results for both themes.
|
|
|
328 |
*
|
|
|
329 |
* @param string $newtheme The incoming new theme.
|
|
|
330 |
* @param string $oldtheme The theme that was already set.
|
|
|
331 |
*/
|
|
|
332 |
function theme_delete_used_in_context_cache(string $newtheme, string $oldtheme): void {
|
|
|
333 |
if ((strlen($newtheme) > 0) && (strlen($oldtheme) > 0)) {
|
|
|
334 |
// Theme -> theme.
|
|
|
335 |
\cache::make('core', 'theme_usedincontext')->delete($oldtheme);
|
|
|
336 |
\cache::make('core', 'theme_usedincontext')->delete($newtheme);
|
|
|
337 |
} else {
|
|
|
338 |
// No theme -> theme, or theme -> no theme.
|
|
|
339 |
\cache::make('core', 'theme_usedincontext')->delete($newtheme . $oldtheme);
|
|
|
340 |
}
|
|
|
341 |
}
|
|
|
342 |
|
|
|
343 |
/**
|
| 1441 |
ariadna |
344 |
* Invalidate all server and client side template caches.
|
| 1 |
efrain |
345 |
*/
|
| 1441 |
ariadna |
346 |
function template_reset_all_caches() {
|
|
|
347 |
global $CFG;
|
| 1 |
efrain |
348 |
|
| 1441 |
ariadna |
349 |
$next = time();
|
|
|
350 |
if (isset($CFG->templaterev) && $next <= $CFG->templaterev && $CFG->templaterev - $next < 60 * 60) {
|
|
|
351 |
// This resolves problems when reset is requested repeatedly within 1s,
|
|
|
352 |
// the < 1h condition prevents accidental switching to future dates
|
|
|
353 |
// because we might not recover from it.
|
|
|
354 |
$next = $CFG->templaterev + 1;
|
| 1 |
efrain |
355 |
}
|
|
|
356 |
|
| 1441 |
ariadna |
357 |
set_config('templaterev', $next);
|
| 1 |
efrain |
358 |
}
|
|
|
359 |
|
|
|
360 |
/**
|
| 1441 |
ariadna |
361 |
* Invalidate all server and client side JS caches.
|
| 1 |
efrain |
362 |
*/
|
| 1441 |
ariadna |
363 |
function js_reset_all_caches() {
|
|
|
364 |
global $CFG;
|
| 1 |
efrain |
365 |
|
| 1441 |
ariadna |
366 |
$next = time();
|
|
|
367 |
if (isset($CFG->jsrev) && $next <= $CFG->jsrev && $CFG->jsrev - $next < 60 * 60) {
|
|
|
368 |
// This resolves problems when reset is requested repeatedly within 1s,
|
|
|
369 |
// the < 1h condition prevents accidental switching to future dates
|
|
|
370 |
// because we might not recover from it.
|
|
|
371 |
$next = $CFG->jsrev + 1;
|
| 1 |
efrain |
372 |
}
|
|
|
373 |
|
| 1441 |
ariadna |
374 |
set_config('jsrev', $next);
|
| 1 |
efrain |
375 |
}
|