You are here

themekey.install in ThemeKey 6.3

Database schema of

@author Markus Kalkbrenner | Cocomore AG

@author profix898

File

themekey.install
View source
<?php

/**
 * @file
 * Database schema of
 * @see themekey.module
 *
 * @author Markus Kalkbrenner | Cocomore AG
 *   @see http://drupal.org/user/124705
 *
 * @author profix898
 *   @see http://drupal.org/user/35192
 */

/**
 * Implements hook_schema().
 */
function themekey_schema() {
  $schema = array();
  $schema['themekey_properties'] = array(
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'property' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'operator' => array(
        'type' => 'varchar',
        'length' => 2,
        'not null' => TRUE,
        'default' => '=',
      ),
      'value' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'theme' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'enabled' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'wildcards' => array(
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'big',
        'serialize' => TRUE,
      ),
      'parent' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'id',
    ),
    'indexes' => array(
      'enabled_parent_weight' => array(
        'enabled',
        'parent',
        'weight',
      ),
      'parent_weight' => array(
        'parent',
        'weight',
      ),
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function themekey_install() {
  drupal_install_schema('themekey');
}

/**
 * Implements hook_uninstall().
 */
function themekey_uninstall() {

  // Drop tables
  drupal_uninstall_schema('themekey');

  // Remove variables
  db_query("DELETE FROM {variable} WHERE name LIKE 'themekey_%%'");
  cache_clear_all('variables', 'cache');
}

/**
 * Implements hook_update_N().
 *
 * Update property 'nid' to 'node:nid'
 */
function themekey_update_6001() {
  $ret = array();

  //
  $result = db_query('SELECT * FROM {themekey_properties} WHERE property = \'nid\'');
  while ($item = db_fetch_array($result)) {
    if (db_result(db_query('SELECT COUNT(id) FROM {themekey_properties} WHERE property = \'node:nid\' AND value = \'%s\'', $item['value'])) > 0) {
      $ret[] = update_sql('DELETE FROM {themekey_properties} WHERE id = ' . $item['id']);
    }
    else {
      $ret[] = update_sql('UPDATE {themekey_properties} SET property = \'node:nid\' WHERE id = ' . $item['id']);
    }
  }
  return $ret;
}

/**
 * Implements hook_update_N().
 */
function themekey_update_6100() {
  $properties = variable_get('themekey_properties', array());
  foreach ($properties as $key => &$property) {
    if (array_key_exists('path', $property) && $key === $property['path']) {
      $property['path'] = FALSE;
    }
  }
  variable_set('themekey_properties', $properties);
  return array();
}

/**
 * Implements hook_update_N().
 */
function themekey_update_6101() {
  $result = update_sql("UPDATE {system} SET weight = 0 WHERE name = 'themekey'");
  return array(
    $result,
  );
}

/**
 * Implements hook_update_N().
 */
function themekey_update_6102() {
  if (module_exists('forum')) {
    variable_set('themekey_module_forum_triggers_taxonomy_vid', 1);
  }
  if (module_exists('taxonomy_menu')) {
    variable_set('themekey_module_taxonomy_menu_triggers_taxonomy_tid', 1);
  }
  return array();
}

/**
 * Implements hook_update_N().
 */
function themekey_update_6103() {
  variable_del('themekey_nodediscover');
  return array();
}

/**
 * Implements hook_update_N().
 */
function themekey_update_6104() {
  $result = update_sql("ALTER TABLE {themekey_properties} DROP COLUMN callbacks");
  return array(
    $result,
  );
}

/**
 * Implements hook_update_N().
 */
function themekey_update_6105() {
  global $db_type;
  $return = array();

  // we need to handle upgrade of module ThemeKey UI here because
  // it will fail when triggered at themekey_ui.install after
  // ThemeKey upgrade from 6.x-1.1 to 6.x.2.0
  if (module_exists('themekey_ui')) {
    $schema_version = drupal_get_installed_schema_version('themekey_ui');
    if (6100 > $schema_version) {
      $return = drupal_install_schema('themekey_ui');
      if (!variable_get('themekey_nodeaspath', 0)) {
        $sql = '';
        if (0 === strpos($db_type, 'mysql')) {
          $sql = "SELECT id, value, theme, nid, vid FROM {themekey_properties} JOIN {node_revisions} ON (value = nid) WHERE property = 'node:nid' AND conditions = '%s'";
        }
        elseif (0 === strpos($db_type, 'pqsql')) {
          $sql = "SELECT id, value, theme, nid, vid FROM {themekey_properties} JOIN {node_revisions} ON (value = nid::character varying) WHERE property = 'node:nid' AND conditions = '%s'";
        }
        if ($result = db_query($sql, 'a:0:{}')) {
          $insert_sql = "INSERT INTO {themekey_ui_node_theme} (nid, vid, theme) VALUES (%d, %d, '%s')";
          $delete_sql = "DELETE FROM {themekey_properties} WHERE id = %d";
          while ($row = db_fetch_array($result)) {
            if ($return['INSERT']['success'] = db_query($insert_sql, $row['nid'], $row['vid'], $row['theme'])) {
              if (!($return['DELETE']['success'] = db_query($delete_sql, $row['id']))) {
                break;
              }
            }
            else {
              $return['INSERT']['success'] = FALSE;
              break;
            }
          }
          if (isset($return['INSERT'])) {
            $return['INSERT']['query'] = check_plain($insert_sql);
          }
          if (isset($return['DELETE'])) {
            $return['DELETE']['query'] = check_plain($delete_sql);
          }
        }
        else {
          $return['SELECT'] = array(
            'success' => FALSE,
            'query' => check_plain($sql),
          );
        }
      }
      variable_del('themekey_nodeaspath');
    }
  }
  return $return;
}

/**
 * Function _themekey_properties_explode_conditions()
 * converts conditions formatted as string into an array.
 * It was part of themekey_build.inc up to version 6.x-1.2.
 * Now it's only required one more time to perform
 * themekey_update_6200()
 *
 * @param $conditions
 *   ThemeKey conditions as string
 *
 * @return
 *   ThemeKey conditions as array
 */
function _themekey_properties_explode_conditions($conditions) {
  if (!is_array($conditions)) {
    $parts = array_filter(explode(';', $conditions));
    $conditions = array();
    foreach ($parts as $part) {
      $part = trim($part);
      if (preg_match('/(.*?)([<>=!~])(.*)/', $part, $matches)) {
        $conditions[] = array(
          'property' => trim($matches[1]),
          'operator' => trim($matches[2]),
          'value' => trim($matches[3]),
        );
      }
    }
  }
  return $conditions;
}

/**
 * Implements hook_update_N().
 */
function themekey_update_6200() {
  $field = array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => '0',
    'initial' => '1',
  );
  db_add_field($ret, 'themekey_properties', 'enabled', $field);
  db_drop_index($ret, 'themekey_properties', 'property');
  db_add_index($ret, 'themekey_properties', 'enabled', array(
    'enabled',
  ));
  $field = array(
    'type' => 'text',
    'not null' => TRUE,
  );
  db_add_field($ret, 'themekey_properties', 'wildcards', $field);
  $weight = (int) db_result(db_query("SELECT MIN(weight) FROM {themekey_properties}")) - 1;
  $insert_query = "INSERT INTO {themekey_properties} (property, value, weight, conditions, theme, enabled, wildcards) VALUES ('drupal:path', '%s', %d, '%s', '%s', 1, '%s')";
  $result = db_query("SELECT * FROM {themekey_paths} WHERE custom = 1");
  while ($item = db_fetch_array($result)) {
    $conditions = unserialize($item['conditions']);
    if (is_array($conditions) && !empty($conditions)) {
      if (!is_array($conditions[0])) {

        // ThemeKey 6.x-1.1 stored conditions for paths as simple string within an array
        $conditions = _themekey_properties_explode_conditions($conditions[0]);
      }
    }
    $insert_success = db_query($insert_query, $item['path'], $weight, serialize($conditions), $item['theme'], $item['wildcards']);
    $ret[] = array(
      'success' => $insert_success,
      'query' => $insert_query,
    );
    $ret[] = update_sql('DELETE FROM {themekey_paths} WHERE id = ' . $item['id']);
  }
  db_drop_field($ret, 'themekey_paths', 'conditions');
  db_drop_field($ret, 'themekey_paths', 'custom');
  db_drop_field($ret, 'themekey_paths', 'theme');
  return $ret;
}

/**
 * Implements hook_update_N().
 */
function themekey_update_6201() {

  // Don't rebuild anymore when themekey_update_6202() will run

  //themekey_rebuild();
  return array();
}

/**
 * Implements hook_update_N().
 */
function themekey_update_6202() {
  $ret = array();
  $field_operator = array(
    'type' => 'varchar',
    'length' => 2,
    'not null' => TRUE,
    'default' => '=',
    'initial' => '=',
  );
  db_add_field($ret, 'themekey_properties', 'operator', $field_operator);
  $field_parent = array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
    'initial' => 0,
  );
  db_add_field($ret, 'themekey_properties', 'parent', $field_parent);
  if ($result = db_query("SELECT * FROM {themekey_properties} WHERE conditions <> '%s'", serialize(array()))) {
    $insert_query = "INSERT INTO {themekey_properties} (property, operator, value, weight, theme, enabled, wildcards, parent) VALUES ('%s', '%s', '%s', %d, '%s', %d, '%s', %d)";
    while ($row = db_fetch_array($result)) {
      $conditions = unserialize($row['conditions']);
      if (is_array($conditions)) {
        $parent = $row['id'];
        foreach ($conditions as $condition) {
          $insert_result = db_query($insert_query, $condition['property'], $condition['operator'], $condition['value'], 0, $row['theme'], $row['enabled'], serialize(array()), $parent);
          if ($insert_result) {
            $parent = db_last_insert_id('themekey_properties', 'id');
          }
          $ret[] = array(
            'success' => $insert_result,
            'query' => $insert_query,
          );
        }
      }
    }
  }
  db_drop_field($ret, 'themekey_properties', 'conditions');
  db_drop_index($ret, 'themekey_properties', 'enabled');
  db_drop_index($ret, 'themekey_properties', 'weight');
  db_add_index($ret, 'themekey_properties', 'enabled_parent_weight', array(
    'enabled',
    'parent',
    'weight',
  ));
  db_add_index($ret, 'themekey_properties', 'parent_weight', array(
    'parent',
    'weight',
  ));
  db_drop_index($ret, 'themekey_paths', 'path');
  db_drop_index($ret, 'themekey_paths', 'fit');
  db_drop_index($ret, 'themekey_paths', 'weight');
  db_add_index($ret, 'themekey_paths', 'path_fit_weight', array(
    'path',
    'fit',
    'weight',
  ));
  return $ret;
}

