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