Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
trigger_action_set_cache_expiry.inc
1 <?php
17 require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
18 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action/trigger_action.inc';
19 require_once SQ_CORE_PACKAGE_PATH.'/system/triggers/trigger_action_types/trigger_action_clear_cache/trigger_action_clear_cache.inc';
20 
33 {
34 
35 
63  public static function execute($settings, &$state)
64  {
65  $use_alternative = array_get_index($settings, 'use_alternative', FALSE);
66  $alt_dsn = array_get_index($settings, 'alt_dsn', '');
67  $alt_dsn_user = array_get_index($settings, 'alt_dsn_user', '');
68  $alt_dsn_pwd = array_get_index($settings, 'alt_dsn_pwd', '');
69  $alt_dsn_type = array_get_index($settings, 'alt_dsn_type', '');
70 
71  $alt_dsn_array = Array(
72  'DSN' => $alt_dsn,
73  'user' => $alt_dsn_user,
74  'password' => $alt_dsn_pwd,
75  'type' => $alt_dsn_type,
76  );
77 
78  // if using alternative dsn, and it is not supplied, bail
79  if ($use_alternative && $alt_dsn == '') {
80  return FALSE;
81  }
82 
83  if (empty($state['asset'])) {
84  // grab the asset if assetid is given, but not the asset.
85  if (empty($state['assetid'])) {
86  return FALSE;
87  } else {
88  $state['asset'] = $GLOBALS['SQ_SYSTEM']->am->getAsset($state['assetid']);
89  }
90  }
91 
92  // check which dsn do we use
93  if (!$use_alternative) {
94  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('dbcache');
95  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
96  } else {
97  // connect to alternative cache db dsn is a little bit different
98  $db = Trigger_Action_Set_Cache_Expiry::_connectAltDSN($alt_dsn_array);
99  if (is_null($db)) return FALSE;
100  MatrixDAL::changeDb('dbalt');
101  // initialise the transaction
102  // see mysource.inc::doTransaction for more info
103  if (!isset($GLOBALS['SQ_SYSTEM']->tm) || !($GLOBALS['SQ_SYSTEM']->tm instanceof Transaction_Manager)) {
104  require_once SQ_INCLUDE_PATH.'/transaction_manager.inc';
105  $GLOBALS['SQ_SYSTEM']->tm = new Transaction_Manager();
106  }
107  $GLOBALS['SQ_SYSTEM']->tm->begin('dbalt');
108  }
109 
110  // work out all affected assetids
111  $todo_assetids = Array();
112  if ($settings['use_current_asset']) {
113  $todo_assetids = Trigger_Action_Set_Cache_Expiry::_getAssetIDs($state['assetid'], $settings);
114  }
115  foreach ($settings['selected_assets'] as $id) {
116  $todo_assetids = array_merge($todo_assetids, Trigger_Action_Set_Cache_Expiry::_getAssetIDs($id, $settings));
117  }
118 
119  // only update db if at least one asset has been selected
120  if (!empty($todo_assetids)) {
121 
122  // work out the expiry time, current time + (value * unit)
123  $value = array_get_index($settings, 'value', 1);
124  $unit = array_get_index($settings, 'unit', 60);
125  $expiry = ts_iso8601(time() + ($value * $unit));
126  try {
127  // update all cache entries that has the current assetid
128  $bind_vars = Array(
129  'expires' => $expiry,
130  'assetids' => $todo_assetids,
131  );
132  MatrixDAL::executeQuery('cache_manager', 'setNewCacheExpiry', $bind_vars);
133  } catch (Exception $e) {
134  if (!$use_alternative) {
135  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
136  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
137  } else {
138  $GLOBALS['SQ_SYSTEM']->tm->rollback('dbalt');
140  }
141  return FALSE;
142  }
143  }
144 
145  if (!$use_alternative) {
146  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
147  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
148  } else {
149  // commit the transaction
150  $GLOBALS['SQ_SYSTEM']->tm->commit('dbalt');
152  }
153 
154  return Array(
155  'assetid' => $state['asset']->id,
156  );
157 
158  }//end execute()
159 
160 
171  public static function getInterface($settings, $prefix, $write_access=FALSE)
172  {
173  $output = parent::getInterface($settings, $prefix, $write_access);
174  $output .= Trigger_Action_Set_Cache_Expiry::_paintTimeUnit($settings, $prefix, $write_access);
175  $output .= '<br />';
176  $output .= Trigger_Action_Set_Cache_Expiry::_paintDSN($settings, $prefix, $write_access);
177 
178  return $output;
179 
180  }//end getInterface()
181 
182 
194  public static function processInterface(&$settings, $request_data)
195  {
196  parent::processInterface($settings, $request_data);
197 
198  $settings['value'] = array_get_index($request_data, 'value', 1);
199  $settings['unit'] = array_get_index($request_data, 'unit', 60);
200 
201  $settings['use_alternative'] = array_get_index($request_data, 'use_alternative', FALSE);
202  $settings['alt_dsn'] = array_get_index($request_data, 'alt_dsn', '');
203  $settings['alt_dsn_user'] = array_get_index($request_data, 'alt_dsn_user', '');
204  $settings['alt_dsn_pwd'] = array_get_index($request_data, 'alt_dsn_pwd', '');
205  $settings['alt_dsn_type'] = array_get_index($request_data, 'alt_dsn_type', '');
206 
207  return FALSE;
208 
209  }//end processInterface()
210 
211 
222  private static function _paintTimeUnit($settings, $prefix, $write_access=FALSE)
223  {
224  // expiry time = value * unit (sec)
225  $value = array_get_index($settings, 'value', 1);
226  $unit = array_get_index($settings, 'unit', 60);
227 
228  $time_unit = Array(
229  60 => translate('minute'),
230  3600 => translate('hour'),
231  86400 => translate('day'),
232  );
233 
234  if ($write_access) {
235  ob_start();
236  int_text_box($prefix.'[value]', $value, FALSE);
237  $value = ob_get_contents();
238  ob_end_clean();
239  ob_start();
240  combo_box($prefix.'[unit]', $time_unit, FALSE, $unit);
241  $unit = ob_get_contents();
242  ob_end_clean();
243  $output = ($value.' '.$unit);
244 
245  } else {
246  $unit = $time_unit[$unit];
247  $output = ($value.' '.$unit.'(s)');
248  }
249 
250  return translate('trigger_set_cache_expiry', $output);
251 
252  }//end _paintTimeUnit()
253 
254 
265  private static function _paintDSN($settings, $prefix, $write_access=FALSE)
266  {
267  // expiry time = value * unit (sec)
268  $use_alternative = array_get_index($settings, 'use_alternative', FALSE);
269  $alt_dsn = array_get_index($settings, 'alt_dsn', '');
270  $alt_dsn_user = array_get_index($settings, 'alt_dsn_user', '');
271  $alt_dsn_pwd = array_get_index($settings, 'alt_dsn_pwd', '');
272  $alt_dsn_type = array_get_index($settings, 'alt_dsn_type', '');
273 
274  if ($write_access) {
275  ?>
276  <script language="JavaScript" type="text/javascript">
277  function enableAltDSN(value) {
278  var prefix = '<?php echo $prefix; ?>';
279  var elems = new Array();
280  elems[0] = '[alt_dsn]';
281  elems[1] = '[alt_dsn_user]';
282  elems[2] = '[alt_dsn_pwd]';
283  elems[3] = '[alt_dsn_type]';
284  for (var x in elems) {
285  var id = prefix + elems[x];
286  var element = document.getElementById(id);
287  if (!value) {
288  element.disabled = 'disabled';
289  element.value = '';
290  } else {
291  element.disabled = '';
292  }
293  }
294  }
295  </script>
296  <?php
297 
298  ob_start();
299  check_box($prefix.'[use_alternative]', TRUE, $use_alternative, 'enableAltDSN(this.checked)');
300  $default = ob_get_contents();
301  ob_end_clean();
302  ob_start();
303  $disabled = (!$use_alternative) ? 'disabled="disabled"' : '';
304  ?>
305  <table border="0">
306  <tr>
307  <td valign="top"><b>DSN</b></td>
308  <td valign="top" style="padding-bottom: 10px;">
309  <?php text_box($prefix.'[alt_dsn]', $alt_dsn, 60, '', FALSE, $disabled); ?>
310  </td>
311  </tr>
312  <tr>
313  <td valign="top"><b>user</b></td>
314  <td valign="top" style="padding-bottom: 10px;">
315  <?php text_box($prefix.'[alt_dsn_user]', $alt_dsn_user, 30, '', FALSE, $disabled); ?>
316  </td>
317  </tr>
318  <tr>
319  <td valign="top"><b>password</b></td>
320  <td valign="top" style="padding-bottom: 10px;">
321  <?php text_box($prefix.'[alt_dsn_pwd]', $alt_dsn_pwd, 30, '', FALSE, $disabled); ?>
322  </td>
323  </tr>
324  <tr>
325  <td valign="top"><b>type</b></td>
326  <td valign="top" style="padding-bottom: 10px;">
327  <?php text_box($prefix.'[alt_dsn_type]', $alt_dsn_type, 30, '', FALSE, $disabled); ?>
328  </td>
329  </tr>
330  </table>
331  <?php
332  $alt_dsn_part = ob_get_contents();
333  ob_end_clean();
334  $output = translate('trigger_set_cache_expiry_default_db', '<b>dbcache</b><br />Use Alternative? '.$default);
335  $output .= '<br />'.translate('trigger_set_cache_expiry_alt_dsn', $alt_dsn_part);
336 
337  } else {
338  $output = 'Cache DSN: ';
339  $output .= '<b>'.((!$use_alternative) ? 'default dbcache DSN' : $alt_dsn).'</b>';
340  }
341 
342  return $output;
343 
344  }//end _paintDSN()
345 
346 
356  private static function &_connectAltDSN($dsn)
357  {
358  try {
359  MatrixDAL::dbConnect($dsn, 'dbalt');
360  $db = MatrixDAL::getDb('dbalt');
361  if ($dsn['type'] === 'oci') {
362  // We cannot specify any functions upon connect for OCI8
363  // functions - we must specify them in functions like
364  // oci_execute()...
365  ;
366  } else {
367  // Ensure that no conversion is done to empty strings and NULLs
368  $db->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL);
369 
370  // Return field names in lowercase
371  $db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
372 
373  // String-ify all fetches
374  $db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, TRUE);
375  }
376 
377  } catch (Exception $e) {
378  if (!SQ_PHP_CLI) {
379  header('HTTP/1.0 500 Internal Server Error');
380  }
381  throw new Exception('Could not create database connection: '.$e->getMessage());
382  return NULL;
383  }
384  return $db;
385 
386  }//end _connectAltDSN()
387 
388 
398  private static function _getAssetIDs($id, $settings)
399  {
400  // empty asset_types = include all type
401  $type_code = empty($settings['asset_types']) ? '' : $settings['asset_types'];
402  $todo_assetids = Array();
403 
404  switch ($settings['level']) {
405  case 'single':
406  $todo_assetids = Array($id);
407  break;
408  case 'dependants':
409  $todo_assetids = array_keys($GLOBALS['SQ_SYSTEM']->am->getDependantChildren($id, $type_code));
410  $todo_assetids[] = $id;
411  break;
412  case 'children':
413  $todo_assetids = array_keys($GLOBALS['SQ_SYSTEM']->am->getChildren($id, $type_code));
414  $todo_assetids[] = $id;
415  break;
416  }
417 
418  return $todo_assetids;
419 
420  }//end _getAssetIDs()
421 
422 
423 }//end class
424 
425 ?>