1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Class definition of FeedsNodeProcessor.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Creates nodes from feed items.
|
10
|
*/
|
11
|
class FeedsNodeProcessor extends FeedsProcessor {
|
12
|
|
13
|
/**
|
14
|
* Define entity type.
|
15
|
*/
|
16
|
public function entityType() {
|
17
|
return 'node';
|
18
|
}
|
19
|
|
20
|
/**
|
21
|
* Implements parent::entityInfo().
|
22
|
*/
|
23
|
protected function entityInfo() {
|
24
|
$info = parent::entityInfo();
|
25
|
$info['label plural'] = t('Nodes');
|
26
|
return $info;
|
27
|
}
|
28
|
|
29
|
/**
|
30
|
* Creates a new node in memory and returns it.
|
31
|
*/
|
32
|
protected function newEntity(FeedsSource $source) {
|
33
|
$node = new stdClass();
|
34
|
$node->type = $this->bundle();
|
35
|
$node->changed = REQUEST_TIME;
|
36
|
$node->created = REQUEST_TIME;
|
37
|
$node->language = LANGUAGE_NONE;
|
38
|
$node->is_new = TRUE;
|
39
|
node_object_prepare($node);
|
40
|
// Populate properties that are set by node_object_prepare().
|
41
|
$node->log = 'Created by FeedsNodeProcessor';
|
42
|
$node->uid = $this->config['author'];
|
43
|
return $node;
|
44
|
}
|
45
|
|
46
|
/**
|
47
|
* Loads an existing node.
|
48
|
*
|
49
|
* If the update existing method is not FEEDS_UPDATE_EXISTING, only the node
|
50
|
* table will be loaded, foregoing the node_load API for better performance.
|
51
|
*
|
52
|
* @todo Reevaluate the use of node_object_prepare().
|
53
|
*/
|
54
|
protected function entityLoad(FeedsSource $source, $nid) {
|
55
|
$node = parent::entityLoad($source, $nid);
|
56
|
|
57
|
if ($this->config['update_existing'] != FEEDS_UPDATE_EXISTING) {
|
58
|
$node->uid = $this->config['author'];
|
59
|
}
|
60
|
|
61
|
node_object_prepare($node);
|
62
|
|
63
|
// Workaround for issue #1247506. See #1245094 for backstory.
|
64
|
if (!empty($node->menu)) {
|
65
|
// If the node has a menu item(with a valid mlid) it must be flagged
|
66
|
// 'enabled'.
|
67
|
$node->menu['enabled'] = (int) (bool) $node->menu['mlid'];
|
68
|
}
|
69
|
|
70
|
// Populate properties that are set by node_object_prepare().
|
71
|
if ($this->config['update_existing'] == FEEDS_UPDATE_EXISTING) {
|
72
|
$node->log = 'Updated by FeedsNodeProcessor';
|
73
|
}
|
74
|
else {
|
75
|
$node->log = 'Replaced by FeedsNodeProcessor';
|
76
|
}
|
77
|
return $node;
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Check that the user has permission to save a node.
|
82
|
*/
|
83
|
protected function entitySaveAccess($entity) {
|
84
|
|
85
|
// The check will be skipped for anonymous nodes.
|
86
|
if ($this->config['authorize'] && !empty($entity->uid)) {
|
87
|
|
88
|
$author = user_load($entity->uid);
|
89
|
|
90
|
// If the uid was mapped directly, rather than by email or username, it
|
91
|
// could be invalid.
|
92
|
if (!$author) {
|
93
|
$message = 'User %uid is not a valid user.';
|
94
|
throw new FeedsAccessException(t($message, array('%uid' => $entity->uid)));
|
95
|
}
|
96
|
|
97
|
if (empty($entity->nid) || !empty($entity->is_new)) {
|
98
|
$op = 'create';
|
99
|
$access = node_access($op, $entity->type, $author);
|
100
|
}
|
101
|
else {
|
102
|
$op = 'update';
|
103
|
$access = node_access($op, $entity, $author);
|
104
|
}
|
105
|
|
106
|
if (!$access) {
|
107
|
$message = 'User %name is not authorized to %op content type %content_type.';
|
108
|
throw new FeedsAccessException(t($message, array('%name' => $author->name, '%op' => $op, '%content_type' => $entity->type)));
|
109
|
}
|
110
|
}
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
* Validates a node.
|
115
|
*/
|
116
|
protected function entityValidate($entity) {
|
117
|
if (!isset($entity->uid) || !is_numeric($entity->uid)) {
|
118
|
$entity->uid = $this->config['author'];
|
119
|
}
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Save a node.
|
124
|
*/
|
125
|
public function entitySave($entity) {
|
126
|
node_save($entity);
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Delete a series of nodes.
|
131
|
*/
|
132
|
protected function entityDeleteMultiple($nids) {
|
133
|
node_delete_multiple($nids);
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Implement expire().
|
138
|
*
|
139
|
* @todo: move to processor stage?
|
140
|
*/
|
141
|
public function expire($time = NULL) {
|
142
|
if ($time === NULL) {
|
143
|
$time = $this->expiryTime();
|
144
|
}
|
145
|
if ($time == FEEDS_EXPIRE_NEVER) {
|
146
|
return;
|
147
|
}
|
148
|
$count = $this->getLimit();
|
149
|
$nodes = db_query_range("SELECT n.nid FROM {node} n JOIN {feeds_item} fi ON fi.entity_type = 'node' AND n.nid = fi.entity_id WHERE fi.id = :id AND n.created < :created", 0, $count, array(':id' => $this->id, ':created' => REQUEST_TIME - $time));
|
150
|
$nids = array();
|
151
|
foreach ($nodes as $node) {
|
152
|
$nids[$node->nid] = $node->nid;
|
153
|
}
|
154
|
$this->entityDeleteMultiple($nids);
|
155
|
if (db_query_range("SELECT 1 FROM {node} n JOIN {feeds_item} fi ON fi.entity_type = 'node' AND n.nid = fi.entity_id WHERE fi.id = :id AND n.created < :created", 0, 1, array(':id' => $this->id, ':created' => REQUEST_TIME - $time))->fetchField()) {
|
156
|
return FEEDS_BATCH_ACTIVE;
|
157
|
}
|
158
|
return FEEDS_BATCH_COMPLETE;
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* Return expiry time.
|
163
|
*/
|
164
|
public function expiryTime() {
|
165
|
return $this->config['expire'];
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Override parent::configDefaults().
|
170
|
*/
|
171
|
public function configDefaults() {
|
172
|
return array(
|
173
|
'expire' => FEEDS_EXPIRE_NEVER,
|
174
|
'author' => 0,
|
175
|
'authorize' => TRUE,
|
176
|
) + parent::configDefaults();
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* Override parent::configForm().
|
181
|
*/
|
182
|
public function configForm(&$form_state) {
|
183
|
$form = parent::configForm($form_state);
|
184
|
|
185
|
$author = user_load($this->config['author']);
|
186
|
$form['author'] = array(
|
187
|
'#type' => 'textfield',
|
188
|
'#title' => t('Author'),
|
189
|
'#description' => t('Select the author of the nodes to be created - leave empty to assign "anonymous".'),
|
190
|
'#autocomplete_path' => 'user/autocomplete',
|
191
|
'#default_value' => empty($author->name) ? 'anonymous' : check_plain($author->name),
|
192
|
);
|
193
|
$form['authorize'] = array(
|
194
|
'#type' => 'checkbox',
|
195
|
'#title' => t('Authorize'),
|
196
|
'#description' => t('Check that the author has permission to create the node.'),
|
197
|
'#default_value' => $this->config['authorize'],
|
198
|
);
|
199
|
$period = drupal_map_assoc(array(FEEDS_EXPIRE_NEVER, 3600, 10800, 21600, 43200, 86400, 259200, 604800, 2592000, 2592000 * 3, 2592000 * 6, 31536000), 'feeds_format_expire');
|
200
|
$form['expire'] = array(
|
201
|
'#type' => 'select',
|
202
|
'#title' => t('Expire nodes'),
|
203
|
'#options' => $period,
|
204
|
'#description' => t('Select after how much time nodes should be deleted. The node\'s published date will be used for determining the node\'s age, see Mapping settings.'),
|
205
|
'#default_value' => $this->config['expire'],
|
206
|
);
|
207
|
return $form;
|
208
|
}
|
209
|
|
210
|
/**
|
211
|
* Override parent::configFormValidate().
|
212
|
*/
|
213
|
public function configFormValidate(&$values) {
|
214
|
if ($author = user_load_by_name($values['author'])) {
|
215
|
$values['author'] = $author->uid;
|
216
|
}
|
217
|
else {
|
218
|
$values['author'] = 0;
|
219
|
}
|
220
|
}
|
221
|
|
222
|
/**
|
223
|
* Reschedule if expiry time changes.
|
224
|
*/
|
225
|
public function configFormSubmit(&$values) {
|
226
|
if ($this->config['expire'] != $values['expire']) {
|
227
|
feeds_reschedule($this->id);
|
228
|
}
|
229
|
parent::configFormSubmit($values);
|
230
|
}
|
231
|
|
232
|
/**
|
233
|
* Override setTargetElement to operate on a target item that is a node.
|
234
|
*/
|
235
|
public function setTargetElement(FeedsSource $source, $target_node, $target_element, $value) {
|
236
|
switch ($target_element) {
|
237
|
case 'created':
|
238
|
$target_node->created = feeds_to_unixtime($value, REQUEST_TIME);
|
239
|
break;
|
240
|
case 'feeds_source':
|
241
|
// Get the class of the feed node importer's fetcher and set the source
|
242
|
// property. See feeds_node_update() how $node->feeds gets stored.
|
243
|
if ($id = feeds_get_importer_id($this->bundle())) {
|
244
|
$class = get_class(feeds_importer($id)->fetcher);
|
245
|
$target_node->feeds[$class]['source'] = $value;
|
246
|
// This effectively suppresses 'import on submission' feature.
|
247
|
// See feeds_node_insert().
|
248
|
$target_node->feeds['suppress_import'] = TRUE;
|
249
|
}
|
250
|
break;
|
251
|
case 'user_name':
|
252
|
if ($user = user_load_by_name($value)) {
|
253
|
$target_node->uid = $user->uid;
|
254
|
}
|
255
|
break;
|
256
|
case 'user_mail':
|
257
|
if ($user = user_load_by_mail($value)) {
|
258
|
$target_node->uid = $user->uid;
|
259
|
}
|
260
|
break;
|
261
|
default:
|
262
|
parent::setTargetElement($source, $target_node, $target_element, $value);
|
263
|
break;
|
264
|
}
|
265
|
}
|
266
|
|
267
|
/**
|
268
|
* Return available mapping targets.
|
269
|
*/
|
270
|
public function getMappingTargets() {
|
271
|
$type = node_type_get_type($this->bundle());
|
272
|
|
273
|
$targets = parent::getMappingTargets();
|
274
|
if ($type && $type->has_title) {
|
275
|
$targets['title'] = array(
|
276
|
'name' => t('Title'),
|
277
|
'description' => t('The title of the node.'),
|
278
|
'optional_unique' => TRUE,
|
279
|
);
|
280
|
}
|
281
|
$targets['nid'] = array(
|
282
|
'name' => t('Node ID'),
|
283
|
'description' => t('The nid of the node. NOTE: use this feature with care, node ids are usually assigned by Drupal.'),
|
284
|
'optional_unique' => TRUE,
|
285
|
);
|
286
|
$targets['uid'] = array(
|
287
|
'name' => t('User ID'),
|
288
|
'description' => t('The Drupal user ID of the node author.'),
|
289
|
);
|
290
|
$targets['user_name'] = array(
|
291
|
'name' => t('Username'),
|
292
|
'description' => t('The Drupal username of the node author.'),
|
293
|
);
|
294
|
$targets['user_mail'] = array(
|
295
|
'name' => t('User email'),
|
296
|
'description' => t('The email address of the node author.'),
|
297
|
);
|
298
|
$targets['status'] = array(
|
299
|
'name' => t('Published status'),
|
300
|
'description' => t('Whether a node is published or not. 1 stands for published, 0 for not published.'),
|
301
|
);
|
302
|
$targets['created'] = array(
|
303
|
'name' => t('Published date'),
|
304
|
'description' => t('The UNIX time when a node has been published.'),
|
305
|
);
|
306
|
$targets['promote'] = array(
|
307
|
'name' => t('Promoted to front page'),
|
308
|
'description' => t('Boolean value, whether or not node is promoted to front page. (1 = promoted, 0 = not promoted)'),
|
309
|
);
|
310
|
$targets['sticky'] = array(
|
311
|
'name' => t('Sticky'),
|
312
|
'description' => t('Boolean value, whether or not node is sticky at top of lists. (1 = sticky, 0 = not sticky)'),
|
313
|
);
|
314
|
|
315
|
// Include language field if Locale module is enabled.
|
316
|
if (module_exists('locale')) {
|
317
|
$targets['language'] = array(
|
318
|
'name' => t('Language'),
|
319
|
'description' => t('The two-character language code of the node.'),
|
320
|
);
|
321
|
}
|
322
|
|
323
|
// Include comment field if Comment module is enabled.
|
324
|
if (module_exists('comment')) {
|
325
|
$targets['comment'] = array(
|
326
|
'name' => t('Comments'),
|
327
|
'description' => t('Whether comments are allowed on this node: 0 = no, 1 = read only, 2 = read/write.'),
|
328
|
);
|
329
|
}
|
330
|
|
331
|
// If the target content type is a Feed node, expose its source field.
|
332
|
if ($id = feeds_get_importer_id($this->bundle())) {
|
333
|
$name = feeds_importer($id)->config['name'];
|
334
|
$targets['feeds_source'] = array(
|
335
|
'name' => t('Feed source'),
|
336
|
'description' => t('The content type created by this processor is a Feed Node, it represents a source itself. Depending on the fetcher selected on the importer "@importer", this field is expected to be for example a URL or a path to a file.', array('@importer' => $name)),
|
337
|
'optional_unique' => TRUE,
|
338
|
);
|
339
|
}
|
340
|
|
341
|
// Let other modules expose mapping targets.
|
342
|
self::loadMappers();
|
343
|
$entity_type = $this->entityType();
|
344
|
$bundle = $this->bundle();
|
345
|
drupal_alter('feeds_processor_targets', $targets, $entity_type, $bundle);
|
346
|
|
347
|
return $targets;
|
348
|
}
|
349
|
|
350
|
/**
|
351
|
* Get nid of an existing feed item node if available.
|
352
|
*/
|
353
|
protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
|
354
|
if ($nid = parent::existingEntityId($source, $result)) {
|
355
|
return $nid;
|
356
|
}
|
357
|
|
358
|
// Iterate through all unique targets and test whether they do already
|
359
|
// exist in the database.
|
360
|
foreach ($this->uniqueTargets($source, $result) as $target => $value) {
|
361
|
switch ($target) {
|
362
|
case 'nid':
|
363
|
$nid = db_query("SELECT nid FROM {node} WHERE nid = :nid", array(':nid' => $value))->fetchField();
|
364
|
break;
|
365
|
case 'title':
|
366
|
$nid = db_query("SELECT nid FROM {node} WHERE title = :title AND type = :type", array(':title' => $value, ':type' => $this->bundle()))->fetchField();
|
367
|
break;
|
368
|
case 'feeds_source':
|
369
|
if ($id = feeds_get_importer_id($this->bundle())) {
|
370
|
$nid = db_query("SELECT fs.feed_nid FROM {node} n JOIN {feeds_source} fs ON n.nid = fs.feed_nid WHERE fs.id = :id AND fs.source = :source", array(':id' => $id, ':source' => $value))->fetchField();
|
371
|
}
|
372
|
break;
|
373
|
}
|
374
|
if ($nid) {
|
375
|
// Return with the first nid found.
|
376
|
return $nid;
|
377
|
}
|
378
|
}
|
379
|
return 0;
|
380
|
}
|
381
|
}
|