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