Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
remove_form_submission.php
1 <?php
33 error_reporting(E_ALL);
34 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
35 
36 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
37 if (empty($SYSTEM_ROOT)) {
38  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
39  exit();
40 }
41 
42 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
43  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
44  exit();
45 }
46 
47 require_once $SYSTEM_ROOT.'/core/include/init.inc';
48 
49 // check date range frmat
50 // if the user supply an invalid date range, select query will not return anything
51 if (count($argv) != 5) {
52  echo 'Usage: remove_form_submission.php <system_root> <custom_form_id> <from_date> <to_date>'."\n";
53  echo 'Date format: YYYY-MM-DD'."\n";
54  exit(1);
55 } else if (preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}/',$argv[3]) != TRUE) {
56  // simple date format YYYY-MM-DD check, nothing fancy
57  echo"ERROR: 'From date' must be in the format 'YYYY-MM-DD'\n";
58  exit(1);
59 } else if (preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}/',$argv[4]) != TRUE) {
60  echo "ERROR: 'To date' must be in the format 'YYYY-MM-DD'\n";
61  exit(1);
62 }
63 require_once SQ_FUDGE_PATH.'/general/datetime.inc';
64 $from_value = iso8601_date_component($argv[3]).' 00:00:00';
65 $to_value = iso8601_date_component($argv[4]).' 23:59:59';
66 
67 // check assetid and asset type
68 $assetid = $argv[2];
69 $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
70 if (is_null($asset)) {
71  echo "ERROR: #$assetid is not a valid asset ID";
72  exit(1);
73 }
74 if (!is_a($asset, 'page_custom_form')) {
75  echo "ERROR: Asset #$assetid is not a custom form asset";
76  exit(1);
77 } else {
78  $form = $asset->getForm();
79  $sub_folder = $form->getSubmissionsFolder();
80 }
81 
82 
83 require_once SQ_FUDGE_PATH.'/db_extras/db_extras.inc';
84 $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
85 $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
86 $db = $GLOBALS['SQ_SYSTEM']->db;
87 
88 
89 $sql = 'SELECT
90  a.assetid
91  FROM
92  ('.SQ_TABLE_RUNNING_PREFIX.'ast a
93  JOIN '.SQ_TABLE_RUNNING_PREFIX.'ast_typ_inhd i ON a.type_code = i.type_code)
94  JOIN '.SQ_TABLE_RUNNING_PREFIX.'ast_lnk l
95  ON l.minorid = a.assetid';
96 $where = 'l.majorid IN (:assetid, :subfolder_assetid)
97  AND i.inhd_type_code = :inhd_type_code
98  AND a.created BETWEEN '.db_extras_todate(MatrixDAL::getDbType(), ':created_from', FALSE).'
99  AND '.db_extras_todate(MatrixDAL::getDbType(), ':created_to', FALSE);
100  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 'a');
101  $where = $GLOBALS['SQ_SYSTEM']->constructRollbackWhereClause($where, 'l');
102 $sql = $sql.$where.' ORDER BY a.created DESC';
103 
104 $query = MatrixDAL::preparePdoQuery($sql);
105 MatrixDAL::bindValueToPdo($query, 'assetid', $asset->id);
106 MatrixDAL::bindValueToPdo($query, 'subfolder_assetid', $sub_folder->id);
107 MatrixDAL::bindValueToPdo($query, 'inhd_type_code', 'form_submission');
108 MatrixDAL::bindValueToPdo($query, 'created_from', $from_value);
109 MatrixDAL::bindValueToPdo($query, 'created_to', $to_value);
110 $assetids = MatrixDAL::executePdoAssoc($query, 0);
111 
112 if (empty($assetids)) {
113  echo "No form submission found for '$asset->name' (#$assetid) within the specified date range\n";
114  exit();
115 }
116 echo 'Found '.count($assetids)." form submission(s) for '$asset->name' (#$assetid)\n(Date range: $from_value to $to_value)\n";
117 
118 $unquoted_assetids = $assetids;
119 
120 // quote the assetids to be used in the IN clause
121 foreach ($assetids as $key => $assetid) {
122  $assetids[$key] = MatrixDAL::quote((String)$assetid);
123 }
124 
125 // break up the assets into chunks of 1000 so that oracle does not complain
126 $assetid_in = Array();
127 foreach (array_chunk($assetids, 999) as $chunk) {
128  $assetid_in[] = ' assetid IN ('.implode(', ', $chunk).')';
129 }
130 $in_assetid = '('.implode(' OR ', $assetid_in).')';
131 
132 $assetid_in = Array();
133 foreach (array_chunk($assetids, 999) as $chunk) {
134  $assetid_in[] = ' minorid IN ('.implode(', ', $chunk).')';
135 }
136 $in_minorid = '('.implode(' OR ', $assetid_in).')';
137 
138 $assetid_in = Array();
139 foreach (array_chunk($assetids, 999) as $chunk) {
140  $assetid_in[] = ' majorid IN ('.implode(', ', $chunk).')';
141 }
142 $in_majorid = '('.implode(' OR ', $assetid_in).')';
143 
144 // start removing entries from the database
145 echo "Removing assets ...\n";
146 $sql = 'DELETE FROM sq_ast WHERE '.$in_assetid;
147 $delete_count = MatrixDAL::executeSql($sql);
148 
149 echo "\tUpdating link table...\n";
150 $sql = 'DELETE FROM sq_ast_lnk WHERE '.$in_minorid;
152 
153 echo "\tUpdating link tree table ...\n";
154 $sql = 'DELETE FROM sq_ast_lnk_tree WHERE linkid NOT IN (SELECT linkid FROM sq_ast_lnk)';
156 $sql = "UPDATE sq_ast_lnk_tree SET num_kids=num_kids-$delete_count WHERE linkid = (SELECT linkid FROM sq_ast_lnk WHERE minorid = '".$sub_folder->id."')";
158 
159 echo "\tUpdating attribute value table ...\n";
160 $sql = 'DELETE FROM sq_ast_attr_val WHERE '.$in_assetid;
162 
163 echo "\tUpdating metadata table ...\n";
164 $sql = 'DELETE FROM sq_ast_mdata WHERE '.$in_assetid;
166 
167 echo "\tUpdating metadata value table ...\n";
168 $sql = 'DELETE FROM sq_ast_mdata_val WHERE '.$in_assetid;
170 
171 echo "\tUpdating workflow table...\n";
172 $sql = 'DELETE FROM sq_ast_wflow WHERE '.$in_assetid;
174 
175 echo "\tUpdating permissions table...\n";
176 $sql = 'DELETE FROM sq_ast_perm WHERE '.$in_assetid;
178 
179 echo "\tUpdating roles table...\n";
180 $sql = 'DELETE FROM sq_ast_role WHERE '.$in_assetid;
182 
183 echo "\tUpdating shadow link table...\n";
184 $sql = 'DELETE FROM sq_shdw_ast_lnk WHERE '.$in_majorid;
186 
187 $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
188 $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
189 
190 echo "\tDeleting asset data directories...\n";
191 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
192 foreach ($unquoted_assetids as $sub_assetid){
193 
194  $data_path_suffix = asset_data_path_suffix('form_submission', $sub_assetid);
195  $data_path = SQ_DATA_PATH.'/private/'.$data_path_suffix;
196  $data_path_public = SQ_DATA_PATH.'/public/'.$data_path_suffix;
197 
198  if (is_dir($data_path)) {
199  if (!delete_directory($data_path)) {
200  trigger_error("Could not delete private data directory for Form Submission (Id: #$assetid)", E_USER_WARNING);
201  }
202  }
203 
204  if (is_dir($data_path_public)) {
205  if (!delete_directory($data_path_public)) {
206  trigger_error("Could not delete public data directory for Form Submission (Id: #$assetid)", E_USER_WARNING);
207  }
208  }
209 }
210 
211 echo "Done\n";