You are here

feeds_atom.module in Feeds Atom 6

Same filename and directory in other branches
  1. 7 feeds_atom.module

Contains the main functionality for feeds_atom.

File

feeds_atom.module
View source
<?php

/**
 * @file
 * Contains the main functionality for feeds_atom.
 */
define('FEEDS_ATOM_TOMBSTONE_NAMESPACE', 'http://purl.org/atompub/tombstones/1.0');

/**
 * Implementation of hook_ctools_plugin_api().
 */
function feeds_atom_ctools_plugin_api($owner, $api) {
  if ($owner == 'feeds' && $api == 'plugins') {
    return array(
      'version' => 1,
    );
  }
}

/**
 * Implementation of hook_feeds_plugins().
 */
function feeds_atom_feeds_plugins() {
  $path = drupal_get_path('module', 'feeds_atom') . '/plugins';
  $info['FeedsAtomRDFParser'] = array(
    'name' => 'FeedsAtomRDF parser',
    'description' => 'Parse data in Atom RDF format.',
    'handler' => array(
      'parent' => 'FeedsParser',
      'class' => 'FeedsAtomRDFParser',
      'file' => 'FeedsAtomRDFParser.inc',
      'path' => $path,
    ),
  );
  $info['FeedsAtomRDFProcessor'] = array(
    'name' => 'FeedsAtomRDF Processing Stage',
    'description' => 'Process my stuff.',
    'help' => 'Processing stage of parsed data.',
    'handler' => array(
      'parent' => 'FeedsNodeProcessor',
      'class' => 'FeedsAtomRDFProcessor',
      'file' => 'FeedsAtomRDFProcessor.inc',
      'path' => $path,
    ),
  );
  return $info;
}

/**
 * Implementation of hook_feeds_atom_rdf_map_alter().
 *
 * We implement this hook on behalf of the taxonomy module so that we can
 * lazy-create terms as needed.  We only do so if the vocabulary the term is in
 * already exists.  If the vocabulary doesn't exist, the incoming term is ignored.
 *
 * @param $target_item
 *   The node that we are creating/editing.
 * @param $source_item
 *   The parsed data array from the feed.
 * @return unknown_type
 */
function taxonomy_feeds_atom_rdf_map_alter(&$target_item, $source_item) {
  if (empty($source_item['rdf']['taxonomy']) || !is_array($source_item['rdf']['taxonomy'])) {

    // Nothing to process
    return;
  }

  // Zero out the taxonomy data that is already there, as it will break if we
  // try to save the node with it there.
  $target_item->taxonomy = array();

  // Process the term data.
  foreach ($source_item['rdf']['taxonomy'] as $source_term) {

    // Add new terms if they don't exist
    $vid = NULL;
    $tid = NULL;

    // Find the vocabulary.
    if (!empty($source_term['vocabulary'])) {

      // Features intergration: Features stores vocabulary machine name's in
      // module key prepended with features_
      if (module_exists('features')) {
        $machine_name = !empty($source_term['machine']) ? $source_term['machine'] : $source_term['vocabulary'];

        // Add in features_ if doesn't exist
        if (strpos($machine_name, 'features_') !== 0) {
          $machine_name = 'features_' . $machine_name;
        }
        $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module = '%s'", strtolower($machine_name)));
      }

      // Fallback to name matching if vid not found above.
      if (empty($vid)) {
        $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE lower(name) = '%s'", strtolower($source_term['vocabulary'])));
      }
    }

    // See if the term already exists
    foreach (taxonomy_get_term_by_name($source_term['title']) as $term) {

      // if VID was not found but name matches or vid is term's vocabulary.
      if (empty($vid) || $vid == $term->vid) {
        $tid = $term->tid;
      }
    }

    // Create the new term if doesn't exist and know vocabulary
    if (empty($tid) && !empty($vid)) {
      $new_term = array(
        'vid' => $vid,
        'name' => $source_term['title'],
        'description' => $source_term['description'],
      );
      taxonomy_save_term($new_term);
      $tid = $new_term['tid'];
    }

    // Apply the term to the target node.
    if (!empty($tid)) {
      $term = taxonomy_get_term($tid, TRUE);
      $target_item->taxonomy[$term->tid] = $term;
    }
  }
}

/**
 * Implementation of hook_feeds_atom_rdf_map_alter().
 */
function filefield_feeds_atom_rdf_map_alter(&$target_item, $source_item, FeedsSource $source) {

  // Use static variables in combination with the FeedsEnclosureUnique class
  // to ensure that imported files are not downloaded more than once.
  static $enclosures = array();
  static $files = array();

  // For any filefield in the incoming data, check to see if a full URL to the file
  // is specified.  If so, import it outright.
  foreach ($source_item['rdf'] as $field_name => $field) {
    $target_field =& $target_item->{$field_name};
    $field_info = content_fields($field_name, $target_item->type);
    if (!empty($field['#attributes']['type']) && $field['#attributes']['type'] == 'filefield') {
      foreach ($field as $i => $instance) {

        // This is only the case if the field in question is a filefield and
        // not the #attributes element.
        if (!empty($instance['full_url'])) {
          if (empty($enclosures[$instance['full_url']])) {
            $enclosures[$instance['full_url']] = new FeedsEnclosureUnique($instance['full_url'], $instance['filemime']);
            $files[$instance['full_url']] = $enclosures[$instance['full_url']]
              ->getFile();
          }
          if ($files[$instance['full_url']]) {
            $target_dir = filefield_widget_file_path($field_info, user_load($target_item->uid));
            if ($info = $enclosures[$instance['full_url']]
              ->saveTo($target_dir)) {
              $info['list'] = array();
              $info['data'] = array(
                'description' => '',
              );
              if ($field_info['list_field']) {
                $info['list'] = $field_info['list_default'];
              }
              $target_field[$i] = $info;
            }
          }
        }
      }
    }
  }
}

/**
 * Implementation of hook_file_delete().
 */
function feeds_atom_file_delete($file) {

  // Clean up our extra tracking information.
  db_query("DELETE FROM {feeds_atom_file_import} WHERE fid = %d", $file->fid);
}

Functions

Constants

Namesort descending Description
FEEDS_ATOM_TOMBSTONE_NAMESPACE @file Contains the main functionality for feeds_atom.