Difference between revisions of "Extension:SimpleRSS.php"

From Organic Design wiki
m (update)
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/nad 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'));
 +
 +
}
 +
 +
# 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) {
  
if (!defined('MEDIAWIKI')) die('Not an entry point.');
+
# 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;
 +
}
  
define('ROBSRSS_VERSION','0.0.0, 2007-10-14');
+
global $wgRSSUseAjax;
  
$wgRobsRSSTag = "rss";
+
if ($wgRSSUseAjax) {
$wgRSSUseAjax = false;
+
# Render a container which does ajax requests for content
$wgExtensionFunctions[] = 'wfSetupRobsRSS';
+
$text = "<div id=foo> an ajax thingie </div>";
$wgExtensionCredits['parserhook'][] = array(
+
}
'name'       => 'RobsRSS',
+
else {
'author'     => '[http://www.organicdesign.co.nz/User:Rob User:Rob]',
+
# Not using ajax, render the output now
'description' => 'A dynamic aggregating RSS reader',
+
'url'       => 'http://www.mediawiki.org/wiki/Extension:RobsRSS',
+
# reserved param keys - other keys are interpreted as feed = url pairs
'version'     => ROBSRSS_VERSION
+
$reserved = array('headtemplate', 'itemtemplate', 'foottemplate', 'itemslimit',
);
+
'useajax', 'useagregation', 'url');
 +
 +
# default values if not passed as params: maybe get from globals
 +
if(!isset($argv[headtemplate])) $argv[headtemplate] = 'RSSHead';
 +
if(!isset($argv[itemtemplate])) $argv[itemtemplate] = 'RSSItem';
 +
if(!isset($argv[foottemplate])) $argv[foottemplate] = 'RSSFoot';
 +
if(!isset($argv[itemslimit])) $argv[itemslimit] = 10;
  
require('extensions/RobsRSSExtension/magpierss-0.72/rss_fetch.inc');
 
  
# NOTE: because different sites seem to use keys for different things we may want
+
# if no agregation, you must supply a url
# to 'squash' keys in some reasonable manner to provide consistent keys to the
+
if(!isset($argv[useagregation]) && !isset($argv[url]))
# template for output - needs testing to determine this.
+
$text = "SimpleRSS: fatal error: explicit ''url'' parameter is required or implicit ''feed''=http://url.com\n";
 +
 +
# if no url specified, ignore any reserved words and take the rest
 +
# as implicit url parameters
 +
foreach($argv as $key => $value) {
 +
if(!in_array($key, $reserved))
 +
$url[$key] = $value;
 +
}
  
# arrays of keys we're interested in
+
if(!isset($argv[url]))
$channel_keys = array('title', 'link', 'pubdate', 'tagline');
+
$data = wfReadRSS($url);
#$item_keys = array(
+
else {
 +
# url param is present
 +
$data = wfReadRSS($argv[url]);
 +
}
  
 +
$text .= print_r($argv, true);
 +
$text .= print_r($url, true);
  
# Called from $wgExtensionFunctions array when initialising extensions
+
# dump data
function wfSetupRobsRSS() {
+
foreach ($data as $v) {
global $wgParser,$wgRSSUseAjax,$wgAjaxExportList,$wgRobsRSSTag;
+
# create head template
$wgParser->setHook($wgRobsRSSTag,'wfRssTagHook');
+
wfListArrayKeys($v->channel, "", $channel);
if ($wgRSSUseAjax) $wgAjaxExportList[] = 'wfRenderRSS'; # Allow Ajax Dispatcher to call wfRenderRSS
+
$text .= wfMakeTemplate($argv[headtemplate], $channel);
}
 
  
# Expand an <rss> tag
+
# loop creating item templates
function wfRssTagHook($text,$argv,&$parser) {
+
for($i=0; $i<$argv[itemslimit]; $i++) {
global $wgRSSUseAjax;
+
if(!isset($v->items[$i])) break;
$args = '';
+
wfListArrayKeys($v->items[$i], "", $itemResult);
foreach ($argv as $k => $v) $args .= "<li><b>$k:</b> <i>$v</i></li>\n";
+
$text .= wfMakeTemplate($argv[itemtemplate], $itemResult);
 +
}
 +
 +
# create foot template (same as head)
 +
wfListArrayKeys($v->channel, "", $channel);
 +
$text .= wfMakeTemplate($argv[foottemplate], $channel);
  
if ($wgRSSUseAjax) {
+
}
# Render a container which does ajax requests for content
+
$html = "<div id=foo> an ajax thingie </div>";
 
 
}
 
}
else {
+
 
# Not using ajax, render the output now
+
# Return result with available parser flags
$html = wfRenderRSS($data,$timestamp);
+
return array(
 +
$text,
 +
found => true,
 +
nowiki => false,
 +
noparse => false,
 +
noargs => false,
 +
isHTML => false
 +
);
 +
 
 
}
 
}
# grab each url and process
 
$data = wfReadRSS(preg_split("/[\n\r]/", $text, -1, PREG_SPLIT_NO_EMPTY));
 
  
# dump data  
 
$channel = array();
 
