You are here

function taxonomy_revision_install in Taxonomy revision 7

Implements hook_install().

File

./taxonomy_revision.install, line 120
Install, update and uninstall functions for the taxonomy revision module.

Code

function taxonomy_revision_install() {
  $schema = array();
  taxonomy_revision_schema_alter($schema);

  // Altering the {taxonomy_term_data} table.
  $spec = $schema['taxonomy_term_data']['fields']['revision_id'];
  $indexes_new = array();

  // If another module had added a {taxonomy_term_data}.revision_id field,
  // then change it to the expected specification. Otherwise, add the field.
  if (db_field_exists('taxonomy_term_data', 'revision_id')) {

    // db_change_field() will fail if any records have type=NULL, so update
    // them to the new default value.
    db_update('taxonomy_term_data')
      ->fields(array(
      'revision_id' => $spec['default'],
    ))
      ->isNull('revision_id')
      ->execute();

    // Indexes using a field being changed must be dropped prior to calling
    // db_change_field(). However, the database API doesn't provide a way to do
    // this without knowing what the old indexes are. Therefore, it is the
    // responsibility of the module that added them to drop them prior to
    // allowing this module to be installed.
    db_change_field('taxonomy_term_data', 'revision_id', 'revision_id', $spec, $indexes_new);
  }
  else {
    db_add_field('taxonomy_term_data', 'revision_id', $spec, $indexes_new);
  }
  $query = db_select('taxonomy_term_data', 'ttd');
  $query
    ->addExpression('MAX(tid)', 'max_tid');
  $max_tid = $query
    ->execute()
    ->fetchField();

  // If we have taxonomy terms already in the {taxonomy_term_data} table we must
  // create the current revisions for them.
  if ($max_tid) {
    $fields = array(
      'tid',
      'vid',
      'name',
      'description',
      'format',
      'weight',
    );
    $query = db_select('taxonomy_term_data', 'ttd')
      ->fields('ttd', $fields);

    // For entities that don't have revisions, the field API uses the entity ID
    // as the revision ID; see field_sql_storage_field_storage_write(). To
    // ensure that the initial term revisions being created here will match up
    // with the existing field API data, the same revision ID must be used.
    $query
      ->addField('ttd', 'tid', 'revision_id');
    $terms = $query
      ->condition('tid', 0, '>')
      ->orderBy('tid', 'ASC')
      ->execute()
      ->fetchAllAssoc('tid', PDO::FETCH_ASSOC);
    foreach ($terms as $term) {
      $term['log'] = 'Initial revision created by the taxonomy revision module on install.';
      db_insert('taxonomy_term_data_revision')
        ->fields($term)
        ->execute();
      db_update('taxonomy_term_data')
        ->fields(array(
        'revision_id' => $term['revision_id'],
      ))
        ->condition('tid', $term['tid'])
        ->execute();
    }
  }
  if (!db_index_exists('taxonomy_term_data', 'revision_id')) {
    db_add_unique_key('taxonomy_term_data', 'revision_id', array(
      'revision_id',
    ));
  }
}