<?php

/**
 * WikiaPhotoGalleryFeedUpdate
 *
 * @package MediaWiki
 * @subpackage Maintanance
 *
 * @author: Marooned <marooned at wikia-inc.com>
 *
 */
ini_set("include_path", dirname(__FILE__) . "/../");
require "commandLine.inc";
if (isset($options['help'])) {
    die("Usage: php WikiaPhotoGalleryFeedUpdate.php [--quiet]\n\n\t\t  --help     you are reading it right now\n\t\t  --quiet    do not print anything to output\n\n");
}
global $wgExternalDatawareDB;
$timestemp = wfTimestamp(TS_DB, time() - 24 * 60 * 60);
$dbr = wfGetDB(DB_SLAVE, array(), $wgExternalDatawareDB);
$oRes = $dbr->select('photo_gallery_feeds', array('url'), array("timestamp < '{$timestemp}'"), __METHOD__);
$count = 0;
while ($oRow = $dbr->fetchObject($oRes)) {
    if (!isset($options['quiet'])) {
        echo "Updating feed: {$oRow->url}\n";
    }
    $images = WikiaPhotoGalleryRSS::parseFeed($oRow->url, true);
    $count++;
}
if (!isset($options['quiet'])) {
    echo "Updated {$count} feeds.\n";
}
 /**
  * Render slideshow preview
  *
  * @author Marooned
  */
 public static function renderFeedSlideshowPreview($slideshow)
 {
     wfProfileIn(__METHOD__);
     $data = WikiaPhotoGalleryRSS::parseFeed($slideshow['params']['rssfeed']);
     //use images from feed
     $slideshow['images'] = $data['images'];
     // handle "crop" attribute
     $crop = isset($slideshow['params']['crop']) ? $slideshow['params']['crop'] == 'true' : false;
     // render thumbnail
     $maxWidth = isset($slideshow['params']['widths']) && is_numeric($slideshow['params']['widths']) ? $slideshow['params']['widths'] : 300;
     $maxHeight = round($maxWidth * 3 / 4);
     // render slideshow images
     foreach ($slideshow['images'] as &$image) {
         preg_match('%(?:' . wfUrlProtocols() . ')([^/]+)%i', $image['link'], $match);
         $image['caption'] = wfMsg('wikiaPhotoGallery-feed-caption', $image['caption'], $image['link'], $match[1]);
         $image['image'] = $image['src'];
     }
     // render gallery HTML preview
     $template = new EasyTemplate(dirname(__FILE__) . '/templates');
     $template->set_vars(array('fromFeed' => true, 'height' => $maxHeight, 'slideshow' => $slideshow, 'width' => $maxWidth));
     $html = $template->render('slideshowPreview');
     wfProfileOut(__METHOD__);
     return $html;
 }
 /**
  * Parse content of <gallery> tag (add images with captions and links provided)
  */
 public function parse(&$parser = null)
 {
     wfProfileIn(__METHOD__);
     //use images passed inside <gallery> tag
     $lines = StringUtils::explode("\n", $this->mText);
     foreach ($lines as $line) {
         if ($line == '') {
             continue;
         }
         $parts = (array) StringUtils::explode('|', $line);
         // get name of an image from current line and remove it from list of params
         $imageName = array_shift($parts);
         if (strpos($line, '%') !== false) {
             $imageName = urldecode($imageName);
         }
         // Allow <gallery> to accept image names without an Image: prefix
         $tp = Title::newFromText($imageName, NS_FILE);
         $nt =& $tp;
         if (is_null($nt)) {
             // Bogus title. Ignore these so we don't bomb out later.
             continue;
         }
         // search for caption and link= param
         $captionParts = array();
         $link = $linktext = $shorttext = '';
         foreach ($parts as $part) {
             if (substr($part, 0, 5) == 'link=') {
                 $link = substr($part, 5);
             } else {
                 if (substr($part, 0, 9) == 'linktext=') {
                     $linktext = substr($part, 9);
                 } else {
                     if (substr($part, 0, 10) == 'shorttext=') {
                         $shorttext = substr($part, 10);
                     } else {
                         $captionParts[] = trim($part);
                     }
                 }
             }
         }
         // support captions with internal links with pipe (Foo.jpg|link=Bar|[[test|link]])
         $caption = implode('|', $captionParts);
         $imageItem = array('name' => $imageName, 'caption' => $caption, 'link' => $link, 'linktext' => $linktext, 'shorttext' => $shorttext, 'data-caption' => htmlspecialchars($caption));
         // store list of images from inner content of tag (to be used by front-end)
         $this->mData['images'][] = $imageItem;
         // store list of images actually shown (to be used by front-end)
         $this->mData['imagesShown'][] = $imageItem;
         // use global instance of parser (RT #44689 / RT #44712)
         $caption = $this->mParser->recursiveTagParse($caption);
         $this->add($nt, $caption, $link);
         // Only add real images (bug #5586)
         if ($nt->getNamespace() == NS_FILE) {
             $this->mParser->mOutput->addImage($nt->getDBkey());
         }
     }
     // support "showrecentuploads" attribute (add 20 recently uploaded images at the end of slideshow)
     if (!empty($this->mShowRecentUploads)) {
         $this->addRecentlyUploaded(self::RECENT_UPLOADS_IMAGES);
     }
     if (!empty($this->mFeedURL)) {
         $data = WikiaPhotoGalleryRSS::parseFeed($this->mFeedURL);
         //title of the feed - used by Lightbox
         $this->mData['feedTitle'] = $data['feedTitle'];
         //use images from feed
         $this->mExternalImages = $data['images'];
         // store list of images from inner content of tag (to be used by front-end)
         $this->mData['externalImages'] = $this->mExternalImages;
         // store list of images actually shown (to be used by front-end)
         $this->mData['imagesShown'] = $this->mExternalImages;
     }
     // store ID of gallery
     if (empty($this->mData['id'])) {
         $this->mData['id'] = self::$galleriesCounter++;
     }
     if (!empty($parser)) {
         $this->recordParserOption($parser);
     }
     wfProfileOut(__METHOD__);
 }