node.install 18.6 KB
Newer Older
1
2
3
<?php
// $Id$

4
5
6
7
8
/**
 * @file
 * Install, update and uninstall functions for the node module.
 */

9
/**
10
 * Implement hook_schema().
11
12
13
 */
function node_schema() {
  $schema['node'] = array(
14
    'description' => 'The base table for nodes.',
15
    'fields' => array(
16
      'nid' => array(
17
        'description' => 'The primary identifier for a node.',
18
19
        'type' => 'serial',
        'unsigned' => TRUE,
20
21
        'not null' => TRUE,
      ),
22
      'vid' => array(
23
        'description' => 'The current {node_revision}.vid version identifier.',
24
25
26
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
27
28
        'default' => 0,
      ),
29
      'type' => array(
30
        'description' => 'The {node_type}.type of this node.',
31
32
33
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
34
35
        'default' => '',
      ),
36
      'language' => array(
37
        'description' => 'The {languages}.language of this node.',
38
39
40
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
41
42
        'default' => '',
      ),
43
      'title' => array(
44
        'description' => 'The title of this node, always treated as non-markup plain text.',
45
46
47
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
48
49
        'default' => '',
      ),
50
      'uid' => array(
51
        'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
52
53
        'type' => 'int',
        'not null' => TRUE,
54
55
        'default' => 0,
      ),
56
      'status' => array(
57
        'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
58
59
        'type' => 'int',
        'not null' => TRUE,
60
61
        'default' => 1,
      ),
62
      'created' => array(
63
        'description' => 'The Unix timestamp when the node was created.',
64
65
        'type' => 'int',
        'not null' => TRUE,
66
67
        'default' => 0,
      ),
68
      'changed' => array(
69
        'description' => 'The Unix timestamp when the node was most recently saved.',
70
71
        'type' => 'int',
        'not null' => TRUE,
72
73
        'default' => 0,
      ),
74
      'comment' => array(
75
        'description' => 'Whether comments are allowed on this node: 0 = no, 1 = closed (read only), 2 = open (read/write).',
76
77
        'type' => 'int',
        'not null' => TRUE,
78
79
        'default' => 0,
      ),
80
      'promote' => array(
81
        'description' => 'Boolean indicating whether the node should be displayed on the front page.',
82
83
        'type' => 'int',
        'not null' => TRUE,
84
85
        'default' => 0,
      ),
86
      'moderate' => array(
87
        'description' => 'Previously, a boolean indicating whether the node was "in moderation"; mostly no longer used.',
88
89
        'type' => 'int',
        'not null' => TRUE,
90
91
        'default' => 0,
      ),
92
      'sticky' => array(
93
        'description' => 'Boolean indicating whether the node should be displayed at the top of lists in which it appears.',
94
95
        'type' => 'int',
        'not null' => TRUE,
96
97
        'default' => 0,
      ),
98
      'tnid' => array(
99
        'description' => 'The translation set id for this node, which equals the node id of the source post in each set.',
100
101
102
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
103
104
        'default' => 0,
      ),
105
      'translate' => array(
106
        'description' => 'A boolean indicating whether this translation page needs to be updated.',
107
108
        'type' => 'int',
        'not null' => TRUE,
109
        'default' => 0,
110
      ),
111
    ),
112
113
114
115
    'indexes' => array(
      'node_changed'        => array('changed'),
      'node_created'        => array('created'),
      'node_moderate'       => array('moderate'),
116
      'node_frontpage'      => array('promote', 'status', 'sticky', 'created'),
117
118
119
120
121
122
      'node_status_type'    => array('status', 'type', 'nid'),
      'node_title_type'     => array('title', array('type', 4)),
      'node_type'           => array(array('type', 4)),
      'uid'                 => array('uid'),
      'tnid'                => array('tnid'),
      'translate'           => array('translate'),
123
    ),
124
    'unique keys' => array(
125
126
      'vid' => array('vid'),
    ),
127
128
129
130
    'foreign keys' => array(
      'vid' => array('node_revision' => 'vid'),
      'uid' => array('users' => 'uid'),
    ),
131
    'primary key' => array('nid'),
132
  );
133
134

  $schema['node_access'] = array(
135
    'description' => 'Identifies which realm/grant pairs a user must possess in order to view, update, or delete specific nodes.',
136
    'fields' => array(
137
      'nid' => array(
138
        'description' => 'The {node}.nid this record affects.',
139
140
141
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
142
143
        'default' => 0,
      ),
144
      'gid' => array(
145
        'description' => "The grant ID a user must possess in the specified realm to gain this row's privileges on the node.",
146
147
148
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
149
150
        'default' => 0,
      ),
151
      'realm' => array(
152
        'description' => 'The realm in which the user must possess the grant ID. Each node access node can define one or more realms.',
153
154
155
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
156
157
        'default' => '',
      ),
158
      'grant_view' => array(
159
        'description' => 'Boolean indicating whether a user with the realm/grant pair can view this node.',
160
161
162
163
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
164
165
        'size' => 'tiny',
      ),
166
      'grant_update' => array(
167
        'description' => 'Boolean indicating whether a user with the realm/grant pair can edit this node.',
168
169
170
171
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
172
173
        'size' => 'tiny',
      ),
174
      'grant_delete' => array(
175
        'description' => 'Boolean indicating whether a user with the realm/grant pair can delete this node.',
176
177
178
179
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
180
        'size' => 'tiny',
181
      ),
182
    ),
183
    'primary key' => array('nid', 'gid', 'realm'),
184
    'foreign keys' => array('node' => 'nid'),
185
  );
186

187
  $schema['node_revision'] = array(
188
    'description' => 'Stores information about each saved version of a {node}.',
189
    'fields' => array(
190
      'nid' => array(
191
        'description' => 'The {node} this version belongs to.',
192
193
194
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
195
196
        'default' => 0,
      ),
197
      'vid' => array(
198
        'description' => 'The primary identifier for this version.',
199
200
        'type' => 'serial',
        'unsigned' => TRUE,
201
202
        'not null' => TRUE,
      ),
203
      'uid' => array(
204
        'description' => 'The {users}.uid that created this version.',
205
206
        'type' => 'int',
        'not null' => TRUE,
207
208
        'default' => 0,
      ),
209
      'title' => array(
210
        'description' => 'The title of this version.',
211
212
213
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
214
215
        'default' => '',
      ),
216
      'log' => array(
217
        'description' => 'The log entry explaining the changes in this version.',
218
219
        'type' => 'text',
        'not null' => TRUE,
220
221
        'size' => 'big',
      ),
222
      'timestamp' => array(
223
        'description' => 'A Unix timestamp indicating when this version was created.',
224
225
        'type' => 'int',
        'not null' => TRUE,
226
227
        'default' => 0,
      ),
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
      'status' => array(
        'description' => 'Boolean indicating whether the node (at the time of this revision) is published (visible to non-administrators).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'comment' => array(
        'description' => 'Whether comments are allowed on this node (at the time of this revision): 0 = no, 1 = closed (read only), 2 = open (read/write).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'promote' => array(
        'description' => 'Boolean indicating whether the node (at the time of this revision) should be displayed on the front page.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'moderate' => array(
        'description' => 'Previously, a boolean indicating whether the node (at the time of this revision) was "in moderation"; mostly no longer used.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'sticky' => array(
        'description' => 'Boolean indicating whether the node (at the time of this revision) should be displayed at the top of lists in which it appears.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
258
    ),
259
260
    'indexes' => array(
      'nid' => array('nid'),
261
262
      'uid' => array('uid'),
    ),
263
    'primary key' => array('vid'),
264
265
266
267
    'foreign keys' => array(
      'node' => 'nid',
      'users' => 'uid'
    ),
268
  );
269
270

  $schema['node_type'] = array(
271
    'description' => 'Stores information about all defined {node} types.',
272
    'fields' => array(
273
      'type' => array(
274
        'description' => 'The machine-readable name of this type.',
275
276
        'type' => 'varchar',
        'length' => 32,
277
278
        'not null' => TRUE,
      ),
279
      'name' => array(
280
        'description' => 'The human-readable name of this type.',
281
282
283
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
284
285
        'default' => '',
      ),
286
      'base' => array(
287
        'description' => 'The base string used to construct callbacks corresponding to this node type.',
288
289
        'type' => 'varchar',
        'length' => 255,
290
291
292
        'not null' => TRUE,
      ),
      'description' => array(
293
        'description' => 'A brief description of this type.',
294
295
        'type' => 'text',
        'not null' => TRUE,
296
297
        'size' => 'medium',
      ),
298
      'help' => array(
299
        'description' => 'Help information shown to the user when creating a {node} of this type.',
300
301
        'type' => 'text',
        'not null' => TRUE,
302
303
        'size' => 'medium',
      ),
304
      'has_title' => array(
305
        'description' => 'Boolean indicating whether this type uses the {node}.title field.',
306
307
308
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
309
310
        'size' => 'tiny',
      ),
311
      'title_label' => array(
312
        'description' => 'The label displayed for the title field on the edit form.',
313
314
315
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
316
317
        'default' => '',
      ),
318
      'has_body' => array(
319
        'description' => 'Boolean indicating whether this type has the body field attached.',
320
321
322
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
323
324
        'size' => 'tiny',
      ),
325
      'body_label' => array(
326
        'description' => 'The label displayed for the body field on the edit form.',
327
328
329
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
330
331
        'default' => '',
      ),
332
      'custom' => array(
333
        'description' => 'A boolean indicating whether this type is defined by a module (FALSE) or by a user via a module like the Content Construction Kit (TRUE).',
334
335
336
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
337
338
        'size' => 'tiny',
      ),
339
      'modified' => array(
340
        'description' => 'A boolean indicating whether this type has been modified by an administrator; currently not used in any way.',
341
342
343
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
344
345
        'size' => 'tiny',
      ),
346
      'locked' => array(
347
        'description' => 'A boolean indicating whether the administrator can change the machine name of this type.',
348
349
350
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
351
352
        'size' => 'tiny',
      ),
353
      'orig_type' => array(
354
        'description' => 'The original machine-readable name of this node type. This may be different from the current type name if the locked field is 0.',
355
356
357
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
358
        'default' => '',
359
      ),
360
    ),
361
    'primary key' => array('type'),
362
  );
363
364
365

  return $schema;
}
366
367

/**
368
369
 * @defgroup updates-6.x-to-7.x System updates from 6.x to 7.x
 * @{
370
371
372
373
374
375
376
377
378
379
380
381
382
383
 */

/**
 * Fix node type 'module' attribute to avoid name-space conflicts.
 */
function node_update_7000() {
  $ret = array();

  $ret[] = update_sql("UPDATE {node_type} SET module = 'node_content' WHERE module = 'node'");
  db_change_field($ret, 'node_type', 'module', 'base', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE));

  return $ret;
}

