* Use ui language now that it is possible
[mediawiki.git] / includes / SpecialUploadMogile.php
blobfb4ae5325b6b7c864a38d98b6b0f39979f4f886b
1 <?php
2 /**
4 * @addtogroup SpecialPage
5 */
7 /**
9 */
10 require_once( 'MogileFS.php' );
12 /**
13 * Entry point
15 function wfSpecialUploadMogile() {
16 global $wgRequest;
17 $form = new UploadFormMogile( $wgRequest );
18 $form->execute();
21 class UploadFormMogile extends UploadForm {
22 /**
23 * Move the uploaded file from its temporary location to the final
24 * destination. If a previous version of the file exists, move
25 * it into the archive subdirectory.
27 * @todo If the later save fails, we may have disappeared the original file.
29 * @param string $saveName
30 * @param string $tempName full path to the temporary file
31 * @param bool $useRename Not used in this implementation
33 function saveUploadedFile( $saveName, $tempName, $useRename = false ) {
34 global $wgOut;
35 $mfs = MogileFS::NewMogileFS();
37 $this->mSavedFile = "image!{$saveName}";
39 if( $mfs->getPaths( $this->mSavedFile )) {
40 $this->mUploadOldVersion = gmdate( 'YmdHis' ) . "!{$saveName}";
41 if( !$mfs->rename( $this->mSavedFile, "archive!{$this->mUploadOldVersion}" ) ) {
42 $wgOut->showFileRenameError( $this->mSavedFile,
43 "archive!{$this->mUploadOldVersion}" );
44 return false;
46 } else {
47 $this->mUploadOldVersion = '';
50 if ( $this->mStashed ) {
51 if (!$mfs->rename($tempName,$this->mSavedFile)) {
52 $wgOut->showFileRenameError($tempName, $this->mSavedFile );
53 return false;
55 } else {
56 if ( !$mfs->saveFile($this->mSavedFile,'normal',$tempName )) {
57 $wgOut->showFileCopyError( $tempName, $this->mSavedFile );
58 return false;
60 unlink($tempName);
62 return true;
65 /**
66 * Stash a file in a temporary directory for later processing
67 * after the user has confirmed it.
69 * If the user doesn't explicitly cancel or accept, these files
70 * can accumulate in the temp directory.
72 * @param string $saveName - the destination filename
73 * @param string $tempName - the source temporary file to save
74 * @return string - full path the stashed file, or false on failure
75 * @access private
77 function saveTempUploadedFile( $saveName, $tempName ) {
78 global $wgOut;
80 $stash = 'stash!' . gmdate( "YmdHis" ) . '!' . $saveName;
81 $mfs = MogileFS::NewMogileFS();
82 if ( !$mfs->saveFile( $stash, 'normal', $tempName ) ) {
83 $wgOut->showFileCopyError( $tempName, $stash );
84 return false;
86 unlink($tempName);
87 return $stash;
90 /**
91 * Stash a file in a temporary directory for later processing,
92 * and save the necessary descriptive info into the session.
93 * Returns a key value which will be passed through a form
94 * to pick up the path info on a later invocation.
96 * @return int
97 * @access private
99 function stashSession() {
100 $stash = $this->saveTempUploadedFile(
101 $this->mUploadSaveName, $this->mUploadTempName );
103 if( !$stash ) {
104 # Couldn't save the file.
105 return false;
108 $key = mt_rand( 0, 0x7fffffff );
109 $_SESSION['wsUploadData'][$key] = array(
110 'mUploadTempName' => $stash,
111 'mUploadSize' => $this->mUploadSize,
112 'mOname' => $this->mOname );
113 return $key;
117 * Remove a temporarily kept file stashed by saveTempUploadedFile().
118 * @access private
119 * @return success
121 function unsaveUploadedFile() {
122 global $wgOut;
123 $mfs = MogileFS::NewMogileFS();
124 if ( ! $mfs->delete( $this->mUploadTempName ) ) {
125 $wgOut->showFileDeleteError( $this->mUploadTempName );
126 return false;
127 } else {
128 return true;