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