MediaWiki
REL1_20
|
00001 <?php 00035 class SpecialUploadStash extends UnlistedSpecialPage { 00036 // UploadStash 00037 private $stash; 00038 00039 // Since we are directly writing the file to STDOUT, 00040 // we should not be reading in really big files and serving them out. 00041 // 00042 // We also don't want people using this as a file drop, even if they 00043 // share credentials. 00044 // 00045 // This service is really for thumbnails and other such previews while 00046 // uploading. 00047 const MAX_SERVE_BYTES = 1048576; // 1MB 00048 00049 public function __construct() { 00050 parent::__construct( 'UploadStash', 'upload' ); 00051 try { 00052 $this->stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash(); 00053 } catch ( UploadStashNotAvailableException $e ) { 00054 } 00055 } 00056 00063 public function execute( $subPage ) { 00064 $this->checkPermissions(); 00065 00066 if ( $subPage === null || $subPage === '' ) { 00067 return $this->showUploads(); 00068 } 00069 return $this->showUpload( $subPage ); 00070 } 00071 00079 public function showUpload( $key ) { 00080 // prevent callers from doing standard HTML output -- we'll take it from here 00081 $this->getOutput()->disable(); 00082 00083 try { 00084 $params = $this->parseKey( $key ); 00085 if ( $params['type'] === 'thumb' ) { 00086 return $this->outputThumbFromStash( $params['file'], $params['params'] ); 00087 } else { 00088 return $this->outputLocalFile( $params['file'] ); 00089 } 00090 } catch( UploadStashFileNotFoundException $e ) { 00091 $code = 404; 00092 $message = $e->getMessage(); 00093 } catch( UploadStashZeroLengthFileException $e ) { 00094 $code = 500; 00095 $message = $e->getMessage(); 00096 } catch( UploadStashBadPathException $e ) { 00097 $code = 500; 00098 $message = $e->getMessage(); 00099 } catch( SpecialUploadStashTooLargeException $e ) { 00100 $code = 500; 00101 $message = 'Cannot serve a file larger than ' . self::MAX_SERVE_BYTES . ' bytes. ' . $e->getMessage(); 00102 } catch( Exception $e ) { 00103 $code = 500; 00104 $message = $e->getMessage(); 00105 } 00106 00107 throw new HttpError( $code, $message ); 00108 } 00109 00118 private function parseKey( $key ) { 00119 $type = strtok( $key, '/' ); 00120 00121 if ( $type !== 'file' && $type !== 'thumb' ) { 00122 throw new UploadStashBadPathException( "Unknown type '$type'" ); 00123 } 00124 $fileName = strtok( '/' ); 00125 $thumbPart = strtok( '/' ); 00126 $file = $this->stash->getFile( $fileName ); 00127 if ( $type === 'thumb' ) { 00128 $srcNamePos = strrpos( $thumbPart, $fileName ); 00129 if ( $srcNamePos === false || $srcNamePos < 1 ) { 00130 throw new UploadStashBadPathException( 'Unrecognized thumb name' ); 00131 } 00132 $paramString = substr( $thumbPart, 0, $srcNamePos - 1 ); 00133 00134 $handler = $file->getHandler(); 00135 $params = $handler->parseParamString( $paramString ); 00136 return array( 'file' => $file, 'type' => $type, 'params' => $params ); 00137 } 00138 00139 return array( 'file' => $file, 'type' => $type ); 00140 } 00141 00150 private function outputThumbFromStash( $file, $params ) { 00151 00152 // this global, if it exists, points to a "scaler", as you might find in the Wikimedia Foundation cluster. See outputRemoteScaledThumb() 00153 // this is part of our horrible NFS-based system, we create a file on a mount point here, but fetch the scaled file from somewhere else that 00154 // happens to share it over NFS 00155 global $wgUploadStashScalerBaseUrl; 00156 00157 $flags = 0; 00158 if ( $wgUploadStashScalerBaseUrl ) { 00159 $this->outputRemoteScaledThumb( $file, $params, $flags ); 00160 } else { 00161 $this->outputLocallyScaledThumb( $file, $params, $flags ); 00162 } 00163 } 00164 00173 private function outputLocallyScaledThumb( $file, $params, $flags ) { 00174 00175 // n.b. this is stupid, we insist on re-transforming the file every time we are invoked. We rely 00176 // on HTTP caching to ensure this doesn't happen. 00177 00178 $flags |= File::RENDER_NOW; 00179 00180 $thumbnailImage = $file->transform( $params, $flags ); 00181 if ( !$thumbnailImage ) { 00182 throw new MWException( 'Could not obtain thumbnail' ); 00183 } 00184 00185 // we should have just generated it locally 00186 if ( !$thumbnailImage->getStoragePath() ) { 00187 throw new UploadStashFileNotFoundException( "no local path for scaled item" ); 00188 } 00189 00190 // now we should construct a File, so we can get mime and other such info in a standard way 00191 // n.b. mimetype may be different from original (ogx original -> jpeg thumb) 00192 $thumbFile = new UnregisteredLocalFile( false, 00193 $this->stash->repo, $thumbnailImage->getStoragePath(), false ); 00194 if ( !$thumbFile ) { 00195 throw new UploadStashFileNotFoundException( "couldn't create local file object for thumbnail" ); 00196 } 00197 00198 return $this->outputLocalFile( $thumbFile ); 00199 00200 } 00201 00215 private function outputRemoteScaledThumb( $file, $params, $flags ) { 00216 00217 // this global probably looks something like 'http://upload.wikimedia.org/wikipedia/test/thumb/temp' 00218 // do not use trailing slash 00219 global $wgUploadStashScalerBaseUrl; 00220 $scalerBaseUrl = $wgUploadStashScalerBaseUrl; 00221 00222 if( preg_match( '/^\/\//', $scalerBaseUrl ) ) { 00223 // this is apparently a protocol-relative URL, which makes no sense in this context, 00224 // since this is used for communication that's internal to the application. 00225 // default to http. 00226 $scalerBaseUrl = wfExpandUrl( $scalerBaseUrl, PROTO_CANONICAL ); 00227 } 00228 00229 // We need to use generateThumbName() instead of thumbName(), because 00230 // the suffix needs to match the file name for the remote thumbnailer 00231 // to work 00232 $scalerThumbName = $file->generateThumbName( $file->getName(), $params ); 00233 $scalerThumbUrl = $scalerBaseUrl . '/' . $file->getUrlRel() . 00234 '/' . rawurlencode( $scalerThumbName ); 00235 00236 // make a curl call to the scaler to create a thumbnail 00237 $httpOptions = array( 00238 'method' => 'GET', 00239 'timeout' => 'default' 00240 ); 00241 $req = MWHttpRequest::factory( $scalerThumbUrl, $httpOptions ); 00242 $status = $req->execute(); 00243 if ( ! $status->isOK() ) { 00244 $errors = $status->getErrorsArray(); 00245 $errorStr = "Fetching thumbnail failed: " . print_r( $errors, 1 ); 00246 $errorStr .= "\nurl = $scalerThumbUrl\n"; 00247 throw new MWException( $errorStr ); 00248 } 00249 $contentType = $req->getResponseHeader( "content-type" ); 00250 if ( ! $contentType ) { 00251 throw new MWException( "Missing content-type header" ); 00252 } 00253 return $this->outputContents( $req->getContent(), $contentType ); 00254 } 00255 00263 private function outputLocalFile( File $file ) { 00264 if ( $file->getSize() > self::MAX_SERVE_BYTES ) { 00265 throw new SpecialUploadStashTooLargeException(); 00266 } 00267 return $file->getRepo()->streamFile( $file->getPath(), 00268 array( 'Content-Transfer-Encoding: binary', 00269 'Expires: Sun, 17-Jan-2038 19:14:07 GMT' ) 00270 ); 00271 } 00272 00280 private function outputContents( $content, $contentType ) { 00281 $size = strlen( $content ); 00282 if ( $size > self::MAX_SERVE_BYTES ) { 00283 throw new SpecialUploadStashTooLargeException(); 00284 } 00285 self::outputFileHeaders( $contentType, $size ); 00286 print $content; 00287 return true; 00288 } 00289 00297 private static function outputFileHeaders( $contentType, $size ) { 00298 header( "Content-Type: $contentType", true ); 00299 header( 'Content-Transfer-Encoding: binary', true ); 00300 header( 'Expires: Sun, 17-Jan-2038 19:14:07 GMT', true ); 00301 // Bug 53032 - It shouldn't be a problem here, but let's be safe and not cache 00302 header( 'Cache-Control: private' ); 00303 header( "Content-Length: $size", true ); 00304 } 00305 00313 public static function tryClearStashedUploads( $formData ) { 00314 if ( isset( $formData['Clear'] ) ) { 00315 $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash(); 00316 wfDebug( "stash has: " . print_r( $stash->listFiles(), true ) ); 00317 if ( ! $stash->clear() ) { 00318 return Status::newFatal( 'uploadstash-errclear' ); 00319 } 00320 } 00321 return Status::newGood(); 00322 } 00323 00330 private function showUploads( $status = null ) { 00331 if ( $status === null ) { 00332 $status = Status::newGood(); 00333 } 00334 00335 // sets the title, etc. 00336 $this->setHeaders(); 00337 $this->outputHeader(); 00338 00339 // create the form, which will also be used to execute a callback to process incoming form data 00340 // this design is extremely dubious, but supposedly HTMLForm is our standard now? 00341 00342 $form = new HTMLForm( array( 00343 'Clear' => array( 00344 'type' => 'hidden', 00345 'default' => true, 00346 'name' => 'clear', 00347 ) 00348 ), $this->getContext(), 'clearStashedUploads' ); 00349 $form->setSubmitCallback( array( __CLASS__ , 'tryClearStashedUploads' ) ); 00350 $form->setTitle( $this->getTitle() ); 00351 $form->setSubmitTextMsg( 'uploadstash-clear' ); 00352 00353 $form->prepareForm(); 00354 $formResult = $form->tryAuthorizedSubmit(); 00355 00356 // show the files + form, if there are any, or just say there are none 00357 $refreshHtml = Html::element( 'a', 00358 array( 'href' => $this->getTitle()->getLocalURL() ), 00359 $this->msg( 'uploadstash-refresh' )->text() ); 00360 $files = $this->stash->listFiles(); 00361 if ( $files && count( $files ) ) { 00362 sort( $files ); 00363 $fileListItemsHtml = ''; 00364 foreach ( $files as $file ) { 00365 // TODO: Use Linker::link or even construct the list in plain wikitext 00366 $fileListItemsHtml .= Html::rawElement( 'li', array(), 00367 Html::element( 'a', array( 'href' => 00368 $this->getTitle( "file/$file" )->getLocalURL() ), $file ) 00369 ); 00370 } 00371 $this->getOutput()->addHtml( Html::rawElement( 'ul', array(), $fileListItemsHtml ) ); 00372 $form->displayForm( $formResult ); 00373 $this->getOutput()->addHtml( Html::rawElement( 'p', array(), $refreshHtml ) ); 00374 } else { 00375 $this->getOutput()->addHtml( Html::rawElement( 'p', array(), 00376 Html::element( 'span', array(), $this->msg( 'uploadstash-nofiles' )->text() ) 00377 . ' ' 00378 . $refreshHtml 00379 ) ); 00380 } 00381 00382 return true; 00383 } 00384 } 00385 00386 class SpecialUploadStashTooLargeException extends MWException {};