When using FeedsSimplePieParser, there is no way to extend the parse() function in a sub-class, for example to add support for "extension" elements (eg: iTunes). Without this, the only way to build upon FeedsSimplePieParser is to copy the entire parse() function and add the couple of extra lines required.

Can FeedsSimplePieParser::parse() be modified to either call a hook to add elements to $item, or even call an object method, allowing sub-classes to override that?

CommentFileSizeAuthor
#2 FeedsSimplePieParser.extension.patch676 byteslyricnz
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

lyricnz’s picture

Obviously, a matching extension would need to be done to ::getMappingSources()

lyricnz’s picture

Status: Active » Needs review
FileSize
676 bytes

First attempt at a patch attached. This uses a protected method on FeedsSimplePieParser for the additional mapping.

Here's an example of an extension using the code from the patch. It overrides getMappingSources() to add the new mapping sources, and implements the new method provided by the patch to add parsing for these items.

/**
 * A simple parser that extends FeedsSimplePieParser by adding support for a
 * couple of iTunes tags.
 */
class MyParser extends FeedsSimplePieParser {
  /**
   * Add the extra mapping sources provided by this parser.
   */
  public function getMappingSources() {
    return parent::getMappingSources() + array(
      'itunes_keywords' => array(
        'name' => t('iTunes:Keywords'),
        'description' => t('iTunes Keywords.'),
      ),
      'itunes_duration' => array(
        'name' => t('iTunes:Duration'),
        'description' => t('iTunes Duration.'),
      ),
    );
  }

  /**
   * Parse the extra mapping sources provided by this parser.
   */
  protected function parseExtensions(&$item, $simplepie_item) {
    $itunes_namespace = 'http://www.itunes.com/dtds/podcast-1.0.dtd';
    if ($value = $simplepie_item->get_item_tags($itunes_namespace, 'keywords')) {
      $item['itunes_keywords'] = $value[0]['data'];
    }
    if ($value = $simplepie_item->get_item_tags($itunes_namespace, 'duration')) {
      $item['itunes_duration'] = $value[0]['data'];
    }
  }
}
alex_b’s picture

Interesting.... without reviewing your patch closer, have you had a look at this issue: #631104: Extensible XML parser (mapping more sources)?

lyricnz’s picture

Yes, I had a look - but that's aiming at something different - a completely generic+extensible XML parser. This patch simply aims to make the SimplePieParser extensible, since it already does 90% of what is needed.

alex_b’s picture

Version: 6.x-1.0-alpha10 » 6.x-1.x-dev
Status: Needs review » Reviewed & tested by the community

#4 - Right.

I think this is a good approach for now. The obvious downside is that only one parser can extend this functionality at a time. If ever we would want to have multiple actors extend parsing we would have to add an API along the lines of the mapping API. But I assume that's too much to bite off at this moment.

RTBC from my point of view. Once this is committed, I'd like to incorporate #2 into the documentation.

alex_b’s picture

Status: Reviewed & tested by the community » Fixed
alex_b’s picture

Added #2 to documentation: http://drupal.org/node/622700

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.