expire.module 6.32 KB
Newer Older
Mike Carper's avatar
Mike Carper committed
1 2 3 4
<?php

/**
 * @file
5
 * Provides logic for page cache expiration.
Mike Carper's avatar
Mike Carper committed
6 7
 */

8 9 10 11
/**
 * Constant definitions.
 */

spleshka's avatar
spleshka committed
12 13 14 15 16
// Module statuses.
define('EXPIRE_STATUS_DISABLED', 0);
define('EXPIRE_STATUS_ENABLED_INTERNAL', 1);
define('EXPIRE_STATUS_ENABLED_EXTERNAL', 2);

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
// Node actions.
define('EXPIRE_NODE_INSERT', 1);
define('EXPIRE_NODE_UPDATE', 2);
define('EXPIRE_NODE_DELETE', 3);

// Comment actions.
define('EXPIRE_COMMENT_INSERT'   , 1);
define('EXPIRE_COMMENT_UPDATE'   , 2);
define('EXPIRE_COMMENT_DELETE'   , 3);
define('EXPIRE_COMMENT_PUBLISH'  , 4);
define('EXPIRE_COMMENT_UNPUBLISH', 5);

// User actions.
define('EXPIRE_USER_INSERT', 1);
define('EXPIRE_USER_UPDATE', 2);
define('EXPIRE_USER_DELETE', 3);
define('EXPIRE_USER_CANCEL', 4);

// VotingAPI actions.
define('EXPIRE_VOTINGAPI_INSERT', 1);
define('EXPIRE_VOTINGAPI_DELETE', 2);

39 40 41 42
// File actions.
define('EXPIRE_FILE_UPDATE', 1);
define('EXPIRE_FILE_DELETE', 2);

spleshka's avatar
spleshka committed
43 44 45 46 47
// Debug levels.
define('EXPIRE_DEBUG_DISABLED', 0);
define('EXPIRE_DEBUG_WATCHDOG', 1);
define('EXPIRE_DEBUG_FULL', 2);

48
// Default values for cache expiration.
49 50 51 52 53 54 55 56 57 58 59 60 61 62
define('EXPIRE_NODE_FRONT_PAGE'             , FALSE);
define('EXPIRE_NODE_NODE_PAGE'              , TRUE);
define('EXPIRE_NODE_TERM_PAGES'             , FALSE);
define('EXPIRE_NODE_REFERENCE_PAGES'        , FALSE);
define('EXPIRE_NODE_CUSTOM'                 , FALSE);
define('EXPIRE_COMMENT_FRONT_PAGE'          , FALSE);
define('EXPIRE_COMMENT_COMMENT_PAGE'        , TRUE);
define('EXPIRE_COMMENT_NODE_PAGE'           , TRUE);
define('EXPIRE_COMMENT_NODE_TERM_PAGES'     , FALSE);
define('EXPIRE_COMMENT_NODE_REFERENCE_PAGES', FALSE);
define('EXPIRE_COMMENT_CUSTOM'              , FALSE);
define('EXPIRE_USER_FRONT_PAGE'             , FALSE);
define('EXPIRE_USER_USER_PAGE'              , TRUE);
define('EXPIRE_USER_CUSTOM'                 , FALSE);
spleshka's avatar
spleshka committed
63
define('EXPIRE_VOTINGAPI_ENTITY'            , FALSE);
64
define('EXPIRE_VOTINGAPI_ENTITY_PAGE'       , FALSE);
spleshka's avatar
spleshka committed
65
define('EXPIRE_VOTINGAPI_CUSTOM'            , FALSE);
66 67 68
define('EXPIRE_FILE_FILE'                   , FALSE);
define('EXPIRE_FILE_FRONT_PAGE'             , FALSE);
define('EXPIRE_FILE_CUSTOM'                 , FALSE);
Mike Carper's avatar
Mike Carper committed
69

70 71 72 73
/**
 * Implementation of hook_menu().
 */
function expire_menu() {
74

75
  $items['admin/config/system/expire'] = array(
76
    'title'            => 'Cache Expiration',
77
    'description'      => 'Settings for expiration of cached pages.',
78 79 80
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('expire_admin_settings_form'),
    'access arguments' => array('administer site configuration'),
81
    'type'             => MENU_NORMAL_ITEM,
82
    'file'             => 'expire.admin.inc',
83 84 85 86 87
  );

  return $items;
}

88
/**
89
 * Implements hook_form_FORM_ID_alter().
90
 */
91 92 93
function expire_form_node_type_form_alter(&$form, &$form_state) {
  module_load_include('admin.inc', 'expire');
  expire_node_settings_form($form, $form_state);
94 95
}

Mike Carper's avatar
Mike Carper committed
96
/**
97
 * Implements hook_node_insert().
Mike Carper's avatar
Mike Carper committed
98
 */
99
function expire_node_insert($node) {
100
  expire_execute_expiration('node', $node, EXPIRE_NODE_INSERT);
101
}
102

