Projet

Général

Profil

Paste
Télécharger (30 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / node / node.install @ db2d93dd

1
<?php
2

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

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

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

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

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

    
373
  $schema['block_node_type'] = array(
374
    'description' => 'Sets up display criteria for blocks based on content types',
375
    'fields' => array(
376
      'module' => array(
377
        'type' => 'varchar',
378
        'length' => 64,
379
        'not null' => TRUE,
380
        'description' => "The block's origin module, from {block}.module.",
381
      ),
382
      'delta' => array(
383
        'type' => 'varchar',
384
        'length' => 32,
385
        'not null' => TRUE,
386
        'description' => "The block's unique delta within module, from {block}.delta.",
387
      ),
388
      'type' => array(
389
        'type' => 'varchar',
390
        'length' => 32,
391
        'not null' => TRUE,
392
        'description' => "The machine-readable name of this type from {node_type}.type.",
393
      ),
394
    ),
395
    'primary key' => array('module', 'delta', 'type'),
396
    'indexes' => array(
397
      'type' => array('type'),
398
    ),
399
  );
400

    
401
  $schema['history'] = array(
402
    'description' => 'A record of which {users} have read which {node}s.',
403
    'fields' => array(
404
      'uid' => array(
405
        'description' => 'The {users}.uid that read the {node} nid.',
406
        'type' => 'int',
407
        'not null' => TRUE,
408
        'default' => 0,
409
      ),
410
      'nid' => array(
411
        'description' => 'The {node}.nid that was read.',
412
        'type' => 'int',
413
        'not null' => TRUE,
414
        'default' => 0,
415
      ),
416
      'timestamp' => array(
417
        'description' => 'The Unix timestamp at which the read occurred.',
418
        'type' => 'int',
419
        'not null' => TRUE,
420
        'default' => 0,
421
      ),
422
    ),
423
    'primary key' => array('uid', 'nid'),
424
    'indexes' => array(
425
      'nid' => array('nid'),
426
    ),
427
  );
428

    
429
  return $schema;
430
}
431

    
432
/**
433
 * Implements hook_install().
434
 */
435
function node_install() {
436
  // Populate the node access table.
437
  db_insert('node_access')
438
    ->fields(array(
439
      'nid' => 0,
440
      'gid' => 0,
441
      'realm' => 'all',
442
      'grant_view' => 1,
443
      'grant_update' => 0,
444
      'grant_delete' => 0,
445
    ))
446
    ->execute();
447
}
448

    
449
/**
450
 * Implements hook_update_dependencies().
451
 */
452
function node_update_dependencies() {
453
  // node_update_7006() migrates node data to fields and therefore must run
454
  // after all Field modules have been enabled, which happens in
455
  // system_update_7027(). It also needs to query the {filter_format} table to
456
  // get a list of existing text formats, so it must run after
457
  // filter_update_7000(), which creates that table.
458
  $dependencies['node'][7006] = array(
459
    'system' => 7027,
460
    'filter' => 7000,
461
  );
462

    
463
  // node_update_7008() migrates role permissions and therefore must run after
464
  // the {role} and {role_permission} tables are properly set up, which happens
465
  // in user_update_7007().
466
  $dependencies['node'][7008] = array(
467
    'user' => 7007,
468
  );
469

    
470
  return $dependencies;
471
}
472

    
473
/**
474
 * Utility function: fetch the node types directly from the database.
475
 *
476
 * This function is valid for a database schema version 7000.
477
 *
478
 * @ingroup update_api
479
 */
