Projet

Général

Profil

Paste
Télécharger (39,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / feeds / plugins / FeedsProcessor.inc @ a192dc0b

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains FeedsProcessor and related classes.
6
 */
7

    
8
// Insert mode for new items.
9
define('FEEDS_SKIP_NEW', 0);
10
define('FEEDS_INSERT_NEW', 1);
11

    
12
// Update mode for existing items.
13
define('FEEDS_SKIP_EXISTING', 0);
14
define('FEEDS_REPLACE_EXISTING', 1);
15
define('FEEDS_UPDATE_EXISTING', 2);
16
// Options for handling content in Drupal but not in source data.
17
define('FEEDS_SKIP_NON_EXISTENT', 'skip');
18
define('FEEDS_DELETE_NON_EXISTENT', 'delete');
19

    
20
// Default limit for creating items on a page load, not respected by all
21
// processors.
22
define('FEEDS_PROCESS_LIMIT', 50);
23

    
24
/**
25
 * Thrown if a validation fails.
26
 */
27
class FeedsValidationException extends Exception {}
28

    
29
/**
30
 * Thrown if a an access check fails.
31
 */
32
class FeedsAccessException extends Exception {}
33

    
34
/**
35
 * Abstract class, defines interface for processors.
36
 */
37
abstract class FeedsProcessor extends FeedsPlugin {
38

    
39
  /**
40
   * Implements FeedsPlugin::pluginType().
41
   */
42
  public function pluginType() {
43
    return 'processor';
44
  }
45

    
46
  /**
47
   * @defgroup entity_api_wrapper Entity API wrapper.
48
   */
49

    
50
  /**
51
   * Entity type this processor operates on.
52
   */
53
  public abstract function entityType();
54

    
55
  /**
56
   * Bundle type this processor operates on.
57
   *
58
   * Defaults to the entity type for entities that do not define bundles.
59
   *
60
   * @return string|NULL
61
   *   The bundle type this processor operates on, or NULL if it is undefined.
62
   */
63
  public function bundle() {
64
    return $this->config['bundle'];
65
  }
66

    
67
  /**
68
   * Provides a list of bundle options for use in select lists.
69
   *
70
   * @return array
71
   *   A keyed array of bundle => label.
72
   */
73
  public function bundleOptions() {
74
    $options = array();
75
    foreach (field_info_bundles($this->entityType()) as $bundle => $info) {
76
      if (!empty($info['label'])) {
77
        $options[$bundle] = $info['label'];
78
      }
79
      else {
80
        $options[$bundle] = $bundle;
81
      }
82
    }
83
    return $options;
84
  }
85

    
86
  /**
87
   * Create a new entity.
88
   *
89
   * @param FeedsSource $source
90
   *   The feeds source that spawns this entity.
91
   *
92
   * @return object
93
   *   A new entity object.
94
   */
95
  protected function newEntity(FeedsSource $source) {
96
    $entity = new stdClass();
97

    
98
    $info = $this->entityInfo();
99
    if (!empty($info['entity keys']['language'])) {
100
      $entity->{$info['entity keys']['language']} = $this->entityLanguage();
101
    }
102

    
103
    return $entity;
104
  }
105

    
106
  /**
107
   * Load an existing entity.
108
   *
109
   * @param $source
110
   *   The feeds source that spawns this entity.
111
   * @param $entity_id
112
   *   The unique id of the entity that should be loaded.
113
   *
114
   * @return
115
   *   A new entity object.
116
   *
117
   * @todo We should be able to batch load these, if we found all of the
118
   *   existing ids first.
119
   */
120
  protected function entityLoad(FeedsSource $source, $entity_id) {
121
    $info = $this->entityInfo();
122

    
123
    if ($this->config['update_existing'] == FEEDS_UPDATE_EXISTING) {
124
      $entities = entity_load($this->entityType(), array($entity_id));
125
      $entity = reset($entities);
126
    }
127
    else {
128
      $args = array(':entity_id' => $entity_id);
129
      $table = db_escape_table($info['base table']);
130
      $key = db_escape_field($info['entity keys']['id']);
131
      $entity = db_query("SELECT * FROM {" . $table . "} WHERE $key = :entity_id", $args)->fetchObject();
132
    }
133

    
134
    if ($entity && !empty($info['entity keys']['language'])) {
135
      $entity->{$info['entity keys']['language']} = $this->entityLanguage();
136
    }
137

    
138
    return $entity;
139
  }
140

    
141
  /**
142
   * Validates an entity.
143
   *
144
   * @throws FeedsValidationException $e
145
   *   Thrown if validation fails.
146
   */
147
  protected function entityValidate($entity) {
148
    $info = $this->entityInfo();
149
    if (empty($info['entity keys']['language'])) {
150
      return;
151
    }
152

    
153
    // Ensure that a valid language is always set.
154
    $key = $info['entity keys']['language'];
155
    $languages = language_list('enabled');
156

    
157
    if (empty($entity->$key) || !isset($languages[1][$entity->$key])) {
158
      $entity->$key = $this->entityLanguage();
159
    }
160
  }
161

    
162
  /**
163
   * Access check for saving an enity.
164
   *
165
   * @param $entity
166
   *   Entity to be saved.
167
   *
168
   * @throws FeedsAccessException $e
169
   *   If the access check fails.
170
   */
171
  protected function entitySaveAccess($entity) {}
172

    
173
  /**
174
   * Save an entity.
175
   *
176
   * @param $entity
177
   *   Entity to be saved.
178
   */
179
  protected abstract function entitySave($entity);
180

    
181
  /**
182
   * Delete a series of entities.
183
   *
184
   * @param $entity_ids
185
   *   Array of unique identity ids to be deleted.
186
   */
187
  protected abstract function entityDeleteMultiple($entity_ids);
188

    
189
  /**
190
   * Wrap entity_get_info() into a method so that extending classes can override
191
   * it and more entity information. Allowed additional keys:
192
   *
193
   * 'label plural' ... the plural label of an entity type.
194
   */
195
  protected function entityInfo() {
196
    $info = entity_get_info($this->entityType());
197

    
198
    // Entity module has defined the plural label in "plural label" instead of
199
    // "label plural". So if "plural label" is defined, this will have priority
200
    // over "label plural".
201
    if (isset($info['plural label'])) {
202
      $info['label plural'] = $info['plural label'];
203
    }
204

    
205
    return $info;
206
  }
207

    
208
  /**
209
   * Returns the current language for entities.
210
   *
211
   * This checks if the configuration value is valid.
212
   *
213
   * @return string
214
   *   The current language code.
215
   */
216
  protected function entityLanguage() {
217
    if (!module_exists('locale')) {
218
      // language_list() may return languages even if the locale module is
219
      // disabled. See https://www.drupal.org/node/173227 why.
220
      // When the locale module is disabled, there are no selectable languages
221
      // in the UI, so the content should be imported in LANGUAGE_NONE.
222
      return LANGUAGE_NONE;
223
    }
224

    
225
    $languages = language_list('enabled');
226

    
227
    return isset($languages[1][$this->config['language']]) ? $this->config['language'] : LANGUAGE_NONE;
228
  }
229

    
230
  /**
231
   * @}
232
   */
233

    
234
  /**
235
   * Process the result of the parsing stage.
236
   *
237
   * @param FeedsSource $source
238
   *   Source information about this import.
239
   * @param FeedsParserResult $parser_result
240
   *   The result of the parsing stage.
241
   */
242
  public function process(FeedsSource $source, FeedsParserResult $parser_result) {
243
    $state = $source->state(FEEDS_PROCESS);
244
    if (!isset($state->removeList) && $parser_result->items) {
245
      $this->initEntitiesToBeRemoved($source, $state);
246
    }
247

    
248
    $skip_new = $this->config['insert_new'] == FEEDS_SKIP_NEW;
249
    $skip_existing = $this->config['update_existing'] == FEEDS_SKIP_EXISTING;
250

    
251
    while ($item = $parser_result->shiftItem()) {
252

    
253
      // Check if this item already exists.
254
      $entity_id = $this->existingEntityId($source, $parser_result);
255
      // If it's included in the feed, it must not be removed on clean.
256
      if ($entity_id) {
257
        unset($state->removeList[$entity_id]);
258
      }
259

    
260
      module_invoke_all('feeds_before_update', $source, $item, $entity_id);
261

    
262
      // If it exists, and we are not updating, or if it does not exist, and we
263
      // are not inserting, pass onto the next item.
264
      if (($entity_id && $skip_existing) || (!$entity_id && $skip_new)) {
265
        continue;
266
      }
267

    
268
      try {
269
        $hash = $this->hash($item);
270
        $changed = $hash !== $this->getHash($entity_id);
271

    
272
        // Do not proceed if the item exists, has not changed, and we're not
273
        // forcing the update.
274
        if ($entity_id && !$changed && !$this->config['skip_hash_check']) {
275
          continue;
276
        }
277

    
278
        // Load an existing entity.
279
        if ($entity_id) {
280
          $entity = $this->entityLoad($source, $entity_id);
281

    
282
          // The feeds_item table is always updated with the info for the most
283
          // recently processed entity. The only carryover is the entity_id.
284
          $this->newItemInfo($entity, $source->feed_nid, $hash);
285
          $entity->feeds_item->entity_id = $entity_id;
286
          $entity->feeds_item->is_new = FALSE;
287
        }
288

    
289
        // Build a new entity.
290
        else {
291
          $entity = $this->newEntity($source);
292
          $this->newItemInfo($entity, $source->feed_nid, $hash);
293
        }
294

    
295
        // Set property and field values.
296
        $this->map($source, $parser_result, $entity);
297
        $this->entityValidate($entity);
298

    
299
        // Allow modules to alter the entity before saving.
300
        module_invoke_all('feeds_presave', $source, $entity, $item, $entity_id);
301
        if (module_exists('rules')) {
302
          rules_invoke_event('feeds_import_'. $source->importer()->id, $entity);
303
        }
304

    
305
        // Enable modules to skip saving at all.
306
        if (!empty($entity->feeds_item->skip)) {
307
          continue;
308
        }
309

    
310
        // This will throw an exception on failure.
311
        $this->entitySaveAccess($entity);
312
        $this->entitySave($entity);
313

    
314
        // Allow modules to perform operations using the saved entity data.
315
        // $entity contains the updated entity after saving.
316
        module_invoke_all('feeds_after_save', $source, $entity, $item, $entity_id);
317

    
318
        // Track progress.
319
        if (empty($entity_id)) {
320
          $state->created++;
321
        }
322
        else {
323
          $state->updated++;
324
        }
325
      }
326

    
327
      // Something bad happened, log it.
328
      catch (Exception $e) {
329
        $state->failed++;
330
        drupal_set_message($e->getMessage(), 'warning');
331
        list($message, $arguments) = $this->createLogEntry($e, $entity, $item);
332
        $source->log('import', $message, $arguments, WATCHDOG_ERROR);
333
      }
334
    }
335

    
336
    // Set messages if we're done.
337
    if ($source->progressImporting() != FEEDS_BATCH_COMPLETE) {
338
      return;
339
    }
340
    // Remove not included items if needed.
341
    // It depends on the implementation of the clean() method what will happen
342
    // to items that were no longer in the source.
343
    $this->clean($state);
344
    $info = $this->entityInfo();
345
    $tokens = array(
346
      '@entity' => strtolower($info['label']),
347
      '@entities' => strtolower($info['label plural']),
348
    );
349
    $messages = array();
350
    if ($state->created) {
351
      $messages[] = array(
352
       'message' => format_plural(
353
          $state->created,
354
          'Created @number @entity.',
355
          'Created @number @entities.',
356
          array('@number' => $state->created) + $tokens
357
        ),
358
      );
359
    }
360
    if ($state->updated) {
361
      $messages[] = array(
362
       'message' => format_plural(
363
          $state->updated,
364
          'Updated @number @entity.',
365
          'Updated @number @entities.',
366
          array('@number' => $state->updated) + $tokens
367
        ),
368
      );
369
    }
370
    if ($state->unpublished) {
371
      $messages[] = array(
372
        'message' => format_plural(
373
            $state->unpublished,
374
            'Unpublished @number @entity.',
375
            'Unpublished @number @entities.',
376
            array('@number' => $state->unpublished) + $tokens
377
        ),
378
      );
379
    }
380
    if ($state->blocked) {
381
      $messages[] = array(
382
        'message' => format_plural(
383
          $state->blocked,
384
          'Blocked @number @entity.',
385
          'Blocked @number @entities.',
386
          array('@number' => $state->blocked) + $tokens
387
        ),
388
      );
389
    }
390
    if ($state->deleted) {
391
      $messages[] = array(
392
       'message' => format_plural(
393
          $state->deleted,
394
          'Removed @number @entity.',
395
          'Removed @number @entities.',
396
          array('@number' => $state->deleted) + $tokens
397
        ),
398
      );
399
    }
400
    if ($state->failed) {
401
      $messages[] = array(
402
       'message' => format_plural(
403
          $state->failed,
404
          'Failed importing @number @entity.',
405
          'Failed importing @number @entities.',
406
          array('@number' => $state->failed) + $tokens
407
        ),
408
        'level' => WATCHDOG_ERROR,
409
      );
410
    }
411
    if (empty($messages)) {
412
      $messages[] = array(
413
        'message' => t('There are no new @entities.', array('@entities' => strtolower($info['label plural']))),
414
      );
415
    }
416
    foreach ($messages as $message) {
417
      drupal_set_message($message['message']);
418
      $source->log('import', $message['message'], array(), isset($message['level']) ? $message['level'] : WATCHDOG_INFO);
419
    }
420
  }
421

    
422
  /**
423
   * Initializes the list of entities to remove.
424
   *
425
   * This populates $state->removeList with all existing entities previously
426
   * imported from the source.
427
   *
428
   * @param FeedsSource $source
429
   *   Source information about this import.
430
   * @param FeedsState $state
431
   *   The FeedsState object for the given stage.
432
   */
433
  protected function initEntitiesToBeRemoved(FeedsSource $source, FeedsState $state) {
434
    $state->removeList = array();
435

    
436
    // We fill it only if needed.
437
    if ($this->config['update_non_existent'] == FEEDS_SKIP_NON_EXISTENT) {
438
      return;
439
    }
440

    
441
    // Get the full list of entities for this source.
442
    $entity_ids = db_select('feeds_item')
443
      ->fields('feeds_item', array('entity_id'))
444
      ->condition('entity_type', $this->entityType())
445
      ->condition('id', $this->id)
446
      ->condition('feed_nid', $source->feed_nid)
447
      ->condition('hash', $this->config['update_non_existent'], '<>')
448
      ->execute()
449
      ->fetchCol();
450

    
451
    if (!$entity_ids) {
452
      return;
453
    }
454

    
455
    $state->removeList = array_combine($entity_ids, $entity_ids);
456
  }
457

    
458
  /**
459
   * Deletes entities which were not found during processing.
460
   *
461
   * @todo batch delete?
462
   *
463
   * @param FeedsState $state
464
   *   The FeedsState object for the given stage.
465
   */
466
  protected function clean(FeedsState $state) {
467
    // We clean only if needed.
468
    if ($this->config['update_non_existent'] == FEEDS_SKIP_NON_EXISTENT) {
469
      return;
470
    }
471

    
472
    if ($total = count($state->removeList)) {
473
      $this->entityDeleteMultiple($state->removeList);
474
      $state->deleted += $total;
475
    }
476
  }
477

    
478
  /**
479
   * Remove all stored results or stored results up to a certain time for a
480
   * source.
481
   *
482
   * @param FeedsSource $source
483
   *   Source information for this expiry. Implementers should only delete items
484
   *   pertaining to this source. The preferred way of determining whether an
485
   *   item pertains to a certain souce is by using $source->feed_nid. It is the
486
   *   processor's responsibility to store the feed_nid of an imported item in
487
   *   the processing stage.
488
   */
489
  public function clear(FeedsSource $source) {
490
    $state = $source->state(FEEDS_PROCESS_CLEAR);
491

    
492
    // Build base select statement.
493
    $select = db_select('feeds_item')
494
      ->fields('feeds_item', array('entity_id'))
495
      ->condition('entity_type', $this->entityType())
496
      ->condition('id', $this->id)
497
      ->condition('feed_nid', $source->feed_nid);
498

    
499
    // If there is no total, query it.
500
    if (!$state->total) {
501
      $state->total = $select->countQuery()->execute()->fetchField();
502
    }
503

    
504
    // Delete a batch of entities.
505
    $entity_ids = $select->range(0, $this->getLimit())->execute()->fetchCol();
506
    $this->entityDeleteMultiple($entity_ids);
507

    
508
    // Report progress, take into account that we may not have deleted as many
509
    // items as we have counted at first.
510
    if ($deleted_count = count($entity_ids)) {
511
      $state->deleted += $deleted_count;
512
      $state->progress($state->total, $state->deleted);
513
    }
514
    else {
515
      $state->progress($state->total, $state->total);
516
    }
517

    
518
    // Report results when done.
519
    if ($source->progressClearing() == FEEDS_BATCH_COMPLETE) {
520
      $info = $this->entityInfo();
521

    
522
      if ($state->deleted) {
523
        $message = format_plural(
524
          $state->deleted,
525
          'Deleted @number @entity',
526
          'Deleted @number @entities',
527
          array(
528
            '@number' => $state->deleted,
529
            '@entity' => strtolower($info['label']),
530
            '@entities' => strtolower($info['label plural']),
531
          )
532
        );
533
        $source->log('clear', $message, array(), WATCHDOG_INFO);
534
        drupal_set_message($message);
535
      }
536
      else {
537
        drupal_set_message(t('There are no @entities to be deleted.', array('@entities' => $info['label plural'])));
538
      }
539
    }
540
  }
541

    
542
  /*
543
   * Report number of items that can be processed per call.
544
   *
545
   * 0 means 'unlimited'.
546
   *
547
   * If a number other than 0 is given, Feeds parsers that support batching
548
   * will only deliver this limit to the processor.
549
   *
550
   * @see FeedsSource::getLimit()
551
   * @see FeedsCSVParser::parse()
552
   */
553
  public function getLimit() {
554
    return variable_get('feeds_process_limit', FEEDS_PROCESS_LIMIT);
555
  }
556

    
557
  /**
558
   * Deletes feed items older than REQUEST_TIME - $time.
559
   *
560
   * Do not invoke expire on a processor directly, but use
561
   * FeedsSource::expire() instead.
562
   *
563
   * @param FeedsSource $source
564
   *   The source to expire entities for.
565
   *
566
   * @param $time
567
   *   (optional) All items produced by this configuration that are older than
568
   *   REQUEST_TIME - $time should be deleted. If NULL, processor should use
569
   *   internal configuration. Defaults to NULL.
570
   *
571
   * @return float
572
   *   FEEDS_BATCH_COMPLETE if all items have been processed, a float between 0
573
   *   and 0.99* indicating progress otherwise.
574
   *
575
   * @see FeedsSource::expire()
576
   */
577
  public function expire(FeedsSource $source, $time = NULL) {
578
    $state = $source->state(FEEDS_PROCESS_EXPIRE);
579

    
580
    if ($time === NULL) {
581
      $time = $this->expiryTime();
582
    }
583
    if ($time == FEEDS_EXPIRE_NEVER) {
584
      return;
585
    }
586

    
587
    $select = $this->expiryQuery($source, $time);
588

    
589
    // If there is no total, query it.
590
    if (!$state->total) {
591
      $state->total = $select->countQuery()->execute()->fetchField();
592
    }
593

    
594
    // Delete a batch of entities.
595
    $entity_ids = $select->range(0, $this->getLimit())->execute()->fetchCol();
596
    if ($entity_ids) {
597
      $this->entityDeleteMultiple($entity_ids);
598
      $state->deleted += count($entity_ids);
599
      $state->progress($state->total, $state->deleted);
600
    }
601
    else {
602
      $state->progress($state->total, $state->total);
603
    }
604
  }
605

    
606
  /**
607
   * Returns a database query used to select entities to expire.
608
   *
609
   * Processor classes should override this method to set the age portion of the
610
   * query.
611
   *
612
   * @param FeedsSource $source
613
   *   The feed source.
614
   * @param int $time
615
   *   Delete entities older than this.
616
   *
617
   * @return SelectQuery
618
   *   A select query to execute.
619
   *
620
   * @see FeedsNodeProcessor::expiryQuery()
621
   */
622
  protected function expiryQuery(FeedsSource $source, $time) {
623
    // Build base select statement.
624
    $info = $this->entityInfo();
625
    $id_key = $info['entity keys']['id'];
626

    
627
    $select = db_select($info['base table'], 'e');
628
    $select->addField('e', $id_key);
629
    $select->join('feeds_item', 'fi', "e.$id_key = fi.entity_id");
630
    $select->condition('fi.entity_type', $this->entityType());
631
    $select->condition('fi.id', $this->id);
632
    $select->condition('fi.feed_nid', $source->feed_nid);
633

    
634
    return $select;
635
  }
636

    
637
  /**
638
   * Counts the number of items imported by this processor.
639
   */
640
  public function itemCount(FeedsSource $source) {
641
    return db_query("SELECT count(*) FROM {feeds_item} WHERE id = :id AND entity_type = :entity_type AND feed_nid = :feed_nid", array(':id' => $this->id, ':entity_type' => $this->entityType(), ':feed_nid' => $source->feed_nid))->fetchField();
642
  }
643

    
644
  /**
645
   * Returns a statically cached version of the target mappings.
646
   *
647
   * @return array
648
   *   The targets for this importer.
649
   */
650
  protected function getCachedTargets() {
651
    $targets = &drupal_static('FeedsProcessor::getCachedTargets', array());
652

    
653
    if (!isset($targets[$this->id])) {
654
      $targets[$this->id] = $this->getMappingTargets();
655
    }
656

    
657
    return $targets[$this->id];
658
  }
659

    
660
  /**
661
   * Returns a statically cached version of the source mappings.
662
   *
663
   * @return array
664
   *   The sources for this importer.
665
   */
666
  protected function getCachedSources() {
667
    $sources = &drupal_static('FeedsProcessor::getCachedSources', array());
668

    
669
    if (!isset($sources[$this->id])) {
670

    
671
      $sources[$this->id] = feeds_importer($this->id)->parser->getMappingSources();
672

    
673
      if (is_array($sources[$this->id])) {
674
        foreach ($sources[$this->id] as $source_key => $source) {
675
          if (empty($source['callback']) || !is_callable($source['callback'])) {
676
            unset($sources[$this->id][$source_key]['callback']);
677
          }
678
        }
679
      }
680
    }
681

    
682
    return $sources[$this->id];
683
  }
684

    
685
  /**
686
   * Execute mapping on an item.
687
   *
688
   * This method encapsulates the central mapping functionality. When an item is
689
   * processed, it is passed through map() where the properties of $source_item
690
   * are mapped onto $target_item following the processor's mapping
691
   * configuration.
692
   *
693
   * For each mapping FeedsParser::getSourceElement() is executed to retrieve
694
   * the source element, then FeedsProcessor::setTargetElement() is invoked
695
   * to populate the target item properly. Alternatively a
696
   * hook_x_targets_alter() may have specified a callback for a mapping target
697
   * in which case the callback is asked to populate the target item instead of
698
   * FeedsProcessor::setTargetElement().
699
   *
700
   * @ingroup mappingapi
701
   *
702
   * @see hook_feeds_parser_sources_alter()
703
   * @see hook_feeds_processor_targets()
704
   * @see hook_feeds_processor_targets_alter()
705
   */
706
  protected function map(FeedsSource $source, FeedsParserResult $result, $target_item = NULL) {
707
    $targets = $this->getCachedTargets();
708
    // Get fields for the entity type we are mapping to.
709
    $fields = field_info_instances($this->entityType(), $this->bundle());
710

    
711
    if (empty($target_item)) {
712
      $target_item = array();
713
    }
714

    
715
    // Many mappers add to existing fields rather than replacing them. Hence we
716
    // need to clear target elements of each item before mapping in case we are
717
    // mapping on a prepopulated item such as an existing node.
718
    foreach ($this->getMappings() as $mapping) {
719
      if (isset($targets[$mapping['target']]['real_target'])) {
720
        $target_name = $targets[$mapping['target']]['real_target'];
721
      }
722
      else {
723
        $target_name = $mapping['target'];
724
      }
725

    
726
      // If the target is a field empty the value for the targeted language
727
      // only.
728
      // In all other cases, just empty the target completely.
729
      if (isset($fields[$target_name])) {
730
        // Empty the target for the specified language.
731
        $target_item->{$target_name}[$mapping['language']] = array();
732
      }
733
      else {
734
        // Empty the whole target.
735
        $target_item->{$target_name} = NULL;
736
      }
737
    }
738

    
739
    // This is where the actual mapping happens: For every mapping we invoke
740
    // the parser's getSourceElement() method to retrieve the value of the
741
    // source element and pass it to the processor's setTargetElement() to stick
742
    // it on the right place of the target item.
743
    foreach ($this->getMappings() as $mapping) {
744
      $value = $this->getSourceValue($source, $result, $mapping['source']);
745

    
746
      $this->mapToTarget($source, $mapping['target'], $target_item, $value, $mapping);
747
    }
748

    
749
    return $target_item;
750
  }
751

    
752
  /**
753
   * Returns the values from the parser, or callback.
754
   *
755
   * @param FeedsSource $source
756
   *   The feed source.
757
   * @param FeedsParserResult $result
758
   *   The parser result.
759
   * @param string $source_key
760
   *   The current key being processed.
761
   *
762
   * @return mixed
763
   *   A value, or a list of values.
764
   */
765
  protected function getSourceValue(FeedsSource $source, FeedsParserResult $result, $source_key) {
766
    $sources = $this->getCachedSources();
767

    
768
    if (isset($sources[$source_key]['callback'])) {
769
      return call_user_func($sources[$source_key]['callback'], $source, $result, $source_key);
770
    }
771

    
772
    return feeds_importer($this->id)->parser->getSourceElement($source, $result, $source_key);
773
  }
774

    
775
  /**
776
   * Maps values onto the target item.
777
   *
778
   * @param FeedsSource $source
779
   *   The feed source.
780
   * @param mixed &$target_item
781
   *   The target item to apply values into.
782
   * @param mixed $value
783
   *   A value, or a list of values.
784
   * @param array $mapping
785
   *   The mapping configuration.
786
   */
787
  protected function mapToTarget(FeedsSource $source, $target, &$target_item, $value, array $mapping) {
788
    $targets = $this->getCachedTargets();
789

    
790
    // Map the source element's value to the target.
791
    // If the mapping specifies a callback method, use the callback instead of
792
    // setTargetElement().
793
    if (isset($targets[$target]['callback'])) {
794

    
795
      // All target callbacks expect an array.
796
      if (!is_array($value)) {
797
        $value = array($value);
798
      }
799

    
800
      call_user_func($targets[$target]['callback'], $source, $target_item, $target, $value, $mapping);
801
    }
802

    
803
    else {
804
      $this->setTargetElement($source, $target_item, $target, $value, $mapping);
805
    }
806
  }
807

    
808
  /**
809
   * Per default, don't support expiry. If processor supports expiry of imported
810
   * items, return the time after which items should be removed.
811
   */
812
  public function expiryTime() {
813
    return FEEDS_EXPIRE_NEVER;
814
  }
815

    
816
  /**
817
   * Declare default configuration.
818
   */
819
  public function configDefaults() {
820
    $info = $this->entityInfo();
821
    $bundle = NULL;
822
    if (empty($info['entity keys']['bundle'])) {
823
      $bundle = $this->entityType();
824
    }
825
    return array(
826
      'mappings' => array(),
827
      'insert_new' => FEEDS_INSERT_NEW,
828
      'update_existing' => FEEDS_SKIP_EXISTING,
829
      'update_non_existent' => FEEDS_SKIP_NON_EXISTENT,
830
      'input_format' => NULL,
831
      'skip_hash_check' => FALSE,
832
      'bundle' => $bundle,
833
      'language' => LANGUAGE_NONE,
834
    );
835
  }
836

    
837
  /**
838
   * Overrides parent::configForm().
839
   */
840
  public function configForm(&$form_state) {
841
    $info = $this->entityInfo();
842
    $form = array();
843

    
844
    if (!empty($info['entity keys']['bundle'])) {
845
      $form['bundle'] = array(
846
        '#type' => 'select',
847
        '#options' => $this->bundleOptions(),
848
        '#title' => !empty($info['bundle name']) ? $info['bundle name'] : t('Bundle'),
849
        '#required' => TRUE,
850
        '#default_value' => $this->bundle(),
851
      );
852
    }
853
    else {
854
      $form['bundle'] = array(
855
        '#type' => 'value',
856
        '#value' => $this->entityType(),
857
      );
858
    }
859

    
860
    if (module_exists('locale') && !empty($info['entity keys']['language'])) {
861
      $form['language'] = array(
862
        '#type' => 'select',
863
        '#options' => array(LANGUAGE_NONE => t('Language neutral')) + locale_language_list('name'),
864
        '#title' => t('Language'),
865
        '#required' => TRUE,
866
        '#default_value' => $this->config['language'],
867
      );
868
    }
869

    
870
    $tokens = array('@entities' => strtolower($info['label plural']));
871

    
872
    $form['insert_new'] = array(
873
      '#type' => 'radios',
874
      '#title' => t('Insert new @entities', $tokens),
875
      '#description' => t('New @entities will be determined using mappings that are a "unique target".', $tokens),
876
      '#options' => array(
877
        FEEDS_INSERT_NEW => t('Insert new @entities', $tokens),
878
        FEEDS_SKIP_NEW => t('Do not insert new @entities', $tokens),
879
      ),
880
      '#default_value' => $this->config['insert_new'],
881
    );
882

    
883
    $form['update_existing'] = array(
884
      '#type' => 'radios',
885
      '#title' => t('Update existing @entities', $tokens),
886
      '#description' =>
887
        t('Existing @entities will be determined using mappings that are a "unique target".', $tokens),
888
      '#options' => array(
889
        FEEDS_SKIP_EXISTING => t('Do not update existing @entities', $tokens),
890
        FEEDS_REPLACE_EXISTING => t('Replace existing @entities', $tokens),
891
        FEEDS_UPDATE_EXISTING => t('Update existing @entities', $tokens),
892
      ),
893
      '#default_value' => $this->config['update_existing'],
894
    );
895
    global $user;
896
    $formats = filter_formats($user);
897
    foreach ($formats as $format) {
898
      $format_options[$format->format] = $format->name;
899
    }
900
    $form['skip_hash_check'] = array(
901
      '#type' => 'checkbox',
902
      '#title' => t('Skip hash check'),
903
      '#description' => t('Force update of items even if item source data did not change.'),
904
      '#default_value' => $this->config['skip_hash_check'],
905
    );
906
    $form['input_format'] = array(
907
      '#type' => 'select',
908
      '#title' => t('Text format'),
909
      '#description' => t('Select the default input format for the text fields of the nodes to be created.'),
910
      '#options' => $format_options,
911
      '#default_value' => isset($this->config['input_format']) ? $this->config['input_format'] : 'plain_text',
912
      '#required' => TRUE,
913
    );
914

    
915
    $form['update_non_existent'] = array(
916
      '#type' => 'radios',
917
      '#title' => t('Action to take when previously imported @entities are missing in the feed', $tokens),
918
      '#description' => t('Select how @entities previously imported and now missing in the feed should be updated.', $tokens),
919
      '#options' => array(
920
        FEEDS_SKIP_NON_EXISTENT => t('Skip non-existent @entities', $tokens),
921
        FEEDS_DELETE_NON_EXISTENT => t('Delete non-existent @entities', $tokens),
922
      ),
923
      '#default_value' => $this->config['update_non_existent'],
924
    );
925

    
926
    return $form;
927
  }
928

    
929
  /**
930
   * Get mappings.
931
   */
932
  public function getMappings() {
933
    $cache = &drupal_static('FeedsProcessor::getMappings', array());
934

    
935
    if (!isset($cache[$this->id])) {
936
      $mappings = $this->config['mappings'];
937
      $targets = $this->getCachedTargets();
938
      $languages = language_list('enabled');
939

    
940
      foreach ($mappings as &$mapping) {
941

    
942
        if (isset($targets[$mapping['target']]['preprocess_callbacks'])) {
943
          foreach ($targets[$mapping['target']]['preprocess_callbacks'] as $callback) {
944
            call_user_func_array($callback, array($targets[$mapping['target']], &$mapping));
945
          }
946
        }
947

    
948
        // Ensure there's always a language set.
949
        if (empty($mapping['language'])) {
950
          $mapping['language'] = LANGUAGE_NONE;
951
        }
952
        else {
953
          // Check if the configured language is available. If not, fallback to
954
          // LANGUAGE_NONE.
955
          if (!isset($languages[1][$mapping['language']])) {
956
            $mapping['language'] = LANGUAGE_NONE;
957
          }
958
        }
959
      }
960

    
961
      $cache[$this->id] = $mappings;
962
    }
963

    
964
    return $cache[$this->id];
965
  }
966

    
967
  /**
968
   * Declare possible mapping targets that this processor exposes.
969
   *
970
   * @ingroup mappingapi
971
   *
972
   * @return
973
   *   An array of mapping targets. Keys are paths to targets
974
   *   separated by ->, values are TRUE if target can be unique,
975
   *   FALSE otherwise.
976
   */
977
  public function getMappingTargets() {
978

    
979
    // The bundle has not been selected.
980
    if (!$this->bundle()) {
981
      $info = $this->entityInfo();
982
      $bundle_name = !empty($info['bundle name']) ? drupal_strtolower($info['bundle name']) : t('bundle');
983
      $plugin_key = feeds_importer($this->id)->config['processor']['plugin_key'];
984
      $url = url('admin/structure/feeds/' . $this->id . '/settings/' . $plugin_key);
985
      drupal_set_message(t('Please <a href="@url">select a @bundle_name</a>.', array('@url' => $url, '@bundle_name' => $bundle_name)), 'warning', FALSE);
986
    }
987

    
988
    return array(
989
      'url' => array(
990
        'name' => t('URL'),
991
        'description' => t('The external URL of the item. E. g. the feed item URL in the case of a syndication feed. May be unique.'),
992
        'optional_unique' => TRUE,
993
      ),
994
      'guid' => array(
995
        'name' => t('GUID'),
996
        'description' => t('The globally unique identifier of the item. E. g. the feed item GUID in the case of a syndication feed. May be unique.'),
997
        'optional_unique' => TRUE,
998
      ),
999
    );
1000
  }
1001

    
1002
  /**
1003
   * Allows other modules to expose targets.
1004
   *
1005
   * @param array &$targets
1006
   *   The existing target array.
1007
   */
1008
  protected function getHookTargets(array &$targets) {
1009
    self::loadMappers();
1010

    
1011
    $entity_type = $this->entityType();
1012
    $bundle = $this->bundle();
1013
    $targets += module_invoke_all('feeds_processor_targets', $entity_type, $bundle);
1014

    
1015
    drupal_alter('feeds_processor_targets', $targets, $entity_type, $bundle);
1016
  }
1017

    
1018
  /**
1019
   * Set a concrete target element. Invoked from FeedsProcessor::map().
1020
   *
1021
   * @ingroup mappingapi
1022
   */
1023
  public function setTargetElement(FeedsSource $source, $target_item, $target_element, $value) {
1024
    switch ($target_element) {
1025
      case 'url':
1026
      case 'guid':
1027
        $target_item->feeds_item->$target_element = $value;
1028
        break;
1029

    
1030
      default:
1031
        $target_item->$target_element = $value;
1032
        break;
1033
    }
1034
  }
1035

    
1036
  /**
1037
   * Retrieve the target entity's existing id if available. Otherwise return 0.
1038
   *
1039
   * @ingroup mappingapi
1040
   *
1041
   * @param FeedsSource $source
1042
   *   The source information about this import.
1043
   * @param FeedsParserResult $result
1044
   *   A FeedsParserResult object.
1045
   *
1046
   * @return int
1047
   *   The serial id of an entity if found, 0 otherwise.
1048
   */
1049
  protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
1050
    $targets = $this->getCachedTargets();
1051

    
1052
    $entity_id = 0;
1053

    
1054
    // Iterate through all unique targets and test whether they already exist in
1055
    // the database.
1056
    foreach ($this->uniqueTargets($source, $result) as $target => $value) {
1057
      if ($target === 'guid' || $target === 'url') {
1058
        $entity_id = db_select('feeds_item')
1059
          ->fields('feeds_item', array('entity_id'))
1060
          ->condition('feed_nid', $source->feed_nid)
1061
          ->condition('entity_type', $this->entityType())
1062
          ->condition('id', $source->id)
1063
          ->condition($target, $value)
1064
          ->execute()
1065
          ->fetchField();
1066
      }
1067

    
1068
      if (!$entity_id && !empty($targets[$target]['unique_callbacks'])) {
1069
        if (!is_array($value)) {
1070
          $value = array($value);
1071
        }
1072

    
1073
        foreach ($targets[$target]['unique_callbacks'] as $callback) {
1074
          if ($entity_id = call_user_func($callback, $source, $this->entityType(), $this->bundle(), $target, $value)) {
1075
            // Stop at the first unique ID returned by a callback.
1076
            break;
1077
          }
1078
        }
1079
      }
1080

    
1081
      // Return with the content id found.
1082
      if ($entity_id) {
1083
        return $entity_id;
1084
      }
1085
    }
1086

    
1087
    return $entity_id;
1088
  }
1089

    
1090

    
1091
  /**
1092
   * Utility function that iterates over a target array and retrieves all
1093
   * sources that are unique.
1094
   *
1095
   * @param $batch
1096
   *   A FeedsImportBatch.
1097
   *
1098
   * @return
1099
   *   An array where the keys are target field names and the values are the
1100
   *   elements from the source item mapped to these targets.
1101
   */
1102
  protected function uniqueTargets(FeedsSource $source, FeedsParserResult $result) {
1103
    $parser = feeds_importer($this->id)->parser;
1104
    $targets = array();
1105
    foreach ($this->getMappings() as $mapping) {
1106
      if (!empty($mapping['unique'])) {
1107
        // Invoke the parser's getSourceElement to retrieve the value for this
1108
        // mapping's source.
1109
        $targets[$mapping['target']] = $parser->getSourceElement($source, $result, $mapping['source']);
1110
      }
1111
    }
1112
    return $targets;
1113
  }
1114

    
1115
  /**
1116
   * Adds Feeds specific information on $entity->feeds_item.
1117
   *
1118
   * @param $entity
1119
   *   The entity object to be populated with new item info.
1120
   * @param $feed_nid
1121
   *   The feed nid of the source that produces this entity.
1122
   * @param $hash
1123
   *   The fingerprint of the source item.
1124
   */
1125
  protected function newItemInfo($entity, $feed_nid, $hash = '') {
1126
    $entity->feeds_item = new stdClass();
1127
    $entity->feeds_item->is_new = TRUE;
1128
    $entity->feeds_item->entity_id = 0;
1129
    $entity->feeds_item->entity_type = $this->entityType();
1130
    $entity->feeds_item->id = $this->id;
1131
    $entity->feeds_item->feed_nid = $feed_nid;
1132
    $entity->feeds_item->imported = REQUEST_TIME;
1133
    $entity->feeds_item->hash = $hash;
1134
    $entity->feeds_item->url = '';
1135
    $entity->feeds_item->guid = '';
1136
  }
1137

    
1138
  /**
1139
   * Loads existing entity information and places it on $entity->feeds_item.
1140
   *
1141
   * @param $entity
1142
   *   The entity object to load item info for. Id key must be present.
1143
   *
1144
   * @return
1145
   *   TRUE if item info could be loaded, false if not.
1146
   */
1147
  protected function loadItemInfo($entity) {
1148
    $entity_info = entity_get_info($this->entityType());
1149
    $key = $entity_info['entity keys']['id'];
1150
    if ($item_info = feeds_item_info_load($this->entityType(), $entity->$key)) {
1151
      $entity->feeds_item = $item_info;
1152
      return TRUE;
1153
    }
1154
    return FALSE;
1155
  }
1156

    
1157
  /**
1158
   * Create MD5 hash of item and mappings array.
1159
   *
1160
   * Include mappings as a change in mappings may have an affect on the item
1161
   * produced.
1162
   *
1163
   * @return string
1164
   *   A hash is always returned, even when the item is empty, NULL or FALSE.
1165
   */
1166
  protected function hash($item) {
1167
    $sources = feeds_importer($this->id)->parser->getMappingSourceList();
1168
    $mapped_item = array_intersect_key($item, array_flip($sources));
1169
    return hash('md5', serialize($mapped_item) . serialize($this->getMappings()));
1170
  }
1171

    
1172
  /**
1173
   * Retrieves the MD5 hash of $entity_id from the database.
1174
   *
1175
   * @return string
1176
   *   Empty string if no item is found, hash otherwise.
1177
   */
1178
  protected function getHash($entity_id) {
1179

    
1180
    if ($hash = db_query("SELECT hash FROM {feeds_item} WHERE entity_type = :type AND entity_id = :id", array(':type' => $this->entityType(), ':id' => $entity_id))->fetchField()) {
1181
      // Return with the hash.
1182
      return $hash;
1183
    }
1184
    return '';
1185
  }
1186

    
1187
  /**
1188
   * DEPRECATED: Creates a log message for exceptions during import.
1189
   *
1190
   * Don't use this method as it concatenates user variables into the log
1191
   * message, which will pollute the locales_source table when the log message
1192
   * is translated. Use ::createLogEntry instead.
1193
   *
1194
   * @param Exception $e
1195
   *   The exception that was throwned during processing the item.
1196
   * @param $entity
1197
   *   The entity object.
1198
   * @param $item
1199
   *   The parser result for this entity.
1200
   *
1201
   * @return string
1202
   *   The message to log.
1203
   *
1204
   * @deprecated
1205
   *   Use ::createLogEntry instead.
1206
   */
1207
  protected function createLogMessage(Exception $e, $entity, $item) {
1208
    $message = $e->getMessage();
1209
    $message .= '<h3>Original item</h3>';
1210
    // $this->exportObjectVars() already runs check_plain() for us, so we can
1211
    // concatenate here as is.
1212
    $message .= '<pre>' . $this->exportObjectVars($item) . '</pre>';
1213
    $message .= '<h3>Entity</h3>';
1214
    $message .= '<pre>' . $this->exportObjectVars($entity) . '</pre>';
1215

    
1216
    return $message;
1217
  }
1218

    
1219
  /**
1220
   * Creates a log entry for when an exception occurred during import.
1221
   *
1222
   * @param Exception $e
1223
   *   The exception that was throwned during processing the item.
1224
   * @param object $entity
1225
   *   The entity object.
1226
   * @param array $item
1227
   *   The parser result for this entity.
1228
   *
1229
   * @return array
1230
   *   The message and arguments to log.
1231
   */
1232
  protected function createLogEntry(Exception $e, $entity, $item) {
1233
    $message = '@exception';
1234
    $message .= '<h3>Original item</h3>';
1235
    $message .= '<pre>!item</pre>';
1236
    $message .= '<h3>Entity</h3>';
1237
    $message .= '<pre>!entity</pre>';
1238
    $arguments = array(
1239
      '@exception' => $e->getMessage(),
1240
      // $this->exportObjectVars() already runs check_plain() for us, so we can
1241
      // use the "!" placeholder.
1242
      '!item' => $this->exportObjectVars($item),
1243
      '!entity' => $this->exportObjectVars($entity),
1244
    );
1245

    
1246
    return array($message, $arguments);
1247
  }
1248

    
1249
  /**
1250
   * Returns a string representation of an object or array for log messages.
1251
   *
1252
   * @param object|array $object
1253
   *   The object to convert.
1254
   *
1255
   * @return string
1256
   *   The sanitized string representation of the object.
1257
   */
1258
  protected function exportObjectVars($object) {
1259
    include_once DRUPAL_ROOT . '/includes/utility.inc';
1260

    
1261
    $out = is_array($object) ? $object : get_object_vars($object);
1262
    $out = array_filter($out, 'is_scalar');
1263

    
1264
    foreach ($out as $key => $value) {
1265
      if (is_string($value)) {
1266
        $out[$key] = truncate_utf8($value, 100, FALSE, TRUE);
1267
      }
1268
    }
1269

    
1270
    if (is_array($object)) {
1271
      return check_plain(drupal_var_export($out));
1272
    }
1273

    
1274
    return check_plain(drupal_var_export((object) $out));
1275
  }
1276

    
1277
  /**
1278
   * Overrides FeedsPlugin::dependencies().
1279
   */
1280
  public function dependencies() {
1281
    $dependencies = parent::dependencies();
1282

    
1283
    // Find out which module defined the entity type.
1284
    $info = $this->entityInfo();
1285
    if (isset($info['module'])) {
1286
      $dependencies[$info['module']] = $info['module'];
1287
    }
1288

    
1289
    return $dependencies;
1290
  }
1291

    
1292
}
1293

    
1294
class FeedsProcessorBundleNotDefined extends Exception {}