deleteArchivedFiles.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Delete archived (non-current) files from the database
  4. *
  5. * Based on deleteOldRevisions.php by Rob Church.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. * @ingroup Maintenance
  24. * @author Aaron Schulz
  25. */
  26. require_once __DIR__ . '/Maintenance.php';
  27. /**
  28. * Maintenance script to delete archived (non-current) files from the database.
  29. *
  30. * @ingroup Maintenance
  31. */
  32. class DeleteArchivedFiles extends Maintenance {
  33. public function __construct() {
  34. parent::__construct();
  35. $this->addDescription( 'Deletes all archived images.' );
  36. $this->addOption( 'delete', 'Perform the deletion' );
  37. $this->addOption( 'force', 'Force deletion of rows from filearchive' );
  38. }
  39. public function execute() {
  40. if ( !$this->hasOption( 'delete' ) ) {
  41. $this->output( "Use --delete to actually confirm this script\n" );
  42. return;
  43. }
  44. # Data should come off the master, wrapped in a transaction
  45. $dbw = $this->getDB( DB_MASTER );
  46. $this->beginTransaction( $dbw, __METHOD__ );
  47. $repo = RepoGroup::singleton()->getLocalRepo();
  48. # Get "active" revisions from the filearchive table
  49. $this->output( "Searching for and deleting archived files...\n" );
  50. $res = $dbw->select(
  51. 'filearchive',
  52. [ 'fa_id', 'fa_storage_group', 'fa_storage_key', 'fa_sha1', 'fa_name' ],
  53. '',
  54. __METHOD__
  55. );
  56. $count = 0;
  57. foreach ( $res as $row ) {
  58. $key = $row->fa_storage_key;
  59. if ( !strlen( $key ) ) {
  60. $this->output( "Entry with ID {$row->fa_id} has empty key, skipping\n" );
  61. continue;
  62. }
  63. /** @var LocalFile $file */
  64. $file = $repo->newFile( $row->fa_name );
  65. try {
  66. $file->lock();
  67. } catch ( LocalFileLockError $e ) {
  68. $this->error( "Could not acquire lock on '{$row->fa_name}', skipping\n" );
  69. continue;
  70. }
  71. $group = $row->fa_storage_group;
  72. $id = $row->fa_id;
  73. $path = $repo->getZonePath( 'deleted' ) .
  74. '/' . $repo->getDeletedHashPath( $key ) . $key;
  75. if ( isset( $row->fa_sha1 ) ) {
  76. $sha1 = $row->fa_sha1;
  77. } else {
  78. // old row, populate from key
  79. $sha1 = LocalRepo::getHashFromKey( $key );
  80. }
  81. // Check if the file is used anywhere...
  82. $inuse = $dbw->selectField(
  83. 'oldimage',
  84. '1',
  85. [
  86. 'oi_sha1' => $sha1,
  87. $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE
  88. ],
  89. __METHOD__,
  90. [ 'FOR UPDATE' ]
  91. );
  92. $needForce = true;
  93. if ( !$repo->fileExists( $path ) ) {
  94. $this->output( "Notice - file '$key' not found in group '$group'\n" );
  95. } elseif ( $inuse ) {
  96. $this->output( "Notice - file '$key' is still in use\n" );
  97. } elseif ( !$repo->quickPurge( $path ) ) {
  98. $this->output( "Unable to remove file $path, skipping\n" );
  99. $file->unlock();
  100. continue; // don't delete even with --force
  101. } else {
  102. $needForce = false;
  103. }
  104. if ( $needForce ) {
  105. if ( $this->hasOption( 'force' ) ) {
  106. $this->output( "Got --force, deleting DB entry\n" );
  107. } else {
  108. $file->unlock();
  109. continue;
  110. }
  111. }
  112. $count++;
  113. $dbw->delete( 'filearchive', [ 'fa_id' => $id ], __METHOD__ );
  114. $file->unlock();
  115. }
  116. $this->commitTransaction( $dbw, __METHOD__ );
  117. $this->output( "Done! [$count file(s)]\n" );
  118. }
  119. }
  120. $maintClass = "DeleteArchivedFiles";
  121. require_once RUN_MAINTENANCE_IF_MAIN;