You are here

function devel_generate_terms in Devel 7

Same name and namespace in other branches
  1. 5 devel_generate.inc \devel_generate_terms()
  2. 6 devel_generate.inc \devel_generate_terms()

Generates taxonomy terms for a list of given vocabularies.

Parameters

$records: int number of terms to create in total.

$vocabs: array list of vocabs to populate.

$maxlength: int maximum length per term.

Return value

array the list of names of the created terms.

2 calls to devel_generate_terms()
devel_generate_term_form_submit in devel_generate/devel_generate.module
FormAPI submission to generate taxonomy terms.
drush_devel_generate_terms in devel_generate/devel_generate.drush.inc
Command callback. Generate a number of terms in given vocabs.

File

devel_generate/devel_generate.inc, line 226

Code

function devel_generate_terms($records, $vocabs, $maxlength = 12) {
  $terms = array();

  // Insert new data:
  $max = db_query('SELECT MAX(tid) FROM {taxonomy_term_data}')
    ->fetchField();
  $start = time();
  for ($i = 1; $i <= $records; $i++) {
    $term = new stdClass();
    switch ($i % 2) {
      case 1:

        // Set vid and vocabulary_machine_name properties.
        $vocab = $vocabs[array_rand($vocabs)];
        $term->vid = $vocab->vid;
        $term->vocabulary_machine_name = $vocab->machine_name;

        // Don't set a parent. Handled by taxonomy_save_term()
        // $term->parent = 0;
        break;
      default:
        while (TRUE) {

          // Keep trying to find a random parent.
          $candidate = mt_rand(1, $max);
          $query = db_select('taxonomy_term_data', 't');
          $query
            ->innerJoin('taxonomy_vocabulary', 'v', 't.vid = v.vid');
          $parent = $query
            ->fields('t', array(
            'tid',
            'vid',
          ))
            ->fields('v', array(
            'machine_name',
          ))
            ->condition('v.vid', array_keys($vocabs), 'IN')
            ->condition('t.tid', $candidate, '>=')
            ->range(0, 1)
            ->execute()
            ->fetchAssoc();
          if ($parent['tid']) {
            break;
          }
        }
        $term->parent = $parent['tid'];

        // Slight speedup due to this property being set.
        $term->vocabulary_machine_name = $parent['machine_name'];
        $term->vid = $parent['vid'];
        break;
    }
    $term->name = devel_generate_word(mt_rand(2, $maxlength));
    $term->description = "description of " . $term->name;
    $term->format = filter_fallback_format();
    $term->weight = mt_rand(0, 10);
    $term->language = LANGUAGE_NONE;
    $term->devel_generate = TRUE;

    // Populate all core fields on behalf of field.module
    module_load_include('inc', 'devel_generate', 'devel_generate.fields');
    devel_generate_fields($term, 'taxonomy_term', $term->vocabulary_machine_name);
    if ($status = taxonomy_term_save($term)) {
      $max += 1;
      if (function_exists('drush_log')) {
        $feedback = drush_get_option('feedback', 1000);
        if ($i % $feedback == 0) {
          $now = time();
          drush_log(dt('Completed !feedback terms (!rate terms/min)', array(
            '!feedback' => $feedback,
            '!rate' => $feedback * 60 / ($now - $start),
          )), 'ok');
          $start = $now;
        }
      }

      // Limit memory usage. Only report first 20 created terms.
      if ($i < 20) {
        $terms[] = $term->name;
      }
      unset($term);
    }
  }
  return $terms;
}