Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
system_integrity_orphaned_assets.php
1 <?php
25 error_reporting(E_ALL);
26 if ((php_sapi_name() != 'cli')) {
27  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
28 }
29 
30 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
31 if (empty($SYSTEM_ROOT)) {
32  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
33  exit();
34 }
35 
36 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
37  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
38  exit();
39 }
40 
41 require_once $SYSTEM_ROOT.'/core/include/init.inc';
42 
43 $MAP_ASSETID = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : '0';
44 
45 $map_asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($MAP_ASSETID),'asset',FALSE);
46 
47 if (empty($MAP_ASSETID) || empty($map_asset_info)) {
48  echo "ERROR: You need to supply the assetid of a valid asset that orphaned assets will be mapped to as the second argument\n";
49  exit();
50 } else {
51  $map_asset =& $GLOBALS['SQ_SYSTEM']->am->getAsset($MAP_ASSETID);
52 }
53 
54 $ROOT_ASSETID = (isset($_SERVER['argv'][3])) ? $_SERVER['argv'][3] : '1';
55 if ($ROOT_ASSETID == 1) {
56  echo "\nWARNING: You are running this integrity checker on the whole system.\nThis is fine, but it may take a long time\n\n";
57 }
58 
59 // ask for the root password for the system
60 echo 'Enter the root password for "'.SQ_CONF_SYSTEM_NAME.'": ';
61 system('stty -echo');
62 $root_password = rtrim(fgets(STDIN, 4094));
63 system('stty echo');
64 
65 // check that the correct root password was entered
66 $root_user =& $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
67 if (!$root_user->comparePassword($root_password)) {
68  echo "ERROR: The root password entered was incorrect\n";
69  exit();
70 }
71 
72 // log in as root
73 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
74  echo "ERROR: Failed login in as root user\n";
75  exit();
76 }
77 
78 $db =& $GLOBALS['SQ_SYSTEM']->db;
79 
80 $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
81 
82 // go through each child of the specified asset, lock it, validate it, unlock it
83 $assets = $GLOBALS['SQ_SYSTEM']->am->getChildren($ROOT_ASSETID, 'asset', FALSE);
84 foreach ($assets as $assetid => $type_code_data) {
85  $type_code = $type_code_data[0]['type_code'];
86 
87  printAssetName($assetid);
88 
89  $sql = 'SELECT
90  linkid,
91  majorid,
92  link_type,
93  sort_order
94  FROM
95  sq_ast_lnk
96  WHERE
97  minorid = :minorid
98  AND
99  link_type <> :link_type';
100 
101  $query = MatrixDAL::preparePdoQuery($sql);
102  MatrixDAL::bindValueToPdo($query, 'minorid', $assetid);
103  MatrixDAL::bindValueToPdo($query, 'link_type', SQ_LINK_NOTICE);
104  $links = MatrixDAL::executePdoAssoc($query);
105 
106  $updated = FALSE;
107  $errors = FALSE;
108 
109  foreach (array_keys($links) as $linkid) {
110  $link =& $links[$linkid];
111  if ($link['linkid'] == 1) continue;
112 
113  $major_asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo(Array($link['majorid']),'asset',FALSE);
114 
115  if (empty($major_asset_info)) {
116 
117  // ARGH, we have a link but our major isn't there anymore... this could be a problem
118  // if it was an exclusive link...!
119  // basically we need to do the whole rubbish regarding deleting asset links in here
120  // because deleteAssetLink() checks to see if the asset is alive... which it isn't >_>;;;
121 
122  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
123  $db =& $GLOBALS['SQ_SYSTEM']->db;
124 
125  // open the transaction
126  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
127 
128  // if this is a significant link
129  if ($link['link_type'] & SQ_SC_LINK_SIGNIFICANT) {
130 
132 
133  // update the parents to tell them that they are going to be one kid less
134  $sub_sql = 'SELECT
135  SUBSTR(t.treeid, 1, (LENGTH(t.treeid) - '.SQ_CONF_ASSET_TREE_SIZE.'))
136  FROM
137  sq_ast_lnk_tree t
138  WHERE
139  t.linkid = :linkid';
140 
141  $sql = 'UPDATE
142  sq_ast_lnk_tree
143  SET
144  num_kids = num_kids - 1
145  WHERE
146  treeid in ('.$sub_sql.')';
147 
148  $query = MatrixDAL::preparePdoQuery($sql);
149  MatrixDAL::bindValueToPdo($query, 'linkid', $link['linkid']);
150  MatrixDAL::execPdoQuery($query);
151 
152 
153  // we can delete all the links under these nodes because it will be a clean start
154  // when we insert into the gap's we create below
155  $sub_sql = 'SELECT
156  ct.treeid
157  FROM
158  sq_ast_lnk_tree pt,
159  sq_ast_lnk_tree ct
160  WHERE
161  pt.linkid = :linkid
162  AND ct.treeid LIKE pt.treeid || '.MatrixDAL::quote('%').'
163  AND ct.treeid > pt.treeid';
164 
165  $sql = 'DELETE FROM
166  sq_ast_lnk_tree
167  WHERE
168  treeid in ('.$sub_sql.')';
169 
170  $query = MatrixDAL::preparePdoQuery($sql);
171  MatrixDAL::bindValueToPdo($query, 'linkid', $link['linkid']);
172  MatrixDAL::execPdoQuery($query);
173 
174  // we are going to set the treeid nodes that this link is associated
175  // with to zero so that we can find it as a gap when we createLink() later on
176 
177  $sql = 'UPDATE
178  sq_ast_lnk_tree
179  SET
180  linkid = :linkid,
181  num_kids = :num_kids
182 
183  WHERE
184  linkid = :old_linkid';
185 
186  $query = MatrixDAL::preparePdoQuery($sql);
187  MatrixDAL::bindValueToPdo($query, 'linkid', '0');
188  MatrixDAL::bindValueToPdo($query, 'num_kids', '0');
189  MatrixDAL::bindValueToPdo($query, 'old_linkid', $link['linkid']);
190  MatrixDAL::execPdoQuery($query);
191 
192  }//end if significant link
193 
194  // move 'em up, higher
195  $sql = 'UPDATE
196  sq_ast_lnk
197  SET
198  sort_order = sort_order - 1
199  WHERE
200  majorid = :majorid
201  AND
202  sort_order > :sort_order';
203 
204  $query = MatrixDAL::preparePdoQuery($sql);
205  MatrixDAL::bindValueToPdo($query, 'majorid', $link['majorid']);
206  MatrixDAL::bindValueToPdo($query, 'sort_order', $link['sort_order']);
207  MatrixDAL::execPdoQuery($query);
208 
209  $sql = 'DELETE FROM
210  sq_ast_lnk
211  WHERE
212  linkid = :linkid';
213 
214  $query = MatrixDAL::preparePdoQuery($sql);
215  MatrixDAL::bindValueToPdo($query, 'linkid', $link['linkid']);
216  MatrixDAL::execPdoQuery($query);
217 
218  // tell the asset it has updated
219  $asset =& $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $type_code);
220  $asset->linksUpdated();
221 
222  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
223  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
224  unset($links[$linkid]);
225  }//end if
226  }//end foreach
227 
228  if ($errors) { // no links
229  printUpdateStatus('FAILED');
230  continue;
231  }
232 
233  if (empty($links)) { // no links
234  $asset =& $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, $type_code);
235  if (!$GLOBALS['SQ_SYSTEM']->am->createAssetLink($map_asset, $asset, SQ_LINK_TYPE_2, 'Orphaned Asset')) {
236  printUpdateStatus('FAILED');
237  continue;
238  }
239  $updated = TRUE;
240  }
241 
242  if ($updated) {
243  printUpdateStatus('OK');
244  } else {
245  printUpdateStatus('--');
246  }
247  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset);
248 
249 }//end foreach
250 
251 $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
252 
253 
264 function printAssetName($assetid)
265 {
266  $asset_info = $GLOBALS['SQ_SYSTEM']->am->getAssetInfo($assetid);
267  $str = '[ #'.$assetid.' ]'.$asset_info[$assetid]['name'];
268  if (strlen($str) > 66) {
269  $str = substr($str, 0, 66).'...';
270  }
271  printf ('%s%'.(70 - strlen($str)).'s', $str,'');
272 
273 }//end printAssetName()
274 
275 
284 function printUpdateStatus($status)
285 {
286  echo "[ $status ]\n";
287 
288 }//end printUpdateStatus()
289 
290 
291 ?>