-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexteuropa_varnish.admin.inc
136 lines (116 loc) · 4.28 KB
/
nexteuropa_varnish.admin.inc
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
<?php
/**
* @file
* Callbacks used by the administration area.
*/
/**
* Generates the cache purge rule editing form.
*/
function nexteuropa_varnish_admin_settings_form($form, $form_state) {
if (!empty($form_state['value'])) {
nexteuropa_varnish_purge_all_form($form, $form_state);
}
$form['purge_cache'] = array(
'#type' => 'fieldset',
'#title' => t('Purge caches'),
'#description' => t('Flush all caches (Drupal & Varnish)'),
);
// If the purge mechanism is prevented to work, then the button must be
// disabled and a warning message must be displayed.
$disabled = _nexteuropa_varnish_temporary_message();
$form['purge_cache']['purge'] = array(
'#type' => 'submit',
'#value' => t('Purge all caches'),
'#submit' => array('nexteuropa_varnish_purge_all_confirm'),
'#disabled' => $disabled,
);
$form['settings'] = array(
'#type' => 'fieldset',
'#title' => t('Settings'),
);
$form['settings']['nexteuropa_varnish_default_purge_rule'] = array(
'#type' => 'checkbox',
'#title' => t('Enable the default purge rule'),
'#description' => t('Activates the default purge rule for all content types.
The rule invalidates the Varnish cache entries whenever content changes
are having an impact on the published/unpublished state.'),
'#default_value' => variable_get('nexteuropa_varnish_default_purge_rule', FALSE),
);
$form['#submit'][] = 'nexteuropa_varnish_admin_settings_cache_clear_submit';
return system_settings_form($form);
}
/**
* Submit callback for clearing the purge rules cache table.
*
* @see nexteuropa_varnish_admin_settings_form()
*/
function nexteuropa_varnish_admin_settings_cache_clear_submit($form, &$form_state) {
cache_clear_all('*', NEXTEUROPA_VARNISH_CACHE_TABLE, TRUE);
}
/**
* Redirect the user from "General" page to the "purge all" confirmation form.
*/
function nexteuropa_varnish_purge_all_confirm($form, &$form_state) {
$form_state['redirect'] = 'admin/config/system/nexteuropa-varnish/purge_all';
}
/**
* Generates the form allowing triggering the full cache purge.
*/
function nexteuropa_varnish_purge_all_form($form, &$form_state) {
$description = t("The action you are about to perform has a deep impact on the site's performance!");
$confirm_message = t("Are you sure you want to purge all site's caches (Varnish included)?");
return confirm_form($form,
$confirm_message,
'admin/config/system/nexteuropa-varnish/general',
$description,
t('Continue'),
t('Cancel')
);
}
/**
* Processes the full flush cache triggered by the "Purge all caches" button.
*
* @see nexteuropa_varnish_admin_settings_form()
*/
function nexteuropa_varnish_purge_all_form_submit($form, &$form_state) {
$confirm_message = t('The Drupal and Varnish caches have been fully flushed.');
$message_level = 'status';
// First clear the actual backend cache (usually DrupalDatabaseCache).
// Otherwise the web frontend cache will receive again outdated cached
// versions of pages.
drupal_flush_all_caches();
// Treating Varnish flushing (inspired by "flexibe_purge" contrib module).
$send_success = _nexteuropa_varnish_varnish_requests_send();
if (!$send_success) {
$confirm_message = t('The Varnish caches have not been purged correctly. Please consult logs for more information.');
$message_level = 'error';
}
drupal_set_message($confirm_message, $message_level);
$form_state['redirect'] = 'admin/config/system/nexteuropa-varnish/general';
}
/**
* Implements hook_FORM_ID_form_validate().
*/
function nexteuropa_varnish_admin_settings_form_validate($form, &$form_state) {
$rule_state = $form_state['values']['nexteuropa_varnish_default_purge_rule'];
if ($rule_state && _nexteuropa_varnish_check_node_rules()) {
form_set_error(
'settings',
t('You can not enable the default purge rule while "Purge rules" of type "node" exist.')
);
}
}
/**
* Checks if rules type of node exist.
*
* @return bool
* TRUE / FALSE depends on the results of the query.
*/
function _nexteuropa_varnish_check_node_rules() {
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'nexteuropa_varnish_cache_purge_rule')
->propertyCondition('paths', '');
$result = $query->execute();
return isset($result['nexteuropa_varnish_cache_purge_rule']);
}