29 error_reporting(E_ALL);
30 if (count($_SERVER[
'argv']) < 3 || php_sapi_name() !=
'cli') {
31 echo
"This script needs to be run in the following format:\n\n";
32 echo
"\tphp system_integrity_invalid_links.php [SYSTEM_ROOT] [-delete|-check] [-remove_notice_links]\n\n";
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";
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";
47 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
49 $ACTION = (isset($_SERVER[
'argv'][2])) ? $_SERVER[
'argv'][2] :
'';
50 $ACTION = ltrim($ACTION,
'-');
51 if (empty($ACTION) || ($ACTION !=
'delete' && $ACTION !=
'check')) {
52 echo
"ERROR: No action specified";
56 $remove_notice_links = FALSE;
57 $NOTICE_ACTION = (isset($_SERVER[
'argv'][3])) ? $_SERVER[
'argv'][3] :
'';
58 $NOTICE_ACTION = ltrim($NOTICE_ACTION,
'-');
59 if ($NOTICE_ACTION ==
'remove_notice_links') {
60 if ($ACTION !=
'delete') trigger_error(
"Cannot remove notice links if the action is not '-delete'", E_USER_ERROR);
61 $remove_notice_links = TRUE;
62 }
else if ($NOTICE_ACTION !=
'') {
63 if ($ACTION ==
'delete') echo
"\nThird argument was mentioned but was not '-remove_notice_links'. Notice Links will not be removed\n\n";
67 $root_user = &$GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
68 if (!$GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user)) {
69 echo
"ERROR: Failed login in as root user";
73 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db');
74 $sql =
"SELECT * FROM sq_ast_lnk a WHERE a.minorid NOT IN (SELECT assetid FROM sq_ast)";
75 if (!$remove_notice_links && $ACTION !=
'check') $sql .=
" AND a.link_type <> :link_type";
77 $sql .=
" UNION SELECT * FROM sq_ast_lnk b WHERE b.majorid NOT IN (SELECT assetid FROM sq_ast) AND b.majorid <> '0'";
78 if (!$remove_notice_links && $ACTION !=
'check') $sql .=
" AND b.link_type <> :link_type";
84 $link_count = count($links);
85 }
catch (Exception $e) {
86 trigger_error(
'Unable to find invalid links due to database error: '.$e->getMessage(), E_USER_ERROR);
89 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
91 if ($link_count == 0) {
92 echo
"\nNo invalid links found\n";
95 echo
"\nFound $link_count invalid links\n";
96 if ($ACTION ==
'delete'){
97 echo
'This script is about to be run SYSTEM WIDE in DELETE mode. Are you sure you want to continue (y/n): ';
98 $confirm = rtrim(fgets(STDIN, 4094));
99 if ($confirm !=
'y') exit(0);
105 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
106 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
108 foreach($links as $link) {
111 if (!($link[
'link_type'] & SQ_SC_LINK_SIGNIFICANT) && !$remove_notice_links)
continue;
113 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
119 num_kids = num_kids - 1
125 LENGTH(SUBSTR(t.treeid, 1, LENGTH(t.treeid) - '.SQ_CONF_ASSET_TREE_SIZE.
')) != 0
127 SUBSTR(t.treeid, 1, LENGTH(t.treeid) - '.SQ_CONF_ASSET_TREE_SIZE.
')
140 }
catch (Exception $e) {
141 trigger_error(
'Unable to update the link tree for linkid: '.$link[
'linkid'].
' due to database error: '.$e->getMessage(), E_USER_ERROR);
154 sq_ast_lnk_tree pt, sq_ast_lnk_tree ct
157 AND (ct.treeid LIKE pt.treeid || '.
'\''.
'%'.
'\''.
'
158 OR ct.treeid = pt.treeid)
164 }
catch (Exception $e) {
165 trigger_error(
'Unable to delete tree links for linkid: '.$link[
'linkid'].
' due to database error: '.$e->getMessage(), E_USER_ERROR);
172 sort_order = sort_order - 1
175 AND sort_order > :sort_order';
181 }
catch (Exception $e) {
182 trigger_error(
'Unable to update sort orders for majorid: '.$link[
'majorid'].
' due to database error: '.$e->getMessage(), E_USER_ERROR);
188 'linkid' => $link[
'linkid'],
189 'majorid' => $link[
'majorid'],
192 }
catch (Exception $e) {
193 trigger_error(
'Unable to delete link with linkid: '.$link[
'linkid'].
' due to database error: '.$e->getMessage(), E_USER_ERROR);
196 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
197 echo
"Deleted Link ID: ".$link[
'linkid'].
" with Major ID: ".$link[
'majorid'].
" and Minor ID: ".$link[
'minorid'].
"\n";
201 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
203 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db');
205 $sql =
'SELECT count(*)
207 WHERE a.assetid NOT IN (SELECT minorid from sq_ast_lnk)
208 AND a.assetid NOT IN (SELECT majorid from sq_ast_lnk)';
213 }
catch (Exception $e) {
214 trigger_error(
'Unable to count orphaned assets due to database error: '.$e->getMessage(), E_USER_ERROR);
217 if (!empty($orphans)) echo
"There are $orphans orphan assets found. You must run system_integrity_orphaned_assets.php on the root folder to rescue these assets.\n";
219 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
220 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();