Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_clear_cache.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
40 {
41 
42 
48  function HIPO_Job_Clear_Cache($code_name='')
49  {
50  $this->HIPO_Job($code_name);
51 
52  }//end constructor
53 
54 
64  public static function paintConfig(&$o, $class, $write_access)
65  {
66  // clear cache threshhold HIPO config entry
67  $o->openField(translate('clear_cache_threshold'));
68 
69  if ($write_access) {
70  text_box($class.'[SQ_HIPO_CLEAR_CACHE_THRESHOLD]', SQ_HIPO_CLEAR_CACHE_THRESHOLD, 5);
71  echo ' '.translate('assets');
72  } else {
73  echo SQ_HIPO_CLEAR_CACHE_THRESHOLD;
74  echo ' '.translate('assets');
75  }
76 
77  $o->closeField();
78 
79  }//end paintConfig()
80 
81 
88  public static function getConfigVars()
89  {
90  return Array(
91  'SQ_HIPO_CLEAR_CACHE_THRESHOLD' => Array('editable' => 1, 'default' => 100),
92  );
93 
94  }//end getConfigVars()
95 
96 
103  function getCodeName()
104  {
105  if (isset($this->_running_vars['linkid'])) {
106  return parent::getCodeName().$this->_running_vars['linkid'];
107  } else {
108  if ($this->_running_vars['delete_all'] == 'yes') {
109  // clear all system cache
110  return parent::getCodeName().'delete_all';
111  } else {
112  return parent::getCodeName().$this->_running_vars['assetid'];
113  }
114  }
115 
116  }//end getCodeName()
117 
118 
125  function getHipoName()
126  {
127  return translate('hipo_name_clear_cache');
128 
129  }//end getHipoName()
130 
131 
140  {
141  return Array(
142  Array(
143  'name' => 'Clear Cache Table',
144  'function_call' => Array(
145  'process_function' => 'processClearCacheTable',
146  ),
147  'running_mode' => 'server',
148  'auto_step' => TRUE,
149  'allow_cancel' => TRUE,
150  'percent_done' => 0,
151  'complete' => FALSE,
152  'message' => '',
153  ),
154  Array(
155  'name' => translate('hipo_clearing_cache'),
156  'function_call' => Array(
157  'process_function' => 'processClearCacheFile',
158  ),
159  'running_mode' => 'server',
160  'auto_step' => TRUE,
161  'allow_cancel' => TRUE,
162  'percent_done' => 0,
163  'complete' => FALSE,
164  'message' => '',
165  ),
166  Array(
167  'name' => 'Repopulating Cache',
168  'function_call' => Array(
169  'process_function' => 'processRepopulate',
170  ),
171  'running_mode' => 'server',
172  'auto_step' => TRUE,
173  'allow_cancel' => TRUE,
174  'percent_done' => 0,
175  'complete' => FALSE,
176  'message' => '',
177  ),
178  );
179 
180  }//end getInitialStepData()
181 
182 
192  {
193  if (SQ_HIPO_CLEAR_CACHE_THRESHOLD == 0) return 0;
194  if (!isset($this->_running_vars['total'])) return 0;
195  return (($this->_running_vars['total'] + 1) / SQ_HIPO_CLEAR_CACHE_THRESHOLD) * 100;
196 
197  }//end getThresholdPercentageRequired()
198 
199 
206  function freestyle()
207  {
208  if ($this->_running_vars['delete_all'] == 'yes') {
209  // run once to clear all cache
210  $this->processClearCacheTable($this->_steps[0], get_class($this));
211  } else {
212  while (!empty($this->_running_vars['todo_assetids'])) {
213  if (!$this->processClearCacheTable($this->_steps[0], get_class($this))) {
214  return FALSE;
215  }
216  }
217  }
218 
219  // todo paths populated, so process them
220  while (!empty($this->_running_vars['todo_filepaths'])) {
221  if (!$this->processClearCacheFile($this->_steps[1], get_class($this))) {
222  return FALSE;
223  }
224  }
225 
226  // repopulate
227  while (!empty($this->_running_vars['todo_repopulate'])) {
228  if (!$this->processRepopulate($this->_steps[2], get_class($this))) {
229  return FALSE;
230  }
231  }
232 
233  return TRUE;
234 
235  }//end freestyle()
236 
237 
244  function prepare()
245  {
246  if (empty($this->_running_vars['assetid']) && $this->_running_vars['delete_all'] == 'no') {
247  trigger_localised_error('HIPO0033', E_USER_WARNING);
248  return FALSE;
249  }
250 
251  if (isset($this->_running_vars['delete_all']) && $this->_running_vars['delete_all'] == 'no') {
252 
253  $assetids = $this->_running_vars['assetid'];
254  // Just in case some old code somewhere still passing the assetid as a string and not array.
255  if (!is_array($assetids)) {
256  $assetids = Array ($assetids);
257  }
258  if (empty($assetids)) {
259  trigger_localised_error('HIPO0032', E_USER_WARNING, $this->_running_vars['assetid']);
260  return FALSE;
261  }//end if
262 
263  $child_assets = Array();
264  $this->_running_vars['todo_assetids'] = Array();
265  foreach ($assetids as $assetid) {
266  switch ($this->_running_vars['level']) {
267  case 'single':
268  $this->_running_vars['todo_assetids'][] = $assetid;
269  break;
270  case 'dependants':
271  $child_assets += $GLOBALS['SQ_SYSTEM']->am->getDependantChildren($assetid, $this->_running_vars['type_codes']);
272  $this->_running_vars['todo_assetids'][] = $assetid;
273  break;
274  case 'children':
275  $child_assets += $GLOBALS['SQ_SYSTEM']->am->getChildren($assetid, $this->_running_vars['type_codes']);
276  $this->_running_vars['todo_assetids'][] = $assetid;
277  break;
278  }//end switch
279 
280  }//end foreach
281 
282  if (!empty($child_assets)) {
283  $child_assets = array_keys($child_assets);
284  $this->_running_vars['todo_assetids'] = array_merge($this->_running_vars['todo_assetids'], $child_assets);
285  }//end if
286 
287  $this->_running_vars['delete_all'] = FALSE;
288  $this->_running_vars['total'] = count($this->_running_vars['todo_assetids']);
289 
290  } else if (isset($this->_running_vars['delete_all']) && $this->_running_vars['delete_all'] == 'yes') {
291  // empty todo_assetids, since we do clearAllCache in a single step
292  $this->_running_vars['todo_assetids'] = Array();
293  $this->_running_vars['delete_all'] = TRUE;
294  $this->_running_vars['total'] = 1;
295  }
296 
297  $this->_running_vars['done_assetids'] = Array();
298  $this->_running_vars['todo_filepaths'] = Array();
299  $this->_running_vars['done_filepaths'] = Array();
300 
301  // note that this option only presents in tool
302  // and it does not work with 'delete_all' = yes
303  $this->_running_vars['todo_repopulate'] = Array();
304  $this->_running_vars['done_repopulate'] = Array();
305  if (isset($this->_running_vars['repopulate']) && $this->_running_vars['repopulate']) {
306  $this->_running_vars['todo_repopulate'] = $this->_running_vars['todo_assetids'];
307  }
308 
309  return parent::prepare();
310 
311  }//end prepare()
312 
313 
323  function processClearCacheTable(&$step_data, $prefix)
324  {
325  $cm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cache_manager');
326 
327  if ($this->_running_vars['delete_all'] === 'yes' || $this->_running_vars['delete_all'] === TRUE) {
328  // get all unique filepaths, and clear all system cache
329  $this->_running_vars['todo_filepaths'] = $cm->getAllFilePaths();
330  $cm->clearAllCache();
331 
332  } else {
333  // prepare all related filepaths, and clear cache for one asset
334  $current_assetid = array_pop($this->_running_vars['todo_assetids']);
335  $file_paths = $cm->getFilePaths(Array($current_assetid));
336  $cm->clearCache(Array($current_assetid));
337  $this->_running_vars['todo_filepaths'] = array_merge($this->_running_vars['todo_filepaths'], $file_paths);
338  $this->_running_vars['done_assetids'][] = $current_assetid;
339  }
340 
341  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($cm);
342 
343  if (empty($this->_running_vars['todo_assetids'])) {
344  $step_data['percent_done'] = 100;
345  $step_data['complete'] = TRUE;
346  } else {
347  $done = count($this->_running_vars['done_assetids']);
348  $total = count($this->_running_vars['todo_assetids']) + $done;
349  $step_data['percent_done'] = ($done / $total) * 100;
350  $step_data['complete'] = FALSE;
351  }
352  return TRUE;
353 
354  }//end processClearCacheTable()
355 
356 
366  function processClearCacheFile(&$step_data, $prefix)
367  {
368  if (!empty($this->_running_vars['todo_filepaths'])) {
369  $paths = array_splice($this->_running_vars['todo_filepaths'], 0, SQ_HIPO_CLEAR_CACHE_THRESHOLD);
370  foreach ($paths as $path) {
371  $path_name = SQ_CACHE_PATH.'/'.$path['path'];
372  if (is_file($path_name)) {
373  if (!unlink($path_name)) {
374  $this->_addError('Failed to delete cache file, '.$path_name, TRUE);
375  }
376  } else {
377  // missing cache file, probably corrupted.
378  // $this->_addError('The cache file can not be found : '.$path_name, TRUE);
379  }
380  }
381  $this->_running_vars['done_filepaths'] = array_merge($this->_running_vars['done_filepaths'], $paths);
382  }
383 
384  if (empty($this->_running_vars['todo_filepaths'])) {
385  $step_data['percent_done'] = 100;
386  $step_data['complete'] = TRUE;
387  } else {
388  $done = count($this->_running_vars['done_filepaths']);
389  $total = count($this->_running_vars['todo_filepaths']) + $done;
390  $step_data['percent_done'] = ($done / $total) * 100;
391  $step_data['complete'] = FALSE;
392  }
393  return TRUE;
394 
395  }//end processClearCacheFile()
396 
397 
407  function processRepopulate(&$step_data, $prefix)
408  {
409  if (!empty($this->_running_vars['todo_repopulate'])) {
410  $current_assetid = array_pop($this->_running_vars['todo_repopulate']);
411  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($current_assetid);
412  $urls_info = $asset->getURLs();
413  $urls = Array();
414  if (!empty($urls_info)) {
415  foreach ($urls_info as $url_info) {
416  if ($url_info['http'] == '1') {
417  $urls[] = 'http://'.$url_info['url'];
418  }
419  if ($url_info['https'] == '1') {
420  $urls[] = 'https://'.$url_info['url'];
421  }
422  }
423  }
424 
425  // Re-generate the cache per URL
426  $headers = Array(
427  'Cache-Control: public',
428  'Pragma: cache',
429  );
430  foreach ($urls as $url) {
431  fetch_url($url, array('RETURNTRANSFER' => 1), $headers, array(), FALSE);
432  }
433 
434  $this->_running_vars['done_repopulate'][] = $current_assetid;
435  }//end if
436 
437  if (empty($this->_running_vars['todo_repopulate'])) {
438  $step_data['percent_done'] = 100;
439  $step_data['complete'] = TRUE;
440  } else {
441  $done = count($this->_running_vars['done_repopulate']);
442  $total = count($this->_running_vars['todo_repopulate']) + $done;
443  $step_data['percent_done'] = ($done / $total) * 100;
444  $step_data['complete'] = FALSE;
445  }
446  return TRUE;
447 
448  }//end processRepopulate()
449 
450 
451 }//end class
452 ?>