480
function _update_7000_node_get_types() {
481
  $node_types = db_query('SELECT * FROM {node_type}')->fetchAllAssoc('type', PDO::FETCH_OBJ);
482

    
483
  // Create default settings for orphan nodes.
484
  $all_types = db_query('SELECT DISTINCT type FROM {node}')->fetchCol();
485
  $extra_types = array_diff($all_types, array_keys($node_types));
486

    
487
  foreach ($extra_types as $type) {
488
    $type_object = new stdClass();
489
    $type_object->type = $type;
490

    
491
    // In Drupal 6, whether you have a body field or not is a flag in the node
492
    // type table. If it's enabled, nodes may or may not have an empty string
493
    // for the bodies. As we can't detect what this setting should be in
494
    // Drupal 7 without access to the Drupal 6 node type settings, we assume
495
    // the default, which is to enable the body field.
496
    $type_object->has_body = 1;
497
    $type_object->body_label = 'Body';
498
    $node_types[$type_object->type] = $type_object;
499
  }
500
  return $node_types;
501
}
502

    
503
/**
504
 * @addtogroup updates-6.x-to-7.x
505
 * @{
506
 */
507

    
508
/**
509
 * Upgrade the node type table and fix node type 'module' attribute to avoid name-space conflicts.
510
 */
511
function node_update_7000() {
512
  // Rename the module column to base.
513
  db_change_field('node_type', 'module', 'base', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE));
514

    
515
  db_add_field('node_type', 'module', array(
516
    'description' => 'The module defining this node type.',
517
    'type' => 'varchar',
518
    'default' => '',
519
    'length' => 255,
520
    'not null' => TRUE,
521
  ));
522

    
523
  db_add_field('node_type', 'disabled', array(
524
    'description' => 'A boolean indicating whether the node type is disabled.',
525
    'type' => 'int',
526
    'not null' => TRUE,
527
    'default' => 0,
528
    'size' => 'tiny'
529
  ));
530

    
531
  $modules = db_select('system', 's')
532
    ->fields('s', array('name'))
533
    ->condition('type', 'module');
534
  db_update('node_type')
535
    ->expression('module', 'base')
536
    ->condition('base', $modules, 'IN')
537
    ->execute();
538

    
539
  db_update('node_type')
540
    ->fields(array('base' => 'node_content'))
541
    ->condition('base', 'node')
542
    ->execute();
543
}
544

    
545
/**
546
 * Rename {node_revisions} table to {node_revision}.
547
 */
548
function node_update_7001() {
549
  db_rename_table('node_revisions', 'node_revision');
550
}
551

    
552
/**
553
 * Extend the node_promote_status index to include all fields required for the node page query.
554
 */
555
function node_update_7002() {
556
  db_drop_index('node', 'node_promote_status');
557
  db_add_index('node', 'node_frontpage', array('promote', 'status', 'sticky', 'created'));
558
}
559

    
560
/**
561
 * Remove the node_counter if the statistics module is uninstalled.
562
 */
563
function node_update_7003() {
564
  if (drupal_get_installed_schema_version('statistics') == SCHEMA_UNINSTALLED) {
565
    db_drop_table('node_counter');
566
  }
567
}
568

    
569
/**
570
 * Extend the existing default preview and teaser settings to all node types.
571
 */
572
function node_update_7004() {
573
  // Get original settings and all types.
574
  $original_length = variable_get('teaser_length', 600);
575
  $original_preview = variable_get('node_preview', 0);
576

    
577
  // Map old preview setting to new values order.
578
  $original_preview ? $original_preview = 2 : $original_preview = 1;
579
  node_type_cache_reset();
580

    
581
  // Apply original settings to all types.
582
  foreach (_update_7000_node_get_types() as $type => $type_object) {
583
    variable_set('teaser_length_' . $type, $original_length);
584
    variable_set('node_preview_' . $type, $original_preview);
585
  }
586
  // Delete old variable but leave 'teaser_length' for aggregator module upgrade.
587
  variable_del('node_preview');
588
}
589

    
590
/**
591
 * Add status/comment/promote and sticky columns to the {node_revision} table.
592
 */
593
function node_update_7005() {
594
  foreach (array('status', 'comment', 'promote', 'sticky') as $column) {
595
    db_add_field('node_revision', $column, array(
596
      'type' => 'int',
597
      'not null' => TRUE,
598
      'default' => 0,
599
    ));
600
  }
601
}
602

    
603
/**
604
 * Convert body and teaser from node properties to fields, and migrate status/comment/promote and sticky columns to the {node_revision} table.
605
 */
