1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Class definition of FeedsNodeProcessor.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Option for handling content in Drupal but not in source data (unpublish
|
10
|
* instead of skip/delete).
|
11
|
*/
|
12
|
define('FEEDS_UNPUBLISH_NON_EXISTENT', 'unpublish');
|
13
|
|
14
|
/**
|
15
|
* Creates nodes from feed items.
|
16
|
*/
|
17
|
class FeedsNodeProcessor extends FeedsProcessor {
|
18
|
|
19
|
/**
|
20
|
* Define entity type.
|
21
|
*/
|
22
|
public function entityType() {
|
23
|
return 'node';
|
24
|
}
|
25
|
|
26
|
/**
|
27
|
* Implements parent::entityInfo().
|
28
|
*/
|
29
|
protected function entityInfo() {
|
30
|
$info = parent::entityInfo();
|
31
|
$info['label plural'] = t('Nodes');
|
32
|
return $info;
|
33
|
}
|
34
|
|
35
|
/**
|
36
|
* Overrides parent::languageOptions().
|
37
|
*/
|
38
|
public function languageOptions() {
|
39
|
// Content types can have "extended" language enabled, allowing all
|
40
|
// available languages, not just enabled. Account for this here.
|
41
|
if (module_exists('i18n_node') && $this->bundle()) {
|
42
|
$node = new stdClass();
|
43
|
$node->type = $this->bundle();
|
44
|
$node->is_new = TRUE;
|
45
|
node_object_prepare($node);
|
46
|
$languages = array(LANGUAGE_NONE => t('Language neutral')) + i18n_node_language_list($node);
|
47
|
return $languages;
|
48
|
}
|
49
|
|
50
|
// If i18n_node is not enabled, default to enabled languages.
|
51
|
return parent::languageOptions();
|
52
|
}
|
53
|
|
54
|
/**
|
55
|
* Creates a new node in memory and returns it.
|
56
|
*/
|
57
|
protected function newEntity(FeedsSource $source) {
|
58
|
$node = parent::newEntity($source);
|
59
|
$node->type = $this->bundle();
|
60
|
$node->changed = REQUEST_TIME;
|
61
|
$node->created = REQUEST_TIME;
|
62
|
$node->is_new = TRUE;
|
63
|
node_object_prepare($node);
|
64
|
// Populate properties that are set by node_object_prepare().
|
65
|
$node->log = 'Created by FeedsNodeProcessor';
|
66
|
$node->uid = $this->config['author'];
|
67
|
return $node;
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Loads an existing node.
|
72
|
*
|
73
|
* If the update existing method is not FEEDS_UPDATE_EXISTING, only the node
|
74
|
* table will be loaded, foregoing the node_load API for better performance.
|
75
|
*
|
76
|
* @todo Reevaluate the use of node_object_prepare().
|
77
|
*/
|
78
|
protected function entityLoad(FeedsSource $source, $nid) {
|
79
|
$node = parent::entityLoad($source, $nid);
|
80
|
|
81
|
if ($this->config['update_existing'] != FEEDS_UPDATE_EXISTING) {
|
82
|
$node->uid = $this->config['author'];
|
83
|
}
|
84
|
|
85
|
node_object_prepare($node);
|
86
|
|
87
|
// Workaround for issue #1247506. See #1245094 for backstory.
|
88
|
if (!empty($node->menu)) {
|
89
|
// If the node has a menu item(with a valid mlid) it must be flagged
|
90
|
// 'enabled'.
|
91
|
$node->menu['enabled'] = (int) (bool) $node->menu['mlid'];
|
92
|
}
|
93
|
|
94
|
// Populate properties that are set by node_object_prepare().
|
95
|
if ($this->config['update_existing'] == FEEDS_UPDATE_EXISTING) {
|
96
|
$node->log = 'Updated by FeedsNodeProcessor';
|
97
|
}
|
98
|
else {
|
99
|
$node->log = 'Replaced by FeedsNodeProcessor';
|
100
|
}
|
101
|
return $node;
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Check that the user has permission to save a node.
|
106
|
*/
|
107
|
protected function entitySaveAccess($entity) {
|
108
|
|
109
|
// The check will be skipped for anonymous nodes.
|
110
|
if ($this->config['authorize'] && !empty($entity->uid)) {
|
111
|
|
112
|
$author = user_load($entity->uid);
|
113
|
|
114
|
// If the uid was mapped directly, rather than by email or username, it
|
115
|
// could be invalid.
|
116
|
if (!$author) {
|
117
|
throw new FeedsAccessException(t('User %uid is not a valid user.', array(
|
118
|
'%uid' => $entity->uid,
|
119
|
)));
|
120
|
}
|
121
|
|
122
|
if (empty($entity->nid) || !empty($entity->is_new)) {
|
123
|
$op = 'create';
|
124
|
$access = node_access($op, $entity->type, $author);
|
125
|
}
|
126
|
else {
|
127
|
$op = 'update';
|
128
|
$access = node_access($op, $entity, $author);
|
129
|
}
|
130
|
|
131
|
if (!$access) {
|
132
|
$message = t('The user %name is not authorized to %op content of type %content_type. To import this item, either the user "@name" (author of the item) must be given the permission to @op content of type @content_type, or the option "Authorize" on the Node processor settings must be turned off.', array(
|
133
|
'%name' => $author->name,
|
134
|
'%op' => $op,
|
135
|
'%content_type' => $entity->type,
|
136
|
'@name' => $author->name,
|
137
|
'@op' => $op,
|
138
|
'@content_type' => $entity->type,
|
139
|
));
|
140
|
throw new FeedsAccessException($message);
|
141
|
}
|
142
|
}
|
143
|
}
|
144
|
|
145
|
/**
|
146
|
* Validates a node.
|
147
|
*/
|
148
|
protected function entityValidate($entity, FeedsSource $source = NULL) {
|
149
|
// Set or correct user ID.
|
150
|
if (!isset($entity->uid) || !is_numeric($entity->uid)) {
|
151
|
$entity->uid = $this->config['author'];
|
152
|
}
|
153
|
elseif (!is_int($entity->uid)) {
|
154
|
// Cast user ID to an integer to prevent array_flip() notices in
|
155
|
// DrupalDefaultEntityController::load() which occur when an user ID is
|
156
|
// not an integer nor a string.
|
157
|
$entity->uid = (int) $entity->uid;
|
158
|
}
|
159
|
|
160
|
// When the import should be authorized, make an extra account switch.
|
161
|
if ($source && $this->config['authorize'] && !empty($entity->uid)) {
|
162
|
$author = user_load($entity->uid);
|
163
|
$switcher = $source->accountSwitcher->switchTo($author);
|
164
|
}
|
165
|
|
166
|
try {
|
167
|
parent::entityValidate($entity, $source);
|
168
|
}
|
169
|
catch (Exception $e) {
|
170
|
// Catch any exceptions, throw these at the end.
|
171
|
}
|
172
|
|
173
|
// When an user switch happened because of an authorized import, be sure to
|
174
|
// switch back to the previous logged in account.
|
175
|
if (!empty($switcher)) {
|
176
|
$switcher->switchBack();
|
177
|
}
|
178
|
if (!empty($e)) {
|
179
|
throw $e;
|
180
|
}
|
181
|
}
|
182
|
|
183
|
/**
|
184
|
* Save a node.
|
185
|
*/
|
186
|
public function entitySave($entity) {
|
187
|
node_save($entity);
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
* Delete a series of nodes.
|
192
|
*/
|
193
|
protected function entityDeleteMultiple($nids) {
|
194
|
node_delete_multiple($nids);
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* Overrides parent::expiryQuery().
|
199
|
*/
|
200
|
protected function expiryQuery(FeedsSource $source, $time) {
|
201
|
$select = parent::expiryQuery($source, $time);
|
202
|
$select->condition('e.created', REQUEST_TIME - $time, '<');
|
203
|
return $select;
|
204
|
}
|
205
|
|
206
|
/**
|
207
|
* Return expiry time.
|
208
|
*/
|
209
|
public function expiryTime() {
|
210
|
return $this->config['expire'];
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* Override parent::configDefaults().
|
215
|
*/
|
216
|
public function configDefaults() {
|
217
|
return array(
|
218
|
'expire' => FEEDS_EXPIRE_NEVER,
|
219
|
'author' => 0,
|
220
|
'authorize' => TRUE,
|
221
|
) + parent::configDefaults();
|
222
|
}
|
223
|
|
224
|
/**
|
225
|
* Override parent::configForm().
|
226
|
*/
|
227
|
public function configForm(&$form_state) {
|
228
|
$form = parent::configForm($form_state);
|
229
|
|
230
|
$author = user_load($this->config['author']);
|
231
|
$form['author'] = array(
|
232
|
'#type' => 'textfield',
|
233
|
'#title' => t('Author'),
|
234
|
'#description' => t('Select the author of the nodes to be created - leave empty to assign "anonymous".'),
|
235
|
'#autocomplete_path' => 'user/autocomplete',
|
236
|
'#default_value' => empty($author->name) ? 'anonymous' : check_plain($author->name),
|
237
|
);
|
238
|
$form['authorize'] = array(
|
239
|
'#type' => 'checkbox',
|
240
|
'#title' => t('Authorize'),
|
241
|
'#description' => t('Check that the author has permission to create the node.'),
|
242
|
'#default_value' => $this->config['authorize'],
|
243
|
);
|
244
|
$period = drupal_map_assoc(array(
|
245
|
FEEDS_EXPIRE_NEVER,
|
246
|
3600,
|
247
|
10800,
|
248
|
21600,
|
249
|
43200,
|
250
|
86400,
|
251
|
259200,
|
252
|
604800,
|
253
|
2592000,
|
254
|
2592000 * 3,
|
255
|
2592000 * 6,
|
256
|
31536000,
|
257
|
), 'feeds_format_expire');
|
258
|
$form['expire'] = array(
|
259
|
'#type' => 'select',
|
260
|
'#title' => t('Expire nodes'),
|
261
|
'#options' => $period,
|
262
|
'#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."),
|
263
|
'#default_value' => $this->config['expire'],
|
264
|
);
|
265
|
// Add on the "Unpublish" option for nodes, update wording.
|
266
|
if (isset($form['update_non_existent'])) {
|
267
|
$form['update_non_existent']['#options'][FEEDS_UNPUBLISH_NON_EXISTENT] = t('Unpublish non-existent nodes');
|
268
|
}
|
269
|
return $form;
|
270
|
}
|
271
|
|
272
|
/**
|
273
|
* Override parent::configFormValidate().
|
274
|
*/
|
275
|
public function configFormValidate(&$values) {
|
276
|
if ($author = user_load_by_name($values['author'])) {
|
277
|
$values['author'] = $author->uid;
|
278
|
}
|
279
|
else {
|
280
|
$values['author'] = 0;
|
281
|
}
|
282
|
}
|
283
|
|
284
|
/**
|
285
|
* Reschedule if expiry time changes.
|
286
|
*/
|
287
|
public function configFormSubmit(&$values) {
|
288
|
if ($this->config['expire'] != $values['expire']) {
|
289
|
feeds_reschedule($this->id);
|
290
|
}
|
291
|
parent::configFormSubmit($values);
|
292
|
}
|
293
|
|
294
|
/**
|
295
|
* Override setTargetElement to operate on a target item that is a node.
|
296
|
*/
|
297
|
public function setTargetElement(FeedsSource $source, $target_node, $target_element, $value) {
|
298
|
switch ($target_element) {
|
299
|
case 'created':
|
300
|
$target_node->created = feeds_to_unixtime($value, REQUEST_TIME);
|
301
|
break;
|
302
|
|
303
|
case 'changed':
|
304
|
// The 'changed' value will be set on the node in feeds_node_presave().
|
305
|
// This is because node_save() always overwrites this value (though
|
306
|
// before invoking hook_node_presave()).
|
307
|
$target_node->feeds_item->node_changed = feeds_to_unixtime($value, REQUEST_TIME);
|
308
|
break;
|
309
|
|
310
|
case 'feeds_source':
|
311
|
// Get the class of the feed node importer's fetcher and set the source
|
312
|
// property. See feeds_node_update() how $node->feeds gets stored.
|
313
|
if ($id = feeds_get_importer_id($this->bundle())) {
|
314
|
$class = get_class(feeds_importer($id)->fetcher);
|
315
|
$target_node->feeds[$class]['source'] = $value;
|
316
|
// This effectively suppresses 'import on submission' feature.
|
317
|
// See feeds_node_insert().
|
318
|
$target_node->feeds['suppress_import'] = TRUE;
|
319
|
}
|
320
|
break;
|
321
|
|
322
|
case 'user_name':
|
323
|
if ($user = user_load_by_name($value)) {
|
324
|
$target_node->uid = $user->uid;
|
325
|
}
|
326
|
break;
|
327
|
|
328
|
case 'user_mail':
|
329
|
if ($user = user_load_by_mail($value)) {
|
330
|
$target_node->uid = $user->uid;
|
331
|
}
|
332
|
break;
|
333
|
|
334
|
default:
|
335
|
parent::setTargetElement($source, $target_node, $target_element, $value);
|
336
|
break;
|
337
|
}
|
338
|
}
|
339
|
|
340
|
/**
|
341
|
* Return available mapping targets.
|
342
|
*/
|
343
|
public function getMappingTargets() {
|
344
|
$type = node_type_get_type($this->bundle());
|
345
|
|
346
|
$targets = parent::getMappingTargets();
|
347
|
if ($type && $type->has_title) {
|
348
|
$targets['title'] = array(
|
349
|
'name' => t('Title'),
|
350
|
'description' => t('The title of the node.'),
|
351
|
'optional_unique' => TRUE,
|
352
|
);
|
353
|
}
|
354
|
$targets['nid'] = array(
|
355
|
'name' => t('Node ID'),
|
356
|
'description' => t('The nid of the node. NOTE: use this feature with care, node ids are usually assigned by Drupal.'),
|
357
|
'optional_unique' => TRUE,
|
358
|
);
|
359
|
$targets['uid'] = array(
|
360
|
'name' => t('User ID'),
|
361
|
'description' => t('The Drupal user ID of the node author.'),
|
362
|
);
|
363
|
$targets['user_name'] = array(
|
364
|
'name' => t('Username'),
|
365
|
'description' => t('The Drupal username of the node author.'),
|
366
|
);
|
367
|
$targets['user_mail'] = array(
|
368
|
'name' => t('User email'),
|
369
|
'description' => t('The email address of the node author.'),
|
370
|
);
|
371
|
$targets['status'] = array(
|
372
|
'name' => t('Published status'),
|
373
|
'description' => t('Whether a node is published or not. 1 stands for published, 0 for not published.'),
|
374
|
);
|
375
|
$targets['created'] = array(
|
376
|
'name' => t('Published date'),
|
377
|
'description' => t('The UNIX time when a node has been published.'),
|
378
|
);
|
379
|
$targets['changed'] = array(
|
380
|
'name' => t('Updated date'),
|
381
|
'description' => t('The Unix timestamp when a node has been last updated.'),
|
382
|
);
|
383
|
$targets['promote'] = array(
|
384
|
'name' => t('Promoted to front page'),
|
385
|
'description' => t('Boolean value, whether or not node is promoted to front page. (1 = promoted, 0 = not promoted)'),
|
386
|
);
|
387
|
$targets['sticky'] = array(
|
388
|
'name' => t('Sticky'),
|
389
|
'description' => t('Boolean value, whether or not node is sticky at top of lists. (1 = sticky, 0 = not sticky)'),
|
390
|
);
|
391
|
|
392
|
// Include language field if Locale module is enabled.
|
393
|
if (module_exists('locale')) {
|
394
|
$targets['language'] = array(
|
395
|
'name' => t('Language'),
|
396
|
'description' => t('The two-character language code of the node.'),
|
397
|
);
|
398
|
}
|
399
|
|
400
|
// Include comment field if Comment module is enabled.
|
401
|
if (module_exists('comment')) {
|
402
|
$targets['comment'] = array(
|
403
|
'name' => t('Comments'),
|
404
|
'description' => t('Whether comments are allowed on this node: 0 = no, 1 = read only, 2 = read/write.'),
|
405
|
);
|
406
|
}
|
407
|
|
408
|
// If the target content type is a Feed node, expose its source field.
|
409
|
if ($id = feeds_get_importer_id($this->bundle())) {
|
410
|
$name = feeds_importer($id)->config['name'];
|
411
|
$targets['feeds_source'] = array(
|
412
|
'name' => t('Feed source'),
|
413
|
'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)),
|
414
|
'optional_unique' => TRUE,
|
415
|
);
|
416
|
}
|
417
|
|
418
|
$this->getHookTargets($targets);
|
419
|
|
420
|
return $targets;
|
421
|
}
|
422
|
|
423
|
/**
|
424
|
* Get nid of an existing feed item node if available.
|
425
|
*/
|
426
|
protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
|
427
|
if ($nid = parent::existingEntityId($source, $result)) {
|
428
|
return $nid;
|
429
|
}
|
430
|
|
431
|
// Iterate through all unique targets and test whether they do already
|
432
|
// exist in the database.
|
433
|
foreach ($this->uniqueTargets($source, $result) as $target => $value) {
|
434
|
switch ($target) {
|
435
|
case 'nid':
|
436
|
$nid = db_query("SELECT nid FROM {node} WHERE nid = :nid", array(
|
437
|
':nid' => $value,
|
438
|
))->fetchField();
|
439
|
break;
|
440
|
|
441
|
case 'title':
|
442
|
$nid = db_query("SELECT nid FROM {node} WHERE title = :title AND type = :type", array(
|
443
|
':title' => $value,
|
444
|
':type' => $this->bundle(),
|
445
|
))->fetchField();
|
446
|
break;
|
447
|
|
448
|
case 'feeds_source':
|
449
|
if ($id = feeds_get_importer_id($this->bundle())) {
|
450
|
$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(
|
451
|
':id' => $id,
|
452
|
':source' => $value,
|
453
|
))->fetchField();
|
454
|
}
|
455
|
break;
|
456
|
}
|
457
|
|
458
|
if ($nid) {
|
459
|
// Return with the first nid found.
|
460
|
return $nid;
|
461
|
}
|
462
|
}
|
463
|
return 0;
|
464
|
}
|
465
|
|
466
|
/**
|
467
|
* Overrides FeedsProcessor::clean().
|
468
|
*
|
469
|
* Allow unpublish instead of delete.
|
470
|
*
|
471
|
* @param FeedsState $state
|
472
|
* The FeedsState object for the given stage.
|
473
|
*/
|
474
|
protected function clean(FeedsState $state) {
|
475
|
// Delegate to parent if not unpublishing or option not set.
|
476
|
if (!isset($this->config['update_non_existent']) || $this->config['update_non_existent'] != FEEDS_UNPUBLISH_NON_EXISTENT) {
|
477
|
return parent::clean($state);
|
478
|
}
|
479
|
|
480
|
$total = count($state->removeList);
|
481
|
if ($total) {
|
482
|
$nodes = node_load_multiple($state->removeList);
|
483
|
foreach ($nodes as &$node) {
|
484
|
$this->loadItemInfo($node);
|
485
|
// Update the hash value of the feed item to ensure that the item gets
|
486
|
// updated in case it reappears in the feed.
|
487
|
$node->feeds_item->hash = $this->config['update_non_existent'];
|
488
|
node_unpublish_action($node);
|
489
|
node_save($node);
|
490
|
$state->unpublished++;
|
491
|
}
|
492
|
}
|
493
|
}
|
494
|
|
495
|
}
|