MediaWiki  1.34.4
UploadFromStash.php
Go to the documentation of this file.
1 <?php
30 class UploadFromStash extends UploadBase {
31  protected $mFileKey;
32  protected $mVirtualTempPath;
33  protected $mFileProps;
34  protected $mSourceType;
35 
36  // an instance of UploadStash
37  private $stash;
38 
39  // LocalFile repo
40  private $repo;
41 
47  public function __construct( $user = false, $stash = false, $repo = false ) {
48  if ( $repo ) {
49  $this->repo = $repo;
50  } else {
51  $this->repo = RepoGroup::singleton()->getLocalRepo();
52  }
53 
54  if ( $stash ) {
55  $this->stash = $stash;
56  } else {
57  if ( $user ) {
58  wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() . "\n" );
59  } else {
60  wfDebug( __METHOD__ . " creating new UploadStash instance with no user\n" );
61  }
62 
63  $this->stash = new UploadStash( $this->repo, $user );
64  }
65  }
66 
71  public static function isValidKey( $key ) {
72  // this is checked in more detail in UploadStash
73  return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
74  }
75 
80  public static function isValidRequest( $request ) {
81  // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
82  // wpSessionKey has no default which guarantees failure if both are missing
83  // (though that should have been caught earlier)
84  return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
85  }
86 
92  public function initialize( $key, $name = 'upload_file', $initTempFile = true ) {
99  $metadata = $this->stash->getMetadata( $key );
100  $this->initializePathInfo( $name,
101  $initTempFile ? $this->getRealPath( $metadata['us_path'] ) : false,
102  $metadata['us_size'],
103  false
104  );
105 
106  $this->mFileKey = $key;
107  $this->mVirtualTempPath = $metadata['us_path'];
108  $this->mFileProps = $this->stash->getFileProps( $key );
109  $this->mSourceType = $metadata['us_source_type'];
110  }
111 
115  public function initializeFromRequest( &$request ) {
116  // sends wpSessionKey as a default when wpFileKey is missing
117  $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
118 
119  // chooses one of wpDestFile, wpUploadFile, filename in that order.
120  $desiredDestName = $request->getText(
121  'wpDestFile',
122  $request->getText( 'wpUploadFile', $request->getText( 'filename' ) )
123  );
124 
125  $this->initialize( $fileKey, $desiredDestName );
126  }
127 
131  public function getSourceType() {
132  return $this->mSourceType;
133  }
134 
139  public function getTempFileSha1Base36() {
140  return $this->mFileProps['sha1'];
141  }
142 
147  public function unsaveUploadedFile() {
148  return $this->stash->removeFile( $this->mFileKey );
149  }
150 
154  public function postProcessUpload() {
155  parent::postProcessUpload();
156  $this->unsaveUploadedFile();
157  }
158 }
UploadBase
Definition: UploadBase.php:42
UploadBase\getRealPath
getRealPath( $srcPath)
Definition: UploadBase.php:324
RepoGroup\singleton
static singleton()
Definition: RepoGroup.php:60
UploadFromStash\$stash
$stash
Definition: UploadFromStash.php:37
UploadFromStash\$mSourceType
$mSourceType
Definition: UploadFromStash.php:34
UploadStash\KEY_FORMAT_REGEX
const KEY_FORMAT_REGEX
Definition: UploadStash.php:55
UploadFromStash\initializeFromRequest
initializeFromRequest(&$request)
Definition: UploadFromStash.php:115
UploadFromStash\getSourceType
getSourceType()
Definition: UploadFromStash.php:131
UploadFromStash\getTempFileSha1Base36
getTempFileSha1Base36()
Get the base 36 SHA1 of the file.
Definition: UploadFromStash.php:139
UploadStash
UploadStash is intended to accomplish a few things:
Definition: UploadStash.php:53
UploadFromStash
Implements uploading from previously stored file.
Definition: UploadFromStash.php:30
UploadFromStash\$repo
$repo
Definition: UploadFromStash.php:40
UploadFromStash\$mVirtualTempPath
$mVirtualTempPath
Definition: UploadFromStash.php:32
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:913
UploadFromStash\postProcessUpload
postProcessUpload()
Remove the database record after a successful upload.
Definition: UploadFromStash.php:154
UploadFromStash\unsaveUploadedFile
unsaveUploadedFile()
Remove a temporarily kept file stashed by saveTempUploadedFile().
Definition: UploadFromStash.php:147
UploadFromStash\__construct
__construct( $user=false, $stash=false, $repo=false)
Definition: UploadFromStash.php:47
UploadFromStash\isValidKey
static isValidKey( $key)
Definition: UploadFromStash.php:71
UploadFromStash\isValidRequest
static isValidRequest( $request)
Definition: UploadFromStash.php:80
UploadBase\initializePathInfo
initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile=false)
Initialize the path information.
Definition: UploadBase.php:254
UploadFromStash\$mFileProps
$mFileProps
Definition: UploadFromStash.php:33
UploadFromStash\$mFileKey
$mFileKey
Definition: UploadFromStash.php:31
UploadFromStash\initialize
initialize( $key, $name='upload_file', $initTempFile=true)
Definition: UploadFromStash.php:92