mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/RSS
synced 2024-11-24 15:54:06 +00:00
3aa74a03f8
* Add function documentation that I should have written before. * Create separate RSSParser class to clean up RSS.php * Create separate RSSHooks class to handle parser initialization and public interface for parser to use. * Move User Agent definition to $wgRSSUserAgent from a define. * Eliminate references to $wgRSSUseGzip (since were using MW's internal HTTP client, it didn't do anything anyway, ATM). Re Tim's emailed review (discovered yesterday): * Switch to DOMXPath::query() from DOMXPath::evaluate() at Tim's suggestion. * Move highlighting callback to its own class, RSSHighlighter, so that it works as a callback without create_function() and other mess.
81 lines
2.1 KiB
PHP
81 lines
2.1 KiB
PHP
<?php
|
|
|
|
class RSSData {
|
|
public $ERROR;
|
|
public $items;
|
|
|
|
/**
|
|
* Constructor, takes a DOMDocument and returns an array of parsed items.
|
|
* @param DOMDocument The pre-parsed XML Document
|
|
* @returns Object RSSData object with a member items that is an array of parsed items,
|
|
*/
|
|
function __construct( $xml ) {
|
|
if ( !( $xml instanceOf DOMDocument ) ) {
|
|
$this->ERROR = "Not passed DOMDocument object.";
|
|
return;
|
|
}
|
|
$xpath = new DOMXPath( $xml );
|
|
$items = $xpath->query( '/rss/channel/item' );
|
|
|
|
if($items->length !== 0) {
|
|
foreach ( $items as $item ) {
|
|
$bit = array();
|
|
foreach ( $item->childNodes as $n ) {
|
|
$name = $this->rssTokenToName( $n->nodeName );
|
|
if ( $name != null ) {
|
|
/* Because for DOMElements the nodeValue is just
|
|
* the text of the containing element, without any
|
|
* tags, it makes this a safe, if unattractive,
|
|
* value to use. If you want to allow people to
|
|
* mark up their RSS, some more precautions are
|
|
* needed. */
|
|
$bit[$name] = $n->nodeValue;
|
|
}
|
|
}
|
|
$this->items[] = $bit;
|
|
}
|
|
} else {
|
|
$this->ERROR = "No RSS items found.";
|
|
return;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Return a string that will be used to map RSS elements that
|
|
* contain similar data (e.g. dc:date, date, and pubDate) to the
|
|
* same array key. This works on WordPress feeds as-is, but it
|
|
* probably needs a way to concert dc:date format dates to be the
|
|
* same as pubDate.
|
|
* @param String $elementName Name of the element we have
|
|
* @returns String Name to map it to
|
|
*/
|
|
protected function rssTokenToName( $n ) {
|
|
switch( $n ) {
|
|
case 'dc:date':
|
|
return 'date';
|
|
# parse "2010-10-18T18:07:00Z"
|
|
case 'pubDate':
|
|
return 'date';
|
|
# parse RFC date
|
|
case 'dc:creator':
|
|
return 'author';
|
|
case 'title':
|
|
return 'title';
|
|
case 'content:encoded':
|
|
return 'encodedContent';
|
|
|
|
case 'slash:comments':
|
|
case 'slash:department':
|
|
case 'slash:section':
|
|
case 'slash:hit_parade':
|
|
case 'feedburner:origLink':
|
|
case 'wfw:commentRss':
|
|
case 'comments':
|
|
case 'category':
|
|
return null;
|
|
|
|
default:
|
|
return $n;
|
|
}
|
|
}
|
|
} |