Projet

Général

Profil

Paste
Télécharger (7,88 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / aggregator / aggregator.processor.inc @ 2196f227

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
 * @param $length
136
 *   The desired length of teaser text, in bytes.
137
 *
138
 * @return
139
 *   A translated string explaining the teaser string length.
140
 */
141
function _aggregator_characters($length) {
142
  return ($length == 0) ? t('Unlimited') : format_plural($length, '1 character', '@count characters');
143
}
144

    
145
/**
146
 * Adds/edits/deletes an aggregator item.
147
 *
148
 * @param $edit
149
 *   An associative array describing the item to be added/edited/deleted.
150
 */
151
function aggregator_save_item($edit) {
152
  if ($edit['title'] && empty($edit['iid'])) {
153
    $edit['iid'] = db_insert('aggregator_item')
154
      ->fields(array(
155
        'title' => $edit['title'],
156
        'link' => $edit['link'],
157
        'author' => $edit['author'],
158
        'description' => $edit['description'],
159
        'guid' => $edit['guid'],
160
        'timestamp' => $edit['timestamp'],
161
        'fid' => $edit['fid'],
162
      ))
163
      ->execute();
164
  }
165
  if ($edit['iid'] && !$edit['title']) {
166
    db_delete('aggregator_item')
167
      ->condition('iid', $edit['iid'])
168
      ->execute();
169
    db_delete('aggregator_category_item')
170
      ->condition('iid', $edit['iid'])
171
      ->execute();
172
  }
173
  elseif ($edit['title'] && $edit['link']) {
174
    // file the items in the categories indicated by the feed
175
    $result = db_query('SELECT cid FROM {aggregator_category_feed} WHERE fid = :fid', array(':fid' => $edit['fid']));
176
    foreach ($result as $category) {
177
      db_merge('aggregator_category_item')
178
        ->key(array(
179
          'iid' => $edit['iid'],
180
          'cid' => $category->cid,
181
        ))
182
        ->execute();
183
    }
184
  }
185
}
186

    
187
/**
188
 * Expires items from a feed depending on expiration settings.
189
 *
190
 * @param $feed
191
 *   Object describing feed.
192
 */
193
function aggregator_expire($feed) {
194
  $aggregator_clear = variable_get('aggregator_clear', 9676800);
195

    
196
  if ($aggregator_clear != AGGREGATOR_CLEAR_NEVER) {
197
    // Remove all items that are older than flush item timer.
198
    $age = REQUEST_TIME - $aggregator_clear;
199
    $iids = db_query('SELECT iid FROM {aggregator_item} WHERE fid = :fid AND timestamp < :timestamp', array(
200
      ':fid' => $feed->fid,
201
      ':timestamp' => $age,
202
    ))
203
    ->fetchCol();
204
    if ($iids) {
205
      db_delete('aggregator_category_item')
206
        ->condition('iid', $iids, 'IN')
207
        ->execute();
208
      db_delete('aggregator_item')
209
        ->condition('iid', $iids, 'IN')
210
        ->execute();
211
    }
212
  }
213
}