You are here

amazons3.install in AmazonS3 7.2

Same filename and directory in other branches
  1. 7 amazons3.install

Install, update and uninstall functions for the AmazonS3 module.

File

amazons3.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the AmazonS3 module.
 */

/**
 * Implements hook_requirements().
 */
function amazons3_requirements($phase) {
  $t = get_t();
  $requirements = array();
  if ($phase != 'runtime') {
    return array();
  }

  // Composer Manager will throw a requirements warning for us.
  if (!class_exists('Drupal\\amazons3\\StreamWrapperConfiguration')) {
    return array();
  }
  $error = NULL;
  try {
    $config = \Drupal\amazons3\StreamWrapperConfiguration::fromDrupalVariables();
    try {
      $client = \Drupal\amazons3\S3Client::factory(array(), $config
        ->getBucket());
      \Drupal\amazons3\S3Client::validateBucketExists($config
        ->getBucket(), $client);
    } catch (\Drupal\amazons3\Exception\S3ConnectValidationException $e) {
      $error = $t($e
        ->getMessage());
    }
  } catch (\InvalidArgumentException $e) {
    $error = $t($e
      ->getMessage());
  }
  if ($error) {
    $configure = l($t('Configure'), 'admin/config/media/amazons3');
    $requirements['amazons3_connection'] = array(
      'severity' => REQUIREMENT_ERROR,
      'title' => $t('AmazonS3 Configuration'),
      'value' => $configure,
      'description' => $error,
    );
  }
  return $requirements;
}

/**
 * Implements hook_uninstall().
 */
function amazons3_uninstall() {
  variable_del('amazons3_key');
  variable_del('amazons3_secret');
  variable_del('amazons3_bucket');
  variable_del('amazons3_cname');
  variable_del('amazons3_domain');
  variable_del('amazons3_domain_scheme');
  variable_del('amazons3_cache');
  variable_del('amazons3_cache_expiration');
  variable_del('amazons3_torrents');
  variable_del('amazons3_presigned_urls');
  variable_del('amazons3_saveas');
  variable_del('amazons3_rrs');
  variable_del('amazons3_hostname');
}

/**
 * Implements hook_schema().
 */
function amazons3_schema() {
  $schema = array();
  $schema['cache_amazons3_metadata'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_amazons3_metadata']['description'] = 'Cache for AmazonS3 metadata.';
  return $schema;
}

/**
 * Install the caching table.
 */
function amazons3_update_7100($sandbox) {
  $schema['amazons3_file'] = array(
    'description' => 'Stores information for uploaded Amazon S3 files.',
    'fields' => array(
      'uri' => array(
        'description' => 'The URI to access the file (either local or remote).',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'filesize' => array(
        'description' => 'The size of the file in bytes.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'timestamp' => array(
        'description' => 'UNIX timestamp for when the file was added.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'dir' => array(
        'description' => 'Boolean indicating whether or not this object is a directory.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'mode' => array(
        'description' => 'The file mode returned by the stat function.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'description' => 'The uid of the user who is associated with the file (not Drupal uid).',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'timestamp' => array(
        'timestamp',
      ),
    ),
    'primary key' => array(
      'uri',
    ),
  );
  db_create_table('amazons3_file', $schema['amazons3_file']);
}

/**
 * Change uid to a varchar.
 */
function amazons3_update_7101(&$sandbox) {
  $spec = array(
    'description' => 'The uid of the user who is associated with the file (not Drupal uid).',
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  );
  db_change_field('amazons3_file', 'uid', 'uid', $spec);
}

/**
 * Update the filesize column to use a bigint, to allow TB filesizes.
 */
function amazons3_update_7102(&$sandbox) {
  $spec = array(
    'description' => 'The size of the file in bytes.',
    'type' => 'int',
    'size' => 'big',
    'length' => 14,
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );
  db_change_field('amazons3_file', 'filesize', 'filesize', $spec);
}

/**
 * Enable composer, add a bucket to all URIs, and delete the HTTPS variable.
 */
function amazons3_update_7200() {
  if (!module_enable(array(
    'composer_manager',
  ))) {
    throw new DrupalUpdateException('Composer Manager could not be enabled.');
  }

  // Run this early so later updates don't fail due to a missing autoloader.
  if (function_exists('drush_composer_manager_write_if_changed')) {
    drush_composer_manager_write_if_changed();
  }
  $bucket = variable_get('amazons3_bucket');
  db_update('file_managed')
    ->condition('uri', 's3://%', 'LIKE')
    ->expression('uri', 'REPLACE(uri, :protocol, :bucket)', array(
    ':protocol' => 's3://',
    ':bucket' => "s3://{$bucket}/",
  ))
    ->execute();
  variable_del('amazons3_https');
}

/**
 * Copy Amazon keys in the database from awssdk 1.x.
 */
function amazons3_update_7201() {
  $t = get_t();
  drupal_set_message($t('AmazonS3 no longer requires the AWS SDK module.'));
  drupal_set_message($t('If your API keys are in settings.php, they will need to be renamed to amazons3_key and amazons3_secret. To avoid writing these values to the database, set amazons3_migrate_credentials to FALSE in settings.php.'));
  if (variable_get('amazons3_migrate_credentials', TRUE)) {
    if ($key = variable_get('aws_key', FALSE)) {
      variable_set('amazons3_key', $key);
    }
    if ($secret = variable_get('aws_secret', FALSE)) {
      variable_set('amazons3_secret', $secret);
    }
    drupal_set_message($t('Credentials have been migrated to AmazonS3 variables.'));
  }
}

/**
 * Switch to using the cache API for metadata.
 */
function amazons3_update_7202() {
  $schema = array();
  $schema['cache_amazons3_metadata'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_amazons3_metadata']['description'] = 'Cache for AmazonS3 metadata.';
  db_create_table('cache_amazons3_metadata', $schema['cache_amazons3_metadata']);
  db_drop_table('amazons3_file');
}

/**
 * Convert all multiline string variables to arrays.
 */
function amazons3_update_7203() {
  $variables = array(
    'amazons3_presigned_urls',
    'amazons3_rrs',
    'amazons3_saveas',
    'amazons3_torrents',
  );
  foreach ($variables as $name) {
    $value = variable_get($name, array());
    if (is_string($value)) {
      $value = explode("\n", $value);
      $value = array_map('trim', $value);
      $value = array_filter($value, 'strlen');
      variable_set($name, $value);
    }
  }
}

/**
 * Convert presigned URLs to structured arrays.
 */
function amazons3_update_7204() {
  $lines = variable_get('amazons3_presigned_urls', array());
  if (empty($lines)) {
    return;
  }
  $config = array();
  foreach ($lines as $line) {
    list($timeout, $pattern) = explode("|", $line);
    $config[] = array(
      'timeout' => $timeout,
      'pattern' => $pattern,
    );
  }
  variable_set('amazons3_presigned_urls', $config);
}

Functions

Namesort descending Description
amazons3_requirements Implements hook_requirements().
amazons3_schema Implements hook_schema().
amazons3_uninstall Implements hook_uninstall().
amazons3_update_7100 Install the caching table.
amazons3_update_7101 Change uid to a varchar.
amazons3_update_7102 Update the filesize column to use a bigint, to allow TB filesizes.
amazons3_update_7200 Enable composer, add a bucket to all URIs, and delete the HTTPS variable.
amazons3_update_7201 Copy Amazon keys in the database from awssdk 1.x.
amazons3_update_7202 Switch to using the cache API for metadata.
amazons3_update_7203 Convert all multiline string variables to arrays.
amazons3_update_7204 Convert presigned URLs to structured arrays.