Permalink
Browse files

Moved getContent from RSS1Element to Type so it can be used with RSS2…

… feeds

Added support for 'content' namespace in RSS2 per user request
Added test for new content support in RSS2


git-svn-id: http://svn.php.net/repository/pear/packages/XML_Feed_Parser/trunk@204920 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
James Stewart
James Stewart committed Jan 10, 2006
1 parent 00f0582 commit 798a580b0c8b34ee10fbc1327ade56d55d32c63c
Showing with 186 additions and 171 deletions.
  1. +0 −1 Parser.php
  2. +1 −1 Parser/RSS1.php
  3. +0 −33 Parser/RSS1Element.php
  4. +7 −0 Parser/RSS2.php
  5. +4 −3 Parser/RSS2Element.php
  6. +34 −0 Parser/Type.php
  7. +37 −37 package.xml
  8. +2 −1 samples/rss2sample.xml
  9. +94 −94 tests/convert-tests.php
  10. +7 −1 tests/rss2Values.php
View
@@ -316,7 +316,6 @@ function getEntryByOffset($offset)
return $this->feed->entries[$offset];
}
} else {
- // print $this->feed->numberEntries;
return false;
}
}
View
@@ -125,7 +125,7 @@ function __construct(DOMDocument $model, $strict = false)
$this->xpath = new DOMXPath($model);
foreach ($this->namespaces as $key => $value) {
$this->xpath->registerNamespace($key, $value);
- }
+ }
$this->numberEntries = $this->count('item');
}
View
@@ -99,39 +99,6 @@ function getId()
return false;
}
- /**
- * The official way to include full content in an RSS1 entry is to use
- * the content module's element 'encoded'. Often, however, the 'description'
- * element is used instead. We will offer that as a fallback.
- *
- * @return string|false
- */
- function getContent()
- {
- $options = array('encoded', 'description');
- foreach ($options as $element) {
- $test = $this->model->getElementsByTagName($element);
- if ($test->length == 0) {
- continue;
- }
- if ($test->item(0)->hasChildNodes()) {
- $value = '';
- foreach ($test->item(0)->childNodes as $child) {
- if ($child instanceof DOMText) {
- $value .= $child->nodeValue;
- } else {
- $simple = simplexml_import_dom($child);
- $value .= $simple->asXML();
- }
- }
- return $value;
- } else if ($test->length > 0) {
- return $test->item(0)->nodeValue;
- }
- }
- return false;
- }
-
/**
* How RSS1 should support for enclosures is not clear. For now we will return
* false.
View
@@ -101,6 +101,10 @@ class XML_Feed_Parser_RSS2 extends XML_Feed_Parser_Type
'date' => array('pubDate'),
'author' => array('managingEditor'));
+ protected $namespaces = array(
+ 'dc' => 'http://purl.org/rss/1.0/modules/dc/',
+ 'content' => 'http://purl.org/rss/1.0/modules/content/');
+
/**
* Our constructor does nothing more than its parent.
*
@@ -118,6 +122,9 @@ function __construct(DOMDocument $model, $strict = false)
}
$this->xpath = new DOMXPath($this->model);
+ foreach ($this->namespaces as $key => $value) {
+ $this->xpath->registerNamespace($key, $value);
+ }
$this->numberEntries = $this->count('item');
}
View
@@ -51,7 +51,8 @@ class XML_Feed_Parser_RSS2Element extends XML_Feed_Parser_RSS2
'enclosure' => array('Enclosure'),
'pubDate' => array('Date'),
'source' => array('Source'),
- 'link' => array('Text'));
+ 'link' => array('Text'),
+ 'content' => array('Content'));
/**
* Here we map some elements to their atom equivalents. This is going to be
@@ -61,10 +62,10 @@ class XML_Feed_Parser_RSS2Element extends XML_Feed_Parser_RSS2
*/
protected $compatMap = array(
'id' => array('guid'),
- 'content' => array('description'),
'updated' => array('lastBuildDate'),
'published' => array('pubdate'),
- 'guidislink' => array('guid', 'ispermalink'));
+ 'guidislink' => array('guid', 'ispermalink'),
+ 'summary' => array('description'));
/**
* Store useful information for later.
View
@@ -332,6 +332,40 @@ function traverseNode($node)
return $content;
}
+ /**
+ * The official way to include full content in an RSS1 entry is to use
+ * the content module's element 'encoded', and RSS2 feeds often duplicate that.
+ * Often, however, the 'description' element is used instead. We will offer that
+ * as a fallback. Atom uses its own approach and overrides this method.
+ *
+ * @return string|false
+ */
+ function getContent()
+ {
+ $options = array('encoded', 'description');
+ foreach ($options as $element) {
+ $test = $this->model->getElementsByTagName($element);
+ if ($test->length == 0) {
+ continue;
+ }
+ if ($test->item(0)->hasChildNodes()) {
+ $value = '';
+ foreach ($test->item(0)->childNodes as $child) {
+ if ($child instanceof DOMText) {
+ $value .= $child->nodeValue;
+ } else {
+ $simple = simplexml_import_dom($child);
+ $value .= $simple->asXML();
+ }
+ }
+ return $value;
+ } else if ($test->length > 0) {
+ return $test->item(0)->nodeValue;
+ }
+ }
+ return false;
+ }
+
/**
* Checks if this element has a particular child element.
*
View
@@ -13,10 +13,10 @@
</maintainer>
</maintainers>
<release>
- <version>0.2.8alpha</version>
- <state>alpha</state>
- <date>2005-12-26</date>
- <notes>Added atom-specific category handling
+ <version>0.2.8alpha</version>
+ <state>alpha</state>
+ <date>2005-12-26</date>
+ <notes>Added atom-specific category handling
Added 'tags' call in atom which is an alias for categories, to match Universal Feed Parser behaviour
Added base64 decoding for atom Text and Content
In atom:link elements, the rel attribute now defaults to 'alternate' if none specified
@@ -72,25 +72,25 @@ Numerous API fixes and consistency fixes following use of Universal Feed Parser
<file name="errors.php"/>
<file name="rss091Values.php"/>
<file name="rss092Values.php"/>
- <file name="tidy.php"/>
- <file name="convert-tests.php" role="script"/>
- <dir name="convertedtests" role="test">
- <file name="amp.php"/>
- <file name="atom.php"/>
- <file name="atom10.php"/>
- <file name="base.php"/>
- <file name="cdf.php"/>
- <file name="date.php"/>
- <file name="encoding.php"/>
- <file name="feedburner.php"/>
- <file name="http.php"/>
- <file name="itunes.php"/>
- <file name="lang.php"/>
- <file name="namespace.php"/>
- <file name="rdf.php"/>
- <file name="rss.php"/>
- <file name="sanitize.php"/>
- </dir>
+ <file name="tidy.php"/>
+ <file name="convert-tests.php" role="script"/>
+ <dir name="convertedtests" role="test">
+ <file name="amp.php"/>
+ <file name="atom.php"/>
+ <file name="atom10.php"/>
+ <file name="base.php"/>
+ <file name="cdf.php"/>
+ <file name="date.php"/>
+ <file name="encoding.php"/>
+ <file name="feedburner.php"/>
+ <file name="http.php"/>
+ <file name="itunes.php"/>
+ <file name="lang.php"/>
+ <file name="namespace.php"/>
+ <file name="rdf.php"/>
+ <file name="rss.php"/>
+ <file name="sanitize.php"/>
+ </dir>
</dir>
<dir name="samples" role="data">
<file name="atom10-example1.xml"/>
@@ -109,33 +109,33 @@ Numerous API fixes and consistency fixes following use of Universal Feed Parser
<file name="rss091-international.xml"/>
<file name="rss091-simple.xml"/>
<file name="rss092-sample.xml"/>
- <file name="illformed-atom10.xml"/>
+ <file name="illformed-atom10.xml"/>
</dir>
</dir>
</filelist>
<deps>
- <dep type="ext" rel="has" optional="yes">tidy</dep>
+ <dep type="ext" rel="has" optional="yes">tidy</dep>
</deps>
</release>
<changelog>
- <release>
- <version>0.2.7alpha</version>
- <state>alpha</state>
- <date>2005-10-22</date>
- <notes>Fixed bug in __call() meaning arguments in requests for feed-level elements weren't passed on correctly
+ <release>
+ <version>0.2.7alpha</version>
+ <state>alpha</state>
+ <date>2005-10-22</date>
+ <notes>Fixed bug in __call() meaning arguments in requests for feed-level elements weren't passed on correctly
Some extra compatibility maps to improve match with Universal Feed Parser</notes>
- </release>
- <release>
- <version>0.2.6alpha</version>
- <state>alpha</state>
- <date>2005-10-22</date>
- <notes>Added support for RSS0.90, RSS0.91 and RSS0.92
+ </release>
+ <release>
+ <version>0.2.6alpha</version>
+ <state>alpha</state>
+ <date>2005-10-22</date>
+ <notes>Added support for RSS0.90, RSS0.91 and RSS0.92
Added tests for RSS0.91 and RSS0.92
Fixed bug meaning that errors weren't being thrown for unrecognised RSS versions
Cleared up the main constructor to prevent DOM errors while detecting feed type
Added code to allow case-insensitive specification of element names as a backup
Fixed bad check of guid's 'ispermalink' attribute in RSS2Element.php</notes>
- </release>
+ </release>
<release>
<version>0.2.5alpha</version>
<state>alpha</state>
View
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
-<rss version="2.0">
+<rss version="2.0" xmlns:content="http://purl.org/rss/1.0.modules/content/">
<channel>
<title>Liftoff News</title>
<link>http://liftoff.msfc.nasa.gov/</link>
@@ -29,6 +29,7 @@
<description>Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.</description>
<pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid>
+ <content:encoded><![CDATA[<p>Test content</p>]]></content:encoded>
</item>
<item>
<title>Astronauts' Dirty Laundry</title>
Oops, something went wrong.

0 comments on commit 798a580

Please sign in to comment.