MediaWiki
REL1_19
|
00001 <?php 00019 class SpecialUploadStash extends UnlistedSpecialPage { 00020 // UploadStash 00021 private $stash; 00022 00023 // Since we are directly writing the file to STDOUT, 00024 // we should not be reading in really big files and serving them out. 00025 // 00026 // We also don't want people using this as a file drop, even if they 00027 // share credentials. 00028 // 00029 // This service is really for thumbnails and other such previews while 00030 // uploading. 00031 const MAX_SERVE_BYTES = 1048576; // 1MB 00032 00033 public function __construct() { 00034 parent::__construct( 'UploadStash', 'upload' ); 00035 try { 00036 $this->stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash(); 00037 } catch ( UploadStashNotAvailableException $e ) { 00038 } 00039 } 00040 00047 public function execute( $subPage ) { 00048 $this->checkPermissions(); 00049 00050 if ( $subPage === null || $subPage === '' ) { 00051 return $this->showUploads(); 00052 } 00053 return $this->showUpload( $subPage ); 00054 } 00055 00062 public function showUpload( $key ) { 00063 // prevent callers from doing standard HTML output -- we'll take it from here 00064 $this->getOutput()->disable(); 00065 00066 try { 00067 $params = $this->parseKey( $key ); 00068 if ( $params['type'] === 'thumb' ) { 00069 return $this->outputThumbFromStash( $params['file'], $params['params'] ); 00070 } else { 00071 return $this->outputLocalFile( $params['file'] ); 00072 } 00073 } catch( UploadStashFileNotFoundException $e ) { 00074 $code = 404; 00075 $message = $e->getMessage(); 00076 } catch( UploadStashZeroLengthFileException $e ) { 00077 $code = 500; 00078 $message = $e->getMessage(); 00079 } catch( UploadStashBadPathException $e ) { 00080 $code = 500; 00081 $message = $e->getMessage(); 00082 } catch( SpecialUploadStashTooLargeException $e ) { 00083 $code = 500; 00084 $message = 'Cannot serve a file larger than ' . self::MAX_SERVE_BYTES . ' bytes. ' . $e->getMessage(); 00085 } catch( Exception $e ) { 00086 $code = 500; 00087 $message = $e->getMessage(); 00088 } 00089 00090 throw new HttpError( $code, $message ); 00091 } 00092 00101 private function parseKey( $key ) { 00102 $type = strtok( $key, '/' ); 00103 00104 if ( $type !== 'file' && $type !== 'thumb' ) { 00105 throw new UploadStashBadPathException( "Unknown type '$type'" ); 00106 } 00107 $fileName = strtok( '/' ); 00108 $thumbPart = strtok( '/' ); 00109 $file = $this->stash->getFile( $fileName ); 00110 if ( $type === 'thumb' ) { 00111 $srcNamePos = strrpos( $thumbPart, $fileName ); 00112 if ( $srcNamePos === false || $srcNamePos < 1 ) { 00113 throw new UploadStashBadPathException( 'Unrecognized thumb name' ); 00114 } 00115 $paramString = substr( $thumbPart, 0, $srcNamePos - 1 ); 00116 00117 $handler = $file->getHandler(); 00118 $params = $handler->parseParamString( $paramString ); 00119 return array( 'file' => $file, 'type' => $type, 'params' => $params ); 00120 } 00121 00122 return array( 'file' => $file, 'type' => $type ); 00123 } 00124 00133 private function outputThumbFromStash( $file, $params ) { 00134 00135 // this global, if it exists, points to a "scaler", as you might find in the Wikimedia Foundation cluster. See outputRemoteScaledThumb() 00136 // 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 00137 // happens to share it over NFS 00138 global $wgUploadStashScalerBaseUrl; 00139 00140 $flags = 0; 00141 if ( $wgUploadStashScalerBaseUrl ) { 00142 $this->outputRemoteScaledThumb( $file, $params, $flags ); 00143 } else { 00144 $this->outputLocallyScaledThumb( $file, $params, $flags ); 00145 } 00146 } 00147 00156 private function outputLocallyScaledThumb( $file, $params, $flags ) { 00157 00158 // n.b. this is stupid, we insist on re-transforming the file every time we are invoked. We rely 00159 // on HTTP caching to ensure this doesn't happen. 00160 00161 $flags |= File::RENDER_NOW; 00162 00163 $thumbnailImage = $file->transform( $params, $flags ); 00164 if ( !$thumbnailImage ) { 00165 throw new MWException( 'Could not obtain thumbnail' ); 00166 } 00167 00168 // we should have just generated it locally 00169 if ( !$thumbnailImage->getStoragePath() ) { 00170 throw new UploadStashFileNotFoundException( "no local path for scaled item" ); 00171 } 00172 00173 // now we should construct a File, so we can get mime and other such info in a standard way 00174 // n.b. mimetype may be different from original (ogx original -> jpeg thumb) 00175 $thumbFile = new UnregisteredLocalFile( false, 00176 $this->stash->repo, $thumbnailImage->getStoragePath(), false ); 00177 if ( !$thumbFile ) { 00178 throw new UploadStashFileNotFoundException( "couldn't create local file object for thumbnail" ); 00179 } 00180 00181 return $this->outputLocalFile( $thumbFile ); 00182 00183 } 00184 00198 private function outputRemoteScaledThumb( $file, $params, $flags ) { 00199 00200 // this global probably looks something like 'http://upload.wikimedia.org/wikipedia/test/thumb/temp' 00201 // do not use trailing slash 00202 global $wgUploadStashScalerBaseUrl; 00203 $scalerBaseUrl = $wgUploadStashScalerBaseUrl; 00204 00205 if( preg_match( '/^\/\//', $scalerBaseUrl ) ) { 00206 // this is apparently a protocol-relative URL, which makes no sense in this context, 00207 // since this is used for communication that's internal to the application. 00208 // default to http. 00209 $scalerBaseUrl = wfExpandUrl( $scalerBaseUrl, PROTO_CANONICAL ); 00210 } 00211 00212 // We need to use generateThumbName() instead of thumbName(), because 00213 // the suffix needs to match the file name for the remote thumbnailer 00214 // to work 00215 $scalerThumbName = $file->generateThumbName( $file->getName(), $params ); 00216 $scalerThumbUrl = $scalerBaseUrl . '/' . $file->getUrlRel() . 00217 '/' . rawurlencode( $scalerThumbName ); 00218 00219 // make a curl call to the scaler to create a thumbnail 00220 $httpOptions = array( 00221 'method' => 'GET', 00222 'timeout' => 'default' 00223 ); 00224 $req = MWHttpRequest::factory( $scalerThumbUrl, $httpOptions ); 00225 $status = $req->execute(); 00226 if ( ! $status->isOK() ) { 00227 $errors = $status->getErrorsArray(); 00228 $errorStr = "Fetching thumbnail failed: " . print_r( $errors, 1 ); 00229 $errorStr .= "\nurl = $scalerThumbUrl\n"; 00230 throw new MWException( $errorStr ); 00231 } 00232 $contentType = $req->getResponseHeader( "content-type" ); 00233 if ( ! $contentType ) { 00234 throw new MWException( "Missing content-type header" ); 00235 } 00236 return $this->outputContents( $req->getContent(), $contentType ); 00237 } 00238 00245 private function outputLocalFile( File $file ) { 00246 if ( $file->getSize() > self::MAX_SERVE_BYTES ) { 00247 throw new SpecialUploadStashTooLargeException(); 00248 } 00249 return $file->getRepo()->streamFile( $file->getPath(), 00250 array( 'Content-Transfer-Encoding: binary', 00251 'Expires: Sun, 17-Jan-2038 19:14:07 GMT' ) 00252 ); 00253 } 00254 00261 private function outputContents( $content, $contentType ) { 00262 $size = strlen( $content ); 00263 if ( $size > self::MAX_SERVE_BYTES ) { 00264 throw new SpecialUploadStashTooLargeException(); 00265 } 00266 self::outputFileHeaders( $contentType, $size ); 00267 print $content; 00268 return true; 00269 } 00270 00278 private static function outputFileHeaders( $contentType, $size ) { 00279 header( "Content-Type: $contentType", true ); 00280 header( 'Content-Transfer-Encoding: binary', true ); 00281 header( 'Expires: Sun, 17-Jan-2038 19:14:07 GMT', true ); 00282 // Bug 53032 - It shouldn't be a problem here, but let's be safe and not cache 00283 header( 'Cache-Control: private' ); 00284 header( "Content-Length: $size", true ); 00285 } 00286 00294 public static function tryClearStashedUploads( $formData ) { 00295 if ( isset( $formData['Clear'] ) ) { 00296 $stash = RepoGroup::singleton()->getLocalRepo()->getUploadStash(); 00297 wfDebug( "stash has: " . print_r( $stash->listFiles(), true ) ); 00298 if ( ! $stash->clear() ) { 00299 return Status::newFatal( 'uploadstash-errclear' ); 00300 } 00301 } 00302 return Status::newGood(); 00303 } 00304 00310 private function showUploads( $status = null ) { 00311 if ( $status === null ) { 00312 $status = Status::newGood(); 00313 } 00314 00315 // sets the title, etc. 00316 $this->setHeaders(); 00317 $this->outputHeader(); 00318 00319 // create the form, which will also be used to execute a callback to process incoming form data 00320 // this design is extremely dubious, but supposedly HTMLForm is our standard now? 00321 00322 $form = new HTMLForm( array( 00323 'Clear' => array( 00324 'type' => 'hidden', 00325 'default' => true, 00326 'name' => 'clear', 00327 ) 00328 ), $this->getContext(), 'clearStashedUploads' ); 00329 $form->setSubmitCallback( array( __CLASS__ , 'tryClearStashedUploads' ) ); 00330 $form->setTitle( $this->getTitle() ); 00331 $form->setSubmitText( wfMsg( 'uploadstash-clear' ) ); 00332 00333 $form->prepareForm(); 00334 $formResult = $form->tryAuthorizedSubmit(); 00335 00336 // show the files + form, if there are any, or just say there are none 00337 $refreshHtml = Html::element( 'a', 00338 array( 'href' => $this->getTitle()->getLocalURL() ), 00339 wfMsg( 'uploadstash-refresh' ) ); 00340 $files = $this->stash->listFiles(); 00341 if ( $files && count( $files ) ) { 00342 sort( $files ); 00343 $fileListItemsHtml = ''; 00344 foreach ( $files as $file ) { 00345 // TODO: Use Linker::link or even construct the list in plain wikitext 00346 $fileListItemsHtml .= Html::rawElement( 'li', array(), 00347 Html::element( 'a', array( 'href' => 00348 $this->getTitle( "file/$file" )->getLocalURL() ), $file ) 00349 ); 00350 } 00351 $this->getOutput()->addHtml( Html::rawElement( 'ul', array(), $fileListItemsHtml ) ); 00352 $form->displayForm( $formResult ); 00353 $this->getOutput()->addHtml( Html::rawElement( 'p', array(), $refreshHtml ) ); 00354 } else { 00355 $this->getOutput()->addHtml( Html::rawElement( 'p', array(), 00356 Html::element( 'span', array(), wfMsg( 'uploadstash-nofiles' ) ) 00357 . ' ' 00358 . $refreshHtml 00359 ) ); 00360 } 00361 00362 return true; 00363 } 00364 } 00365 00366 class SpecialUploadStashTooLargeException extends MWException {};