environment_indicator.install 7.31 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
5
 * Install implementation file.
6
7
8
 */

/**
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
9
 * Implements hook_schema().
10
 */
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
11
12
function environment_indicator_schema() {
  $schema['environment_indicator_environment'] = array(
13
    'description' => 'Table storing preset definitions.',
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
14
15
16
17
    'export' => array(
      'key' => 'machine',
      'key name' => 'Machine name',
      'primary key' => 'envid',
18
19
20
21
      // Exports will be defined as $environment.
      'identifier' => 'environment',
      // Function hook name.
      'default hook' => 'default_environment_indicator_environment',
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
22
23
      'api' => array(
        'owner' => 'environment_indicator',
24
25
        // Base name for api include files.
        'api' => 'default_environment_indicator_environments',
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
        'minimum_version' => 1,
        'current_version' => 1,
      ),
    ),
    'fields' => array(
      'machine' => array(
        'type' => 'varchar',
        'length' => '32',
        'description' => 'Unique ID for environments.',
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => '255',
        'description' => 'Name for the environments.',
      ),
      'envid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
46
47
        // Do not export database-only keys.
        'no export' => TRUE,
Mateu Aguiló Bosch's avatar
Mateu Aguiló Bosch committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
      ),
      'regexurl' => array(
        'type' => 'varchar',
        'length' => '255',
        'description' => 'A regular expression to match against the url.',
      ),
      'settings' => array(
        'type' => 'text',
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'Serialized array with the configuration for the environment.',
      ),
    ),
    'primary key' => array('envid'),
    'unique keys' => array(
      'name' => array('machine'),
    ),
  );
  return $schema;
67
}
68

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
 * Implements hook_uninstall().
 */
function environment_indicator_uninstall() {
  variable_del('environment_indicator_suppress_pages');
  variable_del('environment_indicator_integration');
  variable_del('environment_indicator_git_support');
  // Delete environment_indicator_remote_release.*
  $query = "SELECT name FROM {variable} WHERE name LIKE 'environment_indicator_remote_release.%'";
  $results = db_query($query);
  foreach ($results as $row) {
    // Use variable_del to allow appropriate hooks to run.
    variable_del($row->name);
  }
}

/**
 * Implements hook_requirements().
 */
function environment_indicator_requirements($phase) {
  $requirements = array();
90
  // Ensure translations don't break at install time.
91
92
  $t = get_t();

93
  // Report Drupal version.
94
95
96
97
98
  if ($phase == 'runtime') {
    if (_environment_indicator_has_old_vars()) {
      $requirements['environment_indicator'] = array(
        'title' => $t('Environment indicator'),
        'value' => $t('You are using old environment indicator variables. Please check the !help on how to upgrade your variables for the new release, and then remove the old ones.', array('!help' => l(t('help pages'), 'admin/help/environment_indicator'))),
99
        'severity' => REQUIREMENT_ERROR,
100
101
102
103
104
105
106
107
108
      );
    }
    if (
      !empty($_ENV['AH_SITE_ENVIRONMENT']) &&
      variable_get('environment_indicator_git_support', TRUE)
    ) {
      $severity = REQUIREMENT_WARNING;
      $env = $_ENV['AH_SITE_ENVIRONMENT'];
      if (environment_indicator_ah_valid_post_deploy_hook($env)) {
109
110
111
        // If there is no value in the release variable it can mean that the
        // hook never run. Tell the user to check execute permisions on the
        // script.
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
        $release = variable_get('environment_indicator_remote_release.' . $env, NULL);
        if ($release) {
          $message = $t('The Acquia Cloud hook is installed and reports release: ' . $release);
          $severity = REQUIREMENT_INFO;
        }
        else {
          $message = $t('The Acquia Cloud hook is installed but there is no information about the release. This is normal if you never deployed your code. Otherwise check if the hook script has the execution permisions.');
        }
      }
      else {
        $message = $t('Missing Acquia Cloud hook for environment indicator integration. Please look at the <code>samples/environment-indicator.sh</code> folder and copy it to your <code>hooks/[your-environment]/post-code-deploy</code> directory. See the README.txt for more information.');
      }
      $requirements['environment_indicator_ah'] = array(
        'title' => $t('Environment indicator'),
        'value' => $message,
        'severity' => $severity,
      );
    }
  }
  return $requirements;
}

134
/**
135
 * Add table when updating from 1.x.
136
137
138
139
140
141
142
 */
function environment_indicator_update_7200(&$sandbox) {
  if (!db_table_exists('environment_indicator_environment')) {
    $schema = environment_indicator_schema();
    db_create_table('environment_indicator_environment', $schema['environment_indicator_environment']);
  }
}
143
144
145
146
147
148
149
150
151

/**
 * Add the default text color for existing environments.
 */
function environment_indicator_update_7201(&$sandbox) {
  module_load_include('inc', 'ctools', 'includes/export');
  $environments = ctools_export_crud_load_all('environment_indicator_environment');
  foreach ($environments as $machine => $environment) {
    if (!isset($environment->settings['text_color'])) {
152
      $environment->settings['text_color'] = '#ffffff';
153
154
155
156
      ctools_export_crud_save('environment_indicator_environment', $environment);
    }
  }
}
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189

/**
 * Migrate the old variables to the default environment.
 */
function environment_indicator_update_7202(&$sandbox) {
  module_load_include('inc', 'ctools', 'includes/export');
  $default_environment = ctools_export_crud_load('environment_indicator_environment', 'default_environment');
  // We want to load the environment variables directly from the database, not
  // from settings.php. Using variable_get() is thus not an option, since it
  // uses the values from settings.php to override those in the database.
  $result = db_query("SELECT * FROM {variable} WHERE name LIKE 'environment_indicator_%'");
  $updated = FALSE;
  if ($result) {
    while ($row = $result->fetchAssoc()) {
      switch ($row['name']) {
        case 'environment_indicator_color':
          $default_environment->settings['color'] = unserialize($row['value']);
          variable_del($row['name']);
          $updated = TRUE;
          break;

        case 'environment_indicator_text':
          $default_environment->name = unserialize($row['value']);
          variable_del($row['name']);
          $updated = TRUE;
          break;

        case 'environment_indicator_enabled':
        case 'environment_indicator_margin':
        case 'environment_indicator_position':
          // This variable is no longer used. Remove it.
          variable_del($row['name']);
          break;
190

191
192
193
194
195
196
197
198
199
200
201
202
203
204
        case 'environment_indicator_suppress_pages':
          // This variable is still used, keep it.
          break;
      }
    }
  }

  // Only save if the default environment was not edited yet. The old variables
  // have been removed nonetheless.
  if (!($defaut_environment->export_type & EXPORT_IN_DATABASE) && $default_environment->export_module == 'environment_indicator' && $updated) {
    ctools_export_crud_save('environment_indicator_environment', $default_environment);
    return t('Your default environment was updated with your previous configuration.');
  }
}