You are here

flashnode.install in Flash Node 5.6

File

flashnode.install
View source
<?php

/**
 * Implementation of hook_install().
 *
 * flashnode used to be just flash, so we need to check if {flash} table already
 * exists when we install flashnode. If it does then we need to rename it,
 * otherwise we install the table as normal
 */
function flashnode_install() {
  if (db_table_exists('flash')) {

    // Migrating from an earlier version of flash
    _flashnode_migrate_from_flash();
  }
  else {

    // first install - create the table
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        db_query('
          CREATE TABLE {flashnode} (
            nid INT(10) UNSIGNED NOT NULL ,
            height INT(10) UNSIGNED NOT NULL ,
            width INT(10) UNSIGNED NOT NULL ,
            display TINYINT(3) UNSIGNED NOT NULL ,
            substitution LONGTEXT NOT NULL ,
            flashvars LONGTEXT NOT NULL ,
            base VARCHAR(255) DEFAULT NULL ,
            vid INT(10) UNSIGNED NOT NULL ,
            fid INT(10) UNSIGNED NOT NULL ,
            PRIMARY KEY (nid)
          ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;');
        break;
      case 'pgsql':
        db_query("\n          create table {flashnode} (\n            nid integer not null,\n            height integer not null,\n            width integer not null,\n            display integer not null,\n            substitution text not null default '',\n            flashvars text not null default '',\n            base VARCHAR(255) default NULL,\n            vid integer not null,\n            fid integer not null,\n            PRIMARY KEY (nid)\n          );");
        break;
    }
  }
  drupal_set_message(t('Flash node has been setup.'));
}

/**
 * Updates follow - these apply to the flashnode development path
 * The migrate from flash routine is separate!
 */

/**
 * Rename {flash} table to {flashnode} if an ealier version of flashnode was
 * used
 */
function flashnode_update_1() {
  if (db_table_exists('flash')) {
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        $ret[] = update_sql('RENAME TABLE {flash} TO {flashnode}');
        break;
      case 'pgsql':
        $ret[] = update_sql('ALTER TABLE {flash} RENAME TO {flashnode}');
        break;
    }
  }
  return $ret;
}

/**
 * Build and version no longer used with SWFTools - remove from table
 * Only apply this to MySQL as not all Postgres version support dropping of
 * columns
 */
function flashnode_update_2() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('ALTER TABLE {flashnode} DROP version');
      $ret[] = update_sql('ALTER TABLE {flashnode} DROP build');
      break;
    case 'pgsql':

      /*
      $ret[] = update_sql('ALTER TABLE {flashnode} DROP COLUMN version');
      $ret[] = update_sql('ALTER TABLE {flashnode} DROP COLUMN build');
      */
      break;
  }
  return $ret;
}

/**
 * Update filter table - rename flash to flashnode
 */
function flashnode_update_3() {
  $ret[] = update_sql("UPDATE {filters} SET module='flashnode' WHERE module='flash'");
  return $ret;
}

/**
 * Update node entries - replace [flash] macros with [flashnode]
 * Note - check for [flash|nid=] to avoid clashing with flash_filter which is a
 * separate module!
 */
function flashnode_update_4() {

  // Initialise the return array
  $ret = array();

  // Get all existing nodes that contain flashnode macros
  $result = db_query("SELECT nid, vid, body, teaser FROM {node_revisions} WHERE body LIKE '%[flash|nid=%' OR teaser LIKE '%[flash|nid=%'");

  // Iterate through the results
  while ($node = db_fetch_object($result)) {

    // Amend the body and teaser
    $node->body = str_replace('[flash|nid=', '[flashnode|nid=', $node->body);
    $node->teaser = str_replace('[flash|nid=', '[flashnode|nid=', $node->teaser);

    // Update the database with the changes
    if (db_query("UPDATE {node_revisions} SET body='%s', teaser='%s' WHERE vid=%d", $node->body, $node->teaser, $node->vid)) {
      $ret[] = array(
        'success' => 1,
        'query' => 'Updated filter in node ' . $node->nid,
      );
    }
    else {
      $ret[] = array(
        'success' => 0,
        'query' => 'Failed to update filter in node ' . $node->nid,
      );
    }
  }

  // Reset the cache to ensure any pages using filters are updated
  cache_clear_all('*', 'cache_filter', true);
  return $ret;
}

/**
 * Update files table - rename _flash filename to _flashnode for consistency
 */
function flashnode_update_5() {
  $ret[] = update_sql("UPDATE {files} SET filename='_flashnode' WHERE filename='_flash'");
  return $ret;
}

/**
 * Add substitution column as flashnode 5.2 makes this user-definable on a per
 * node basis
 */
function flashnode_update_6() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {flashnode} ADD substitution longtext NOT NULL");
      break;
    case 'pgsql':
      db_add_column($ret, 'flashnode', 'substitution', 'text', array(
        'default' => '',
        'not null' => TRUE,
      ));
      break;
  }
  return $ret;
}

/**
 * Add flashvars and base columns as flashnode 5.2 makes these user-definable
 * on a per node basis
 */
