Manual:UploadStash

From MediaWiki.org
Jump to navigation Jump to search

The UploadStash is intended to accomplish a few things:

  • Enable applications to temporarily stash files without publishing them to the wiki.
Several parts of MediaWiki do this in similar ways: UploadBase, UploadWizard, and FirefoggChunkedExtension. And there are several that reimplement stashing from scratch, in idiosyncratic ways. The idea is to unify them all here. Mostly all of them are the same except for storing some custom fields, which we subsume into the data array.
  • Enable applications to find said files later, as long as the db table or temp files haven't been purged.
  • Enable the uploading user (and only the uploading user) to access said files, and thumbnails of said files, via a URL.
    • We accomplish this using a database table, with ownership checking as you might expect. See SpecialUploadStash, which implements a web interface to some files stored this way.

The UploadStash right now is mostly intended to show you one user's slice of the entire stash. The user parameter is only optional because there are few cases where we clean out the stash from an automated script. In the future we might refactor this.

  • UploadStash represents the entire stash of temporary files.
  • UploadStashFile is a filestore for the actual physical disk files.
  • UploadFromStash extends UploadBase, and represents a single stashed file as it is moved from the stash to the regular file repository

Draft code[edit]

Below are drafts of code that allow you to save and retrieve files from the UploadStash.

/**
 * @param {string} $form_file_field_name
 * @param {User} $User
 * @throws {MWException}
 * @return {null|string} null or a stash upload file key
 */
function saveMetadataFileAsStash( $form_file_field_name = null, User $User ) {
	$result = null;

	if ( !empty( $form_file_field_name ) && !empty( $_FILES[$form_file_field_name]['name'] ) ) {
		/**
		 * this method should make sure
		 * - $Status = self::isFileEmpty();
		 * - $Status = self::noFileErrors();
		 * - $Status = self::fileWasUploaded();
		 * - $Status = self::isAcceptedFileExtension();
		 * - $Status = self::isAcceptedMimeType();
		 * - $Status = self::mimeTypeAndExtensionMatch();
		 * - any other file checks necessary
		 */
		$Status = FileChecks::isUploadedFileValid( $_FILES[$form_file_field_name]['name'] );

		if ( !$Status->ok ) {
			throw new MWException( $Status->getMessage() );
		}

		global $wgLocalFileRepo;
		$UploadStash = new UploadStash( new LocalRepo( $wgLocalFileRepo), $User );

		$result = $UploadStash
			->stashFile(
				$_FILES[$form_file_field_name]['tmp_name'],
				null
			)
			->getFileKey();
	}

	return $result;
}

/**
 * @param {string} $stash_key
 * @param {User} $User
 * @return {null|UploadStashFile}
 */
function getMetadataFromStash( $stash_key = null, User $User ) {
	$result = null;

	if ( !empty( $stash_key ) ) {
		global $wgLocalFileRepo;
		$UploadStash = new UploadStash( new LocalRepo( $wgLocalFileRepo ), $User );
		$result = $UploadStash->getFile( $stash_key );
	}

	return $result;
}

See also[edit]