migrateFileRepoLayout.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. /**
  3. * Copy all files in FileRepo to an originals container using SHA1 paths.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Copy all files in FileRepo to an originals container using SHA1 paths.
  26. *
  27. * This script should be run while the repo is still set to the old layout.
  28. *
  29. * @ingroup Maintenance
  30. */
  31. class MigrateFileRepoLayout extends Maintenance {
  32. public function __construct() {
  33. parent::__construct();
  34. $this->addDescription( 'Copy files in repo to a different layout.' );
  35. $this->addOption( 'oldlayout', "Old layout; one of 'name' or 'sha1'", true, true );
  36. $this->addOption( 'newlayout', "New layout; one of 'name' or 'sha1'", true, true );
  37. $this->addOption( 'since', "Copy only files from after this timestamp", false, true );
  38. $this->setBatchSize( 50 );
  39. }
  40. public function execute() {
  41. $oldLayout = $this->getOption( 'oldlayout' );
  42. if ( !in_array( $oldLayout, [ 'name', 'sha1' ] ) ) {
  43. $this->fatalError( "Invalid old layout." );
  44. }
  45. $newLayout = $this->getOption( 'newlayout' );
  46. if ( !in_array( $newLayout, [ 'name', 'sha1' ] ) ) {
  47. $this->fatalError( "Invalid new layout." );
  48. }
  49. $since = $this->getOption( 'since' );
  50. $repo = $this->getRepo();
  51. $be = $repo->getBackend();
  52. if ( $be instanceof FileBackendDBRepoWrapper ) {
  53. $be = $be->getInternalBackend(); // avoid path translations for this script
  54. }
  55. $dbw = $repo->getMasterDB();
  56. $origBase = $be->getContainerStoragePath( "{$repo->getName()}-original" );
  57. $startTime = wfTimestampNow();
  58. // Do current and archived versions...
  59. $conds = [];
  60. if ( $since ) {
  61. $conds[] = 'img_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $since ) );
  62. }
  63. $batchSize = $this->getBatchSize();
  64. $batch = [];
  65. $lastName = '';
  66. do {
  67. $res = $dbw->select( 'image',
  68. [ 'img_name', 'img_sha1' ],
  69. array_merge( [ 'img_name > ' . $dbw->addQuotes( $lastName ) ], $conds ),
  70. __METHOD__,
  71. [ 'LIMIT' => $batchSize, 'ORDER BY' => 'img_name' ]
  72. );
  73. foreach ( $res as $row ) {
  74. $lastName = $row->img_name;
  75. /** @var LocalFile $file */
  76. $file = $repo->newFile( $row->img_name );
  77. // Check in case SHA1 rows are not populated for some files
  78. $sha1 = strlen( $row->img_sha1 ) ? $row->img_sha1 : $file->getSha1();
  79. if ( !strlen( $sha1 ) ) {
  80. $this->error( "Image SHA-1 not known for {$row->img_name}." );
  81. } else {
  82. if ( $oldLayout === 'sha1' ) {
  83. $spath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
  84. } else {
  85. $spath = $file->getPath();
  86. }
  87. if ( $newLayout === 'sha1' ) {
  88. $dpath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
  89. } else {
  90. $dpath = $file->getPath();
  91. }
  92. $status = $be->prepare( [
  93. 'dir' => dirname( $dpath ), 'bypassReadOnly' => 1 ] );
  94. if ( !$status->isOK() ) {
  95. $this->error( print_r( $status->getErrors(), true ) );
  96. }
  97. $batch[] = [ 'op' => 'copy', 'overwrite' => true,
  98. 'src' => $spath, 'dst' => $dpath, 'img' => $row->img_name ];
  99. }
  100. foreach ( $file->getHistory() as $ofile ) {
  101. $sha1 = $ofile->getSha1();
  102. if ( !strlen( $sha1 ) ) {
  103. $this->error( "Image SHA-1 not set for {$ofile->getArchiveName()}." );
  104. continue;
  105. }
  106. if ( $oldLayout === 'sha1' ) {
  107. $spath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
  108. } elseif ( $ofile->isDeleted( File::DELETED_FILE ) ) {
  109. $spath = $be->getContainerStoragePath( "{$repo->getName()}-deleted" ) .
  110. '/' . $repo->getDeletedHashPath( $sha1 ) .
  111. $sha1 . '.' . $ofile->getExtension();
  112. } else {
  113. $spath = $ofile->getPath();
  114. }
  115. if ( $newLayout === 'sha1' ) {
  116. $dpath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
  117. } else {
  118. $dpath = $ofile->getPath();
  119. }
  120. $status = $be->prepare( [
  121. 'dir' => dirname( $dpath ), 'bypassReadOnly' => 1 ] );
  122. if ( !$status->isOK() ) {
  123. $this->error( print_r( $status->getErrors(), true ) );
  124. }
  125. $batch[] = [ 'op' => 'copy', 'overwrite' => true,
  126. 'src' => $spath, 'dst' => $dpath, 'img' => $ofile->getArchiveName() ];
  127. }
  128. if ( count( $batch ) >= $batchSize ) {
  129. $this->runBatch( $batch, $be );
  130. $batch = [];
  131. }
  132. }
  133. } while ( $res->numRows() );
  134. if ( count( $batch ) ) {
  135. $this->runBatch( $batch, $be );
  136. }
  137. // Do deleted versions...
  138. $conds = [];
  139. if ( $since ) {
  140. $conds[] = 'fa_deleted_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $since ) );
  141. }
  142. $batch = [];
  143. $lastId = 0;
  144. do {
  145. $res = $dbw->select( 'filearchive', [ 'fa_storage_key', 'fa_id', 'fa_name' ],
  146. array_merge( [ 'fa_id > ' . $dbw->addQuotes( $lastId ) ], $conds ),
  147. __METHOD__,
  148. [ 'LIMIT' => $batchSize, 'ORDER BY' => 'fa_id' ]
  149. );
  150. foreach ( $res as $row ) {
  151. $lastId = $row->fa_id;
  152. $sha1Key = $row->fa_storage_key;
  153. if ( !strlen( $sha1Key ) ) {
  154. $this->error( "Image SHA-1 not set for file #{$row->fa_id} (deleted)." );
  155. continue;
  156. }
  157. $sha1 = substr( $sha1Key, 0, strpos( $sha1Key, '.' ) );
  158. if ( $oldLayout === 'sha1' ) {
  159. $spath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
  160. } else {
  161. $spath = $be->getContainerStoragePath( "{$repo->getName()}-deleted" ) .
  162. '/' . $repo->getDeletedHashPath( $sha1Key ) . $sha1Key;
  163. }
  164. if ( $newLayout === 'sha1' ) {
  165. $dpath = "{$origBase}/{$sha1[0]}/{$sha1[1]}/{$sha1[2]}/{$sha1}";
  166. } else {
  167. $dpath = $be->getContainerStoragePath( "{$repo->getName()}-deleted" ) .
  168. '/' . $repo->getDeletedHashPath( $sha1Key ) . $sha1Key;
  169. }
  170. $status = $be->prepare( [
  171. 'dir' => dirname( $dpath ), 'bypassReadOnly' => 1 ] );
  172. if ( !$status->isOK() ) {
  173. $this->error( print_r( $status->getErrors(), true ) );
  174. }
  175. $batch[] = [ 'op' => 'copy', 'src' => $spath, 'dst' => $dpath,
  176. 'overwriteSame' => true, 'img' => "(ID {$row->fa_id}) {$row->fa_name}" ];
  177. if ( count( $batch ) >= $batchSize ) {
  178. $this->runBatch( $batch, $be );
  179. $batch = [];
  180. }
  181. }
  182. } while ( $res->numRows() );
  183. if ( count( $batch ) ) {
  184. $this->runBatch( $batch, $be );
  185. }
  186. $this->output( "Done (started $startTime)\n" );
  187. }
  188. protected function getRepo() {
  189. return RepoGroup::singleton()->getLocalRepo();
  190. }
  191. protected function runBatch( array $ops, FileBackend $be ) {
  192. $this->output( "Migrating file batch:\n" );
  193. foreach ( $ops as $op ) {
  194. $this->output( "\"{$op['img']}\" (dest: {$op['dst']})\n" );
  195. }
  196. $status = $be->doOperations( $ops, [ 'bypassReadOnly' => 1 ] );
  197. if ( !$status->isOK() ) {
  198. $this->output( print_r( $status->getErrors(), true ) );
  199. }
  200. $this->output( "Batch done\n\n" );
  201. }
  202. }
  203. $maintClass = MigrateFileRepoLayout::class;
  204. require_once RUN_MAINTENANCE_IF_MAIN;