summaryrefslogtreecommitdiff
path: root/includes/SpecialUploadMogile.php
blob: 27af62e763dc4be4fe898014286f0ce176c9a579 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
 *
 * @addtogroup SpecialPage
 */

/**
 * You will need the extension MogileClient to use this special page.
 */
require_once( 'MogileFS.php' );

/**
 * Entry point
 */
function wfSpecialUploadMogile() {
	global $wgRequest;
	$form = new UploadFormMogile( $wgRequest );
	$form->execute();
}

/**
 * Extends Special:Upload with MogileFS.
 * @addtogroup SpecialPage
 */
class UploadFormMogile extends UploadForm {
	/**
	 * Move the uploaded file from its temporary location to the final
	 * destination. If a previous version of the file exists, move
	 * it into the archive subdirectory.
	 *
	 * @todo If the later save fails, we may have disappeared the original file.
	 *
	 * @param string $saveName
	 * @param string $tempName full path to the temporary file
	 * @param bool $useRename  Not used in this implementation
	 */
	function saveUploadedFile( $saveName, $tempName, $useRename = false ) {
		global $wgOut;
		$mfs = MogileFS::NewMogileFS();

		$this->mSavedFile = "image!{$saveName}";

		if( $mfs->getPaths( $this->mSavedFile )) {
			$this->mUploadOldVersion = gmdate( 'YmdHis' ) . "!{$saveName}";
			if( !$mfs->rename( $this->mSavedFile, "archive!{$this->mUploadOldVersion}" ) ) {
				$wgOut->showFileRenameError( $this->mSavedFile,
				  "archive!{$this->mUploadOldVersion}" );
				return false;
			}
		} else {
			$this->mUploadOldVersion = '';
		}

		if ( $this->mStashed ) {
			if (!$mfs->rename($tempName,$this->mSavedFile)) {
				$wgOut->showFileRenameError($tempName, $this->mSavedFile );
				return false;
			}
		} else {
			if ( !$mfs->saveFile($this->mSavedFile,'normal',$tempName )) {
				$wgOut->showFileCopyError( $tempName, $this->mSavedFile );
				return false;
			}
			unlink($tempName);
		}
		return true;
	}

	/**
	 * Stash a file in a temporary directory for later processing
	 * after the user has confirmed it.
	 *
	 * If the user doesn't explicitly cancel or accept, these files
	 * can accumulate in the temp directory.
	 *
	 * @param string $saveName - the destination filename
	 * @param string $tempName - the source temporary file to save
	 * @return string - full path the stashed file, or false on failure
	 * @access private
	 */
	function saveTempUploadedFile( $saveName, $tempName ) {
		global $wgOut;

		$stash = 'stash!' . gmdate( "YmdHis" ) . '!' . $saveName;
		$mfs = MogileFS::NewMogileFS();
		if ( !$mfs->saveFile( $stash, 'normal', $tempName ) ) {
			$wgOut->showFileCopyError( $tempName, $stash );
			return false;
		}
		unlink($tempName);
		return $stash;
	}

	/**
	 * Stash a file in a temporary directory for later processing,
	 * and save the necessary descriptive info into the session.
	 * Returns a key value which will be passed through a form
	 * to pick up the path info on a later invocation.
	 *
	 * @return int
	 * @access private
	 */
	function stashSession() {
		$stash = $this->saveTempUploadedFile(
			$this->mUploadSaveName, $this->mUploadTempName );

		if( !$stash ) {
			# Couldn't save the file.
			return false;
		}

		$key = mt_rand( 0, 0x7fffffff );
		$_SESSION['wsUploadData'][$key] = array(
			'mUploadTempName' => $stash,
			'mUploadSize'     => $this->mUploadSize,
			'mOname'          => $this->mOname );
		return $key;
	}

	/**
	 * Remove a temporarily kept file stashed by saveTempUploadedFile().
	 * @access private
	 * @return success
	 */
	function unsaveUploadedFile() {
		global $wgOut;
		$mfs = MogileFS::NewMogileFS();
		if ( ! $mfs->delete( $this->mUploadTempName ) ) {
			$wgOut->showFileDeleteError( $this->mUploadTempName );
			return false;
		} else {
			return true;
		}
	}
}
?>