https://www.mediawiki.org/wiki/Special:Code/MediaWiki/112308

Revision: 112308
Author:   wikinaut
Date:     2012-02-24 10:16:34 +0000 (Fri, 24 Feb 2012)
Log Message:
-----------
v2.00 can parse ATOM feeds, at least some. This is a major improvement over 
pre-2.00 versions which only could read and parse RSS feeds but no ATOM feeds. 
Version 2.00 begins to keep care of namespaces in the XML. The parser still 
leaves room for further improvements. At least, E:RSS can now read 
E:WikiArticleFeeds generated RSS _and_ ATOM feeds.

Modified Paths:
--------------
    trunk/extensions/RSS/RELEASE-NOTES
    trunk/extensions/RSS/RSS.php
    trunk/extensions/RSS/RSSData.php
    trunk/extensions/RSS/RSSParser.php

Modified: trunk/extensions/RSS/RELEASE-NOTES
===================================================================
--- trunk/extensions/RSS/RELEASE-NOTES  2012-02-24 10:06:12 UTC (rev 112307)
+++ trunk/extensions/RSS/RELEASE-NOTES  2012-02-24 10:16:34 UTC (rev 112308)
@@ -11,8 +11,13 @@
   (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
+  parsing differently flavoured RSS feeds and ATOM feeds
 
+=== Version 2.00 2012-02-24 ===
+* first version which can parse RSS and at least some ATOM feeds
+  partial solution of bug 30028 "Error parsing XML for RSS" - improve and 
harden
+  Extension:RSS when parsing differently flavoured RSS feeds and ATOM feeds
+
 === Version 1.94 2012-02-23 ===
 * changed white list definition and behaviour:
 

Modified: trunk/extensions/RSS/RSS.php
===================================================================
--- trunk/extensions/RSS/RSS.php        2012-02-24 10:06:12 UTC (rev 112307)
+++ trunk/extensions/RSS/RSS.php        2012-02-24 10:16:34 UTC (rev 112308)
@@ -4,7 +4,7 @@
  *
  * @file
  * @ingroup Extensions
- * @version 1.94
+ * @version 2.00
  * @author mutante, Daniel Kinzler, Rdb, Mafs, Thomas Gries, Alxndr, Chris 
Reigrut, K001
  * @author Kellan Elliott-McCrea <[email protected]> -- author of MagpieRSS
  * @author Jeroen De Dauw
@@ -14,7 +14,7 @@
  * @link http://www.mediawiki.org/wiki/Extension:RSS Documentation
  */
 
-define( "EXTENSION_RSS_VERSION", "1.94 20120223" );
+define( "EXTENSION_RSS_VERSION", "2.00 20120224" );
 
 if ( !defined( 'MEDIAWIKI' ) ) {
        die( "This is not a valid entry point.\n" );
@@ -49,6 +49,7 @@
 // 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.
+// for debugging set $wgRSSCacheCompare = 1;
 $wgRSSCacheCompare = false;
 
 // 5 second timeout

Modified: trunk/extensions/RSS/RSSData.php
===================================================================
--- trunk/extensions/RSS/RSSData.php    2012-02-24 10:06:12 UTC (rev 112307)
+++ trunk/extensions/RSS/RSSData.php    2012-02-24 10:16:34 UTC (rev 112308)
@@ -15,8 +15,29 @@
                        return;
                }
                $xpath = new DOMXPath( $xml );
-               $items = $xpath->query( '/rss/channel/item' );
+       
+               // register namespace as below, and apply a regex to the 
expression
+               // http://de3.php.net/manual/en/domxpath.query.php#103461
+               $namespaceURI = $xml->lookupnamespaceURI( NULL );
 
+               if ( ( null !== $namespaceURI ) ) {
+                       $defaultNS = "defaultNS";
+                       $xpath->registerNamespace( $defaultNS, $namespaceURI );
+                       $defaultNS = "defaultNS:";
+               } else {
+                       $defaultNS = "";
+               }
+
+               $q = "/rss/channel/item";
+               $q = preg_replace( '#(::|/\s*|\A)(?![/@].+?|[a-z\-]+::)#', '$1' 
. $defaultNS . '$2', $q );
+               $items = $xpath->query( $q ); // is it an RSS feed ?
+
+               if ( $items->length === 0 ) {
+                       $q = "/feed/entry";
+                       $q = preg_replace( 
'#(::|/\s*|\A)(?![/@].+?|[a-z\-]+::)#', '$1' . $defaultNS . '$2', $q );
+                       $items = $xpath->query( $q ); // is it an ATOM feed ?
+               }
+
                if( $items->length !== 0 ) {
                        foreach ( $items as $item ) {
                                $bit = array();
@@ -37,7 +58,7 @@
                                $this->items[] = $bit;
                        }
                } else {
-                       $this->error = 'No RSS items found.';
+                       $this->error = 'No RSS//ATOM items found.';
                        return;
                }
        }
