function testSize()
 {
     $is = new ImageServing(array(1), 100, array("w" => 1, "h" => 1));
     $this->assertEquals('100px-56,237,0,180', $is->getCut(290, 180));
     $this->assertEquals('100px-68,285,0,216', $is->getCut(350, 216));
     $is = new ImageServing(array(1), 270, array("w" => 3, "h" => 1));
     $this->assertEquals('270px-0,428,57,200', $is->getCut(428, 285));
     $this->assertEquals('270px-0,669,119,342', $is->getCut(669, 593));
     $is = new ImageServing(array(1), 200, array("w" => 2, "h" => 1));
     $this->assertEquals('200px-0,314,65,222', $is->getCut(314, 654));
     $this->assertEquals('200px-0,572,36,322', $is->getCut(572, 355));
 }
 /**
  * Returns image from page.
  * @param $mPageId int page id
  * @return string - image url
  */
 protected function getImageFromPageId($mPageId)
 {
     if (!is_array($mPageId)) {
         $mPageId = array($mPageId);
     }
     $imageServing = new ImageServing($mPageId, $this->thumbWidth, array("w" => $this->thumbWidth, "h" => $this->thumbHeight));
     $imageUrl = '';
     foreach ($imageServing->getImages(1) as $value) {
         if (!empty($value[0]['name'])) {
             $tmpTitle = Title::newFromText($value[0]['name'], NS_FILE);
             $image = wfFindFile($tmpTitle);
             if (empty($image)) {
                 return '';
             }
             $imageUrl = wfReplaceImageServer($image->getThumbUrl($imageServing->getCut($image->getWidth(), $image->getHeight()) . "-" . $image->getName()));
         }
     }
     return $imageUrl;
 }
 public function saveSettings($settings, $cityId = null)
 {
     global $wgCityId, $wgUser;
     $cityId = empty($cityId) ? $wgCityId : $cityId;
     // Verify wordmark length ( CONN-116 )
     if (!empty($settings['wordmark-text'])) {
         $settings['wordmark-text'] = trim($settings['wordmark-text']);
     }
     if (empty($settings['wordmark-text'])) {
         // Do not save wordmark if its empty.
         unset($settings['wordmark-text']);
     } else {
         if (mb_strlen($settings['wordmark-text']) > 50) {
             $settings['wordmark-text'] = mb_substr($settings['wordmark-text'], 0, 50);
         }
     }
     if (isset($settings['favicon-image-name']) && strpos($settings['favicon-image-name'], 'Temp_file_') === 0) {
         $temp_file = new LocalFile(Title::newFromText($settings['favicon-image-name'], 6), RepoGroup::singleton()->getLocalRepo());
         $file = new LocalFile(Title::newFromText(self::FaviconImageName, 6), RepoGroup::singleton()->getLocalRepo());
         $file->upload($temp_file->getPath(), '', '');
         $temp_file->delete('');
         Wikia::invalidateFavicon();
         $settings['favicon-image-url'] = $file->getURL();
         $settings['favicon-image-name'] = $file->getName();
         $file->repo->forceMaster();
         $history = $file->getHistory(1);
         if (count($history) == 1) {
             $oldFaviconFile = array('url' => $history[0]->getURL(), 'name' => $history[0]->getArchiveName());
         }
     }
     if (isset($settings['wordmark-image-name']) && strpos($settings['wordmark-image-name'], 'Temp_file_') === 0) {
         $temp_file = new LocalFile(Title::newFromText($settings['wordmark-image-name'], 6), RepoGroup::singleton()->getLocalRepo());
         $file = new LocalFile(Title::newFromText(self::WordmarkImageName, 6), RepoGroup::singleton()->getLocalRepo());
         $file->upload($temp_file->getPath(), '', '');
         $temp_file->delete('');
         $settings['wordmark-image-url'] = $file->getURL();
         $settings['wordmark-image-name'] = $file->getName();
         $file->repo->forceMaster();
         $history = $file->getHistory(1);
         if (count($history) == 1) {
             $oldFile = array('url' => $history[0]->getURL(), 'name' => $history[0]->getArchiveName());
         }
     }
     if (isset($settings['background-image-name']) && strpos($settings['background-image-name'], 'Temp_file_') === 0) {
         $temp_file = new LocalFile(Title::newFromText($settings['background-image-name'], 6), RepoGroup::singleton()->getLocalRepo());
         $file = new LocalFile(Title::newFromText(self::BackgroundImageName, 6), RepoGroup::singleton()->getLocalRepo());
         $file->upload($temp_file->getPath(), '', '');
         $temp_file->delete('');
         $settings['background-image'] = $file->getURL();
         $settings['background-image-name'] = $file->getName();
         $settings['background-image-width'] = $file->getWidth();
         $settings['background-image-height'] = $file->getHeight();
         $imageServing = new ImageServing(null, 120, array("w" => "120", "h" => "65"));
         $settings['user-background-image'] = $file->getURL();
         $settings['user-background-image-thumb'] = wfReplaceImageServer($file->getThumbUrl($imageServing->getCut($file->getWidth(), $file->getHeight(), "origin") . "-" . $file->getName()));
         $file->repo->forceMaster();
         $history = $file->getHistory(1);
         if (count($history) == 1) {
             $oldBackgroundFile = array('url' => $history[0]->getURL(), 'name' => $history[0]->getArchiveName());
         }
     }
     $reason = wfMsg('themedesigner-reason', $wgUser->getName());
     // update history
     if (!empty($GLOBALS[self::WikiFactoryHistory])) {
         $history = $GLOBALS[self::WikiFactoryHistory];
         $lastItem = end($history);
         $revisionId = intval($lastItem['revision']) + 1;
     } else {
         $history = array();
         $revisionId = 1;
     }
     // #140758 - Jakub
     // validation
     // default color values
     foreach (ThemeDesignerHelper::getColorVars() as $sColorVar => $sDefaultValue) {
         if (!isset($settings[$sColorVar]) || !ThemeDesignerHelper::isValidColor($settings[$sColorVar])) {
             $settings[$sColorVar] = $sDefaultValue;
         }
     }
     // update WF variable with current theme settings
     WikiFactory::setVarByName(self::WikiFactorySettings, $cityId, $settings, $reason);
     // add entry
     $history[] = array('settings' => $settings, 'author' => $wgUser->getName(), 'timestamp' => wfTimestampNow(), 'revision' => $revisionId);
     // limit history size to last 10 changes
     $history = array_slice($history, -self::HistoryItemsLimit);
     if (count($history) > 1) {
         for ($i = 0; $i < count($history) - 1; $i++) {
             if (isset($oldFaviconFile) && isset($history[$i]['settings']['favicon-image-name'])) {
                 if ($history[$i]['settings']['favicon-image-name'] == self::FaviconImageName) {
                     $history[$i]['settings']['favicon-image-name'] = $oldFaviconFile['name'];
                     $history[$i]['settings']['favicon-image-url'] = $oldFaviconFile['url'];
                 }
             }
             if (isset($oldFile) && isset($history[$i]['settings']['wordmark-image-name'])) {
                 if ($history[$i]['settings']['wordmark-image-name'] == self::WordmarkImageName) {
                     $history[$i]['settings']['wordmark-image-name'] = $oldFile['name'];
                     $history[$i]['settings']['wordmark-image-url'] = $oldFile['url'];
                 }
             }
             if (isset($oldBackgroundFile) && isset($history[$i]['settings']['background-image-name'])) {
                 if ($history[$i]['settings']['background-image-name'] == self::BackgroundImageName) {
                     $history[$i]['settings']['background-image-name'] = $oldBackgroundFile['name'];
                 }
             }
         }
     }
     WikiFactory::setVarByName(self::WikiFactoryHistory, $cityId, $history, $reason);
 }
 /**
  * Returns image or snippet for the category on id basis.
  * Uses in modified getArticle
  *
  * @param $iCategoryId int category pageId
  * @return array
  */
 protected function getCategoryImageOrSnippet($iCategoryId)
 {
     $title = Title::newFromID($iCategoryId);
     $sCategoryDBKey = $title->getDBKey();
     // tries to get image from images in category
     $result = CategoryDataService::getAlphabetical($sCategoryDBKey, NS_FILE, 1);
     if (!empty($result)) {
         $counter = 0;
         foreach ($result as $item) {
             if ($counter > F::App()->wg->maxCategoryExhibitionSubcatChecks) {
                 break;
             }
             $imageServing = new ImageServing(array($item['page_id']), $this->thumbWidth, array("w" => $this->thumbWidth, "h" => $this->thumbHeight));
             $itemTitle = Title::newFromID($item['page_id']);
             $image = wfFindFile($itemTitle);
             if (!empty($image)) {
                 $imageSrc = wfReplaceImageServer($image->getThumbUrl($imageServing->getCut($image->width, $image->height) . "-" . $image->getName()));
                 return array('imageUrl' => (string) $imageSrc, 'snippetText' => '');
             }
             $counter++;
         }
     }
     // if no images found, tries to get image or snippet from artice
     unset($result);
     $result = CategoryDataService::getAlphabetical($sCategoryDBKey, NS_MAIN, 10);
     if (!empty($result)) {
         $counter = 0;
         $snippetText = '';
         $imageUrl = '';
         foreach ($result as $item) {
             if ($counter > F::App()->wg->maxCategoryExhibitionSubcatChecks) {
                 break;
             }
             $imageUrl = $this->getImageFromPageId($item['page_id']);
             if (!empty($imageUrl)) {
                 break;
             }
             if (empty($snippetText)) {
                 $snippetService = new ArticleService($item['page_id']);
                 $snippetText = $snippetService->getTextSnippet();
             }
             $counter++;
         }
         return array('imageUrl' => $imageUrl, 'snippetText' => $snippetText);
     } else {
         return array('imageUrl' => '', 'snippetText' => '');
     }
 }
 private function getImageSrc($filename)
 {
     $imageSrc = '';
     $fileTitle = Title::newFromText($filename, NS_FILE);
     $image = wfFindFile($fileTitle);
     if (!is_object($image) || $image->height == 0 || $image->width == 0) {
         return $imageSrc;
     } else {
         $thumbDim = $image->height > $image->width ? $image->width : $image->height;
         $imageServing = new ImageServing(array($fileTitle->getArticleID()), $thumbDim, array("w" => $thumbDim, "h" => $thumbDim));
         $imageSrc = wfReplaceImageServer($image->getThumbUrl($imageServing->getCut($thumbDim, $thumbDim) . "-" . $image->getName()));
     }
     return $imageSrc;
 }
 public function getHTML()
 {
     global $wgCategoryExhibitionMediaSectionRows;
     $cachedContent = $this->getFromCache();
     if (empty($cachedContent)) {
         // grabs data for videos and images
         $aTmpData = $this->fetchSectionItems(array(NS_FILE));
         // we wan't old videos
         if (is_array($aTmpData) && count($aTmpData) > 0) {
             $pages = Paginator::newFromArray($aTmpData, $wgCategoryExhibitionMediaSectionRows * 4);
             $pageData = $pages->getPage($this->paginatorPosition, true);
             $aData = array();
             foreach ($pageData as $item) {
                 $itemTitle = Title::newFromID($item['page_id']);
                 $forceHeight = '';
                 $forceWidth = '';
                 $isVideo = WikiaFileHelper::isFileTypeVideo($itemTitle);
                 // item is image
                 $image = wfFindFile($itemTitle);
                 $elementClass = 'lightbox';
                 if (!is_object($image) || $image->height == 0 || $image->width == 0) {
                     $imageSrc = '';
                 } else {
                     $proportions = $image->width / $image->height;
                     if ($proportions < 1) {
                         $calculatedWidth = floor($proportions * $this->thumbWidth);
                     } else {
                         $calculatedWidth = $this->thumbMedia;
                     }
                     $forceWidth = floor($calculatedWidth);
                     $forceHeight = floor($calculatedWidth / $proportions);
                     $imageServing = new ImageServing(array($item['page_id']), $calculatedWidth, array("w" => $image->width, "h" => $image->height));
                     $imageSrc = wfReplaceImageServer($image->getThumbUrl($imageServing->getCut($image->width, $image->height) . "-" . $image->getName()));
                     if ($isVideo) {
                         $videoSizeClass = ThumbnailHelper::getThumbnailSize($forceWidth);
                         $elementClass .= ' video video-thumbnail ' . $videoSizeClass;
                     }
                 }
                 $linkedFiles = $this->getLinkedFiles($itemTitle);
                 if (!empty($linkedFiles)) {
                     $linkText = $linkedFiles->getText();
                     $linkFullUrl = $linkedFiles->getFullURL();
                 } else {
                     $linkText = '';
                     $linkFullUrl = '';
                 }
                 // types casting for proper caching;
                 $aData[] = array('id' => $item['page_id'], 'title' => $itemTitle->getText(), 'key' => $itemTitle->getDBKey(), 'img' => (string) $imageSrc, 'url' => $itemTitle->getFullURL(), 'dimensions' => array('w' => (int) $forceWidth, 'h' => (int) $forceHeight), 'class' => $elementClass, 'data-ref' => $itemTitle->getPrefixedURL(), 'targetUrl' => $linkFullUrl, 'targetText' => $linkText, 'isVideo' => $isVideo);
             }
             $aContent = array('data' => $aData, 'category' => $this->categoryTitle->getText(), 'paginator' => $pages->getBarHTML($this->sUrl));
             $this->saveToCache($aContent);
         } else {
             return false;
         }
     } else {
         $aContent = $cachedContent;
     }
     if (!empty($aContent) && is_array($aContent)) {
         $oTmpl = new EasyTemplate(dirname(__FILE__) . "/templates/");
         $oTmpl->set_vars($aContent);
         $oTmpl->set_vars(array('fromAjax' => $this->isFromAjax));
         if ($this->isFromAjax) {
             return array('page' => $oTmpl->render($this->templateName), 'paginator' => $oTmpl->mVars['paginator']);
         } else {
             return $oTmpl->render($this->templateName);
         }
     }
 }
 /**
  * @author Jakub Kurcek
  * @param format string 'rss' or 'atom'
  */
 private function FeedRecentImages($format)
 {
     global $wgTitle, $wgLang, $wgRequest;
     // local settings
     $maxImagesNumber = 20;
     $defaultWidth = 124;
     $defaultHeight = 72;
     $imageServing = new ImageServing(array(), $defaultWidth, array("w" => $defaultWidth, "h" => $defaultHeight));
     $dbw = wfGetDB(DB_SLAVE);
     $res = $dbw->select('image', array("img_name", "img_user_text", "img_size", "img_width", "img_height"), array("img_media_type != 'VIDEO'", "img_width > 32", "img_height > 32"), false, array("ORDER BY" => "img_timestamp DESC", "LIMIT" => $maxImagesNumber));
     $thumbSize = $wgRequest->getText("size", false);
     if ($defaultWidth) {
         $thumbSize = (int) $thumbSize;
     }
     $feedArray = array();
     while ($row = $dbw->fetchObject($res)) {
         $tmpTitle = Title::newFromText($row->img_name, NS_FILE);
         $image = wfFindFile($tmpTitle);
         if (!$image) {
             continue;
         }
         $testImage = wfReplaceImageServer($image->getThumbUrl($imageServing->getCut($row->img_width, $row->img_height) . "-" . $image->getName()));
         $feedArray[] = array('title' => '', 'description' => $row->img_name, 'url' => $tmpTitle->getFullURL(), 'date' => $image->getTimestamp(), 'author' => $row->img_user_text, 'otherTags' => array('image' => $testImage));
     }
     $this->showFeed($format, wfMsg('feed-title-recent-images'), $feedArray);
 }
 public function executeBackgroundImageUpload()
 {
     // check rights
     if (!ThemeDesignerHelper::checkAccess()) {
         $this->displayRestrictionError(__METHOD__);
     }
     $upload = new UploadBackgroundFromFile();
     $status = $this->uploadImage($upload);
     if ($status['status'] === 'uploadattempted' && $status['isGood']) {
         $file = $upload->getLocalFile();
         /* @var $file LocalFile */
         $this->backgroundImageUrl = wfReplaceImageServer($file->getUrl());
         $this->backgroundImageName = $file->getName();
         $this->backgroundImageHeight = $file->getHeight();
         $this->backgroundImageWidth = $file->getWidth();
         //get cropped URL
         $is = new ImageServing(null, 120, array("w" => "120", "h" => "100"));
         $this->backgroundImageThumb = wfReplaceImageServer($file->getThumbUrl($is->getCut($file->width, $file->height, "origin") . "-" . $file->getName()));
         // if background image url is not set then it means there was some problem
         if ($this->backgroundImageUrl == null) {
             $this->errors = array(wfMsg('themedesigner-unknown-error'));
         }
     } else {
         if ($status['status'] === 'error') {
             $this->errors = $status['errors'];
         }
     }
 }