static function parseFeed($url, $omitDB = false) { global $wgOut; //reset array self::$data = array('feedTitle' => '', 'images' => array()); if (!$omitDB) { //check DB for this URL - we might have it already (background task will refresh it) $data = self::getDataByUrl($url); } if ($omitDB || is_null($data)) { //no data in DB - fetch from feed $itemCount = 0; $rssContent = Http::get($url); $feed = new SimplePie(); $feed->set_raw_data($rssContent); $feed->init(); self::$data['feedTitle'] = $feed->get_title(); foreach ($feed->get_items() as $item) { $enclosures = $item->get_enclosures(); $enclosuresFound = false; //we have enclosures - use them instead of content of feed (usually there are bigger image while content uses thumbnails) if (!is_null($enclosures)) { foreach ($enclosures as $enclosure) { $type = $enclosure->get_type(); if (!empty($type) && substr($type, 0, 6) === 'image/') { self::$data['images'][] = array('src' => $enclosure->get_link(), 'caption' => $item->get_title(), 'link' => $item->get_link()); $enclosuresFound = true; break; //one image per feed } } } //if enclosures has not been found or doesn't contain any images - use regular method if (!$enclosuresFound) { //look for <img /> tags $description = $item->get_description(); preg_match('/<img .*?src=([\'"])(.*?\\.(?:jpg|jpeg|gif|png))\\1/', $description, $matches); if (!empty($matches[2])) { self::$data['images'][] = array('src' => $matches[2], 'caption' => $item->get_title(), 'link' => $item->get_link()); } } if (count(self::$data['images']) >= 20 || ++$itemCount > 50) { break; //take up to 20 images, from up to 50 articles. } } //store data in DB only if valid rss (images found) if (count(self::$data['images'])) { self::setData($url, self::$data); } } else { self::$data = $data; } return self::$data; }
/** * 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; }
<?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"; }
/** * 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__); }