Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
rollback_management.inc
1 <?php
41 function truncate_rollback_entries($table_name)
42 {
43  $sql = 'TRUNCATE TABLE sq_rb_'.$table_name;
44  try {
45  $result = MatrixDAL::executeSql($sql);
46  } catch (Exception $e) {
47  throw new Exception('Unable to truncate table '.$table_name.' due to the following error: '.$e->getMessage());
48  }//end try catch
49 
50 }//end truncate_rollback_entries()
51 
52 
62 function close_rollback_entries($table_name, $date)
63 {
64  $sql = 'UPDATE sq_rb_'.$table_name.' SET sq_eff_to = :date1 WHERE sq_eff_to IS NULL';
65  $affected_rows = 0;
66 
67  try {
68  $query = MatrixDAL::preparePdoQuery($sql);
69  MatrixDAL::bindValueToPdo($query, 'date1', $date);
70  $affected_rows = MatrixDAL::execPdoQuery($query);
71  } catch (Exception $e) {
72  throw new Exception('Unable to update rollback table '.$table_name.' due to the following error:'.$e->getMessage());
73  }//end try catch
74 
75  return $affected_rows;
76 
77 }//end close_rollback_entries()
78 
79 
90 function open_rollback_entries($table_name, $table_columns, $date)
91 {
92  $columns = $table_columns[$table_name]['columns'];
93  $sql = 'INSERT INTO sq_rb_'.$table_name.' ('.implode(', ', $columns).
94  ', sq_eff_from, sq_eff_to)
95  SELECT '.implode(',', $columns).',:date1 , NULL FROM sq_'.$table_name;
96  $affected_rows = 0;
97 
98  try {
99  $query = MatrixDAL::preparePdoQuery($sql);
100  MatrixDAL::bindValueToPdo($query, 'date1', $date);
101  $affected_rows = MatrixDAL::execPdoQuery($query);
102  } catch (Exception $e) {
103  throw new Exception('Unable to insert into rollback table '.$table_name.' due to the following error:'.$e->getMessage());
104  }//end try catch
105 
106  return $affected_rows;
107 
108 }//end open_rollback_entries()
109 
110 
121 function align_rollback_entries($table_name, $date)
122 {
123  $sql = 'UPDATE sq_rb_'.$table_name.'
124  SET sq_eff_from = :date1
125  WHERE sq_eff_from < :date2';
126  $affected_rows = 0;
127 
128  try {
129  $query = MatrixDAL::preparePdoQuery($sql);
130  MatrixDAL::bindValueToPdo($query, 'date1', $date);
131  MatrixDAL::bindValueToPdo($query, 'date2', $date);
132  $affected_rows = MatrixDAL::execPdoQuery($query);
133  } catch (Exception $e) {
134  throw new Exception('Unable to update rollback table '.$table_name.' due to the following error:'.$e->getMessage());
135  }//end try catch
136 
137  return $affected_rows;
138 
139 }//end align_rollback_entries()
140 
141 
151 function delete_rollback_entries($table_name, $date)
152 {
153  $sql = 'DELETE FROM sq_rb_'.$table_name.'
154  WHERE sq_eff_to <= :date1';
155  $affected_rows = 0;
156 
157  try {
158  $query = MatrixDAL::preparePdoQuery($sql);
159  MatrixDAL::bindValueToPdo($query, 'date1', $date);
160  $affected_rows = MatrixDAL::execPdoQuery($query);
161  } catch (Exception $e) {
162  throw new Exception('Unable to delete rollback table '.$table_name.' due to the following error:'.$e->getMessage());
163  }//end try catch
164 
165  return $affected_rows;
166 
167 }//end delete_rollback_entries()
168 
169 
178 function delete_redundant_rollback_entries($table_name)
179 {
180  $relevant_tables = Array(
181  'ast' => Array(
182  'assetid',
183  ),
184  'ast_attr_val' => Array(
185  'assetid',
186  'attrid',
187  'contextid',
188  ),
189  );
190  if (!in_array($table_name, array_keys($relevant_tables))) {
191  return 0;
192  }
193 
194  // For now, we only clean entries for Cron Manager
195  $assets = Array(
196  $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cron_manager'),
197  );
198 
199 
200  $affected_rows = 0;
201  foreach($assets as $asset) {
202 
203  // Get all the entries for the given asset
204  $sql = 'SELECT * FROM sq_rb_'.$table_name.'
205  WHERE assetid=:assetid';
206  try {
207  $query = MatrixDAL::preparePdoQuery($sql);
208  MatrixDAL::bindValueToPdo($query, 'assetid', $asset->id);
209  $result = MatrixDAL::executePdoAssoc($query);
210  } catch (Exception $e) {
211  throw new Exception('Unable get asset info from table '.$table_name.' for assetid #'.$asset->id.' due to the following error: '.$e->getMessage());
212  }//end try catch
213 
214  // Get the very first rollback entries in the table
215  $org_rows = Array();
216  foreach($result as $index => $row) {
217  $key = '';
218  foreach($relevant_tables[$table_name] as $col) {
219  if (!isset($row[$col])) {
220  trigger_error('Expected column "'.$col.'" not found in rollback table "'.$table_name.'"');
221  return 0;
222  }
223  $key .= $row[$col].'.';
224  }//end foreach
225  if (!empty($key)) {
226  if (!empty($org_rows[$key]['sq_eff_from'])) {
227  if (strtotime($row['sq_eff_from']) < strtotime($org_rows[$key]['sq_eff_from'])) {
228  $org_rows[$key]['sq_eff_from'] = $row['sq_eff_from'];
229  $org_rows[$key]['index'] = $index;
230  }
231  } else {
232  $org_rows[$key]['sq_eff_from'] = $row['sq_eff_from'];
233  $org_rows[$key]['index'] = $index;
234  }
235  }
236  }//end foreach
237 
238  if (!empty($org_rows)) {
239  // Keep the very first entries and delete the rest
240  foreach($org_rows as $org_row) {
241  $rb_row = $result[$org_row['index']];
242 
243  // Open these rollback entires
244  $where ='WHERE sq_eff_from=\''.$rb_row['sq_eff_from'].'\' AND '.
245  (is_null($rb_row['sq_eff_to']) ? 'sq_eff_to IS NULL ' : 'sq_eff_to=\''.$rb_row['sq_eff_to'].'\' ');
246  foreach($relevant_tables[$table_name] as $col) {
247  $where .= ' AND '.$col.'=:'.$col;
248  }//end foreach
249  $sql = 'UPDATE sq_rb_'.$table_name.'
250  SET sq_eff_to = NULL '.
251  $where;
252  try {
253  $query = MatrixDAL::preparePdoQuery($sql);
254  foreach($relevant_tables[$table_name] as $col) {
255  MatrixDAL::bindValueToPdo($query, $col, $rb_row[$col]);
256  }
257  MatrixDAL::execPdoQuery($query);
258  } catch (Exception $e) {
259  throw new Exception('Unable to update rollback table '.$table_name.' due to the following error:'.$e->getMessage());
260  }//end try catch
261 
262  unset($result[$org_row['index']]);
263  }//end foreach
264 
265  // Get rid of rest
266  foreach($result as $row) {
267 
268  $where ='WHERE sq_eff_from=\''.$row['sq_eff_from'].'\' AND '.
269  (is_null($row['sq_eff_to']) ? 'sq_eff_to IS NULL ' : 'sq_eff_to=\''.$row['sq_eff_to'].'\' ');
270  foreach($relevant_tables[$table_name] as $col) {
271  $where .= ' AND '.$col.'=:'.$col;
272  }//end foreach
273  $sql = 'DELETE FROM sq_rb_'.$table_name.' '.
274  $where;
275  try {
276  $query = MatrixDAL::preparePdoQuery($sql);
277  foreach($relevant_tables[$table_name] as $col) {
278  MatrixDAL::bindValueToPdo($query, $col, $row[$col]);
279  $sql = str_replace(':'.$col, $row[$col], $sql);
280  }
281  $affected_rows += MatrixDAL::execPdoQuery($query);
282  } catch (Exception $e) {
283  throw new Exception('Unable to delete rollback table '.$table_name.' due to the following error:'.$e->getMessage());
284  }
285  }//end foreach
286  }//end if
287 
288  }//end foreach
289 
290  return $affected_rows;
291 
292 }//end delete_redundant_rollback_entries()
293 
294 
301 function get_rollback_table_names()
302 {
303  $table_names = Array();
304 
305  $packages_installed = $GLOBALS['SQ_SYSTEM']->getInstalledPackages();
306 
307  if (empty($packages_installed)) return Array();
308 
309  foreach ($packages_installed as $package_array) {
310  if ($package_array['code_name'] == '__core__') {
311  $table_file = SQ_CORE_PACKAGE_PATH.'/tables.xml';
312  } else {
313  $table_file = SQ_PACKAGES_PATH.'/'.$package_array['code_name'].'/tables.xml';
314  }
315 
316  if (!file_exists($table_file)) continue;
317 
318  try {
319  $root = new SimpleXMLElement($table_file, LIBXML_NOCDATA, TRUE);
320  } catch (Exception $e) {
321  throw new Exception('Unable to parse table file : '.$table_file.' due to the following error: '.$e->getMessage());
322  }//end try catch
323 
324  foreach ($root->children() as $child) {
325  $first_child_name = $child->getName();
326  break;
327  }//end foreach
328  if ($root->getName() != 'schema' || $first_child_name != 'tables') {
329  trigger_error('Invalid table schema for file "'.$table_file.'"', E_USER_ERROR);
330  }
331 
332  $table_root = $child;
333 
334  foreach ($table_root->children() as $table_child) {
335  if ((string) $table_child->attributes()->require_rollback) {
336  $table_name = (string) $table_child->attributes()->name;
337  array_push($table_names, $table_name);
338  }//end if
339  }//end foreach
340  }
341 
342  return $table_names;
343 
344 }//end get_rollback_table_names()
345 
346 
356 function purge_file_versioning($date, $num_rows=0)
357 {
358  $history_table = 'sq_file_vers_history';
359  $file_table = 'sq_file_vers_file';
360 
361  // Get all the file versioning entries
362  $sql = 'SELECT * FROM '.$history_table.' h JOIN '.$file_table.' f ON h.fileid = f.fileid WHERE to_date <= :date1';
363  if ($num_rows > 0) {
364  $sql = db_extras_modify_limit_clause($sql, MatrixDAL::getDbType(), $num_rows);
365  }
366 
367  try {
368  $query = MatrixDAL::preparePdoQuery($sql);
369  MatrixDAL::bindValueToPdo($query, 'date1', $date);
370  $result = MatrixDAL::executePdoAssoc($query);
371  } catch (Exception $e) {
372  throw new Exception('Unable to select from rollback table '.$table_name.' due to the following error:'.$e->getMessage());
373  }//end try catch
374 
375  $count = 0;
376 
377  // Delete the files - if it isn't there then don't worry because it means the
378  // DB entry shouldn't have been there in the first place
379  foreach ($result as $row) {
380  $ffv_file = SQ_SYSTEM_ROOT.'/data/file_repository/'.$row['path'].'/'.$row['filename'].',ffv'.$row['version'];
381  unlink($ffv_file);
382  $count++;
383  }
384 
385  // If we're chunkified, then ensure that only the chunked entries are dealt with
386  $chunk = Array();
387  $chunk_query = '';
388  reset($result);
389  if (($num_rows > 0) && (!empty($result))) {
390  foreach ($result as $row) {
391  $chunk[] = '\''.((string) $row['fileid']).'\'';
392  }
393 
394  $chunk_query = ' AND fileid IN ('.implode(', ',$chunk).')';
395  }
396 
397  // Now delete the entries from the table
398  $sql = 'DELETE FROM '.$history_table.' WHERE to_date <= :date1'.$chunk_query;
399  $affected_rows = 0;
400 
401  try {
402  $query = MatrixDAL::preparePdoQuery($sql);
403  MatrixDAL::bindValueToPdo($query, 'date1', $date);
404  $affected_rows = MatrixDAL::execPdoQuery($query);
405  } catch (Exception $e) {
406  throw new Exception('Unable to delete from rollback table '.$table_name.' due to the following error:'.$e->getMessage());
407  }//end try catch
408 
409  return $affected_rows;
410 
411 }//end purge_file_versioning()
412 
426 function rollback_found(array $tables=array())
427 {
428  if (empty($tables)) {
429  return NULL;
430  }
431 
432  $queries = array();
433  foreach ($tables as $table) {
434  $sql = "SELECT 1 FROM sq_rb_".$table." WHERE sq_eff_to IS NULL";
435  $sql = db_extras_modify_limit_clause($sql, MatrixDAL::getDbType(), 1);
436  $queries[] = "(".$sql.")";
437  }
438  $query = "SELECT COUNT(*) FROM (".implode(" UNION ALL ", $queries).") rbcheck";
439  $result = MatrixDAL::preparePdoQuery($query);
440  $count = MatrixDAL::executePdoOne($result);
441  if ($count > 0) {
442  return TRUE;
443  }
444  return FALSE;
445 }
446 
447 ?>