41 function truncate_rollback_entries($table_name)
43 $sql =
'TRUNCATE TABLE sq_rb_'.$table_name;
46 }
catch (Exception $e) {
47 throw new Exception(
'Unable to truncate table '.$table_name.
' due to the following error: '.$e->getMessage());
62 function close_rollback_entries($table_name, $date)
64 $sql =
'UPDATE sq_rb_'.$table_name.
' SET sq_eff_to = :date1 WHERE sq_eff_to IS NULL';
71 }
catch (Exception $e) {
72 throw new Exception(
'Unable to update rollback table '.$table_name.
' due to the following error:'.$e->getMessage());
75 return $affected_rows;
90 function open_rollback_entries($table_name, $table_columns, $date)
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;
102 }
catch (Exception $e) {
103 throw new Exception(
'Unable to insert into rollback table '.$table_name.
' due to the following error:'.$e->getMessage());
106 return $affected_rows;
121 function align_rollback_entries($table_name, $date)
123 $sql =
'UPDATE sq_rb_'.$table_name.
'
124 SET sq_eff_from = :date1
125 WHERE sq_eff_from < :date2';
133 }
catch (Exception $e) {
134 throw new Exception(
'Unable to update rollback table '.$table_name.
' due to the following error:'.$e->getMessage());
137 return $affected_rows;
151 function delete_rollback_entries($table_name, $date)
153 $sql =
'DELETE FROM sq_rb_'.$table_name.
'
154 WHERE sq_eff_to <= :date1';
161 }
catch (Exception $e) {
162 throw new Exception(
'Unable to delete rollback table '.$table_name.
' due to the following error:'.$e->getMessage());
165 return $affected_rows;
178 function delete_redundant_rollback_entries($table_name)
180 $relevant_tables = Array(
184 'ast_attr_val' => Array(
190 if (!in_array($table_name, array_keys($relevant_tables))) {
196 $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'cron_manager'),
201 foreach($assets as $asset) {
204 $sql =
'SELECT * FROM sq_rb_'.$table_name.
'
205 WHERE assetid=:assetid';
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());
216 foreach($result as $index => $row) {
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.
'"');
223 $key .= $row[$col].
'.';
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;
232 $org_rows[$key][
'sq_eff_from'] = $row[
'sq_eff_from'];
233 $org_rows[$key][
'index'] = $index;
238 if (!empty($org_rows)) {
240 foreach($org_rows as $org_row) {
241 $rb_row = $result[$org_row[
'index']];
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;
249 $sql = 'UPDATE sq_rb_
'.$table_name.'
250 SET sq_eff_to = NULL
'.
253 $query = MatrixDAL::preparePdoQuery($sql);
254 foreach($relevant_tables[$table_name] as $col) {
255 MatrixDAL::bindValueToPdo($query, $col, $rb_row[$col]);
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());
262 unset($result[$org_row['index
']]);
266 foreach($result as $row) {
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;
273 $sql = 'DELETE FROM sq_rb_
'.$table_name.' '.
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);
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());
290 return $affected_rows;
292 }//end delete_redundant_rollback_entries()
301 function get_rollback_table_names()
303 $table_names = Array();
305 $packages_installed = $GLOBALS['SQ_SYSTEM
']->getInstalledPackages();
307 if (empty($packages_installed)) return Array();
309 foreach ($packages_installed as $package_array) {
310 if ($package_array['code_name
'] == '__core__
') {
311 $table_file = SQ_CORE_PACKAGE_PATH.'/tables.xml
';
313 $table_file = SQ_PACKAGES_PATH.'/
'.$package_array['code_name
'].'/tables.xml
';
316 if (!file_exists($table_file)) continue;
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());
324 foreach ($root->children() as $child) {
325 $first_child_name = $child->getName();
328 if ($root->getName() != 'schema
' || $first_child_name != 'tables
') {
329 trigger_error('Invalid table schema
for file
"'.$table_file.'"', E_USER_ERROR);
332 $table_root = $child;
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);
344 }//end get_rollback_table_names()
356 function purge_file_versioning($date, $num_rows=0)
358 $history_table = 'sq_file_vers_history
';
359 $file_table = 'sq_file_vers_file
';
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
';
364 $sql = db_extras_modify_limit_clause($sql, MatrixDAL::getDbType(), $num_rows);
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());
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'];
389 if (($num_rows > 0) && (!empty($result))) {
390 foreach ($result as $row) {
391 $chunk[] =
'\''.((string) $row[
'fileid']).
'\'';
394 $chunk_query =
' AND fileid IN ('.implode(
', ',$chunk).
')';
398 $sql =
'DELETE FROM '.$history_table.
' WHERE to_date <= :date1'.$chunk_query;
405 }
catch (Exception $e) {
406 throw new Exception(
'Unable to delete from rollback table '.$table_name.
' due to the following error:'.$e->getMessage());
409 return $affected_rows;
426 function rollback_found(array $tables=array())
428 if (empty($tables)) {
433 foreach ($tables as $table) {
434 $sql =
"SELECT 1 FROM sq_rb_".$table.
" WHERE sq_eff_to IS NULL";
436 $queries[] =
"(".$sql.
")";
438 $query =
"SELECT COUNT(*) FROM (".implode(
" UNION ALL ", $queries).
") rbcheck";