<?php
/**
- * Implements Special:UploadStash
+ * Implements Special:UploadStash.
*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup SpecialPage
+ * @ingroup Upload
+ */
+
+/**
* Web access for files temporarily stored by UploadStash.
*
* For example -- files that were uploaded with the UploadWizard extension are stored temporarily
*
* Since this is based on the user's session, in effect this creates a private temporary file area.
* However, the URLs for the files cannot be shared.
- *
- * @file
- * @ingroup SpecialPage
- * @ingroup Upload
*/
-
class SpecialUploadStash extends UnlistedSpecialPage {
// UploadStash
private $stash;
try {
$this->stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash();
} catch ( UploadStashNotAvailableException $e ) {
- return null;
}
}
* @return Boolean: success
*/
public function execute( $subPage ) {
- global $wgUser;
-
- if ( !$this->userCanExecute( $wgUser ) ) {
- $this->displayRestrictionError();
- return;
- }
+ $this->checkPermissions();
- if ( !isset( $subPage ) || $subPage === '' ) {
+ if ( $subPage === null || $subPage === '' ) {
return $this->showUploads();
}
-
return $this->showUpload( $subPage );
}
-
/**
* If file available in stash, cats it out to the client as a simple HTTP response.
* n.b. Most sanity checking done in UploadStashLocalFile, so this is straightforward.
*
* @param $key String: the key of a particular requested file
+ * @return bool
*/
public function showUpload( $key ) {
- global $wgOut;
-
// prevent callers from doing standard HTML output -- we'll take it from here
- $wgOut->disable();
+ $this->getOutput()->disable();
try {
$params = $this->parseKey( $key );
$message = $e->getMessage();
}
- wfHttpError( $code, OutputPage::getStatusMessage( $code ), $message );
- return false;
+ throw new HttpError( $code, $message );
}
/**
return array( 'file' => $file, 'type' => $type );
}
-
-
-
/**
* Get a thumbnail for file, either generated locally or remotely, and stream it out
- * @param String $key: key for the file in the stash
- * @param int $width: width of desired thumbnail
+ *
+ * @param $file
+ * @param $params array
+ *
* @return boolean success
*/
private function outputThumbFromStash( $file, $params ) {
} else {
$this->outputLocallyScaledThumb( $file, $params, $flags );
}
-
-
}
-
/**
* Scale a file (probably with a locally installed imagemagick, or similar) and output it to STDOUT.
* @param $file: File object
}
// we should have just generated it locally
- if ( ! $thumbnailImage->getPath() ) {
+ if ( !$thumbnailImage->getStoragePath() ) {
throw new UploadStashFileNotFoundException( "no local path for scaled item" );
}
// now we should construct a File, so we can get mime and other such info in a standard way
// n.b. mimetype may be different from original (ogx original -> jpeg thumb)
- $thumbFile = new UnregisteredLocalFile( false, $this->stash->repo, $thumbnailImage->getPath(), false );
- if ( ! $thumbFile ) {
+ $thumbFile = new UnregisteredLocalFile( false,
+ $this->stash->repo, $thumbnailImage->getStoragePath(), false );
+ if ( !$thumbFile ) {
throw new UploadStashFileNotFoundException( "couldn't create local file object for thumbnail" );
}
// this global probably looks something like 'http://upload.wikimedia.org/wikipedia/test/thumb/temp'
// do not use trailing slash
global $wgUploadStashScalerBaseUrl;
+ $scalerBaseUrl = $wgUploadStashScalerBaseUrl;
+
+ if( preg_match( '/^\/\//', $scalerBaseUrl ) ) {
+ // this is apparently a protocol-relative URL, which makes no sense in this context,
+ // since this is used for communication that's internal to the application.
+ // default to http.
+ $scalerBaseUrl = wfExpandUrl( $scalerBaseUrl, PROTO_CANONICAL );
+ }
// We need to use generateThumbName() instead of thumbName(), because
// the suffix needs to match the file name for the remote thumbnailer
// to work
$scalerThumbName = $file->generateThumbName( $file->getName(), $params );
- $scalerThumbUrl = $wgUploadStashScalerBaseUrl . '/' . $file->getUrlRel() .
+ $scalerThumbUrl = $scalerBaseUrl . '/' . $file->getUrlRel() .
'/' . rawurlencode( $scalerThumbName );
// make a curl call to the scaler to create a thumbnail
$req = MWHttpRequest::factory( $scalerThumbUrl, $httpOptions );
$status = $req->execute();
if ( ! $status->isOK() ) {
- $errors = $status->getWikiTextArray( $status->getErrorsArray() );
- throw new MWException( "Fetching thumbnail failed: " . print_r( $errors, 1 ) );
+ $errors = $status->getErrorsArray();
+ $errorStr = "Fetching thumbnail failed: " . print_r( $errors, 1 );
+ $errorStr .= "\nurl = $scalerThumbUrl\n";
+ throw new MWException( $errorStr );
}
$contentType = $req->getResponseHeader( "content-type" );
if ( ! $contentType ) {
/**
* Output HTTP response for file
* Side effect: writes HTTP response to STDOUT.
- * XXX could use wfStreamfile (in includes/Streamfile.php), but for consistency with outputContents() doing it this way.
- * XXX is mimeType really enough, or do we need encoding for full Content-Type header?
*
* @param $file File object with a local path (e.g. UnregisteredLocalFile, LocalFile. Oddly these don't share an ancestor!)
+ * @return bool
*/
- private function outputLocalFile( $file ) {
+ private function outputLocalFile( File $file ) {
if ( $file->getSize() > self::MAX_SERVE_BYTES ) {
throw new SpecialUploadStashTooLargeException();
}
- self::outputFileHeaders( $file->getMimeType(), $file->getSize() );
- readfile( $file->getPath() );
- return true;
+ return $file->getRepo()->streamFile( $file->getPath(),
+ array( 'Content-Transfer-Encoding: binary',
+ 'Expires: Sun, 17-Jan-2038 19:14:07 GMT' )
+ );
}
/**
* Side effect: writes HTTP response to STDOUT.
* @param String $content: content
* @param String $mimeType: mime type
+ * @return bool
*/
private function outputContents( $content, $contentType ) {
$size = strlen( $content );
* Default action when we don't have a subpage -- just show links to the uploads we have,
* Also show a button to clear stashed files
* @param Status : $status - the result of processRequest
+ * @return bool
*/
private function showUploads( $status = null ) {
- global $wgOut;
if ( $status === null ) {
$status = Status::newGood();
}
$this->setHeaders();
$this->outputHeader();
-
// create the form, which will also be used to execute a callback to process incoming form data
// this design is extremely dubious, but supposedly HTMLForm is our standard now?
'default' => true,
'name' => 'clear',
)
- ), 'clearStashedUploads' );
+ ), $this->getContext(), 'clearStashedUploads' );
$form->setSubmitCallback( array( __CLASS__ , 'tryClearStashedUploads' ) );
$form->setTitle( $this->getTitle() );
- $form->setSubmitText( wfMsg( 'uploadstash-clear' ) );
+ $form->setSubmitTextMsg( 'uploadstash-clear' );
$form->prepareForm();
$formResult = $form->tryAuthorizedSubmit();
-
// show the files + form, if there are any, or just say there are none
$refreshHtml = Html::element( 'a',
array( 'href' => $this->getTitle()->getLocalURL() ),
- wfMsg( 'uploadstash-refresh' ) );
+ $this->msg( 'uploadstash-refresh' )->text() );
$files = $this->stash->listFiles();
- if ( count( $files ) ) {
+ if ( $files && count( $files ) ) {
sort( $files );
$fileListItemsHtml = '';
foreach ( $files as $file ) {
$this->getTitle( "file/$file" )->getLocalURL() ), $file )
);
}
- $wgOut->addHtml( Html::rawElement( 'ul', array(), $fileListItemsHtml ) );
+ $this->getOutput()->addHtml( Html::rawElement( 'ul', array(), $fileListItemsHtml ) );
$form->displayForm( $formResult );
- $wgOut->addHtml( Html::rawElement( 'p', array(), $refreshHtml ) );
+ $this->getOutput()->addHtml( Html::rawElement( 'p', array(), $refreshHtml ) );
} else {
- $wgOut->addHtml( Html::rawElement( 'p', array(),
- Html::element( 'span', array(), wfMsg( 'uploadstash-nofiles' ) )
+ $this->getOutput()->addHtml( Html::rawElement( 'p', array(),
+ Html::element( 'span', array(), $this->msg( 'uploadstash-nofiles' )->text() )
. ' '
. $refreshHtml
) );