How to programmatically create a large taxonomy in Drupal 7? I need to create a taxonomy with an entry for every living language in the world (nearly 7,000, using ISO 639-3 codes: http://en.wikipedia.org/wiki/List_of_ISO_639-3_codes). Note that the ISO 639-3 code is stored as a custom field.

In light of this: what is the easiest way to create this list directly via MySQL using a PHP (or other) script? 

I have attempted to directly create entries by inserting values directly into MySQL:

INSERT INTO taxonomy_term_data (vid, name, description, format) VALUES (2,"Ghotuo","&lt;p&gt;&lt;em&gt;Africa&lt;/em&gt;&lt;/p&gt;","filtered_html");

INSERT INTO field_data_field_iso_639_3 (entity_type, bundle, entity_id, revision_id, language, delta, field_iso_639_3_value) VALUES ("taxonomy_term", "language", 3, 3, "und", 0, "aaa");

INSERT INTO url_alias (source, alias, language) VALUES ("taxonomy/term/3", "language/ghotuo", "und");


I am obviously missing something, because this entry is not seen by Drupal in the "Languages" taxonomy. 

I have looked at this question but am unsure how to set this up as a stand-alone PHP script (not incorporated into the site itself). 

Any ideas how to programmatically create this taxonomy? TIA.
Myself still learning how to properly write modules, I managed to come up with the following code for saving the terms into separate table named MODULENAME_data. It may not be perfect - but it apparently worked. I'm open to downvoting, but even more to suggestions on how to improve it.

// Make the table name a constant for easier coding
// We will store it's vid as system variable after we have created it
define("MODULENAME_VOCAB_MACHINE_NAME", "iso_639_3_codes_data");

function MODULENAME_enable(){
  // Create the vocabulary.
  $vocab = array(
    'name' =&gt; 'Vocabulary name',
    'machine_name' =&gt; MODULENAME_VOCAB_MACHINE_NAME,
    'description' =&gt; t('Vocabulary of iso 639-3 language codes'),
    'module' =&gt; 'taxonomy',
    'hierarchy' =&gt; 0,
    'module' =&gt; 'MODULENAME',
  );
  taxonomy_vocabulary_save( (object) $vocab);

  // Now that we've created the vocabulary, let's store its vid as a system variable
  $vocab = taxonomy_vocabulary_machine_name_load(MODULENAME_VOCAB_MACHINE_NAME);
  $vid = $vocab-&gt;vid;
  variable_set('MODULENAME_vocab_id', $vid);

  // Create custom field.
  $field = array(
    'field_name' =&gt; 'field_custom_field',
    'type' =&gt; 'text',
    'label' =&gt; t('Symbol') // where is that used? I dunno, but API says so ;-)
  );
  field_create_field($field);

  // Create field instance on the vocab
  $instance = array(
    'field_name' =&gt; 'field_custom_field',
    'entity_type' =&gt; 'taxonomy_term',
    'bundle' =&gt; MODULENAME_VOCAB_MACHINE_NAME,
    'label' =&gt; t('Custom field'),
    'description' =&gt; t('Description'),
    'required' =&gt; true,
    'widget' =&gt; array(
      'type' =&gt; 'text_textfield',
     )
  );
  field_create_instance($instance);

  // Here you would include an include file that provides row data
  // (probably an array) and then iterate over it to populate table
  // such as

      // Save terms.
      foreach ($items as $item) {
        taxonomy_term_save((object) array(
          'name' =&gt; $item[0],
          'vid' =&gt; $vid, 
          'field_custom_field' =&gt; array(
            LANGUAGE_NONE =&gt; array(
              0 =&gt; array(
                'value' =&gt; $item[1] ,
              )
            )
          ),    
        ));

}

function MODULENAME_disable(){
  // Some system cleanup.

  // Delete vocabulary. This deletes terms and field references too.
  taxonomy_vocabulary_delete(variable_get('MODULENAME_vocab_id'));

  // Delete variable from system.
  variable_del('MODULENAME_vocab_id');
}


Word of comment:


I'm still not sure whether the hook_disable() would be supposed to do what I put into it, but my understanding is that once a module is disabled, the features installed by it would be supposed to be unavailable too. I'm unsure about the field references - not only do they remain in the system in my code, but once we re-enable the module, the references will not be restored due to new IDs.
I'm also not sure how one would make the vocabulary i18n-savvy, meaning - when you would like to install the vocabulary along with translations - vocabularies provide several i18n modes and I'm not in the knowledge how to support them programatically.


This is something I would love to learn from someone.