CreateFileOp.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup FileBackend
  20. */
  21. /**
  22. * Create a file in the backend with the given content.
  23. * Parameters for this operation are outlined in FileBackend::doOperations().
  24. */
  25. class CreateFileOp extends FileOp {
  26. protected function allowedParams() {
  27. return [
  28. [ 'content', 'dst' ],
  29. [ 'overwrite', 'overwriteSame', 'headers' ],
  30. [ 'dst' ]
  31. ];
  32. }
  33. protected function doPrecheck( array &$predicates ) {
  34. $status = StatusValue::newGood();
  35. // Check if the source data is too big
  36. $maxBytes = $this->backend->maxFileSizeInternal();
  37. if ( strlen( $this->getParam( 'content' ) ) > $maxBytes ) {
  38. $status->fatal( 'backend-fail-maxsize', $this->params['dst'], $maxBytes );
  39. return $status;
  40. }
  41. // Check if an incompatible destination file exists
  42. $status->merge( $this->precheckDestExistence( $predicates ) );
  43. $this->params['dstExists'] = $this->destExists; // see FileBackendStore::setFileCache()
  44. if ( $status->isOK() ) {
  45. // Update file existence predicates
  46. $predicates['exists'][$this->params['dst']] = true;
  47. $predicates['sha1'][$this->params['dst']] = $this->sourceSha1;
  48. }
  49. return $status; // safe to call attempt()
  50. }
  51. protected function doAttempt() {
  52. if ( $this->overwriteSameCase ) {
  53. $status = StatusValue::newGood(); // nothing to do
  54. } else {
  55. // Create the file at the destination
  56. $status = $this->backend->createInternal( $this->setFlags( $this->params ) );
  57. }
  58. return $status;
  59. }
  60. protected function getSourceSha1Base36() {
  61. return Wikimedia\base_convert( sha1( $this->params['content'] ), 16, 36, 31 );
  62. }
  63. public function storagePathsChanged() {
  64. return [ $this->params['dst'] ];
  65. }
  66. }