Changeset 2453

Show
Ignore:
Timestamp:
09/06/08 22:57:09 (3 months ago)
Author:
dmondark
Message:

090508-dom: First pass of porting AtomHandler to DOM. create_atom_wrapper, add_posts and get_collection methods now uses DOM to generate the feed so the main entries feed now works, other feeds are broken so far, obviously.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/090508-dom/system/classes/atomhandler.php

    r2442 r2453  
    8181        $namespaces= array( 'default' => 'http://www.w3.org/2005/Atom' ); 
    8282        $namespaces= Plugins::filter( 'atom_get_collection_namespaces', $namespaces ); 
    83         $namespaces= array_map( create_function( '$value,$key', 'return ( ( $key == "default" ) ? "xmlns" : "xmlns:" . $key ) . "=\"" . $value ."\"";' ), $namespaces, array_keys($namespaces) ); 
    84         $namespaces= implode( ' ', $namespaces ); 
    85  
    86         $xml= new SimpleXMLElement( '<feed ' . $namespaces . '></feed>' ); 
    87  
    88         $feed_title= $xml->addChild( 'title', htmlspecialchars( Options::get( 'title' ) ) ); 
     83        $namespaces=array_combine( array_map( create_function( '$key', 'return ( ( $key == "default" ) ? "xmlns" : "xmlns:" . $key ) ;' ), array_keys( $namespaces ) ) ,array_values($namespaces) ); 
     84 
     85        $xml= new DOMDocument( '1.0' ); 
     86        $feed = $xml->appendChild( $xml->createElement('feed') ); 
     87         
     88        foreach ( $namespaces as $namespace=>$uri ){ 
     89            $feed->setAttribute( $namespace, $uri ); 
     90        } 
     91 
     92        $feed_title= $feed->appendChild( $xml->createElement( 'title' ) ); 
     93        $feed_title->appendChild( $xml->createTextNode( Options::get( 'title' ) ) ); 
    8994 
    9095        if ( $tagline= Options::get( 'tagline' ) ) { 
    91             $feed_subtitle= $xml->addChild( 'subtitle', htmlspecialchars( $tagline ) ); 
    92         } 
    93  
    94         $feed_updated= $xml->addChild( 'updated', date( 'c', time() ) ); 
    95  
    96         $feed_link= $xml->addChild( 'link' ); 
    97         $feed_link->addAttribute( 'rel', 'alternate' ); 
    98         $feed_link->addAttribute( 'href', $alternate ); 
    99  
    100         $feed_link= $xml->addChild( 'link' ); 
    101         $feed_link->addAttribute( 'rel', 'self' ); 
    102         $feed_link->addAttribute( 'href', $self ); 
     96            $feed_subtitle= $feed->appendChild( $xml->createElement( 'subtitle' ) ); 
     97            $feed_subtitle->appendChild( $xml->createTextNode( $tagline ) ); 
     98        } 
     99 
     100        $feed_updated= $feed->appendChild( $xml->createElement('updated', date( 'c', time() ) ) ); 
     101 
     102        $feed_link= $feed->appendChild( $xml->createElement( 'link' ) ); 
     103        $feed_link->setAttribute( 'rel', 'alternate' ); 
     104        $feed_link->setAttribute( 'href', $alternate ); 
     105 
     106        $feed_link= $feed->appendChild( $xml->createElement( 'link' ) ); 
     107        $feed_link->setAttribute( 'rel', 'self' ); 
     108        $feed_link->setAttribute( 'href', $self ); 
    103109 
    104110        $page= ( isset( $rr_args['page'] ) ) ? $rr_args['page'] : 1; 
     
    111117 
    112118            $rr_args['page']= $firstpage; 
    113             $feed_link= $xml->addChild( 'link' ); 
    114             $feed_link->addAttribute( 'rel', 'first' ); 
    115             $feed_link->addAttribute( 'href', URL::get( $rr_name, $rr_args ) ); 
    116             $feed_link->addAttribute( 'type', 'application/atom+xml' ); 
    117             $feed_link->addAttribute( 'title', _t('First Page') ); 
     119            $feed_link= $feed->appendChild( $xml->createElement( 'link' ) ); 
     120            $feed_link->setAttribute( 'rel', 'first' ); 
     121            $feed_link->setAttribute( 'href', URL::get( $rr_name, $rr_args ) ); 
     122            $feed_link->setAttribute( 'type', 'application/atom+xml' ); 
     123            $feed_link->setAttribute( 'title', _t('First Page') ); 
    118124 
    119125            if ( $prevpage > $firstpage ) { 
    120126                $rr_args['page']= $prevpage; 
    121                 $feed_link= $xml->addChild( 'link' ); 
    122                 $feed_link->addAttribute( 'rel', 'previous' ); 
    123                 $feed_link->addAttribute( 'href', URL::get( $rr_name, $rr_args ) ); 
    124                 $feed_link->addAttribute( 'type', 'application/atom+xml' ); 
    125                 $feed_link->addAttribute( 'title', _t('Previous Page') ); 
     127                $feed_link= $feed->appendChild( $xml->createElement( 'link' ) ); 
     128                $feed_link->setAttribute( 'rel', 'previous' ); 
     129                $feed_link->setAttribute( 'href', URL::get( $rr_name, $rr_args ) ); 
     130                $feed_link->setAttribute( 'type', 'application/atom+xml' ); 
     131                $feed_link->setAttribute( 'title', _t('Previous Page') ); 
    126132            } 
    127133 
    128134            if ( $nextpage <= $lastpage ) { 
    129135                $rr_args['page']= $nextpage; 
    130                 $feed_link= $xml->addChild( 'link' ); 
    131                 $feed_link->addAttribute( 'rel', 'next' ); 
    132                 $feed_link->addAttribute( 'href', URL::get( $rr_name, $rr_args ) ); 
    133                 $feed_link->addAttribute( 'type', 'application/atom+xml' ); 
    134                 $feed_link->addAttribute( 'title', _t('Next Page') ); 
     136                $feed_link= $feed->appendChild( $xml->createElement( 'link' ) ); 
     137                $feed_link->setAttribute( 'rel', 'next' ); 
     138                $feed_link->setAttribute( 'href', URL::get( $rr_name, $rr_args ) ); 
     139                $feed_link->setAttribute( 'type', 'application/atom+xml' ); 
     140                $feed_link->setAttribute( 'title', _t('Next Page') ); 
    135141            } 
    136142 
    137143            $rr_args['page']= $lastpage; 
    138             $feed_link= $xml->addChild( 'link' ); 
    139             $feed_link->addAttribute( 'rel', 'last' ); 
    140             $feed_link->addAttribute( 'href', URL::get( $rr_name, $rr_args ) ); 
    141             $feed_link->addAttribute( 'type', 'application/atom+xml' ); 
    142             $feed_link->addAttribute( 'title', _t('Last Page') ); 
     144            $feed_link= $feed->appendChild( $xml->createElement( 'link' ) ); 
     145            $feed_link->setAttribute( 'rel', 'last' ); 
     146            $feed_link->setAttribute( 'href', URL::get( $rr_name, $rr_args ) ); 
     147            $feed_link->setAttribute( 'type', 'application/atom+xml' ); 
     148            $feed_link->setAttribute( 'title', _t('Last Page') ); 
    143149        } 
    144150 
    145151        $rr_args['page']= $page; 
    146152 
    147         $feed_generator= $xml->addChild( 'generator', 'Habari' ); 
    148         $feed_generator->addAttribute( 'uri', 'http://www.habariproject.org/' ); 
    149         $feed_generator->addAttribute( 'version', Version::get_habariversion() ); 
    150  
    151         $feed_id= $xml->addChild( 'id', 'tag:' . Site::get_url('hostname') . ',' . date("Y-m-d") . ':' . $id . '/' . Options::get( 'GUID' ) ); 
     153        $feed_generator= $feed->appendChild( $xml->createElement( 'generator', 'Habari' ) ); 
     154        $feed_generator->setAttribute( 'uri', 'http://www.habariproject.org/' ); 
     155        $feed_generator->setAttribute( 'version', Version::get_habariversion() ); 
     156 
     157        $feed_id= $feed->appendChild( $xml->createElement( 'id', 'tag:' . Site::get_url('hostname') . ',' . date("Y-m-d") . ':' . $id . '/' . Options::get( 'GUID' ) ) ); 
    152158 
    153159        Plugins::act( 'atom_create_wrapper', $xml ); 
     
    163169    public function add_posts($xml, $posts) 
    164170    { 
     171        //TODO:Is this the best way to retreive the feed element? : 
     172        $elements= $xml->getElementsByTagName( 'feed' ); 
     173        $feed= $elements->item(0); 
     174         
    165175        foreach ( $posts as $post ) { 
    166176            $user= User::get_by_id( $post->user_id ); 
    167             $title= ( $this->is_auth() ) ? htmlspecialchars( $post->title ) : htmlspecialchars( $post->title_atom ); 
    168             $content= ( $this->is_auth() ) ? htmlspecialchars( $post->content ) : htmlspecialchars( $post->content_atom ); 
    169  
    170             $feed_entry= $xml->addChild( 'entry' ); 
    171             $entry_title= $feed_entry->addChild( 'title', $title ); 
    172  
    173             $entry_link= $feed_entry->addChild( 'link' ); 
    174             $entry_link->addAttribute( 'rel', 'alternate' ); 
    175             $entry_link->addAttribute( 'href', $post->permalink ); 
    176  
    177             $entry_link= $feed_entry->addChild( 'link' ); 
    178             $entry_link->addAttribute( 'rel', 'edit' ); 
    179             $entry_link->addAttribute( 'href', URL::get( 'atom_entry', "slug={$post->slug}" ) ); 
    180  
    181             $entry_author= $feed_entry->addChild( 'author' ); 
    182             $author_name= $entry_author->addChild( 'name', $user->displayname ); 
    183  
    184             $entry_id= $feed_entry->addChild( 'id', $post->guid ); 
    185  
    186             $entry_updated= $feed_entry->addChild( 'updated', date( 'c', strtotime( $post->updated ) ) ); 
    187             $entry_edited= $feed_entry->addChild( 'app:edited', date( 'c', strtotime( $post->updated ) ), 'http://www.w3.org/2007/app' ); 
    188  
    189                 foreach ( $post->tags as $tag ) { 
    190                     $entry_category= $feed_entry->addChild( 'category' ); 
    191                     $entry_category->addAttribute( 'term', $tag ); 
    192                 } 
    193  
    194             $entry_content= $feed_entry->addChild( 'content', $content ); 
    195             $entry_content->addAttribute( 'type', 'html' ); 
     177            $title= ( $this->is_auth() ) ? $post->title : $post->title_atom; 
     178            $content= ( $this->is_auth() ) ? $post->content : $post->content_atom; 
     179 
     180            $feed_entry= $feed->appendChild( $xml->createElement( 'entry' ) ); 
     181            $entry_title= $feed_entry->appendChild( $xml->createElement( 'title' ) ); 
     182            $entry_title->appendChild( $xml->createTextNode( $title ) ); 
     183 
     184            $entry_link= $feed_entry->appendChild( $xml->createElement( 'link' ) ); 
     185            $entry_link->setAttribute( 'rel', 'alternate' ); 
     186            $entry_link->setAttribute( 'href', $post->permalink ); 
     187 
     188            $entry_link= $feed_entry->appendChild( $xml->createElement( 'link' ) ); 
     189            $entry_link->setAttribute( 'rel', 'edit' ); 
     190            $entry_link->setAttribute( 'href', URL::get( 'atom_entry', "slug={$post->slug}" ) ); 
     191 
     192            $entry_author= $feed_entry->appendChild( $xml->createElement( 'author' ) ); 
     193            $author_name= $entry_author->appendChild( $xml->createElement( 'name' ) ); 
     194            $author_name= $entry_author->appendChild( $xml->createTextNode( $user->displayname ) ); 
     195 
     196            $entry_id= $feed_entry->appendChild( $xml->createElement( 'id', $post->guid ) ); 
     197 
     198            $entry_updated= $feed_entry->appendChild( $xml->createElement( 'updated', date( 'c', strtotime( $post->updated ) ) ) ); 
     199            $entry_edited= $feed_entry->appendChild( $xml->createElementNS( 'http://www.w3.org/2007/app', 'app:edited', date( 'c', strtotime( $post->updated ) ) ) ); 
     200 
     201            foreach ( $post->tags as $tag ) { 
     202                $entry_category= $feed_entry->appendChild( $xml->createElement( 'category' ) ); 
     203                $entry_category->setAttribute( 'term', $tag ); 
     204            } 
     205 
     206            $entry_content= $feed_entry->appendChild( $xml->createElement( 'content' ) ); 
     207            $entry_content->setAttribute( 'type', 'html' ); 
     208            $entry_content->appendChild( $xml->createTextNode( $content ) ); 
    196209            Plugins::act( 'atom_add_post', $feed_entry, $post ); 
    197210        } 
     
    646659 
    647660        Plugins::act( 'atom_get_collection', $xml, $params, $handler_vars ); 
    648         $xml= $xml->asXML(); 
     661        $xml= $xml->saveXML(); 
    649662 
    650663        ob_clean();