Difference between revisions of "Extension:SimpleRSS.php"

From Organic Design wiki
(updated: need to figure ot dom stuff)
(rm trailing ?>)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
<?php
 
<?php
# Extension:RobsRSS{{Category:Extensions|RobsRSS}}{{php}}
+
# Extension:SimpleRSS{{Category:Extensions|SimpleRSS}}{{php}}
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
# - Author: [http://www.organicdesign.co.nz/nad User:Rob]{{Category:Extensions created with Template:Extension}}
+
# - Author: [http://www.organicdesign.co.nz/Rob User:Rob]{{Category:Extensions created with Template:Extension}}
 +
 +
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 +
 +
define('SIMPLERSS_VERSION','1.0.0, 2007-05-05');
 +
 +
$wgSimpleRSSMagic = "rss";
 +
$wgSimpleRSSTag = "rss";
 +
$wgExtensionFunctions[] = 'wfSetupSimpleRSS';
 +
$wgHooks['LanguageGetMagic'][] = 'wfSimpleRSSLanguageGetMagic';
 +
 +
$wgExtensionCredits['parserhook'][] = array(
 +
'name' => 'SimpleRSS',
 +
'author' => '[http://www.organicdesign.co.nz/nad User:Rob]',
 +
'description' => 'Integration of RSS with MediaWiki',
 +
'url' => 'http://www.organicdesign.co.nz/Extension:SimpleRSS',
 +
'version' => SIMPLERSS_VERSION
 +
);
 +
 
 +
require_once('extensions/SimpleRSS/magpierss-0.72/rss_fetch.inc');
 +
 
 +
class SimpleRSS {
 +
 +
# Properties
 +
var $prop1 = 'default value';
 +
var $prop2 = 'default value';
 +
 +
# Constructor
 +
function SimpleRSS() {
 +
global $wgHooks,$wgParser,$wgSimpleRSSMagic,$wgSimpleRSSTag;
 +
 +
# Add the parser-function
 +
$wgParser->setFunctionHook($wgSimpleRSSMagic,array($this,'magicRss'));
 +
 +
# Add the tagHook
 +
$wgParser->setHook($wgSimpleRSSTag,array($this,'tagRss'));
 +
 +
}
  
if (!defined('MEDIAWIKI')) die('Not an entry point.');
+
# Convert the <rss> tags to HTML
 +
function tagRss($text,$argv,&$parser) {
 +
$args = '';
 +
foreach ($argv as $k => $v) $args .= "<li><b>$k:</b> <i>$v</i></li>\n";
 +
return "<h3>tagRss():</h3>\n<ul>$args<li>Content:</b> $text</ul>\n";
 +
}
 +
 
 +
# Needed in some versions to prevent Special:Version from breaking
 +
function __toString() { return 'SimpleRSS'; }
 +
 
 +
# Expand the rss-magic
 +
function magicRss(&$parser) {
 +
 
 +
# Populate $argv with both named and numeric parameters
 +
$argv = array();
 +
foreach (func_get_args() as $arg) if (!is_object($arg)) {
 +
if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg;
 +
}
 +
 
 +
global $wgRSSUseAjax;
 +
 
 +
if ($wgRSSUseAjax) {
 +
# Render a container which does ajax requests for content
 +
$text = "<div id=foo> an ajax thingie </div>";
 +
}
 +
else {
 +
# Not using ajax, render the output now
 +
 
 +
# TODO enforce required params: url template
 +
$error = "fatal error: no template provided";
 +
$error = "fatal error: no url provided";
 +
 
 +
# template params
 +
$headTemplate = "RSSHead";
 +
$itemTemplate = "RSSItem";
 +
$footTemplate = "RSSFoot";
 +
 
 +
# grab each url and process
 +
$data = wfReadRSS(preg_split("/[\n\r]/", $argv[url], -1, PREG_SPLIT_NO_EMPTY));
 +
 
 +
# dump data
 +
foreach ($data as $v) {
 +
# create head template
 +
wfListArrayKeys($v->channel, "", $channel);
 +
$text .= wfMakeTemplate($headTemplate, $channel);
 +
 
 +
# loop creating item templates
 +
foreach($v->items as $item) {
 +
wfListArrayKeys($item, "", $itemResult);
 +
$text .= wfMakeTemplate($itemTemplate, $itemResult);
 +
}
 +
 
 +
# create foot template (same as head)
 +
wfListArrayKeys($v->channel, "", $channel);
 +
$text .= wfMakeTemplate($footTemplate, $channel);
 +
 
 +
}
 +
 +
}
 +
 
 +
# Return result with available parser flags
 +
return array(
 +
$text,
 +
found => true,
 +
nowiki => false,
 +
noparse => false,
 +
noargs => false,
 +
isHTML => false
 +
);
  
define('ROBSRSS_VERSION','0.0.0, 2007-10-14');
+
}
  
$wgRobsRSSTag                    = "rss";
 
$wgRSSUseAjax                    = false;
 
$wgExtensionFunctions[]          = 'wfSetupRobsRSS';
 
$wgExtensionCredits['parserhook'][] = array(
 
        'name'        => 'RobsRSS',
 
        'author'      => '[http://www.organicdesign.co.nz/User:Rob User:Rob]',
 
        'description' => 'A dynamic aggregating RSS reader',
 
        'url'        => 'http://www.mediawiki.org/wiki/Extension:RobsRSS',
 
        'version'    => ROBSRSS_VERSION
 
        );
 
  
  
# Called from $wgExtensionFunctions array when initialising extensions
+
} # object end
function wfSetupRobsRSS() {
 
        global $wgParser,$wgRSSUseAjax,$wgAjaxExportList,$wgRobsRSSTag;
 
        $wgParser->setHook($wgRobsRSSTag,'wfRssTagHook');
 
        if ($wgRSSUseAjax) $wgAjaxExportList[] = 'wfRenderRSS'; # Allow Ajax Dispatcher to call wfRenderRSS
 
        }
 
  
