Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
rollback_management.php
1 <?php
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);
30 }
31 
32 require_once 'Console/Getopt.php';
33 
34 $shortopt = 'd:p:s:q::f:';
35 $longopt = Array('enable-rollback', 'disable-rollback', 'reset-rollback', 'delete-redundant-entries');
36 
37 $args = Console_Getopt::readPHPArgv();
38 array_shift($args);
39 $options = Console_Getopt::getopt($args, $shortopt, $longopt);
40 
41 if ($options instanceof PEAR_Error) {
42  usage();
43 }
44 
45 if (empty($options[0])) usage();
46 
47 $PURGE_FV_DATE = '';
48 $ROLLBACK_DATE = '';
49 $SYSTEM_ROOT = '';
50 $ENABLE_ROLLBACK = FALSE;
51 $DISABLE_ROLLBACK = FALSE;
52 $RESET_ROLLBACK = FALSE;
53 $DELETE_REDUNDANT_ENTRIES = FALSE;
54 $QUIET = FALSE;
55 
56 $valid_option = FALSE;
57 foreach ($options[0] as $option) {
58 
59  switch ($option[0]) {
60  case 'd':
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])) {
64  usage();
65  }
66  $ROLLBACK_DATE = $option[1];
67  $valid_option = TRUE;
68  break;
69 
70  case 'p':
71  if (!empty($ROLLBACK_DATE)) usage();
72  if (empty($option[1])) usage();
73  $matches = Array();
74  if (!preg_match('|^(\d+)([hdwmy])$|', $option[1], $matches)) {
75  usage();
76  }
77 
78  $time_num = (int)$matches[1];
79  $time_units = '';
80  switch ($matches[2]) {
81  case 'h' :
82  $time_units = 'hour';
83  break;
84  case 'd' :
85  $time_units = 'day';
86  break;
87  case 'w' :
88  $time_units = 'week';
89  break;
90  case 'm' :
91  $time_units = 'month';
92  break;
93  case 'y' :
94  $time_units = 'year';
95  break;
96  }
97  if ($time_num > 1) $time_units .= 's';
98  $ROLLBACK_DATE = date('Y-m-d H:i:s', strtotime('-'.$time_num.' '.$time_units));
99  $valid_option = TRUE;
100  break;
101 
102  case 'f':
103  if (!empty($PURGE_FV_DATE)) usage();
104  if (empty($option[1])) usage();
105  $matches = Array();
106  if (!preg_match('|^(\d+)([hdwmy])$|', $option[1], $matches)) {
107  usage();
108  }
109 
110  $time_num = (int)$matches[1];
111  $time_units = '';
112  switch ($matches[2]) {
113  case 'h' :
114  $time_units = 'hour';
115  break;
116  case 'd' :
117  $time_units = 'day';
118  break;
119  case 'w' :
120  $time_units = 'week';
121  break;
122  case 'm' :
123  $time_units = 'month';
124  break;
125  case 'y' :
126  $time_units = 'year';
127  break;
128  }
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;
132  break;
133 
134  case 's':
135  if (empty($option[1])) usage();
136  if (!is_dir($option[1])) usage();
137  $SYSTEM_ROOT = $option[1];
138  break;
139 
140  case '--enable-rollback':
141  if ($DISABLE_ROLLBACK || $RESET_ROLLBACK || $DELETE_REDUNDANT_ENTRIES) {
142  usage();
143  }
144  $ENABLE_ROLLBACK = TRUE;
145  $valid_option = TRUE;
146  break;
147 
148  case '--disable-rollback':
149  if ($ENABLE_ROLLBACK || $RESET_ROLLBACK || $DELETE_REDUNDANT_ENTRIES) {
150  usage();
151  }
152  $DISABLE_ROLLBACK = TRUE;
153  $valid_option = TRUE;
154  break;
155 
156  case '--reset-rollback':
157  if ($ENABLE_ROLLBACK || $DISABLE_ROLLBACK || $DELETE_REDUNDANT_ENTRIES) {
158  usage();
159  }
160  $RESET_ROLLBACK = TRUE;
161  $valid_option = TRUE;
162  break;
163 
164  case '--delete-redundant-entries':
165  if ($ENABLE_ROLLBACK || $DISABLE_ROLLBACK || $RESET_ROLLBACK) {
166  usage();
167  }
168  $DELETE_REDUNDANT_ENTRIES = TRUE;
169  $valid_option = TRUE;
170  break;
171 
172  case 'q':
173  $QUIET = TRUE;
174  break;
175  default:
176  echo 'Invalid option - '.$option[0];
177  usage();
178  }//end switch
179 
180 }//end foreach arguments
181 
182 if (!$valid_option) {
183  usage();
184 }
185 
186 if ($ENABLE_ROLLBACK || $DISABLE_ROLLBACK || $RESET_ROLLBACK) {
187  if (!empty($ROLLBACK_DATE) || !empty($PURGE_FV_DATE)) {
188  usage();
189  }
190  $ROLLBACK_DATE = date('Y-m-d H:i:s');
191 }
192 
193 if (!empty($ROLLBACK_DATE) && !empty($PURGE_FV_DATE)) {
194  usage();
195 }
196 
197 if (empty($SYSTEM_ROOT)) {
198  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
199  usage();
200  exit();
201 }
202 
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";
205  usage();
206  exit();
207 }
208 
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';
212 
213 // get the tables from table_columns into a var
214 // that will not clash with other vars
215 $SQ_TABLE_COLUMNS = $tables;
216 
217 $tables = get_rollback_table_names();
218 
219 // the number of rows to limit to as to avoid an out of memory error
220 // MUST be greater than 1
221 $LIMIT_ROWS = 500;
222 
223 // Last chance to stop from removing redundnet rollback entries
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)? ";
228 
229  $choice = rtrim(fgets(STDIN, 4094));
230  if (strtolower($choice) != 'y') {
231  echo "\nScript aborted.\n";
232  exit;
233  }
234 
235  echo "\n";
236 }
237 
238 $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
239 $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
240 $db = MatrixDAL::getDb();
241 
242 if ($PURGE_FV_DATE) {
243  $affected_rows = purge_file_versioning($PURGE_FV_DATE);
244  if (!$QUIET) {
245  echo $affected_rows.' FILE VERSIONING FILES AND ENTRIES DELETED'."\n";
246  }
247 } else {
248 
249  if ($RESET_ROLLBACK) {
250  // truncate toll back tables here then enable rollback
251  foreach ($tables as $table) {
252  truncate_rollback_entries($table);
253  if (!$QUIET) {
254  echo 'Rollback table sq_rb_'.$table." truncated.\n";
255  }
256  }
257  $ENABLE_ROLLBACK = TRUE;
258  echo "\nEnabling rollback...\n\n";
259  }
260 
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";
265  exit;
266  }
267  }
268 
269  foreach ($tables as $table) {
270 
271  if ($ENABLE_ROLLBACK) {
272  $affected_rows = open_rollback_entries($table, $SQ_TABLE_COLUMNS, $ROLLBACK_DATE);
273  if (!$QUIET) {
274  echo $affected_rows.' ENTRIES OPENED IN sq_rb_'.$table."\n";
275  }
276 
277  continue;
278  }
279  if ($DISABLE_ROLLBACK) {
280  $affected_rows = close_rollback_entries($table, $ROLLBACK_DATE);
281  if (!$QUIET) {
282  echo $affected_rows.' ENTRIES CLOSED IN sq_rb_'.$table."\n";
283  }
284 
285  continue;
286  }
287  if ($ROLLBACK_DATE) {
288  $affected_rows = delete_rollback_entries($table, $ROLLBACK_DATE);
289  if (!$QUIET) {
290  echo $affected_rows.' ENTRIES DELETED IN sq_rb_'.$table."\n";
291  }
292 
293  $affected_rows = align_rollback_entries($table, $ROLLBACK_DATE);
294  if (!$QUIET) {
295  echo $affected_rows.' ENTRIES ALIGNED IN sq_rb_'.$table."\n";
296  }
297 
298  continue;
299  }//end if
300  if ($DELETE_REDUNDANT_ENTRIES) {
301  $affected_rows = delete_redundant_rollback_entries($table);
302  if (!$QUIET) {
303  echo $affected_rows.' ENTRIES REMOVED IN sq_rb_'.$table."\n";
304  }
305  }
306  }//end foreach
307 }//end else
308 
309 $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
310 $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
311 
312 
319 function usage()
320 {
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";
333  exit();
334 
335 }//end usage()
336 
337 
338 ?>