Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
system_integrity_clean_old_files.php
1 <?php
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);
27 
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";
31  exit();
32 }
33 
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";
36  exit();
37 }
38 
39 require_once 'Console/Getopt.php';
40 $shortopt = '';
41 $longopt = Array('delete-orphans');
42 
43 $args = Console_Getopt::readPHPArgv();
44 array_shift($args);
45 $options = Console_Getopt::getopt($args, $shortopt, $longopt);
46 
47 $DELETE = FALSE;
48 foreach ($options[0] as $option) {
49  switch ($option[0]) {
50  case '--delete-orphans':
51  $DELETE = TRUE;
52  break;
53  }
54 
55 }
56 
57 require_once $SYSTEM_ROOT.'/core/include/init.inc';
58 
59 // check that the correct root password was entered
60 $root_user = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
61 
62 // log in as root
63 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
64  echo "ERROR: Failed logging in as root user\n";
65  exit();
66 }
67 
68 $fv = $GLOBALS['SQ_SYSTEM']->getFileVersioning();
69 $children = $GLOBALS['SQ_SYSTEM']->am->getTypeAssetids('file', FALSE);
70 $count = 0;
71 $orphans = Array();
72 
73 // scan the file type of assets and find any orphan files in the data directories
74 echo "[Total ".count($children)." assets will be checked.]\n\n";
75 foreach ($children as $assetid) {
76 
77  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
78  $file_name = $asset->attr('name');
79 
80  $data_paths = Array($asset->data_path);
81  if (file_exists($asset->data_path_public)) {
82  $data_paths[] = $asset->data_path_public;
83  }
84 
85  // find the old version of files for this file asset
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;
93  } else {
94  $ophs = array_diff($diff, Array($file_name));
95  foreach ($ophs as $file) {
96  $orphans[$assetid][] = $data_path.'/'.$file;
97  $count++;
98  }
99  }
100  }
101 
102  printAssetid($assetid);
103  if (isset($orphans[$assetid])) {
104  printStatus('FOUND');
105  } else {
106  printStatus('OK');
107  }
108 
109  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($asset, TRUE);
110 
111 }//end foreach $children
112 
113 
114 // if there is any orphan files, print them
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";
120  }
121 } else {
122  echo "No orphan files found\nBye\n";
123  exit();
124 }
125 
126 // are we really going to delete them?
127 if ($DELETE) {
128  do {
129  echo "DO YOU REALLY WANT TO DELETE THESE FILES? (Yes/No) : ";
130  $answer = rtrim(fgets(STDIN, 4094));
131  if ($answer == 'Yes') {
132  break;
133  } else if ($answer == 'No') {
134  echo "\nBye\n";
135  exit();
136  }
137  } while (TRUE);
138 
139  foreach ($orphans as $assetid => $files) {
140  echo "[Processing $assetid]\n";
141  foreach ($files as $file) {
142  if (unlink($file)) {
143  echo str_repeat(' ', 4)."Successful : $file deleted\n";
144  } else {
145  trigger_error("Failed to delete $file\n", E_USER_ERROR);
146  }
147  }
148  }
149 }
150 
151 
158 function usage()
159 {
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";
163  exit();
164 
165 }//end usage()
166 
167 
178 function printAssetid($assetid)
179 {
180  $str = '[ #'.$assetid.' ]';
181  if (strlen($str) > 36) {
182  $str = substr($str, 0, 36).'...';
183  }
184  printf ('%s%'.(40 - strlen($str)).'s', $str,'');
185 
186 }//end printAssetName()
187 
188 
197 function printStatus($status)
198 {
199  echo "[ $status ]\n";
200 
201 }//end printUpdateStatus()
202 
203 
204 ?>
205 
206