/**
  * Handle image upload
  *
  * Returns array with uploaded files details or error details
  */
 public function uploadImage($uploadFieldName = self::DEFAULT_FILE_FIELD_NAME, $destFileName = null, $forceOverwrite = false)
 {
     global $IP, $wgRequest, $wgUser;
     wfProfileIn(__METHOD__);
     $ret = false;
     // check whether upload is enabled (RT #53714)
     if (!WikiaPhotoGalleryHelper::isUploadAllowed()) {
         $ret = array('error' => true, 'message' => wfMsg('uploaddisabled'));
         wfProfileOut(__METHOD__);
         return $ret;
     }
     $imageName = stripslashes(!empty($destFileName) ? $destFileName : $wgRequest->getFileName($uploadFieldName));
     // validate name and content of uploaded photo
     $nameValidation = $this->checkImageName($imageName, $uploadFieldName);
     if ($nameValidation == UploadBase::SUCCESS) {
         // get path to uploaded image
         $imagePath = $wgRequest->getFileTempName($uploadFieldName);
         // check if image with this name is already uploaded
         if ($this->imageExists($imageName) && !$forceOverwrite) {
             // upload as temporary file
             $this->log(__METHOD__, "image '{$imageName}' already exists!");
             $tempName = $this->tempFileName($wgUser);
             $title = Title::makeTitle(NS_FILE, $tempName);
             $localRepo = RepoGroup::singleton()->getLocalRepo();
             $file = new FakeLocalFile($title, $localRepo);
             $file->upload($wgRequest->getFileTempName($uploadFieldName), '', '');
             // store uploaded image in GarbageCollector (image will be removed if not used)
             $tempId = $this->tempFileStoreInfo($tempName);
             // generate thumbnail (to fit 200x200 box) of temporary file
             $width = min(WikiaPhotoGalleryHelper::thumbnailMaxWidth, $file->width);
             $height = min(WikiaPhotoGalleryHelper::thumbnailMaxHeight, $file->height);
             $thumbnail = $file->transform(array('height' => $height, 'width' => $width));
             // split uploaded file name into name + extension (foo-bar.png => foo-bar + png)
             list($fileName, $extensionsName) = UploadBase::splitExtensions($imageName);
             $extensionName = !empty($extensionsName) ? end($extensionsName) : '';
             $this->log(__METHOD__, 'upload successful');
             $ret = array('conflict' => true, 'name' => $imageName, 'nameParts' => array($fileName, $extensionName), 'tempId' => $tempId, 'size' => array('height' => $file->height, 'width' => $file->width), 'thumbnail' => array('height' => $thumbnail->height, 'url' => $thumbnail->url, 'width' => $thumbnail->width));
         } else {
             // use regular MW upload
             $this->log(__METHOD__, "image '{$imageName}' is new one - uploading as MW file");
             $this->log(__METHOD__, "uploading '{$imagePath}' as File:{$imageName}");
             // create title and file objects for MW image to create
             $imageTitle = Title::newFromText($imageName, NS_FILE);
             $imageFile = new LocalFile($imageTitle, RepoGroup::singleton()->getLocalRepo());
             // perform upload
             $result = $imageFile->upload($imagePath, '', '');
             $this->log(__METHOD__, !empty($result->ok) ? 'upload successful' : 'upload failed');
             $ret = array('success' => !empty($result->ok), 'name' => $imageName, 'size' => array('height' => !empty($result->ok) ? $imageFile->getHeight() : 0, 'width' => !empty($result->ok) ? $imageFile->getWidth() : 0));
         }
     } else {
         $reason = $nameValidation;
         $this->log(__METHOD__, "upload failed - file name is not valid (error #{$reason})");
         $ret = array('error' => true, 'reason' => $reason, 'message' => $this->translateError($reason));
     }
     wfProfileOut(__METHOD__);
     return $ret;
 }
Exemplo n.º 2
0
 private function executeImage()
 {
     if (empty($this->mParams['tempName'])) {
         $this->dieUsageMsg('The tempName parameter must be set');
     }
     $tempFile = new FakeLocalFile(Title::newFromText($this->mParams['tempName'], 6), RepoGroup::singleton()->getLocalRepo());
     $duplicate = $this->getFileDuplicate($tempFile->getLocalRefPath());
     if ($duplicate) {
         return array('title' => $duplicate->getTitle()->getText());
     } else {
         $title = $this->getUniqueTitle(wfStripIllegalFilenameChars($this->mParams['title']));
         if (isset($this->mParams['license'])) {
             $pageText = SpecialUpload::getInitialPageText('', $this->mParams['license']);
         }
         $file = new LocalFile($title, RepoGroup::singleton()->getLocalRepo());
         $file->upload($tempFile->getPath(), '', $pageText ? $pageText : '');
         return array('title' => $file->getTitle()->getText());
     }
 }
 /**
  * Remove the data about given file from the garbage collector
  */
 public function tempFileClearInfo($id)
 {
     global $wgExternalSharedDB;
     wfProfileIn(__METHOD__);
     $imagePath = $this->tempFileGetPath($id);
     $imageName = basename($imagePath);
     $this->log(__METHOD__, "removing temp file '{$imageName}' (#{$id})");
     // remove from file repo
     $imageTitle = Title::newFromText($imageName, NS_FILE);
     $repo = RepoGroup::singleton()->getLocalRepo();
     $imageFile = new FakeLocalFile($imageTitle, $repo);
     $imageFile->delete('');
     // remove from DB
     $dbw = wfGetDB(DB_MASTER, array(), $wgExternalSharedDB);
     $dbw->delete('garbage_collector', array('gc_id' => $id), __METHOD__);
     $dbw->commit();
     wfProfileOut(__METHOD__);
 }
