SpecialUploadMogile.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup SpecialPage
  5. */
  6. /**
  7. * You will need the extension MogileClient to use this special page.
  8. */
  9. require_once( 'MogileFS.php' );
  10. /**
  11. * Entry point
  12. */
  13. function wfSpecialUploadMogile() {
  14. global $wgRequest;
  15. $form = new UploadFormMogile( $wgRequest );
  16. $form->execute();
  17. }
  18. /**
  19. * Extends Special:Upload with MogileFS.
  20. * @ingroup SpecialPage
  21. */
  22. class UploadFormMogile extends UploadForm {
  23. /**
  24. * Move the uploaded file from its temporary location to the final
  25. * destination. If a previous version of the file exists, move
  26. * it into the archive subdirectory.
  27. *
  28. * @todo If the later save fails, we may have disappeared the original file.
  29. *
  30. * @param string $saveName
  31. * @param string $tempName full path to the temporary file
  32. * @param bool $useRename Not used in this implementation
  33. */
  34. function saveUploadedFile( $saveName, $tempName, $useRename = false ) {
  35. global $wgOut;
  36. $mfs = MogileFS::NewMogileFS();
  37. $this->mSavedFile = "image!{$saveName}";
  38. if( $mfs->getPaths( $this->mSavedFile )) {
  39. $this->mUploadOldVersion = gmdate( 'YmdHis' ) . "!{$saveName}";
  40. if( !$mfs->rename( $this->mSavedFile, "archive!{$this->mUploadOldVersion}" ) ) {
  41. $wgOut->showFileRenameError( $this->mSavedFile,
  42. "archive!{$this->mUploadOldVersion}" );
  43. return false;
  44. }
  45. } else {
  46. $this->mUploadOldVersion = '';
  47. }
  48. if ( $this->mStashed ) {
  49. if (!$mfs->rename($tempName,$this->mSavedFile)) {
  50. $wgOut->showFileRenameError($tempName, $this->mSavedFile );
  51. return false;
  52. }
  53. } else {
  54. if ( !$mfs->saveFile($this->mSavedFile,'normal',$tempName )) {
  55. $wgOut->showFileCopyError( $tempName, $this->mSavedFile );
  56. return false;
  57. }
  58. unlink($tempName);
  59. }
  60. return true;
  61. }
  62. /**
  63. * Stash a file in a temporary directory for later processing
  64. * after the user has confirmed it.
  65. *
  66. * If the user doesn't explicitly cancel or accept, these files
  67. * can accumulate in the temp directory.
  68. *
  69. * @param string $saveName - the destination filename
  70. * @param string $tempName - the source temporary file to save
  71. * @return string - full path the stashed file, or false on failure
  72. * @access private
  73. */
  74. function saveTempUploadedFile( $saveName, $tempName ) {
  75. global $wgOut;
  76. $stash = 'stash!' . gmdate( "YmdHis" ) . '!' . $saveName;
  77. $mfs = MogileFS::NewMogileFS();
  78. if ( !$mfs->saveFile( $stash, 'normal', $tempName ) ) {
  79. $wgOut->showFileCopyError( $tempName, $stash );
  80. return false;
  81. }
  82. unlink($tempName);
  83. return $stash;
  84. }
  85. /**
  86. * Stash a file in a temporary directory for later processing,
  87. * and save the necessary descriptive info into the session.
  88. * Returns a key value which will be passed through a form
  89. * to pick up the path info on a later invocation.
  90. *
  91. * @return int
  92. * @access private
  93. */
  94. function stashSession() {
  95. $stash = $this->saveTempUploadedFile(
  96. $this->mUploadSaveName, $this->mUploadTempName );
  97. if( !$stash ) {
  98. # Couldn't save the file.
  99. return false;
  100. }
  101. $key = mt_rand( 0, 0x7fffffff );
  102. $_SESSION['wsUploadData'][$key] = array(
  103. 'mUploadTempName' => $stash,
  104. 'mUploadSize' => $this->mUploadSize,
  105. 'mOname' => $this->mOname );
  106. return $key;
  107. }
  108. /**
  109. * Remove a temporarily kept file stashed by saveTempUploadedFile().
  110. * @access private
  111. * @return success
  112. */
  113. function unsaveUploadedFile() {
  114. global $wgOut;
  115. $mfs = MogileFS::NewMogileFS();
  116. if ( ! $mfs->delete( $this->mUploadTempName ) ) {
  117. $wgOut->showFileDeleteError( $this->mUploadTempName );
  118. return false;
  119. } else {
  120. return true;
  121. }
  122. }
  123. }