You are here

class FeedsEnclosureUnique in Feeds Atom 7

Same name and namespace in other branches
  1. 6 plugins/FeedsAtomRDFProcessor.inc \FeedsEnclosureUnique

Specialized version of FeedsEnclosure to ensure uniqueness when saving.

@todo, This class was necessary in D6 however in D7 it seems that saveTo() might not be called.

Hierarchy

Expanded class hierarchy of FeedsEnclosureUnique

File

plugins/FeedsAtomRDFProcessor.inc, line 214
Contains the feeds atom RDF processor class.

View source
class FeedsEnclosureUnique extends FeedsEnclosure {

  /**
   * Saves the file represented by this enclosure to disk.
   *
   * If the file already exists, based on its sha1() hash, then we will simply
   * reuse the existing file rather than saving a new one.
   *
   * @param $target_dir
   *   The directory to which to save the file.  Note that if the file has already
   *   been imported it is possible that it will not be in the requested directory,
   *   in which case this method returns the existing file info in its existing
   *   location.
   * @return
   *   The file info array as defined by filefield of the file that we just saved,
   *   or of the pre-existing file that should be used instead.
   */
  public function saveTo($target_dir) {
    $new_file_hash = sha1_file($this->file);
    $fid = db_query("SELECT fid FROM {feeds_atom_file_import} WHERE sha1 = :sha1", array(
      ':sha1' => $new_file_hash,
    ))
      ->fetchField();
    if ($fid) {

      // Pull the info for the existing file and return that. We won't save
      // the new file at all.
      $info = field_file_load($fid);
      return $info;
    }
    else {

      // Save the new file, and record its hash for later matching.
      $info = field_file_save_file($this->file, array(), $target_dir);

      // @todo Please review the conversion of this statement to the D7 database API syntax.

      /* db_query("INSERT INTO {feeds_atom_file_import} (fid, sha1) VALUES (%d, '%s')", $info['fid'], $new_file_hash) */
      $id = db_insert('feeds_atom_file_import')
        ->fields(array(
        'fid' => $info['fid'],
        'sha1' => $new_file_hash,
      ))
        ->execute();
      return $info;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsEnclosureUnique::saveTo public function Saves the file represented by this enclosure to disk.