You are here

class FeedsHTTPFetcher in Feeds 7.2

Same name and namespace in other branches
  1. 6 plugins/FeedsHTTPFetcher.inc \FeedsHTTPFetcher
  2. 7 plugins/FeedsHTTPFetcher.inc \FeedsHTTPFetcher

Fetches data via HTTP.

Hierarchy

Expanded class hierarchy of FeedsHTTPFetcher

12 string references to 'FeedsHTTPFetcher'
FeedsFileHTTPTestCase::setUpImporter in tests/feeds_fetcher_http.test
Setup importer to import items from testing/feeds/nodes.csv.
FeedsFileHTTPTestCase::testFormValidation in tests/feeds_fetcher_http.test
Test the Feed URL form.
FeedsFileHTTPTestCase::testHTTPCacheDisabled in tests/feeds_fetcher_http.test
Tests if the data is not cached when the option for caching is disabled.
FeedsHooksTestCase::testHookPluginTypeConfigDefaults in tests/feeds_hooks.test
Tests the hook hook_PLUGIN_TYPE_config_defaults().
FeedsImporter::configDefaults in includes/FeedsImporter.inc
Return defaults for feed configuration.

... See full list

File

plugins/FeedsHTTPFetcher.inc, line 121

View source
class FeedsHTTPFetcher extends FeedsFetcher {

  /**
   * Implements FeedsFetcher::fetch().
   */
  public function fetch(FeedsSource $source) {
    $source_config = $source
      ->getConfigFor($this);
    if ($this->config['use_pubsubhubbub'] && ($raw = $this
      ->subscriber($source->feed_nid)
      ->receive())) {
      return new FeedsFetcherResult($raw);
    }
    $fetcher_result = new FeedsHTTPFetcherResult($source_config['source']);

    // When request_timeout is empty, the global value is used.
    $fetcher_result
      ->setTimeout($this->config['request_timeout']);
    $fetcher_result
      ->setAcceptInvalidCert($this->config['accept_invalid_cert']);
    $fetcher_result
      ->setCacheHttpResult($this->config['cache_http_result']);
    return $fetcher_result;
  }

  /**
   * Clear caches.
   */
  public function clear(FeedsSource $source) {
    $source_config = $source
      ->getConfigFor($this);
    $url = $source_config['source'];
    feeds_include_library('http_request.inc', 'http_request');
    http_request_clear_cache($url);
  }

  /**
   * Implements FeedsFetcher::request().
   */
  public function request($feed_nid = 0) {
    feeds_dbg($_GET);
    @feeds_dbg(file_get_contents('php://input'));

    // A subscription verification has been sent, verify.
    if (isset($_GET['hub_challenge'])) {
      $this
        ->subscriber($feed_nid)
        ->verifyRequest();
    }
    else {
      $source = feeds_source($this->id, $feed_nid);
      try {
        $source
          ->existing()
          ->import();
      } catch (Exception $e) {

        // In case of an error, respond with a 503 Service (temporary)
        // unavailable.
        $source
          ->log('import', 'An exception occurred: %exception', array(
          '%exception' => $e
            ->getMessage(),
        ), WATCHDOG_ERROR);
        header('HTTP/1.1 503 "Not Found"', NULL, 503);
        drupal_exit();
      }
    }

    // Will generate the default 200 response.
    header('HTTP/1.1 200 "OK"', NULL, 200);
    drupal_exit();
  }

  /**
   * Override parent::configDefaults().
   */
  public function configDefaults() {
    return array(
      'auto_detect_feeds' => FALSE,
      'use_pubsubhubbub' => FALSE,
      'designated_hub' => '',
      'request_timeout' => NULL,
      'auto_scheme' => 'http',
      'accept_invalid_cert' => FALSE,
      'cache_http_result' => TRUE,
    ) + parent::configDefaults();
  }

