You are here

comment_upload.install in Comment Upload 6

Same filename and directory in other branches
  1. 5 comment_upload.install

The install file that defines the tables in use by comment_upload.

Written by Heine Deelstra. Copyright (c) 2008 by Ustima (http://ustima.com). All rights reserved.

This module is licensed under GPL v2. See LICENSE.txt for more information.

File

comment_upload.install
View source
<?php

/**
 * @file
 * The install file that defines the tables in use by comment_upload.
 *
 * Written by Heine Deelstra. Copyright (c) 2008 by Ustima (http://ustima.com). All rights reserved.
 *
 * This module is licensed under GPL v2. See LICENSE.txt for more information.
 */

/**
 * Implementation of hook_install().
 */
function comment_upload_install() {
  drupal_install_schema('comment_upload');
}

/**
 * Implementation of hook_uninstall().
 */
function comment_upload_uninstall() {
  drupal_uninstall_schema('comment_upload');
  db_query("DELETE FROM {variable} WHERE name LIKE 'comment_upload_%'");
}

/**
 * Implementation of hook_schema().
 */
function comment_upload_schema() {
  $schema['comment_upload'] = array(
    'description' => t('Stores uploaded file information and table associations.'),
    'fields' => array(
      'fid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('Primary Key: The {files}.fid.'),
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The {node}.nid of the comment the uploaded files is associated with.'),
      ),
      'cid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The {comment}.cid associated with the uploaded file.'),
      ),
      'description' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => t('Description of the uploaded file.'),
      ),
      'list' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => t('Whether the file should be visibly listed on the comment: yes(1) or no(0).'),
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => t('Weight of this upload in relation to other uploads - determines the order.'),
      ),
      'legacy_fid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The file ID from the Drupal 5 version, if applicable.'),
      ),
    ),
    'primary key' => array(
      'fid',
    ),
    'indexes' => array(
      'cid_fid' => array(
        'cid',
        'fid',
      ),
      'nid' => array(
        'nid',
      ),
    ),
  );
  return $schema;
}

// FIXME: Update from comment_upload 5.x
function comment_upload_update_1() {
  $ret = array();
  $ret[] = update_sql("ALTER TABLE {comment_files} ADD nid int NOT NULL default '0'");

  // loop through all the comment upload records and populate the nid column
  $results = db_query("SELECT c.cid, c.nid FROM {comments} c WHERE c.cid IN (SELECT DISTINCT cf.cid FROM {comment_files} cf)");
  while ($c = db_fetch_object($results)) {
    $ret[] = update_sql("UPDATE {comment_files} SET nid = {$c->nid} WHERE cid = {$c->cid}");
  }
  return $ret;
}

/**
 * Move previously saved data from {files}, {file_revisions} and {comment_files}
 * into {comment_upload_files}.
 *
 * Implementation of hook_update_N().
 */
function comment_upload_update_2() {
  $ret = array();
  $ret[] = update_sql("CREATE TABLE {comment_upload_files} (\n          `fid` int(10) unsigned NOT NULL default '0',\n          `nid` int(10) unsigned NOT NULL default '0',\n          `cid` int NOT NULL default '0',\n          `filename` varchar(255) NOT NULL default '',\n          `filepath` varchar(255) NOT NULL default '',\n          `filemime` varchar(255) NOT NULL default '',\n          `filesize` int(10) unsigned NOT NULL default '0',\n           description varchar(255) NOT NULL default '',\n           list tinyint(1) unsigned NOT NULL default 0,\n           PRIMARY KEY (`fid`)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
  $max_fid = db_result(db_query("SELECT MAX(fid) FROM {comment_files}"));
  $results = db_query("SELECT f.fid, cf.nid, cf.cid, cf.cid, f.filename, f.filepath, f.filemime, f.filesize, r.list, r.description FROM {files} f INNER JOIN {file_revisions} r ON f.fid = r.fid INNER JOIN {comment_files} cf ON f.fid = cf.fid WHERE f.nid = 0");
  while ($c = db_fetch_object($results)) {
    db_query("INSERT INTO {comment_upload_files} (fid, nid, cid, filename, filepath, filemime, filesize, description, list) VALUES(%d, %d, %d, '%s', '%s', '%s', %d, '%s', %d)", $c->fid, $c->nid, $c->cid, $c->filename, $c->filepath, $c->filemime, $c->filesize, $c->description, $c->list);
    db_query("DELETE FROM {files} WHERE fid = %d", $c->fid);
    db_query("DELETE FROM {file_revisions} WHERE fid = %d", $c->fid);
  }
  if ($max_fid) {
    $ret[] = update_sql("INSERT INTO {sequences} (name, id) VALUES('" . db_prefix_tables('{comment_upload_files}') . "_fid', {$max_fid})");
  }
  $ret[] = update_sql("DROP TABLE {comment_files}");
  return $ret;
}

/**
 * Update column cid to be an unsigned int (MySQL only).
 * Add index on nid.
 *
 * Implementation of hook_update_N().
 */
function comment_upload_update_3() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {comment_upload_files} CHANGE COLUMN cid cid int unsigned NOT NULL default '0'");
      $ret[] = update_sql("ALTER TABLE {comment_upload_files} ADD INDEX(nid)");
      break;
  }
  return $ret;
}

