Projet

Général

Profil

Révision ed9a13f1

Ajouté par Assos Assos il y a presque 4 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/feeds/plugins/FeedsProcessor.inc
50 50
  /**
51 51
   * Entity type this processor operates on.
52 52
   */
53
  public abstract function entityType();
53
  abstract public function entityType();
54 54

  
55 55
  /**
56 56
   * Bundle type this processor operates on.
57 57
   *
58 58
   * Defaults to the entity type for entities that do not define bundles.
59 59
   *
60
   * @return string|NULL
60
   * @return string|null
61 61
   *   The bundle type this processor operates on, or NULL if it is undefined.
62 62
   */
63 63
  public function bundle() {
......
87 87
   * Provides a list of languages available on the site.
88 88
   *
89 89
   * @return array
90
   *   A keyed array of language_key => language_name (example: 'en' => 'English').
90
   *   A keyed array of language_key => language_name. For example:
91
   *   'en' => 'English'.
91 92
   */
92 93
  public function languageOptions() {
93 94
    $languages = array(
......
123 124
  }
124 125

  
125 126
  /**
126
   * Load an existing entity.
127
   * Loads an existing entity.
127 128
   *
128
   * @param $source
129
   * @param FeedsSource $source
129 130
   *   The feeds source that spawns this entity.
130
   * @param $entity_id
131
   *   The unique id of the entity that should be loaded.
131
   * @param mixed $entity_id
132
   *   The id of the entity to load.
132 133
   *
133
   * @return
134
   * @return object
134 135
   *   A new entity object.
135 136
   *
136 137
   * @todo We should be able to batch load these, if we found all of the
......
160 161
  /**
161 162
   * Validates an entity.
162 163
   *
164
   * @param object $entity
165
   *   The entity to validate.
166
   * @param FeedsSource $source
167
   *   (optional) The source to import from.
168
   *
163 169
   * @throws FeedsValidationException $e
164 170
   *   Thrown if validation fails.
165 171
   */
166
  protected function entityValidate($entity) {
172
  protected function entityValidate($entity, FeedsSource $source = NULL) {
167 173
    $info = $this->entityInfo();
168 174

  
169 175
    if (!empty($info['entity keys']['language'])) {
......
179 185
    // Perform field validation if entity is fieldable.
180 186
    if (!empty($info['fieldable'])) {
181 187
      try {
182
        field_attach_validate($this->entityType(), $entity);
188
        $this->validateFields($entity);
183 189
      }
184 190
      catch (FieldValidationException $e) {
185 191
        $errors = array();
186 192

  
187 193
        // Unravel the errors inside the FieldValidationException.
188
        foreach ($e->errors as $field_name => $field_errors) {
189
          foreach ($field_errors as $langcode => $field_item_errors) {
190
            $errors = array_merge($errors, $this->unravelFieldValidationExceptionErrors($field_item_errors));
191
          }
194
        foreach ($e->errors as $field_errors) {
195
          $errors = array_merge($errors, $this->unravelFieldValidationExceptionErrors($field_errors));
192 196
        }
193 197

  
194 198
        // Compose error message. If available, use the entity label to indicate
......
212 216
  }
213 217

  
214 218
  /**
215
   * Helper function to unravel error messages hidden in a FieldValidationException.
219
   * Validates fields of an entity.
220
   *
221
   * This is mostly a copy of the field_attach_validate() function. The
222
   * difference is that field_attach_validate() validates *all* fields on an
223
   * entity, while Feeds only validates the fields where the user mapped to.
224
   *
225
   * @param object $entity
226
   *   The entity for which the field to validate.
227
   *
228
   * @throws FieldValidationException
229
   *   If validation errors are found, a FieldValidationException is thrown. The
230
   *   'errors' property contains the array of errors, keyed by field name,
231
   *   language and delta.
232
   */
233
  protected function validateFields($entity) {
234
    $entity_type = $this->entityType();
235

  
236
    // Get fields for the entity type we are mapping to.
237
    $fields = field_info_instances($entity_type, $this->bundle());
238

  
239
    // Get targets.
240
    $targets = $this->getCachedTargets();
241

  
242
    $errors = array();
243
    $null = NULL;
244

  
245
    // Validate all fields that we are mapping to.
246
    foreach ($this->getMappings() as $mapping) {
247
      // Get real target name.
248
      if (isset($targets[$mapping['target']]['real_target'])) {
249
        $target_name = $targets[$mapping['target']]['real_target'];
250
      }
251
      else {
252
        $target_name = $mapping['target'];
253
      }
254

  
255
      if (isset($fields[$target_name])) {
256
        // Validate this field.
257
        _field_invoke_default('validate', $entity_type, $entity, $errors, $null, array(
258
          'field_name' => $target_name,
259
        ));
260
        _field_invoke('validate', $entity_type, $entity, $errors, $null, array(
261
          'field_name' => $target_name,
262
        ));
263
      }
264
    }
265

  
266
    // Let other modules validate the entity.
267
    // Avoid module_invoke_all() to let $errors be taken by reference.
268
    foreach (module_implements('field_attach_validate') as $module) {
269
      $function = $module . '_field_attach_validate';
270
      $function($entity_type, $entity, $errors);
271
    }
272

  
273
    if ($errors) {
274
      throw new FieldValidationException($errors);
275
    }
276
  }
277

  
278
  /**
279
   * Helper function to unravel error messages hidden in a
280
   * FieldValidationException.
216 281
   *
217 282
   * @param array $field_item_errors
218 283
   *   The errors for a single field item.
......
220 285
   * @return array
221 286
   *   The unraveled error messages.
222 287
   */
223
  protected function unravelFieldValidationExceptionErrors($field_item_errors) {
288
  protected function unravelFieldValidationExceptionErrors(array $field_item_errors) {
224 289
    $errors = array();
225 290

  
226 291
    foreach ($field_item_errors as $field_item_error) {
......
238 303
  }
239 304

  
240 305
  /**
241
   * Access check for saving an enity.
306
   * Access check for saving an entity.
242 307
   *
243
   * @param $entity
244
   *   Entity to be saved.
308
   * @param object $entity
309
   *   The entity to be saved.
245 310
   *
246 311
   * @throws FeedsAccessException $e
247 312
   *   If the access check fails.
......
249 314
  protected function entitySaveAccess($entity) {}
250 315

  
251 316
  /**
252
   * Save an entity.
317
   * Saves an entity.
253 318
   *
254
   * @param $entity
255
   *   Entity to be saved.
319
   * @param object $entity
320
   *   The entity to be saved.
256 321
   */
257
  protected abstract function entitySave($entity);
322
  abstract protected function entitySave($entity);
258 323

  
259 324
  /**
260
   * Delete a series of entities.
325
   * Deletes a series of entities.
261 326
   *
262
   * @param $entity_ids
327
   * @param array $entity_ids
263 328
   *   Array of unique identity ids to be deleted.
264 329
   */
265
  protected abstract function entityDeleteMultiple($entity_ids);
330
  abstract protected function entityDeleteMultiple($entity_ids);
266 331

  
267 332
  /**
268
   * Wrap entity_get_info() into a method so that extending classes can override
269
   * it and more entity information. Allowed additional keys:
333
   * Wrapper around entity_get_info().
270 334
   *
271
   * 'label plural' ... the plural label of an entity type.
335
   * The entity_get_info() function is wrapped so that extending classes can
336
   * override it and add more entity information.
337
   *
338
   * Allowed additional keys:
339
   * - label plural
340
   *   the plural label of an entity type.
272 341
   */
273 342
  protected function entityInfo() {
274 343
    $info = entity_get_info($this->entityType());
......
372 441

  
373 442
        // Set property and field values.
374 443
        $this->map($source, $parser_result, $entity);
375
        $this->entityValidate($entity);
444

  
445
        // Allow modules to alter the entity before validating.
446
        module_invoke_all('feeds_prevalidate', $source, $entity, $item, $entity_id);
447
        $this->entityValidate($entity, $source);
376 448

  
377 449
        // Allow modules to alter the entity before saving.
378 450
        module_invoke_all('feeds_presave', $source, $entity, $item, $entity_id);
379 451
        if (module_exists('rules')) {
380
          rules_invoke_event('feeds_import_'. $source->importer()->id, $entity);
452
          rules_invoke_event('feeds_import_' . $source->importer()->id, $entity);
381 453
        }
382 454

  
383 455
        // Enable modules to skip saving at all.
......
427 499
    $messages = array();
428 500
    if ($state->created) {
429 501
      $messages[] = array(
430
       'message' => format_plural(
502
        'message' => format_plural(
431 503
          $state->created,
432 504
          'Created @number @entity.',
433 505
          'Created @number @entities.',
......
437 509
    }
438 510
    if ($state->updated) {
439 511
      $messages[] = array(
440
       'message' => format_plural(
512
        'message' => format_plural(
441 513
          $state->updated,
442 514
          'Updated @number @entity.',
443 515
          'Updated @number @entities.',
......
467 539
    }
468 540
    if ($state->deleted) {
469 541
      $messages[] = array(
470
       'message' => format_plural(
542
        'message' => format_plural(
471 543
          $state->deleted,
472 544
          'Removed @number @entity.',
473 545
          'Removed @number @entities.',
......
477 549
    }
478 550
    if ($state->failed) {
479 551
      $messages[] = array(
480
       'message' => format_plural(
552
        'message' => format_plural(
481 553
          $state->failed,
482 554
          'Failed importing @number @entity.',
483 555
          'Failed importing @number @entities.',
......
554 626
  }
555 627

  
556 628
  /**
557
   * Remove all stored results or stored results up to a certain time for a
558
   * source.
629
   * Removes all imported items for a source.
559 630
   *
560 631
   * @param FeedsSource $source
561 632
   *   Source information for this expiry. Implementers should only delete items
......
617 688
    }
618 689
  }
619 690

  
620
  /*
691
  /**
621 692
   * Report number of items that can be processed per call.
622 693
   *
623 694
   * 0 means 'unlimited'.
......
640 711
   *
641 712
   * @param FeedsSource $source
642 713
   *   The source to expire entities for.
643
   *
644
   * @param $time
714
   * @param int|null $time
645 715
   *   (optional) All items produced by this configuration that are older than
646 716
   *   REQUEST_TIME - $time should be deleted. If NULL, processor should use
647 717
   *   internal configuration. Defaults to NULL.
648 718
   *
649
   * @return float
650
   *   FEEDS_BATCH_COMPLETE if all items have been processed, a float between 0
651
   *   and 0.99* indicating progress otherwise.
719
   * @return float|null
720
   *   FEEDS_BATCH_COMPLETE if all items have been processed, null if expiring
721
   *   is disabled, a float between 0 and 0.99* indicating progress otherwise.
652 722
   *
653 723
   * @see FeedsSource::expire()
654 724
   */
......
714 784

  
715 785
  /**
716 786
   * Counts the number of items imported by this processor.
787
   *
788
   * @return int
789
   *   The number of items imported by this processor.
717 790
   */
718 791
  public function itemCount(FeedsSource $source) {
719
    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();
792
    return db_query("SELECT count(*) FROM {feeds_item} WHERE id = :id AND entity_type = :entity_type AND feed_nid = :feed_nid", array(
793
      ':id' => $this->id,
794
      ':entity_type' => $this->entityType(),
795
      ':feed_nid' => $source->feed_nid,
796
    ))->fetchField();
720 797
  }
721 798

  
722 799
  /**
......
884 961
  }
885 962

  
886 963
  /**
887
   * Per default, don't support expiry. If processor supports expiry of imported
888
   * items, return the time after which items should be removed.
964
   * Returns the time in seconds after which items should be removed.
965
   *
966
   * If expiry of items isn't supported, FEEDS_EXPIRE_NEVER is returned.
967
   *
968
   * By default, expiring items isn't supported. Processors that want to support
969
   * this feature, should override this method.
970
   *
971
   * @return int
972
   *   The expiry time or FEEDS_EXPIRE_NEVER if expiry isn't supported.
889 973
   */
890 974
  public function expiryTime() {
891 975
    return FEEDS_EXPIRE_NEVER;
892 976
  }
893 977

  
894 978
  /**
895
   * Declare default configuration.
979
   * {@inheritdoc}
896 980
   */
897 981
  public function configDefaults() {
898 982
    $info = $this->entityInfo();
......
949 1033
  }
950 1034

  
951 1035
  /**
952
   * Overrides parent::configForm().
1036
   * {@inheritdoc}
953 1037
   */
954 1038
  public function configForm(&$form_state) {
955 1039
    $info = $this->entityInfo();
......
1058 1142

  
1059 1143
  /**
1060 1144
   * Get mappings.
1145
   *
1146
   * @return array
1147
   *   A list of configured mappings.
1061 1148
   */
1062 1149
  public function getMappings() {
1063 1150
    $cache = &drupal_static('FeedsProcessor::getMappings', array());
......
1099 1186
   *
1100 1187
   * @ingroup mappingapi
1101 1188
   *
1102
   * @return
1189
   * @return array
1103 1190
   *   An array of mapping targets. Keys are paths to targets
1104 1191
   *   separated by ->, values are TRUE if target can be unique,
1105 1192
   *   FALSE otherwise.
......
1166 1253
  /**
1167 1254
   * Retrieve the target entity's existing id if available. Otherwise return 0.
1168 1255
   *
1169
   * @ingroup mappingapi
1170
   *
1171 1256
   * @param FeedsSource $source
1172 1257
   *   The source information about this import.
1173 1258
   * @param FeedsParserResult $result
......
1175 1260
   *
1176 1261
   * @return int
1177 1262
   *   The serial id of an entity if found, 0 otherwise.
1263
   *
1264
   * @ingroup mappingapi
1178 1265
   */
1179 1266
  protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
1180 1267
    $targets = $this->getCachedTargets();
......
1217 1304
    return $entity_id;
1218 1305
  }
1219 1306

  
1220

  
1221 1307
  /**
1222
   * Utility function that iterates over a target array and retrieves all
1223
   * sources that are unique.
1308
   * Returns all mapping targets that are marked as unique.
1224 1309
   *
1225
   * @param $batch
1226
   *   A FeedsImportBatch.
1310
   * @param FeedsSource $source
1311
   *   The source information about this import.
1312
   * @param FeedsParserResult $result
1313
   *   A FeedsParserResult object.
1227 1314
   *
1228
   * @return
1315
   * @return array
1229 1316
   *   An array where the keys are target field names and the values are the
1230 1317
   *   elements from the source item mapped to these targets.
1231 1318
   */
......
1245 1332
  /**
1246 1333
   * Adds Feeds specific information on $entity->feeds_item.
1247 1334
   *
1248
   * @param $entity
1335
   * @param object $entity
1249 1336
   *   The entity object to be populated with new item info.
1250
   * @param $feed_nid
1337
   * @param int $feed_nid
1251 1338
   *   The feed nid of the source that produces this entity.
1252
   * @param $hash
1339
   * @param string $hash
1253 1340
   *   The fingerprint of the source item.
1254 1341
   */
1255 1342
  protected function newItemInfo($entity, $feed_nid, $hash = '') {
......
1268 1355
  /**
1269 1356
   * Loads existing entity information and places it on $entity->feeds_item.
1270 1357
   *
1271
   * @param $entity
1358
   * @param object $entity
1272 1359
   *   The entity object to load item info for. Id key must be present.
1273 1360
   *
1274
   * @return
1361
   * @return bool
1275 1362
   *   TRUE if item info could be loaded, false if not.
1276 1363
   */
1277 1364
  protected function loadItemInfo($entity) {
......
1323 1410
   *
1324 1411
   * @param Exception $e
1325 1412
   *   The exception that was throwned during processing the item.
1326
   * @param $entity
1413
   * @param object $entity
1327 1414
   *   The entity object.
1328
   * @param $item
1415
   * @param object $item
1329 1416
   *   The parser result for this entity.
1330 1417
   *
1331 1418
   * @return string
......
1424 1511

  
1425 1512
}
1426 1513

  
1427
class FeedsProcessorBundleNotDefined extends Exception {}
1428

  
1429 1514
/**
1430 1515
 * Form after build callback for the field "update_existing".
1431 1516
 *
1432 1517
 * Adds descriptions to options of this field.
1433 1518
 */
1434 1519
function feeds_processor_config_form_update_existing_after_build($field) {
1435
  $field[FEEDS_REPLACE_EXISTING]['#description'] =  t('Loads records directly from the database, bypassing the Entity API. Faster, but use with caution.');
1436
  $field[FEEDS_UPDATE_EXISTING]['#description'] =  t('Loads complete @entities using the Entity API for full integration with other modules. Slower, but most reliable.', $field['#tokens']);
1520
  $field[FEEDS_REPLACE_EXISTING]['#description'] = t('Loads records directly from the database, bypassing the Entity API. Faster, but use with caution.');
1521
  $field[FEEDS_UPDATE_EXISTING]['#description'] = t('Loads complete @entities using the Entity API for full integration with other modules. Slower, but most reliable.', $field['#tokens']);
1437 1522
  return $field;
1438 1523
}

Formats disponibles : Unified diff