CopyFileOp.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * Copy a file from one storage path to another in the backend.
  25. * Parameters for this operation are outlined in FileBackend::doOperations().
  26. */
  27. class CopyFileOp 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 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['dst']] = true;
  60. $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
  61. }
  62. return $status; // safe to call attempt()
  63. }
  64. protected function doAttempt() {
  65. if ( $this->overwriteSameCase ) {
  66. $status = StatusValue::newGood(); // nothing to do
  67. } elseif ( $this->params['src'] === $this->params['dst'] ) {
  68. // Just update the destination file headers
  69. $headers = $this->getParam( 'headers' ) ?: [];
  70. $status = $this->backend->describeInternal( $this->setFlags( [
  71. 'src' => $this->params['dst'], 'headers' => $headers
  72. ] ) );
  73. } else {
  74. // Copy the file to the destination
  75. $status = $this->backend->copyInternal( $this->setFlags( $this->params ) );
  76. }
  77. return $status;
  78. }
  79. public function storagePathsRead() {
  80. return [ $this->params['src'] ];
  81. }
  82. public function storagePathsChanged() {
  83. return [ $this->params['dst'] ];
  84. }
  85. }