27 error_reporting(E_ALL);
28 if ((php_sapi_name() !=
'cli')) {
29 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
32 require_once
'Console/Getopt.php';
34 $shortopt =
'd:p:s:q::f:';
35 $longopt = Array(
'enable-rollback',
'disable-rollback',
'reset-rollback',
'delete-redundant-entries');
37 $args = Console_Getopt::readPHPArgv();
39 $options = Console_Getopt::getopt($args, $shortopt, $longopt);
41 if ($options instanceof PEAR_Error) {
45 if (empty($options[0])) usage();
50 $ENABLE_ROLLBACK = FALSE;
51 $DISABLE_ROLLBACK = FALSE;
52 $RESET_ROLLBACK = FALSE;
53 $DELETE_REDUNDANT_ENTRIES = FALSE;
56 $valid_option = FALSE;
57 foreach ($options[0] as $option) {
61 if (!empty($ROLLBACK_DATE)) usage();
62 if (empty($option[1])) usage();
63 if (!preg_match(
'|^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}$|', $option[1])) {
66 $ROLLBACK_DATE = $option[1];
71 if (!empty($ROLLBACK_DATE)) usage();
72 if (empty($option[1])) usage();
74 if (!preg_match(
'|^(\d+)([hdwmy])$|', $option[1], $matches)) {
78 $time_num = (int)$matches[1];
80 switch ($matches[2]) {
91 $time_units =
'month';
97 if ($time_num > 1) $time_units .=
's';
98 $ROLLBACK_DATE = date(
'Y-m-d H:i:s', strtotime(
'-'.$time_num.
' '.$time_units));
103 if (!empty($PURGE_FV_DATE)) usage();
104 if (empty($option[1])) usage();
106 if (!preg_match(
'|^(\d+)([hdwmy])$|', $option[1], $matches)) {
110 $time_num = (int)$matches[1];
112 switch ($matches[2]) {
114 $time_units =
'hour';
120 $time_units =
'week';
123 $time_units =
'month';
126 $time_units =
'year';
129 if ($time_num > 1) $time_units .=
's';
130 $PURGE_FV_DATE = date(
'Y-m-d H:i:s', strtotime(
'-'.$time_num.
' '.$time_units));
131 $valid_option = TRUE;
135 if (empty($option[1])) usage();
136 if (!is_dir($option[1])) usage();
137 $SYSTEM_ROOT = $option[1];
140 case '--enable-rollback':
141 if ($DISABLE_ROLLBACK || $RESET_ROLLBACK || $DELETE_REDUNDANT_ENTRIES) {
144 $ENABLE_ROLLBACK = TRUE;
145 $valid_option = TRUE;
148 case '--disable-rollback':
149 if ($ENABLE_ROLLBACK || $RESET_ROLLBACK || $DELETE_REDUNDANT_ENTRIES) {
152 $DISABLE_ROLLBACK = TRUE;
153 $valid_option = TRUE;
156 case '--reset-rollback':
157 if ($ENABLE_ROLLBACK || $DISABLE_ROLLBACK || $DELETE_REDUNDANT_ENTRIES) {
160 $RESET_ROLLBACK = TRUE;
161 $valid_option = TRUE;
164 case '--delete-redundant-entries':
165 if ($ENABLE_ROLLBACK || $DISABLE_ROLLBACK || $RESET_ROLLBACK) {
168 $DELETE_REDUNDANT_ENTRIES = TRUE;
169 $valid_option = TRUE;
176 echo
'Invalid option - '.$option[0];
182 if (!$valid_option) {
186 if ($ENABLE_ROLLBACK || $DISABLE_ROLLBACK || $RESET_ROLLBACK) {
187 if (!empty($ROLLBACK_DATE) || !empty($PURGE_FV_DATE)) {
190 $ROLLBACK_DATE = date(
'Y-m-d H:i:s');
193 if (!empty($ROLLBACK_DATE) && !empty($PURGE_FV_DATE)) {
197 if (empty($SYSTEM_ROOT)) {
198 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
203 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
204 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
209 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
210 require_once SQ_INCLUDE_PATH.
'/rollback_management.inc';
211 require SQ_DATA_PATH.
'/private/db/table_columns.inc';
215 $SQ_TABLE_COLUMNS = $tables;
217 $tables = get_rollback_table_names();
224 if ($DELETE_REDUNDANT_ENTRIES) {
225 echo
"\nIMPORTANT: You have selected the option to remove all the redundant entries in the Rollback table.";
226 echo
"\nThis will remove all the redundant entries for Cron Manager from the rollback tables.";
227 echo
"\nAre you sure you want to proceed (Y/N)? ";
229 $choice = rtrim(fgets(STDIN, 4094));
230 if (strtolower($choice) !=
'y') {
231 echo
"\nScript aborted.\n";
238 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
239 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
242 if ($PURGE_FV_DATE) {
243 $affected_rows = purge_file_versioning($PURGE_FV_DATE);
245 echo $affected_rows.
' FILE VERSIONING FILES AND ENTRIES DELETED'.
"\n";
249 if ($RESET_ROLLBACK) {
251 foreach ($tables as $table) {
252 truncate_rollback_entries($table);
254 echo
'Rollback table sq_rb_'.$table.
" truncated.\n";
257 $ENABLE_ROLLBACK = TRUE;
258 echo
"\nEnabling rollback...\n\n";
261 if ($ENABLE_ROLLBACK) {
262 $rollback_check = rollback_found($tables);
263 if ($rollback_check) {
264 echo
"Rollback has been enabled before, it doesn't need to be enabled again.\n";
269 foreach ($tables as $table) {
271 if ($ENABLE_ROLLBACK) {
272 $affected_rows = open_rollback_entries($table, $SQ_TABLE_COLUMNS, $ROLLBACK_DATE);
274 echo $affected_rows.
' ENTRIES OPENED IN sq_rb_'.$table.
"\n";
279 if ($DISABLE_ROLLBACK) {
280 $affected_rows = close_rollback_entries($table, $ROLLBACK_DATE);
282 echo $affected_rows.
' ENTRIES CLOSED IN sq_rb_'.$table.
"\n";
287 if ($ROLLBACK_DATE) {
288 $affected_rows = delete_rollback_entries($table, $ROLLBACK_DATE);
290 echo $affected_rows.
' ENTRIES DELETED IN sq_rb_'.$table.
"\n";
293 $affected_rows = align_rollback_entries($table, $ROLLBACK_DATE);
295 echo $affected_rows.
' ENTRIES ALIGNED IN sq_rb_'.$table.
"\n";
300 if ($DELETE_REDUNDANT_ENTRIES) {
301 $affected_rows = delete_redundant_rollback_entries($table);
303 echo $affected_rows.
' ENTRIES REMOVED IN sq_rb_'.$table.
"\n";
309 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
310 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
321 echo
"\nUSAGE: rollback_management.php -s <system_root> [-d <date>] [-p <period>] [--enable-rollback] [--disable-rollback] [--reset-rollback] [--delete-redundant-entries] [-q --quiet]\n".
322 "--enable-rollback Enables rollback in MySource Matrix\n".
323 "--disable-rollback Disables rollback in MySource Matrix\n".
324 "--reset-rollback Removes all rollback information and enables rollback in MySource Matrix\n".
325 "--delete-redundant-entries Removes all the unnecessary Cron Manager asset rollback entries\n".
326 "-q No output will be sent\n".
327 "-d The date to set rollback entries to in the format YYYY-MM-DD HH:MM:SS\n".
328 "-p The period to purge rollback entries before\n".
329 "-f The period to purge file versioning entries and files before\n".
330 "(For -p and -f, the period is in the format nx where n is the number of units and x is one of:\n".
331 " h - hours\t\n d - days\t\n w - weeks\t\n m - months\t\n y - years\n".
332 "\nNOTE: only one of [-d -p -f --enable-rollback --disable-rollback --reset-rollback] option is allowed to be specified\n";