Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cron_job_funnelback_log_rotation.inc
1 <?php
17 require_once SQ_CORE_PACKAGE_PATH.'/system/cron/cron_job/cron_job.inc';
18 require_once SQ_FUDGE_PATH.'/db_extras/db_extras.inc';
19 
33 {
34 
35 
42  function __construct($assetid=0)
43  {
44  parent::__construct($assetid);
45 
46  }//end constructor
47 
48 
60  function create(&$link)
61  {
62  require_once SQ_CORE_PACKAGE_PATH.'/system/system_asset_fns.inc';
63  if (!system_asset_fns_create_pre_check($this)) {
64  return FALSE;
65  }
66  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
67  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
68 
69  if ($linkid = parent::create($link)) {
70  if (!system_asset_fns_create_cleanup($this)) {
71  $linkid = FALSE;
72  }
73  }
74 
75  if ($linkid) {
76  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
77  } else {
78  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
79  }
80 
81  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
82  return $linkid;
83 
84  }//end create()
85 
86 
96  function _getName($short_name=FALSE)
97  {
98  return $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name');
99 
100  }//end _getName()
101 
102 
109  function canClone()
110  {
111  return FALSE;
112 
113  }//end canClone()
114 
115 
127  function morph($new_type_code)
128  {
129  trigger_localised_error('CRON0024', E_USER_WARNING, $GLOBALS['SQ_SYSTEM']->am->getTypeInfo($this->type(), 'name'));
130  return FALSE;
131 
132  }//end morph()
133 
134 
143  function canDelete()
144  {
145  return FALSE;
146 
147  }//end canDelete()
148 
149 
163  function _exec(&$msg)
164  {
165  $fm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('funnelback_manager');
166 
167  $collections = $fm->getCollections();
168  $rotated = Array();
169 
170  // Rotate the logs for each collection
171  foreach ($collections as $collection_id => $collection) {
172  $log_path = $fm->getCollectionDataPath($collection_id, 'log');
173 
174  $log_files = glob($log_path.'/*.log');
175  if (!empty($log_files)) {
176  foreach ($log_files as $log_file) {
177  $log_name = basename($log_file);
178 
179  // Click logs are not rotated yet, remove this line if needed
180  if ($log_name == 'click.log') continue;
181 
182  // Archive
183  $archive_name = $log_name.'.'.date('Ymd');
184  $archive_path = $log_path.'/'.$archive_name;
185  $alternative = 1;
186  while (file_exists($archive_path)) {
187  $archive_path = $log_path.'/'.$archive_name.'.'.((string) $alternative);
188  $alternative++;
189  }//end while
190  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
191  move_file($log_file, $archive_path);
192  touch($log_file);
193 
194  // Keep a list for the report to work against
195  $rotated[$collection_id] = $archive_path;
196 
197  // Clean up
198  $period = $fm->attr('keep_log_period');
199  if (!empty($period)) {
200  $expiry_date = strtotime('-'.((int) $period).' months');
201  $archive_logs = glob($log_path.'/'.$log_name.'.*');
202  foreach ($archive_logs as $archive_log) {
203  $found_archive_log_name = basename($archive_log);
204  preg_match('/log\.([0-9]+)/i', $found_archive_log_name, $match);
205  $log_date = array_get_index($match, 1, 0);
206  if (!empty($log_date)) {
207  $log_date_expiry = strtotime($log_date);
208  if ($log_date_expiry < $expiry_date) {
209  // Log expired, removing....
210  unlink($archive_log);
211 
212  // Also remember to remove from rotated (no point trying to generate a report if a log is missing)
213  if (isset($rotated[$collection_id]) && $rotated[$collection_id] == $found_archive_log_name) {
214  unset($rotated[$collection_id]);
215  }//end if
216  }//end if
217  }//end if
218  }//end foreach
219  }//end if
220  }//end foreach
221  }//end if
222  }//end foreach
223 
224  if (!empty($rotated)) {
225  // Logs were rotated, now to generate some reports
226  $log_reports = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('funnelback_search_log_report', FALSE, TRUE);
227 
228  // start up a hipo for each report
229  $hh = $GLOBALS['SQ_SYSTEM']->getHipoHerder();
230 
231  foreach ($log_reports as $reportid => $info) {
232  $report = $GLOBALS['SQ_SYSTEM']->am->getAsset($reportid);
233  $vars = Array(
234  'assetid' => $reportid,
235  'report' => $report,
236  'logs' => $rotated,
237  );
238  $status = $hh->freestyleHIPO('hipo_job_generate_report', $vars);
239  }//end foreach
240  }//end if
241 
242  return SQ_CRON_JOB_COMPLETED;
243 
244  }//end _exec()
245 
246 
257  function run()
258  {
259  $res = parent::run();
260  if ($res & SQ_CRON_JOB_REMOVE) {
261  $res = $res - SQ_CRON_JOB_REMOVE;
262  }
263  return $res;
264 
265  }//end run()
266 
267 
268 }//end class
269 
270 ?>