You are here

function drush_domain_generate_domains in Domain Access 7.3

Same name and namespace in other branches
  1. 8 domain/domain.drush.inc \drush_domain_generate_domains()

Generate a list of domains for testing.

In my environment, I name subdomains one.* two.* up to ten. I also use foo.* bar.* and baz.*. We also want a non-subdomain here and use myexample.com.

The script may also add test1, test2, test3 up to any number to test a large number of domains. This test is mostly for UI testing.

Parameters

$primary: The root domain to use for domain creation.

Return value

A list of the domains created.

File

./domain.drush.inc, line 156
Drush commands for Domain Access.

Code

function drush_domain_generate_domains($primary = 'example.com') {

  // Check the number of domains to create.
  $count = drush_get_option('count');
  $domains = domain_domains(TRUE);
  if (empty($count)) {
    $count = 15;
  }

  // Ensure we don't duplicate any domains.
  $existing = array();
  if (!empty($domains)) {
    foreach ($domains as $domain) {
      $existing[] = $domain['subdomain'];
    }
  }

  // Set up one.* and so on.
  $names = array(
    'one',
    'two',
    'three',
    'four',
    'five',
    'six',
    'seven',
    'eight',
    'nine',
    'ten',
    'foo',
    'bar',
    'baz',
  );

  // Set the creation array.
  $new = array(
    $primary,
  );
  foreach ($names as $name) {
    $new[] = $name . '.' . $primary;
  }

  // Include a non subdomain.
  $new[] = 'my' . $primary;

  // Filter against existing so we can count correctly.
  $prepared = array();
  foreach ($new as $key => $value) {
    if (!in_array($value, $existing)) {
      $prepared[] = $value;
    }
  }

  // Add any test domains.
  if ($count > 15 || empty($prepared)) {

    // Find the highest numbered test domain.
    $start = db_query("SELECT sitename FROM {domain} WHERE sitename LIKE 'test%' ORDER BY domain_id DESC")
      ->fetchField();
    $start = (int) str_ireplace('test', '', $start);
    $j = count($prepared);
    for ($i = $start + 1; $j <= $count; $i++) {
      $prepared[] = 'test' . $i . '.' . $primary;
      $j++;
    }
  }

  // Get the initial item weight for sorting.
  $start_weight = db_query("SELECT weight FROM {domain} ORDER BY weight DESC")
    ->fetchField();
  $prepared = array_slice($prepared, 0, $count);
  foreach ($prepared as $key => $item) {
    $record = array(
      'sitename' => $item != $primary ? ucwords(str_replace(".{$primary}", '', $item)) : variable_get('site_name', 'Drupal'),
      'subdomain' => strtolower($item),
      'scheme' => 'http',
      'valid' => 1,
      'weight' => $item != $primary ? $key + $start_weight + 1 : -1,
      'is_default' => 0,
    );
    $created = domain_save($record, $record);
    drush_print(dt('Created !domain.', array(
      '!domain' => $record['sitename'],
    )));
  }
  db_update('domain')
    ->condition('subdomain', $primary)
    ->fields(array(
    'is_default' => 1,
  ))
    ->execute();
  if (empty($new)) {
    drush_print(dt('No new domains were created.'));
  }
}