Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[lhc/web/wiklou.git] / includes / SpecialUploadMogile.php
1 <?php
2 /**
3 *
4 * @addtogroup SpecialPage
5 */
6
7 /**
8 *
9 */
10 require_once( 'MogileFS.php' );
11
12 /**
13 * Entry point
14 */
15 function wfSpecialUploadMogile() {
16 global $wgRequest;
17 $form = new UploadFormMogile( $wgRequest );
18 $form->execute();
19 }
20
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.
26 *
27 * @todo If the later save fails, we may have disappeared the original file.
28 *
29 * @param string $saveName
30 * @param string $tempName full path to the temporary file
31 * @param bool $useRename Not used in this implementation
32 */
33 function saveUploadedFile( $saveName, $tempName, $useRename = false ) {
34 global $wgOut;
35 $mfs = MogileFS::NewMogileFS();
36
37 $this->mSavedFile = "image!{$saveName}";
38
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;
45 }
46 } else {
47 $this->mUploadOldVersion = '';
48 }
49
50 if ( $this->mStashed ) {
51 if (!$mfs->rename($tempName,$this->mSavedFile)) {
52 $wgOut->showFileRenameError($tempName, $this->mSavedFile );
53 return false;
54 }
55 } else {
56 if ( !$mfs->saveFile($this->mSavedFile,'normal',$tempName )) {
57 $wgOut->showFileCopyError( $tempName, $this->mSavedFile );
58 return false;
59 }
60 unlink($tempName);
61 }
62 return true;
63 }
64
65 /**
66 * Stash a file in a temporary directory for later processing
67 * after the user has confirmed it.
68 *
69 * If the user doesn't explicitly cancel or accept, these files
70 * can accumulate in the temp directory.
71 *
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
76 */
77 function saveTempUploadedFile( $saveName, $tempName ) {
78 global $wgOut;
79
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;
85 }
86 unlink($tempName);
87 return $stash;
88 }
89
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.
95 *
96 * @return int
97 * @access private
98 */
99 function stashSession() {
100 $stash = $this->saveTempUploadedFile(
101 $this->mUploadSaveName, $this->mUploadTempName );
102
103 if( !$stash ) {
104 # Couldn't save the file.
105 return false;
106 }
107
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;
114 }
115
116 /**
117 * Remove a temporarily kept file stashed by saveTempUploadedFile().
118 * @access private
119 * @return success
120 */
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;
129 }
130 }
131 }
132 ?>