Changeset 667

Show
Ignore:
Timestamp:
2008-07-04 22:48:39 (2 months ago)
Author:
arthus
Message:

Refactored Lifestream to use database... still working on more API interactions

Files:
1 modified

Legend:

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

    r666 r667  
    11<?php 
    22 
    3 //require 'simplepie.php'; 
     3require 'simplepie.php'; 
    44require 'idna_convert.php'; 
    55 
     
    2727        switch( DB::get_driver_name() ) {  
    2828            case 'sqlite': 
    29                 $q= ''; 
    30                 $q= "CREATE TABLE " . DB::table('l_source') . "(  
    31                     id INTEGER NOT NULL AUTOINCREMENT, 
    32                     title VARCHAR(255) NOT NULL, 
    33                     profile_url VARCHAR(255) NOT NULL, 
    34                     feed_url VARCHAR(255) NOT NULL, 
    35                     favicon VARCHAR(255) NOT NULL, 
    36                     CREATE UNIQUE INDEX IF NOT EXISTS id ON ". DB::table('l_source') . "(id); 
    37                 );"; 
    38                  
    39                 $q .= "CREATE TABLE " . DB::table('l_data') . "( 
     29                $q = "CREATE TABLE " . DB::table('l_data') . "( 
    4030                    id INTIGER NOT NULL AUTOINCREMENT, 
    4131                    name VARCHAR(255) NOT NULL, 
    4232                    content TEXT NOT NULL, 
     33                    data BLOB NOT NULL, 
    4334                    date VARCHAR(255) NOT NULL, 
    4435                    link VARCHAR(255) NOT NULL, 
     
    4940            break; 
    5041            case 'mysql' : 
    51                 $q= ''; 
    52                 $q= "CREATE TABLE " . DB::table('l_source') . "(  
    53                     id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
    54                     title VARCHAR(255) NOT NULL, 
    55                     profile_url VARCHAR(255) NOT NULL, 
    56                     feed_url VARCHAR(255) NOT NULL, 
    57                     favicon VARCHAR(255) NOT NULL, 
    58                     UNIQUE KEY id (id) 
    59                 );"; 
    60                 $q .= "CREATE TABLE " . DB::table('l_data') . "( 
     42                $q = "CREATE TABLE " . DB::table('l_data') . "( 
    6143                    id INT UNSIGNED NOT NULL AUTO_INCREMENT, 
    6244                    name VARCHAR(255) NOT NULL, 
    6345                    content TEXT NOT NULL, 
     46                    data BLOB NOT NULL, 
    6447                    date VARCHAR(255) NOT NULL, 
    6548                    link VARCHAR(255) NOT NULL, 
     
    7861        $rules[] = new RewriteRule(array( 
    7962            'name' => 'lifestream', 
    80             'parse_regex' => '/^lifestream[\/]{0,1}$/i', 
    81             'build_str' => 'lifestream', 
     63            'parse_regex' => '/^' . Options::get('lifestream__lifeurl') . '[\/]{0,1}$/i', 
     64            'build_str' => Options::get('lifestream__lifeurl'), 
    8265            'handler' => 'LifeStreamHandler', 
    8366            'action' => 'display_lifestream', 
     
    127110                $ui = new FormUI( 'lifestream' ); 
    128111                // Add a text control for the feed URL 
    129                 $feedurl= $ui->append('text', 'lifeurl', 'lifestream__lifeurl', _t('Lifestream URL')); 
     112                $feedurl= $ui->append('text', 'feedurl', 'lifestream__feedurl', _t('Feed URL')); 
    130113                // Mark the field as required 
    131114                $feedurl->add_validator('validate_required'); 
     
    133116                $feedurl->add_validator('validate_url'); 
    134117                 
     118                // Add a text control for the rewrite base 
     119                $rewritebase= $ui->append('text', 'lifeurl', 'lifestream__lifeurl', _t('Lifestream URL')); 
     120                // Mark the field as required 
     121                $rewritebase->add_validator('validate_required'); 
     122                 
    135123                $submit= $ui->append( 'submit', 'submit', _t('Save') ); 
    136124 
     
    140128            } 
    141129        } 
     130    } 
     131     
     132    public function insert($entries = array()) { 
     133        foreach($entries as $entry) { 
     134            DB::insert(DB::table('l_data'), $entry); 
     135        } 
     136    } 
     137 
     138    public function get_entries($type = 'any', $offset = 0, $number = 20) { 
     139        $query= ''; 
     140        $query.= 'SELECT * FROM ' . DB::table('l_data'); 
     141         
     142        if($type != 'any') { 
     143            $query.= " WHERE name= '$type'"; 
     144        } 
     145         
     146        $query.= ' ORDER BY date DESC'; 
     147        $query.= ' LIMIT ' . $offset . ', ' . $number; 
     148        $results = DB::get_results( $query ); 
     149         
     150        return $results; 
    142151    } 
    143152     
     
    151160     
    152161    public function __construct() { 
     162        $this->config= simplexml_load_file( dirname( __FILE__ ) . '/lifestream.config.xml' ); 
    153163        $this->theme= Themes::create(); 
    154164    } 
    155165     
    156166    public function act_display_lifestream() { 
    157         $this->archive_feeds(); 
    158         $this->theme->assign( 'lifestream', $this->stream_contents ); 
     167         
     168        $this->fetch_fields(); 
     169         
     170        $this->theme->assign( 'lifestream', LifeStream::get_entries() ); 
    159171        $this->theme->assign( 'title', 'Lifestream - ' . Options::get( 'title' ) ); 
    160172        $this->theme->assign( 'streams', $this->config->stream ); 
    161173        $this->theme->display( 'lifestream' ); 
    162174    } 
    163  
    164     private function fetch_remote_file( $file ) { 
    165  
    166         $path = parse_url( $file ); 
    167  
    168         if ($fs = @fsockopen($path['host'], isset($path['port'])?$path['port']:80)) { 
    169  
    170             $header = "GET " . $path['path'] . " HTTP/1.0\r\nHost: " . $path['host'] . "\r\n\r\n"; 
    171  
    172             fwrite($fs, $header); 
    173  
    174             $buffer = ''; 
    175  
    176             while ($tmp = fread($fs, 1024)) { $buffer .= $tmp; } 
    177  
    178             preg_match('/HTTP\/[0-9\.]{1,3} ([0-9]{3})/', $buffer, $http); 
    179             preg_match('/Location: (.*)/', $buffer, $redirect); 
    180  
    181             if (isset($redirect[1]) && $file != trim($redirect[1])) { return self::fetch_remote_file(trim($redirect[1])); } 
    182  
    183             if (isset($http[1]) && $http[1] == 200) { return substr($buffer, strpos($buffer, "\r\n\r\n") +4); } else { return false; } 
    184  
    185         } else { return false; } 
    186  
    187     } 
    188      
    189     private function favicache( $feed, $name ) { 
    190         $folder= '/user/plugins/lifestream/images/'; 
    191  
    192         if ( !is_dir( ABSPATH . $folder ) ) {  
    193             mkdir( ABSPATH . $folder, 0777 );  
    194         } 
    195  
    196         $url= parse_url( $feed ); 
    197  
    198         $cache= self::fetch_remote_file( 'http://' . $url['host'] . '/favicon.ico' ); 
    199          
    200         if ( !$cache ) { 
    201             preg_match( '/<link.*(?:rel="icon" href="(.*)"|href="(.*)" rel="icon").*>/U', 
    202             self::fetch_remote_file( $_POST['link_url'] ), $matches ); 
    203             $cache= self::fetch_remote_file( $matches[1] ); 
    204         } 
    205  
    206         if ( $cache ) { 
    207             file_put_contents( HABARI_PATH . '/' . $folder . md5( $url['host'] ) . '.ico', $cache ); 
    208             $icon= get_option( 'siteurl' ) . $folder . md5( $url['host'] ) . '.ico'; 
    209             } elseif( is_file( HABARI_PATH  . '/' . 'user/plugins/lifestream/images/icon.gif' ) ) { 
    210                 $icon= Site::get_url( 'habari' ) . 'user/plugins/lifestream/images/icon.gif'; 
    211         } 
    212          
    213         $wpdb->query( 'UPDATE `' . DB::table( 'l_source' ) . '` SET `favicon` = "' . $icon . '" WHERE `title` = "' . $name . '"' ); 
    214         return false; 
    215     } 
    216      
    217     /** 
    218     * Grab all the sources we have stored in the db. 
    219     * <code> 
    220     * foreach( $streams->collect() as $source ) { 
    221     *   echo $source->title;     
    222     * } 
    223     * </code> 
    224     */ 
    225     public function collect() { 
    226         $sources= DB::get_results( "SELECT * FROM " . DB::table('l_data') ); 
    227         if( is_array( $sources ) ) { 
    228             return $sources; 
    229         } else { 
    230             return array(); 
    231         } 
    232     }    
    233      
    234     private function get_feeds() { 
    235         $this->config= simplexml_load_file( dirname( __FILE__ ) . '/lifestream.config.xml' ); 
     175     
     176 
     177     
     178    public function fetch_feeds() { 
    236179        foreach ( $this->config->stream as $stream ) { 
    237180            $feed = new SimplePie( (string) $stream['feedURL'], HABARI_PATH . '/' . (string) $this->config->cache['location'], (int) $this->config->cache['expire'] ); 
     
    241184                    $name= $stream['name']; 
    242185                    $date = strtotime( substr( $entry->get_date(), 0, 25 ) ); 
    243                     $this->stream_contents[$date]['name']= (string) $name; 
    244                     $this->stream_contents[$date]['title']= $entry->get_title(); 
    245                     $this->stream_contents[$date]['link']= $entry->get_permalink(); 
    246                     $this->stream_contents[$date]['date']= strtotime( substr( $entry->get_date(), 0, 25 ) ); 
     186                    $data['name']= (string) $name; 
     187                    $data['content']= $entry->get_title(); 
     188                    $data['link']= $entry->get_permalink(); 
     189                    $data['date']= strtotime( substr( $entry->get_date(), 0, 25 ) ); 
    247190                    if ( $enclosure = $entry->get_enclosure( 0 ) ) { 
    248                         $this->stream_contents[$date]['enclosure'] = $enclosure->get_link(); 
     191                        $data['data'] = $enclosure->get_link(); 
    249192                    } 
     193                     
     194                    $this->stream_contents[$date]= $data; 
     195                     
    250196                } 
    251197            } 
    252198        } 
    253         krsort( $this->stream_contents ); 
     199         
     200        ksort( $this->stream_contents ); 
     201         
     202        LifeStream::insert($this->stream_contents); 
     203         
    254204        return $this->stream_contents; 
    255205    } 
    256      
    257     public function archive_feeds() { 
    258         foreach( self::get_feeds() as $archive ) { 
    259             if( !DB::exists( DB::table('l_data'), array( 'link' => $archive['link'], 'date' => $archive['date'] ) ) ) { 
    260                 $insert= array(); 
    261                 $insert['name']= addslashes( $archive['name'] ); 
    262                 $insert['content']= addslashes( $archive['title'] ); 
    263                 $insert['date']= $archive['date']; 
    264                 $insert['link']= $archive['link']; 
    265                 $insert['enabled']= 1; 
    266                 return DB::insert( DB::table( 'l_data' ), $insert ); 
    267             } 
    268         } 
    269     } 
    270      
    271     /** 
    272     * Method to grab all of our lifestream data from the DB. 
    273     * <code> 
    274     * foreach( $streams->show_streams() as $stream ) { 
    275     *   // do something clever 
    276     * } 
    277     * </code> 
    278     */ 
    279     public function show_streams() { 
    280         $show= DB::get_results( "SELECT * FROM " . DB::table( 'l_data' ) . " WHERE enabled = 1 ORDER BY date DESC" ); 
    281         return $show; 
    282     } 
    283      
    284     public function source( $name ) { 
    285         $which= DB::get_results( "SELECT profile_url, favicon FROM " . DB::table( 'l_source' ) ." WHERE title = '$name'" ); 
    286         return $which[0]; 
    287     } 
    288      
    289     public function legend_types() { 
    290         $types= DB::get_results( "SELECT * FROM " . DB::table( 'l_source' ) ." ORDER BY title" ); 
    291         return $types; 
    292     }    
     206 
    293207} 
    294208