Drupal PHP Cross Reference Content Management Systems

Source: /modules/aggregator/aggregator.processor.inc - 207 lines - 7924 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Processor functions for the aggregator module.
   6   */
   7  
   8  /**
   9   * Implements hook_aggregator_process_info().
  10   */
  11  function aggregator_aggregator_process_info() {
  12    return array(
  13      'title' => t('Default processor'),
  14      'description' => t('Creates lightweight records from feed items.'),
  15    );
  16  }
  17  
  18  /**
  19   * Implements hook_aggregator_process().
  20   */
  21  function aggregator_aggregator_process($feed) {
  22    if (is_object($feed)) {
  23      if (is_array($feed->items)) {
  24        foreach ($feed->items as $item) {
  25          // Save this item. Try to avoid duplicate entries as much as possible. If
  26          // we find a duplicate entry, we resolve it and pass along its ID is such
  27          // that we can update it if needed.
  28          if (!empty($item['guid'])) {
  29            $entry = db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = :fid AND guid = :guid", array(':fid' => $feed->fid, ':guid' => $item['guid']))->fetchObject();
  30          }
  31          elseif ($item['link'] && $item['link'] != $feed->link && $item['link'] != $feed->url) {
  32            $entry = db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = :fid AND link = :link", array(':fid' => $feed->fid, ':link' => $item['link']))->fetchObject();
  33          }
  34          else {
  35            $entry = db_query("SELECT iid, timestamp FROM {aggregator_item} WHERE fid = :fid AND title = :title", array(':fid' => $feed->fid, ':title' => $item['title']))->fetchObject();
  36          }
  37          if (!$item['timestamp']) {
  38            $item['timestamp'] = isset($entry->timestamp) ? $entry->timestamp : REQUEST_TIME;
  39          }
  40  
  41          // Make sure the item title and author fit in the 255 varchar column.
  42          $item['title'] = truncate_utf8($item['title'], 255, TRUE, TRUE);
  43          $item['author'] = truncate_utf8($item['author'], 255, TRUE, TRUE);
  44          aggregator_save_item(array('iid' => (isset($entry->iid) ? $entry->iid : ''), 'fid' => $feed->fid, 'timestamp' => $item['timestamp'], 'title' => $item['title'], 'link' => $item['link'], 'author' => $item['author'], 'description' => $item['description'], 'guid' => $item['guid']));
  45        }
  46      }
  47    }
  48  }
  49  
  50  /**
  51   * Implements hook_aggregator_remove().
  52   */
  53  function aggregator_aggregator_remove($feed) {
  54    $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->fid))->fetchCol();
  55    if ($iids) {
  56      db_delete('aggregator_category_item')
  57        ->condition('iid', $iids, 'IN')
  58        ->execute();
  59    }
  60    db_delete('aggregator_item')
  61      ->condition('fid', $feed->fid)
  62      ->execute();
  63  
  64    drupal_set_message(t('The news items from %site have been removed.', array('%site' => $feed->title)));
  65  }
  66  
  67  /**
  68   * Implements hook_form_aggregator_admin_form_alter().
  69   *
  70   * Form alter aggregator module's own form to keep processor functionality
  71   * separate from aggregator API functionality.
  72   */
  73  function aggregator_form_aggregator_admin_form_alter(&$form, $form_state) {
  74    if (in_array('aggregator', variable_get('aggregator_processors', array('aggregator')))) {
  75      $info = module_invoke('aggregator', 'aggregator_process', 'info');
  76      $items = drupal_map_assoc(array(3, 5, 10, 15, 20, 25), '_aggregator_items');
  77      $period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval');
  78      $period[AGGREGATOR_CLEAR_NEVER] = t('Never');
  79  
  80      // Only wrap into a collapsible fieldset if there is a basic configuration.
  81      if (isset($form['basic_conf'])) {
  82        $form['modules']['aggregator'] = array(
  83          '#type' => 'fieldset',
  84          '#title' => t('Default processor settings'),
  85          '#description' => $info['description'],
  86          '#collapsible' => TRUE,
  87          '#collapsed' => !in_array('aggregator', variable_get('aggregator_processors', array('aggregator'))),
  88        );
  89      }
  90      else {
  91        $form['modules']['aggregator'] = array();
  92      }
  93  
  94      $form['modules']['aggregator']['aggregator_summary_items'] = array(
  95        '#type' => 'select',
  96        '#title' => t('Number of items shown in listing pages'),
  97        '#default_value' => variable_get('aggregator_summary_items', 3),
  98        '#empty_value' => 0,
  99        '#options' => $items,
 100      );
 101  
 102      $form['modules']['aggregator']['aggregator_clear'] = array(
 103        '#type' => 'select',
 104        '#title' => t('Discard items older than'),
 105        '#default_value' => variable_get('aggregator_clear', 9676800),
 106        '#options' => $period,
 107        '#description' => t('Requires a correctly configured <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status'))),
 108      );
 109  
 110      $form['modules']['aggregator']['aggregator_category_selector'] = array(
 111        '#type' => 'radios',
 112        '#title' => t('Select categories using'),
 113        '#default_value' => variable_get('aggregator_category_selector', 'checkboxes'),
 114        '#options' => array('checkboxes' => t('checkboxes'),
 115        'select' => t('multiple selector')),
 116        '#description' => t('For a small number of categories, checkboxes are easier to use, while a multiple selector works well with large numbers of categories.'),
 117      );
 118      $form['modules']['aggregator']['aggregator_teaser_length'] = array(
 119        '#type' => 'select',
 120        '#title' => t('Length of trimmed description'),
 121        '#default_value' => variable_get('aggregator_teaser_length', 600),
 122        '#options' => drupal_map_assoc(array(0, 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000), '_aggregator_characters'),
 123        '#description' => t("The maximum number of characters used in the trimmed version of content.")
 124      );
 125  
 126    }
 127  }
 128  
 129  /**
 130   * Creates display text for teaser length option values.
 131   *
 132   * Callback for drupal_map_assoc() within
 133   * aggregator_form_aggregator_admin_form_alter().
 134   */
 135  function _aggregator_characters($length) {
 136    return ($length == 0) ? t('Unlimited') : format_plural($length, '1 character', '@count characters');
 137  }
 138  
 139  /**
 140   * Adds/edits/deletes an aggregator item.
 141   *
 142   * @param $edit
 143   *   An associative array describing the item to be added/edited/deleted.
 144   */
 145  function aggregator_save_item($edit) {
 146    if ($edit['title'] && empty($edit['iid'])) {
 147      $edit['iid'] = db_insert('aggregator_item')
 148        ->fields(array(
 149          'title' => $edit['title'],
 150          'link' => $edit['link'],
 151          'author' => $edit['author'],
 152          'description' => $edit['description'],
 153          'guid' => $edit['guid'],
 154          'timestamp' => $edit['timestamp'],
 155          'fid' => $edit['fid'],
 156        ))
 157        ->execute();
 158    }
 159    if ($edit['iid'] && !$edit['title']) {
 160      db_delete('aggregator_item')
 161        ->condition('iid', $edit['iid'])
 162        ->execute();
 163      db_delete('aggregator_category_item')
 164        ->condition('iid', $edit['iid'])
 165        ->execute();
 166    }
 167    elseif ($edit['title'] && $edit['link']) {
 168      // file the items in the categories indicated by the feed
 169      $result = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = :fid', array(':fid' => $edit['fid']));
 170      foreach ($result as $category) {
 171        db_merge('aggregator_category_item')
 172          ->key(array(
 173            'iid' => $edit['iid'],
 174            'cid' => $category->cid,
 175          ))
 176          ->execute();
 177      }
 178    }
 179  }
 180  
 181  /**
 182   * Expires items from a feed depending on expiration settings.
 183   *
 184   * @param $feed
 185   *   Object describing feed.
 186   */
 187  function aggregator_expire($feed) {
 188    $aggregator_clear = variable_get('aggregator_clear', 9676800);
 189  
 190    if ($aggregator_clear != AGGREGATOR_CLEAR_NEVER) {
 191      // Remove all items that are older than flush item timer.
 192      $age = REQUEST_TIME - $aggregator_clear;
 193      $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid AND timestamp < :timestamp', array(
 194        ':fid' => $feed->fid,
 195        ':timestamp' => $age,
 196      ))
 197      ->fetchCol();
 198      if ($iids) {
 199        db_delete('aggregator_category_item')
 200          ->condition('iid', $iids, 'IN')
 201          ->execute();
 202        db_delete('aggregator_item')
 203          ->condition('iid', $iids, 'IN')
 204          ->execute();
 205      }
 206    }
 207  }

title

Description

title

Description

title

Description

title

title

Body