Пример #1
0
 protected static function getFileInfo($name)
 {
     $info = \WikiaDataAccess::cache(self::getFileKey($name), self::ICON_CACHE_TTL, function () use($name) {
         $file = \GlobalFile::newFromText($name . '.gif', \Wikia::NEWSLETTER_WIKI_ID);
         return ['name' => $name, 'url' => $file->getUrlGenerator()->url(), 'height' => $file->getHeight(), 'width' => $file->getWidth()];
     });
     return $info;
 }
Пример #2
0
 public static function uploadFile(PromoImage $promoImage, GlobalTitle $sourceTitle, $sourceWikiId)
 {
     $sourceFile = \GlobalFile::newFromText($sourceTitle->getText(), $sourceWikiId);
     if (!$sourceFile->exists()) {
         echo "sourceFile doesn't exist" . PHP_EOL;
         return false;
     }
     $sourceImageUrl = $sourceFile->getUrl();
     $user = User::newFromName('WikiaBot');
     $imageData = new stdClass();
     $imageData->name = $promoImage->getPathname();
     $imageData->description = $imageData->comment = wfMessage('wikiahome-image-auto-uploaded-comment')->plain();
     $result = ImagesService::uploadImageFromUrl($sourceImageUrl, $imageData, $user);
     if (!$result['status']) {
         var_dump($result['errors'], $sourceImageUrl);
     }
     return $result['status'];
 }
Пример #3
0
 public static function getImageSrcByTitle($cityId, $articleTitle, $width = null, $height = null)
 {
     global $wgMemc;
     wfProfileIn(__METHOD__);
     $imageKey = wfSharedMemcKey('image_url_from_wiki', $cityId . $articleTitle . $width . $height);
     $imageSrc = $wgMemc->get($imageKey);
     if ($imageSrc === false) {
         $globalFile = GlobalFile::newFromText($articleTitle, $cityId);
         if ($globalFile->exists()) {
             $imageSrc = $globalFile->getCrop($width, $height);
         } else {
             $imageSrc = null;
         }
         $wgMemc->set($imageKey, $imageSrc, 60 * 60 * 24 * 3);
     }
     wfProfileOut(__METHOD__);
     return $imageSrc;
 }
Пример #4
0
 /**
  * @dataProvider testNewFromTextDbNameMatchProvider
  * @group UsingDB
  */
 public function testNewFromTextDbNameMatch($row, $cityId)
 {
     $this->getSelectRowMock()->expects($this->any())->method('selectRow')->will($this->returnCallback(function ($table, $vars, $conds, $fname) use($row) {
         if ($fname == 'GlobalFile::loadData') {
             if ($conds['img_name'] == $row->img_name) {
                 return $row;
             } else {
                 return false;
             }
         } else {
             // don't mess with WikiFactory accessing database
             return $this->getCurrentInvocation()->callOriginal();
         }
     }));
     $dbName = $row->img_name;
     $name = str_replace("_", " ", $dbName);
     $file = GlobalFile::newFromText($dbName, $cityId);
     $file2 = GlobalFile::newFromText($name, $cityId);
     $file3 = GlobalFile::newFromText("Not-existing-Image.jpg", $cityId);
     $this->assertTrue($file->exists());
     $this->assertTrue($file2->exists());
     $this->assertFalse($file3->exists());
 }
 /**
  * @param int $imageId
  * @param string $destinationName
  * @param int $targetWikiId
  * @param int $sourceWikiId
  * @return array
  */
 public function uploadSingleImage($imageId, $destinationName, $targetWikiId, $sourceWikiId)
 {
     global $IP;
     $imageTitle = \GlobalTitle::newFromId($imageId, $sourceWikiId);
     $sourceFile = \GlobalFile::newFromText($imageTitle->getText(), $sourceWikiId);
     if ($sourceFile->exists()) {
         $sourceImageUrl = $sourceFile->getUrl();
     } else {
         $this->error('image is not accessible', ['city_id' => $sourceWikiId, 'title' => $imageTitle->getText()]);
         return ['status' => 1];
     }
     $cityUrl = \WikiFactory::getVarValueByName("wgServer", $targetWikiId);
     if (empty($cityUrl)) {
         $this->error('unable to get wgServer', ['wiki_id' => $targetWikiId]);
         return ['status' => 1];
     }
     $destinationName = \PromoImage::fromPathname($destinationName)->ensureCityIdIsSet($sourceWikiId)->getPathname();
     $command = "SERVER_ID={$targetWikiId} php {$IP}/maintenance/wikia/ImageReview/PromoteImage/upload.php" . ' --originalimageurl=' . escapeshellarg($sourceImageUrl) . ' --destimagename=' . escapeshellarg($destinationName) . ' --wikiid=' . escapeshellarg($sourceWikiId);
     $output = wfShellExec($command, $exitStatus);
     if ($exitStatus) {
         $this->error('uploadSingleImage error', ['command' => $command, 'city_url' => $cityUrl, 'output' => $output, 'exitStatus' => $exitStatus]);
     } else {
         $this->info('uploadSingleImage success', ['output' => $output, 'src_img_url' => $sourceImageUrl, 'dest_name' => $destinationName]);
     }
     $output = json_decode($output);
     return ['status' => $exitStatus, 'name' => $output->name, 'id' => $output->id];
 }
