/update/common/scripts/4.6/removetrashedimages.php

https://bitbucket.org/ericsagnes/ezpublish-multisite · PHP · 219 lines · 106 code · 15 blank · 98 comment · 2 complexity · f49ce60e2706605bbeef8388eb89b800 MD5 · raw file

  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * File containing the script to cleanup duplicated trashed images.
  5. *
  6. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  7. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  8. * @version 2012.8
  9. * @package update
  10. * @see Issue 17781
  11. */
  12. /**
  13. * Traverses $directory to recursively find "trashed" directories and remove them.
  14. *
  15. * @param string $directory
  16. */
  17. function filesystemCleanup( $directory )
  18. {
  19. foreach (
  20. new RecursiveIteratorIterator(
  21. new RecursiveDirectoryIterator( $directory ),
  22. RecursiveIteratorIterator::CHILD_FIRST
  23. ) as $entry )
  24. {
  25. if ( $entry->getFilename() !== "trashed" )
  26. {
  27. continue;
  28. }
  29. foreach (
  30. new RecursiveIteratorIterator(
  31. new RecursiveDirectoryIterator( $entry ),
  32. RecursiveIteratorIterator::CHILD_FIRST
  33. ) as $trashedDirectoryEntry )
  34. {
  35. if ( $trashedDirectoryEntry->isDir() )
  36. {
  37. rmdir( $trashedDirectoryEntry );
  38. }
  39. else
  40. {
  41. unlink( $trashedDirectoryEntry );
  42. }
  43. }
  44. rmdir( $entry );
  45. }
  46. }
  47. /**
  48. * Remove content from "trashed" directories under $directory from ezdbfile and
  49. * ezdbfile_data tables.
  50. *
  51. * @param string $directory
  52. */
  53. function databaseCleanup( $directory )
  54. {
  55. list( $host, $user, $pass, $db, $port, $socket ) = eZINI::instance( "file.ini" )
  56. ->variableMulti(
  57. "ClusteringSettings",
  58. array(
  59. "DBHost",
  60. "DBUser",
  61. "DBPassword",
  62. "DBName",
  63. "DBPort",
  64. "DBSocket",
  65. )
  66. );
  67. $db = new mysqli( $host, $user, $pass, $db, $port, $socket );
  68. // Creating a temporary table to hold name_hash to delete from
  69. // both ezdbfile and ezdbfile_data tables.
  70. $db->query( "
  71. CREATE TEMPORARY TABLE ezdbfile_cleanup (
  72. name_hash VARCHAR(34) NOT NULL,
  73. PRIMARY KEY (name_hash)
  74. )"
  75. );
  76. // Replacing \ and / by a regex equivalent matching both.
  77. $imagesDirectory = str_replace(
  78. array( "\\", "/" ),
  79. "[\\\\/]",
  80. // Removing trailing \ and /
  81. rtrim(
  82. $directory,
  83. "\\/"
  84. )
  85. );
  86. // Filling the temporary table with trashed objects to remove
  87. $db->query( "
  88. INSERT INTO ezdbfile_cleanup
  89. SELECT name_hash
  90. FROM ezdbfile
  91. WHERE datatype LIKE 'image/%' AND
  92. name REGEXP '^{$imagesDirectory}[\\\\/].*[\\\\/].*[\\\\/]trashed[\\\\/]'"
  93. );
  94. // Removing from both tables at once.
  95. $db->query( "
  96. DELETE FROM ezdbfile, ezdbfile_data
  97. USING ezdbfile
  98. JOIN ezdbfile_data ON ezdbfile.name_hash = ezdbfile_data.name_hash
  99. JOIN ezdbfile_cleanup ON ezdbfile.name_hash = ezdbfile_cleanup.name_hash"
  100. );
  101. $db->close();
  102. }
  103. /**
  104. * Removes content from "trashed" directories under $directory from ezdfsfile table
  105. * and below the mount point.
  106. *
  107. * @param string $directory
  108. */
  109. function dfsCleanup( $directory )
  110. {
  111. list( $host, $user, $pass, $db, $port, $socket, $mountPoint ) = eZINI::instance( "file.ini" )
  112. ->variableMulti(
  113. "eZDFSClusteringSettings",
  114. array(
  115. "DBHost",
  116. "DBUser",
  117. "DBPassword",
  118. "DBName",
  119. "DBPort",
  120. "DBSocket",
  121. "MountPointPath",
  122. )
  123. );
  124. $db = new mysqli( $host, $user, $pass, $db, $port, $socket );
  125. // Replacing \ and / by a regex equivalent matching both.
  126. $imagesDirectory = str_replace(
  127. array( "\\", "/" ),
  128. "[\\\\/]",
  129. // Removing trailing \ and /
  130. rtrim(
  131. $directory,
  132. "\\/"
  133. )
  134. );
  135. $db->query( "
  136. DELETE FROM ezdfsfile
  137. WHERE scope = 'image' AND
  138. name REGEXP '^{$imagesDirectory}[\\\\/].*[\\\\/].*[\\\\/]trashed[\\\\/]'"
  139. );
  140. $db->close();
  141. array_map( "filesystemCleanup", glob( "$mountPoint/$directory/*" ) );
  142. }
  143. require "autoload.php";
  144. $script = eZScript::instance(
  145. array(
  146. "description" => "eZ Publish trashed images sanitizer script (#017781).",
  147. "use-session" => false,
  148. "use-modules" => false,
  149. "use-extensions" => true,
  150. )
  151. );
  152. $script->startup();
  153. $options = $script->getOptions(
  154. "[n]",
  155. "",
  156. array(
  157. "-q" => "Quiet mode",
  158. "n" => "Do not wait"
  159. )
  160. );
  161. $script->initialize();
  162. $cli = eZCLI::instance();
  163. if ( !isset( $options['n'] ) )
  164. {
  165. $cli->warning( "This cleanup script will remove any images from trashed objects." );
  166. $cli->warning( "For more details about this cleanup, take a look at: http://issues.ez.no/17781." );
  167. $cli->warning();
  168. $cli->warning( "IT IS YOUR RESPONSABILITY TO TAKE CARE THAT NO ITEMS REMAINS IN TRASH BEFORE RUNNING THIS SCRIPT." );
  169. $cli->warning();
  170. $cli->warning( "You have 30 seconds to break the script (press Ctrl-C)." );
  171. sleep( 30 );
  172. }
  173. $fileHandler = eZINI::instance( "file.ini" )->variable( "ClusteringSettings", "FileHandler" );
  174. $directory = eZSys::varDirectory() . "/storage/images";
  175. switch ( strtolower( $fileHandler ) )
  176. {
  177. case "ezfsfilehandler":
  178. case "ezfs2filehandler":
  179. array_map( "filesystemCleanup", glob( "$directory/*" ) );
  180. break;
  181. case "ezdbfilehandler":
  182. databaseCleanup( $directory );
  183. break;
  184. case "ezdfsfilehandler":
  185. dfsCleanup( $directory );
  186. break;
  187. default:
  188. $cli->error( "Unsupported '$fileHandler' FileHandler." );
  189. }
  190. $script->shutdown();
  191. ?>