25 error_reporting(E_ALL);
26 if ((php_sapi_name() !=
'cli')) trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
28 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
29 if (empty($SYSTEM_ROOT) || !is_dir($SYSTEM_ROOT)) {
30 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
34 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
35 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
39 require_once
'Console/Getopt.php';
41 $longopt = Array(
'delete-orphans');
43 $args = Console_Getopt::readPHPArgv();
45 $options = Console_Getopt::getopt($args, $shortopt, $longopt);
48 foreach ($options[0] as $option) {
50 case '--delete-orphans':
57 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
60 $root_user = $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
63 if (!$GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user)) {
64 echo
"ERROR: Failed logging in as root user\n";
68 $fv = $GLOBALS[
'SQ_SYSTEM']->getFileVersioning();
69 $children = $GLOBALS[
'SQ_SYSTEM']->am->getTypeAssetids(
'file', FALSE);
74 echo
"[Total ".count($children).
" assets will be checked.]\n\n";
75 foreach ($children as $assetid) {
77 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($assetid);
78 $file_name = $asset->attr(
'name');
80 $data_paths = Array($asset->data_path);
81 if (file_exists($asset->data_path_public)) {
82 $data_paths[] = $asset->data_path_public;
86 foreach ($data_paths as $data_path) {
87 $files = list_files($data_path);
88 $ffiles = list_files($data_path.
'/.FFV');
89 $diff = array_intersect($files, $ffiles);
90 if (count($diff) == 1) {
91 $file = array_pop($diff);
92 if ($file == $file_name)
continue;
94 $ophs = array_diff($diff, Array($file_name));
95 foreach ($ophs as $file) {
96 $orphans[$assetid][] = $data_path.
'/'.$file;
102 printAssetid($assetid);
103 if (isset($orphans[$assetid])) {
104 printStatus(
'FOUND');
109 $GLOBALS[
'SQ_SYSTEM']->am->forgetAsset($asset, TRUE);
115 if (!empty($orphans)) {
116 echo
"\n[Total $count orphan files found]\n\n";
117 foreach ($orphans as $assetid => $files) {
118 echo
"[ #$assetid ]\n";
119 echo implode(
"\n", $files).
"\n\n";
122 echo
"No orphan files found\nBye\n";
129 echo
"DO YOU REALLY WANT TO DELETE THESE FILES? (Yes/No) : ";
130 $answer = rtrim(fgets(STDIN, 4094));
131 if ($answer ==
'Yes') {
133 }
else if ($answer ==
'No') {
139 foreach ($orphans as $assetid => $files) {
140 echo
"[Processing $assetid]\n";
141 foreach ($files as $file) {
143 echo str_repeat(
' ', 4).
"Successful : $file deleted\n";
145 trigger_error(
"Failed to delete $file\n", E_USER_ERROR);
160 echo
"USAGE: system_integrity_data_files.php <system_root> [--delete-orphans]\n\n";
161 echo
"--delete-orphans : If this option is given, the script deletes the found orphans file\n";
162 echo
"\nNOTE: Please make sure that you run the script without --delete-orphans option first.\n";
178 function printAssetid($assetid)
180 $str =
'[ #'.$assetid.
' ]';
181 if (strlen($str) > 36) {
182 $str = substr($str, 0, 36).
'...';
184 printf (
'%s%'.(40 - strlen($str)).
's', $str,
'');
197 function printStatus($status)
199 echo
"[ $status ]\n";