FU r106752: use "media-" instead of "images-" in container names. Long live books...
[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 /**
20 * @param $user User
21 * @param $stash UploadStash
22 * @param $repo FileRepo
23 */
24 public function __construct( $user = false, $stash = false, $repo = false ) {
25 // user object. sometimes this won't exist, as when running from cron.
26 $this->user = $user;
27
28 if( $repo ) {
29 $this->repo = $repo;
30 } else {
31 $this->repo = RepoGroup::singleton()->getLocalRepo();
32 }
33
34 if( $stash ) {
35 $this->stash = $stash;
36 } else {
37 if( $user ) {
38 wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() . "\n" );
39 } else {
40 wfDebug( __METHOD__ . " creating new UploadStash instance with no user\n" );
41 }
42
43 $this->stash = new UploadStash( $this->repo, $this->user );
44 }
45
46 return true;
47 }
48
49 /**
50 * @param $key string
51 * @return bool
52 */
53 public static function isValidKey( $key ) {
54 // this is checked in more detail in UploadStash
55 return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
56 }
57
58 /**
59 * @param $request WebRequest
60 *
61 * @return Boolean
62 */
63 public static function isValidRequest( $request ) {
64 // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
65 // wpSessionKey has no default which guarantees failure if both are missing
66 // (though that should have been caught earlier)
67 return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
68 }
69
70 /**
71 * @param $key string
72 * @param $name string
73 */
74 public function initialize( $key, $name = 'upload_file' ) {
75 /**
76 * Confirming a temporarily stashed upload.
77 * We don't want path names to be forged, so we keep
78 * them in the session on the server and just give
79 * an opaque key to the user agent.
80 */
81 $metadata = $this->stash->getMetadata( $key );
82 $this->initializePathInfo( $name,
83 $this->getRealPath ( $metadata['us_path'] ),
84 $metadata['us_size'],
85 false
86 );
87
88 $this->mFileKey = $key;
89 $this->mVirtualTempPath = $metadata['us_path'];
90 $this->mFileProps = $this->stash->getFileProps( $key );
91 $this->mSourceType = $metadata['us_source_type'];
92 }
93
94 /**
95 * @param $request WebRequest
96 */
97 public function initializeFromRequest( &$request ) {
98 // sends wpSessionKey as a default when wpFileKey is missing
99 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
100
101 // chooses one of wpDestFile, wpUploadFile, filename in that order.
102 $desiredDestName = $request->getText( 'wpDestFile', $request->getText( 'wpUploadFile', $request->getText( 'filename' ) ) );
103
104 return $this->initialize( $fileKey, $desiredDestName );
105 }
106
107 /**
108 * @return string
109 */
110 public function getSourceType() {
111 return $this->mSourceType;
112 }
113
114 /**
115 * File has been previously verified so no need to do so again.
116 *
117 * @return bool
118 */
119 protected function verifyFile() {
120 return true;
121 }
122
123 /**
124 * Stash the file.
125 *
126 * @return UploadStashFile
127 */
128 public function stashFile() {
129 // replace mLocalFile with an instance of UploadStashFile, which adds some methods
130 // that are useful for stashed files.
131 $this->mLocalFile = parent::stashFile();
132 return $this->mLocalFile;
133 }
134
135 /**
136 * This should return the key instead of the UploadStashFile instance, for backward compatibility.
137 * @return String
138 */
139 public function stashSession() {
140 return $this->stashFile()->getFileKey();
141 }
142
143 /**
144 * Remove a temporarily kept file stashed by saveTempUploadedFile().
145 * @return success
146 */
147 public function unsaveUploadedFile() {
148 return $this->stash->removeFile( $this->mFileKey );
149 }
150
151 /**
152 * Perform the upload, then remove the database record afterward.
153 * @param $comment string
154 * @param $pageText string
155 * @param $watch bool
156 * @param $user User
157 * @return Status
158 */
159 public function performUpload( $comment, $pageText, $watch, $user ) {
160 $rv = parent::performUpload( $comment, $pageText, $watch, $user );
161 $this->unsaveUploadedFile();
162 return $rv;
163 }
164 }