MDL-59431 requirejs: Prioritise errors
[moodle.git] / lib / requirejs.php
CommitLineData
adeb96d2
DW
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 * This file is serving optimised JS for RequireJS.
19 *
20 * @package core
21 * @copyright 2015 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25// Disable moodle specific debug messages and any errors in output,
26// comment out when debugging or better look into error log!
27define('NO_DEBUG_DISPLAY', true);
28
29// We need just the values from config.php and minlib.php.
30define('ABORT_AFTER_CONFIG', true);
31require('../config.php'); // This stops immediately at the beginning of lib/setup.php.
32require_once("$CFG->dirroot/lib/jslib.php");
33require_once("$CFG->dirroot/lib/classes/requirejs.php");
34
35$slashargument = min_get_slash_argument();
36if (!$slashargument) {
37 // The above call to min_get_slash_argument should always work.
38 die('Invalid request');
39}
40
41$slashargument = ltrim($slashargument, '/');
42if (substr_count($slashargument, '/') < 1) {
43 header('HTTP/1.0 404 not found');
44 die('Slash argument must contain both a revision and a file path');
45}
46// Split into revision and module name.
47list($rev, $file) = explode('/', $slashargument, 2);
48$rev = min_clean_param($rev, 'INT');
49$file = '/' . min_clean_param($file, 'SAFEPATH');
50
51// Only load js files from the js modules folder from the components.
52$jsfiles = array();
53list($unused, $component, $module) = explode('/', $file, 3);
54
55// No subdirs allowed - only flat module structure please.
56if (strpos('/', $module) !== false) {
57 die('Invalid module');
58}
59
60// Some (huge) modules are better loaded lazily (when they are used). If we are requesting
61// one of these modules, only return the one module, not the combo.
62$lazysuffix = "-lazy.js";
63$lazyload = (strpos($module, $lazysuffix) !== false);
64
65if ($lazyload) {
66 // We are lazy loading a single file - so include the component/filename pair in the etag.
67 $etag = sha1($rev . '/' . $component . '/' . $module);
68} else {
69 // We loading all (non-lazy) files - so only the rev makes this request unique.
70 $etag = sha1($rev);
71}
72
73
74// Use the caching only for meaningful revision numbers which prevents future cache poisoning.
75if ($rev > 0 and $rev < (time() + 60 * 60)) {
76 $candidate = $CFG->localcachedir . '/requirejs/' . $etag;
77
78 if (file_exists($candidate)) {
79 if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
80 // We do not actually need to verify the etag value because our files
81 // never change in cache because we increment the rev parameter.
82 js_send_unmodified(filemtime($candidate), $etag);
83 }
84 js_send_cached($candidate, $etag, 'requirejs.php');
85 exit(0);
86
87 } else {
88 $jsfiles = array();
89 if ($lazyload) {
90 $jsfiles = core_requirejs::find_one_amd_module($component, $module);
91 } else {
92 // Here we respond to the request by returning ALL amd modules. This saves
93 // round trips in production.
94
95 $jsfiles = core_requirejs::find_all_amd_modules();
96 }
97
98 $content = '';
99 foreach ($jsfiles as $modulename => $jsfile) {
39c40ada 100 $js = file_get_contents($jsfile);
d45a6d4f 101 if ($js === false) {
39c40ada 102 error_log('Failed to load JavaScript file ' . $jsfile);
0d8ad3b5
AN
103 $js = "/* Failed to load JavaScript file {$jsfile}. */\n";
104 $content = $js . $content;
105 continue;
39c40ada
TH
106 }
107 $js .= "\n";
adeb96d2
DW
108 // Inject the module name into the define.
109 $replace = 'define(\'' . $modulename . '\', ';
110 $search = 'define(';
111 // Replace only the first occurrence.
112 $js = implode($replace, explode($search, $js, 2));
113 $content .= $js;
114 }
115
116 js_write_cache_file_content($candidate, $content);
117 // Verify nothing failed in cache file creation.
118 clearstatcache();
119 if (file_exists($candidate)) {
120 js_send_cached($candidate, $etag, 'requirejs.php');
121 exit(0);
122 }
123 }
124}
125
126if ($lazyload) {
127 $jsfiles = core_requirejs::find_one_amd_module($component, $module, true);
128} else {
129 $jsfiles = core_requirejs::find_all_amd_modules(true);
130}
131
132$content = '';
133foreach ($jsfiles as $modulename => $jsfile) {
134 $shortfilename = str_replace($CFG->dirroot, '', $jsfile);
135 $js = "// ---- $shortfilename ----\n";
136 $js .= file_get_contents($jsfile) . "\n";
137 // Inject the module name into the define.
138 $replace = 'define(\'' . $modulename . '\', ';
139 $search = 'define(';
140
141 if (strpos($js, $search) === false) {
142 // We can't call debugging because we only have minimal config loaded.
143 header('HTTP/1.0 500 error');
39c40ada 144 die('JS file: ' . $shortfilename . ' cannot be loaded, or does not contain a javascript module in AMD format. "define()" not found.');
adeb96d2
DW
145 }
146
147 // Replace only the first occurrence.
148 $js = implode($replace, explode($search, $js, 2));
149 $content .= $js;
150}
d900d617 151js_send_uncached($content, 'requirejs.php');