function flashnode_update_7() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {flashnode} ADD flashvars longtext NOT NULL");
      $ret[] = update_sql("ALTER TABLE {flashnode} ADD base VARCHAR(255) NOT NULL");
      break;
    case 'pgsql':
      db_add_column($ret, 'flashnode', 'flashvars', 'text', array(
        'default' => '',
        'not null' => TRUE,
      ));
      db_add_column($ret, 'flashnode', 'base', 'varchar(255)', array(
        'default' => '',
        'not null' => TRUE,
      ));
      break;
  }
  return $ret;
}

/**
 * Set substitution column value to !default
 */
function flashnode_update_8() {
  $ret[] = update_sql("UPDATE {flashnode} SET substitution='!default'");
  return $ret;
}

/**
 * Add two additional columns in preparation for Drupal 6
 * Need to add fid and vid
 * Set vid = nid, and set fid based on the entries in {files}
 * In Drupal 6 nid will be dropped, so we need fid as of now!
 */
function flashnode_update_9() {

  // Add new columns
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {flashnode} ADD vid int NOT NULL default '0'");
      $ret[] = update_sql("ALTER TABLE {flashnode} ADD fid int NOT NULL default '0'");

      // Populate columns with data
      $ret[] = update_sql("UPDATE {flashnode} fn INNER JOIN {node} n ON fn.nid = n.nid SET fn.vid = n.vid");
      $ret[] = update_sql("UPDATE {flashnode} fn INNER JOIN {files} f ON fn.nid = f.nid SET fn.fid = f.fid");
      break;
    case 'pgsql':
      db_add_column($ret, 'flashnode', 'vid', 'int', array(
        'default' => 0,
        'not null' => TRUE,
      ));
      db_add_column($ret, 'flashnode', 'fid', 'int', array(
        'default' => 0,
        'not null' => TRUE,
      ));

      // Populate columns with data
      $ret[] = update_sql("UPDATE {flashnode} fn SET vid = n.vid FROM {node} n WHERE fn.nid = n.nid");
      $ret[] = update_sql("UPDATE {flashnode} fn SET fid = f.fid FROM {files} f WHERE fn.nid = f.nid");
      break;
  }
  return $ret;
}

/**
 * If flashnode is being installed in place of flash then to Drupal it looks
 * like the first time this module has been installed. In reality we have an
 * existing table we want to keep with our existing flash content, so we have to
 * simulate the update routines as part of installation.
 */
function _flashnode_migrate_from_flash() {

  /**
   * Do update to utf8, if required - at this stage there are no text fields to
   * convert so it will just change the default character set if required
   */
  _flashnode_update_utf8();

  /**
   * See if fid column exists, and drop it if it does. Not all versions of flash
   * included fid so it may already not exist! Not all Postgres can drop
   * columns, so omit dropping in that case
   */
  if (db_num_rows(db_query("SHOW COLUMNS FROM {%s} LIKE '%s'", 'flash', 'fid'))) {
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        db_query('ALTER TABLE {flash} DROP fid');
        break;
      case 'pgsql':

        //db_query('ALTER TABLE {flash} DROP COLUMN fid');
        break;
    }
  }

  /**
   * Drop version and build, unless Postgres, then rename table from {flash} to
   * {flashnode} to bring table name in line with node name
   */
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query('ALTER TABLE {flash} DROP version');
      db_query('ALTER TABLE {flash} DROP build');
      db_query('RENAME TABLE {flash} TO {flashnode}');
      break;
    case 'pgsql':

      //db_query('ALTER TABLE {flash} DROP COLUMN version');

      //db_query('ALTER TABLE {flash} DROP COLUMN build');
      db_query('ALTER TABLE {flash} RENAME TO {flashnode}');
      break;
  }

  // Update filters table
  db_query("UPDATE {filters} SET module='flashnode' WHERE module='flash'");

  // Get all existing nodes that contain macros
  $result = db_query("SELECT vid, body, teaser FROM {node_revisions} WHERE body LIKE '%[flash|nid=%' OR teaser LIKE '%[flash|nid=%'");

  // Iterate through the results
  while ($node = db_fetch_object($result)) {

    // Update body and teaser
    $node->body = str_replace('[flash|nid=', '[flashnode|nid=', $node->body);
    $node->teaser = str_replace('[flash|nid=', '[flashnode|nid=', $node->teaser);

    // Update the database
    db_query("UPDATE {node_revisions} SET body='%s', teaser='%s' WHERE vid=%d", $node->body, $node->teaser, $node->vid);
  }

  // Reset the cache to ensure any pages using filters are updated
  cache_clear_all('*', 'cache_filter', true);

  /**
   * Amend the variable table by deleting default version and build which are no
   * longer required, and change the name on the remaining variables to
   * flashnode_ to make consistent with module name
   */
  db_query("UPDATE {variable} SET name='flashnode_default_path' WHERE name='flash_default_path'");
  db_query("UPDATE {variable} SET name='flashnode_updated' WHERE name='flash_updated'");
  db_query("DELETE FROM {variable} WHERE name='flash_version'");
  db_query("DELETE FROM {variable} WHERE name='flash_build'");

  // Update files table - change filename from _flash to _flashnode
  db_query("UPDATE {files} SET filename='_flashnode' WHERE filename='_flash'");

  // Update node table - change type from flash to flashnode
  db_query("UPDATE {node} SET type='flashnode' WHERE type='flash'");

  // Add substitution and base column
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("ALTER TABLE {flashnode} ADD substitution longtext NOT NULL");
      db_query("ALTER TABLE {flashnode} ADD flashvars longtext NOT NULL");
      db_query("ALTER TABLE {flashnode} ADD base VARCHAR(255) NOT NULL");
      break;
    case 'pgsql':
      _flashnode_db_add_column($ret, 'flashnode', 'substitution', 'text', array(
        'default' => '',
        'not null' => TRUE,
      ));
      _flashnode_db_add_column($ret, 'flashnode', 'flashvars', 'text', array(
        'default' => '',
        'not null' => TRUE,
      ));
      _flashnode_db_add_column($ret, 'flashnode', 'base', 'varchar(255)', array(
        'default' => '',
        'not null' => TRUE,
      ));
      break;
  }

  // Set default value of substitution
  db_query("UPDATE {flashnode} SET substitution='!default'");

  // End of update routine
  drupal_set_message('Successfully migrated database from {flash} to {flashnode}.');
  return;
}

