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