34 error_reporting(E_ALL);
35 if (php_sapi_name() !=
'cli') {
36 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
39 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
40 if (empty($SYSTEM_ROOT)) {
41 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
45 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
46 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
50 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
51 require_once SQ_INCLUDE_PATH.
'/general_occasional.inc';
52 require_once SQ_FUDGE_PATH.
'/db_extras/db_extras.inc';
54 error_reporting(E_ALL);
55 if (ini_get(
'memory_limit') !=
'-1') ini_set(
'memory_limit',
'-1');
57 $root_user = &$GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
58 if (!$GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user)) {
59 echo
"ERROR: Failed logging in as root user\n";
68 $script_start = time();
70 echo_headline(
'TRUNCATING TREE');
73 $sql =
'TRUNCATE sq_ast_lnk_tree;';
75 $sql =
'TRUNCATE TABLE sq_ast_lnk_tree;';
83 echo_headline(
'ANALYSING '.$num_links.
' SIGNIFICANT LINKS');
88 $base_sql =
'SELECT l.majorid, l.linkid, l.minorid
94 l.sort_order, l.linkid, l.majorid, l.minorid';
107 if (count($result) == 0)
break;
109 foreach ($result as $data) {
110 $majorid = $data[
'majorid'];
111 unset($data[
'majorid']);
112 if (!isset($index[$majorid])) $index[$majorid] = Array();
113 $index[$majorid][] = $data;
116 if ($echo_i % 200 == 0) fwrite(STDERR,
'.');
120 $offset += count($result);
124 fwrite(STDERR,
"\n");
126 echo_headline(
'CREATING INSERTS');
130 $sql =
"COPY sq_ast_lnk_tree (treeid, linkid, num_kids) FROM stdin;\n"
131 .
'-'.
"\t".
'1'.
"\t".(string)count($index[1]);
134 $sql =
'INSERT INTO sq_ast_lnk_tree (treeid, linkid, num_kids) VALUES ('.MatrixDAL::quote(
'-').
', '.
MatrixDAL::quote(1).
', '.
MatrixDAL::quote(count($index[1])).
');';
141 recurse_tree_create(1,
'');
142 fwrite(STDERR,
"\n");
150 echo_headline($echo_i.
' TREE ENTRIES CREATED');
154 fwrite(STDERR,
"\n\n*************************************************************************
155 PLEASE RUN regenerate_treeids_for_triggers.php SCRIPT AFTER RECREATING
156 LINK TREE IF YOU HAVE ANY TRIGGERS INSTALLED ON THE SYSTEM
157 *************************************************************************\n\n");
159 $script_end = time();
160 $script_duration = $script_end - $script_start;
161 echo
'-- Script Start : ', $script_start,
' Script End : ', $script_end,
"\n";
162 echo
'-- Script Duration: '.floor($script_duration / 60).
'mins '.($script_duration % 60).
"seconds\n";
163 fwrite(STDERR,
'-- Script Duration: '.floor($script_duration / 60).
'mins '.($script_duration % 60).
"seconds\n");
177 function echo_headline($s)
183 $duration = $end - $start;
184 fwrite(STDERR,
'-- Duration: '.floor($duration / 60).
'mins '.($duration % 60).
"seconds\n");
187 fwrite(STDERR,
"--------------------------------------\n$s\n--------------------------------------\n");
203 function recurse_tree_create($majorid, $path)
205 global $db, $index, $echo_i, $pgdb;
207 foreach ($index[$majorid] as $i => $data) {
208 $treeid = $path.asset_link_treeid_convert($i,
true);
209 $num_kids = (empty($index[$data[
'minorid']])) ? 0 : count($index[$data[
'minorid']]);
212 $sql = $treeid.
"\t".(string)$data[
'linkid'].
"\t".(
string)$num_kids;
214 $sql =
'INSERT INTO sq_ast_lnk_tree (treeid, linkid, num_kids) VALUES ('.MatrixDAL::quote($treeid).
','.
MatrixDAL::quote((
int) $data[
'linkid']).
','.
MatrixDAL::quote($num_kids).
');';
220 if ($echo_i % 200 == 0) fwrite(STDERR,
'.');
223 recurse_tree_create($data[
'minorid'], $treeid);