agrcache.module 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <?php
  2. /**
  3. * @file
  4. * Provides imagecache style generation of css/js aggregates.
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function agrcache_menu() {
  10. $directory_path = variable_get('file_public_path', conf_path() . '/files');
  11. foreach (array('css', 'js') as $type) {
  12. $items[$directory_path . "/$type/%"] = array(
  13. 'title' => "Generate $type aggregate",
  14. 'page callback' => 'agrcache_generate_aggregate',
  15. 'page arguments' => array(count(explode('/', $directory_path)) + 1, $type),
  16. 'access callback' => TRUE,
  17. 'type' => MENU_CALLBACK,
  18. );
  19. }
  20. return $items;
  21. }
  22. /**
  23. * Implements hook_element_info_alter().
  24. */
  25. function agrcache_element_info_alter(&$type) {
  26. // Swap in our own aggregation callback.
  27. if (isset($type['styles']['#aggregate_callback'])) {
  28. $type['styles']['#aggregate_callback'] = 'agrcache_aggregate_css';
  29. }
  30. }
  31. /**
  32. * Implements hook_theme_registry_alter().
  33. */
  34. function agrcache_theme_registry_alter(&$theme_registry) {
  35. // For JavaScript there is no nice hook_elements() implementation to hook
  36. // into, aggregation is done inline in drupal_get_js(). So instead take over
  37. // template_preprocess_html.
  38. // @see http://drupal.org/node/330082
  39. // @see http://drupal.org/node/352951
  40. $index = array_search('template_process_html', $theme_registry['html']['process functions']);
  41. $theme_registry['html']['process functions'][$index] = '_agrcache_process_html';
  42. }
  43. /**
  44. * Replacement for template_process_html().
  45. */
  46. function _agrcache_process_html(&$variables) {
  47. // Render page_top and page_bottom into top level variables.
  48. $variables['page_top'] = drupal_render($variables['page']['page_top']);
  49. $variables['page_bottom'] = drupal_render($variables['page']['page_bottom']);
  50. // Place the rendered HTML for the page body into a top level variable.
  51. $variables['page'] = $variables['page']['#children'];
  52. $variables['page_bottom'] .= agrcache_get_js('footer');
  53. $variables['head'] = drupal_get_html_head();
  54. $variables['css'] = drupal_add_css();
  55. $variables['styles'] = drupal_get_css();
  56. $variables['scripts'] = agrcache_get_js();
  57. }
  58. /**
  59. * Replacement for drupal_get_js().
  60. */
  61. function agrcache_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALSE) {
  62. if (!isset($javascript)) {
  63. $javascript = drupal_add_js();
  64. }
  65. if (empty($javascript)) {
  66. return '';
  67. }
  68. // Allow modules to alter the JavaScript.
  69. if (!$skip_alter) {
  70. drupal_alter('js', $javascript);
  71. }
  72. // Filter out elements of the given scope.
  73. $items = array();
  74. foreach ($javascript as $key => $item) {
  75. if ($item['scope'] == $scope) {
  76. $items[$key] = $item;
  77. }
  78. }
  79. $output = '';
  80. // The index counter is used to keep aggregated and non-aggregated files in
  81. // order by weight.
  82. $index = 1;
  83. $processed = array();
  84. $files = array();
  85. $preprocess_js = (variable_get('preprocess_js', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
  86. // A dummy query-string is added to filenames, to gain control over
  87. // browser-caching. The string changes on every update or full cache
  88. // flush, forcing browsers to load a new copy of the files, as the
  89. // URL changed. Files that should not be cached (see drupal_add_js())
  90. // get REQUEST_TIME as query-string instead, to enforce reload on every
  91. // page request.
  92. $default_query_string = variable_get('css_js_query_string', '0');
  93. // For inline Javascript to validate as XHTML, all Javascript containing
  94. // XHTML needs to be wrapped in CDATA. To make that backwards compatible
  95. // with HTML 4, we need to comment out the CDATA-tag.
  96. $embed_prefix = "\n<!--//--><![CDATA[//><!--\n";
  97. $embed_suffix = "\n//--><!]]>\n";
  98. // Since Javascript may look for arguments in the url and act on them, some
  99. // third-party code might require the use of a different query string.
  100. $js_version_string = variable_get('drupal_js_version_query_string', 'v=');
  101. // Sort the JavaScript so that it appears in the correct order.
  102. uasort($items, 'drupal_sort_css_js');
  103. // Provide the page with information about the individual JavaScript files
  104. // used, information not otherwise available when aggregation is enabled.
  105. $setting['ajaxPageState']['js'] = array_fill_keys(array_keys($items), 1);
  106. unset($setting['ajaxPageState']['js']['settings']);
  107. drupal_add_js($setting, 'setting');
  108. // If we're outputting the header scope, then this might be the final time
  109. // that drupal_get_js() is running, so add the setting to this output as well
  110. // as to the drupal_add_js() cache. If $items['settings'] doesn't exist, it's
  111. // because drupal_get_js() was intentionally passed a $javascript argument
  112. // stripped off settings, potentially in order to override how settings get
  113. // output, so in this case, do not add the setting to this output.
  114. if ($scope == 'header' && isset($items['settings'])) {
  115. $items['settings']['data'][] = $setting;
  116. }
  117. // Loop through the JavaScript to construct the rendered output.
  118. $element = array(
  119. '#tag' => 'script',
  120. '#value' => '',
  121. '#attributes' => array(
  122. 'type' => 'text/javascript',
  123. ),
  124. );
  125. foreach ($items as $item) {
  126. $query_string = empty($item['version']) ? $default_query_string : $js_version_string . $item['version'];
  127. switch ($item['type']) {
  128. case 'setting':
  129. $js_element = $element;
  130. $js_element['#value_prefix'] = $embed_prefix;
  131. $js_element['#value'] = 'jQuery.extend(Drupal.settings, ' . drupal_json_encode(drupal_array_merge_deep_array($item['data'])) . ");";
  132. $js_element['#value_suffix'] = $embed_suffix;
  133. $output .= theme('html_tag', array('element' => $js_element));
  134. break;
  135. case 'inline':
  136. $js_element = $element;
  137. if ($item['defer']) {
  138. $js_element['#attributes']['defer'] = 'defer';
  139. }
  140. $js_element['#value_prefix'] = $embed_prefix;
  141. $js_element['#value'] = $item['data'];
  142. $js_element['#value_suffix'] = $embed_suffix;
  143. $processed[$index++] = theme('html_tag', array('element' => $js_element));
  144. break;
  145. case 'file':
  146. $js_element = $element;
  147. if (!$item['preprocess'] || !$preprocess_js) {
  148. if ($item['defer']) {
  149. $js_element['#attributes']['defer'] = 'defer';
  150. }
  151. $query_string_separator = (strpos($item['data'], '?') !== FALSE) ? '&' : '?';
  152. $js_element['#attributes']['src'] = file_create_url($item['data']) . $query_string_separator . ($item['cache'] ? $query_string : REQUEST_TIME);
  153. $processed[$index++] = theme('html_tag', array('element' => $js_element));
  154. }
  155. else {
  156. // By increasing the index for each aggregated file, we maintain
  157. // the relative ordering of JS by weight. We also set the key such
  158. // that groups are split by items sharing the same 'group' value and
  159. // 'every_page' flag. While this potentially results in more aggregate
  160. // files, it helps make each one more reusable across a site visit,
  161. // leading to better front-end performance of a website as a whole.
  162. // See drupal_add_js() for details.
  163. $key = 'aggregate_' . $item['group'] . '_' . $item['every_page'] . '_' . $index;
  164. $processed[$key] = '';
  165. $files[$key][$item['data']] = $item;
  166. }
  167. break;
  168. case 'external':
  169. $js_element = $element;
  170. // Preprocessing for external JavaScript files is ignored.
  171. if ($item['defer']) {
  172. $js_element['#attributes']['defer'] = 'defer';
  173. }
  174. $js_element['#attributes']['src'] = $item['data'];
  175. $processed[$index++] = theme('html_tag', array('element' => $js_element));
  176. break;
  177. }
  178. }
  179. // Aggregate any remaining JS files that haven't already been output.
  180. // This is the only hunk of code change from drupal_get_js().
  181. if ($preprocess_js && count($files) > 0) {
  182. $map = array();
  183. foreach ($files as $key => $file_set) {
  184. $data = agrcache_build_aggregate_cache($file_set, 'js');
  185. $uri = $data['uri'];
  186. if (!empty($data['#write_cache'])) {
  187. $map['files'][$data['key']] = $data['uri'];
  188. $map['callbacks'][$data['uri']] = $file_set;
  189. }
  190. // Only include the file if was written successfully. Errors are logged
  191. // using watchdog.
  192. if ($uri) {
  193. $preprocess_file = file_create_url($uri);
  194. $js_element = $element;
  195. $js_element['#attributes']['src'] = $preprocess_file;
  196. $processed[$key] = theme('html_tag', array('element' => $js_element));
  197. }
  198. }
  199. }
  200. if (!empty($map)) {
  201. agrcache_add_to_variable('agrcache_js_cache_files', $map);
  202. }
  203. // Keep the order of JS files consistent as some are preprocessed and others are not.
  204. // Make sure any inline or JS setting variables appear last after libraries have loaded.
  205. return implode('', $processed) . $output;
  206. }
  207. /**
  208. * Replacement for drupal_aggregate_css().
  209. */
  210. function agrcache_aggregate_css(&$css_groups) {
  211. $preprocess_css = (variable_get('preprocess_css', FALSE) && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update'));
  212. $map = array();
  213. // For each group that needs aggregation, aggregate its items.
  214. foreach ($css_groups as $key => $group) {
  215. switch ($group['type']) {
  216. // If a file group can be aggregated into a single file, do so, and set
  217. // the group's data property to the file path of the aggregate file.
  218. case 'file':
  219. if ($group['preprocess'] && $preprocess_css) {
  220. $data = agrcache_build_aggregate_cache($group['items'], 'css');
  221. if ($data) {
  222. $css_groups[$key]['data'] = $data['uri'];
  223. if (!empty($data['#write_cache'])) {
  224. $map['files'][$data['key']] = $data['uri'];
  225. $map['callbacks'][$data['uri']] = $group['items'];
  226. }
  227. }
  228. }
  229. break;
  230. // Aggregate all inline CSS content into the group's data property.
  231. case 'inline':
  232. $css_groups[$key]['data'] = '';
  233. foreach ($group['items'] as $item) {
  234. $css_groups[$key]['data'] .= drupal_load_stylesheet_content($item['data'], $item['preprocess']);
  235. }
  236. break;
  237. }
  238. }
  239. if (!empty($map)) {
  240. agrcache_add_to_variable('agrcache_css_cache_files', $map);
  241. }
  242. }
  243. /**
  244. * Replacement for drupal_build_css_cache() and drupal_build_js_cache().
  245. */
  246. function agrcache_build_aggregate_cache($files, $type) {
  247. $data = '';
  248. $uri = '';
  249. $map = variable_get('agrcache_' . $type . '_cache_files', array());
  250. $key = hash('sha256', serialize($files));
  251. if (isset($map['files'][$key])) {
  252. return array('uri' => $map['files'][$key]);
  253. }
  254. else {
  255. // To ensure a new filenames are created only when the contents of the
  256. // hashed files changes, use a hash of the contents for the filename.
  257. $function = 'agrcache_collect_' . $type . '_group';
  258. $data = $function($files);
  259. if ($data) {
  260. // Prefix filename to prevent blocking by firewalls which reject files
  261. // starting with "ad*".
  262. $filename = $type . '_' . drupal_hash_base64($data) . ".$type";
  263. // Create the aggregate directory within the files folder.
  264. $path = "public://$type";
  265. $uri = $path . '/' . $filename;
  266. return array(
  267. 'key' => $key,
  268. 'uri' => $uri,
  269. '#write_cache' => TRUE,
  270. );
  271. }
  272. else {
  273. return FALSE;
  274. }
  275. }
  276. }
  277. /**
  278. * Collect javascript files.
  279. */
  280. function agrcache_collect_js_group($js) {
  281. // JavaScript aggregation currently only collects the files together, so
  282. // re-use agrcache_process_js_group();
  283. return agrcache_process_js_group($js);
  284. }
  285. /**
  286. * College CSS files.
  287. */
  288. function agrcache_collect_css_group($css) {
  289. $contents = '';
  290. foreach ($css as $stylesheet) {
  291. if ($stylesheet['type'] == 'file' && file_exists($stylesheet['data'])) {
  292. $contents .= file_get_contents($stylesheet['data']);
  293. }
  294. }
  295. return $contents;
  296. }
  297. /**
  298. * Process a js group for aggregation.
  299. */
  300. function agrcache_process_js_group($js) {
  301. $data = '';
  302. foreach ($js as $path => $info) {
  303. if ($info['preprocess']) {
  304. // Append a ';' and a newline after each JS file to prevent them from running together.
  305. $data .= file_get_contents($path) . ";\n";
  306. }
  307. }
  308. return $data;
  309. }
  310. /**
  311. * Aggregate and compress css groups.
  312. */
  313. function agrcache_process_css_group($css) {
  314. // Build aggregate CSS file.
  315. $data = '';
  316. foreach ($css as $stylesheet) {
  317. // Only 'file' stylesheets can be aggregated.
  318. if ($stylesheet['type'] == 'file') {
  319. $contents = drupal_load_stylesheet($stylesheet['data'], TRUE);
  320. // Build the base URL of this CSS file: start with the full URL.
  321. $css_base_url = file_create_url($stylesheet['data']);
  322. // Move to the parent.
  323. $css_base_url = substr($css_base_url, 0, strrpos($css_base_url, '/'));
  324. // Simplify to a relative URL if the stylesheet URL starts with the
  325. // base URL of the website.
  326. if (substr($css_base_url, 0, strlen($GLOBALS['base_root'])) == $GLOBALS['base_root']) {
  327. $css_base_url = substr($css_base_url, strlen($GLOBALS['base_root']));
  328. }
  329. _drupal_build_css_path(NULL, $css_base_url . '/');
  330. // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths.
  331. $data .= preg_replace_callback('/url\(\s*[\'"]?(?![a-z]+:|\/+)([^\'")]+)[\'"]?\s*\)/i', '_drupal_build_css_path', $contents);
  332. }
  333. }
  334. // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import,
  335. // @import rules must proceed any other style, so we move those to the top.
  336. $regexp = '/@import[^;]+;/i';
  337. preg_match_all($regexp, $data, $matches);
  338. $data = preg_replace($regexp, '', $data);
  339. $data = implode('', $matches[0]) . $data;
  340. return $data;
  341. }
  342. /**
  343. * Menu callback to generate a css aggregate.
  344. */
  345. function agrcache_generate_aggregate($filename, $type) {
  346. // Recreate the full uri from the filename.
  347. $path = "public://$type";
  348. $uri = $path . '/' . $filename;
  349. $map = variable_get('agrcache_' . $type . '_cache_files', array());
  350. $compression = variable_get($type . '_gzip_compression', TRUE) && variable_get('clean_url', 0) && extension_loaded('zlib');
  351. // This callback should only be called if the file does not already exist on
  352. // disk since the webserver will serve the file directly, bypassing PHP when it does.
  353. // However it is possible that the file was created during bootstrap by another request.
  354. if (file_exists($uri)) {
  355. $data = file_get_contents($uri);
  356. if ($compression) {
  357. $compressed = gzencode($data, 9, FORCE_GZIP);
  358. }
  359. }
  360. elseif (isset($map['callbacks'][$uri])) {
  361. // @todo: consider adding locking here.
  362. // @see drupal.org/node/886488
  363. $function = 'agrcache_process_' . $type . '_group';
  364. $data = $function($map['callbacks'][$uri]);
  365. // Check file_exists() again, in case the file was built during processing.
  366. if (!file_exists($uri)) {
  367. // Create the file.
  368. file_prepare_directory($path, FILE_CREATE_DIRECTORY);
  369. if (!file_exists($uri) && !file_unmanaged_save_data($data, $uri, FILE_EXISTS_REPLACE)) {
  370. drupal_add_http_header('Status', '503 Service Unavailable');
  371. print t('Error generating aggregate');
  372. drupal_exit();
  373. }
  374. // If gzip compression is enabled, clean URLs are enabled (which means
  375. // that rewrite rules are working) and the zlib extension is available then
  376. // create a gzipped version of this file. This file is served conditionally
  377. // to browsers that accept gzip using .htaccess rules.
  378. if ($compression) {
  379. $compressed = gzencode($data, 9, FORCE_GZIP);
  380. if (!file_exists($uri . '.gz') && !file_unmanaged_save_data($compressed, $uri . '.gz', FILE_EXISTS_REPLACE)) {
  381. drupal_add_http_header('Status', '503 Service Unavailable');
  382. print t('Error generating aggregate');
  383. drupal_exit();
  384. }
  385. }
  386. }
  387. $content_type = $type == 'css' ? 'text/css' : 'application/javascript';
  388. $headers = array();
  389. $headers['Content-Type'] = $content_type;
  390. // Ensure this file can be cached by browsers and reverse proxies.
  391. $headers['Cache-Control'] = 'public, max-age=1209600';
  392. // Since the file name is based on a hash of file contents, there is no
  393. // problem allowing the browser to cache it for as long as possible.
  394. // Set this to two weeks since that's what .htaccess does if mod_expires
  395. // is enabled.
  396. $headers['Expires'] = gmdate(DATE_RFC1123, REQUEST_TIME + 1209600);
  397. // When possible, serve the gzip version of the file via PHP.
  398. if (!empty($compressed) && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) {
  399. $headers['Content-Encoding'] = 'gzip';
  400. $data = $compressed;
  401. ini_set('zlib.output_compression', '0');
  402. }
  403. foreach ($headers as $key => $value) {
  404. drupal_add_http_header($key, $value);
  405. }
  406. print $data;
  407. drupal_exit();
  408. }
  409. else {
  410. watchdog('agrcache', 'Received request for a non-existent css aggregate @uri', array('@uri' => $uri));
  411. }
  412. }
  413. /**
  414. * Merge the existing value of a variable from the database with new values.
  415. *
  416. * @param $name
  417. * The name of the variable.
  418. * @param $values
  419. * An array of values to add.
  420. * @param $default
  421. * The default value of the variable, as passed to variable_get().
  422. * This defaults to an empty array().
  423. */
  424. function agrcache_add_to_variable($name, $values, $default = array()) {
  425. // This function bypasses variable_get() on purpose. Since when dealing with
  426. // 'volatile' variables such as css and javascript maps that may be updated
  427. // by multiple threads, it is necessary to minimize the potential race
  428. // condition where threads may overwrite the value of the variable with a
  429. // version missing values that were just added by a previous process.
  430. // This approach lowers the window for that race condition to the time
  431. // between the database query and variable_set(), which should be only a
  432. // couple of milliseconds compared to up to over a second between
  433. // variable_initialize() and reaching this function.
  434. $original = $default;
  435. $result = db_query('SELECT value FROM {variable} WHERE name = :name', array(':name' => $name))->fetchField();
  436. if ($result) {
  437. $original = unserialize($result);
  438. }
  439. variable_set($name, array_merge_recursive($original, $values));
  440. }