Changeset 2293

Show
Ignore:
Timestamp:
12/11/10 07:23:51 (18 months ago)
Author:
petsagouris
Message:

[Branch 1.5] Splitted the RSSWriter class into RSSWriter and RSSItem.
Need to test this one.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/1.5/libs/extensions/RSSWriterClass/rsswriter.php

    r2235 r2293  
    11<?php 
     2/** 
     3 * RSS class 
     4 * 
     5 * @author George Petsagourakis <petsagouris@gmail.com> 
     6 * 
     7 * @package HotaruCMS 
     8 * @subpackage RSS 
     9 */ 
    210 
    311/** 
    412 * Publishes content as an RSS feed. 
     13 * 
    514 * @url: http://snipplr.com/view/23/rss-writer-class/ 
    615 * @uses RSSItem 
    7  * @example 
    8  * <code> 
     16 * @example <code> 
    917 * $feed = new RSS(); 
    1018 * $feed->title = "RSS Feed Title"; 
     
    2735class RSS { 
    2836 
     37        /** 
     38         * The feed's title 
     39         * 
     40         * @var string 
     41         */ 
    2942        public $title; 
     43        /** 
     44         * The feed's URL 
     45         *  
     46         * @var string 
     47         */ 
    3048        public $rss_link; 
     49        /** 
     50         * The feed's atom link (for caching) 
     51         * 
     52         * @var string 
     53         */ 
    3154        public $link; 
     55        /** 
     56         * The feed's description text 
     57         * 
     58         * @var string 
     59         */ 
    3260        public $description; 
     61        /** 
     62         * The feed's language identifier 
     63         * 
     64         * @var string 
     65         */ 
    3366        public $language = "en-us"; 
     67        /** 
     68         * The feed's publication date 
     69         * 
     70         * @var string 
     71         */ 
    3472        public $pubDate; 
     73        /** 
     74         * The feed's items (an array of RSSItem objects) 
     75         * 
     76         * @var array 
     77         */ 
    3578        public $items = array(); 
     79        /** 
     80         * Additional tag entries for the parent tag. 
     81         * 
     82         * @var array 
     83         */ 
    3684        public $tags = array(); 
    3785 
     86        /** 
     87         * The constructor set's up the object with an initial url of the feed. 
     88         * 
     89         * @param string $rss_link the link of the feed 
     90         */ 
    3891        public function __construct($rss_link) 
    3992        { 
     
    4194        } 
    4295 
     96        /** 
     97         * Adds RSSItem objects to the RSS::items array 
     98         * 
     99         * @param array $item An array that will be used in constructing the RSSItem object 
     100         */ 
    43101        public function addItem($item) 
    44102        { 
    45103                if (is_array($item)) { 
    46104                        foreach ($item as $i) { 
    47                                 array_push($this->items, new RSSItem($i)); 
     105                                $this->items[] = new RSSItem($i); 
    48106                        } 
    49107                } 
     
    51109        } 
    52110 
     111        /** 
     112         * Publication date setter for the feed 
     113         * 
     114         * @param string $when  
     115         */ 
    53116        public function setPubDate($when) 
    54117        { 
     
    56119        } 
    57120 
     121        /** 
     122         * 
     123         * @param <type> $tag 
     124         * @param <type> $value 
     125         */ 
    58126        public function addTag($tag, $value) 
    59127        { 
     
    98166 
    99167} 
    100  
    101 /** 
    102  * Represents an item on the RSS feed. 
    103  * @see RSS 
    104  */ 
    105 class RSSItem { 
    106  
    107         public $title; 
    108         public $link; 
    109         public $description; 
    110         public $pubDate; 
    111         public $guid; 
    112         public $tags = array(); 
    113         public $attachment; 
    114         public $length; 
    115         public $mimetype; 
    116  
    117         public function __construct($options) 
    118         { 
    119                 if (isset($options['title'])) { 
    120                         $this->title = stripslashes(html_entity_decode(urldecode($options['title']), ENT_QUOTES, 'UTF-8')); 
    121                 } 
    122  
    123                 if (isset($options['link'])) { 
    124                         $this->link = html_entity_decode($options['link'], ENT_QUOTES, 'UTF-8'); 
    125                 } 
    126  
    127                 if (isset($options['date'])) { 
    128                         $this->pubDate = date("D, d M Y H:i:s O", ((strtotime($options['date'])) ? strtotime($options['date']) : $options['date'])); 
    129                 } 
    130  
    131                 if (isset($options['description'])) { 
    132                         $this->description = "<![CDATA[ ".stripslashes(urldecode($options['description']))." ]]>"; 
    133                 } 
    134  
    135                 if (isset($options['enclosure'])) { 
    136                         $this->attachment = $options['enclosure']['url']; 
    137                         $this->mimetype = $options['enclosure']['type']; 
    138                         $this->length = $options['enclosure']['length']; 
    139                 } 
    140  
    141                 if (isset($options['author'])) { 
    142                         $this->tags['author'] = $options['author']; 
    143                 } 
    144         } 
    145  
    146         public function out() 
    147         { 
    148                 $out = "<item>\n"; 
    149                 $out .= "<title>".$this->title."</title>\n"; 
    150                 $out .= "<link>".$this->link."</link>\n"; 
    151                 $out .= "<description>".$this->description."</description>\n"; 
    152                 $out .= "<pubDate>".((empty($this->pubDate)) ? date("D, d M Y H:i:s O") : $this->pubDate)."</pubDate>\n"; 
    153  
    154                 if (!empty($this->attachment)) { 
    155                         $out .= "<enclosure url='{$this->attachment}' length='{$this->length}' type='{$this->mimetype}' />"; 
    156                 } 
    157  
    158                 $this->guid = (empty($this->guid)) ? $this->link : $this->guid; 
    159  
    160                 $out .= "<guid>".$this->guid."</guid>\n"; 
    161  
    162                 foreach ($this->tags as $key => $val) { 
    163                         $out .= "<$key>$val</$key\n>"; 
    164                 } 
    165  
    166                 return $out."</item>\n"; 
    167         } 
    168  
    169 }