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