@@ -52,10 +73,11 @@
         * @param $n String: name of the element we have
         * @return String Name to map it to
         */
-       protected function rssTokenToName( $n ) {
-               switch( $n ) {
+       protected function rssTokenToName( $name ) {
+               switch( $name ) {
                        case 'dc:date':
                        case 'pubDate':
+                       case 'updated':
                                return 'date';
                        case 'dc:creator':
                                return 'author';
@@ -73,9 +95,8 @@
                        case 'comments':
                        case 'category':
                                return null;
-
                        default:
-                               return $n;
+                               return $name;
                }
        }
 }
\ No newline at end of file

Modified: trunk/extensions/RSS/RSSParser.php
===================================================================
--- trunk/extensions/RSS/RSSParser.php  2012-02-24 10:06:12 UTC (rev 112307)
+++ trunk/extensions/RSS/RSSParser.php  2012-02-24 10:16:34 UTC (rev 112308)
@@ -328,24 +328,32 @@
 
                foreach ( array_keys( $item ) as $info ) {
                        switch ( $info ) {
+                       // ATOM <id> elements and RSS <link> elements are item 
link urls
+                       case 'id':
+                               $txt = $this->sanitizeUrl( $item['id'] );
+                               $renderedItem = str_replace( '{{{link}}}', 
$txt, $renderedItem );
+                               break;
                        case 'link':
-                               $txt = $this->sanitizeUrl( $item[ $info ] );
+                               if ( !isset( $item['id'] ) ) {
+                                       $txt = $this->sanitizeUrl( 
$item['link'] );
+                               }
+                               $renderedItem = str_replace( '{{{link}}}', 
$txt, $renderedItem );
                                break;
                        case 'date':
                                $tempTimezone = date_default_timezone_get();
                                date_default_timezone_set( 'UTC' );
-                               $txt = date( $this->date, strtotime( 
$this->escapeTemplateParameter( $item[ $info ] ) ) );
+                               $txt = date( $this->date, strtotime( 
$this->escapeTemplateParameter( $item['date'] ) ) );
                                date_default_timezone_set( $tempTimezone );
+                               $renderedItem = str_replace( '{{{date}}}', 
$txt, $renderedItem );
                                break;
                        default:
-                               $str = $this->escapeTemplateParameter( $item[ 
$info ] ); 
+                               $str = $this->escapeTemplateParameter( 
$item[$info] ); 
                                if ( mb_strlen( $str ) > $this->ItemMaxLength ) 
{
                                        $str = mb_substr( $str, 0, 
$this->ItemMaxLength ) . " ...";
                                }
                                $txt = $this->highlightTerms(  $str );
+                               $renderedItem = str_replace( '{{{' . $info . 
'}}}', $txt, $renderedItem );
                        }
-                       
-                       $renderedItem = str_replace( '{{{' . $info . '}}}', 
$txt, $renderedItem );
                }
 
                // nullify all remaining info items in the template


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to