OpenWetWare:Software/Extensions/RSS/XFeed

From OpenWetWare
Jump to navigationJump to search

The following information comes from Wikimedia: Wikimedia XFeed page


Integrate wikimedia:RSS feeds into wiki pages using magpieRSS (a wikimedia:PHP wikimedia:RSS parser class) in a custom wikimedia:Mediawiki extension:

I needed a quick way to collect the feeds from my users Wordpress blogs onto the front page of my MediaWiki installation. This plugin does no caching, and will grab the feed on every request.

I'm not a PHP developer and would welcome input to improve the code and output (I'm sure I've made a mess of it).

--Brill 04:55, 15 August 2006 (UTC)

Syntax

<xfeeds [titlecolour="#B0C4DE"] [contentcolour="#eeeeee"] [feedlimit="1"] [totallimit="10"]>
http://feed-url-1
http://feed-url-2
</xfeeds>

Note: The attributes are optional and some basic defaults will be used if none are specified.

Example

<xfeeds contentcolour="#eeeeee" feedlimit="3" totallimit="10">
http://www.digg.com/rss/containertechnology.xml
http://rss.slashdot.org/Slashdot/slashdot
http://brill.pappin.ca/wp-rss2.php
</xfeeds>

Result

A list of items sorted by date and including title, content, date and feed channel.

"Pirillo and LaPorte hope to attract a sponsor and begin producing original content, but for now will let the site evolve from the ground up. Contributors will keep 100 percent control of the content they provide to the site, Pirillo wrote."
2006-08-15 00:55:56 EDT (digg / Technology)
A unique amalgam of Linux mail server and open source development platform, the Zimbra Collaboration Suite (ZCS) combines truly integrated e-mail, calendaring, and contacts with a mashup-friendly AJAX client and easy extensibility using Zimlets.

Source

Note: The default location for you to install magpie rss is in your includes folder; if you have installed it somewhere else then you will have to change the XFeeds code from require_once('magpierss/rss_fetch.inc'); to wherever you installed it.

Add an XFeeds.php include to you LocalSettings.php like every other extension.

<?php

require_once('magpierss/rss_fetch.inc');

# Oringinal Author: http://meta.wikimedia.org/wiki/User:Brill
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/XFeeds.php");

$wgExtensionFunctions[] = "wfXFeeds";

$wgExtensionCredits['parserhook'][] = array(
    'name'=>'XFeeds',
    'author'=>'[http://meta.wikimedia.org/wiki/User:Brill Brill]',
    'url'=>'http://meta.wikimedia.org/w/index.php?title=XFeed_-_RSS_Feed_Aggregator',
    'description' => 'RSS feed aggregator'
    );

function wfXFeeds() {
    global $wgParser;
    $wgParser->setHook( "xfeeds", "renderFeeds" );
}

# The callback function for converting the input text to HTML output
function renderFeeds( $input, $argv, &$parser ) {
        $parser->disableCache();

        $contentcolour = getParameter($argv, "contentcolour", "#EEEEEE");
        $titlecolour = getParameter($argv, "titlecolour", "#B0C4DE");
        $feedlimit = getParameter($argv, "feedlimit", 0);
        $totallimit = getParameter($argv, "totallimit", 10);

        $feedItems = array();

        $feedUrls = explode("\n",$input);
        #$output = "";
        foreach ($feedUrls as $url){
                if(strlen($url) > 0){
                        $feed = fetch_rss( $url );
                        $channel_title = $feed->channel['title'];
                        $channel_link = $feed->channel['link'];
                        $itemCount = 1;
                        foreach ($feed->items as $item) {
                                $href = $item['link'];
                                $title = $item['title'];
                                $description = $item['description'];
                                $date = $item['pubdate'];
                                $timestamp = 0;
                                if($date == ""){
                                        $date = $item['dc']['date'];
                                        if($date != ""){
                                                $timestamp = pubDateToTimestamp($date);
                                        }
                                }else{
                                        $timestamp = pubDateToTimestamp($date);
                                }
                                $tStampStr = $date;
                                if($timestamp > 0){
                                        $tStampStr = formatDateTimestamp($timestamp);
                                }

$outputItem = <<<EOF
<div style="border: 1px solid $titlecolour; background-color: $contentcolour; width: 100%; margin: 2px 0px 2px 0px;">
        <div style="padding: 2px 2px 2px 2px; margin: 0px 0px 0px 0px;; font-weight: bold; font-size: 14px; background-color: $titlecolour; width: 100%;">
                <div><a href="$href">$title</a></div>
                <div style="font-weight: normal; font-size: 12px; color: Gray; font-style: italic; float: right; margin: 0px 4px 0px 2px; border-bottom-style: solid; border-bottom-width: 1px; border-bottom-color: $titlecolour;">$tStampStr [<a href="$channel_link">$channel_title</a>]</div>
        </div>
        <div style="padding: 2px 2px 2px 15px; margin: 0px 0px 0px 0px;">$description</div>
</div>
EOF;

                                $feedItems[$timestamp] = $outputItem;
                                $itemCount = $itemCount + 1;
                                if($itemCount > $feedlimit){
                                        break;
                                }
                        }
                }
        }

        krsort($feedItems, SORT_NUMERIC);
        $output = "";
        $outCount = 0;
        foreach($feedItems as $tsKey => $itemValue){
                $output .= $itemValue."\n";
                $outCount = $outCount + 1;
                if($outCount > $totallimit){
                        break;
                }
        }


    return $output;
}

function getParameter($argArray, $key, $defaultValue){
        $value = $argArray[$key];
        if(isset($value)){
                return $value;
        }else{
                return $defaultValue;
        }
}

function pubDateToTimestamp($dateStr){
        $tstamp = strtotime($dateStr,0);
        return $tstamp;
}

function formatDateTimestamp($tstamp){
        $result = strftime("%Y-%m-%d %T %Z", $tstamp);
        return $result;
}
?>
   

Change Log

  • 2007-02-02
    • Fixed typo in extension credits.
  • 2007-01-25
    • Changed back to "$feed->items" per magpierss 0.72 documentation. I'm not sure what the mentioned php error was, but the change is what was causing the blank page problems.
    • Added parserhook credits
Kitzke 20:50, 25 January 2007 (UTC)
  • 2006-09-11 - Changed "$feed->items" to "$feedItems" because that's what it's supposed to be (Fixes a php error.)
Also changed the place that magpie is grabbed from to be the includes or current directory, because that's the recommended location. Dantman
  • 2006-08-30 - Changed html output method - Thanks Mark F.

Known Issues

  • several feed item data formats are not parsed correctly, which causes only the last one to be added.
    • I've seen this before with date formats, but haven't had to fix them yet. Brill 06:33, 2 February 2007 (UTC)

Sites Using This Extension

Go ahead, add your site so we can all see the extension in action.

I would love to use it here, but need some help!:

wikimedia:Category:MediaWiki extensions wikimedia:Category:RSS Extensions