Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
recreate_link_tree.php
1 <?php
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);
37 }
38 
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";
42  exit();
43 }
44 
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";
47  exit();
48 }
49 
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';
53 
54 error_reporting(E_ALL);
55 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', '-1');
56 
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";
60  exit();
61 }
62 
63 //-- MAIN() --//
64 
65 $db = MatrixDAL::getDb();
66 $pgdb = (MatrixDAL::getDbType() == 'pgsql');
67 
68 $script_start = time();
69 
70 echo_headline('TRUNCATING TREE');
71 
72 if ($pgdb) {
73  $sql = 'TRUNCATE sq_ast_lnk_tree;';
74 } else {
75  $sql = 'TRUNCATE TABLE sq_ast_lnk_tree;';
76 }
77 
78 echo $sql,"\n";
79 
80 // Work out how many significant links we actually have
81 $sql = 'SELECT COUNT(*) FROM sq_ast_lnk l WHERE '.db_extras_bitand(MatrixDAL::getDbType(), 'l.link_type', MatrixDAL::quote(SQ_SC_LINK_SIGNIFICANT)).' > 0';
82 $num_links = MatrixDAL::executeSqlOne($sql);
83 echo_headline('ANALYSING '.$num_links.' SIGNIFICANT LINKS');
84 
85 // Because DAL doesn't support row-by-row fetch, we'll do chunked fetch instead,
86 // 2000 at a time. If we don't get that many results, we'll do one last query
87 // in case anything had been added since - if we get zero results, we are done
88 $base_sql = 'SELECT l.majorid, l.linkid, l.minorid
89  FROM
90  sq_ast_lnk l
91  WHERE
92  '.db_extras_bitand(MatrixDAL::getDbType(), 'l.link_type', MatrixDAL::quote(SQ_SC_LINK_SIGNIFICANT)).' > 0
93  ORDER BY
94  l.sort_order, l.linkid, l.majorid, l.minorid';
95 
96 $offset = 0;
97 $chunk_size = 2000;
98 $echo_i = 0;
99 
100 $index = Array();
101 
102 while (TRUE) {
103  $sql = db_extras_modify_limit_clause($base_sql, MatrixDAL::getDbType(), $chunk_size, $offset);
104  $result = MatrixDAL::executeSqlAssoc($sql);
105 
106  // If no further results, we're done.
107  if (count($result) == 0) break;
108 
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;
114 
115  $echo_i++;
116  if ($echo_i % 200 == 0) fwrite(STDERR, '.');
117  }
118 
119  // advance the chains by as many results we actually got
120  $offset += count($result);
121 
122 }//end while
123 
124 fwrite(STDERR, "\n");
125 
126 echo_headline('CREATING INSERTS');
127 
128 // if the DB is postgres use the COPY syntax for quicker insert
129 if ($pgdb) {
130  $sql = "COPY sq_ast_lnk_tree (treeid, linkid, num_kids) FROM stdin;\n"
131  .'-'."\t".'1'."\t".(string)count($index[1]);
132 
133 } else {
134  $sql = 'INSERT INTO sq_ast_lnk_tree (treeid, linkid, num_kids) VALUES ('.MatrixDAL::quote('-').', '.MatrixDAL::quote(1).', '.MatrixDAL::quote(count($index[1])).');';
135 
136 }
137 
138 echo $sql,"\n";
139 
140 $echo_i = 0;
141 recurse_tree_create(1, '');
142 fwrite(STDERR, "\n");
143 
144 if ($pgdb) {
145  echo "\\.\n";
146 } else {
147  echo "COMMIT;\n";
148 }
149 
150 echo_headline($echo_i.' TREE ENTRIES CREATED');
151 
152 // as a part of bug fix #3864 Rebuilding Link Tree breaks triggers , ask user runnin the script to
153 // also run regenerate_treeids_for_triggers.php script as well
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");
158 
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");
164 
165 
166 //-- FUNCTIONS --//
167 
168 
177 function echo_headline($s)
178 {
179  static $start = 0;
180 
181  if ($start) {
182  $end = time();
183  $duration = $end - $start;
184  fwrite(STDERR, '-- Duration: '.floor($duration / 60).'mins '.($duration % 60)."seconds\n");
185  }
186 
187  fwrite(STDERR, "--------------------------------------\n$s\n--------------------------------------\n");
188 
189  $start = time();
190 
191 }//end echo_headline()
192 
193 
203 function recurse_tree_create($majorid, $path)
204 {
205  global $db, $index, $echo_i, $pgdb;
206 
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']]);
210 
211  if ($pgdb) {
212  $sql = $treeid."\t".(string)$data['linkid']."\t".(string)$num_kids;
213  } else {
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).');';
215  }
216 
217  echo $sql,"\n";
218 
219  $echo_i++;
220  if ($echo_i % 200 == 0) fwrite(STDERR, '.');
221 
222  if ($num_kids) {
223  recurse_tree_create($data['minorid'], $treeid);
224  }
225 
226  }//end foreach
227 
228 }//end recurse_tree_create()
229 
230 
231 ?>