MoveFileOp.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Helper class for representing operations with transaction support.
  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 FileBackend
  22. */
  23. /**
  24. * Move a file from one storage path to another in the backend.
  25. * Parameters for this operation are outlined in FileBackend::doOperations().
  26. */
  27. class MoveFileOp extends FileOp {
  28. protected function allowedParams() {
  29. return [
  30. [ 'src', 'dst' ],
  31. [ 'overwrite', 'overwriteSame', 'ignoreMissingSource', 'headers' ],
  32. [ 'src', 'dst' ]
  33. ];
  34. }
  35. protected function doPrecheck( array &$predicates ) {
  36. $status = StatusValue::newGood();
  37. // Check source file existence
  38. $srcExists = $this->fileExists( $this->params['src'], $predicates );
  39. if ( $srcExists === false ) {
  40. if ( $this->getParam( 'ignoreMissingSource' ) ) {
  41. $this->doOperation = false; // no-op
  42. // Update file existence predicates (cache 404s)
  43. $predicates['exists'][$this->params['src']] = false;
  44. $predicates['sha1'][$this->params['src']] = false;
  45. return $status; // nothing to do
  46. } else {
  47. $status->fatal( 'backend-fail-notexists', $this->params['src'] );
  48. return $status;
  49. }
  50. } elseif ( $srcExists === FileBackend::EXISTENCE_ERROR ) {
  51. $status->fatal( 'backend-fail-stat', $this->params['src'] );
  52. return $status;
  53. }
  54. // Check if an incompatible destination file exists
  55. $status->merge( $this->precheckDestExistence( $predicates ) );
  56. $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
  57. if ( $status->isOK() ) {
  58. // Update file existence predicates
  59. $predicates['exists'][$this->params['src']] = false;
  60. $predicates['sha1'][$this->params['src']] = false;
  61. $predicates['exists'][$this->params['dst']] = true;
  62. $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
  63. }
  64. return $status; // safe to call attempt()
  65. }
  66. protected function doAttempt() {
  67. if ( $this->overwriteSameCase ) {
  68. if ( $this->params['src'] === $this->params['dst'] ) {
  69. // Do nothing to the destination (which is also the source)
  70. $status = StatusValue::newGood();
  71. } else {
  72. // Just delete the source as the destination file needs no changes
  73. $status = $this->backend->deleteInternal( $this->setFlags(
  74. [ 'src' => $this->params['src'] ]
  75. ) );
  76. }
  77. } elseif ( $this->params['src'] === $this->params['dst'] ) {
  78. // Just update the destination file headers
  79. $headers = $this->getParam( 'headers' ) ?: [];
  80. $status = $this->backend->describeInternal( $this->setFlags(
  81. [ 'src' => $this->params['dst'], 'headers' => $headers ]
  82. ) );
  83. } else {
  84. // Move the file to the destination
  85. $status = $this->backend->moveInternal( $this->setFlags( $this->params ) );
  86. }
  87. return $status;
  88. }
  89. public function storagePathsRead() {
  90. return [ $this->params['src'] ];
  91. }
  92. public function storagePathsChanged() {
  93. return [ $this->params['src'], $this->params['dst'] ];
  94. }
  95. }