Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
tool_clear_squid_cache.inc
1 <?php
18 require_once SQ_SYSTEM_ROOT.'/core/assets/system/tool/tool.inc';
19 require_once SQ_DATA_PATH.'/private/conf/tools.inc';
20 
35 {
36 
37 
44  function __construct($assetid=0)
45  {
46  parent::__construct($assetid);
47 
48  }//end constructor
49 
50 
60  public static function paintTool(Backend_Outputter $o, $type_code)
61  {
62  $tool_info = $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($type_code);
63  $o->openSection($tool_info['name']);
64 
65  require_once SQ_DATA_PATH.'/private/conf/tools.inc';
66  $preferencesBroken = array();
67  $squid_hostnames = trim(SQ_TOOL_SQUID_CACHE_HOSTNAMES);
68  if (empty($squid_hostnames) === TRUE) {
69  $preferencesBroken[] = 'squid_tool_no_hostnames';
70  }
71 
72  $squidclient_path = trim(SQ_TOOL_SQUID_CACHE_PATH);
73  if (empty($squidclient_path) === TRUE) {
74  $preferencesBroken[] = 'squid_tool_path_empty';
75  } else {
76  if (is_file($squidclient_path) === FALSE) {
77  $preferencesBroken[] = 'squid_tool_path_invalid';
78  }
79  }
80 
81  if (empty($preferencesBroken) === FALSE) {
82  $o->openField(translate('squid_tool_not_configured'));
83  foreach ($preferencesBroken as $brokenItem) {
84  $o->note(translate($brokenItem));
85  }
86  $o->closeField();
87  $o->closeSection();
88  return;
89  }
90 
91  // interface to get to asset using asset finder
92  $o->openField(translate('asset'));
93  asset_finder($type_code.'_assetid');
94  $o->note(translate('squid_tool_asset_note'));
95  $o->closeField();
96 
97  // if clear now box is checked, cache is cleared at commit
98  $o->openField(translate('squid_tool_clear_now_field'));
99  check_box($type_code.'_clear_now', TRUE);
100  $o->closeField();
101 
102  // if populate now box is checked, will request page using CURL
103  $o->openField(translate('squid_tool_populate_question'));
104  check_box($type_code.'_populate_now', TRUE);
105  echo translate('squid_tool_populate_note');
106  $o->closeField();
107 
108  // cascade options
109  $o->openField(translate('cascade_question'));
110  $options = Array(
111  'single' => translate('this_asset'),
112  'children' => translate('this_asset_and_children'),
113  );
114  check_box($type_code.'_cascade', TRUE);
115  // combo_box($type_code.'_cascade', $options, '', FALSE);
116  echo translate('squid_tool_cascade_note');
117  echo '<br /><br />';
118  $o->note(translate('squid_tool_note'));
119  $o->closeField();
120 
121  $o->closeSection();
122 
123  // scheduled jobs section
124  $o->openSection(translate('squid_tool_schedule_section'));
125 
126  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
127  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_squid_cache_purge');
129 
130  // print scheduled jobs, one_off or repeating
131  if (!empty($purge_jobs)) {
132  $o->openField(translate('squid_tool_scheduled_job_field'), 'new_line');
133  ?>
134  <table class="sq-backend-table">
135  <tr>
136  <td class="sq-backend-table-header"><?php echo translate('asset'); ?></td>
137  <td class="sq-backend-table-header"><?php echo translate('when'); ?></td>
138  <td align="center" class="sq-backend-table-header" style="text-align: center;"><?php echo translate('delete_question'); ?></td>
139  </tr>
140  <?php
141  for ($i = 0; $i < count($purge_jobs); $i++) {
142  ?>
143  <tr>
144  <td class="sq-backend-table-cell">
145  <?php echo get_asset_tag_line($purge_jobs[$i]->attr('ownerid')); ?>
146 
147  </td>
148  <td class="sq-backend-table-cell">
149  <?php echo $purge_jobs[$i]->readableWhen(); ?>
150  </td>
151  <td align="center" class="sq-backend-table-cell">
152  <?php
153  // only print checkbox if user has write access to asset
154  if ($purge_jobs[$i]->canDelete()) {
155  if ($purge_jobs[$i]->writeAccess('')) {
156  check_box($type_code.'_remove_fs[]', $purge_jobs[$i]->id);
157  }
158  } else {
159  echo '&nbsp;';
160  }
161  ?>
162  </td>
163  </tr>
164  <?php
165  }//end for
166  ?>
167  </table><br />
168  <?php
169  $o->closeField();
170  }//end if
171 
172  $cron_epoc = (int)$cron_mgr->attr('epoch');
173  $cron_refresh = (int)$cron_mgr->attr('refresh_time');
174  if ($cron_epoc > 0 && $cron_refresh > 0) {
175 
176  // prints the interface for adding a new one-off job
177  $o->openField(translate('squid_tool_one_off_field'), 'new_line');
178  require_once SQ_FUDGE_PATH.'/general/datetime.inc';
179  $new_job = new Cron_Job_Squid_Cache_Purge();
180  $edit_fns = $new_job->getEditFns();
181  $edit_fns->paintInLineBackend($new_job, $o, $type_code.'_one_off');
182  echo '&nbsp;<br /><br />';
183  $o->closeField();
184 
185  // prints the interface for adding a new repeating job
186  $o->openField(translate('squid_tool_repeating_field'), 'new_line');
187  $new_job = new Cron_Job_Squid_Cache_Purge();
188  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
189  $new_job->setAttrValue('type', 'repeating');
190  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
191  $edit_fns = $new_job->getEditFns();
192  $edit_fns->paintInLineBackend($new_job, $o, $type_code.'_repeating');
193  $o->note(translate('cron_manager_next_run', $cron_mgr->readableRefreshTime(), readable_datetime($cron_mgr->timeOfNextRun())));
194  $o->closeField();
195 
196  } else {
197  echo translate('cron_manager_not_configured');
198  }
199 
200  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($cron_mgr);
201 
202  $o->closeSection();
203 
204  }//end paintTool()
205 
206 
216  public static function processTool(Backend_Outputter $o, $type_code)
217  {
218  // If this isn't set in the post array at all, then there was an error
219  // being shown (eg empty hostnames or invalid squidclient path).
220  // So don't process anything.
221  if (isset($_POST[$type_code.'_assetid']) === FALSE) {
222  return FALSE;
223  }
224 
225  $cron_mgr = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager');
226  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_squid_cache_purge');
227 
228  // remove jobs
229  if (!empty($_POST[$type_code.'_remove_fs'])) {
230  $GLOBALS['SQ_SYSTEM']->am->includeAsset('cron_job_squid_cache_purge');
232  if (!empty($purge_jobs)) {
233  if ($GLOBALS['SQ_SYSTEM']->am->acquireLock($cron_mgr->id, 'links')) {
234  for ($i = 0; $i < count($purge_jobs); $i++) {
235  if (in_array($purge_jobs[$i]->id, $_POST[$type_code.'_remove_fs'])) {
236  $cron_mgr->removeJob($purge_jobs[$i]);
237  }
238  }
239 
240  $GLOBALS['SQ_SYSTEM']->am->releaseLock($cron_mgr->id, 'links');
241  } else {
242  trigger_localised_error('SYS0232', E_USER_NOTICE);
243  }
244  }
245  // remove job only, do not print error msg
246  if (!$_REQUEST[$type_code.'_assetid']['assetid']) {
247  return FALSE;
248  }
249  }
250 
251  // error: asset is not defined
252  if (!$_REQUEST[$type_code.'_assetid']['assetid']) {
253  trigger_localised_error('SQUID0001', E_USER_NOTICE);
254  return FALSE;
255  }
256 
257  // set HIPO running_vars
258  $assetid = $_REQUEST[$type_code.'_assetid']['assetid'];
259  $cascade = (isset($_REQUEST[$type_code.'_cascade'])) ? $_REQUEST[$type_code.'_cascade'] : FALSE;
260  $populate_now = FALSE;
261  if (isset($_REQUEST[$type_code.'_populate_now']) && $_REQUEST[$type_code.'_populate_now']) {
262  $populate_now = TRUE;
263  }
264  $vars = Array(
265  'assetid' => $assetid,
266  'cascade' => $cascade,
267  'repopulate' => $populate_now,
268  );
269 
270  // schedule job
271  $owner = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
272 
273  $urls = $GLOBALS['SQ_SYSTEM']->am->getURLs($assetid);
274  if (empty($urls)) {
275  return FALSE;
276  }
277 
278  if ($owner->adminAccess('')) {
279 
280  // one off
281  $new_job = new Cron_Job_Squid_Cache_Purge();
282  $edit_fns = $new_job->getEditFns();
283  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
284  $new_job->setAttrValue('ownerid', $assetid);
285  $new_job->setAttrValue('cascade', $cascade);
286  $new_job->setAttrValue('repopulate', $populate_now);
287  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
288  if ($edit_fns->processInLineBackend($new_job, $o, $type_code.'_one_off')) {
289  $cron_mgr->addJob($new_job, $GLOBALS['SQ_SYSTEM']->user);
290  }
291 
292  // repeating
293  $new_job = new Cron_Job_Squid_Cache_Purge();
294  $edit_fns = $new_job->getEditFns();
295  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
296  $new_job->setAttrValue('type', 'repeating');
297  $new_job->setAttrValue('ownerid',$assetid);
298  $new_job->setAttrValue('cascade', $cascade);
299  $new_job->setAttrValue('repopulate', $populate_now);
300  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
301  if ($edit_fns->processInLineBackend($new_job, $o, $type_code.'_repeating')) {
302  $cron_mgr->addJob($new_job, $GLOBALS['SQ_SYSTEM']->user);
303  }
304 
305  // run HIPO job if the 'clear now' box is checked
306  $run_now = isset($_REQUEST[$type_code.'_clear_now']) && $_REQUEST[$type_code.'_clear_now'];
307  if ($populate_now) $run_now = TRUE;
308  if ($run_now) {
309  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
310  $hh->queueHipo('hipo_job_tool_clear_squid_cache', $vars, '', SQ_PACKAGES_PATH.'/squid/hipo_jobs');
311  $url = $hh->runQueuedJobs();
312  if (!empty($url)) $o->setRedirect($url);
313  }
314 
315  } else {
316  trigger_localised_error('SQUID0003', E_USER_NOTICE, translate('squid_cron_name', $owner->id));
317  }//end if has write access
318 
319  return FALSE; // we want to repaint the tool
320 
321  }//end processTool()
322 
323 
324 }//end class
325 
326 
327 ?>