Difference between revisions of "Extension:SimpleRSS.php"
m |
(updated code for testing) |
||
Line 2: | Line 2: | ||
# Extension:RobsRSS{{Category:Extensions|RobsRSS}}{{php}} | # Extension:RobsRSS{{Category:Extensions|RobsRSS}}{{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/ | + | # - Author: [http://www.organicdesign.co.nz/nad User:Rob]{{Category:Extensions created with Template:Extension}} |
if (!defined('MEDIAWIKI')) die('Not an entry point.'); | if (!defined('MEDIAWIKI')) die('Not an entry point.'); | ||
Line 8: | Line 8: | ||
define('ROBSRSS_VERSION','0.0.0, 2007-10-14'); | define('ROBSRSS_VERSION','0.0.0, 2007-10-14'); | ||
− | $wgRobsRSSTag | + | $wgRobsRSSTag = "rss"; |
− | $wgRSSUseAjax | + | $wgRSSUseAjax = false; |
− | $wgExtensionFunctions[] | + | $wgExtensionFunctions[] = 'wfSetupRobsRSS'; |
$wgExtensionCredits['parserhook'][] = array( | $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 | |
− | + | ); | |
+ | |||
+ | require('extensions/RobsRSSExtension/magpierss-0.72/rss_fetch.inc'); | ||
+ | |||
+ | # NOTE: because different sites seem to use keys for different things we may want | ||
+ | # to 'squash' keys in some reasonable manner to provide consistent keys to the | ||
+ | # template for output - needs testing to determine this. | ||
+ | |||
+ | # arrays of keys we're interested in | ||
+ | $channel_keys = array('title', 'link', 'pubdate', 'tagline'); | ||
+ | #$item_keys = array( | ||
# Called from $wgExtensionFunctions array when initialising extensions | # Called from $wgExtensionFunctions array when initialising extensions | ||
function wfSetupRobsRSS() { | 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 | # Expand an <rss> tag | ||
function wfRssTagHook($text,$argv,&$parser) { | 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) { | ||
+ | # 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 | ||
+ | $html = wfRenderRSS($data,$timestamp); | ||
+ | } | ||
+ | # 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], "*"); | ||
− | + | } | |
− | + | # $html .= print_r($channel, true); | |
− | + | ||
− | + | ||
− | + | return $html; | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | + | function list_array_keys($array, $indent) { | |
− | + | foreach($array as $key => $value) { | |
− | + | $output .= "$indent $key\n"; | |
− | + | if(is_array($value)) | |
− | + | $output .= list_array_keys($value, $indent."*"); | |
+ | } | ||
+ | return $output; | ||
+ | } | ||
# Render a portion of RSS data (which is in the useful format returned by wfReadRSS) | # Render a portion of RSS data (which is in the useful format returned by wfReadRSS) | ||
function wfRenderRSS($data,$timestamp = false) { | function wfRenderRSS($data,$timestamp = false) { | ||
− | + | return $html; | |
− | + | } | |
# Read RSS from URL's and return as some kind of useful runtime structure | # Read RSS from URL's and return as some kind of useful runtime structure | ||
function wfReadRSS($urls) { | function wfReadRSS($urls) { | ||
− | + | $data = array(); | |
− | + | foreach ($urls as $url) { | |
− | + | $data[] = fetch_rss($url); | |
− | + | } | |
− | + | return $data; | |
− | + | } | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | / | + | # parse raw xml to fix entities |
− | + | # taken from http://lastrss.oslab.net/ | |
− | + | function unhtmlentities ($string) { | |
− | + | // Get HTML entities table | |
− | + | $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES); | |
− | + | // Flip keys<==>values | |
− | + | $trans_tbl = array_flip ($trans_tbl); | |
− | + | // Add support for ' entity (missing in HTML_ENTITIES) | |
− | + | $trans_tbl += array(''' => "'"); | |
− | + | // Replace entities by values | |
− | + | return strtr ($string, $trans_tbl); | |
+ | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
?> | ?> |
Revision as of 00:34, 17 October 2007
<?php
- Extension:RobsRSS
- - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
- - Author: User:RobCategory:Extensions created with Template:Extension
if (!defined('MEDIAWIKI')) die('Not an entry point.');
define('ROBSRSS_VERSION','0.0.0, 2007-10-14');
$wgRobsRSSTag = "rss"; $wgRSSUseAjax = false; $wgExtensionFunctions[] = 'wfSetupRobsRSS'; $wgExtensionCredits['parserhook'][] = array( 'name' => 'RobsRSS', 'author' => 'User:Rob', 'description' => 'A dynamic aggregating RSS reader', 'url' => 'http://www.mediawiki.org/wiki/Extension:RobsRSS', 'version' => ROBSRSS_VERSION );
require('extensions/RobsRSSExtension/magpierss-0.72/rss_fetch.inc');
- NOTE: because different sites seem to use keys for different things we may want
- to 'squash' keys in some reasonable manner to provide consistent keys to the
- template for output - needs testing to determine this.
- arrays of keys we're interested in
$channel_keys = array('title', 'link', 'pubdate', 'tagline');
- $item_keys = array(
- Called from $wgExtensionFunctions array when initialising extensions
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 .= "
\n";
if ($wgRSSUseAjax) { # Render a container which does ajax requests for content
$html = "
";
} else { # Not using ajax, render the output now $html = wfRenderRSS($data,$timestamp); } # 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
".$v->channel[title]."
\n"; $html .= "\n
Channel keys
\n";
$html .= list_array_keys($v->channel, "*");
$html .= "\n
Item keys
\n";
$html .= list_array_keys($v->items[0], "*");
}
- $html .= print_r($channel, true);
return $html;
}
function list_array_keys($array, $indent) { foreach($array as $key => $value) { $output .= "$indent $key\n"; if(is_array($value)) $output .= list_array_keys($value, $indent."*"); } 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) { $data = array(); foreach ($urls as $url) { $data[] = fetch_rss($url); } return $data; }
# parse raw xml to fix entities # taken from http://lastrss.oslab.net/
function unhtmlentities ($string) { // Get HTML entities table $trans_tbl = get_html_translation_table (HTML_ENTITIES, ENT_QUOTES); // Flip keys<==>values $trans_tbl = array_flip ($trans_tbl); // Add support for ' entity (missing in HTML_ENTITIES) $trans_tbl += array(''' => "'"); // Replace entities by values return strtr ($string, $trans_tbl); }
?>