/**
 * Move all data in {comment_upload_files} to {files} and {comment_upload}.
 *
 * Implementation of hook_update_N().
 */
function comment_upload_update_6000() {
  global $db_url;
  $schema = array();
  $schema['comment_upload'] = array(
    'description' => t('Stores uploaded file information and table associations.'),
    'fields' => array(
      'fid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('Primary Key: The {files}.fid.'),
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The {node}.nid of the comment the uploaded files is associated with.'),
      ),
      'cid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The {comment}.cid associated with the uploaded file.'),
      ),
      'description' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => t('Description of the uploaded file.'),
      ),
      'list' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => t('Whether the file should be visibly listed on the comment: yes(1) or no(0).'),
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => t('Weight of this upload in relation to other uploads - determines the order.'),
      ),
      'legacy_fid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => t('The file ID from the Drupal 5 version, if applicable.'),
      ),
    ),
    'primary key' => array(
      'fid',
    ),
    'indexes' => array(
      'cid_fid' => array(
        'cid',
        'fid',
      ),
      'nid' => array(
        'nid',
      ),
    ),
  );
  $ret = array();
  db_create_table($ret, 'comment_upload', $schema['comment_upload']);
  if (substr($db_url, 0, 5) == 'mysql') {
    $max_file_id = db_result(db_query("SELECT MAX(fid) FROM {files}"));
    $offset = $max_file_id + 1;
    db_query("INSERT INTO {files} (fid, uid, filename, filepath, filemime, filesize, status, timestamp) SELECT cuf.fid + %d, c.uid, cuf.filename, cuf.filepath, cuf.filemime, cuf.filesize, 1, c.timestamp FROM {comment_upload_files} cuf INNER JOIN {comments} c ON cuf.cid = c.cid", $offset);
    $max_file_id = db_result(db_query("SELECT MAX(fid) FROM {files}"));
    db_query("ALTER TABLE {files} AUTO_INCREMENT = %d", $max_file_id + 1);
    db_query("INSERT INTO {comment_upload} (fid, nid, cid, description, list, legacy_fid) SELECT cuf.fid + %d, cuf.nid, cuf.cid, cuf.description, cuf.list, cuf.fid FROM {comment_upload_files} cuf", $offset);
  }
  else {
    $result = db_query("SELECT cuf.*, c.uid FROM {comment_upload_files} cuf INNER JOIN {comments} c ON cuf.cid = c.cid");
    while ($file = db_fetch_object($result)) {
      db_query("INSERT INTO {files} (filename, filepath, filemime, filesize, uid, status) VALUES ('%s', '%s', '%s', %d, %d, %d)", $file->filename, $file->filepath, $file->filemime, $file->filesize, $file->uid, 1);
      $fid = db_last_insert_id('files', 'fid');
      db_query("INSERT INTO {comment_upload} (fid, nid, cid, description, list) VALUES (%d, %d, %d, '%s', %d)", $fid, $file->nid, $file->cid, $file->description, $file->list);
    }
  }
  db_drop_table($ret, 'comment_upload_files');
  return $ret;
}
function comment_upload_update_6002() {
  $ret = array();
  $ret[] = update_sql("UPDATE {system} SET weight = 2 WHERE name = 'comment_upload'");
  return $ret;
}

Functions

Namesort descending Description
comment_upload_install Implementation of hook_install().
comment_upload_schema Implementation of hook_schema().
comment_upload_uninstall Implementation of hook_uninstall().
comment_upload_update_1
comment_upload_update_2 Move previously saved data from {files}, {file_revisions} and {comment_files} into {comment_upload_files}.
comment_upload_update_3 Update column cid to be an unsigned int (MySQL only). Add index on nid.
comment_upload_update_6000 Move all data in {comment_upload_files} to {files} and {comment_upload}.
comment_upload_update_6002