Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
remove_internal_message.php
1 <?php
24 error_reporting(E_ALL);
25 if ((php_sapi_name() != 'cli')) {
26  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
27 }
28 
29 require_once 'Console/Getopt.php';
30 
31 $shortopt = 's:p:f:t:y:u:';
32 $longopt = Array('quiet', 'show-query-only');
33 
34 $args = Console_Getopt::readPHPArgv();
35 array_shift($args);
36 $options = Console_Getopt::getopt($args, $shortopt, $longopt);
37 
38 if (empty($options[0])) usage();
39 
40 $SYSTEM_ROOT = '';
41 $PERIOD = '';
42 $USER_FROM = '';
43 $USER_TO = '';
44 $MSG_TYPE = '';
45 $MSG_STATUS = '';
46 $SHOW_QUERY_ONLY = FALSE;
47 $QUIET = FALSE;
48 
49 foreach ($options[0] as $option) {
50 
51  switch ($option[0]) {
52 
53  case 's':
54  if (empty($option[1])) usage();
55  if (!is_dir($option[1])) usage();
56  $SYSTEM_ROOT = $option[1];
57  break;
58 
59  case 'p':
60  if (empty($option[1])) usage();
61  $matches = Array();
62  if (!preg_match('|^(\d+)([hdwmy])$|', $option[1], $matches)) {
63  usage();
64  }
65 
66  $time_num = (int) $matches[1];
67  $time_units = '';
68  switch ($matches[2]) {
69  case 'h' :
70  $time_units = 'hour';
71  break;
72  case 'd' :
73  $time_units = 'day';
74  break;
75  case 'w' :
76  $time_units = 'week';
77  break;
78  case 'm' :
79  $time_units = 'month';
80  break;
81  case 'y' :
82  $time_units = 'year';
83  break;
84  }
85  if ($time_num > 1) $time_units .= 's';
86 
87  $PERIOD = date('Y-m-d H:i:s', strtotime('-'.$time_num.' '.$time_units));
88  break;
89 
90  case 'f':
91  case 't':
92  $value = (string) $option[1];
93  if ($value != '0' && empty($value)) usage();
94  $var = ($option[0] == 'f') ? '$USER_FROM' : '$USER_TO';
95  eval($var.' = $option[1];');
96  break;
97 
98  case 'y':
99  if (empty($option[1])) usage();
100  $MSG_TYPE = $option[1];
101  break;
102 
103  case 'u':
104  if (empty($option[1])) usage();
105  //if (!in_array($option[1], Array('U', 'D', 'R'))) usage();
106  if (!preg_match('/[U|D|R]{1}(:[U|D|R]{1}(:[U|D|R])?)?/', $option[1])) usage();
107  $MSG_STATUS = $option[1];
108  break;
109 
110  case '--quiet':
111  $QUIET = TRUE;
112  break;
113 
114  case '--show-query-only':
115  $SHOW_QUERY_ONLY = TRUE;
116  break;
117 
118  default:
119  echo 'Invalid option - '.$option[0];
120  usage();
121  break;
122 
123  }//end switch
124 
125 }//end foreach arguments
126 
127 if (empty($SYSTEM_ROOT)) {
128  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
129  usage();
130 }
131 
132 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
133  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
134  usage();
135 }
136 
137 if (empty($PERIOD)) usage();
138 
139 require_once $SYSTEM_ROOT.'/core/include/init.inc';
140 
141 $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
142 $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
143 $db =& $GLOBALS['SQ_SYSTEM']->db;
144 
145 purge_internal_message($PERIOD, $USER_FROM, $USER_TO, $MSG_TYPE, $MSG_STATUS);
146 
147 if ($SHOW_QUERY_ONLY) {
148  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
149 } else {
150  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
151 }
152 $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
153 
154 
167 function purge_internal_message($period, $user_from='', $user_to='', $msg_type='', $msg_status='')
168 {
169  global $db, $QUIET, $SHOW_QUERY_ONLY;
170  $bind_vars = Array();
171 
172  $sql = 'DELETE FROM'."\n";
173  $sql .= ' '.SQ_TABLE_RUNNING_PREFIX.'internal_msg'."\n";
174  $sql .= 'WHERE'."\n";
175  $sql .= ' sent <= :sent_before'."\n";
176  $bind_vars['sent_before'] = $period;
177 
178  $userids = Array(
179  Array(
180  'field_name' => 'userfrom',
181  'value' => (string)$user_from,
182  ),
183  Array(
184  'field_name' => 'userto',
185  'value' => (string)$user_to,
186  ),
187  );
188 
189  foreach ($userids as $userid) {
190 
191  if (strlen(trim($userid['value'])) != 0) {
192  if ($userid['value'] == 'all') {
193 
194  // All messages sent from/to users
195  $sql .= ' AND '.$userid['field_name'].' <> '.MatrixDAL::quote('0')."\n";
196 
197  } else if (strpos($userid['value'], ':') !== FALSE) {
198 
199  // Multiple userids found
200  $ids = explode(':', $userid['value']);
201  if (count($ids) >= 1) {
202  $sql .= ' AND (';
203  foreach ($ids as $id) {
204  if (strlen(trim($id)) == 0) continue;
205  if (trim($id) == 'all') usage(TRUE);
206  if (strpos($id, '*') !== FALSE && substr($id, -1) == '*') {
207  $sql .= $userid['field_name'].' LIKE '.MatrixDAL::quote(substr($id, 0, -1).':%').' OR ';
208  } else {
209  $sql .= $userid['field_name'].' = '.MatrixDAL::quote($id).' OR ';
210  }
211  }
212  $sql = substr($sql, 0, -4).')'."\n";
213  }
214 
215  } else {
216 
217  // Single Userid found
218  if (strpos($userid['value'], '*') !== FALSE && substr($userid['value'], -1) == '*') {
219  $sql .= ' AND '.$userid['field_name'].' LIKE '.MatrixDAL::quote(substr($userid['value'], 0, -1).':%')."\n";
220  } else {
221  $sql .= ' AND '.$userid['field_name'].' = '.MatrixDAL::quote($userid['value'])."\n";
222  }
223 
224  }
225  }
226 
227  }//end foreach userids
228 
229  // Type of message
230  if (!empty($msg_type)) {
231  if (strpos($msg_type, '*') !== FALSE && substr($msg_type, -1) == '*') {
232  $sql .= ' AND type LIKE :msg_type'."\n";
233  $bind_vars['msg_type'] = substr($msg_type, 0, -1).'%';
234  } else {
235  $sql .= ' AND type = :msg_type'."\n";
236  $bind_vars['msg_type'] = $msg_type;
237  }
238  }
239 
240  // Message Status
241  if (!empty($msg_status)) {
242  if (strpos($msg_status, ':') !== FALSE) {
243  $tmp = explode(':', $msg_status);
244  $sql .= ' and status IN (';
245  foreach($tmp as $token) {
246  $sql .= MatrixDAL::quote($token).', ';
247  }
248  $sql = substr($sql, 0, -2).")\n";
249  } else {
250  $sql .= ' AND status = :msg_status'."\n";
251  $bind_vars['msg_status'] = $msg_status;
252  }
253  }
254 
255  $query = MatrixDAL::preparePdoQuery($sql);
256  foreach ($bind_vars as $bind_var => $bind_value) {
257  MatrixDAL::bindValueToPdo($query, $bind_var, $bind_value);
258  }
259  MatrixDAL::execPdoQuery($query);
260  $affected_rows = MatrixDAL::getDbType() == 'oci' ? oci_num_rows($query) : $query->rowCount();
261 
262  if (!$QUIET) {
263  echo "\n".$affected_rows.' INTERNAL MESSAGES '.($SHOW_QUERY_ONLY ? 'CAN BE ' : '').'DELETED'."\n\n";
264  }
265 
266  if ($SHOW_QUERY_ONLY) {
267  echo str_repeat('*', 50)."\n";
268  echo '* Expected SQL query to run'."\n";
269  echo str_repeat('*', 50)."\n";
270  echo $sql;
271  echo str_repeat('*', 50)."\n";
272  }
273 
274 }//end purge_internal_message()
275 
276 
285 function usage($rollback=FALSE)
286 {
287  echo "\nUSAGE: remove_internal_message.php -s <system_root> -p <period> [-f userfrom] [-t userto] [-y msg type] [-u status] [--show-query-only] [--quiet]\n\n".
288  "-p The period to remove internal messages before\n".
289  "-f The userid that the message is sent from, e.g. all or 7, 229*, 229*:323*:7\n".
290  "-t The userid that the message is sent to, e.g. all or 7, 229*, 229*:323*:7\n".
291  "-y The type of internal message to remove, e.g. asset.linking.create, cron.*\n".
292  "-u The status of internal message to remove, e.g. U(nread), R(ead), D(eleted) or multiple like U:R or R:D\n".
293  "--show-query-only The script only prints the query without running it.\n".
294  "--quiet No output will be sent\n".
295  "(For -p, the period is in the format nx where n is the number of units and x is one of:\n".
296  " h - hours\t\n d - days\t\n w - weeks\t\n m - months\t\n y - years)\n\n".
297  "(For -f and -t, the userid can take different formats which is one of:\n".
298  " all\t\t- internal messages that are sent from/to other than system, i.e. not equal to 0\t\n".
299  " single id\t- for example, 7 for public user or 221* for LDAP bridge(#221) users\t\n".
300  " multiple ids\t- more than one of the above single ids, for example, 7:221*:328* for Public or LDAP bridge(#221) or IPB bridge(#328) users)\t\n\n".
301  "Examples:\n\n".
302  "1. Delete all the internal messages older than last 2 days\n".
303  " \$ php remove_internal_message.php -s SYSTEM_ROOT -p 2d\n\n".
304  "2. Delete all the internal messages sent to LDAP Bridge(#221) and IPB Bridge(#328) users in last 2 days\n".
305  " \$ php remove_internal_message.php -s SYSTEM_ROOT -p 2d -t 221*:328*\n\n".
306  "3. Delete all the internal messages sent from the system and Message type starts with asset in last 1 month\n".
307  " \$ php remove_internal_message.php -s SYSTEM_ROOT -p 1m -f 0 -y asset.*\n\n".
308  "4. Delete all the internal messages sent to users have been deleted in last 3 days\n".
309  " \$ php remove_internal_message.php -s SYSTEM_ROOT -p 3d -t all -u D\n\n".
310  "Inbox Message Examples:\n\n".
311  "1. Delete all read and deleted inbox messages (sent and inbox) in last 3 days\n".
312  " \$ php remove_internal_message.php -s SYSTEM_ROOT -p 3d -y inbox.* -u R:D\n\n".
313  "2. Delete all read inbox sent messages in last 3 days\n".
314  " \$ php remove_internal_message.php -s SYSTEM_ROOT -p 3d -y inbox.sent -u R\n\n".
315  "3. Delete all deleted inbox messages (sent and inbox) sent to LDAP bridge(#211) users in last 3 days\n".
316  " \$ php remove_internal_message.php -s SYSTEM_ROOT -p 3d -y inbox.* -u D -t 221*\n\n";
317 
318  if ($rollback) {
319  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
320  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
321  }
322 
323  exit();
324 
325 }//end usage()
326 
327 
328 ?>