Changeset 3517 for trunk/wp-includes/rss-functions.php
- Timestamp:
- 02/12/2006 07:53:23 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/rss-functions.php
r3083 r3517 31 31 var $current_field = ''; 32 32 var $current_namespace = false; 33 33 34 34 //var $ERROR = ""; 35 35 36 36 var $_CONTENT_CONSTRUCTS = array('content', 'summary', 'info', 'title', 'tagline', 'copyright'); 37 37 38 38 function MagpieRSS ($source) { 39 39 40 40 # if PHP xml isn't compiled in, die 41 41 # 42 42 if ( !function_exists('xml_parser_create') ) 43 43 trigger_error( "Failed to load PHP's XML Extension. http://www.php.net/manual/en/ref.xml.php" ); 44 44 45 45 $parser = @xml_parser_create(); 46 46 47 47 if ( !is_resource($parser) ) 48 48 trigger_error( "Failed to create an instance of PHP's XML parser. http://www.php.net/manual/en/ref.xml.php"); 49 49 50 50 51 51 $this->parser = $parser; 52 52 53 53 # pass in parser, and a reference to this object 54 54 # setup handlers … … 57 57 xml_set_element_handler($this->parser, 58 58 'feed_start_element', 'feed_end_element' ); 59 59 60 60 xml_set_character_data_handler( $this->parser, 'feed_cdata' ); 61 61 62 62 $status = xml_parse( $this->parser, $source ); 63 63 64 64 if (! $status ) { 65 65 $errorcode = xml_get_error_code( $this->parser ); … … 73 73 } 74 74 } 75 75 76 76 xml_parser_free( $this->parser ); 77 77 78 78 $this->normalize(); 79 79 } 80 80 81 81 function feed_start_element($p, $element, &$attrs) { 82 82 $el = $element = strtolower($element); 83 83 $attrs = array_change_key_case($attrs, CASE_LOWER); 84 84 85 85 // check for a namespace, and split if found 86 86 $ns = false; … … 91 91 $this->current_namespace = $ns; 92 92 } 93 93 94 94 # if feed type isn't set, then this is first element of feed 95 95 # identify feed from root element … … 111 111 return; 112 112 } 113 113 114 114 if ( $el == 'channel' ) 115 115 { … … 120 120 $this->initem = true; 121 121 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 126 126 // if we're in the default namespace of an RSS feed, 127 127 // record textinput or image fields … … 133 133 $this->intextinput = true; 134 134 } 135 135 136 136 elseif ( 137 137 $this->feed_type == RSS and … … 141 141 $this->inimage = true; 142 142 } 143 143 144 144 # handle atom content constructs 145 145 elseif ( $this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) ) … … 149 149 $el = 'atom_content'; 150 150 } 151 151 152 152 $this->incontent = $el; 153 154 155 } 156 153 154 155 } 156 157 157 // if inside an Atom content construct (e.g. content or summary) field treat tags as text 158 158 elseif ($this->feed_type == ATOM and $this->incontent ) … … 163 163 array_keys($attrs), 164 164 array_values($attrs) ) ); 165 165 166 166 $this->append_content( "<$element $attrs_str>" ); 167 167 168 168 array_unshift( $this->stack, $el ); 169 169 } 170 170 171 171 // Atom support many links per containging element. 172 172 // Magpie treats link elements of type rel='alternate' … … 182 182 $link_el = 'link_' . $attrs['rel']; 183 183 } 184 184 185 185 $this->append($link_el, $attrs['href']); 186 186 } … … 190 190 } 191 191 } 192 193 194 192 193 194 195 195 function feed_cdata ($p, $text) { 196 196 197 197 if ($this->feed_type == ATOM and $this->incontent) 198 198 { … … 204 204 } 205 205 } 206 206 207 207 function feed_end_element ($p, $el) { 208 208 $el = strtolower($el); 209 209 210 210 if ( $el == 'item' or $el == 'entry' ) 211 211 { … … 223 223 } 224 224 elseif ($this->feed_type == ATOM and in_array($el, $this->_CONTENT_CONSTRUCTS) ) 225 { 225 { 226 226 $this->incontent = false; 227 227 } … … 246 246 array_shift( $this->stack ); 247 247 } 248 248 249 249 $this->current_namespace = false; 250 250 } 251 251 252 252 function concat (&$str1, $str2="") { 253 253 if (!isset($str1) ) { … … 256 256 $str1 .= $str2; 257 257 } 258 258 259 259 function append_content($text) { 260 260 if ( $this->initem ) { … … 265 265 } 266 266 } 267 267 268 268 // smart append - field and namespace aware 269 269 function append($el, $text) { … … 307 307 $this->channel[ $el ], $text ); 308 308 } 309 310 } 311 } 312 309 310 } 311 } 312 313 313 function normalize () { 314 314 // if atom populate rss fields … … 321 321 if ( isset($item['atom_content'])) 322 322 $item['content']['encoded'] = $item['atom_content']; 323 323 324 324 $this->items[$i] = $item; 325 } 325 } 326 326 } 327 327 elseif ( $this->is_rss() ) { … … 333 333 if ( isset($item['content']['encoded'] ) ) 334 334 $item['atom_content'] = $item['content']['encoded']; 335 335 336 336 $this->items[$i] = $item; 337 337 } 338 338 } 339 339 } 340 340 341 341 function is_rss () { 342 342 if ( $this->feed_type == RSS ) { 343 return $this->feed_version; 343 return $this->feed_version; 344 344 } 345 345 else { … … 347 347 } 348 348 } 349 349 350 350 function is_atom() { 351 351 if ( $this->feed_type == ATOM ) { … … 379 379 // initialize constants 380 380 init(); 381 381 382 382 if ( !isset($url) ) { 383 383 // error("fetch_rss called without a url"); 384 384 return false; 385 385 } 386 386 387 387 // if cache is disabled 388 388 if ( !MAGPIE_CACHE_ON ) { … … 404 404 // 3. if cached obj fails freshness check, fetch remote 405 405 // 4. if remote fails, return stale object, or error 406 406 407 407 $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE ); 408 408 409 409 if (MAGPIE_DEBUG and $cache->ERROR) { 410 410 debug($cache->ERROR, E_USER_WARNING); 411 411 } 412 413 412 413 414 414 $cache_status = 0; // response of check_cache 415 415 $request_headers = array(); // HTTP headers to send with fetch 416 416 $rss = 0; // parsed RSS object 417 417 $errormsg = 0; // errors, if any 418 418 419 419 if (!$cache->ERROR) { 420 420 // return cache HIT, MISS, or STALE … … 433 433 } 434 434 } 435 435 436 436 // else attempt a conditional get 437 437 438 438 // setup headers 439 439 if ( $cache_status == 'STALE' ) { … … 444 444 } 445 445 } 446 446 447 447 $resp = _fetch_remote_file( $url, $request_headers ); 448 448 449 449 if (isset($resp) and $resp) { 450 450 if ($resp->status == '304' ) { … … 484 484 $errormsg = "Unable to retrieve RSS file for unknown reasons."; 485 485 } 486 486 487 487 // else fetch failed 488 488 489 489 // attempt to return cached object 490 490 if ($rss) { … … 494 494 return $rss; 495 495 } 496 496 497 497 // else we totally failed 498 // error( $errormsg ); 499 498 // error( $errormsg ); 499 500 500 return false; 501 501 502 502 } // end if ( !MAGPIE_CACHE_ON ) { 503 503 } // end fetch_rss() … … 512 512 $client->rawheaders = $headers; 513 513 } 514 514 515 515 @$client->fetch($url); 516 516 return $client; … … 520 520 function _response_to_rss ($resp) { 521 521 $rss = new MagpieRSS( $resp->results ); 522 523 // if RSS parsed successfully 522 523 // if RSS parsed successfully 524 524 if ( $rss and !$rss->ERROR) { 525 525 526 526 // find Etag, and Last-Modified 527 527 foreach($resp->headers as $h) { … … 534 534 $val = ""; 535 535 } 536 536 537 537 if ( $field == 'ETag' ) { 538 538 $rss->etag = $val; 539 539 } 540 540 541 541 if ( $field == 'Last-Modified' ) { 542 542 $rss->last_modified = $val; 543 543 } 544 544 } 545 546 return $rss; 545 546 return $rss; 547 547 } // else construct error message 548 548 else { 549 549 $errormsg = "Failed to parse RSS file."; 550 550 551 551 if ($rss) { 552 552 $errormsg .= " (" . $rss->ERROR . ")"; 553 553 } 554 554 // error($errormsg); 555 555 556 556 return false; 557 557 } // end if ($rss and !$rss->error) … … 570 570 define('MAGPIE_INITALIZED', 1); 571 571 } 572 572 573 573 if ( !defined('MAGPIE_CACHE_ON') ) { 574 574 define('MAGPIE_CACHE_ON', 1); … … 586 586 define('MAGPIE_CACHE_FRESH_ONLY', 0); 587 587 } 588 588 589 589 if ( !defined('MAGPIE_DEBUG') ) { 590 590 define('MAGPIE_DEBUG', 0); … … 593 593 if ( !defined('MAGPIE_USER_AGENT') ) { 594 594 $ua = 'WordPress/' . $wp_version; 595 595 596 596 if ( MAGPIE_CACHE_ON ) { 597 597 $ua = $ua . ')'; … … 600 600 $ua = $ua . '; No cache)'; 601 601 } 602 602 603 603 define('MAGPIE_USER_AGENT', $ua); 604 604 } 605 605 606 606 if ( !defined('MAGPIE_FETCH_TIME_OUT') ) { 607 607 define('MAGPIE_FETCH_TIME_OUT', 2); // 2 second timeout 608 608 } 609 609 610 610 // use gzip encoding to fetch rss files if supported? 611 611 if ( !defined('MAGPIE_USE_GZIP') ) { 612 define('MAGPIE_USE_GZIP', true); 612 define('MAGPIE_USE_GZIP', true); 613 613 } 614 614 } … … 642 642 var $MAX_AGE = 43200; // when are files stale, default twelve hours 643 643 var $ERROR = ''; // accumulate error messages 644 644 645 645 function RSSCache ($base='', $age='') { 646 646 if ( $base ) { … … 650 650 $this->MAX_AGE = $age; 651 651 } 652 653 } 654 652 653 } 654 655 655 /*=======================================================================*\ 656 656 Function: set 657 657 Purpose: add an item to the cache, keyed on url 658 658 Input: url from wich the rss file was fetched 659 Output: true on sucess 659 Output: true on sucess 660 660 \*=======================================================================*/ 661 661 function set ($url, $rss) { … … 663 663 $cache_option = 'rss_' . $this->file_name( $url ); 664 664 $cache_timestamp = 'rss_' . $this->file_name( $url ) . '_ts'; 665 665 666 666 if ( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$cache_option'") ) 667 667 add_option($cache_option, '', '', 'no'); 668 668 if ( !$wpdb->get_var("SELECT option_name FROM $wpdb->options WHERE option_name = '$cache_timestamp'") ) 669 669 add_option($cache_timestamp, '', '', 'no'); 670 670 671 671 update_option($cache_option, $rss); 672 672 update_option($cache_timestamp, time() ); 673 673 674 674 return $cache_option; 675 675 } 676 676 677 677 /*=======================================================================*\ 678 678 Function: get 679 679 Purpose: fetch an item from the cache 680 680 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 \*=======================================================================*/ 683 683 function get ($url) { 684 684 $this->ERROR = ""; 685 685 $cache_option = 'rss_' . $this->file_name( $url ); 686 686 687 687 if ( ! get_option( $cache_option ) ) { 688 688 $this->debug( … … 691 691 return 0; 692 692 } 693 693 694 694 $rss = get_option( $cache_option ); 695 695 696 696 return $rss; 697 697 } … … 702 702 and whether the object is older then MAX_AGE (ie. STALE) 703 703 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 \*=======================================================================*/ 706 706 function check_cache ( $url ) { 707 707 $this->ERROR = ""; … … 730 730 /*=======================================================================*\ 731 731 Function: serialize 732 \*=======================================================================*/ 732 \*=======================================================================*/ 733 733 function serialize ( $rss ) { 734 734 return serialize( $rss ); … … 737 737 /*=======================================================================*\ 738 738 Function: unserialize 739 \*=======================================================================*/ 739 \*=======================================================================*/ 740 740 function unserialize ( $data ) { 741 741 return unserialize( $data ); 742 742 } 743 743 744 744 /*=======================================================================*\ 745 745 Function: file_name … … 747 747 Input: url from wich the rss file was fetched 748 748 Output: a file name 749 \*=======================================================================*/ 749 \*=======================================================================*/ 750 750 function file_name ($url) { 751 751 return md5( $url ); 752 752 } 753 753 754 754 /*=======================================================================*\ 755 755 Function: error 756 756 Purpose: register error 757 \*=======================================================================*/ 757 \*=======================================================================*/ 758 758 function error ($errormsg, $lvl=E_USER_WARNING) { 759 759 // append PHP's error message if track_errors enabled … … 777 777 778 778 function parse_w3cdtf ( $date_str ) { 779 779 780 780 # regex to match wc3dtf 781 781 $pat = "/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/"; 782 782 783 783 if ( preg_match( $pat, $date_str, $match ) ) { 784 784 list( $year, $month, $day, $hours, $minutes, $seconds) = 785 785 array( $match[1], $match[2], $match[3], $match[4], $match[5], $match[6]); 786 786 787 787 # calc epoch for current date assuming GMT 788 788 $epoch = gmmktime( $hours, $minutes, $seconds, $month, $day, $year); 789 789 790 790 $offset = 0; 791 791 if ( $match[10] == 'Z' ) { … … 795 795 list( $tz_mod, $tz_hour, $tz_min ) = 796 796 array( $match[8], $match[9], $match[10]); 797 797 798 798 # zero out the variables 799 799 if ( ! $tz_hour ) { $tz_hour = 0; } 800 800 if ( ! $tz_min ) { $tz_min = 0; } 801 801 802 802 $offset_secs = (($tz_hour*60)+$tz_min)*60; 803 803 804 804 # is timezone ahead of GMT? then subtract offset 805 805 # … … 807 807 $offset_secs = $offset_secs * -1; 808 808 } 809 810 $offset = $offset_secs; 809 810 $offset = $offset_secs; 811 811 } 812 812 $epoch = $epoch + $offset; … … 830 830 echo "</a><br />\n"; 831 831 echo "</li>\n"; 832 } 832 } 833 833 echo "</ul>"; 834 834 }
Note: See TracChangeset
for help on using the changeset viewer.