/**
 * Convert a 4.7 table to UTF-8 as part of installation as we won't get it via
 * update.php
 *
 * This code taken from install.inc and we have to repeat it here as it is not
 * available to us during a module install, only module update, but we might
 * need to migrate the table over
 */
function _flashnode_update_utf8() {
  switch ($GLOBALS['db_type']) {

    // Only for MySQL 4.1+
    case 'mysqli':
      break;
    case 'mysql':
      if (version_compare(mysql_get_server_info($GLOBALS['active_db']), '4.1.0', '<')) {
        return;
      }
      break;
    case 'pgsql':
      return;
  }

  // See if database uses UTF-8 already
  global $db_url;
  $url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
  $db_name = substr($url['path'], 1);
  $result = db_fetch_array(db_query('SHOW CREATE DATABASE `%s`', $db_name));
  if (preg_match('/utf8/i', array_pop($result))) {
    return;
  }
  db_query('ALTER TABLE {flash} DEFAULT CHARACTER SET utf8');
  return;
}

/**
 * Add a column to a database using syntax appropriate for PostgreSQL.
 * Save result of SQL commands in $ret array.
 *
 * This code is the same as update.php except we replace update_sql with
 * db_query. We have to repeat it here as during a module install we don't have
 * the function available to us.
 */
function _flashnode_db_add_column(&$ret, $table, $column, $type, $attributes = array()) {
  if (array_key_exists('not null', $attributes) and $attributes['not null']) {
    $not_null = 'NOT NULL';
  }
  if (array_key_exists('default', $attributes)) {
    if (is_null($attributes['default'])) {
      $default_val = 'NULL';
      $default = 'default NULL';
    }
    elseif ($attributes['default'] === FALSE) {
      $default = '';
    }
    else {
      $default_val = "{$attributes['default']}";
      $default = "default {$attributes['default']}";
    }
  }
  db_query("ALTER TABLE {" . $table . "} ADD {$column} {$type}");
  if ($default) {
    db_query("ALTER TABLE {" . $table . "} ALTER {$column} SET {$default}");
  }
  if ($not_null) {
    if ($default) {
      db_query("UPDATE {" . $table . "} SET {$column} = {$default_val}");
    }
    db_query("ALTER TABLE {" . $table . "} ALTER {$column} SET NOT NULL");
  }
}

Functions

Namesort descending Description
flashnode_install Implementation of hook_install().
flashnode_update_1 Rename {flash} table to {flashnode} if an ealier version of flashnode was used
flashnode_update_2 Build and version no longer used with SWFTools - remove from table Only apply this to MySQL as not all Postgres version support dropping of columns
flashnode_update_3 Update filter table - rename flash to flashnode
flashnode_update_4 Update node entries - replace [flash] macros with [flashnode] Note - check for [flash|nid=] to avoid clashing with flash_filter which is a separate module!
flashnode_update_5 Update files table - rename _flash filename to _flashnode for consistency
flashnode_update_6 Add substitution column as flashnode 5.2 makes this user-definable on a per node basis
flashnode_update_7 Add flashvars and base columns as flashnode 5.2 makes these user-definable on a per node basis
flashnode_update_8 Set substitution column value to !default
flashnode_update_9 Add two additional columns in preparation for Drupal 6 Need to add fid and vid Set vid = nid, and set fid based on the entries in {files} In Drupal 6 nid will be dropped, so we need fid as of now!
_flashnode_db_add_column Add a column to a database using syntax appropriate for PostgreSQL. Save result of SQL commands in $ret array.
_flashnode_migrate_from_flash If flashnode is being installed in place of flash then to Drupal it looks like the first time this module has been installed. In reality we have an existing table we want to keep with our existing flash content, so we have to simulate the update…
_flashnode_update_utf8 Convert a 4.7 table to UTF-8 as part of installation as we won't get it via update.php