API * Better log events info * Added RAW debugging format
[mediawiki.git] / includes / SpecialUploadMogile.php
blob05bfca085a92eb7a1676e45eccff1a8bc9913356
1 <?php
2 /**
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
8 /**
11 require_once( 'MogileFS.php' );
13 /**
14 * Entry point
16 function wfSpecialUploadMogile() {
17 global $wgRequest;
18 $form = new UploadFormMogile( $wgRequest );
19 $form->execute();
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.
29 * @todo If the later save fails, we may have disappeared the original file.
31 * @param string $saveName
32 * @param string $tempName full path to the temporary file
33 * @param bool $useRename Not used in this implementation
35 function saveUploadedFile( $saveName, $tempName, $useRename = false ) {
36 global $wgOut;
37 $mfs = MogileFS::NewMogileFS();
39 $this->mSavedFile = "image!{$saveName}";
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;
48 } else {
49 $this->mUploadOldVersion = '';
52 if ( $this->mStashed ) {
53 if (!$mfs->rename($tempName,$this->mSavedFile)) {
54 $wgOut->showFileRenameError($tempName, $this->mSavedFile );
55 return false;
57 } else {
58 if ( !$mfs->saveFile($this->mSavedFile,'normal',$tempName )) {
59 $wgOut->showFileCopyError( $tempName, $this->mSavedFile );
60 return false;
62 unlink($tempName);
64 return true;
67 /**
68 * Stash a file in a temporary directory for later processing
69 * after the user has confirmed it.
71 * If the user doesn't explicitly cancel or accept, these files
72 * can accumulate in the temp directory.
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
79 function saveTempUploadedFile( $saveName, $tempName ) {
80 global $wgOut;
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;
88 unlink($tempName);
89 return $stash;
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.
98 * @return int
99 * @access private
101 function stashSession() {
102 $stash = $this->saveTempUploadedFile(
103 $this->mUploadSaveName, $this->mUploadTempName );
105 if( !$stash ) {
106 # Couldn't save the file.
107 return false;
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;
119 * Remove a temporarily kept file stashed by saveTempUploadedFile().
120 * @access private
121 * @return success
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;