Skip to content
Snippets Groups Projects
FeedsSource.inc 35.4 KiB
Newer Older
    // Ensure that the source is persistent (= defined in DB).
    return parent::existing();
   * Returns the configuration for a specific client class.
   *
   * @param FeedsSourceInterface $client
   *   An object that is an implementer of FeedsSourceInterface.
   *
   * @return
   *   An array stored for $client.
   */
  public function getConfigFor(FeedsSourceInterface $client) {
    $class = get_class($client);
    return isset($this->config[$class]) ? $this->config[$class] : $client->sourceDefaults();
  /**
   * Sets the configuration for a specific client class.
   *
   * @param FeedsSourceInterface $client
   *   An object that is an implementer of FeedsSourceInterface.
   * @param $config
   *   The configuration for $client.
   *
   * @return
   *   An array stored for $client.
   */
  public function setConfigFor(FeedsSourceInterface $client, $config) {
    $this->config[get_class($client)] = $config;
  }

  /**
   * Return defaults for feed configuration.
   */
  public function configDefaults() {
    // Collect information from plugins.
    $defaults = array();
    foreach ($this->importer->plugin_types as $type) {
      if ($this->importer->$type->hasSourceConfig()) {
        $defaults[get_class($this->importer->$type)] = $this->importer->$type->sourceDefaults();
      }
    }
    return $defaults;
  }

  /**
   * Override parent::configForm().
   */
  public function configForm(&$form_state) {
    // Collect information from plugins.
    $form = array();
    foreach ($this->importer->plugin_types as $type) {
      if ($this->importer->$type->hasSourceConfig()) {
        $class = get_class($this->importer->$type);
Alex Barth's avatar
Alex Barth committed
        $config = isset($this->config[$class]) ? $this->config[$class] : array();
        $form[$class] = $this->importer->$type->sourceForm($config);
        $form[$class]['#tree'] = TRUE;
      }
    }
    return $form;
  }

  /**
   * Override parent::configFormValidate().
   */
  public function configFormValidate(&$values) {
    foreach ($this->importer->plugin_types as $type) {
      $class = get_class($this->importer->$type);
      if (isset($values[$class]) && $this->importer->$type->hasSourceConfig()) {
        $this->importer->$type->sourceFormValidate($values[$class]);
      }
    }
  }
Alex Barth's avatar
Alex Barth committed
  /**
   * Writes to feeds log.
   */
  public function log($type, $message, $variables = array(), $severity = WATCHDOG_NOTICE) {
    feeds_log($this->id, $this->feed_nid, $type, $message, $variables, $severity);
  }

   * Background job helper. Starts a background job using the Drupal queue.
   * @see FeedsSource::startImport()
   * @see FeedsSource::startClear()
   *   Method to execute on importer; one of 'import' or 'clear'.
   */
  protected function startBackgroundJob($method) {
    $job = array(
      'type' => $this->id,
      'id' => $this->feed_nid,
    );
    $queue = DrupalQueue::get('feeds_source_' . $method);
    $queue->createItem($job);

    switch ($method) {
      case 'import':
        $state = $this->state(FEEDS_PARSE);
        break;

      case 'clear':
        $state = $this->state(FEEDS_PROCESS_CLEAR);
        break;

      case 'expire':
        $state = $this->state(FEEDS_PROCESS_EXPIRE);
        break;
    }

    if (isset($state)) {
      $state->progress = 0;
      $this->save();
    }
  }

  /**
   * Batch API helper. Starts a Batch API job.
   *
   * @see FeedsSource::startImport()
   * @see FeedsSource::startClear()
   * @see feeds_batch()
   *
   * @param $title
   *   Title to show to user when executing batch.
   * @param $method
   *   Method to execute on importer; one of 'import' or 'clear'.
   */
  protected function startBatchAPIJob($title, $method) {
    $batch = array(
      'title' => $title,
      'operations' => array(
        array('feeds_batch', array($method, $this->id, $this->feed_nid)),
      ),
      'progress_message' => '',
    );
    batch_set($batch);
  }

  /**
   * Acquires a lock for this source.
   *
   * @throws FeedsLockException
   *   If a lock for the requested job could not be acquired.
   */
  protected function acquireLock() {
    if (!lock_acquire("feeds_source_{$this->id}_{$this->feed_nid}", 60.0)) {
      throw new FeedsLockException(t('Cannot acquire lock for source @id / @feed_nid.', array('@id' => $this->id, '@feed_nid' => $this->feed_nid)));
    }
  }

  /**
   * Releases a lock for this source.
   */
  protected function releaseLock() {
    lock_release("feeds_source_{$this->id}_{$this->feed_nid}");

    // Switch back to original account.
    $this->switchBack();
  }

  /**
   * Switches to the owner of the feed or user 1 if the importer is not attached
   * to a content type.
   */
  protected function switchAccount() {
    // Use author of feed node.
    if ($this->feed_nid) {
      $node = node_load($this->feed_nid);
      if (!empty($node->uid)) {
        $account = user_load($node->uid);
      }
    }

    // If the owner of the feed node is anonymous or if the importer is not
    // attached to a content type, pick user 1 instead.
    if (empty($account)) {
      $account = user_load(1);
    }

    $this->accountSwitcher->switchTo($account);
  }

  /**
   * Switches back to the original user.
   */
  protected function switchBack() {
    $this->accountSwitcher->switchBack();
  /**
   * Implements FeedsConfigurable::dependencies().
   */
  public function dependencies() {
    $dependencies = parent::dependencies();
    return array_merge($dependencies, $this->importer()->dependencies());
  }