384
385
386
387
388
389
390
391
392
/**
 * Rename {node_revisions} table to {node_revision}.
 */
function node_update_7001() {
  $ret = array();
  db_rename_table($ret, 'node_revisions', 'node_revision');
  return $ret;
}

393
394
395
396
397
398
399
400
401
402
/**
 * Extend the node_promote_status index to include all fields required for the node page query.
 */
function node_update_7002() {
  $ret = array();
  db_drop_index($ret, 'node', 'node_promote_status');
  db_add_index($ret, 'node', 'node_frontpage', array('promote', 'status', 'sticky', 'created'));
  return $ret;
}

403
/**
404
405
406
407
 * Remove the node_counter if the statistics module is uninstalled.
 */
function node_update_7003() {
  $ret = array();
408
  if (drupal_get_installed_schema_version('statistics') == SCHEMA_UNINSTALLED) {
409
410
411
412
413
    db_drop_table($ret, 'node_counter');
  }
  return $ret;
}

414
415
416
417
418
419
420
421
422
423
/**
 * Extend the existing default preview and teaser settings to all node types.
 */
function node_update_7004() {
  // Get original settings and all types.
  $original_length = variable_get('teaser_length', 600);
  $original_preview = variable_get('node_preview', 0);

  // Map old preview setting to new values order.
  $original_preview ? $original_preview = 2 : $original_preview = 1;
424
  node_type_clear();
425
  $type_list = node_type_get_types();
426
427

  // Apply original settings to all types.
428
  foreach ($type_list as $type => $object) {
429
430
431
432
433
    variable_set('teaser_length_' . $type, $original_length);
    variable_set('node_preview_' . $type, $original_preview);
  }
  // Delete old variable but leave 'teaser_length' for aggregator module upgrade.
  variable_del('node_preview');
434
435

  return array();
436
437
}

