-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrender_boost.module
596 lines (501 loc) · 16.6 KB
/
render_boost.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<?php
/**
* @file
* Render cache module.
*/
/**
* Implements hook_boot().
*/
function render_boost_boot() {
$config = variable_get('render_boost_settings');
if ($config['enable'] && $config['debug']) {
// Init statistics data.
global $_render_boost_devel;
$_render_boost_devel['count'] = 0;
$_render_boost_devel['checksum'] = 0;
$_render_boost_devel['get'] = 0;
$_render_boost_devel['store'] = 0;
drupal_register_shutdown_function('render_boost_shutdown');
}
}
/**
* Disable devel output for AJAX queries.
*/
function render_boost_ajax_render_alter() {
$GLOBALS['render_boost_debug_shutdown'] = FALSE;
}
/**
* Implements hook_flush_caches().
*/
function render_boost_flush_caches() {
$config = variable_get('render_boost_settings');
return array('cache_render');
}
/**
* Internal function to measure time for devel output.
*/
function render_boost_microtime_float() {
list($usec, $sec) = explode(" ", microtime());
return ((float) $usec + (float) $sec);
}
/**
* Runs on shutdown to display developer information.
*
* Render_boost_boot() registers this function as a shutdown function.
*/
function render_boost_shutdown() {
if (isset($GLOBALS['render_boost_debug_shutdown'])) {
return;
}
global $_render_boost_devel;
print '<div class="dev-render">Render boost called: ' . $_render_boost_devel['count'] . '
<span class="checksum">checksum check: <b>' . round($_render_boost_devel['checksum'] * 1000) . 'ms</b></span>
<span class="store">cache store: <b>' . round($_render_boost_devel['store'] * 1000) . 'ms</b></span>
<span class="get">cache get: <b>' . round($_render_boost_devel['get'] * 1000) . 'ms</b></span>
<span class="total">Total: <b>' . round(($_render_boost_devel['checksum'] + $_render_boost_devel['store'] + $_render_boost_devel['get']) * 1000) . 'ms</b></span>
</div>';
}
/**
* Generate checksum for render element.
*/
function render_boost_checksum($element) {
if (empty($element)) {
return FALSE;
}
global $_render_boost_devel;
$now = render_boost_microtime_float();
$_render_boost_devel['count']++;
$serialize = serialize($element);
$checksum = md5($serialize);
$_render_boost_devel['checksum'] = $_render_boost_devel['checksum'] + (render_boost_microtime_float() - $now);
return $checksum;
}
function render_boost_sanitize_form(&$element){
static $deep;
if(!$deep){
$deep = 1;
}else{
$deep ++;
}
if($deep > 3){
return;
}
if(is_array($element)){
if (isset($element['form_token'])) {
unset($element['form_token']);
}
if (isset($element['form_build_id'])) {
unset($element['form_build_id']);
}
if (isset($element['#build_id'])) {
unset($element['#build_id']);
}
if (isset($element['date'])) {
unset($element['date']);
}
if (isset($element['#attached'])) {
unset($element['#attached']);
}
foreach($element as $key => $value){
render_boost_sanitize_form($value);
$element[$key] = $value;
}
}
}
function render_boost_sanitize_objects(&$item, $key){
if(is_object($item)){
$item = render_boost_get_object_values($item);
if(isset($item['created'])){
unset($item['created']);
}
}
}
/**
* Get first level properties from object.
*/
function render_boost_get_object_values($object) {
$result = array();
foreach ($object as $key => $val) {
if (is_string($val) || is_bool($val) || is_numeric($val)) {
$result[$key] = $val;
}
}
return $result;
}
/**
* Store cached data.
*/
function render_boost_store_cache($checksum, $data) {
if (empty($data)) {
// Do not cache empty data.
return;
}
global $_render_boost_devel;
$now = render_boost_microtime_float();
// Construct the filepath where JS translation files are stored.
$config = variable_get('render_boost_settings');
$expire_after = $config['expire'] ? $config['expire'] : 3600;
$expire = REQUEST_TIME + $expire_after ;
cache_set($checksum, $data, 'cache_render', $expire);
$_render_boost_devel['store'] = $_render_boost_devel['store'] + (render_boost_microtime_float() - $now);
}
function render_boost_store_checksums($checksum = NULL){
static $checksums;
if(!$checksums){
$checksums = array();
}
if($checksum){
$checksums[] = $checksum;
}
return $checksums;
}
/**
* Get cache by checksum.
*/
function render_boost_get_cache($checksum) {
global $_render_boost_devel;
$now = render_boost_microtime_float();
$checksums = &drupal_static('render_boost_get_cache');
if (isset($checksums[$checksum])) {
return $checksums[$checksum];
}
$return = FALSE;
$config = variable_get('render_boost_settings');
if ($cache = cache_get($checksum, 'cache_render')) {
if(isset($cache->data['js'])){
foreach($cache->data['js'] as $key => $val){
drupal_add_js($key, $val['type']);
}
}
if(isset($cache->data['css'])){
foreach($cache->data['css'] as $key => $val){
drupal_add_css($key, $val);
}
}
if(isset($cache->data['settings'])){
foreach($cache->data['settings'] as $key => $val){
drupal_add_js($val, 'setting');
}
}
$return = '<!-- cached:' . $checksum. ' -->' . $cache->data['output'] . '<!-- /cached:' . $checksum. ' -->';
}
$checksums[$checksum] = $return;
render_boost_store_checksums($checksum);
$_render_boost_devel['get'] = $_render_boost_devel['get'] + (render_boost_microtime_float() - $now);
return $return ;
}
/**
* Process form states and attachments fo cached data.
*/
function render_boost_enable_forms_states_attaches($elements) {
if (!empty($elements['#states'])) {
// drupal_process_states($elements);
}
if (!empty($elements['#attached'])) {
// drupal_process_attached($elements);
}
foreach ($elements as $key => $val) {
if (is_array($val)) {
render_boost_enable_forms_states_attaches($val);
}
}
}
/**
* Main function. Replace drupal_render function.
*/
function render_boost_drupal_render(&$elements) {
$output = render_boost_render($elements);
preg_match_all('/<!-- render_boost_form:(.*?):(.*?) --\>/', $output, $matches);
if (!empty($matches[0])) {
$form_storage = render_boost_form_storage();
foreach ($matches[2] as $key => $form_serial_number) {
$rendered_element = $form_storage[$form_serial_number]['render'];
$output = str_replace($matches[0][$key], $rendered_element, $output);
}
}
return $output;
}
/**
* Multy time used render.
*/
function render_boost_render(&$elements) {
$empty_array = array(
'#printed' => FALSE,
);
if ($elements === $empty_array) {
return "";
}
if (isset($elements['#markup'])) {
// No cache for markup.
return render_boost_original_render($elements);
}
if (isset($elements['#ajax'])) {
// No cache for markup.
return render_boost_original_render($elements);
}
if (isset($elements['#type']) && $elements['#type'] == 'scripts') {
// No cache for window.Drupal = ....
return render_boost_original_render($elements);
}
if (isset($elements['#name']) && $elements['#name'] == 'form_build_id') {
// No cache for form_build_id
return render_boost_original_render($elements);
}
// Early-return nothing if user does not have access.
if (empty($elements) || (isset($elements['#access']) && !$elements['#access'])) {
return '';
}
// Do not print elements twice.
if (!empty($elements['#printed'])) {
return '';
}
if ($check_sum = render_boost_checksum($elements)) {
$output = render_boost_get_cache($check_sum);
if (FALSE === $output) {
$scripts_before = drupal_add_js();
$css_before = drupal_add_css();
$output = render_boost_original_render($elements);
$scripts_after = drupal_add_js();
$css_after = drupal_add_css();
if (!empty($output)) {
$data = array();
$data['output'] = $output;
$scripts_keys_before = array_keys($scripts_before);
$scripts_keys_after = array_keys($scripts_after);
if($diff = array_diff($scripts_keys_after,$scripts_keys_before)){
foreach($diff as $key){
$data['js'][$key] = $scripts_after[$key];
}
}
$data_keys_before = array_keys($scripts_before['settings']['data']);
$data_keys_after = array_keys($scripts_after['settings']['data']);
if($diff = array_diff($data_keys_after,$data_keys_before)){
foreach($diff as $key){
$data['settings'][$key] = $scripts_after['settings']['data'][$key];
}
}
$css_keys_before = array_keys($css_before);
$css_keys_after = array_keys($css_after);
if($diff = array_diff($css_keys_after,$css_keys_before)){
foreach($diff as $key){
$data['css'][$key] = $css_after[$key];
}
}
render_boost_store_cache($check_sum, $data);
}
}
}
else {
$output = render_boost_original_render($elements);
}
return $output;
}
/**
* Used by render_boost_replace_dynamic_content to get data for element by name.
*/
function render_boost_get_form_element($name, $elements) {
if (isset($elements['#type']) && $elements['#type'] == 'form') {
if (isset($elements[$name])) {
return $elements[$name];
}
}
else {
foreach ($elements as $variable => $element) {
if (is_array($element) && isset($element[$name])) {
return $element[$name];
}
}
}
}
/**
* Alter Performace page settings to add Render Cache settings.
*/
function render_boost_form_system_performance_settings_alter(&$form, &$form_state, $form_id) {
$config = variable_get('render_boost_settings');
$form['render_boost'] = array(
'#type' => 'fieldset',
'#title' => t('Render boost'),
'#description' => t('Render cache helps to avoid needless re-rendering of page elements by caching results.'),
'#tree' => TRUE,
);
$form['render_boost']['enable'] = array(
'#type' => 'checkbox',
'#title' => t("Enable render cache."),
'#default_value' => $config['enable'],
);
$form['render_boost']['settings'] = array(
'#type' => 'container',
'#states' => array(
// Hide the query log settings when not displaying query log.
'invisible' => array(
'input[name="render_boost[enable]"]' => array('checked' => FALSE),
),
),
);
$period = drupal_map_assoc(array(1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400), 'format_interval');
$form['render_boost']['settings']['expire'] = array(
'#type' => 'select',
'#title' => t("Cache expiration time"),
'#options' => $period,
'#default_value' => $config['expire'] ? $config['expire'] : 3600 ,
'#description' => t('Clean cache after this period.'),
);
$form['render_boost']['settings']['debug'] = array(
'#type' => 'checkbox',
'#title' => t("Enable debug output."),
'#default_value' => $config['debug'],
);
$form['#submit'][] = 'render_boost_system_settings_submit';
}
/**
* Save Render Cache settings.
*/
function render_boost_system_settings_submit($form, &$form_state) {
$config = variable_get('render_boost_settings');
$config['enable'] = $form_state['values']['render_boost']['enable'];
$config['expire'] = $form_state['values']['render_boost']['settings']['expire'];
$config['debug'] = $form_state['values']['render_boost']['settings']['debug'];
variable_set('render_boost_settings', $config);
}
/**
* Original drupal_render function except cache functions.
*/
function render_boost_original_render(&$elements) {
// Early-return nothing if user does not have access.
if (empty($elements) || (isset($elements['#access']) && !$elements['#access'])) {
return '';
}
// Do not print elements twice.
if (!empty($elements['#printed'])) {
return '';
}
// If #markup is set, ensure #type is set. This allows to specify just #markup
// on an element without setting #type.
if (isset($elements['#markup']) && !isset($elements['#type'])) {
$elements['#type'] = 'markup';
}
// If the default values for this element have not been loaded yet, populate
// them.
if (isset($elements['#type']) && empty($elements['#defaults_loaded'])) {
$elements += element_info($elements['#type']);
}
// Make any final changes to the element before it is rendered. This means
// that the $element or the children can be altered or corrected before the
// element is rendered into the final text.
if (isset($elements['#pre_render'])) {
foreach ($elements['#pre_render'] as $function) {
$elements = $function($elements);
}
}
// Allow #pre_render to abort rendering.
if (!empty($elements['#printed'])) {
return '';
}
// Get the children of the element, sorted by weight.
$children = element_children($elements, TRUE);
// Initialize this element's #children, unless a #pre_render callback already
// preset #children.
if (!isset($elements['#children'])) {
$elements['#children'] = '';
}
// Call the element's #theme function if it is set. Then any children of the
// element have to be rendered there.
if (isset($elements['#theme'])) {
$elements['#children'] = theme($elements['#theme'], $elements);
}
// If #theme was not set and the element has children, render them now.
// This is the same process as drupal_render_children() but is inlined
// for speed.
if ($elements['#children'] == '') {
foreach ($children as $key) {
$elements['#children'] .= render_boost_original_render($elements[$key]);
}
}
// Let the theme functions in #theme_wrappers add markup around the rendered
// children.
if (isset($elements['#theme_wrappers'])) {
foreach ($elements['#theme_wrappers'] as $theme_wrapper) {
$elements['#children'] = theme($theme_wrapper, $elements);
}
}
// Filter the outputted content and make any last changes before the
// content is sent to the browser. The changes are made on $content
// which allows the output'ed text to be filtered.
if (isset($elements['#post_render'])) {
foreach ($elements['#post_render'] as $function) {
$elements['#children'] = $function($elements['#children'], $elements);
}
}
// Add any JavaScript state information associated with the element.
if (!empty($elements['#states'])) {
drupal_process_states($elements);
}
// Add additional libraries, CSS, JavaScript an other custom
// attached data associated with this element.
if (!empty($elements['#attached'])) {
drupal_process_attached($elements);
}
$prefix = isset($elements['#prefix']) ? $elements['#prefix'] : '';
$suffix = isset($elements['#suffix']) ? $elements['#suffix'] : '';
$output = $prefix . $elements['#children'] . $suffix;
$elements['#printed'] = TRUE;
return $output;
}
function render_boost_form_storage($form_id = NULL, $form_render = NULL){
static $form_storage;
if($form_render){
if ($cache = cache_get($form_id, 'cache_render')) {
$subcaches = cache_get_multiple($cache->data, 'cache_render');
$checksums = &drupal_static('render_boost_get_cache');
$tmplist = array();
foreach($cache->data as $form_chksum){
$tmplist[$form_chksum] = $form_chksum;
}
foreach($subcaches as $checksum => $subcache){
unset($tmplist[$checksum]);
if(isset($checksums[$checksum])){
continue;
}
$checksums[$checksum] = $subcache->data['output'];
if(isset($subcache->data['js'])){
foreach($subcache->data['js'] as $key => $val){
drupal_add_js($key, $val['type']);
}
}
if(isset($subcache->data['css'])){
foreach($subcache->data['css'] as $key => $val){
drupal_add_css($key, $val);
}
}
if(isset($subcache->data['settings'])){
foreach($subcache->data['settings'] as $key => $val){
drupal_add_js($val, 'setting');
}
}
}
foreach($tmplist as $form_chksum){
$checksums[$form_chksum] = '';
}
$form_storage[] = array(
'elements' => $form_render,
'render' => render_boost_original_render($form_render)
);
}else{
$before = render_boost_store_checksums();
$form_storage[] = array(
'elements' => $form_render,
'render' => render_boost_original_render($form_render)
);
$after = render_boost_store_checksums();
if($diff = array_diff($after,$before)){
$config = variable_get('render_boost_settings');
$expire_after = $config['expire'] ? $config['expire'] : 3600;
$expire = REQUEST_TIME + $expire_after ;
cache_set($form_id, $diff, 'cache_render', $expire);
}
}
return count($form_storage) - 1;
}else{
return $form_storage;
}
}