103 104 105 106
/**
 * Implements hook_node_update().
 */
function expire_node_update($node) {
107
  expire_execute_expiration('node', $node, EXPIRE_NODE_UPDATE);
108
}
109

110 111 112 113
/**
 * Implements hook_node_delete().
 */
function expire_node_delete($node) {
114
  expire_execute_expiration('node', $node, EXPIRE_NODE_DELETE);
115 116
}

117
/**
118
 * Implements hook_comment_insert().
119
 */
120
function expire_comment_insert($comment) {
121
  expire_execute_expiration('comment', $comment, EXPIRE_COMMENT_INSERT);
122 123
}

124
/**
125
 * Implements hook_comment_update().
126
 */
127
function expire_comment_update($comment) {
128
  expire_execute_expiration('comment', $comment, EXPIRE_COMMENT_UPDATE);
129
}
130

131
/**
132
 * Implements hook_comment_delete().
133
 */
134
function expire_comment_delete($comment) {
135
  expire_execute_expiration('comment', $comment, EXPIRE_COMMENT_DELETE);
136 137 138
}

/**
139
 * Implements hook_comment_publish().
140
 */
141
function expire_comment_publish($comment) {
142
  expire_execute_expiration('comment', $comment, EXPIRE_COMMENT_PUBLISH);
Mike Carper's avatar
Mike Carper committed
143 144 145
}

/**
146
 * Implements hook_comment_unpublish().
Mike Carper's avatar
Mike Carper committed
147
 */
148
function expire_comment_unpublish($comment) {
149
  expire_execute_expiration('comment', $comment, EXPIRE_COMMENT_UNPUBLISH);
Mike Carper's avatar
Mike Carper committed
150 151 152
}

/**
153
 * Implements hook_user_insert().
Mike Carper's avatar
Mike Carper committed
154
 */
155
function expire_user_insert($account) {
156
  expire_execute_expiration('user', $account, EXPIRE_USER_INSERT);
Mike Carper's avatar
Mike Carper committed
157 158 159
}

/**
160
 * Implements hook_user_update().
Mike Carper's avatar
Mike Carper committed
161
 */
162
function expire_user_update(&$edit, $account) {
163
  expire_execute_expiration('user', $account, EXPIRE_USER_UPDATE);
Mike Carper's avatar
Mike Carper committed
164 165
}

166
/**
167
 * Implements hook_user_delete().
168
 */
169
function expire_user_delete($account) {
170
  expire_execute_expiration('user', $account, EXPIRE_USER_DELETE);
171 172
}

173 174 175 176
/**
 * Implements hook_user_cancel().
 */
function expire_user_cancel($account) {
177
  expire_execute_expiration('user', $account, EXPIRE_USER_CANCEL);
178 179
}

180
/**
181
 * Implements hook_votingapi_insert().
182
 */
183
function expire_votingapi_insert($votes) {
184
  expire_execute_expiration('votingapi', $votes, EXPIRE_VOTINGAPI_INSERT);
185 186 187
}

/**
188
 * Implements hook_votingapi_delete().
189 190
 */
function expire_votingapi_delete($votes) {
191
  expire_execute_expiration('votingapi', $votes, EXPIRE_VOTINGAPI_DELETE);
192 193
}

194 195 196 197 198 199 200 201 202 203 204 205 206 207
/**
 * Implements hook_file_update().
 */
function expire_file_update($file) {
  expire_execute_expiration('file', $file, EXPIRE_FILE_UPDATE);
}

/**
 * Implements hook_file_delete().
 */
function expire_file_delete($file) {
  expire_execute_expiration('file', $file, EXPIRE_FILE_DELETE);
}

208
/**
209
 * Execute expiration method for object.
210
 */
211
function expire_execute_expiration($type, $object, $action) {
spleshka's avatar
spleshka committed
212 213
  $status = variable_get('expire_status', EXPIRE_STATUS_DISABLED);
  if ($status) {
214
    return _expire_get_expiration_handler($type)->expire($object, $action);
spleshka's avatar
spleshka committed
215 216
  }
  return FALSE;
217
}
218

219
/**
220 221 222 223 224 225 226
 * Return class object that should handle expiration.
 *
 * @param $type
 *   Type that expiration is called for.
 *   Example: 'node', 'user', 'votingapi', etc.
 *
 * @return mixed
227
 */
228
function _expire_get_expiration_handler($type) {
229 230 231 232
  static $cache_objects;
  if (!isset($cache_objects[$type])) {
    $class = variable_get('expire_handler_' . $type, 'Expire' . drupal_ucfirst($type));
    $cache_objects[$type] = new $class();
233
  }
234
  return $cache_objects[$type];
235
}
236 237 238 239 240 241 242 243 244 245 246 247 248

/**
 * Simple print_r to html function.
 *
 * @param $data
 *   Array with output data.
 *
 * @return string
 *   print_r contents in nicely formatted html.
 */
function expire_print_r($data) {
  return str_replace('    ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities(print_r($data, TRUE))));
}