Example #1
0
 /**
  * Scale a file with a remote "scaler", as exists on the Wikimedia Foundation
  * cluster, and output it to STDOUT.
  * Note: Unlike the usual thumbnail process, the web client never sees the
  * cluster URL; we do the whole HTTP transaction to the scaler ourselves
  * and cat the results out.
  * Note: We rely on NFS to have propagated the file contents to the scaler.
  * However, we do not rely on the thumbnail being created in NFS and then
  * propagated back to our filesystem. Instead we take the results of the
  * HTTP request instead.
  * Note: No caching is being done here, although we are instructing the
  * client to cache it forever.
  *
  * @param File $file
  * @param array $params Scaling parameters ( e.g. array( width => '50' ) );
  * @param int $flags Scaling flags ( see File:: constants )
  * @throws MWException
  * @return bool Success
  */
 private function outputRemoteScaledThumb($file, $params, $flags)
 {
     // This option probably looks something like
     // '//upload.wikimedia.org/wikipedia/test/thumb/temp'. Do not use
     // trailing slash.
     $scalerBaseUrl = $this->getConfig()->get('UploadStashScalerBaseUrl');
     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 = $scalerBaseUrl . '/' . $file->getUrlRel() . '/' . rawurlencode($scalerThumbName);
     // make a curl call to the scaler to create a thumbnail
     $httpOptions = array('method' => 'GET', 'timeout' => 5);
     $req = MWHttpRequest::factory($scalerThumbUrl, $httpOptions, __METHOD__);
     $status = $req->execute();
     if (!$status->isOK()) {
         $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) {
         throw new MWException("Missing content-type header");
     }
     return $this->outputContents($req->getContent(), $contentType);
 }