40 error_reporting(E_ALL);
41 if ((php_sapi_name() !=
'cli')) {
42 trigger_error(
"\nYou can only run this script from the command line.\n", E_USER_ERROR);
45 $options = getopt(
'm:s:p:d:');
47 if (empty($options)) {
52 if (!isset($options[
'm']) || !is_dir($options[
'm'])) {
53 echo
"\nYou need to supply the path to the System Root as the argument of -m option.
54 Run the script without argument to see its usage.\n\n";
58 require_once $options[
'm'].
'/core/include/init.inc';
61 if (isset($options[
'd'])) {
62 if (is_file($options[
'd'])) {
63 delete_old_shadow_links($options[
'd']);
65 echo
"\nYou need to supply the path to the PHP file as the argument of -d option.
66 Run the script without argument to see its usage.\n\n";
72 $old_links = get_old_shadow_links();
75 if (empty($old_links)) {
76 echo
"\nThere are no old shadow links in the system.\n\n";
81 print_links($old_links);
84 if (!isset($options[
's']) && !isset($options[
'p'])) {
86 You will need to run this script with -s or -p option to save the shadow links to files
87 and -d option to delete the shadow links stored in the output file generated by -p option.
88 (You have to use an SQL client like psql to execute the SQL statements generated by -s option)
93 if (isset($options[
's'])) {
94 save_sql_statements($old_links, $options[
's']);
98 if (isset($options[
'p'])) {
99 save_linkid_array($old_links, $options[
'p']);
110 function delete_old_shadow_links($file)
116 if (!isset($linkids) || !is_array($linkids)) {
117 echo
"\nERROR: The file $file is not in a correct format.\n\n";
122 $chunks = array_chunk($linkids, 100);
125 $total_deleted_links = 0;
128 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
129 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
132 foreach ($chunks as $chunk) {
133 $delete_linkids = Array();
134 foreach ($chunk as $linkid) {
138 $sql =
'DELETE FROM sq_shdw_ast_lnk WHERE linkid IN ('.implode(
',', $delete_linkids).
')';
140 $total_deleted_links += $deleted_rows;
144 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
145 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
146 throw new Exception(
'Can not delete old shadow links due to the DB error: '.$e->getMessage());
149 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
150 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
152 echo
"\n$total_deleted_links old shadow link(s) from $file were deleted successfully.\n\n";
164 function save_linkid_array($links, $file)
168 foreach ($links as $link) {
169 $linkids[] =
"'".$link[
'linkid'].
"', /* link_type=".$link[
'link_type']
170 .
", majorid=".$link[
'majorid'].
", minorid=".$link[
'minorid']
176 '.implode(
"\n", $linkids).
'
182 if (file_put_contents($file, $php) === FALSE) {
183 echo
"\nERROR: Can not write PHP array to $file\n\n";
185 echo
"\nThe PHP array to delete the old shadow links are saved to $file\n\n";
198 function save_sql_statements($links, $file)
202 foreach ($links as $link) {
203 $sql .=
'DELETE FROM sq_shdw_ast_lnk WHERE linkid='.MatrixDAL::quote($link[
'linkid'])
204 .
'; -- link_type='.$link[
'link_type'].
', majorid='.$link[
'majorid']
205 .
', minorid='.$link[
'minorid'].
"\n";
209 if (file_put_contents($file, $sql) === FALSE) {
210 echo
"\nERROR: Can not write SQL statements to $file\n\n";
212 echo
"\nThe SQL statements to delete the old shadow links are saved to $file\n\n";
231 function print_links($links)
233 echo
"\nList of old shadow links:\n\n";
235 $row_format =
"%7s |%5s |%8s | %-55s\n";
236 $row_delimiter_format =
"%'-7s-+%'-5s-+%'-8s-+%'-55s-\n";
239 printf($row_format,
'linkid',
'type',
'majorid',
'minorid');
240 printf($row_delimiter_format,
'',
'',
'',
'');
243 foreach ($links as $link) {
244 printf($row_format, $link[
'linkid'], $link[
'link_type'], $link[
'majorid'], $link[
'minorid']);
247 echo
"(Number of links: ".count($links).
")\n";
257 function get_old_shadow_links()
260 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db');
262 $sql =
'SELECT linkid, link_type, majorid, minorid FROM sq_shdw_ast_lnk ORDER BY majorid, link_type, linkid';
265 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
268 $am = $GLOBALS[
'SQ_SYSTEM']->am;
269 $old_links = Array();
270 foreach ($links as $link) {
271 $shadow_assetid = $link[
'minorid'];
272 $id_parts = explode(
':', $shadow_assetid);
273 if (isset($id_parts[1])) {
274 $bridge_id = $id_parts[0];
275 $bridge = $am->getAsset($bridge_id,
'', FALSE);
276 if (is_null($bridge) || !method_exists($bridge,
'getAsset')) {
277 $old_links[] = $link;
280 $shadow_asset = $bridge->getAsset($shadow_assetid,
'', FALSE, TRUE);
281 if (is_null($shadow_asset)) $old_links[] = $link;
296 function print_usage()
299 This script is used to clean the old shadow links whose minor shadow assets are
300 no longer valid because their bridges can not find them anymore.
302 php clean_old_shadow_links.php [OPTION]
305 -m Required. This option requires an argument which is the Matrix System Root.
306 -s Optional. This option requires a file path to save the SQL statements to.
307 -p Optional. This option requires a file path to save the PHP array to.
308 -d Optional. This option requires the path of a PHP file generated by
309 the -p option to delete old shadow links from the database.
312 List the old shadow links:
313 php scripts/clean_old_shadow_links.php -m $PWD
315 Save the SQL statements to delete old shadow links to file:
316 php scripts/clean_old_shadow_links.php -m $PWD -s delete_shd_links.sql
318 Save the shadow links under PHP array format:
319 php scripts/clean_old_shadow_links.php -m $PWD -p old_shadow_links.inc
321 Delete the shadow links in the PHP file:
322 php scripts/clean_old_shadow_links.php -m $PWD -d old_shadow_links.inc