foreach ($data as $v) {
 
$channel[$v->channel[title]][channel] = $v->channel;
 
$channel[$v->channel[title]][items] = $v->items;
 
  
$html .= "\n<h2>".$v->channel[title]."</h2>\n";
 
$html .= "\n<h3>Channel keys</h3>\n";
 
$html .= list_array_keys($v->channel, "*");
 
$html .= "\n<h3>Item keys</h3>\n";
 
$html .= list_array_keys($v->items[0], "*");
 
  
}
+
} # object end
# $html .= print_r($channel, true);
+
 
+
 
+
# build array of key-value pairs into template syntax
return $html;
+
# {{TemplateName|title=foo|link=http://foo.com|description=Hello!}}
}
+
function wfMakeTemplate($template, $array) {
 +
foreach($array as $key => $value)
 +
$params .= "\n|".$key."=".$value;
 +
return "{{".$template.$params."}}\n";
 +
}
 +
 
  
function list_array_keys($array, $indent) {
+
# build array of keys to publish (it's recursive!)
 +
# WARNING because this function uses a hash to eliminate duplicate keys
 +
# it no good for lists of items, only one at a time.
 +
function wfListArrayKeys($array, $parent="", &$result) {
 
foreach($array as $key => $value) {
 
foreach($array as $key => $value) {
$output .= "$indent $key\n";
 
 
if(is_array($value))
 
if(is_array($value))
$output .= list_array_keys($value, $indent."*");
+
wfListArrayKeys($value, "$parent$key", $result);
 +
else
 +
$result[$parent.$key] = $value;
 
}
 
}
return $output;
 
 
}
 
}
  
# Render a portion of RSS data (which is in the useful format returned by wfReadRSS)
 
function wfRenderRSS($data,$timestamp = false) {
 
return $html;
 
}
 
 
# Read RSS from URL's and return as some kind of useful runtime structure
 
 
function wfReadRSS($urls) {
 
function wfReadRSS($urls) {
$data = array();
+
if(is_array($urls)) {
foreach ($urls as $url) {
+
$data = array();
$data[] = fetch_rss($url);
+
foreach ($urls as $key => $value) {
 +
$data[$key] = fetch_rss($value);
 
}
 
}
 +
} else $data[] = fetch_rss($urls);
 
return $data;
 
return $data;
 
}
 
}
  
# parse raw xml to fix entities
+
 
# taken from http://lastrss.oslab.net/
+
# Needed in MediaWiki >1.8.0 for magic word hooks to work properly
  function unhtmlentities ($string) {
+
function wfSimpleRSSLanguageGetMagic(&$magicWords,$langCode = 0) {
      // Get HTML entities table
+
global $wgSimpleRSSMagic;
      $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES);
+
$magicWords[$wgSimpleRSSMagic] = array(0,$wgSimpleRSSMagic);
      // Flip keys<==>values
+
return true;
      $trans_tbl = array_flip ($trans_tbl);
+
}
      // Add support for &apos; entity (missing in HTML_ENTITIES)
+
 
      $trans_tbl += array('&apos;' => "'");
+
# Called from $wgExtensionFunctions array when initialising extensions
      // Replace entities by values
+
function wfSetupSimpleRSS() {
      return strtr ($string, $trans_tbl);
+
global $wgSimpleRSS;
  }
+
$wgSimpleRSS = new SimpleRSS();
 +
}
 +
 
  
  
 
?>
 
?>

Revision as of 20:33, 18 October 2007

<?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

    # reserved param keys - other keys are interpreted as feed = url pairs $reserved = array('headtemplate', 'itemtemplate', 'foottemplate', 'itemslimit', 'useajax', 'useagregation', 'url');

    # default values if not passed as params: maybe get from globals if(!isset($argv[headtemplate])) $argv[headtemplate] = 'RSSHead'; if(!isset($argv[itemtemplate])) $argv[itemtemplate] = 'RSSItem'; if(!isset($argv[foottemplate])) $argv[foottemplate] = 'RSSFoot'; if(!isset($argv[itemslimit])) $argv[itemslimit] = 10;


    # if no agregation, you must supply a url if(!isset($argv[useagregation]) && !isset($argv[url])) $text = "SimpleRSS: fatal error: explicit url parameter is required or implicit feed=http://url.com\n";

    # if no url specified, ignore any reserved words and take the rest # as implicit url parameters foreach($argv as $key => $value) { if(!in_array($key, $reserved)) $url[$key] = $value; }

    if(!isset($argv[url])) $data = wfReadRSS($url); else { # url param is present $data = wfReadRSS($argv[url]); }

    $text .= print_r($argv, true); $text .= print_r($url, true);

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

    # loop creating item templates for($i=0; $i<$argv[itemslimit]; $i++) { if(!isset($v->items[$i])) break; wfListArrayKeys($v->items[$i], "", $itemResult); $text .= wfMakeTemplate($argv[itemtemplate], $itemResult); }

    # create foot template (same as head) wfListArrayKeys($v->channel, "", $channel); $text .= wfMakeTemplate($argv[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) { if(is_array($urls)) { $data = array(); foreach ($urls as $key => $value) { $data[$key] = fetch_rss($value); } } else $data[] = fetch_rss($urls); 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(); }


    ?>