[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/comment_upload/ -> comment_upload.install (source)

   1  <?php
   2  // $Id: comment_upload.install,v 1.4.2.10 2009/02/01 20:06:40 netaustin Exp $
   3  
   4  
   5  /**
   6   * @file
   7   * The install file that defines the tables in use by comment_upload.
   8   *
   9   * Written by Heine Deelstra. Copyright (c) 2008 by Ustima (http://ustima.com). All rights reserved.
  10   *
  11   * This module is licensed under GPL v2. See LICENSE.txt for more information.
  12   */
  13  
  14  /**
  15   * Implementation of hook_install().
  16   */
  17  function comment_upload_install() {
  18    drupal_install_schema('comment_upload');
  19  }
  20  
  21  /**
  22   * Implementation of hook_uninstall().
  23   */
  24  function comment_upload_uninstall() {
  25    drupal_uninstall_schema('comment_upload');
  26    db_query("DELETE FROM {variable} WHERE name LIKE 'comment_upload_%'");
  27  }
  28  
  29  /**
  30   * Implementation of hook_schema().
  31   */
  32  function comment_upload_schema() {
  33    $schema['comment_upload'] = array(
  34      'description' => t('Stores uploaded file information and table associations.'),
  35      'fields' => array(
  36        'fid' => array(
  37          'type' => 'int',
  38          'unsigned' => TRUE,
  39          'not null' => TRUE,
  40          'default' => 0,
  41          'description' => t('Primary Key: The {files}.fid.'),
  42        ),
  43        'nid' => array(
  44          'type' => 'int',
  45          'unsigned' => TRUE,
  46          'not null' => TRUE,
  47          'default' => 0,
  48          'description' => t('The {node}.nid of the comment the uploaded files is associated with.'),
  49        ),
  50        'cid' => array(
  51          'type' => 'int',
  52          'unsigned' => TRUE,
  53          'not null' => TRUE,
  54          'default' => 0,
  55          'description' => t('The {comment}.cid associated with the uploaded file.'),
  56        ),
  57        'description' => array(
  58          'type' => 'varchar',
  59          'length' => 255,
  60          'not null' => TRUE,
  61          'default' => '',
  62          'description' => t('Description of the uploaded file.'),
  63        ),
  64        'list' => array(
  65          'type' => 'int',
  66          'unsigned' => TRUE,
  67          'not null' => TRUE,
  68          'default' => 0,
  69          'size' => 'tiny',
  70          'description' => t('Whether the file should be visibly listed on the comment: yes(1) or no(0).'),
  71        ),
  72        'weight' => array(
  73          'type' => 'int',
  74          'not null' => TRUE,
  75          'default' => 0,
  76          'size' => 'tiny',
  77          'description' => t('Weight of this upload in relation to other uploads - determines the order.'),
  78        ),
  79        'legacy_fid' => array(
  80          'type' => 'int',
  81          'unsigned' => TRUE,
  82          'not null' => TRUE,
  83          'default' => 0,
  84          'description' => t('The file ID from the Drupal 5 version, if applicable.'),
  85        ),
  86      ),
  87      'primary key' => array('fid'),
  88      'indexes' => array(
  89        'cid_fid' => array('cid', 'fid'),
  90        'nid' => array('nid'),
  91      ),
  92    );
  93  
  94    return $schema;
  95  }
  96  
  97  // FIXME: Update from comment_upload 5.x
  98  function comment_upload_update_1() {
  99    $ret = array();
 100    $ret[] = update_sql("ALTER TABLE {comment_files} ADD nid int NOT NULL default '0'");
 101  
 102    // loop through all the comment upload records and populate the nid column
 103    $results = db_query("SELECT c.cid, c.nid FROM {comments} c WHERE c.cid IN (SELECT DISTINCT cf.cid FROM {comment_files} cf)");
 104    while ($c = db_fetch_object($results)) {
 105      $ret[] = update_sql("UPDATE {comment_files} SET nid = $c->nid WHERE cid = $c->cid");
 106    }
 107    return $ret;
 108  }
 109  
 110  /**
 111   * Move previously saved data from {files}, {file_revisions} and {comment_files}
 112   * into {comment_upload_files}.
 113   *
 114   * Implementation of hook_update_N().
 115   */
 116  function comment_upload_update_2() {
 117    $ret = array();
 118    $ret[] = update_sql("CREATE TABLE {comment_upload_files} (
 119            `fid` int(10) unsigned NOT NULL default '0',
 120            `nid` int(10) unsigned NOT NULL default '0',
 121            `cid` int NOT NULL default '0',
 122            `filename` varchar(255) NOT NULL default '',
 123            `filepath` varchar(255) NOT NULL default '',
 124            `filemime` varchar(255) NOT NULL default '',
 125            `filesize` int(10) unsigned NOT NULL default '0',
 126             description varchar(255) NOT NULL default '',
 127             list tinyint(1) unsigned NOT NULL default 0,
 128             PRIMARY KEY (`fid`)
 129          ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
 130  
 131    $max_fid = db_result(db_query("SELECT MAX(fid) FROM {comment_files}"));
 132    $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");
 133  
 134    while ($c = db_fetch_object($results)) {
 135      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);
 136      db_query("DELETE FROM {files} WHERE fid = %d", $c->fid);
 137      db_query("DELETE FROM {file_revisions} WHERE fid = %d", $c->fid);
 138    }
 139    if ($max_fid) {
 140      $ret[] = update_sql("INSERT INTO {sequences} (name, id) VALUES('". db_prefix_tables('{comment_upload_files}') ."_fid', $max_fid)");
 141    }
 142    $ret[] = update_sql("DROP TABLE {comment_files}");
 143    return $ret;
 144  }
 145  
 146  /**
 147   * Update column cid to be an unsigned int (MySQL only).
 148   * Add index on nid.
 149   *
 150   * Implementation of hook_update_N().
 151   */
 152  function comment_upload_update_3() {
 153    $ret = array();
 154    switch ($GLOBALS['db_type']) {
 155      case 'mysql':
 156      case 'mysqli':
 157        $ret[] = update_sql("ALTER TABLE {comment_upload_files} CHANGE COLUMN cid cid int unsigned NOT NULL default '0'");
 158        $ret[] = update_sql("ALTER TABLE {comment_upload_files} ADD INDEX(nid)");
 159      break;
 160    }
 161    return $ret;
 162  }
 163  
 164  /**
 165   * Move all data in {comment_upload_files} to {files} and {comment_upload}.
 166   *
 167   * Implementation of hook_update_N().
 168   */
 169  function comment_upload_update_6000() {
 170    global $db_url;
 171    $schema = array();
 172    $schema['comment_upload'] = array(
 173      'description' => t('Stores uploaded file information and table associations.'),
 174      'fields' => array(
 175        'fid' => array(
 176          'type' => 'int',
 177          'unsigned' => TRUE,
 178          'not null' => TRUE,
 179          'default' => 0,
 180          'description' => t('Primary Key: The {files}.fid.'),
 181        ),
 182        'nid' => array(
 183          'type' => 'int',
 184          'unsigned' => TRUE,
 185          'not null' => TRUE,
 186          'default' => 0,
 187          'description' => t('The {node}.nid of the comment the uploaded files is associated with.'),
 188        ),
 189        'cid' => array(
 190          'type' => 'int',
 191          'unsigned' => TRUE,
 192          'not null' => TRUE,
 193          'default' => 0,
 194          'description' => t('The {comment}.cid associated with the uploaded file.'),
 195        ),
 196        'description' => array(
 197          'type' => 'varchar',
 198          'length' => 255,
 199          'not null' => TRUE,
 200          'default' => '',
 201          'description' => t('Description of the uploaded file.'),
 202        ),
 203        'list' => array(
 204          'type' => 'int',
 205          'unsigned' => TRUE,
 206          'not null' => TRUE,
 207          'default' => 0,
 208          'size' => 'tiny',
 209          'description' => t('Whether the file should be visibly listed on the comment: yes(1) or no(0).'),
 210        ),
 211        'weight' => array(
 212          'type' => 'int',
 213          'not null' => TRUE,
 214          'default' => 0,
 215          'size' => 'tiny',
 216          'description' => t('Weight of this upload in relation to other uploads - determines the order.'),
 217        ),
 218        'legacy_fid' => array(
 219          'type' => 'int',
 220          'unsigned' => TRUE,
 221          'not null' => TRUE,
 222          'default' => 0,
 223          'description' => t('The file ID from the Drupal 5 version, if applicable.'),
 224        ),
 225      ),
 226      'primary key' => array('fid'),
 227      'indexes' => array(
 228        'cid_fid' => array('cid', 'fid'),
 229        'nid' => array('nid'),
 230      ),
 231    );
 232  
 233  
 234    $ret = array();
 235  
 236    db_create_table($ret, 'comment_upload', $schema['comment_upload']);
 237    if (substr($db_url, 0, 5) == 'mysql') {
 238      $max_file_id = db_result(db_query("SELECT MAX(fid) FROM files"));
 239      $offset = $max_file_id + 1;
 240      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);
 241      $max_file_id = db_result(db_query("SELECT MAX(fid) FROM {files}"));
 242      db_query("ALTER TABLE {files} AUTO_INCREMENT = %d", $max_file_id + 1);
 243      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);
 244    }
 245  
 246    else {
 247      $result = db_query("SELECT cuf.*, c.uid FROM {comment_upload_files} cuf INNER JOIN {comments} c ON cuf.cid = c.cid");
 248      while ($file = db_fetch_object($result)) {
 249        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);
 250        $fid = db_last_insert_id('files', 'fid');
 251        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);
 252      }
 253    }
 254  
 255    db_drop_table($ret, 'comment_upload_files');
 256    return $ret;
 257  }
 258  
 259  
 260  function comment_upload_update_6002() {
 261    $ret = array();
 262    $ret[] = update_sql("UPDATE {system} SET weight = 2 WHERE name = 'comment_upload'");
 263    return $ret;
 264  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7