Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_tool_clear_squid_cache.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
34 {
35 
36 
43  function __construct($code_name='')
44  {
45  $this->uses_trans = FALSE;
46  parent::__construct($code_name);
47  $GLOBALS['SQ_SYSTEM']->lm->includePackageStrings('squid');
48 
49  }//end constructor
50 
51 
61  public function getCodeName()
62  {
63  return parent::getCodeName().'-'.$this->_running_vars['assetid'];
64 
65  }//end getCodeName()
66 
67 
74  public function getHipoName()
75  {
76  return translate('squid_hipo_name_clear_squid_cache');
77 
78  }//end getHipoName()
79 
80 
88  public function getInitialStepData()
89  {
90  return Array(
91  Array(
92  'name' => translate('squid_hipo_clearing_squid_cache'),
93  'function_call' => Array(
94  'process_function' => 'processClearCache',
95  ),
96  'running_mode' => 'server',
97  'auto_step' => TRUE,
98  'percent_done' => 0,
99  'complete' => FALSE,
100  'message' => '',
101  'allow_cancel' => TRUE,
102  ),
103  );
104 
105  }//end getInitialStepData()
106 
107 
114  public function freestyle()
115  {
116  while (!empty($this->_running_vars['todo_urls'])) {
117  if (!$this->processClearCache($this->_steps[0], get_class($this))) {
118  return FALSE;
119  }
120  }
121  return TRUE;
122 
123  }//end freestyle()
124 
125 
132  public function prepare()
133  {
134 
135  require_once SQ_DATA_PATH.'/private/conf/tools.inc';
136 
137  // get the prefs setting for squidclient
138  $this->_running_vars['hostnames'] = explode("\n", trim(SQ_TOOL_SQUID_CACHE_HOSTNAMES));
139  $this->_running_vars['path'] = SQ_TOOL_SQUID_CACHE_PATH;
140  if ($this->_running_vars['path'] == '') {
141  $this->_running_vars['path'] = '/usr/bin/squidclient';
142  }
143  $this->_running_vars['port'] = SQ_TOOL_SQUID_CACHE_PORT;
144  if ($this->_running_vars['port'] != '') {
145  $this->_running_vars['port'] = '-p '.$this->_running_vars['port'];
146  }
147  $this->_running_vars['option'] = SQ_TOOL_SQUID_CACHE_OPTION;
148  $this->_running_vars['slash'] = SQ_TOOL_SQUID_CACHE_SLASH;
149  $this->_running_vars['url_port'] = SQ_TOOL_SQUID_URL_PORT;
150 
151  // if cascade option is enabled, get the children of the selected asset
152  $assetids = Array($this->_running_vars['assetid']);
153  if ($this->_running_vars['cascade']) {
154  $children = array_keys($GLOBALS['SQ_SYSTEM']->am->getChildren($this->_running_vars['assetid']));
155  $assetids = array_merge($assetids, $children);
156  }
157  // add urls applied to each asset into the todo array
158  $this->_running_vars['todo_urls'] = Array();
159  foreach ($assetids as $assetid) {
160  $result = $GLOBALS['SQ_SYSTEM']->am->getURLs($assetid);
161  $this->_running_vars['todo_urls'] = array_merge($result, $this->_running_vars['todo_urls']);
162  }
163  $this->_running_vars['done_urls'] = Array();
164  return parent::prepare();
165 
166  }//end prepare()
167 
168 
178  public function processClearCache(Array &$step_data, $prefix)
179  {
180  if (!empty($this->_running_vars['todo_urls'])) {
181 
182  $hostnames = $this->_running_vars['hostnames'];
183  $path = $this->_running_vars['path'];
184  $port = $this->_running_vars['port'];
185  $options = $this->_running_vars['option'];
186  $cascade = $this->_running_vars['cascade'];
187  $index = array_shift(array_keys($this->_running_vars['todo_urls']));
188  $urls = Array($this->_running_vars['todo_urls'][$index]['url']);
189 
190  // For the top level URL, include the one with trailing slash too (bug #5996)
191  if ($this->_running_vars['slash'] || strpos($this->_running_vars['todo_urls'][$index]['url'], '/') === FALSE) {
192  // include the same url, with a trailing slash
193  $urls[] = $this->_running_vars['todo_urls'][$index]['url'].'/';
194  }
195  $protocols = Array();
196  if ($this->_running_vars['todo_urls'][$index]['http']) {
197  $protocols[] = 'http';
198  }
199  if ($this->_running_vars['todo_urls'][$index]['https']) {
200  $protocols[] = 'https';
201  }
202 
203  $ms = $GLOBALS['SQ_SYSTEM']->getMessagingService();
204 
205  foreach ($hostnames as $hostname) {
206 
207  $hostname = trim($hostname);
208  foreach ($urls as $url) {
209  foreach ($protocols as $protocol) {
210  $output = Array();
211 
212  if ($this->_running_vars['url_port'] <> '') {
213 
214  // Explode into an array based on the first slash it finds (the '2' in the third
215  // parameter means only two array elements will be created)
216  $path_parts = explode('/', $url, 2);
217 
218  // Add the port to the host name
219  $path_parts[0] .= ':'.$this->_running_vars['url_port'];
220 
221  // Put it back together again
222  $url = implode('/', $path_parts);
223 
224  } //end if
225 
226  $system_log_command = "$path -h $hostname $port $options -m PURGE $protocol://$url";
227  error_log('Running: '.$system_log_command);
228  exec(escapeshellcmd($system_log_command), $output, $status);
229 
230  // error_log("\n\t"."$path -h $hostname $port $options -m PURGE $protocol://$url \n". serialize($output)."\n");
231  if (empty($output)) {
232  trigger_localised_error('SQUID0002', E_USER_WARNING);
233  } else {
234  // add a system log message if successful
235  $msg = $ms->newMessage();
236  $msg->type = 'purge.squid.cache';
237  $msg->from = 0; // a system message
238 
239  $command_str = $system_log_command;
240  $repopulate = ($this->_running_vars['repopulate']) ? translate('yes') : translate('no');
241  $command_str .= "\nRepopulate Cache: ".$repopulate;
242  $cascade = ($this->_running_vars['cascade']) ? translate('yes') : translate('no');
243  $command_str .= ', Cascade: '.$cascade;
244  $system_log_output = "Output : \n";
245  foreach ($output as $key=>$line) {
246  $system_log_output .= "$line\n";
247  }
248  $msg_reps = Array(
249  'command' => $command_str,
250  'result' => $system_log_output,
251  );
252 
253  $msg->replacements = $msg_reps;
254  $ms->send(Array($msg));
255  }//end if
256 
257  }//end foreach - protocols
258 
259  }//end foreach - urls
260 
261  }//end foreach - squid hostnames
262 
263  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($ms);
264 
265  // re-request the page to recache it
266  if ($this->_running_vars['repopulate']) {
267  foreach ($urls as $url) {
268  $this->requestPage("$protocol://$url", $hostname, $port);
269  }
270  }
271 
272  // add this url to the done array
273  $step_data['message'] = translate('squid_clearing_squid_cache', $url);
274  $this->_running_vars['done_urls'][] = $url;
275 
276  // clean up
277  unset($this->_running_vars['todo_urls'][$index]);
278 
279  }//end if
280 
281  // update progress
282  if (empty($this->_running_vars['todo_urls'])) {
283  $step_data['percent_done'] = 100;
284  $step_data['complete'] = TRUE;
285  } else {
286  $total = count($this->_running_vars['todo_urls']) + count($this->_running_vars['done_urls']);
287  $step_data['percent_done'] =(count($this->_running_vars['done_urls']) / $total) * 100;
288  $step_data['complete'] = FALSE;
289  }
290  return TRUE;
291 
292  }//end processClearCache()
293 
294 
305  public function requestPage($url, $host, $port)
306  {
307  // set the correct port if it isn't the default one
308  if ($port != '') {
309  // port variable contains '-p ' which needs to be stripped
310  $port = ':'.trim(str_replace('-p','', $port));
311  }
312 
313  // set correct cacheable headers
314  $headers = Array(
315  'Cache-Control: public',
316  'Pragma: cache',
317  );
318 
319  $options = array(
320  'PROXY' => $host.$port,
321  );
322 
323  ob_start();
324  $response = fetch_url($url, $options, $headers, FALSE);
325  ob_end_clean();
326 
327  }//end requestPage()
328 
329 
330 }//end class
331 
332 
333 ?>