Development

#2551: sfFeed2Plugin_with_image.patch

You must first sign up to be able to contribute.

Ticket #2551: sfFeed2Plugin_with_image.patch

File sfFeed2Plugin_with_image.patch, 17.2 kB (added by FabianLange, 10 months ago)
  • lib/sfAtom1Feed.class.php

    old new  
    7070    $this->setAuthorName((string) $feedXml->author->name); 
    7171    $this->setAuthorEmail((string) $feedXml->author->email); 
    7272    $this->setAuthorLink((string) $feedXml->author->uri); 
     73 
     74    $image = new sfFeedImage(array( 
     75      "favicon"  => (string)$feedXml->icon, 
     76      "image"    => (string)$feedXml->logo, 
     77    )); 
     78    $this->setImage($image); 
     79 
    7380    $categories = array(); 
    7481    foreach($feedXml->category as $category) 
    7582    { 
     
    200207      $xml[] = '  <subtitle>'.$this->getSubtitle().'</subtitle>'; 
    201208    } 
    202209 
     210    if ($this->getImage()) 
     211    { 
     212      $xml[] = '  <icon>'.$this->getImage()->getFavicon().'</icon>'; 
     213      $xml[] = '  <logo>'.$this->getImage()->getImage().'</logo>'; 
     214    } 
     215 
    203216    if(is_array($this->getCategories())) 
    204217    { 
    205218      foreach ($this->getCategories() as $category) 
  • lib/sfFeed.class.php

    old new  
    2222{ 
    2323  protected 
    2424    $items = array(), 
     25    $image, 
    2526    $title, 
    2627    $link, 
    2728    $description, 
     
    5253  public function initialize($feed_array) 
    5354  { 
    5455    $this->setItems(isset($feed_array['items']) ? $feed_array['items'] : ''); 
     56    $this->setImage(isset($feed_array['image']) ? $feed_array['image'] : ''); 
    5557    $this->setTitle(isset($feed_array['title']) ? $feed_array['title'] : ''); 
    5658    $this->setLink(isset($feed_array['link']) ? $feed_array['link'] : ''); 
    5759    $this->setDescription(isset($feed_array['description']) ? $feed_array['description'] : ''); 
     
    164166    { 
    165167      $this->items = array_slice($this->items, 0, $count); 
    166168    } 
     169    return $this; 
     170  } 
    167171 
     172  /** 
     173   * Retrieves the feed image 
     174   * 
     175   * @return sfFeedImage actual sfFeedImage object 
     176   */ 
     177  public function getImage() 
     178  { 
     179    return $this->image; 
     180  } 
     181 
     182  /** 
     183   * Defines the image/icon of the feed 
     184   * 
     185   * @param image sfFeedImage object 
     186   * 
     187   * @return sfFeed the current sfFeed object 
     188   */ 
     189  public function setImage($image) 
     190  { 
     191    $this->image = $image; 
     192     
    168193    return $this; 
    169194  } 
    170195 
    171196  public function setTitle ($title) 
    172197  { 
    173198    $this->title = $title; 
     199    //if an image is there that has no title yet set it as well 
     200    if ($this->image instanceof sfFeedImage && !$this->image->getTitle()) 
     201    { 
     202      $this->image->setTitle($title); 
     203    } 
    174204  } 
    175205 
    176206  public function getTitle () 
     
    181211  public function setLink ($link) 
    182212  { 
    183213    $this->link = $link; 
     214    //if an image is there that has no link yet set it as well 
     215    if ($this->image instanceof sfFeedImage && !$this->image->getLink()) 
     216    { 
     217      $this->image->setLink($link); 
     218    } 
    184219  } 
    185220 
    186221  public function getLink () 
  • lib/sfFeedImage.class.php

    old new  
     1<?php 
     2 
     3/* 
     4 * This file is part of the sfFeed2 package. 
     5 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> 
     6 * (c) 2004-2007 Francois Zaninotto <francois.zaninotto@symfony-project.com> 
     7 *  
     8 * For the full copyright and license information, please view the LICENSE 
     9 * file that was distributed with this source code. 
     10 */ 
     11 
     12/** 
     13 * 
     14 * Adds support for icons/images for atom and rss feeds in an unified extensible way. 
     15 * Both (rss and atom) specs are quite limited, but this class should cope with possible enhancements. 
     16 * Note that not everything is used. Atom at the moment only takes favicon (1:1 ratio) and 
     17 * image (approx 2:1 ratio) image urls. 
     18 * Rss only takes the image, but expects additionally the size of that image. 
     19 * 
     20 * @package    sfFeed2 
     21 * @author     Fabian Lange <fabian.lange@web.de> 
     22 */ 
     23class sfFeedImage 
     24{ 
     25  private 
     26   $favicon, 
     27   $image, 
     28   $faviconX, 
     29   $faviconY, 
     30   $imageX, 
     31   $imageY, 
     32   $title, 
     33   $link, 
     34   $feed; 
     35 
     36  public function __construct($item_array = array()) 
     37  { 
     38    if($item_array) 
     39    { 
     40      $this->initialize($item_array);  
     41    } 
     42  } 
     43   
     44  /** 
     45   * Sets the feed image parameters, based on an associative array 
     46   * 
     47   * @param array an associative array 
     48   * 
     49   * @return sfFeedImage the current sfFeedImage object 
     50   */ 
     51  public function initialize($item_array) 
     52  { 
     53    $this->setFavicon(isset($item_array['favicon']) ? $item_array['favicon'] : ''); 
     54    $this->setImage(isset($item_array['image']) ? $item_array['image'] : ''); 
     55    $this->setFaviconX(isset($item_array['faviconX']) ? $item_array['faviconX'] : ''); 
     56    $this->setFaviconY(isset($item_array['faviconY']) ? $item_array['faviconY'] : ''); 
     57    $this->setImageX(isset($item_array['imageX']) ? $item_array['imageX'] : ''); 
     58    $this->setImageY(isset($item_array['imageY']) ? $item_array['imageY'] : ''); 
     59    $this->setTitle(isset($item_array['title']) ? $item_array['title'] : ''); 
     60    $this->setLink(isset($item_array['link']) ? $item_array['link'] : ''); 
     61    $this->setFeed(isset($item_array['feed']) ? $item_array['feed'] : ''); 
     62 
     63    return $this; 
     64  } 
     65 
     66  public function setFavicon ($favicon) 
     67  { 
     68    $this->favicon = $favicon; 
     69  } 
     70 
     71  public function getFavicon () 
     72  { 
     73    return $this->favicon; 
     74  } 
     75 
     76  public function setImage ($image) 
     77  { 
     78    $this->image = $image; 
     79  } 
     80 
     81  public function getImage () 
     82  { 
     83    return $this->image; 
     84  } 
     85 
     86  public function setFaviconX ($faviconX) 
     87  { 
     88    $this->faviconX = $faviconX; 
     89  } 
     90 
     91  public function getFaviconX () 
     92  { 
     93    return $this->faviconX; 
     94  } 
     95 
     96  public function setFaviconY ($faviconY) 
     97  { 
     98    $this->faviconY = $faviconY; 
     99  } 
     100 
     101  public function getFaviconY () 
     102  { 
     103    return $this->faviconY; 
     104  } 
     105 
     106  public function setImageX ($imageX) 
     107  { 
     108    $this->imageX = $imageX; 
     109  } 
     110 
     111  public function getImageX () 
     112  { 
     113    return $this->imageX; 
     114  } 
     115 
     116  public function setImageY ($imageY) 
     117  { 
     118    $this->imageY = $imageY; 
     119  } 
     120 
     121  public function getImageY () 
     122  { 
     123    return $this->imageY; 
     124  } 
     125 
     126  public function setTitle ($title) 
     127  { 
     128    $this->title = $title; 
     129  } 
     130 
     131  public function getTitle () 
     132  { 
     133    return $this->title; 
     134  } 
     135 
     136  public function setLink ($link) 
     137  { 
     138    $this->link = $link; 
     139  } 
     140 
     141  public function getLink () 
     142  { 
     143    return $this->link; 
     144  } 
     145 
     146  public function setFeed ($feed) 
     147  { 
     148    $this->feed = $feed; 
     149  } 
     150 
     151  public function getFeed () 
     152  { 
     153    return $this->feed; 
     154  } 
     155 
     156} 
  • lib/sfRssFeed.class.php

    old new  
    7171    $this->setDescription((string) $feedXml->channel[0]->description); 
    7272    $this->setLanguage((string) $feedXml->channel[0]->language); 
    7373 
     74    if ($feedXml->channel[0]->image) 
     75    { 
     76      $image = new sfFeedImage(array( 
     77        "image"  => (string)$feedXml->channel[0]->image->url, 
     78        "imageX" => (int)$feedXml->channel[0]->image->width, 
     79        "imageY" => (int)$feedXml->channel[0]->image->height, 
     80        "link"   => (string)$feedXml->channel[0]->image->link, 
     81        "title"  => (string)$feedXml->channel[0]->image->title 
     82      )); 
     83      $this->setImage($image); 
     84    } 
     85 
    7486    $categories = array(); 
    7587    foreach($feedXml->channel[0]->category as $category) 
    7688    { 
     
    189201    { 
    190202      $xml[] = '    <language>'.$this->getLanguage().'</language>'; 
    191203    } 
     204    if ($this->getImage()) 
     205    { 
     206      $xml[] = '    <image>'; 
     207      $xml[] = '      <url>'.$this->getImage()->getImage().'</url>'; 
     208      $xml[] = '      <title>'.$this->getImage()->getTitle().'</title>'; 
     209      $xml[] = '      <link>'.$this->context->getController()->genUrl($this->getImage()->getLink(), true).'</link>'; 
     210      $xml[] = '      <width>'.$this->getImage()->getImageX().'</width>'; 
     211      $xml[] = '      <height>'.$this->getImage()->getImageY().'</height>'; 
     212      $xml[] = '    </image>'; 
     213    } 
    192214    if(strpos($this->version, '2.') !== false) 
    193215    { 
    194216      if(is_array($this->getCategories())) 
  • README

    old new  
    1212 
    1313== Contents == 
    1414 
    15 This plugin contains three data structure classes: 
     15This plugin contains four data structure classes: 
    1616 
    1717 * `sfFeed` 
    1818 * `sfFeedItem` 
     19 * `sfFeedImage` 
    1920 * `sfFeedEnclosure` 
    2021 
    2122It also contains specific classes containing specific input/output methods based on specific feed formats: 
     
    122123    <author_email>pclive@myblog.com</author_email> 
    123124  </author> 
    124125  <id>4543D55FF756G734</id> 
    125  
     126  <icon>http://www.myblog.com/favicon.ico</icon> 
     127  
    126128  <entry> 
    127129    <title>I love mice</title> 
    128130    <link href="http://www.myblog.com/permalink/i-love-mice" /> 
     
    164166  $feed->setAuthorEmail('pclive@myblog.com'); 
    165167  $feed->setAuthorName('Peter Clive'); 
    166168 
     169  $feedImage = new sfFeedImage(); 
     170  $feedImage->setFavicon('http://www.myblog.com/favicon.ico'); 
     171  $feed->setImage($feedImage); 
     172 
    167173  $c = new Criteria; 
    168174  $c->addDescendingOrderByColumn(PostPeer::CREATED_AT); 
    169175  $c->setLimit(5); 
     
    446452== Trunk == 
    447453 
    448454 * Pascal.Borreli : symfony coding practices : removed lib closing tag (#2657) 
     455 * Fabian Lange : added image capabilities (#2551) 
    449456 
    450457=== 2007-04-15 | 0.9.4 Beta === 
    451458 
  • test/unit/sfAtom1FeedTest.php

    old new  
    5050  'pubDate' => '123456', 
    5151); 
    5252 
     53$image_params = array( 
     54  'title' => 'symfony project', 
     55  'link' => 'http://www.symfony-project.org', 
     56  'favicon' => 'http://www.symfony-project.org/favicon.ico',  
     57  'image' => 'http://www.symfony-project.org/images/symfony_logo.gif', 
     58  'faviconX' => '16', 
     59  'faviconY' => '16', 
     60  'imageX' => '176', 
     61  'imageY' => '37', 
     62); 
     63 
    5364$feed = new sfAtom1Feed(); 
    5465$feed->initialize($feedParams); 
    5566$feedItem = new sfFeedItem(); 
     
    5869$feedItem2 = new sfFeedItem(); 
    5970$feedItem2->initialize($item2Params); 
    6071$feed->addItem($feedItem2); 
     72$feedImage = new sfFeedImage(); 
     73$feedImage->initialize($image_params); 
     74$feed->setImage($feedImage); 
    6175 
    62 $t = new lime_test(52, new lime_output_color()); 
     76$t = new lime_test(56, new lime_output_color()); 
    6377 
    6478$t->diag('toXML() - generated feed'); 
    6579$feedString = $feed->toXml(); 
     
    8195$t->is((string) $feedXml->subtitle, $feedParams['subtitle'], '<subtitle> contains the proper subtitle'); 
    8296$t->is((string) $feedXml->category[0]['term'], $feedParams['categories'][0], '<category> contains the feed category'); 
    8397$t->is((string) $feedXml->category[1]['term'], $feedParams['categories'][1], '<category> contains the feed category'); 
     98$t->is((string) $feedXml->icon, $image_params['favicon'], '<favicon> contains the proper favicon'); 
     99$t->is((string) $feedXml->logo, $image_params['image'], '<logo> contains the proper logo'); 
    84100 
    85101$t->diag('toXML() - generated feed items'); 
    86102$t->is((string) $feedXml->entry[0]->title, $itemParams['title'], '<entry><title> contains the item title'); 
     
    116132$t->is($generatedFeed->getCategories(), $feed->getCategories(), 'The categories property is properly set'); 
    117133$t->is($generatedFeed->getFeedUrl(), $feed->getFeedUrl(), 'The feedUrl property is properly set'); 
    118134$t->is($generatedFeed->getEncoding(), $feed->getEncoding(), 'The encoding property is properly set'); 
     135$t->is($generatedFeed->getImage()->getFavicon(), $feed->getImage()->getFavicon(), 'The feed favicon property is properly set'); 
     136$t->is($generatedFeed->getImage()->getImage(), $feed->getImage()->getImage(), 'The feed logo property is properly set'); 
    119137 
    120138$t->diag('fromXML() - generated feed items'); 
    121139$items = $generatedFeed->getItems(); 
  • test/unit/sfFeedImageTest.php

    old new  
     1<?php 
     2 
     3define('SF_ROOT_DIR', realpath(dirname(__FILE__).'/../../../..')); 
     4define('SF_APP',      'frontend'); 
     5include(dirname(__FILE__).'/../../../../test/bootstrap/functional.php'); 
     6 
     7$b = new sfTestBrowser(); 
     8$b->initialize(); 
     9 
     10$t = new lime_test(9, new lime_output_color()); 
     11 
     12$image_params = array( 
     13  'title' => 'symfony project', 
     14  'link' => 'http://www.symfony-project.org', 
     15  'favicon' => 'http://www.symfony-project.org/favicon.ico',  
     16  'image' => 'http://www.symfony-project.org/images/symfony_logo.gif', 
     17  'faviconX' => '16', 
     18  'faviconY' => '16', 
     19  'imageX' => '176', 
     20  'imageY' => '37', 
     21); 
     22 
     23$item = new sfFeedImage(); 
     24$t->isa_ok($item->initialize($image_params), 'sfFeedImage', 'initialize() returns the current feed image object'); 
     25$t->is($item->getTitle(), $image_params['title'], 'getTitle() gets the feed image title'); 
     26$t->is($item->getLink(), $image_params['link'], 'getLink() gets the feed image link'); 
     27$t->is($item->getFavicon(), $image_params['favicon'], 'getFavicon() gets the feed favicon url'); 
     28$t->is($item->getImage(), $image_params['image'], 'getImage() gets the feed image url'); 
     29$t->is($item->getFaviconX(), $image_params['faviconX'], 'getFaviconX() gets the feed favicon x size'); 
     30$t->is($item->getFaviconY(), $image_params['faviconY'], 'getFaviconY() gets the feed favicon y size'); 
     31$t->is($item->getImageX(), $image_params['imageX'], 'getImageX() gets the feed image x size'); 
     32$t->is($item->getImageY(), $image_params['imageY'], 'getImageY() gets the feed image y size'); 
     33 
  • test/unit/sfRssFeedTest.php

    old new  
    5151  'authorEmail' => 'fooitem2@bar.com', 
    5252); 
    5353 
     54$image_params = array( 
     55  'title' => 'symfony project', 
     56  'link' => 'http://www.symfony-project.org', 
     57  'favicon' => 'http://www.symfony-project.org/favicon.ico',  
     58  'image' => 'http://www.symfony-project.org/images/symfony_logo.gif', 
     59  'faviconX' => '16', 
     60  'faviconY' => '16', 
     61  'imageX' => '176', 
     62  'imageY' => '37', 
     63); 
     64 
    5465$feed = new sfRssFeed(); 
    5566$feed->initialize($feedParams); 
    5667$feedItem = new sfFeedItem(); 
     
    5970$feedItem2 = new sfFeedItem(); 
    6071$feedItem2->initialize($item2Params); 
    6172$feed->addItem($feedItem2); 
     73$feedImage = new sfFeedImage(); 
     74$feedImage->initialize($image_params); 
     75$feed->setImage($feedImage); 
    6276 
    63 $t = new lime_test(52, new lime_output_color()); 
     77$t = new lime_test(62, new lime_output_color()); 
    6478 
    6579$t->diag('toXML() - generated feed'); 
    6680$feedString = $feed->toXml(); 
     
    7690$t->is((string) $feedXml->channel[0]->managingEditor, $feedParams['authorEmail'].' ('.$feedParams['authorName'].')', '<managingEditor> contains the author email and name'); 
    7791$t->is((string) $feedXml->channel[0]->pubDate, strftime('%Y-%m-%dT%H:%M:%SZ', $item2Params['pubDate']), '<pubDate> contains the latest publication date of all feed items'); 
    7892$t->is_deeply(array((string) $feedXml->channel[0]->category[0], (string) $feedXml->channel[0]->category[1]), $feedParams['categories'], '<category> contains the correct categories'); 
     93$t->is((string) $feedXml->channel[0]->image->url, $image_params['image'], '<image><url> contains the proper image'); 
     94$t->is((string) $feedXml->channel[0]->image->width, $image_params['imageX'], '<image><width> contains the proper image x'); 
     95$t->is((string) $feedXml->channel[0]->image->height, $image_params['imageY'], '<image><height> contains the proper image y'); 
     96$t->is((string) $feedXml->channel[0]->image->link, $image_params['link'], '<image><link> contains the proper image link'); 
     97$t->is((string) $feedXml->channel[0]->image->title, $image_params['title'], '<image><title> contains the proper image title'); 
    7998 
    8099$t->diag('toXML() - generated feed items'); 
    81100$t->is((string) $feedXml->channel[0]->item[0]->title, $itemParams['title'], '<item><title> contains the item title'); 
     
    114133$t->is($generatedFeed->getCategories(), $feed->getCategories(), 'The categories property is properly set'); 
    115134$t->isnt($generatedFeed->getFeedUrl(), $feed->getFeedUrl(), 'The feedUrl property cannot be set from a RSS feed'); 
    116135$t->is($generatedFeed->getEncoding(), $feed->getEncoding(), 'The encoding property is properly set'); 
     136$t->is($generatedFeed->getImage()->getImage(), $feed->getImage()->getImage(), 'The feed image url is correctly set'); 
     137$t->is($generatedFeed->getImage()->getImageX(), $feed->getImage()->getImageX(), 'The feed image x is correctly set'); 
     138$t->is($generatedFeed->getImage()->getImageY(), $feed->getImage()->getImageY(), 'The feed image y is correctly set'); 
     139$t->is($generatedFeed->getImage()->getLink(), $feed->getImage()->getLink(), 'The feed image link is correctly set'); 
     140$t->is($generatedFeed->getImage()->getTitle(), $feed->getImage()->getTitle(), 'The feed image title is correctly set'); 
    117141 
    118142$t->diag('fromXML() - generated feed items'); 
    119143$items = $generatedFeed->getItems();