606
function node_update_7006(&$sandbox) {
607
  $sandbox['#finished'] = 0;
608

    
609
  // Get node type info for every invocation.
610
  node_type_cache_reset();
611

    
612
  if (!isset($sandbox['total'])) {
613
    // Initial invocation.
614

    
615
    // First, create the body field.
616
    $body_field = array(
617
      'field_name' => 'body',
618
      'type' => 'text_with_summary',
619
      'module' => 'text',
620
      'cardinality' => 1,
621
      'entity_types' => array('node'),
622
      'translatable' => TRUE,
623
    );
624
    _update_7000_field_create_field($body_field);
625

    
626
    $default_trim_length = variable_get('teaser_length', 600);
627

    
628
    // Get node type info, specifically the body field settings.
629
    $node_types = _update_7000_node_get_types();
630

    
631
    // Add body field instances for existing node types.
632
    foreach ($node_types as $node_type) {
633
      if ($node_type->has_body) {
634
        $trim_length = variable_get('teaser_length_' . $node_type->type, $default_trim_length);
635

    
636
        $instance = array(
637
          'entity_type' => 'node',
638
          'bundle' => $node_type->type,
639
          'label' => $node_type->body_label,
640
          'description' => isset($node_type->description) ? $node_type->description : '',
641
          'required' => (isset($node_type->min_word_count) && $node_type->min_word_count > 0) ? 1 : 0,
642
          'widget' => array(
643
            'type' => 'text_textarea_with_summary',
644
            'settings' => array(
645
              'rows' => 20,
646
              'summary_rows' => 5,
647
            ),
648
            'weight' => -4,
649
            'module' => 'text',
650
          ),
651
          'settings' => array('display_summary' => TRUE),
652
          'display' => array(
653
            'default' => array(
654
              'label' => 'hidden',
655
              'type' => 'text_default',
656
            ),
657
            'teaser' => array(
658
              'label' => 'hidden',
659
              'type' => 'text_summary_or_trimmed',
660
              'trim_length' => $trim_length,
661
            ),
662
          ),
663
        );
664
        _update_7000_field_create_instance($body_field, $instance);
665
        variable_del('teaser_length_' . $node_type->type);
666
      }
667
      // Leave 'teaser_length' variable for aggregator module upgrade.
668

    
669
      $sandbox['node_types_info'][$node_type->type] = array(
670
        'has_body' => $node_type->has_body,
671
      );
672
    }
673

    
674
    // Used below when updating the stored text format of each node body.
675
    $sandbox['existing_text_formats'] = db_query("SELECT format FROM {filter_format}")->fetchCol();
676

    
677
    // Initialize state for future calls.
678
    $sandbox['last'] = 0;
679
    $sandbox['count'] = 0;
680

    
681
    $query = db_select('node', 'n');
682
    $query->join('node_revision', 'nr', 'n.nid = nr.nid');
683
    $sandbox['total'] = $query->countQuery()->execute()->fetchField();
684

    
685
    $sandbox['body_field_id'] = $body_field['id'];
686
  }
687
  else {
688
    // Subsequent invocations.
689

    
690
    $found = FALSE;
691
    if ($sandbox['total']) {
692
      // Operate on every revision of every node (whee!), in batches.
693
      $batch_size = 200;
694
      $query = db_select('node_revision', 'nr');
695
      $query->innerJoin('node', 'n', 'n.nid = nr.nid');
696
      $query
697
        ->fields('nr', array('nid', 'vid', 'body', 'teaser', 'format'))
698
        ->fields('n', array('type', 'status', 'comment', 'promote', 'sticky', 'language'))
699
        ->condition('nr.vid', $sandbox['last'], '>')
700
        ->orderBy('nr.vid', 'ASC')
701
        ->range(0, $batch_size);
702
      $revisions = $query->execute();
703

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

    
714
        if ($sandbox['node_types_info'][$revision->type]['has_body']) {
715
          $node = (object) array(
716
            'nid' => $revision->nid,
717
            'vid' => $revision->vid,
718
            'type' => $revision->type,
719
          );
720
          // After node_update_7009() we will always have LANGUAGE_NONE as
721
          // language neutral language code, but here we still have empty
722
          // strings.
723
          $langcode = empty($revision->language) ? LANGUAGE_NONE : $revision->language;
724
          if (!empty($revision->teaser) && $revision->teaser != text_summary($revision->body)) {
725
            $node->body[$langcode][0]['summary'] = $revision->teaser;
726
          }
727
          // Do this after text_summary() above.
728
          $break = '<!--break-->';
729
          if (substr($revision->body, 0, strlen($break)) == $break) {
730
            $revision->body = substr($revision->body, strlen($break));
731
          }
732
          $node->body[$langcode][0]['value'] = $revision->body;
733
          // Update the revision's text format for the changes to the Drupal 7
734
          // filter system. This uses the same kind of logic that occurs, for
735
          // example, in user_update_7010(), but we do this here rather than
736
          // via a separate set of database queries, since we are already
737
          // migrating the data.
738
          if (empty($revision->body) && empty($revision->format)) {
739
            $node->body[$langcode][0]['format'] = NULL;
740
          }
741
          elseif (!in_array($revision->format, $sandbox['existing_text_formats'])) {
742
            $node->body[$langcode][0]['format'] = variable_get('filter_default_format', 1);
743
          }
744
          else {
745
            $node->body[$langcode][0]['format'] = $revision->format;
746
          }
747
          // This is a core update and no contrib modules are enabled yet, so
748
          // we can assume default field storage for a faster update.
749
          _update_7000_field_sql_storage_write('node', $node->type, $node->nid, $node->vid, 'body', $node->body);
750
        }
751

    
752
        // Migrate the status columns to the {node_revision} table.
753
        db_update('node_revision')
754
          ->fields(array(
755
            'status' => $revision->status,
756
            'comment' => $revision->comment,
757
            'promote' => $revision->promote,
758
            'sticky' => $revision->sticky,
759
          ))
760
          ->condition('vid', $revision->vid)
761
          ->execute();
762

    
763
        $sandbox['last'] = $revision->vid;
764
        $sandbox['count'] += 1;
765
      }
766

    
767
      $sandbox['#finished'] = min(0.99, $sandbox['count'] / $sandbox['total']);
768
    }
769

    
770
    if (!$found) {
771
      // All nodes are processed.
772

    
773
      // Remove the now-obsolete body info from node_revision.
774
      db_drop_field('node_revision', 'body');
775
      db_drop_field('node_revision', 'teaser');
776
      db_drop_field('node_revision', 'format');
777

    
778
      // Remove node_type properties related to the former 'body'.
779
      db_drop_field('node_type', 'has_body');
780
      db_drop_field('node_type', 'body_label');
781

    
782
      // We're done.
783
      $sandbox['#finished'] = 1;
784
    }
785
  }