438
/**
439
 * Add status/comment/promote/moderate and sticky columns to the {node_revision} table.
440
 */
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
function node_update_7005() {
  $ret = array();
  foreach(array('status', 'comment', 'promote', 'moderate', 'sticky') as $column) {
    db_add_field($ret, 'node_revision', $column, array(
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ));
  }
  return $ret;
}

/**
 * Convert body and teaser from node properties to fields, and migrate status/comment/promote/moderate and sticky columns to the {node_revision} table.
 */
function node_update_7006(&$context) {
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
  $ret = array('#finished' => 0);

  // Get node type info for every invocation.
  node_type_clear();
  $node_types = node_type_get_types();

  if (!isset($context['total'])) {
    // Initial invocation.

    // Re-save node types to create body field instances.
    foreach ($node_types as $type => $info) {
      if ($info->has_body) {
        node_type_save($info);
      }
    }

    // Initialize state for future calls.
    $context['last'] = 0;
    $context['count'] = 0;

    $query = db_select('node', 'n');
    $query->join('node_revision', 'nr', 'n.vid = nr.vid');
    $context['total'] = $query->countQuery()->execute()->fetchField();
  }
  else {
    // Subsequent invocations.

    $found = FALSE;
    if ($context['total']) {
      // Operate on every revision of every node (whee!), in batches.
      $batch_size = 50;
      $query = db_select('node', 'n');
      $nr = $query->innerJoin('node_revision', 'nr', 'n.vid = nr.vid');
      $revisions = $query
491
        ->fields('n', array('type', 'status', 'comment', 'promote', 'moderate', 'sticky'))
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
        ->fields($nr)
        ->condition('nr.vid', $context['last'], '>')
        ->orderBy('nr.vid', 'ASC')
        ->execute();

      // Load each reversion of each node, set up 'body'
      // appropriately, and save the node's field data.  Note that
      // node_load() will not return the body or teaser values from
      // {node_revision} because those columns have been removed from the
      // schema structure in memory (but not yet from the database),
      // so we get the values from the explicit query of the table
      // instead.
      foreach ($revisions as $revision) {
        $found = TRUE;

        if ($node_types[$revision->type]->has_body) {
          $node = (object) array(
            'nid' => $revision->nid,
            'vid' => $revision->vid,
            'type' => $revision->type,
          );
          if (!empty($revision->teaser) && $revision->teaser != text_summary($revision->body)) {
            $node->body[0]['summary'] = $revision->teaser;
          }
          // Do this after text_summary() above.
          $break = '<!--break-->';
          if (substr($revision->body, 0, strlen($break)) == $break) {
            $revision->body = substr($revision->body, strlen($break));
          }
          $node->body[0]['value'] = $revision->body;
          $node->body[0]['format'] = $revision->format;
          // This is a core update and no contrib modules are enabled yet, so
          // we can assume default field storage for a faster update.
          field_sql_storage_field_storage_write('node', $node, FIELD_STORAGE_INSERT, array());
        }

528
529
530
531
532
533
534
535
536
537
538
539
540
        // Migrate the status columns to the {node_revision} table.
        db_update('node_revision')
          ->fields(array(
            'vid' => $revision->vid,
            'status' => $revision->status,
            'comment' => $revision->comment,
            'promote' => $revision->promote,
            'moderate' => $revision->moderate,
            'sticky' => $revision->sticky,
          ))
          ->condition('vid', $revision->vid)
          ->execute();

541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
        $context['last'] = $revision->vid;
        $context['count'] += 1;

        if (--$batch_size == 0) {
          break;
        }
      }

      $ret['#finished'] = min(0.99, $context['count'] / $context['total']);
    }

    if (!$found) {
      // All nodes are processed.
      $ret[] = array('success' => TRUE, 'query' => "{$context['total']} node body and teaser properties migrated to the 'body' field.");

      // Remove the now-obsolete body info from node_revision.
      db_drop_field($ret, 'node_revision', 'body');
      db_drop_field($ret, 'node_revision', 'teaser');
      db_drop_field($ret, 'node_revision', 'format');

      // We're done.
      $ret['#finished'] = 1;
    }
  }

  return $ret;
}

569
570
571
/**
 * Remove column min_word_count.
 */
572
function node_update_7007() {
573
574
575
576
577
  $ret = array();
  db_drop_field($ret, 'node_type', 'min_word_count');
  return $ret;
}

578
579
580
/**
 * @} End of "defgroup updates-6.x-to-7.x"
 * The next series of updates should start at 8000.
581
 */