  /**
   * Override parent::configForm().
   */
  public function configForm(&$form_state) {
    $form = array();
    $form['auto_detect_feeds'] = array(
      '#type' => 'checkbox',
      '#title' => t('Auto detect feeds'),
      '#description' => t('If the supplied URL does not point to a feed but an HTML document, attempt to extract a feed URL from the document.'),
      '#default_value' => $this->config['auto_detect_feeds'],
    );
    $form['use_pubsubhubbub'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use PubSubHubbub'),
      '#description' => t('Attempt to use a <a href="http://en.wikipedia.org/wiki/PubSubHubbub">PubSubHubbub</a> subscription if available.'),
      '#default_value' => $this->config['use_pubsubhubbub'],
    );
    $form['advanced'] = array(
      '#title' => t('Advanced settings'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['advanced']['auto_scheme'] = array(
      '#type' => 'textfield',
      '#title' => t('Automatically add scheme'),
      '#description' => t('If the supplied URL does not contain the scheme, use this one automatically. Keep empty to force the user to input the scheme.'),
      '#default_value' => $this->config['auto_scheme'],
    );
    $form['advanced']['designated_hub'] = array(
      '#type' => 'textfield',
      '#title' => t('Designated hub'),
      '#description' => t('Enter the URL of a designated PubSubHubbub hub (e. g. superfeedr.com). If given, this hub will be used instead of the hub specified in the actual feed.'),
      '#default_value' => $this->config['designated_hub'],
      '#states' => array(
        'visible' => array(
          ':input[name="use_pubsubhubbub"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );

    // Per importer override of global http request timeout setting.
    $form['advanced']['request_timeout'] = array(
      '#type' => 'textfield',
      '#title' => t('Request timeout'),
      '#description' => t('Timeout in seconds to wait for an HTTP get request to finish.') . '<br />' . t('<strong>Note:</strong> if left empty, the global timeout setting will be used, which is @timeout seconds. You can set the global timeout setting by setting the variable "@variable".', array(
        '@timeout' => variable_get('http_request_timeout', 30),
        '@variable' => 'http_request_timeout',
      )),
      '#default_value' => $this->config['request_timeout'],
      '#element_validate' => array(
        'element_validate_integer_positive',
      ),
      '#maxlength' => 3,
      '#size' => 30,
    );
    $form['advanced']['accept_invalid_cert'] = array(
      '#type' => 'checkbox',
      '#title' => t('Accept invalid SSL certificates'),
      '#description' => t('<strong>IMPORTANT:</strong> This setting will force cURL to completely ignore all SSL errors. This is a <strong>major security risk</strong> and should only be used during development.'),
      '#default_value' => $this->config['accept_invalid_cert'],
    );
    $form['advanced']['cache_http_result'] = array(
      '#type' => 'checkbox',
      '#title' => t('Cache HTTP result of request'),
      '#description' => '<p>' . t('Disabling this cache means that the downloaded source will not be cached (for example: on the file system or on memcache), but will be redownloaded on every feeds import attempt. This can be helpful if the source to download is dynamically generated and will always be different, or if it is very large (50 MB+) which would cost additional webspace.') . '</p><p>' . t("If you're having issues with Feeds not processing changes from the source file, or if you are experiencing caching issues, you can disable the caching of this feeds content.") . '</p>',
      '#default_value' => $this->config['cache_http_result'],
    );
    return $form;
  }

  /**
   * Expose source form.
   */
  public function sourceForm($source_config) {
    $form = array();
    $form['source'] = array(
      '#type' => 'textfield',
      '#title' => t('URL'),
      '#description' => t('Enter a feed URL.'),
      '#default_value' => isset($source_config['source']) ? $source_config['source'] : '',
      '#maxlength' => NULL,
      '#required' => TRUE,
    );
    return $form;
  }

  /**
   * Override parent::sourceFormValidate().
   */
  public function sourceFormValidate(&$values) {
    $values['source'] = trim($values['source']);

    // Keep a copy for error messages.
    $original_url = $values['source'];
    $parts = parse_url($values['source']);
    if (empty($parts['scheme']) && $this->config['auto_scheme']) {
      $values['source'] = $this->config['auto_scheme'] . '://' . $values['source'];
    }
    if (!feeds_valid_url($values['source'], TRUE)) {
      $form_key = 'feeds][' . get_class($this) . '][source';
      form_set_error($form_key, t('The URL %source is invalid.', array(
        '%source' => $original_url,
      )));
    }
    elseif ($this->config['auto_detect_feeds']) {
      feeds_include_library('http_request.inc', 'http_request');
      $url = http_request_get_common_syndication($values['source'], array(
        'accept_invalid_cert' => $this->config['accept_invalid_cert'],
      ));
      if ($url) {
        $values['source'] = $url;
      }
    }
  }

  /**
   * Override sourceSave() - subscribe to hub.
   */
  public function sourceSave(FeedsSource $source) {
    if ($this->config['use_pubsubhubbub']) {

      // If this is a feeds node we want to delay the subscription to
      // feeds_exit() to avoid transaction race conditions.
      if ($source->feed_nid) {
        $job = array(
          'fetcher' => $this,
          'source' => $source,
        );
        feeds_set_subscription_job($job);
      }
      else {
        $this
          ->subscribe($source);
      }
    }
  }

  /**
   * Override sourceDelete() - unsubscribe from hub.
   */
  public function sourceDelete(FeedsSource $source) {
    if ($this->config['use_pubsubhubbub']) {

      // If we're in a feed node, queue the unsubscribe,
      // else process immediately.
      if ($source->feed_nid) {
        $job = array(
          'type' => $source->id,
          'id' => $source->feed_nid,
          'period' => 0,
          'periodic' => FALSE,
        );
        JobScheduler::get('feeds_push_unsubscribe')
          ->set($job);
      }
      else {
        $this
          ->unsubscribe($source);
      }
    }
  }

  /**
   * Implement FeedsFetcher::subscribe() - subscribe to hub.
   */
  public function subscribe(FeedsSource $source) {
    $source_config = $source
      ->getConfigFor($this);
    $this
      ->subscriber($source->feed_nid)
      ->subscribe($source_config['source'], url($this
      ->path($source->feed_nid), array(
      'absolute' => TRUE,
    )), valid_url($this->config['designated_hub']) ? $this->config['designated_hub'] : '');
  }

  /**
   * Implement FeedsFetcher::unsubscribe() - unsubscribe from hub.
   */
  public function unsubscribe(FeedsSource $source) {
    $source_config = $source
      ->getConfigFor($this);
    $this
      ->subscriber($source->feed_nid)
      ->unsubscribe($source_config['source'], url($this
      ->path($source->feed_nid), array(
      'absolute' => TRUE,
    )));
  }

  /**
   * Implement FeedsFetcher::importPeriod().
   */
  public function importPeriod(FeedsSource $source) {
    if ($this
      ->subscriber($source->feed_nid)
      ->subscribed()) {

      // Delay for three days if there is a successful subscription.
      return 259200;
    }
  }

  /**
   * Convenience method for instantiating a subscriber object.
   */
  protected function subscriber($subscriber_id) {
    return PushSubscriber::instance($this->id, $subscriber_id, 'PuSHSubscription', PuSHEnvironment::instance());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FeedsConfigurable::$config protected property Holds the actual configuration information.
FeedsConfigurable::$disabled protected property CTools export enabled status of this object.
FeedsConfigurable::$export_type protected property CTools export type of this object.
FeedsConfigurable::$id protected property An unique identifier for the configuration.
FeedsConfigurable::addConfig public function Similar to setConfig but adds to existing configuration.
FeedsConfigurable::configFormSubmit public function Submission handler for configForm(). 2
FeedsConfigurable::configFormValidate public function Validation handler for configForm(). 3
FeedsConfigurable::copy public function Copy a configuration. 1
FeedsConfigurable::doesExist public function Determine whether this object is persistent. 1
FeedsConfigurable::existing public function Determines whether this object is persistent and enabled. 1
FeedsConfigurable::getConfig public function Implements getConfig(). 1
FeedsConfigurable::hasConfigForm public function Returns whether or not the configurable has a config form.
FeedsConfigurable::isEnabled public function Determine whether this object is enabled.
FeedsConfigurable::setConfig public function Set configuration.
FeedsConfigurable::validateConfig public function Validates the configuration. 2
FeedsConfigurable::__get public function Overrides magic method __get().
FeedsConfigurable::__isset public function Override magic method __isset(). This is needed due to overriding __get().
FeedsFetcher::afterImport public function Invoked after an import is finished. 1
FeedsFetcher::menuItem public function Menu item definition for fetchers of this class. Note how the path component in the item definition matches the return value of FeedsFetcher::path().
FeedsFetcher::path public function Construct a path for a concrete fetcher/source combination. The result of this method matches up with the general path definition in FeedsFetcher::menuItem(). For example usage look at FeedsHTTPFetcher.
FeedsFetcher::pluginType public function Implements FeedsPlugin::pluginType(). Overrides FeedsPlugin::pluginType
FeedsHTTPFetcher::clear public function Clear caches. Overrides FeedsFetcher::clear
FeedsHTTPFetcher::configDefaults public function Override parent::configDefaults(). Overrides FeedsPlugin::configDefaults
FeedsHTTPFetcher::configForm public function Override parent::configForm(). Overrides FeedsConfigurable::configForm
FeedsHTTPFetcher::fetch public function Implements FeedsFetcher::fetch(). Overrides FeedsFetcher::fetch
FeedsHTTPFetcher::importPeriod public function Implement FeedsFetcher::importPeriod(). Overrides FeedsFetcher::importPeriod
FeedsHTTPFetcher::request public function Implements FeedsFetcher::request(). Overrides FeedsFetcher::request
FeedsHTTPFetcher::sourceDelete public function Override sourceDelete() - unsubscribe from hub. Overrides FeedsPlugin::sourceDelete
FeedsHTTPFetcher::sourceForm public function Expose source form. Overrides FeedsPlugin::sourceForm
FeedsHTTPFetcher::sourceFormValidate public function Override parent::sourceFormValidate(). Overrides FeedsPlugin::sourceFormValidate
FeedsHTTPFetcher::sourceSave public function Override sourceSave() - subscribe to hub. Overrides FeedsPlugin::sourceSave
FeedsHTTPFetcher::subscribe public function Implement FeedsFetcher::subscribe() - subscribe to hub. Overrides FeedsFetcher::subscribe
FeedsHTTPFetcher::subscriber protected function Convenience method for instantiating a subscriber object.
FeedsHTTPFetcher::unsubscribe public function Implement FeedsFetcher::unsubscribe() - unsubscribe from hub. Overrides FeedsFetcher::unsubscribe
FeedsPlugin::$pluginDefinition protected property The plugin definition.
FeedsPlugin::all public static function Get all available plugins.
FeedsPlugin::byType public static function Gets all available plugins of a particular type.
FeedsPlugin::child public static function Determines whether given plugin is derived from given base plugin.
FeedsPlugin::dependencies public function Implements FeedsConfigurable::dependencies(). Overrides FeedsConfigurable::dependencies 1
FeedsPlugin::hasSourceConfig public function Returns TRUE if $this->sourceForm() returns a form. Overrides FeedsSourceInterface::hasSourceConfig
FeedsPlugin::instance public static function Instantiates a FeedsPlugin object. Overrides FeedsConfigurable::instance
FeedsPlugin::loadMappers public static function Loads on-behalf implementations from mappers/ directory.
FeedsPlugin::pluginDefinition public function Returns the plugin definition.
FeedsPlugin::save public function Save changes to the configuration of this object. Delegate saving to parent (= Feed) which will collect information from this object by way of getConfig() and store it. Overrides FeedsConfigurable::save 1
FeedsPlugin::setPluginDefinition protected function Sets the plugin definition.
FeedsPlugin::sourceDefaults public function Implements FeedsSourceInterface::sourceDefaults(). Overrides FeedsSourceInterface::sourceDefaults 1
FeedsPlugin::typeOf public static function Determines the type of a plugin.
FeedsPlugin::__construct protected function Constructs a FeedsPlugin object. Overrides FeedsConfigurable::__construct