MediaWiki  master
UploadFromStash.php
Go to the documentation of this file.
1 <?php
25 
32 class UploadFromStash extends UploadBase {
33  protected $mFileKey;
34  protected $mVirtualTempPath;
35  protected $mFileProps;
36  protected $mSourceType;
37 
38  // an instance of UploadStash
39  private $stash;
40 
41  // LocalFile repo
42  private $repo;
43 
49  public function __construct( $user = false, $stash = false, $repo = false ) {
50  if ( $repo ) {
51  $this->repo = $repo;
52  } else {
53  $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
54  }
55 
56  if ( $stash ) {
57  $this->stash = $stash;
58  } else {
59  if ( $user ) {
60  wfDebug( __METHOD__ . " creating new UploadStash instance for " . $user->getId() );
61  } else {
62  wfDebug( __METHOD__ . " creating new UploadStash instance with no user" );
63  }
64 
65  $this->stash = new UploadStash( $this->repo, $user );
66  }
67  }
68 
73  public static function isValidKey( $key ) {
74  // this is checked in more detail in UploadStash
75  return (bool)preg_match( UploadStash::KEY_FORMAT_REGEX, $key );
76  }
77 
82  public static function isValidRequest( $request ) {
83  // this passes wpSessionKey to getText() as a default when wpFileKey isn't set.
84  // wpSessionKey has no default which guarantees failure if both are missing
85  // (though that should have been caught earlier)
86  return self::isValidKey( $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) ) );
87  }
88 
94  public function initialize( $key, $name = 'upload_file', $initTempFile = true ) {
101  $metadata = $this->stash->getMetadata( $key );
102  $this->initializePathInfo( $name,
103  $initTempFile ? $this->getRealPath( $metadata['us_path'] ) : false,
104  $metadata['us_size'],
105  false
106  );
107 
108  $this->mFileKey = $key;
109  $this->mVirtualTempPath = $metadata['us_path'];
110  $this->mFileProps = $this->stash->getFileProps( $key );
111  $this->mSourceType = $metadata['us_source_type'];
112  }
113 
117  public function initializeFromRequest( &$request ) {
118  // sends wpSessionKey as a default when wpFileKey is missing
119  $fileKey = $request->getText( 'wpFileKey', $request->getText( 'wpSessionKey' ) );
120 
121  // chooses one of wpDestFile, wpUploadFile, filename in that order.
122  $desiredDestName = $request->getText(
123  'wpDestFile',
124  $request->getText( 'wpUploadFile', $request->getText( 'filename' ) )
125  );
126 
127  $this->initialize( $fileKey, $desiredDestName );
128  }
129 
133  public function getSourceType() {
134  return $this->mSourceType;
135  }
136 
141  public function getTempFileSha1Base36() {
142  return $this->mFileProps['sha1'];
143  }
144 
149  public function unsaveUploadedFile() {
150  return $this->stash->removeFile( $this->mFileKey );
151  }
152 
156  public function postProcessUpload() {
157  parent::postProcessUpload();
158  $this->unsaveUploadedFile();
159  }
160 }
UploadBase
Definition: UploadBase.php:43
UploadBase\getRealPath
getRealPath( $srcPath)
Definition: UploadBase.php:321
UploadFromStash\$stash
$stash
Definition: UploadFromStash.php:39
UploadFromStash\$mSourceType
$mSourceType
Definition: UploadFromStash.php:36
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:149
UploadStash\KEY_FORMAT_REGEX
const KEY_FORMAT_REGEX
Definition: UploadStash.php:55
UploadFromStash\initializeFromRequest
initializeFromRequest(&$request)
Definition: UploadFromStash.php:117
UploadFromStash\getSourceType
getSourceType()
Definition: UploadFromStash.php:133
UploadFromStash\getTempFileSha1Base36
getTempFileSha1Base36()
Get the base 36 SHA1 of the file.
Definition: UploadFromStash.php:141
UploadStash
UploadStash is intended to accomplish a few things:
Definition: UploadStash.php:53
UploadFromStash
Implements uploading from previously stored file.
Definition: UploadFromStash.php:32
UploadFromStash\$repo
$repo
Definition: UploadFromStash.php:42
UploadFromStash\$mVirtualTempPath
$mVirtualTempPath
Definition: UploadFromStash.php:34
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:156
UploadFromStash\unsaveUploadedFile
unsaveUploadedFile()
Remove a temporarily kept file stashed by saveTempUploadedFile().
Definition: UploadFromStash.php:149
UploadFromStash\__construct
__construct( $user=false, $stash=false, $repo=false)
Definition: UploadFromStash.php:49
UploadFromStash\isValidKey
static isValidKey( $key)
Definition: UploadFromStash.php:73
UploadFromStash\isValidRequest
static isValidRequest( $request)
Definition: UploadFromStash.php:82
UploadBase\initializePathInfo
initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile=false)
Definition: UploadBase.php:251
UploadFromStash\$mFileProps
$mFileProps
Definition: UploadFromStash.php:35
UploadFromStash\$mFileKey
$mFileKey
Definition: UploadFromStash.php:33
UploadFromStash\initialize
initialize( $key, $name='upload_file', $initTempFile=true)
Definition: UploadFromStash.php:94