Exemplo n.º 4
0
 private function createTempFile($filepath)
 {
     $tempFile = new FakeLocalFile(Title::newFromText(uniqid('Temp_', true), 6), RepoGroup::singleton()->getLocalRepo());
     $tempFile->upload($filepath, '', '');
     // TODO: Add to the garbage collector
     return $tempFile;
 }
Exemplo n.º 5
0
 function clean()
 {
     global $wgRequest;
     $file = new FakeLocalFile(Title::newFromText($wgRequest->getVal('mwname'), 6), RepoGroup::singleton()->getLocalRepo());
     $file->delete('');
     $this->tempFileClearInfo($wgRequest->getVal('tempid'));
 }
 /**
  * @param $fileName String
  * @param $fileuploader WikiaTempFilesUpload
  * @return bool|int|MediaTransformOutput
  */
 protected function storeInTempImage($fileName, $fileuploader)
 {
     wfProfileIn(__METHOD__);
     if (filesize($fileName) > self::AVATAR_MAX_SIZE) {
         wfProfileOut(__METHOD__);
         return UPLOAD_ERR_FORM_SIZE;
     }
     $tempName = $fileuploader->tempFileName($this->wg->User);
     $title = Title::makeTitle(NS_FILE, $tempName);
     $localRepo = RepoGroup::singleton()->getLocalRepo();
     /**
      * @var $ioh ImageOperationsHelper
      */
     $ioh = new ImageOperationsHelper();
     $out = $ioh->postProcessFile($fileName);
     if ($out !== true) {
         wfProfileOut(__METHOD__);
         return $out;
     }
     $file = new FakeLocalFile($title, $localRepo);
     $status = $file->upload($fileName, '', '');
     if ($status->ok) {
         $width = min(self::AVATAR_DEFAULT_SIZE, $file->width);
         $height = min(self::AVATAR_DEFAULT_SIZE, $file->height);
         $thumbnail = $file->transform(['height' => $height, 'width' => $width]);
     } else {
         $errors = $status->getErrorsArray();
         $errMsg = 'Unable to upload temp file fo avatar. Error(s): ';
         foreach ($errors as $error) {
             $errMsg .= $error[0] . ', ';
         }
         $errMsg = rtrim($errMsg, ', ');
         wfDebugLog(__METHOD__, $errMsg);
         $thumbnail = false;
     }
     wfProfileOut(__METHOD__);
     return $thumbnail;
 }
Exemplo n.º 7
0
<input id="ImageUploadExtraId" type="hidden" value="<?php 
echo urlencode($extraId);
?>
" />
<?php 
$file_mwname = new FakeLocalFile(Title::newFromText($mwname, 6), RepoGroup::singleton()->getLocalRepo());
$file_name = new LocalFile(Title::newFromText($partname . '.' . $extension, 6), RepoGroup::singleton()->getLocalRepo());
echo wfMsg('wmu-conflict-inf', $file_name->getName());
?>
<table cellspacing="0" id="ImageUploadConflictTable">
	<tr>
		<td style="border-right: 1px solid #CCC;">
			<h2><?php 
echo wfMsg('wmu-rename');
?>
</h2>
			<div style="margin: 5px 0;">
				<input type="text" id="ImageUploadRenameName" value="<?php 
echo $partname;
?>
" />
				<label for="ImageUploadRenameName">.<?php 
echo $extension;
?>
</label>
	                        <input id="ImageUploadRenameExtension" type="hidden" value="<?php 
echo $extension;
?>
" />
				<input type="button" value="<?php 
echo wfMsg('wmu-insert');
 /**
  * @param $fileName String
  * @param $fileuploader WikiaTempFilesUpload
  * @return bool|int|MediaTransformOutput
  */
 protected function storeInTempImage($fileName, $fileuploader)
 {
     $this->app->wf->ProfileIn(__METHOD__);
     if (filesize($fileName) > self::AVATAR_MAX_SIZE) {
         return UPLOAD_ERR_FORM_SIZE;
     }
     $tempName = $fileuploader->tempFileName($this->wg->User);
     $title = Title::makeTitle(NS_FILE, $tempName);
     $localRepo = RepoGroup::singleton()->getLocalRepo();
     /**
      * @var $ioh ImageOperationsHelper
      */
     $ioh = F::build('ImageOperationsHelper');
     $out = $ioh->postProcessFile($fileName);
     if ($out !== true) {
         return $out;
     }
     $file = new FakeLocalFile($title, $localRepo);
     $file->upload($fileName, '', '');
     $width = min(self::AVATAR_DEFAULT_SIZE, $file->width);
     $height = min(self::AVATAR_DEFAULT_SIZE, $file->height);
     $thumbnail = $file->transform(array('height' => $height, 'width' => $width));
     $this->app->wf->ProfileOut(__METHOD__);
     return $thumbnail;
 }