786
}
787

    
788
/**
789
 * Remove column min_word_count.
790
 */
791
function node_update_7007() {
792
  db_drop_field('node_type', 'min_word_count');
793
}
794

    
795
/**
796
 * Split the 'administer nodes' permission from 'access content overview'.
797
 */
798
function node_update_7008() {
799
  $roles = user_roles(FALSE, 'administer nodes');
800
  foreach ($roles as $rid => $role) {
801
    _update_7000_user_role_grant_permissions($rid, array('access content overview'), 'node');
802
  }
803
}
804

    
805
/**
806
 * Convert node languages from the empty string to LANGUAGE_NONE.
807
 */
808
function node_update_7009() {
809
  db_update('node')
810
    ->fields(array('language' => LANGUAGE_NONE))
811
    ->condition('language', '')
812
    ->execute();
813
}
814

    
815
/**
816
 * Add the {block_node_type} table.
817
 */
818
function node_update_7010() {
819
  $schema['block_node_type'] = array(
820
    'description' => 'Sets up display criteria for blocks based on content types',
821
    'fields' => array(
822
      'module' => array(
823
        'type' => 'varchar',
824
        'length' => 64,
825
        'not null' => TRUE,
826
        'description' => "The block's origin module, from {block}.module.",
827
      ),
828
      'delta' => array(
829
        'type' => 'varchar',
830
        'length' => 32,
831
        'not null' => TRUE,
832
        'description' => "The block's unique delta within module, from {block}.delta.",
833
      ),
834
      'type' => array(
835
        'type' => 'varchar',
836
        'length' => 32,
837
        'not null' => TRUE,
838
        'description' => "The machine-readable name of this type from {node_type}.type.",
839
      ),
840
    ),
841
    'primary key' => array('module', 'delta', 'type'),
842
    'indexes' => array(
843
      'type' => array('type'),
844
    ),
845
  );
846

    
847
  db_create_table('block_node_type', $schema['block_node_type']);
848
}
849

    
850
/**
851
 * @} End of "addtogroup updates-6.x-to-7.x".
852
 */