/**
 * Implements hook_update_N().
 */
function themekey_update_6203() {

  // moved themekey_rebuild() to themekey_update_6300()
  return array();
}

/**
 * Implements hook_update_N().
 */
function themekey_update_6300() {

  // moved themekey_rebuild() to themekey_update_6301()
  $ret = array();
  db_drop_table($ret, 'themekey_paths');
  return $ret;
}

/**
 * Implements hook_update_N().
 */
function themekey_update_6302() {
  module_load_include('inc', 'themekey', 'themekey_base');
  module_load_include('inc', 'themekey', 'themekey_build');
  themekey_rebuild();
  return array();
}

Functions

Namesort descending Description
themekey_install Implements hook_install().
themekey_schema Implements hook_schema().
themekey_uninstall Implements hook_uninstall().
themekey_update_6001 Implements hook_update_N().
themekey_update_6100 Implements hook_update_N().
themekey_update_6101 Implements hook_update_N().
themekey_update_6102 Implements hook_update_N().
themekey_update_6103 Implements hook_update_N().
themekey_update_6104 Implements hook_update_N().
themekey_update_6105 Implements hook_update_N().
themekey_update_6200 Implements hook_update_N().
themekey_update_6201 Implements hook_update_N().
themekey_update_6202 Implements hook_update_N().
themekey_update_6203 Implements hook_update_N().
themekey_update_6300 Implements hook_update_N().
themekey_update_6302 Implements hook_update_N().
_themekey_properties_explode_conditions Function _themekey_properties_explode_conditions() converts conditions formatted as string into an array. It was part of themekey_build.inc up to version 6.x-1.2. Now it's only required one more time to perform themekey_update_6200()