You are here

function backup_migrate_item::generate_id in Backup and Migrate 8.3

Same name and namespace in other branches
  1. 8.2 includes/crud.inc \backup_migrate_item::generate_id()
  2. 6.3 includes/crud.inc \backup_migrate_item::generate_id()
  3. 6.2 includes/crud.inc \backup_migrate_item::generate_id()
  4. 7.3 includes/crud.inc \backup_migrate_item::generate_id()
  5. 7.2 includes/crud.inc \backup_migrate_item::generate_id()

Return a random (very very likely unique) string id for a new item.

1 call to backup_migrate_item::generate_id()
backup_migrate_item::unique_id in includes/crud.inc
Make sure this item has a unique id. Should only be called for new items or the item will collide with itself.

File

includes/crud.inc, line 614
CRUD functions for backup and migrate types (schedules, profiles etc.).

Class

backup_migrate_item
A base class for items which can be stored in the database, listed, edited, deleted etc.

Code

function generate_id() {
  $id = md5(uniqid(mt_rand(), true));

  // Find the shortest possible unique id from (min 4 chars).
  for ($i = 4; $i < 32; $i++) {
    $new_id = substr($id, 0, $i);
    if (!$this
      ->item($new_id)) {
      return $new_id;
    }
  }

  // If we get here, then all 28 increasingly complex ids were already taken so we'll try again.
  // this could theoretially lead to an infinite loop, but the odds are incredibly low.
  return $this
    ->generate_id();
}