MediaWiki REL1_35
UploadFromStash.php
Go to the documentation of this file.
1<?php
25
33 protected $mFileKey;
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}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
MediaWikiServices is the service locator for the application scope of MediaWiki.
UploadBase and subclasses are the backend of MediaWiki's file uploads.
getRealPath( $srcPath)
initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile=false)
Implements uploading from previously stored file.
getTempFileSha1Base36()
Get the base 36 SHA1 of the file.
static isValidKey( $key)
static isValidRequest( $request)
initializeFromRequest(&$request)
unsaveUploadedFile()
Remove a temporarily kept file stashed by saveTempUploadedFile().
postProcessUpload()
Remove the database record after a successful upload.
__construct( $user=false, $stash=false, $repo=false)
initialize( $key, $name='upload_file', $initTempFile=true)
UploadStash is intended to accomplish a few things:
const KEY_FORMAT_REGEX