Changeset 865

Show
Ignore:
Timestamp:
08/31/08 06:06:06 (3 months ago)
Author:
rickc
Message:

Plugin: podcast. Start getting the rss portion of the plugin done. Still not usable.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/podcast/trunk/podcast.plugin.php

    r628 r865  
    1010class Podcast extends Plugin 
    1111{ 
     12/*  private $itunesCategory = array( 
     13        'Arts' => array( 'Design', 'Fashion & Beauty', 'Food', 'Literature', 'Performing Arts', 'Visual Arts' ), 
     14        'Business' => array( 'Business News', 'Careers', 'Investing', 'Management & Marketing', 'Shopping' ), 
     15        'Comedy', 
     16        'Education' => array( 'Education Technology', 'Higher Education', 'K-12', 'Language Courses', 'Training' ), 
     17        'Games & Hobbies' => array( 'Automotive', 'Aviation', 'Hobbies', 'Other Games', 'Video Games' ), 
     18        'Government & Organizations' => array( 'Local', 'National', 'Non-Profit', 'Regional' ), 
     19        'Health' => array( 'Alternative Health', 'Fitness & Nutrition', 'Self-Help', 'Sexuality' ), 
     20        'Kids & Family', 
     21        'Music', 
     22        'News & Politics', 
     23        'Religion & Spirituality' => array( 'Buddhism', 'Christianity', 'Hinduism', 'Islam', 'Judaism', 'Other', 'Spirituality' ), 
     24        'Science & Medicine' => array( 'Medicine', 'Natural Sciences', 'Social Sciences' ), 
     25        'Society & Culture' => array( 'History', 'Personal Journals', 'Philosophy', 'Places & Travel' ), 
     26        'Sports & Recreation' => array( 'Amateur', 'College & High School', 'Outdoor', 'Professional' ), 
     27        'Technology' => array( 'Gadgets', 'Tech News', 'Podcasting', 'Software How-To' ), 
     28        'TV & Film', 
     29    ); 
     30*/ 
    1231    /** 
    1332    * 
     
    6887    { 
    6988        if ($plugin_id == $this->plugin_id()){ 
     89            $feeds = Options::get( 'podcast__feeds' ); 
     90            if( isset( $feeds ) && count( $feeds ) > 0 ) { 
     91                $feed_obj = new ArrayObject( $feeds ); 
     92                for( $it = $feed_obj->getIterator(); $it->valid(); $it->next() ) { 
     93                    if( $action == 'feed_' .  md5(  $it->key() ) ) { 
     94                        $ui = new FormUI( 'feed' ); 
     95                        $label = sprintf( _t( 'Edit %s iTunes options' ), $it->key() ); 
     96                        $itunes = $ui->append( 'fieldset', 'itunes', $label ); 
     97                        $ui->append( 'submit', 'submit', _t( 'Submit' ) ); 
     98                        $ui->out(); 
     99                    } 
     100                } 
     101            } 
    70102            switch ($action){ 
    71103                case 'managefeeds' : 
     
    79111                    $feeddata = array_keys(Options::get('podcast__feeds')); 
    80112                    if(count($feeddata) > 0) { 
    81                         $editfeed = $ui->append('fieldset', 'editfeed', 'Manage Feeds'); 
     113                        $editfeed = $ui->append('fieldset', 'editfeed', _t( 'Manage Feeds' ) ); 
    82114                        $editfeed->append('static', 'managelabel', '<p>' . _t('Uncheck the feeds that you wish to delete.') . '</p>'); 
    83                         $feeds = $editfeed->append('checkboxes', 'feeds', 'null:null', 'Feeds'); 
     115                        $feeds = $editfeed->append('checkboxes', 'feeds', 'null:null', _t( 'Feeds' ) ); 
    84116                        $feeds->options = array_combine($feeddata, $feeddata); 
    85117                        $feeds->value = $feeddata;  
     
    143175    * 
    144176    * @param FormUI $form The publish form 
    145     * @param Post $post The string id of a plugin, generated by the system 
    146     * @return array The array of actions to attach to the specified $plugin_id 
     177    * @param Post $post  
     178    * @return array  
    147179    */ 
    148180    public function action_form_publish($form, $post) 
    149181    { 
    150         if($post->content_type == Post::type('podcast') ) { 
     182        if($post->content_type === Post::type('podcast') ) { 
    151183            $feeds = Options::get('podcast__feeds'); 
    152184            $output = ''; 
     
    163195    } 
    164196 
     197    /* 
     198    * Modify a post before it is updated 
     199    * 
     200    * Called whenever a post is about to be updated or published .  
     201    * 
     202    * @param Post $post The post being saved, by reference 
     203    * @param FormUI $form The form that was submitted on the publish page 
     204    */ 
     205    public function action_publish_post($post, $form) 
     206    { 
     207        if( $post->content_type === Post::type( 'podcast' ) ) { 
     208    Utils::debug( $form->get_values()); 
     209            $feeds = Options::get('podcast__feeds'); 
     210            foreach($feeds as $feed => $feedtype) { 
     211                $control_id = md5($feed); 
     212                $fieldname = "enclosure_{$control_id}"; 
     213                $customfield = $form->$fieldname; 
     214                $post->info->{$feed} = $customfield->value; 
     215            } 
     216        } 
     217    } 
     218 
    165219    /** 
    166220    * Add rewrite rules to map podcast feeds to this plugin 
     
    171225    public function filter_rewrite_rules( $rules ) { 
    172226        $feeds = Options::get('podcast__feeds'); 
    173         $feed_regex = implode('|', $feeds); 
     227        $feed_regex = implode('|', array_keys( $feeds ) ); 
    174228        $rules[] = new RewriteRule(array( 
    175             'name' => 'item', 
     229            'name' => 'podcast', 
    176230            'parse_regex' => '%podcast/(?P<name>' . $feed_regex . ')/(?P<feed_type>rss|atom)/?$%i', 
    177231            'build_str' => 'podcast/{$name}/{$feed_type}', 
     
    181235            'is_active' => 1, 
    182236        )); 
    183  
    184237        return $rules; 
    185238    } 
     
    210263    * @param string $feed_name The name of the feed to output 
    211264    */ 
    212     public function produce_rss($feed_name) 
    213     { 
    214         echo <<< RSS 
    215 RSS goes here. 
    216 RSS; 
     265    public function produce_rss( $feed_name ) 
     266    { 
     267        $xml = $this->create_rss_wrapper( $feed_name ); 
     268        $posts= Posts::get( array( 'status' => Post::status( 'published' ), 'content_type' => Post::type( 'podcast' ), array( 'info' => $feed_name ) ) ); 
     269        $xml = $this->add_posts( $xml, $posts, $feed_name ); 
     270        Plugins::act( 'podcast_rss_collection', $xml, $posts, $feed_name ); 
     271        ob_clean(); 
     272 
     273        header( 'Content-Type: application/xml' ); 
     274        echo $xml->asXML(); 
     275        exit; 
     276    } 
     277 
     278    /** 
     279     * Creates a basic RSS-format XML structure with channel and items elements 
     280     * @return SimpleXMLElement The requested RSS document 
     281     */ 
     282    public function create_rss_wrapper( $feed_name ) 
     283    { 
     284        $xml= new SimpleXMLElement( '<?xml version="1.0" encoding="UTF-8" ?><rss></rss>' ); 
     285        $xml->addAttribute( 'xmlns:xmlns:itunes', 'http://www.itunes.com/dtds/podcast-1.0.dtd' ); 
     286        $xml->addAttribute(  'version', '2.0' ); 
     287        $channel= $xml->addChild( 'channel' ); 
     288        $title= $channel->addChild( 'title', htmlspecialchars( Options::get('title') ) ); 
     289        $link= $channel->addChild( 'link', Site::get_url('habari') ); 
     290        if ( $tagline= Options::get( 'tagline' ) ) { 
     291            $description= $channel->addChild( 'description', htmlspecialchars( $tagline ) ); 
     292        } 
     293        $pubDate= $channel->addChild( 'lastBuildDate', date( DATE_RFC822, strtotime( Post::get()->pubdate ) ) ); 
     294        $generator= $channel->addChild( 'generator', 'Habari ' . Version::get_habariversion() . ' http://habariproject.org/' ); 
     295 
     296        $itunes_author = $channel->addChild( 'xmlns:itunes:author', 'nothing' ); 
     297        $itunes_subtitle = $channel->addChild( 'xmlns:itunes:subtitle', 'nothing' ); 
     298        $itunes_summary = $channel->addChild( 'xmlns:itunes:summary', 'nothing' ); 
     299        $itunes_owner = $channel->addChild( 'xmlns:itunes:owner' ); 
     300        $itunes_owner_name = $itunes_owner->addChild( 'xmlns:itunes:name', 'nothing' ); 
     301        $itunes_owner_email = $itunes_owner->addChild( 'xmlns:itunes:email', 'nothing' ); 
     302        $itunes_explicit = $channel->addChild( 'xmlns:itunes:explicit', 'no' ); 
     303        $itunes_image = $channel->addChild( 'xmlns:itunes:image', 'nothing' ); 
     304        $itunes_image->addAttribute( 'href', 'nothing' ); 
     305        $itunes_category = $channel->addChild( 'xmlns:itunes:category' ); 
     306        $itunes_category->addAttribute( 'text', 'nothing' ); 
     307 
     308        Plugins::act( 'podcast_create_wrapper', $xml ); 
     309        return $xml; 
     310    } 
     311 
     312    /** 
     313     * Add posts as items in the provided xml structure 
     314     * @param SimpleXMLElement $xml The document to add to 
     315     * @param array $posts An array of Posts to add to the XML 
     316     * @return SimpleXMLElement The resultant XML with added posts 
     317     */ 
     318    public function add_posts($xml, $posts, $feed_name ) 
     319    { 
     320        $items = $xml->channel; 
     321        foreach ( $posts as $post ) { 
     322            if ($post instanceof Post) { 
     323                $item= $items->addChild( 'item' ); 
     324                $title= $item->addChild( 'title', htmlspecialchars( $post->title ) ); 
     325                $link= $item->addChild( 'link', $post->permalink ); 
     326                $description= $item->addChild( 'description', htmlspecialchars( $post->content ) ); 
     327                $pubdate= $item->addChild ( 'pubDate', date( DATE_RFC822, strtotime( $post->pubdate ) ) ); 
     328                $guid= $item->addChild( 'guid', $post->guid ); 
     329                $guid->addAttribute( 'isPermaLink', 'false' ); 
     330                $enclosure = $item->addChild( 'enclosure' ); 
     331                $enclosure->addAttribute( 'url', $post->info->feed ); 
     332                $enclosure->addAttribute( 'length', filesize( $post->info->feed ) ); 
     333                $enclosure->addAttribute( 'type', 'audio/mpeg' ); 
     334                $category = $item->addChild( 'Category', '' ); 
     335 
     336                $itunes_author = $item->addChild( 'xmlns:itunes:author', $post->author->displayname ); 
     337                $itunes_explicit = $item->addChild( 'xmlns:itunes:explicit', 'Clean' ); 
     338                $itunes_subtitle = $item->addChild( 'xmlns:itunes:subtitle', '' ); 
     339                $itunes_summary = $item->addChild( 'xmlns:itunes:summary', '' ); 
     340                $itunes_duration = $item->addChild( 'xmlns:itunes:duration', '' ); 
     341                $itunes_keywords = $item->addChild( 'xmlns:itunes:keywords', '' ); 
     342 
     343                Plugins::act( 'podcast_add_post', $item, $post ); 
     344            } 
     345        } 
     346        return $xml; 
    217347    } 
    218348