28 error_reporting(E_ALL);
29 ini_set(
'memory_limit',
'1024M');
30 if ((php_sapi_name() !=
'cli')) {
31 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
34 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
35 if (empty($SYSTEM_ROOT)) {
36 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
40 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
41 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
45 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
48 if (count($argv) !== 3) {
49 echo
'Usage: system_integrity_fix_sort_orders.php <SYSTEM_ROOT> <PARENT ASSET ID>'.
"\n";
53 $parentid = $_SERVER[
'argv'][2];
54 $parent =& $GLOBALS[
'SQ_SYSTEM']->am->getAsset($parentid);
55 if (is_null($parent)) {
56 echo
"ERROR: Unable to retrieve that asset.\n";
60 $root_user =& $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
62 if (!$GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user)) {
63 echo
"ERROR: Failed login as root user\n";
68 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
70 $todo = Array($parentid);
73 echo
"\n".
'---BEGIN---'.
"\n";
75 $success = sortAssets($todo, $done);
77 echo
"\n".
'---COMPLETED---'.
"\n";
80 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
94 function sortAssets($todo, $done)
98 $parentid = array_shift($todo);
103 $sql =
'SELECT linkid, minorid
105 WHERE majorid = :parentid
106 AND link_type IN ('.MatrixDAL::quote(SQ_LINK_TYPE_1).
', '.
MatrixDAL::quote(SQ_LINK_TYPE_2).
')
107 ORDER BY sort_order ASC';
112 }
catch (Exception $e) {
113 throw new Exception(
'Unable to get linkids for parent: '.$parentid.
' due to database error: '.$e->getMessage());
116 echo
"\n".
'- Updating the sort order for kids of: #'.$parentid.
'...';
119 $childids = $linkids = Array();
120 foreach ($results as $row) {
122 $linkids[] = $row[
'linkid'];
124 $childids[] = $row[
'minorid'];
127 if (!empty($linkids)) {
131 if ($db_type ==
'oci') {
137 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
138 foreach (array_chunk($linkids, $chunk_size, TRUE) as $chunk) {
140 foreach ($chunk as $i => $linkid) {
141 $cases .=
'WHEN (linkid = '.$linkid.
') THEN '.$i.
' ';
143 $sql =
'UPDATE sq_ast_lnk
144 SET sort_order = CASE '.$cases.
' ELSE sort_order END
145 WHERE linkid IN ('.implode(
', ', $chunk).
')';
148 }
catch (Exception $e) {
149 throw new Exception(
'Unable to update sort_order for parent: '.$parentid.
' due to database error: '.$e->getMessage());
150 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
151 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
154 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
158 if (!in_array($parentid, $done)) {
166 if (!empty($childids)) {
167 echo
"\n\t".
'- Searching immediate children of: #'.$parentid.
' for branches';
168 foreach ($childids as $assetid) {
170 if (!in_array($assetid, $done)) {
174 $sql =
'SELECT minorid
176 WHERE majorid = :assetid';
181 }
catch (Exception $e) {
182 throw new Exception(
'Unable to check children of parent: '.$parentid.
' due to database error: '.$e->getMessage());
185 if ((!empty($children)) && count($children) > 1) {
190 foreach ($children as $grandchild) {
191 $link = $GLOBALS[
'SQ_SYSTEM']->am->getLink($grandchild[
'minorid'], NULL,
'', TRUE, NULL,
'minor');
192 if (!empty($link) && (($link[
'link_type'] == SQ_LINK_TYPE_1) || ($link[
'link_type'] == SQ_LINK_TYPE_2))) {
199 echo
"\n\t\t#".$assetid.
' is a parent with kids that will be sorted';
207 echo
"\n".
'* '.count($todo).
' items left to process'.
"\n";
208 echo
'* Using '.round((memory_get_usage()/1048576), 2).
' MB'.
"\n";
210 sortAssets($todo, $done);