properly handle the case where a file disappears during the uploadwizard process
[lhc/web/wiklou.git] / includes / upload / UploadFromStash.php
1 <?php
2 /**
3 * Implements uploading from previously stored file.
4 *
5 * @file
6 * @ingroup upload
7 * @author Bryan Tong Minh
8 */
9
10 class UploadFromStash extends UploadBase {
11 protected $mFileKey, $mVirtualTempPath, $mFileProps, $mSourceType;
12
13 // an instance of UploadStash
14 private $stash;
15
16 //LocalFile repo
17 private $repo;
18
19 public function __construct( $user = false, $stash = false, $repo = false ) {
20 // user object. sometimes this won't exist, as when running from cron.
21 $this->user = $user;
22
23 if( $repo ) {
24 $this->repo = $repo;
25 } else {
26 $this->repo = RepoGroup::singleton()->getLocalRepo();
27 }
28
29 if( $stash ) {
30 $this->stash = $stash;
31 } else {
32 wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() . "\n" );
33 $this->stash = new UploadStash( $this->repo, $this->user );
34 }
35
36 return true;
37 }
38
39 public static function isValidKey( $key ) {
40 // this is checked in more detail in UploadStash
41 return preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
42 }
43
44 /**
45 * @param $request WebRequest
46 *
47 * @return Boolean
48 */
49 public static function isValidRequest( $request ) {
50 return self::isValidKey( $request->getText( 'wpFileKey' ) || $request->getText( 'wpSessionKey' ) );
51 }
52
53 public function initialize( $key, $name = 'upload_file' ) {
54 /**
55 * Confirming a temporarily stashed upload.
56 * We don't want path names to be forged, so we keep
57 * them in the session on the server and just give
58 * an opaque key to the user agent.
59 */
60 $metadata = $this->stash->getMetadata( $key );
61 $this->initializePathInfo( $name,
62 $this->getRealPath ( $metadata['us_path'] ),
63 $metadata['us_size'],
64 false
65 );
66
67 $this->mFileKey = $key;
68 $this->mVirtualTempPath = $metadata['us_path'];
69 $this->mFileProps = $this->stash->getFileProps( $key );
70 $this->mSourceType = $metadata['us_source_type'];
71 }
72
73 /**
74 * @param $request WebRequest
75 */
76 public function initializeFromRequest( &$request ) {
77 $fileKey = $request->getText( 'wpFileKey' ) || $request->getText( 'wpSessionKey' );
78
79 $desiredDestName = $request->getText( 'wpDestFile' );
80 if( !$desiredDestName ) {
81 $desiredDestName = $request->getText( 'wpUploadFile' ) || $request->getText( 'filename' );
82 }
83 return $this->initialize( $fileKey, $desiredDestName );
84 }
85
86 public function getSourceType() {
87 return $this->mSourceType;
88 }
89
90 /**
91 * File has been previously verified so no need to do so again.
92 *
93 * @return bool
94 */
95 protected function verifyFile() {
96 return true;
97 }
98
99 /**
100 * There is no need to stash the image twice
101 */
102 public function stashFile( $key = null ) {
103 if ( !empty( $this->mLocalFile ) ) {
104 return $this->mLocalFile;
105 }
106 return parent::stashFile( $key );
107 }
108
109 /**
110 * Alias for stashFile
111 */
112 public function stashSession( $key = null ) {
113 return $this->stashFile( $key );
114 }
115
116 /**
117 * Remove a temporarily kept file stashed by saveTempUploadedFile().
118 * @return success
119 */
120 public function unsaveUploadedFile() {
121 return $this->stash->removeFile( $this->mFileKey );
122 }
123
124 /**
125 * Perform the upload, then remove the database record afterward.
126 */
127 public function performUpload( $comment, $pageText, $watch, $user ) {
128 $rv = parent::performUpload( $comment, $pageText, $watch, $user );
129 $this->unsaveUploadedFile();
130 return $rv;
131 }
132
133 }