# Expand an <rss> tag
 
function wfRssTagHook($text,$argv,&$parser) {
 
        global $wgRSSUseAjax;
 
        $args = '';
 
        foreach ($argv as $k => $v) $args .= "<li><b>$k:</b> <i>$v</i></li>\n";
 
  
        if ($wgRSSUseAjax) {
+
# build array of key-value pairs into template syntax
                # Render a container which does ajax requests for content
+
# {{TemplateName|title=foo|link=http://foo.com|description=Hello!}}
                $html = "<div id=foo> an ajax thingie </div>";
+
function wfMakeTemplate($template, $array) {
                }
+
foreach($array as $key => $value)
        else {
+
$params .= "\n|".$key."=".$value;
                # Not using ajax, render the output now
+
return "{{".$template.$params."}}\n";
                $html = wfRenderRSS($data,$timestamp);
+
}
                }
 
        # grab each url and process
 
        $data = wfReadRSS(preg_split("/[\n\r]/", $text, -1, PREG_SPLIT_NO_EMPTY));
 
  
        # dump data       
 
        foreach ($data as $k => $v) $html .= "<li><b>$k:</b> <i>$v</i></li>\n";
 
       
 
        return $html;
 
        }
 
  
# Render a portion of RSS data (which is in the useful format returned by wfReadRSS)
+
# build array of keys to publish (it's recursive!)
function wfRenderRSS($data,$timestamp = false) {
+
# WARNING because this function uses a hash to eliminate duplicate keys
        return $html;
+
# it no good for lists of items, only one at a time.
        }
+
function wfListArrayKeys($array, $parent="", &$result) {
 +
foreach($array as $key => $value) {
 +
if(is_array($value))
 +
wfListArrayKeys($value, "$parent$key", $result);
 +
else
 +
$result[$parent.$key] = $value;
 +
}
 +
}
  
# Read RSS from URL's and return as some kind of useful runtime structure
 
 
function wfReadRSS($urls) {
 
function wfReadRSS($urls) {
        $data = array();
+
$data = array();
        foreach ($urls as $url) {
+
foreach ($urls as $url) {
                $doc = new DOMDocument();
+
$data[] = fetch_rss($url);
                # do some cache stuff here
+
}
                $doc->load($url);
+
return $data;
                # get feed details
+
}
                $feed=$doc->getElementsByTagName('channel')->item(0);
 
        $feed_title = $feed->getElementsByTagName('title')
 
        ->item(0)->childNodes->item(0)->nodeValue;
 
        $feed_link = $feed->getElementsByTagName('link')
 
        ->item(0)->childNodes->item(0)->nodeValue;
 
        $feed_desc = $feed->getElementsByTagName('description')
 
        ->item(0)->childNodes->item(0)->nodeValue;
 
                $data[] = $feed;
 
  
/*                foreach ($doc->getElementsByTagName('item') as $item) {
 
                # what follows looks overly complicated? simplify?
 
                $item_title=$item->getElementsByTagName('title')->nodeValue;
 
                $item_link=$doc->item($i)->getElementsByTagName('link')
 
                ->item(0)->childNodes->item(0)->nodeValue;
 
                $item_desc=$doc->item($i)->getElementsByTagName('description')
 
                ->item(0)->childNodes->item(0)->nodeValue;
 
                $item_date=$doc->item($i)->getElementsByTagName('pubDate')
 
                ->item(0)->childNodes->item(0)->nodeValue;
 
                # <pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
 
                $data[] = "<p>$item_title: $item_link $item_desc $item_date";
 
  
            # add collected info into $data
+
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly
            # TODO need to grab date information so we can sort
+
function wfSimpleRSSLanguageGetMagic(&$magicWords,$langCode = 0) {
            # Date is in UDF from memory
+
global $wgSimpleRSSMagic;
            }*/
+
$magicWords[$wgSimpleRSSMagic] = array(0,$wgSimpleRSSMagic);
                }
+
return true;
        return $data;
+
}
        }
 
  
