Localisation updates for core and extension messages from translatewiki.net
[mediawiki.git] / includes / upload / UploadFromStash.php
blob8805c50b5051f82555f480bfd9d5ccf1e560b61c
1 <?php
2 /**
3 * Implements uploading from previously stored file.
5 * @ingroup Upload
6 * @author Bryan Tong Minh
7 */
8 class UploadFromStash extends UploadBase {
9 protected $mFileKey, $mVirtualTempPath, $mFileProps, $mSourceType;
11 // an instance of UploadStash
12 private $stash;
14 //LocalFile repo
15 private $repo;
17 /**
18 * @param $user User
19 * @param $stash UploadStash
20 * @param $repo FileRepo
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;
26 if( $repo ) {
27 $this->repo = $repo;
28 } else {
29 $this->repo = RepoGroup::singleton()->getLocalRepo();
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" );
41 $this->stash = new UploadStash( $this->repo, $this->user );
44 return true;
47 /**
48 * @param $key string
49 * @return bool
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 );
56 /**
57 * @param $request WebRequest
59 * @return Boolean
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' ) ) );
68 /**
69 * @param $key string
70 * @param $name string
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.
79 $metadata = $this->stash->getMetadata( $key );
80 $this->initializePathInfo( $name,
81 $this->getRealPath ( $metadata['us_path'] ),
82 $metadata['us_size'],
83 false
86 $this->mFileKey = $key;
87 $this->mVirtualTempPath = $metadata['us_path'];
88 $this->mFileProps = $this->stash->getFileProps( $key );
89 $this->mSourceType = $metadata['us_source_type'];
92 /**
93 * @param $request WebRequest
95 public function initializeFromRequest( &$request ) {
96 // sends wpSessionKey as a default when wpFileKey is missing
97 $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
99 // chooses one of wpDestFile, wpUploadFile, filename in that order.
100 $desiredDestName = $request->getText( 'wpDestFile', $request->getText( 'wpUploadFile', $request->getText( 'filename' ) ) );
102 return $this->initialize( $fileKey, $desiredDestName );
106 * @return string
108 public function getSourceType() {
109 return $this->mSourceType;
113 * File has been previously verified so no need to do so again.
115 * @return bool
117 protected function verifyFile() {
118 return true;
122 * Stash the file.
124 * @return UploadStashFile
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;
134 * This should return the key instead of the UploadStashFile instance, for backward compatibility.
135 * @return String
137 public function stashSession() {
138 return $this->stashFile()->getFileKey();
142 * Remove a temporarily kept file stashed by saveTempUploadedFile().
143 * @return bool success
145 public function unsaveUploadedFile() {
146 return $this->stash->removeFile( $this->mFileKey );
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
157 public function performUpload( $comment, $pageText, $watch, $user ) {
158 $rv = parent::performUpload( $comment, $pageText, $watch, $user );
159 $this->unsaveUploadedFile();
160 return $rv;