Пример #6
0
 public static function getFaviconFullUrl()
 {
     global $wgMemc;
     $mMemcacheKey = wfMemcKey(self::FAVICON_URL_CACHE_KEY);
     $mData = $wgMemc->get($mMemcacheKey);
     $faviconFilename = 'Favicon.ico';
     if (empty($mData)) {
         $localFaviconTitle = Title::newFromText($faviconFilename, NS_FILE);
         #FIXME: Checking existance of Title in order to use File. #VID-1744
         if ($localFaviconTitle->exists()) {
             $localFavicon = wfFindFile($faviconFilename);
         }
         if ($localFavicon) {
             $favicon = $localFavicon->getURL();
         } else {
             $favicon = GlobalFile::newFromText($faviconFilename, self::COMMUNITY_WIKI_ID)->getURL();
         }
         $wgMemc->set($mMemcacheKey, $favicon, 86400);
     }
     return $mData;
 }
Пример #7
0
 /**
  * Get thumbnail for given image.
  *
  * @param $fileName
  * @param $cityId
  */
 protected function getThumbData($fileName, $cityId)
 {
     $f = GlobalFile::newFromText($fileName, $cityId);
     if ($f instanceof GlobalFile && $f->exists()) {
         return ['url' => $f->getUrl(), 'width' => $f->getWidth(), 'height' => $f->getHeight()];
     } else {
         return ['url' => '', 'width' => null, 'height' => null];
     }
 }
Пример #8
0
 /**
  * @param $outputModel
  * @return mixed
  */
 protected function getImage($wikiId, $articleId)
 {
     $dbName = '';
     try {
         $row = \WikiFactory::getWikiByID($wikiId);
         if ($row) {
             $dbName = $row->city_dbname;
             if (!empty($dbName)) {
                 $db = wfGetDB(DB_SLAVE, [], $dbName);
                 // throws if database does not exits.
                 $imageServing = new \ImageServing([$articleId], self::IMAGE_SIZE, self::IMAGE_SIZE, $db);
                 $isResult = $imageServing->getImages(1);
                 $images = isset($isResult[$articleId]) ? $isResult[$articleId] : false;
                 if ($images && sizeof($images) > 0) {
                     $imageName = $images[0]['name'];
                     $file = \GlobalFile::newFromText($imageName, $wikiId);
                     if ($file->exists()) {
                         return $imageServing->getUrl($file, $file->getWidth(), $file->getHeight());
                     }
                 }
             }
         }
     } catch (\DBConnectionError $ex) {
         // Swallow this exception. there is no simple way of telling if database does not exist other than catching exception.
         // Or am I wrong ?
         \Wikia::log(__METHOD__, false, "Cannot get database connection to " . $dbName);
     }
     return null;
 }
 /**
  * @param string $imageName
  * @param string $lang
  * @param int $wikiId
  * @return GlobalFile|null
  */
 protected function findGlobalFileImage($imageName, $lang, $wikiId)
 {
     //try to find image on lang specific corporate wiki
     $f = null;
     $visualizationModel = new CityVisualization();
     $cityList = $visualizationModel->getVisualizationWikisData();
     if (isset($cityList[$lang])) {
         $f = GlobalFile::newFromText($imageName, $cityList[$lang]['wikiId']);
     } else {
         //if image wasn't found, try to find it on wiki itself
         $promoImage = (new PromoImage(PromoImage::MAIN))->setCityId($wikiId);
         $f = $promoImage->getOriginFile();
     }
     return $f;
 }
