Show
Ignore:
Timestamp:
01/09/06 12:35:00 (3 years ago)
Author:
mbonetti
Message:

Formatting, and fixed a tiny bug in rss_dbcache

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/rss/extlib/rss_fetch.inc

    r1155 r1164  
    1616 * 
    1717 */ 
    18   
     18 
    1919// Setup MAGPIE_DIR for use on hosts that don't include 
    2020// the current path in include_path. 
     
    5151define ('MAGPIE_FEED_ORIGIN_NOT_FETCHED', 64); 
    5252 
    53 /*  
     53/* 
    5454 * CONSTANTS - redefine these in your script to change the 
    5555 * behaviour of fetch_rss() currently, most options effect the cache 
     
    8383    Input:    url of RSS file 
    8484    Output:   parsed RSS object (see rss_parse.inc) 
    85  
     85  
    8686    NOTES ON CACHEING:   
    8787    If caching is on (MAGPIE_CACHE_ON) fetch_rss will first check the cache. 
     
    104104    // initialize constants 
    105105    init(); 
    106      
     106 
    107107    if ( !isset($url) ) { 
    108108        error("fetch_rss called without a url"); 
    109109        return false; 
    110110    } 
    111      
     111 
    112112    // if cache is disabled 
    113113    if ( !MAGPIE_CACHE_ON ) { 
     
    115115        $resp = _fetch_remote_file( $url ); 
    116116        if ( is_success( $resp->status ) ) { 
    117         return _response_to_rss( $resp, 
    118                      MAGPIE_FEED_ORIGIN_NETWORK | MAGPIE_FEED_ORIGIN_HTTP_200 ); 
    119         } 
    120         else { 
     117            return _response_to_rss( $resp, 
     118                                     MAGPIE_FEED_ORIGIN_NETWORK | MAGPIE_FEED_ORIGIN_HTTP_200 ); 
     119        } else { 
    121120            error("Failed to fetch $url and cache is off"); 
    122121            return false; 
    123122        } 
    124     }  
     123    } 
    125124    // else cache is ON 
    126125    else { 
     
    130129        // 3. if cached obj fails freshness check, fetch remote 
    131130        // 4. if remote fails, return stale object, or error 
    132          
    133     // sameer: changed to use db 
     131 
     132        // sameer: changed to use db 
    134133        $cache = new RSSdbCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE ); 
    135          
     134 
    136135        if (MAGPIE_DEBUG and $cache->ERROR) { 
    137136            debug($cache->ERROR, E_USER_WARNING); 
    138137        } 
    139          
    140          
     138 
     139 
    141140        $cache_status    = 0;       // response of check_cache 
    142141        $request_headers = array(); // HTTP headers to send with fetch 
    143142        $rss             = 0;       // parsed RSS object 
    144143        $errormsg        = 0;       // errors, if any 
    145          
     144 
    146145        // store parsed XML by desired output encoding 
    147146        // as character munging happens at parse time 
    148147        //$cache_key       = $url . MAGPIE_OUTPUT_ENCODING; 
    149     // sameer: removed the encoding from the key 
     148        // sameer: removed the encoding from the key 
    150149        $cache_key       = $url; 
    151          
     150 
    152151        if (!$cache->ERROR) { 
    153152            // return cache HIT, MISS, or STALE 
    154         $cache_status = $cache->check_cache( $cache_key ); 
    155         } 
    156      
     153            $cache_status = $cache->check_cache( $cache_key ); 
     154        } 
     155 
    157156        // if object cached, and cache is fresh, return cached obj 
    158157        if ( $cache_status == 'HIT' ) { 
    159             $rss = $cache->get( $cache_key ); 
     158            $rss = $cache->get 
     159                   ( $cache_key ); 
    160160            if ( isset($rss) and $rss ) { 
    161161                $rss->from_cache = 1; 
    162162                if ( MAGPIE_DEBUG > 1) { 
    163                 debug("MagpieRSS: Cache HIT", E_USER_NOTICE); 
    164             } 
    165         $rss -> rss_origin = MAGPIE_FEED_ORIGIN_CACHE | MAGPIE_FEED_ORIGIN_NOT_FETCHED; 
     163                    debug("MagpieRSS: Cache HIT", E_USER_NOTICE); 
     164                } 
     165                $rss -> rss_origin = MAGPIE_FEED_ORIGIN_CACHE | MAGPIE_FEED_ORIGIN_NOT_FETCHED; 
    166166                return $rss; 
    167167            } 
    168168        } 
    169          
     169 
    170170        // else attempt a conditional get 
    171          
     171 
    172172        // setup headers 
    173173        if ( $cache_status == 'STALE' ) { 
    174             $rss = $cache->get( $cache_key ); 
    175         if ( isset($rss->etag) &&  $rss->etag and  
    176          isset($rss->last_modified) && $rss->last_modified ) { 
     174            $rss = $cache->get 
     175                   ( $cache_key ); 
     176            if ( isset($rss->etag) &&  $rss->etag and 
     177                    isset($rss->last_modified) && $rss->last_modified ) { 
    177178                $request_headers['If-None-Match'] = $rss->etag; 
    178179                $request_headers['If-Last-Modified'] = $rss->last_modified; 
    179             }        
    180         } 
    181          
     180            } 
     181        } 
     182 
    182183        $resp = _fetch_remote_file( $url, $request_headers ); 
    183          
     184 
    184185        if (isset($resp) and $resp) { 
    185186            if ($resp->status == '304' ) { 
     
    190191                // reset cache on 304 (at minutillo insistent prodding) 
    191192                $rss->rss_origin = MAGPIE_FEED_ORIGIN_CACHE | MAGPIE_FEED_ORIGIN_HTTP_304; 
    192                 $cache->set($cache_key, $rss); 
     193                $cache->set 
     194                ($cache_key, $rss); 
    193195                return $rss; 
    194196            } 
    195197            elseif ( is_success( $resp->status ) ) { 
    196         $rss = _response_to_rss( $resp, MAGPIE_FEED_ORIGIN_NETWORK | MAGPIE_FEED_ORIGIN_HTTP_200 ); 
     198                $rss = _response_to_rss( $resp, MAGPIE_FEED_ORIGIN_NETWORK | MAGPIE_FEED_ORIGIN_HTTP_200 ); 
    197199                if ( $rss ) { 
    198200                    if (MAGPIE_DEBUG > 1) { 
     
    200202                    } 
    201203                    // add object to cache 
    202                     $cache->set( $cache_key, $rss ); 
     204                    $cache->set 
     205                    ( $cache_key, $rss ); 
    203206                    return $rss; 
    204207                } 
     
    210213                } 
    211214                elseif ( $resp->error ) { 
    212                     # compensate for Snoopy's annoying habbit to tacking 
     215# compensate for Snoopy's annoying habbit to tacking 
    213216                    # on '\n' 
    214217                    $http_error = substr($resp->error, 0, -2); 
     
    219222                } 
    220223            } 
    221         } 
    222         else { 
     224        } else { 
    223225            $errormsg = "Unable to retrieve RSS file for unknown reasons."; 
    224226        } 
    225          
     227 
    226228        // else fetch failed 
    227          
     229 
    228230        // attempt to return cached object 
    229231        if ($rss) { 
     
    231233                debug("Returning STALE object for $url"); 
    232234            } 
    233         $rss -> rss_origin = MAGPIE_FEED_ORIGIN_CACHE; 
    234         if (is_object($resp) && isset($resp->status)) { 
    235         switch ($resp->status) { 
    236         case '404': 
    237             $rss -> rss_origin |= MAGPIE_FEED_ORIGIN_HTTP_404; 
    238             break; 
    239         default: 
    240             $rss -> rss_origin |= MAGPIE_FEED_ORIGIN_HTTP_TIMEOUT; 
    241             break;           
    242         } 
    243         } else { 
    244         $rss -> rss_origin |= MAGPIE_FEED_ORIGIN_HTTP_TIMEOUT; 
    245         } 
     235            $rss -> rss_origin = MAGPIE_FEED_ORIGIN_CACHE; 
     236            if (is_object($resp) && isset($resp->status)) { 
     237                switch ($resp->status) { 
     238                case '404': 
     239                        $rss -> rss_origin |= MAGPIE_FEED_ORIGIN_HTTP_404; 
     240                    break; 
     241                default: 
     242                    $rss -> rss_origin |= MAGPIE_FEED_ORIGIN_HTTP_TIMEOUT; 
     243                    break; 
     244                } 
     245            } else { 
     246                $rss -> rss_origin |= MAGPIE_FEED_ORIGIN_HTTP_TIMEOUT; 
     247            } 
    246248            return $rss; 
    247249        } 
    248          
     250 
    249251        // else we totally failed 
    250     if ($errormsg) { 
    251         global $MAGPIE_ERROR;  
    252         $MAGPIE_ERROR = $errormsg;    
    253     } 
    254         //error( $errormsg );  
    255          
     252        if ($errormsg) { 
     253            global $MAGPIE_ERROR; 
     254            $MAGPIE_ERROR = $errormsg; 
     255        } 
     256        //error( $errormsg ); 
     257 
    256258        return false; 
    257          
     259 
    258260    } // end if ( !MAGPIE_CACHE_ON ) { 
    259261} // end fetch_rss() 
     
    265267 
    266268function error ($errormsg, $lvl=E_USER_WARNING) { 
    267         global $MAGPIE_ERROR; 
    268          
    269         // append PHP's error message if track_errors enabled 
    270         if ( isset($php_errormsg) ) {  
    271             $errormsg .= " ($php_errormsg)"; 
    272         } 
    273         if ( $errormsg ) { 
    274             $errormsg = "MagpieRSS: $errormsg"; 
    275             $MAGPIE_ERROR = $errormsg; 
    276             trigger_error( $errormsg, $lvl);                 
    277         } 
     269    global $MAGPIE_ERROR; 
     270 
     271    // append PHP's error message if track_errors enabled 
     272    if ( isset($php_errormsg) ) { 
     273        $errormsg .= " ($php_errormsg)"; 
     274    } 
     275    if ( $errormsg ) { 
     276        $errormsg = "MagpieRSS: $errormsg"; 
     277        $MAGPIE_ERROR = $errormsg; 
     278        trigger_error( $errormsg, $lvl); 
     279    } 
    278280} 
    279281 
     
    281283    trigger_error("MagpieRSS [debug] $debugmsg", $lvl); 
    282284} 
    283              
     285 
    284286/*=======================================================================*\ 
    285287    Function:   magpie_error 
     
    288290function magpie_error ($errormsg="") { 
    289291    global $MAGPIE_ERROR; 
    290      
    291     if ( isset($errormsg) and $errormsg ) {  
     292 
     293    if ( isset($errormsg) and $errormsg ) { 
    292294        $MAGPIE_ERROR = $errormsg; 
    293295    } 
    294      
    295     return $MAGPIE_ERROR;    
     296 
     297    return $MAGPIE_ERROR; 
    296298} 
    297299 
     
    312314        $client->rawheaders = $headers; 
    313315    } 
    314      
     316 
    315317    @$client->fetch($url); 
    316318    return $client; 
     
    326328function _response_to_rss ($resp, $rss_origin = 0) { 
    327329    $rss = new MagpieRSS( $resp->results, MAGPIE_OUTPUT_ENCODING, MAGPIE_INPUT_ENCODING, MAGPIE_DETECT_ENCODING ); 
    328      
    329     // if RSS parsed successfully        
     330 
     331    // if RSS parsed successfully 
    330332    if ( $rss and !$rss->ERROR) { 
    331           
    332          // find Etag, and Last-Modified 
    333     foreach($resp->headers as $h) { 
     333 
     334        // find Etag, and Last-Modified 
     335        foreach($resp->headers as $h) { 
    334336            // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1" 
    335337            if (strpos($h, ": ")) { 
    336338                list($field, $val) = explode(": ", $h, 2); 
    337             } 
    338             else { 
     339            } else { 
    339340                $field = $h; 
    340341                $val = ""; 
    341342            } 
    342              
     343 
    343344            if ( $field == 'ETag' ) { 
    344345                $rss->etag = $val; 
    345346            } 
    346              
     347 
    347348            if ( $field == 'Last-Modified' ) { 
    348349                $rss->last_modified = $val; 
    349350            } 
    350351        } 
    351          
    352     $rss -> rss_origin = $rss_origin; 
    353         return $rss;     
     352 
     353        $rss -> rss_origin = $rss_origin; 
     354        return $rss; 
    354355    } // else construct error message 
    355356    else { 
    356357        $errormsg = "Failed to parse RSS file."; 
    357          
     358 
    358359        if ($rss) { 
    359360            $errormsg .= " (" . $rss->ERROR . ")"; 
    360361        } 
    361362        error($errormsg); 
    362          
     363 
    363364        return false; 
    364365    } // end if ($rss and !$rss->error) 
     
    373374    if ( defined('MAGPIE_INITALIZED') ) { 
    374375        return; 
    375     } 
    376     else { 
     376    } else { 
    377377        define('MAGPIE_INITALIZED', true); 
    378378    } 
    379      
     379 
    380380    if ( !defined('MAGPIE_CACHE_ON') ) { 
    381381        define('MAGPIE_CACHE_ON', true); 
     
    397397        define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); 
    398398    } 
    399      
     399 
    400400    if ( !defined('MAGPIE_INPUT_ENCODING') ) { 
    401401        define('MAGPIE_INPUT_ENCODING', null); 
    402402    } 
    403      
     403 
    404404    if ( !defined('MAGPIE_DETECT_ENCODING') ) { 
    405405        define('MAGPIE_DETECT_ENCODING', true); 
    406406    } 
    407      
     407 
    408408    if ( !defined('MAGPIE_DEBUG') ) { 
    409409        define('MAGPIE_DEBUG', 0); 
    410410    } 
    411      
     411 
    412412    if ( !defined('MAGPIE_USER_AGENT') ) { 
    413413        $ua = 'MagpieRSS/'. MAGPIE_VERSION . ' (+http://magpierss.sf.net'; 
    414          
     414 
    415415        if ( MAGPIE_CACHE_ON ) { 
    416416            $ua = $ua . ')'; 
    417         } 
    418         else { 
     417        } else { 
    419418            $ua = $ua . '; No cache)'; 
    420419        } 
    421          
     420 
    422421        define('MAGPIE_USER_AGENT', $ua); 
    423422    } 
    424      
     423 
    425424    if ( !defined('MAGPIE_FETCH_TIME_OUT') ) { 
    426425        define('MAGPIE_FETCH_TIME_OUT', 5); // 5 second timeout 
    427426    } 
    428      
     427 
    429428    // use gzip encoding to fetch rss files if supported? 
    430429    if ( !defined('MAGPIE_USE_GZIP') ) { 
    431         define('MAGPIE_USE_GZIP', true);     
     430        define('MAGPIE_USE_GZIP', true); 
    432431    } 
    433432} 
     
    442441     
    443442    All of them take an HTTP status code as input, and return true or false 
    444  
     443  
    445444    All this code is adapted from LWP's HTTP::Status. 
    446445\*=======================================================================*/ 
     
    451450    Purpose:    return true if Informational status code 
    452451\*=======================================================================*/ 
    453 function is_info ($sc) {  
    454     return $sc >= 100 && $sc < 200;  
     452function is_info ($sc) { 
     453    return $sc >= 100 && $sc < 200; 
    455454} 
    456455 
     
    459458    Purpose:    return true if Successful status code 
    460459\*=======================================================================*/ 
    461 function is_success ($sc) {  
    462     return $sc >= 200 && $sc < 300;  
     460function is_success ($sc) { 
     461    return $sc >= 200 && $sc < 300; 
    463462} 
    464463 
     
    467466    Purpose:    return true if Redirection status code 
    468467\*=======================================================================*/ 
    469 function is_redirect ($sc) {  
    470     return $sc >= 300 && $sc < 400;  
     468function is_redirect ($sc) { 
     469    return $sc >= 300 && $sc < 400; 
    471470} 
    472471 
     
    475474    Purpose:    return true if Error status code 
    476475\*=======================================================================*/ 
    477 function is_error ($sc) {  
    478     return $sc >= 400 && $sc < 600;  
     476function is_error ($sc) { 
     477    return $sc >= 400 && $sc < 600; 
    479478} 
    480479 
     
    483482    Purpose:    return true if Error status code, and its a client error 
    484483\*=======================================================================*/ 
    485 function is_client_error ($sc) {  
    486     return $sc >= 400 && $sc < 500;  
     484function is_client_error ($sc) { 
     485    return $sc >= 400 && $sc < 500; 
    487486} 
    488487 
     
    491490    Purpose:    return true if Error status code, and its a server error 
    492491\*=======================================================================*/ 
    493 function is_server_error ($sc) {  
    494     return $sc >= 500 && $sc < 600;  
     492function is_server_error ($sc) { 
     493    return $sc >= 500 && $sc < 600; 
    495494} 
    496495