/**
  * 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;
 }
 /**
  * Return HTML and messages for gallery editor
  */
 public static function getEditorDialog()
 {
     global $wgExtensionMessagesFiles, $wgTitle, $wgRequest;
     wfProfileIn(__METHOD__);
     // show upload form?
     $showUploadForm = WikiaPhotoGalleryHelper::isUploadAllowed();
     // list of recently uploaded images
     $recentlyUploaded = WikiaPhotoGalleryHelper::getRecentlyUploadedThumbs();
     // list of images on current article
     $imagesOnPage = WikiaPhotoGalleryHelper::getImagesFromPageThumbs($wgTitle);
     // render dialog
     $template = new EasyTemplate(dirname(__FILE__) . '/templates');
     $template->set_vars(array('alignments' => array('left', 'center', 'right'), 'imagesOnPage' => WikiaPhotoGalleryHelper::renderImagesList('images', $imagesOnPage), 'recentlyUploaded' => WikiaPhotoGalleryHelper::renderImagesList('uploaded', $recentlyUploaded), 'showUploadForm' => $showUploadForm));
     $html = $template->render('editorDialog');
     // get list of this extension messages
     $messages = array();
     require $wgExtensionMessagesFiles['WikiaPhotoGallery'];
     // contains i18n in $messages
     $list = array_keys($messages['en']);
     // additional messages
     $list[] = 'save';
     $list[] = 'ok';
     $list[] = 'cancel';
     // toolbar buttons tooltips
     $list[] = 'bold_tip';
     $list[] = 'italic_tip';
     $list[] = 'link_tip';
     $msg = array();
     foreach ($list as $key) {
         $msg[$key] = wfMsg($key);
     }
     // get list of gallery parameters default values
     $ig = new WikiaPhotoGallery();
     $defaultParamValues = $ig->getDefaultParamValues();
     $res = array('html' => $html, 'msg' => $msg, 'defaultParamValues' => $defaultParamValues);
     wfProfileOut(__METHOD__);
     return $res;
 }