Пример #10
0
 function uploadSingleImage($imageId, $destinationName, $targetWikiId, $sourceWikiId)
 {
     global $IP, $wgWikiaLocalSettingsPath;
     $retval = "";
     $imageTitle = GlobalTitle::newFromId($imageId, $sourceWikiId);
     $sourceImageUrl = null;
     $sourceFile = \GlobalFile::newFromText($imageTitle->getText(), $sourceWikiId);
     if ($sourceFile->exists()) {
         $sourceImageUrl = $sourceFile->getUrl();
     } else {
         $this->log('Apparently the image from city_id=' . $sourceWikiId . ' ' . $imageTitle->getText() . ' is unaccessible');
         return array('status' => 1);
     }
     $city_url = WikiFactory::getVarValueByName("wgServer", $targetWikiId);
     if (empty($city_url)) {
         $this->log('Apparently the server for ' . $targetWikiId . ' is not available via WikiFactory');
         return array('status' => 1);
     }
     $destinationName = PromoImage::fromPathname($destinationName)->ensureCityIdIsSet($sourceWikiId)->getPathname();
     $sCommand = "SERVER_ID={$targetWikiId} php {$IP}/maintenance/wikia/ImageReview/PromoteImage/upload.php";
     $sCommand .= " --originalimageurl=" . escapeshellarg($sourceImageUrl);
     $sCommand .= " --destimagename=" . escapeshellarg($destinationName);
     $sCommand .= " --wikiid=" . escapeshellarg($sourceWikiId);
     $sCommand .= " --conf {$wgWikiaLocalSettingsPath}";
     $logdata = ['command' => $sCommand, 'city_url' => $city_url];
     $output = wfShellExec($sCommand, $retval);
     $logdata['output'] = $output;
     $logdata['retval'] = $retval;
     if ($retval) {
         $this->log('Upload error! (' . $city_url . '). Error code returned: ' . $retval . ' Error was: ' . $output);
     } else {
         $this->log('Upload successful: ' . $output);
     }
     $output = json_decode($output);
     return array('status' => $retval, 'name' => $output->name, 'id' => $output->id);
 }
 protected function saveAdditionalFiles($additionalImagesNames)
 {
     $unchangedTypes = array();
     $imagesToProcess = array();
     $allFiles = array();
     foreach ($additionalImagesNames as $singleFileName) {
         $promoImage = PromoImage::fromPathname($singleFileName);
         if (!$promoImage->isType(PromoImage::INVALID)) {
             // check if file exists, if not do not add it to processed files, effectively removing it
             $file = GlobalFile::newFromText($promoImage->getPathname(), $this->wg->cityId);
             if ($file->exists()) {
                 array_push($promoImages, $promoImage);
             }
             if (in_array($unchangedTypes, $promoImage->getType())) {
                 WikiaLogger::instance()->info("SpecialPromote additional files duplicated type", ['method' => __METHOD__, 'type_duplicated' => $promoImage->getType()]);
             } else {
                 array_push($unchangedTypes, $promoImage->getType());
             }
             array_push($allFiles, $promoImage);
         } else {
             array_push($imagesToProcess, $singleFileName);
         }
     }
     $freeImageTypeSlots = array_diff(PromoImage::listAllAdditionalTypes(), $unchangedTypes);
     foreach ($imagesToProcess as $uploadedImageFileName) {
         $imageType = array_shift($freeImageTypeSlots);
         if (!empty($imageType)) {
             $promoImage = new PromoImage($imageType, $this->wg->DBname);
             $promoImage->processUploadedFile($uploadedImageFileName);
             array_push($allFiles, $promoImage);
         } else {
             WikiaLogger::instance()->info("SpecialPromote too many uploaded files", ['method' => __METHOD__]);
             break;
         }
     }
     foreach ($freeImageTypeSlots as $imageType) {
         $promoImage = new PromoImage($imageType, $this->wg->DBname);
         // attempt to delete leftover image types from current wiki
         if ($promoImage->getOriginFile(F::app()->wg->cityId)->exists()) {
             $promoImage->deleteImage();
         }
         // attempt to delete leftover image types from corporate wiki
         if ($promoImage->corporateFileByLang($this->wg->ContLanguageCode)->exists()) {
             $promoImage->deleteImageFromCorporate();
         }
     }
     return $allFiles;
 }
 public static function fileExists(PromoImage $promoImage, $cityId)
 {
     $f = GlobalFile::newFromText($promoImage->getPathname(), $cityId);
     $title = GlobalTitle::newFromText($promoImage->getPathname(), NS_FILE, $cityId);
     return $title->exists() && $f->exists();
 }
Пример #13
0
 public function corporateFileByLang($lang)
 {
     $wiki_id = (new WikiaCorporateModel())->getCorporateWikiIdByLang($lang);
     return GlobalFile::newFromText($this->getPathname(), $wiki_id);
 }
Пример #14
0
 /**
  * @param $cityId
  * @param $data
  * @return array
  */
 private function formatOutputRowHTML($cityId, $data)
 {
     $njordProps = $this->getNjordPropIds();
     $row = [];
     $row["wiki_url"] = $this->getWikiaDataById($cityId)->city_url;
     $row[self::NJORD_ARTICLE_PROP_TITLE] = $data[$njordProps[self::NJORD_ARTICLE_PROP_TITLE]];
     $row[self::NJORD_ARTICLE_PROP_DESCR] = $data[$njordProps[self::NJORD_ARTICLE_PROP_DESCR]];
     if (!empty($data[$njordProps[self::NJORD_ARTICLE_PROP_IMAGE]])) {
         $heroImageTitle = $data[$njordProps[self::NJORD_ARTICLE_PROP_IMAGE]];
         $image = GlobalFile::newFromText($heroImageTitle, $cityId);
         $row[self::NJORD_ARTICLE_PROP_IMAGE] = $image->getUrlGenerator()->original()->url();
     } else {
         $row[self::NJORD_ARTICLE_PROP_IMAGE] = null;
     }
     return $row;
 }