Make WordPress Core


Ignore:
Timestamp:
02/12/2006 07:53:23 AM (19 years ago)
Author:
ryan
Message:

Death to trailing tabs. Props Mark J. fixes #2405

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/rss-functions.php

    r3083 r3517  
    3131    var $current_field      = '';
    3232    var $current_namespace  = false;
    33    
     33
    3434    //var $ERROR = "";
    35    
     35
    3636    var $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright');
    3737
    3838    function MagpieRSS ($source) {
    39        
     39
    4040        # if PHP xml isn't compiled in, die
    4141        #
    4242        if ( !function_exists('xml_parser_create') )
    4343            trigger_error( "Failed to load PHP's XML Extension. http://www.php.net/manual/en/ref.xml.php" );
    44        
     44
    4545        $parser = @xml_parser_create();
    46        
     46
    4747        if ( !is_resource($parser) )
    4848            trigger_error( "Failed to create an instance of PHP's XML parser. http://www.php.net/manual/en/ref.xml.php");
    4949
    50        
     50
    5151        $this->parser = $parser;
    52        
     52
    5353        # pass in parser, and a reference to this object
    5454        # setup handlers
     
    5757        xml_set_element_handler($this->parser,
    5858                'feed_start_element', 'feed_end_element' );
    59                        
     59
    6060        xml_set_character_data_handler( $this->parser, 'feed_cdata' );
    61    
     61
    6262        $status = xml_parse( $this->parser, $source );
    63        
     63
    6464        if (! $status ) {
    6565            $errorcode = xml_get_error_code( $this->parser );
     
    7373            }
    7474        }
    75        
     75
    7676        xml_parser_free( $this->parser );
    7777
    7878        $this->normalize();
    7979    }
    80    
     80
    8181    function feed_start_element($p, $element, &$attrs) {
    8282        $el = $element = strtolower($element);
    8383        $attrs = array_change_key_case($attrs, CASE_LOWER);
    84        
     84
    8585        // check for a namespace, and split if found
    8686        $ns = false;
     
    9191            $this->current_namespace = $ns;
    9292        }
    93            
     93
    9494        # if feed type isn't set, then this is first element of feed
    9595        # identify feed from root element
     
    111111            return;
    112112        }
    113    
     113
    114114        if ( $el == 'channel' )
    115115        {
     
    120120            $this->initem = true;
    121121            if ( isset($attrs['rdf:about']) ) {
    122                 $this->current_item['about'] = $attrs['rdf:about']; 
    123             }
    124         }
    125        
     122                $this->current_item['about'] = $attrs['rdf:about'];
     123            }
     124        }
     125
    126126        // if we're in the default namespace of an RSS feed,
    127127        //  record textinput or image fields
     
    133133            $this->intextinput = true;
    134134        }
    135        
     135
    136136        elseif (
    137137            $this->feed_type == RSS and
     
    141141            $this->inimage = true;
    142142        }
    143        
     143
    144144        # handle atom content constructs
    145145        elseif ( $this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
     
    149149                $el = 'atom_content';
    150150            }
    151            
     151
    152152            $this->incontent = $el;
    153            
    154            
    155         }
    156        
     153
     154
     155        }
     156
    157157        // if inside an Atom content construct (e.g. content or summary) field treat tags as text
    158158        elseif ($this->feed_type == ATOM and $this->incontent )
     
    163163                    array_keys($attrs),
    164164                    array_values($attrs) ) );
    165            
     165
    166166            $this->append_content( "<$element $attrs_str>"  );
    167                    
     167
    168168            array_unshift( $this->stack, $el );
    169169        }
    170        
     170
    171171        // Atom support many links per containging element.
    172172        // Magpie treats link elements of type rel='alternate'
     
    182182                $link_el = 'link_' . $attrs['rel'];
    183183            }
    184            
     184
    185185            $this->append($link_el, $attrs['href']);
    186186        }
     
    190190        }
    191191    }
    192    
    193 
    194    
     192
     193
     194
    195195    function feed_cdata ($p, $text) {
    196        
     196
    197197        if ($this->feed_type == ATOM and $this->incontent)
    198198        {
     
    204204        }
    205205    }
    206    
     206
    207207    function feed_end_element ($p, $el) {
    208208        $el = strtolower($el);
    209        
     209
    210210        if ( $el == 'item' or $el == 'entry' )
    211211        {
     
    223223        }
    224224        elseif ($this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) )
    225         {   
     225        {
    226226            $this->incontent = false;
    227227        }
     
    246246            array_shift( $this->stack );
    247247        }
    248        
     248
    249249        $this->current_namespace = false;
    250250    }
    251    
     251
    252252    function concat (&$str1, $str2="") {
    253253        if (!isset($str1) ) {
     
    256256        $str1 .= $str2;
    257257    }
    258    
     258
    259259    function append_content($text) {
    260260        if ( $this->initem ) {
     
    265265        }
    266266    }
    267    
     267
    268268    // smart append - field and namespace aware
    269269    function append($el, $text) {
     
    307307                    $this->channel[ $el ], $text );
    308308            }
    309            
    310         }
    311     }
    312    
     309
     310        }
     311    }
     312
    313313    function normalize () {
    314314        // if atom populate rss fields
     
    321321                if ( isset($item['atom_content']))
    322322                    $item['content']['encoded'] = $item['atom_content'];
    323                
     323
    324324                $this->items[$i] = $item;
    325             }       
     325            }
    326326        }
    327327        elseif ( $this->is_rss() ) {
     
    333333                if ( isset($item['content']['encoded'] ) )
    334334                    $item['atom_content'] = $item['content']['encoded'];
    335            
     335
    336336                $this->items[$i] = $item;
    337337            }
    338338        }
    339339    }
    340    
     340
    341341    function is_rss () {
    342342        if ( $this->feed_type == RSS ) {
    343             return $this->feed_version; 
     343            return $this->feed_version;
    344344        }
    345345        else {
     
    347347        }
    348348    }
    349    
     349
    350350    function is_atom() {
    351351        if ( $this->feed_type == ATOM ) {
     
    379379    // initialize constants
    380380    init();
    381    
     381
    382382    if ( !isset($url) ) {
    383383        // error("fetch_rss called without a url");
    384384        return false;
    385385    }
    386    
     386
    387387    // if cache is disabled
    388388    if ( !MAGPIE_CACHE_ON ) {
     
    404404        // 3. if cached obj fails freshness check, fetch remote
    405405        // 4. if remote fails, return stale object, or error
    406        
     406
    407407        $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
    408        
     408
    409409        if (MAGPIE_DEBUG and $cache->ERROR) {
    410410            debug($cache->ERROR, E_USER_WARNING);
    411411        }
    412        
    413        
     412
     413
    414414        $cache_status    = 0;       // response of check_cache
    415415        $request_headers = array(); // HTTP headers to send with fetch
    416416        $rss             = 0;       // parsed RSS object
    417417        $errormsg        = 0;       // errors, if any
    418        
     418
    419419        if (!$cache->ERROR) {
    420420            // return cache HIT, MISS, or STALE
     
    433433            }
    434434        }
    435        
     435
    436436        // else attempt a conditional get
    437        
     437
    438438        // setup headers
    439439        if ( $cache_status == 'STALE' ) {
     
    444444            }
    445445        }
    446        
     446
    447447        $resp = _fetch_remote_file( $url, $request_headers );
    448        
     448
    449449        if (isset($resp) and $resp) {
    450450            if ($resp->status == '304' ) {
     
    484484            $errormsg = "Unable to retrieve RSS file for unknown reasons.";
    485485        }
    486        
     486
    487487        // else fetch failed
    488        
     488
    489489        // attempt to return cached object
    490490        if ($rss) {
     
    494494            return $rss;
    495495        }
    496        
     496
    497497        // else we totally failed
    498         // error( $errormsg ); 
    499        
     498        // error( $errormsg );
     499
    500500        return false;
    501        
     501
    502502    } // end if ( !MAGPIE_CACHE_ON ) {
    503503} // end fetch_rss()
     
    512512        $client->rawheaders = $headers;
    513513    }
    514    
     514
    515515    @$client->fetch($url);
    516516    return $client;
     
    520520function _response_to_rss ($resp) {
    521521    $rss = new MagpieRSS( $resp->results );
    522    
    523     // if RSS parsed successfully       
     522
     523    // if RSS parsed successfully
    524524    if ( $rss and !$rss->ERROR) {
    525        
     525
    526526        // find Etag, and Last-Modified
    527527        foreach($resp->headers as $h) {
     
    534534                $val = "";
    535535            }
    536            
     536
    537537            if ( $field == 'ETag' ) {
    538538                $rss->etag = $val;
    539539            }
    540            
     540
    541541            if ( $field == 'Last-Modified' ) {
    542542                $rss->last_modified = $val;
    543543            }
    544544        }
    545        
    546         return $rss;   
     545
     546        return $rss;
    547547    } // else construct error message
    548548    else {
    549549        $errormsg = "Failed to parse RSS file.";
    550        
     550
    551551        if ($rss) {
    552552            $errormsg .= " (" . $rss->ERROR . ")";
    553553        }
    554554        // error($errormsg);
    555        
     555
    556556        return false;
    557557    } // end if ($rss and !$rss->error)
     
    570570        define('MAGPIE_INITALIZED', 1);
    571571    }
    572    
     572
    573573    if ( !defined('MAGPIE_CACHE_ON') ) {
    574574        define('MAGPIE_CACHE_ON', 1);
     
    586586        define('MAGPIE_CACHE_FRESH_ONLY', 0);
    587587    }
    588    
     588
    589589        if ( !defined('MAGPIE_DEBUG') ) {
    590590        define('MAGPIE_DEBUG', 0);
     
    593593    if ( !defined('MAGPIE_USER_AGENT') ) {
    594594        $ua = 'WordPress/' . $wp_version;
    595        
     595
    596596        if ( MAGPIE_CACHE_ON ) {
    597597            $ua = $ua . ')';
     
    600600            $ua = $ua . '; No cache)';
    601601        }
    602        
     602
    603603        define('MAGPIE_USER_AGENT', $ua);
    604604    }
    605    
     605
    606606    if ( !defined('MAGPIE_FETCH_TIME_OUT') ) {
    607607        define('MAGPIE_FETCH_TIME_OUT', 2); // 2 second timeout
    608608    }
    609    
     609
    610610    // use gzip encoding to fetch rss files if supported?
    611611    if ( !defined('MAGPIE_USE_GZIP') ) {
    612         define('MAGPIE_USE_GZIP', true);   
     612        define('MAGPIE_USE_GZIP', true);
    613613    }
    614614}
     
    642642    var $MAX_AGE    = 43200;        // when are files stale, default twelve hours
    643643    var $ERROR      = '';           // accumulate error messages
    644    
     644
    645645    function RSSCache ($base='', $age='') {
    646646        if ( $base ) {
     
    650650            $this->MAX_AGE = $age;
    651651        }
    652    
    653     }
    654    
     652
     653    }
     654
    655655/*=======================================================================*\
    656656    Function:   set
    657657    Purpose:    add an item to the cache, keyed on url
    658658    Input:      url from wich the rss file was fetched
    659     Output:     true on sucess 
     659    Output:     true on sucess
    660660\*=======================================================================*/
    661661    function set ($url, $rss) {
     
    663663        $cache_option = 'rss_' . $this->file_name( $url );
    664664        $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts';
    665        
     665
    666666        if ( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$cache_option'") )
    667667            add_option($cache_option, '', '', 'no');
    668668        if ( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$cache_timestamp'") )
    669669            add_option($cache_timestamp, '', '', 'no');
    670        
     670
    671671        update_option($cache_option, $rss);
    672672        update_option($cache_timestamp, time() );
    673        
     673
    674674        return $cache_option;
    675675    }
    676    
     676
    677677/*=======================================================================*\
    678678    Function:   get
    679679    Purpose:    fetch an item from the cache
    680680    Input:      url from wich the rss file was fetched
    681     Output:     cached object on HIT, false on MISS 
    682 \*=======================================================================*/ 
     681    Output:     cached object on HIT, false on MISS
     682\*=======================================================================*/
    683683    function get ($url) {
    684684        $this->ERROR = "";
    685685        $cache_option = 'rss_' . $this->file_name( $url );
    686        
     686
    687687        if ( ! get_option( $cache_option ) ) {
    688688            $this->debug(
     
    691691            return 0;
    692692        }
    693        
     693
    694694        $rss = get_option( $cache_option );
    695        
     695
    696696        return $rss;
    697697    }
     
    702702                and whether the object is older then MAX_AGE (ie. STALE)
    703703    Input:      url from wich the rss file was fetched
    704     Output:     cached object on HIT, false on MISS 
    705 \*=======================================================================*/     
     704    Output:     cached object on HIT, false on MISS
     705\*=======================================================================*/
    706706    function check_cache ( $url ) {
    707707        $this->ERROR = "";
     
    730730/*=======================================================================*\
    731731    Function:   serialize
    732 \*=======================================================================*/     
     732\*=======================================================================*/
    733733    function serialize ( $rss ) {
    734734        return serialize( $rss );
     
    737737/*=======================================================================*\
    738738    Function:   unserialize
    739 \*=======================================================================*/     
     739\*=======================================================================*/
    740740    function unserialize ( $data ) {
    741741        return unserialize( $data );
    742742    }
    743    
     743
    744744/*=======================================================================*\
    745745    Function:   file_name
     
    747747    Input:      url from wich the rss file was fetched
    748748    Output:     a file name
    749 \*=======================================================================*/     
     749\*=======================================================================*/
    750750    function file_name ($url) {
    751751        return md5( $url );
    752752    }
    753    
     753
    754754/*=======================================================================*\
    755755    Function:   error
    756756    Purpose:    register error
    757 \*=======================================================================*/         
     757\*=======================================================================*/
    758758    function error ($errormsg, $lvl=E_USER_WARNING) {
    759759        // append PHP's error message if track_errors enabled
     
    777777
    778778function parse_w3cdtf ( $date_str ) {
    779    
     779
    780780    # regex to match wc3dtf
    781781    $pat = "/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/";
    782    
     782
    783783    if ( preg_match( $pat, $date_str, $match ) ) {
    784784        list( $year, $month, $day, $hours, $minutes, $seconds) =
    785785            array( $match[1], $match[2], $match[3], $match[4], $match[5], $match[6]);
    786        
     786
    787787        # calc epoch for current date assuming GMT
    788788        $epoch = gmmktime( $hours, $minutes, $seconds, $month, $day, $year);
    789        
     789
    790790        $offset = 0;
    791791        if ( $match[10] == 'Z' ) {
     
    795795            list( $tz_mod, $tz_hour, $tz_min ) =
    796796                array( $match[8], $match[9], $match[10]);
    797            
     797
    798798            # zero out the variables
    799799            if ( ! $tz_hour ) { $tz_hour = 0; }
    800800            if ( ! $tz_min ) { $tz_min = 0; }
    801        
     801
    802802            $offset_secs = (($tz_hour*60)+$tz_min)*60;
    803            
     803
    804804            # is timezone ahead of GMT?  then subtract offset
    805805            #
     
    807807                $offset_secs = $offset_secs * -1;
    808808            }
    809            
    810             $offset = $offset_secs; 
     809
     810            $offset = $offset_secs;
    811811        }
    812812        $epoch = $epoch + $offset;
     
    830830                    echo "</a><br />\n";
    831831                    echo "</li>\n";
    832                 }       
     832                }
    833833            echo "</ul>";
    834834    }
Note: See TracChangeset for help on using the changeset viewer.