853

    
854
/**
855
 * @addtogroup updates-7.x-extra
856
 * @{
857
 */
858

    
859
/**
860
 * Update the database from Drupal 6 to match the schema.
861
 */
862
function node_update_7011() {
863
  // Drop node moderation field.
864
  db_drop_field('node', 'moderate');
865
  db_drop_index('node', 'node_moderate');
866

    
867
  // Change {node_revision}.status field to default to 1.
868
  db_change_field('node_revision', 'status', 'status', array(
869
    'type' => 'int',
870
    'not null' => TRUE,
871
    'default' => 1,
872
  ));
873

    
874
  // Change {node_type}.module field default.
875
  db_change_field('node_type', 'module', 'module', array(
876
    'type' => 'varchar',
877
    'length' => 255,
878
    'not null' => TRUE,
879
  ));
880
}
881

    
882
/**
883
 * Switches body fields to untranslatable while upgrading from D6 and makes them language neutral.
884
 */
885
function node_update_7012() {
886
  // If we are upgrading from D6, then body fields should be set back to
887
  // untranslatable, as D6 did not know about the idea of translating fields,
888
  // but only nodes. If a D7 > D7 update is running we need to skip this update,
889
  // as it is a valid use case to have translatable body fields in this context.
890
  if (variable_get('update_d6', FALSE)) {
891
    // Make node bodies untranslatable: field_update_field() cannot be used
892
    // throughout the upgrade process and we do not have an update counterpart
893
    // for _update_7000_field_create_field(). Hence we are forced to update the
894
    // 'field_config' table directly. This is a safe operation since it is
895
    // being performed while upgrading from D6. Perfoming the same operation
896
    // during a D7 update is highly discouraged.
897
    db_update('field_config')
898
      ->fields(array('translatable' => 0))
899
      ->condition('field_name', 'body')
900
      ->execute();
901

    
902
    // Switch field languages to LANGUAGE_NONE, since initially they were
903
    // assigned the node language.
904
    foreach (array('field_data_body', 'field_revision_body') as $table) {
905
      db_update($table)
906
        ->fields(array('language' => LANGUAGE_NONE))
907
        ->execute();
908
    }
909

    
910
    node_type_cache_reset();
911
  }
912
}
913

    
914
/**
915
 * Change {node}.vid default value from 0 to NULL to avoid deadlock issues on MySQL.
916
 */
917
function node_update_7013() {
918
  db_drop_unique_key('node', 'vid');
919
  db_change_field('node', 'vid', 'vid', array(
920
    'description' => 'The current {node_revision}.vid version identifier.',
921
    'type' => 'int',
922
    'unsigned' => TRUE,
923
    'not null' => FALSE,
924
    'default' => NULL,
925
  ));
926
  db_add_unique_key('node', 'vid', array('vid'));
927
}
928

    
929
/**
930
 * Add an index on {node}.language.
931
 */
932
function node_update_7014() {
933
  db_add_index('node', 'language', array('language'));
934
}
935

    
936
/**
937
 * Enable node types that may have been erroneously disabled in Drupal 7.36.
938
 */
939
function node_update_7015() {
940
  db_update('node_type')
941
    ->fields(array('disabled' => 0))
942
    ->condition('base', 'node_content')
943
    ->execute();
944
}
945

    
946
/**
947
 * @} End of "addtogroup updates-7.x-extra".
948
 */