new version RSS 1.90: streamlined template use, extended documentation; replaced parsing of each single channel subelement (item) by one final parser call when rendering

This commit is contained in:
Thomas Gries 2011-08-15 21:52:08 +00:00
parent 224cbc4e0f
commit 06e9fa6c74
4 changed files with 177 additions and 80 deletions

View file

@ -1,24 +1,70 @@
Extension page on mediawiki.org: http://www.mediawiki.org/wiki/Extension:RSS
RELEASE NOTES of the MediaWiki extension RSS
http://www.mediawiki.org/wiki/Extension:RSS
== Change Log ==
*original by mutante 25.03.2005
*extended by Duesentrieb 30.04.2005
*extended by Rdb78 07.07.2005
*extended by Mafs 10.07.2005, 24.07.2005
*extended by Niffler 28.02.2006
*modified by Dzag 07.2006
*modified by Alxndr 09.2006
=== TO DO ===
* bug 30377 add a new parameter to limit the number of characters when rendering
the channel item <description>
* set an upper default limit for HttpRequest request size when fetching feeds
doing a HEAD request first to ask for the size but that value may not be
available. Check how much data is returned as its coming back
in which case you'd override the content fetch callback and count data as it
comes in or check the header, again, if it's provided, at the start of data
coming in. Then you could abort cleanly once it's gotten too much
(otherwise using the defaults - PHP will abort the entire program when your
memory usage gets too high)
* bug 30028 "Error parsing XML for RSS" - improve and harden Extension:RSS when
parsing differently flavoured RSS feeds
=== Version 1.90 2011-08-15 ===
* removed parsing of each single channel subelement (item)
* only the finally constructed feed is sent to the recursive parser:
in pre-1.9 versions, each channel subelement (item) was sent to the parser
* [[MediaWiki:Rss-item]] default has channel subelement <description> added
* Rss template default name has been changed:
until 1.8: [[Template:RSSPost]]
1.9: [[MediaWiki:Rss-feed]], an existing [[Template:RSSPost]]
takes precedence to be compatible to pre-1.9 versions
* introducing [[MediaWiki:Rss-feed]] with a meaningful default.
The channel subelements which make the feed are rendered in this
new standard layout:
* <title>
: <description>
: <author> <date>
* There are several ways to customize the final layout of feed items:
1. Admins can change the [[MediaWiki:Rss-feed]] default page
2. Users can use the optional template= parameter to tell the extension
to render the feed with a different layout
<rss template=Pagename>...</rss> use layout as in [[Template:Pagename]]
3. <rss template=Namespace:Pagename>...</rss> use [[Namespace:Pagename]]
=== Version 1.8 2010-10-19 ===
* removed dependencies on Snoopy in favor of MediaWiki's internal HttpRequest
* removed MagpieRSS (perhaps to be replaced later with SimplePie)
because of concerns about MagpieRSS's security problems and
lack of maintenance.
* Added ability to do more with the layout of RSS feeds.
=== Version 1.7 2010-07-23 ===
* cleaned up, included MagpieRSS library
* put into the WMF subversion repository by Jeroen De Dauw
http://www.mediawiki.org/wiki/User:Jeroen_De_Dauw
* i18n file added by TranslateWiki.net people
== Change Log of pre-1.7 versions 2010 and before ==
(latest on top)
*modified by [[User:K001|K001]] 15:15, 26 January 2010 (UTC): version 1.6, added support for date formats
*modified by Peter Newman: 03:15, 7 October 2009 (UTC) Added htmlspecialchars escaping to the displayed strings
*modified by [[User:Cmreigrut|Cmreigrut]] 19:05, 19 November 2008 (UTC): added date (if specified) to short output
*modified by --[[User:Wikinaut|Wikinaut]] 11:17, 7 May 2008 (UTC) : changed method to disable chaching; Extension is now compatible to MediaWiki 1.12
*modified by Svanslyck 02.2008, replacing all « and » with "
*<del>This has been updated to work better on newer (1.9) MediaWiki software, with the help of [[User:Duesentrieb]]. --[[User:CryptoQuick|CryptoQuick]] 14:26, 24 January 2007 (UTC)</del>
**This appears not to be true; I have received numerous emails about it not working with 1.9+. I would love to help debug and fix the extension, but my host has not upgraded to PHP 5 and I'm thus stuck at MediaWiki 1.6.8, so that's as far as this is guaranteed to work properly. If anyone develops a fix, please post a link to it here! &#x2014;[[User:Alxndr|Alxndr]]&#x00a0;<sup>([[User talk:Alxndr|t]])</sup> 02:02, 16 June 2007 (UTC)
***I just found [http://nako.us/2007/03/16/mediawiki-19-fix-for-wfstrencode/ this fork] that purports to have a fix for the new loss of wfStrEncode(). I can't test it though so can anyone else verify that it works? &#x2014;[[User:Alxndr|Alxndr]]&#x00a0;<sup>([[User talk:Alxndr|t]])</sup> 02:18, 16 June 2007 (UTC)
*modified by --[[User:Wikinaut|Wikinaut]] 11:17, 7 May 2008 (UTC) : changed method to disable chaching; Extension is now compatible to MediaWiki 1.12
*modified by [[User:Cmreigrut|Cmreigrut]] 19:05, 19 November 2008 (UTC): added date (if specified) to short output
*modified by Peter Newman: 03:15, 7 October 2009 (UTC) Added htmlspecialchars escaping to the displayed strings
*modified by [[User:K001|K001]] 15:15, 26 January 2010 (UTC): version 1.6, added support for date formats
=== Version 1.7 ===
2010-7-23
* Cleaned up, included MagpieRSS library and put onto the WMF subversion repository by Jeroen De Dauw - http://www.mediawiki.org/wiki/User:Jeroen_De_Dauw
* i18n file added by TranslateWiki.net people
*modified by Alxndr 09.2006
*modified by Dzag 07.2006
*extended by Niffler 28.02.2006
*extended by Mafs 10.07.2005, 24.07.2005
*extended by Rdb78 07.07.2005
*extended by Duesentrieb 30.04.2005
*original by mutante 25.03.2005

View file

@ -12,7 +12,7 @@ $messages = array();
* @author Łukasz Garczewski (TOR) <tor@wikia-inc.com>
*/
$messages['en'] = array(
'rss-desc' => 'Displays an RSS feed on a wiki page',
'rss-desc' => 'Displays RSS feeds on MediaWiki pages in a standard or in user-definable formats using template pages',
'rss-error' => 'Failed to load RSS feed from $1: $2',
'rss-empty' => 'Failed to load RSS feed from $1!',
'rss-fetch-nourl' => 'Fetch called without a URL!',
@ -20,7 +20,23 @@ $messages['en'] = array(
'rss-parse-error' => 'Error parsing XML for RSS',
'rss-ns-permission' => 'RSS is not allowed in this namespace',
'rss-url-permission' => 'This URL is not allowed to be included',
'rss-item' => '{{$1 | title = {{{title}}} | link = {{{link}}} | date = {{{date}}} | author = {{{author}}} }}',
'rss-item' => '{{$1 | title = {{{title}}} | link = {{{link}}} | date = {{{date}}} | author = {{{author}}} | description = {{{description}}} }}',
'rss-feed' => "<!-- the following are two alternative templates. The first is the basic default template for feeds -->; '''<span class='plainlinks'>[{{{link}}} {{{title}}}]</span>'''
: {{{description}}}
: {{{author}}} {{{date}}}<!-- don't use newline here --><!-- The second is an improved version which requires Extension:ParserFunctions --><!-- ; '''<span class='plainlinks'>[{{{link}}} {{{title}}}]</span>'''{{#if: {{{description|}}}|: {{{description}}}}}{{#if: {{{author|}}} | {{#if: {{{date|}}} |: &mdash; {{{author}}} {{{date}}}}} | {{#if: {{{author|}}}|: &mdash; {{{author}}}}} {{#if: {{{date|}}}|:{{{date}}}}}|}} -->",
);
$messages['qqq'] = array(
'rss-item' => "
; $1
: ''not to be localised''
: The RSS extension substitutes this placeholder with the name of a template page. The content of this template page determines the final layout of the RSS feed on the rendered wiki page. The Extension:RSS currently uses 'MediaWiki:Rss-feed' as default for $1. This means that the content of [[MediaWiki:Rss-feed]] determines how RSS feed items are rendered.
: It allows users to let RSS feeds rendered differently by using different (optional) 'template=<pagename>' parameters in the rss wiki tags.
; title = {{{title}}} | link = {{{link}}} | ...
: 'title' (left) is the variable name under which the content of an RSS feed field 'title' (right) is passed to the Template $1 where this is then used in the feed rendering.
: This ''may'' be localised, but the content of the template $1 page (default [[MediaWiki:Rss-feed]] and potentially other RSS feed template pages on this wiki) needs then to be localised, too.
: 'title' (right) is a name (property) of RSS feeds and is certainly not to be localised in any way.
: ''I suggest not to localise anything.''",
);
/** Afrikaans (Afrikaans)

50
RSS.php
View file

@ -4,13 +4,13 @@
*
* @file
* @ingroup Extensions
* @version 1.8
* @author mutante, Daniel Kinzler, Rdb, Mafs, Alxndr, Chris Reigrut, K001
* @version 1.90
* @author mutante, Daniel Kinzler, Rdb, Mafs, Thomas Gries, Alxndr, Chris Reigrut, K001
* @author Kellan Elliott-McCrea <kellan@protest.net> -- author of MagpieRSS
* @author Jeroen De Dauw
* @author Jack Phoenix <jack@countervandalism.net>
* @copyright © Kellan Elliott-McCrea <kellan@protest.net>
* @copyright © mutante, Daniel Kinzler, Rdb, Mafs, Alxndr, Chris Reigrut, K001
* @copyright © mutante, Daniel Kinzler, Rdb, Mafs, Thomas Gries, Alxndr, Chris Reigrut, K001
* @link http://www.mediawiki.org/wiki/Extension:RSS Documentation
*/
@ -21,21 +21,11 @@ if ( !defined( 'MEDIAWIKI' ) ) {
// Extension credits that will show up on Special:Version
$wgExtensionCredits['parserhook'][] = array(
'name' => 'RSS feed',
'author' => array(
'Kellan Elliott-McCrea',
'mutante',
'Daniel Kinzler',
'Rdb',
'Mafs',
'Alxndr',
'Wikinaut',
'Chris Reigrut',
'K001',
'Jack Phoenix',
'Jeroen De Dauw',
'Mark A. Hershberger'
'author' => array( 'Kellan Elliott-McCrea', 'mutante', 'Daniel Kinzler',
'Rdb', 'Mafs', 'Alxndr', 'Thomas Gries', 'Chris Reigrut',
'K001', 'Jack Phoenix', 'Jeroen De Dauw', 'Mark A. Hershberger'
),
'version' => '1.9',
'version' => '1.90 20110815',
'url' => 'http://www.mediawiki.org/wiki/Extension:RSS',
'descriptionmsg' => 'rss-desc',
);
@ -49,16 +39,24 @@ $wgAutoloadClasses['RSSData'] = $dir . 'RSSData.php';
$wgHooks['ParserFirstCallInit'][] = 'RSSHooks::parserInit';
$wgRSSCacheAge = 3600; // one hour
$wgRSSCacheCompare = false; // Check cached content, if available, against remote.
// $wgRSSCacheCompare should be set to false or a timeout
// (less than $wgRSSCacheAge) after which a comparison will
// be made.
$wgRSSFetchTimeout = 5; // 5 second timeout
$wgRSSNamespaces = null; // Ignore the RSS tag in all but the namespaces listed here.
// null (the default) means the <rss> tag can be used
// anywhere.
// one hour
$wgRSSCacheAge = 3600;
// Check cached content, if available, against remote.
// $wgRSSCacheCompare should be set to false or a timeout
// (less than $wgRSSCacheAge) after which a comparison will be made.
$wgRSSCacheCompare = false;
// 5 second timeout
$wgRSSFetchTimeout = 5;
// Ignore the RSS tag in all but the namespaces listed here.
// null (the default) means the <rss> tag can be used anywhere.
$wgRSSNamespaces = null;
// URL whitelist of RSS Feeds:
// if there are items in the array, and the used URL isn't in the array,
// it will not be allowed (originally proposed in bug 27768)
$wgRSSAllowedFeeds = array();
// Agent to use for fetching feeds

View file

@ -65,13 +65,42 @@ class RSSParser {
$this->filterOut = self::explodeOnSpaces( $args['filterout'] );
}
// 'template' is the pagename of a user's itemTemplate including
// a further pagename for the feedTemplate
// In that way everything is handled via these two pages
// and no default pages or templates are used.
// 'templatename' is an optional pagename of a user's feedTemplate
// In that way it substitutes $1 (default: RSSPost) in MediaWiki:Rss-item
if ( isset( $args['template'] ) ) {
$titleObject = Title::newFromText( $args['template'], NS_TEMPLATE );
$article = new Article( $titleObject, 0 );
$this->itemTemplate = $article->fetchContent();
$itemTemplateTitleObject = Title::newFromText( $args['template'], NS_TEMPLATE );
$itemTemplateArticleObject = new Article( $itemTemplateTitleObject, 0 );
$this->itemTemplate = $itemTemplateArticleObject->fetchContent();
} else {
$templateName = isset( $args['templatename'] ) ? $args['templatename'] : 'RSSPost';
$this->itemTemplate = wfMsgNoTrans( 'rss-item', $templateName );
if ( isset( $args['templatename'] ) ) {
$feedTemplatePagename = $args['templatename'];
} else {
// compatibility patch for rss extension
$feedTemplatePagename = 'RSSPost';
$feedTemplateTitleObject = Title::newFromText( $feedTemplatePagename, NS_TEMPLATE );
if ( !$feedTemplateTitleObject->exists() ) {
$feedTemplatePagename = Title::makeTitleSafe( NS_MEDIAWIKI, 'Rss-feed' );
}
}
// MediaWiki:Rss-item = {{ feedTemplatePagename | title = {{{title}}} | ... }}
// if the attribute parameter templatename= is not present
// then it defaults to
// {{ Template:RSSPost | title = {{{title}}} | ... }} - if Template:RSSPost exists from pre-1.9 versions
// {{ MediaWiki:Rss-feed | title = {{{title}}} | ... }} - otherwise
$this->itemTemplate = wfMsgNoTrans( 'rss-item', $feedTemplatePagename );
}
}
@ -213,8 +242,11 @@ class RSSParser {
* @param $frame the frame param to pass to recursiveTagParse()
*/
function renderFeed( $parser, $frame ) {
$output = '';
if ( isset( $this->itemTemplate ) ) {
$renderedFeed = '';
if ( isset( $this->itemTemplate ) && isset( $parser ) && isset( $frame ) ) {
$headcnt = 0;
if ( $this->reversed ) {
$this->rss->items = array_reverse( $this->rss->items );
@ -226,43 +258,48 @@ class RSSParser {
}
if ( $this->canDisplay( $item ) ) {
$output .= $this->renderItem( $item, $parser, $frame );
$renderedFeed .= $this->renderItem( $item ) . "\n";
$headcnt++;
}
}
}
return $output;
$renderedFeed = $parser->recursiveTagParse( $renderedFeed, $frame );
}
return $renderedFeed;
}
/**
* Render each item, filtering it out if necessary, applying any highlighting.
*
* @param $item Array: an array produced by RSSData where keys are the
* names of the RSS elements
* @param $parser Parser the parser param to pass to recursiveTagParse()
* @param $frame the frame param to pass to recursiveTagParse()
* @param $item Array: an array produced by RSSData where keys are the names of the RSS elements
*/
protected function renderItem( $item, $parser, $frame ) {
$output = "";
if ( isset( $parser ) && isset( $frame ) ) {
$rendered = $this->itemTemplate;
// $info will only be an XML element name, so we're safe
// using it. $item[$info] is handled by the XML parser --
// and that means bad RSS with stuff like
// <description><script>alert("hi")</script></description> will find its
// rogue <script> tags neutered.
foreach ( array_keys( $item ) as $info ) {
if ( $info != 'link' ) {
$txt = $this->highlightTerms( $this->escapeTemplateParameter( $item[ $info ] ) );
} else {
$txt = $this->sanitizeUrl( $item[ $info ] );
}
$rendered = str_replace( '{{{' . $info . '}}}', $txt, $rendered );
}
protected function renderItem( $item ) {
$output = $parser->recursiveTagParse( $rendered, $frame );
$renderedItem = $this->itemTemplate;
// $info will only be an XML element name, so we're safe using it.
// $item[$info] is handled by the XML parser --
// and that means bad RSS with stuff like
// <description><script>alert("hi")</script></description> will find its
// rogue <script> tags neutered.
foreach ( array_keys( $item ) as $info ) {
if ( $info != 'link' ) {
$txt = $this->highlightTerms( $this->escapeTemplateParameter( $item[ $info ] ) );
} else {
$txt = $this->sanitizeUrl( $item[ $info ] );
}
$renderedItem = str_replace( '{{{' . $info . '}}}', $txt, $renderedItem );
}
return $output;
// nullify all remaining info items in the template
// without a corresponding info in the current feed item
$renderedItem = preg_replace( "!{{{[^}]+}}}!U", "", $renderedItem );
return $renderedItem;
}
/**