# HG changeset patch # User Matti Hamalainen # Date 1544620301 -7200 # Node ID 6ad17bc3d1a65f9c4be550e3004bc58bbdfef698 # Parent d0bcc6c7fc660ea515a47c391af48a14476b74e4 Add 'trash' option to 'clean' command to clean up any unmanaged files residing inside thumbnail/medium image subdirectories. Also clarify the help for 'clean' command. diff -r d0bcc6c7fc66 -r 6ad17bc3d1a6 mgtool.php --- a/mgtool.php Wed Dec 12 14:52:00 2018 +0200 +++ b/mgtool.php Wed Dec 12 15:11:41 2018 +0200 @@ -35,6 +35,7 @@ define("GCLEAN_CACHES" , 0x01); define("GCLEAN_IMAGES" , 0x02); +define("GCLEAN_TRASH" , 0x04); define("GCLEAN_ALL" , 0x0f); define("GUPD_MED_IMAGE" , 0x01); @@ -364,8 +365,9 @@ } -function mgDeleteQuiet($path, &$noDelete) +function mgDeleteConditional($path, &$noDelete) { + global $flagDoDelete, $countDoDelete; if (is_dir($path)) { if (($dirHandle = @opendir($path)) === FALSE) @@ -374,22 +376,28 @@ while (($dirFile = @readdir($dirHandle)) !== FALSE) { if ($dirFile != "." && $dirFile != "..") - mgDeleteQuiet($path."/".$dirFile, $noDelete); + mgDeleteConditional($path."/".$dirFile, $noDelete); } closedir($dirHandle); if (!array_key_exists($path, $noDelete)) { - //echo "DEL DIR '".$path."'\n"; - rmdir($path); + $countDoDelete++; + if ($flagDoDelete) + rmdir($path); + else + echo "DEL DIR '".$path."'\n"; } } else if (file_exists($path) && !array_key_exists($path, $noDelete)) { - //echo "DEL FILE '".$path."'\n"; - unlink($path); + $countDoDelete++; + if ($flagDoDelete) + unlink($path); + else + echo "DEL FILE '".$path."'\n"; } } @@ -532,6 +540,12 @@ mgCheckQuit(); + $tnPath = $path."/".$galTNPath; + $medPath = $path."/".$galMedPath; + $generatedFiles = []; + $generatedFiles[$tnPath] = 1; + $generatedFiles[$medPath] = 1; + // Cleanup mode if ($mode == GCMD_CLEAN) { @@ -547,6 +561,21 @@ mgDelete($path."/".$galTNPath, FALSE); mgDelete($path."/".$galMedPath, FALSE); } + + if ($galCleanFlags & GCLEAN_TRASH) + { + foreach ($entries as $ename => &$edata) + { + $medFilename = $medPath."/".$ename.".".mgGetSetting("med_format"); + $tnFilename = $tnPath."/".$ename.".".mgGetSetting("tn_format"); + $generatedFiles[$medFilename] = 1; + $generatedFiles[$tnFilename] = 1; + } + + // Delete any "trash" files from medium/thumbnail dirs + mgDeleteConditional($tnPath, $generatedFiles); + mgDeleteConditional($medPath, $generatedFiles); + } } } else @@ -572,12 +601,6 @@ } // Start actual processing - $tnPath = $path."/".$galTNPath; - $medPath = $path."/".$galMedPath; - $generatedFiles = []; - $generatedFiles[$tnPath] = 1; - $generatedFiles[$medPath] = 1; - $nentries = count($entries); $nentry = 0; echo $path." .. "; @@ -587,6 +610,9 @@ $nentry++; $efilename = $path."/".$ename; + $medFilename = $medPath."/".$ename.".".mgGetSetting("med_format"); + $tnFilename = $tnPath."/".$ename.".".mgGetSetting("tn_format"); + $capFilename = $path."/".$edata["base"].".txt"; if (array_key_exists($ename, $galEntries)) $galEntry = &$galEntries[$ename]; @@ -602,17 +628,10 @@ $edata[$ckey] = $cval; } - // Handle entry based on type if ($edata["type"] == 0) { $updFlags = 0; - $medFilename = $medPath."/".$ename.".".mgGetSetting("med_format"); - $tnFilename = $tnPath."/".$ename.".".mgGetSetting("tn_format"); - $capFilename = $path."/".$edata["base"].".txt"; - - $generatedFiles[$medFilename] = 1; - $generatedFiles[$tnFilename] = 1; // Check what we need to update .. if (!file_exists($medFilename) || filemtime($medFilename) < $edata["mtime"]) @@ -691,10 +710,6 @@ } } - // Delete any "trash" files from medium/thumbnail dirs - mgDeleteQuiet($tnPath, $generatedFiles); - mgDeleteQuiet($medPath, $generatedFiles); - echo "\r".$path." ..... DONE\n"; mgCheckQuit(TRUE); @@ -847,9 +862,11 @@ " and EXIF/caption/etc information to be re-scanned even\n". " if the file timestamps do not justify re-scanning.\n". "\n". - " clean [path]\n". - " Delete all generated files, or data cache files or\n". - " everything inside medium/thumbnail image directories.\n". + " clean