Advertisement
Guest User

detect zombie files

a guest
Jul 25th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.79 KB | None | 0 0
  1. <?php
  2.  
  3. $SERVERNAME = "localhost";
  4. $USERNAME = "root";
  5. $PASSWORD = "root";
  6. $DATABASE = "uss";
  7.  
  8. $ROOTPATH = "/var/www/blabla";
  9. $OUT_FILE_PATH = "zombies.txt";
  10.  
  11. $REMOVE_ZOMBIE_FILES = false;
  12. $REMOVE_FROM_DATABASE = false;
  13.  
  14. // Create connection
  15. $conn = new mysqli($SERVERNAME, $USERNAME, $PASSWORD, $DATABASE);
  16.  
  17. // Check connection
  18. if ($conn->connect_error) {
  19.     die("Connection failed: " . $conn->connect_error);
  20. }
  21.  
  22. echo "Connected successfully\n";
  23.  
  24. // Prepare queries
  25. $joinTables = [
  26.     'message_file',
  27.     'third_party_license_verification_files',
  28.     'training_exercise_file',
  29.     'user',
  30.     'work_expirence_verification_files',
  31.     'easy_sports_resource',
  32.     'resource',
  33. ];
  34.  
  35. $jsonResultTables = [
  36.     'application_result',
  37.     'assignment_result',
  38. ];
  39.  
  40. $queries = [
  41.     'file' => "SELECT f.id as id FROM file f",
  42. ];
  43.  
  44. foreach ($joinTables as $table) {
  45.     $queries[$table] = "SELECT f.id as id FROM file f join $table a on a.file_id = f.id";
  46. }
  47.  
  48. foreach ($jsonResultTables as $table) {
  49.     $queries[$table] = "SELECT a.result_given as result FROM $table a";
  50. }
  51.  
  52.  
  53. // Executing queries
  54. $usedFileIds = $allFileIds = [];
  55.  
  56. foreach ($queries as $tableName => $query) {
  57.    
  58.     $result = $conn->query($query);
  59.     if (!$result) {
  60.         echo "Error executing query: ".$query."\n".$conn->error;
  61.         exit;
  62.     }
  63.  
  64.  
  65.     $ids = [];
  66.  
  67.     $isJson = in_array($tableName, ['application_result', 'assignment_result']);
  68.  
  69.  
  70.     if ($result->num_rows > 0) {
  71.         while($row = $result->fetch_assoc()) {
  72.             if ($isJson) {
  73.                 $data = $row['result'];
  74.                 $jsonData = json_decode($data, true);
  75.  
  76.                 searchIds($ids, $jsonData);
  77.  
  78.                 continue;
  79.             }
  80.  
  81.             $ids[] = $row["id"];
  82.         }
  83.     }
  84.  
  85.     if ($isJson) echo "[FROM-JSON] ";
  86.     echo "Found ".count($ids)." entities on {$tableName}\n";
  87.  
  88.     $result->free();
  89.  
  90.     if ($tableName == 'file') {
  91.         $allFileIds = $ids;
  92.         continue;
  93.     }
  94.  
  95.     $diff = array_intersect($usedFileIds, $ids);
  96.     if (count($diff)) {
  97.         echo "Warning! Found already ".count($diff)." entities on other tables!\n";
  98.     }
  99.  
  100.     $usedFileIds = array_merge($usedFileIds, $ids);
  101.     echo "  Total found used files: ".count($usedFileIds)."\n";
  102. }
  103.  
  104.  
  105. $unusedFileIds = array_diff($allFileIds, $usedFileIds);
  106. $unusedPercentage = number_format(count($unusedFileIds) / count($allFileIds) * 100, 2);
  107.  
  108. echo "\nThere was found ".count($unusedFileIds)." zombie files ( {$unusedPercentage} % )\n";
  109. if (!$unusedFileIds) {
  110.     echo "Nothing to delete. Exiting\n";
  111.     exit;
  112. }
  113.  
  114. // prepare path files
  115. $paths = [];
  116. $query = "SELECT f.path as p FROM file f where id in (". implode(',', $unusedFileIds) .")";
  117. $result = $conn->query($query);
  118. if (!$result) {
  119.     echo "Error executing query: ".$query."\n".$conn->error;
  120.     exit;
  121. }
  122. if ($result->num_rows > 0) {
  123.     while($row = $result->fetch_assoc()) {
  124.         $paths[] = $row["p"];
  125.     }
  126. }
  127.  
  128. // remove unused entities
  129. if ($REMOVE_FROM_DATABASE) {
  130.     $query = "DELETE FROM file where id in (". implode(',', $unusedFileIds) .")";
  131.     $conn->exec($query);
  132.     echo "Records (".$unusedFileIds.") deleted successfully\n";
  133. }
  134.  
  135. // removing path files
  136. $unexistingFiles = $readyToDeleteFiles = [];
  137. foreach ($paths as $path) {
  138.     $fullPath = $ROOTPATH.$path;
  139.     if (!file_exists($fullPath)) {
  140.         $unexistingFiles[] = $path;
  141.         continue;
  142.     }
  143.     $readyToDeleteFiles[] = $fullPath;
  144.     if ($REMOVE_ZOMBIE_FILES) {
  145.         unlink($fullPath);
  146.     }
  147. }
  148.  
  149. if (count($unexistingFiles) > 0) {
  150.  
  151.     $unexistingPercentage = number_format(count($unexistingFiles) / count($paths) * 100, 2);
  152.  
  153.     echo "Unexisting files: ".count($unexistingFiles)." ( {$unexistingPercentage} % )\n";
  154. }
  155.  
  156. if (count($readyToDeleteFiles) > 0) {
  157.     $myfile = fopen($OUT_FILE_PATH, "w+") or die("Unable to open file!");
  158.     foreach ($readyToDeleteFiles as $filePath) {
  159.         $txt = $filePath."\n";
  160.         fwrite($myfile, $txt);
  161.     }
  162.     fclose($myfile);
  163.  
  164.     if ($REMOVE_ZOMBIE_FILES) {
  165.         echo "Deleted files: ".count($readyToDeleteFiles)."\n";
  166.     } else {
  167.         echo "Ready to delete files: ".count($readyToDeleteFiles)."\n";
  168.     }
  169.  
  170.     echo "The files path are written in ".$OUT_FILE_PATH."\n";
  171. }
  172.  
  173.  
  174.  
  175. mysqli_close($conn);
  176.  
  177. /*=================================
  178. =            functions            =
  179. =================================*/
  180.  
  181. function searchIds(array &$ids, array $json = null)
  182. {
  183.     if (!$json) return;
  184.  
  185.     if (!is_array($json)) return;
  186.  
  187.     if (isset($json['path']) && isset($json['id'])) {
  188.         $ids[] = $json['id'];
  189.     }
  190.  
  191.     foreach ($json as $key => $value) {
  192.         if (is_array($value)) {
  193.             searchIds($ids, $value);
  194.         }
  195.     }
  196. }
  197.  
  198. /*=====  End of functions  ======*/
  199.  
  200. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement