Handles AdvAgg Validator installation and upgrade tasks.

Fichier

advagg_validator/advagg_validator.install

View source
<?php


/**
 * @file
 * Handles AdvAgg Validator installation and upgrade tasks.
 */

/**
 * @addtogroup hooks
 * @{
 */

/**
 * Implements hook_install().
 */
function advagg_validator_install() {
    module_load_include('install', 'advagg', 'advagg');
    $tables = array(
        'advagg_validator' => array(
            'filename_hash',
            'content_hash',
        ),
    );
    $schema = advagg_validator_schema();
    foreach ($tables as $table => $fields) {
        // Change utf8_bin to ascii_bin.
        advagg_install_change_table_collation($table, $fields, 'ascii_bin', $schema[$table]['fields']);
    }
}

/**
 * Implements hook_requirements().
 */
function advagg_validator_requirements($phase) {
    $requirements = array();
    // Ensure translations don't break at install time.
    $t = get_t();
    // If not at runtime, return here.
    if ($phase !== 'runtime') {
        return $requirements;
    }
    // Check version.
    $module_name = 'advagg_validator';
    $lib_name = 'csslint';
    list($description, $info) = advagg_get_version_description($lib_name, $module_name, TRUE);
    if (!empty($description)) {
        $requirements["{$module_name}_{$lib_name}_updates"] = array(
            'title' => $t('@module_name', array(
                '@module_name' => $info['name'],
            )),
            'severity' => REQUIREMENT_WARNING,
            'value' => $t('The @name library needs to be updated.', array(
                '@name' => $lib_name,
            )),
            'description' => $description,
        );
    }
    $lib_name = 'jshint';
    list($description, $info) = advagg_get_version_description($lib_name, $module_name, TRUE);
    if (!empty($description)) {
        $requirements["{$module_name}_{$lib_name}_updates"] = array(
            'title' => $t('@module_name', array(
                '@module_name' => $info['name'],
            )),
            'severity' => REQUIREMENT_WARNING,
            'value' => $t('The @name library needs to be updated.', array(
                '@name' => $lib_name,
            )),
            'description' => $description,
        );
    }
    return $requirements;
}

/**
 * Implements hook_schema().
 */
function advagg_validator_schema() {
    // Create database table.
    $schema['advagg_validator'] = array(
        'description' => 'CSS/JS files that have been ran through a validator, checking syntax etc.',
        'fields' => array(
            'filename' => array(
                'description' => 'Path and filename of the file relative to Drupal webroot.',
                'type' => 'text',
                'size' => 'normal',
                'not null' => TRUE,
            ),
            'filename_hash' => array(
                'description' => 'Hash of path and filename. Used to join tables and for lookup.',
                'type' => 'char',
                'length' => 43,
                'not null' => TRUE,
                'default' => '',
                'binary' => TRUE,
                'collation' => 'ascii_bin',
                'charset' => 'ascii',
            ),
            'content_hash' => array(
                'description' => 'Hash of the file content. Used to see if the file has changed.',
                'type' => 'char',
                'length' => 43,
                'not null' => TRUE,
                'default' => '',
                'binary' => TRUE,
                'collation' => 'ascii_bin',
                'charset' => 'ascii',
            ),
            'filetype' => array(
                'description' => 'Filetype.',
                'type' => 'varchar',
                'length' => 8,
                'not null' => TRUE,
                'default' => '',
                'binary' => TRUE,
            ),
            'data' => array(
                'description' => 'Validation data about this file.',
                'type' => 'blob',
                'not null' => TRUE,
                'size' => 'big',
                'translatable' => TRUE,
                'serialize' => TRUE,
            ),
        ),
        'indexes' => array(
            'content_hash' => array(
                'content_hash',
            ),
            'filetype' => array(
                'filetype',
            ),
        ),
        'primary key' => array(
            'filename_hash',
        ),
    );
    return $schema;
}

/**
 * Update the schema making the varchar columns utf8_bin in MySQL.
 */
function advagg_validator_update_7201(&$sandbox) {
    $db_type = Database::getConnection()->databaseType();
    $tables_altered = array();
    if ($db_type === 'mysql') {
        $results = db_query("SHOW FULL FIELDS FROM {advagg_validator}")->fetchAllAssoc('Field');
        foreach ($results as $row) {
            if (stripos($row->Type, 'varchar') !== FALSE && $row->Collation !== 'utf8_bin') {
                db_query("ALTER TABLE {advagg_validator} MODIFY {$row->Field} {$row->Type} CHARACTER SET utf8 COLLATE utf8_bin");
                $tables_altered['advagg_validator'][] = $row->Field;
            }
        }
    }
    if (empty($tables_altered)) {
        return t('Nothing needed to happen in AdvAgg Validator.');
    }
    return t('The following columns inside of these database tables where converted to utf8_bin: <br />@data', array(
        '@data' => print_r($tables_altered, TRUE),
    ));
}

/**
 * Update schema making the varchar columns char. Change utf8_bin to ascii_bin.
 */
function advagg_validator_update_7202(&$sandbox) {
    module_load_include('install', 'advagg', 'advagg');
    $tables = array(
        'advagg_validator' => array(
            'filename_hash',
            'content_hash',
        ),
    );
    $schema = advagg_validator_schema();
    foreach ($tables as $table => $fields) {
        foreach ($fields as $field) {
            // Change varchar to char.
            db_change_field($table, $field, $field, $schema[$table]['fields'][$field]);
        }
        // Change utf8_bin to ascii_bin.
        advagg_install_change_table_collation($table, $fields, 'ascii_bin', $schema[$table]['fields']);
    }
}

/**
 * @} End of "addtogroup hooks".
 */

Functions

Titre Deprecated Résumé
advagg_validator_install Implements hook_install().
advagg_validator_requirements Implements hook_requirements().
advagg_validator_schema Implements hook_schema().
advagg_validator_update_7201 Update the schema making the varchar columns utf8_bin in MySQL.
advagg_validator_update_7202 Update schema making the varchar columns char. Change utf8_bin to ascii_bin.