?>
+
# Called from $wgExtensionFunctions array when initialising extensions
 +
function wfSetupSimpleRSS() {
 +
global $wgSimpleRSS;
 +
$wgSimpleRSS = new SimpleRSS();
 +
}

Latest revision as of 19:49, 28 December 2008

<?php

  1. Extension:SimpleRSS
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:Php

  1. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  2. - Author: User:RobCategory:Extensions created with Template:Extension

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('SIMPLERSS_VERSION','1.0.0, 2007-05-05');

$wgSimpleRSSMagic = "rss"; $wgSimpleRSSTag = "rss"; $wgExtensionFunctions[] = 'wfSetupSimpleRSS'; $wgHooks['LanguageGetMagic'][] = 'wfSimpleRSSLanguageGetMagic';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'SimpleRSS', 'author' => 'User:Rob', 'description' => 'Integration of RSS with MediaWiki', 'url' => 'http://www.organicdesign.co.nz/Extension:SimpleRSS', 'version' => SIMPLERSS_VERSION );

require_once('extensions/SimpleRSS/magpierss-0.72/rss_fetch.inc');

class SimpleRSS {

# Properties var $prop1 = 'default value'; var $prop2 = 'default value';

# Constructor function SimpleRSS() { global $wgHooks,$wgParser,$wgSimpleRSSMagic,$wgSimpleRSSTag;

# Add the parser-function $wgParser->setFunctionHook($wgSimpleRSSMagic,array($this,'magicRss'));

# Add the tagHook $wgParser->setHook($wgSimpleRSSTag,array($this,'tagRss'));

}

# Convert the <rss> tags to HTML function tagRss($text,$argv,&$parser) { $args = ;

foreach ($argv as $k => $v) $args .= "

  • $k: $v
  • \n"; return "

    tagRss():

    \n

      $args
    • Content: $text

    \n";

    }

    # Needed in some versions to prevent Special:Version from breaking function __toString() { return 'SimpleRSS'; }

    # Expand the rss-magic function magicRss(&$parser) {

    # Populate $argv with both named and numeric parameters $argv = array(); foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]] = $match[2]; else $argv[] = $arg; }

    global $wgRSSUseAjax;

    if ($wgRSSUseAjax) { # Render a container which does ajax requests for content

    $text = "

    an ajax thingie

    ";

    } else { # Not using ajax, render the output now

    # TODO enforce required params: url template $error = "fatal error: no template provided"; $error = "fatal error: no url provided";

    # template params $headTemplate = "RSSHead"; $itemTemplate = "RSSItem"; $footTemplate = "RSSFoot";

    # grab each url and process $data = wfReadRSS(preg_split("/[\n\r]/", $argv[url], -1, PREG_SPLIT_NO_EMPTY));

    # dump data foreach ($data as $v) { # create head template wfListArrayKeys($v->channel, "", $channel); $text .= wfMakeTemplate($headTemplate, $channel);

    # loop creating item templates foreach($v->items as $item) { wfListArrayKeys($item, "", $itemResult); $text .= wfMakeTemplate($itemTemplate, $itemResult); }

    # create foot template (same as head) wfListArrayKeys($v->channel, "", $channel); $text .= wfMakeTemplate($footTemplate, $channel);

    }

    }

    # Return result with available parser flags return array( $text, found => true, nowiki => false, noparse => false, noargs => false, isHTML => false );

    }


    } # object end


    1. build array of key-value pairs into template syntax
    2. Template:TemplateName

    function wfMakeTemplate($template, $array) { foreach($array as $key => $value) $params .= "\n|".$key."=".$value; return "Template:".$template.$params."\n"; }


    1. build array of keys to publish (it's recursive!)
    2. WARNING because this function uses a hash to eliminate duplicate keys
    3. it no good for lists of items, only one at a time.

    function wfListArrayKeys($array, $parent="", &$result) { foreach($array as $key => $value) { if(is_array($value)) wfListArrayKeys($value, "$parent$key", $result); else $result[$parent.$key] = $value; } }

    function wfReadRSS($urls) { $data = array(); foreach ($urls as $url) { $data[] = fetch_rss($url); } return $data; }


    1. Needed in MediaWiki >1.8.0 for magic word hooks to work properly

    function wfSimpleRSSLanguageGetMagic(&$magicWords,$langCode = 0) { global $wgSimpleRSSMagic; $magicWords[$wgSimpleRSSMagic] = array(0,$wgSimpleRSSMagic); return true; }

    1. Called from $wgExtensionFunctions array when initialising extensions

    function wfSetupSimpleRSS() { global $wgSimpleRSS; $wgSimpleRSS = new SimpleRSS(); }