summaryrefslogtreecommitdiff
path: root/modules/upload/upload.install
blob: 26924f050bfbf1e0423b55370ed5f9c20f0f6dbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
// $Id$

/**
 * @file
 * Install, update and uninstall functions for the upload module.
 */

/**
 * @file
 * This is the install file for the upload module.
 */

/**
 * Implement hook_schema().
 */
function upload_schema() {
  $schema['upload'] = array(
    'description' => 'Stores uploaded file information and table associations.',
    'fields' => array(
      'fid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Primary Key: The {file}.fid.',
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {node}.nid associated with the uploaded file.',
      ),
      'vid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Primary Key: The {node}.vid associated with the uploaded file.',
      ),
      'description' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Description of the uploaded file.',
        'translatable' => TRUE,
      ),
      'list' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Whether the file should be visibly listed on the node: yes(1) or no(0).',
      ),
      'weight' => array(
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
        'description' => 'Weight of this upload in relation to other uploads in this node.',
      ),
    ),
    'primary key' => array('vid', 'fid'),
    'foreign keys' => array(
      'fid' => array('files' => 'fid'),
      'nid' => array('node' => 'nid'),
      'vid' => array('node' => 'vid'),
    ),
    'indexes' => array(
      'fid' => array('fid'),
      'nid' => array('nid'),
    ),
  );

  return $schema;
}


/**
 * Migrate upload module files from {files} to {file}.
 */
function upload_update_7000(&$sandbox) {

  /*
    TODO: Fix the updates. This is broken. See http://drupal.org/node/329301#comment-1404336
    Also note new DB structure http://drupal.org/node/227232#comment-1683976
  */

  if (!isset($sandbox['progress'])) {
    // Initialize batch update information.
    $sandbox['progress'] = 0;
    $sandbox['last_fid_processed'] = -1;
    $sandbox['max'] = db_query("SELECT COUNT(DISTINCT u.fid) FROM {upload} u")->fetchField();
  }

  // As a batch operation move records from {files} into the {file} table.
  $limit = 500;
  $result = db_query_range("SELECT DISTINCT u.fid FROM {upload} u ORDER BY u.vid", array(), 0, $limit);
  foreach ($result as $record) {
    $old_file = db_query('SELECT f.* FROM {files} f WHERE f.fid = :fid', array(':fid' => $record->fid))->fetch(PDO::FETCH_OBJ);
    if (!$old_file) {
      continue;
    }

    $new_file = db_query('SELECT f.* FROM {files} f WHERE f.filepath = :filepath', array(':filepath' => $old_file->uri))->fetch(PDO::FETCH_OBJ);
    if (!$new_file) {
      // Re-save the file into the new {file} table.
      $new_file = clone $old_file;
      drupal_write_record('file', $new_file);
    }

    // If the fid has changed we need to update the {upload} record to use the
    // new id.
    if (!empty($new_file->fid) && ($new_file->fid != $old_file->fid)) {
      db_update('upload')
        ->fields(array('fid' => $new_file->fid))
        ->condition('fid', $old_file->fid)
        ->execute();
    }

    // Update our progress information for the batch update.
    $sandbox['progress']++;
    $sandbox['last_fid_processed'] = $old_file->fid;
  }

  // Indicate our current progress to the batch update system. If there's no
  // max value then there's nothing to update and we're finished.
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
}