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);
29 require_once
'Console/Getopt.php';
31 $shortopt =
's:p:f:t:y:u:';
32 $longopt = Array(
'quiet',
'show-query-only');
34 $args = Console_Getopt::readPHPArgv();
36 $options = Console_Getopt::getopt($args, $shortopt, $longopt);
38 if (empty($options[0])) usage();
46 $SHOW_QUERY_ONLY = FALSE;
49 foreach ($options[0] as $option) {
54 if (empty($option[1])) usage();
55 if (!is_dir($option[1])) usage();
56 $SYSTEM_ROOT = $option[1];
60 if (empty($option[1])) usage();
62 if (!preg_match(
'|^(\d+)([hdwmy])$|', $option[1], $matches)) {
66 $time_num = (int) $matches[1];
68 switch ($matches[2]) {
79 $time_units =
'month';
85 if ($time_num > 1) $time_units .=
's';
87 $PERIOD = date(
'Y-m-d H:i:s', strtotime(
'-'.$time_num.
' '.$time_units));
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];');
99 if (empty($option[1])) usage();
100 $MSG_TYPE = $option[1];
104 if (empty($option[1])) usage();
106 if (!preg_match(
'/[U|D|R]{1}(:[U|D|R]{1}(:[U|D|R])?)?/', $option[1])) usage();
107 $MSG_STATUS = $option[1];
114 case '--show-query-only':
115 $SHOW_QUERY_ONLY = TRUE;
119 echo
'Invalid option - '.$option[0];
127 if (empty($SYSTEM_ROOT)) {
128 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
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";
137 if (empty($PERIOD)) usage();
139 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
141 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
142 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
143 $db =& $GLOBALS[
'SQ_SYSTEM']->db;
145 purge_internal_message($PERIOD, $USER_FROM, $USER_TO, $MSG_TYPE, $MSG_STATUS);
147 if ($SHOW_QUERY_ONLY) {
148 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
150 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
152 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
167 function purge_internal_message($period, $user_from=
'', $user_to=
'', $msg_type=
'', $msg_status=
'')
169 global $db, $QUIET, $SHOW_QUERY_ONLY;
170 $bind_vars = Array();
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;
180 'field_name' =>
'userfrom',
181 'value' => (
string)$user_from,
184 'field_name' =>
'userto',
185 'value' => (
string)$user_to,
189 foreach ($userids as $userid) {
191 if (strlen(trim($userid[
'value'])) != 0) {
192 if ($userid[
'value'] ==
'all') {
197 }
else if (strpos($userid[
'value'],
':') !== FALSE) {
200 $ids = explode(
':', $userid[
'value']);
201 if (count($ids) >= 1) {
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 ';
212 $sql = substr($sql, 0, -4).
')'.
"\n";
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";
221 $sql .=
' AND '.$userid[
'field_name'].
' = '.
MatrixDAL::quote($userid[
'value']).
"\n";
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).
'%';
235 $sql .=
' AND type = :msg_type'.
"\n";
236 $bind_vars[
'msg_type'] = $msg_type;
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) {
248 $sql = substr($sql, 0, -2).
")\n";
250 $sql .=
' AND status = :msg_status'.
"\n";
251 $bind_vars[
'msg_status'] = $msg_status;
256 foreach ($bind_vars as $bind_var => $bind_value) {
263 echo
"\n".$affected_rows.
' INTERNAL MESSAGES '.($SHOW_QUERY_ONLY ?
'CAN BE ' :
'').
'DELETED'.
"\n\n";
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";
271 echo str_repeat(
'*', 50).
"\n";
285 function usage($rollback=FALSE)
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".
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";
319 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
320 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();