node.install 34.6 KB
Newer Older
1 2
<?php

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

8
use Drupal\Component\Uuid\Uuid;
9 10
use Drupal\Core\Language\Language;

11
/**
12
 * Implements hook_schema().
13 14 15
 */
function node_schema() {
  $schema['node'] = array(
16
    'description' => 'The base table for nodes.',
17
    'fields' => array(
18
      'nid' => array(
19
        'description' => 'The primary identifier for a node.',
20 21
        'type' => 'serial',
        'unsigned' => TRUE,
22 23
        'not null' => TRUE,
      ),
24 25 26 27 28 29
      'uuid' => array(
        'description' => 'Unique Key: Universally unique identifier for this entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
      ),
30 31
      // Defaults to NULL in order to avoid a brief period of potential
      // deadlocks on the index.
32
      'vid' => array(
33
        'description' => 'The current {node_field_revision}.vid version identifier.',
34 35
        'type' => 'int',
        'unsigned' => TRUE,
36 37
        'not null' => FALSE,
        'default' => NULL,
38
      ),
39
      'type' => array(
40
        'description' => 'The type of this node.',
41 42 43
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
44 45
        'default' => '',
      ),
46 47 48 49 50 51 52 53 54 55
    ),
    'indexes' => array(
      'node_type' => array(array('type', 4)),
    ),
    'unique keys' => array(
      'vid' => array('vid'),
      'uuid' => array('uuid'),
    ),
    'foreign keys' => array(
      'node_revision' => array(
56
        'table' => 'node_revision',
57 58 59 60 61 62
        'columns' => array('vid' => 'vid'),
      ),
    ),
    'primary key' => array('nid'),
  );

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
  $schema['node_revision'] = array(
    'description' => 'Stores information about each saved version of a {node}.',
    'fields' => array(
      'nid' => array(
        'description' => 'The {node} this version belongs to.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'description' => 'The primary identifier for this version.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'revision_uid' => array(
        'description' => 'The {users}.uid that created this version.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'log' => array(
        'description' => 'The log entry explaining the changes in this version.',
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
      ),
      'revision_timestamp' => array(
        'description' => 'The Unix timestamp when the version was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'langcode' => array(
        'description' => 'The {language}.langcode of this version.',
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'indexes' => array(
      'nid' => array('nid'),
      'revision_uid' => array('revision_uid'),
      'node_langcode' => array('langcode'),
    ),
    'foreign keys' => array(
      'versioned_node' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
      'version_author' => array(
        'table' => 'users',
        'columns' => array('revision_uid' => 'uid'),
      ),
    ),
    'primary key' => array('vid'),
  );

123 124
  // Node field storage.
  $schema['node_field_data'] = array(
125
    'description' => 'Data table for node base fields.',
126 127 128
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a node.',
129
        'type' => 'int',
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'description' => 'The current {node_field_revision}.vid version identifier.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'The {node_type}.type of this node.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'langcode' => array(
        'description' => 'The {language}.langcode of these node property values.',
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
      'default_langcode' => array(
        'description' => 'Boolean indicating whether the property values are in the {language}.langcode of this node.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
159
      'title' => array(
160
        'description' => 'The title of this node, always treated as non-markup plain text.',
161 162 163
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
164 165
        'default' => '',
      ),
166
      'uid' => array(
167
        'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
168
        'type' => 'int',
169
        'unsigned' => TRUE,
170
        'not null' => TRUE,
171 172
        'default' => 0,
      ),
173
      'status' => array(
174
        'description' => 'Boolean indicating whether the node translation is published (visible to non-administrators).',
175 176
        'type' => 'int',
        'not null' => TRUE,
177 178
        'default' => 1,
      ),
179
      'created' => array(
180
        'description' => 'The Unix timestamp when the node translation was created.',
181 182
        'type' => 'int',
        'not null' => TRUE,
183 184
        'default' => 0,
      ),
185
      'changed' => array(
186
        'description' => 'The Unix timestamp when the node translation was most recently saved.',
187 188
        'type' => 'int',
        'not null' => TRUE,
189 190
        'default' => 0,
      ),
191
      'promote' => array(
192
        'description' => 'Boolean indicating whether the node translation should be displayed on the front page.',
193 194
        'type' => 'int',
        'not null' => TRUE,
195 196
        'default' => 0,
      ),
197
      'sticky' => array(
198
        'description' => 'Boolean indicating whether the node translation should be displayed at the top of lists in which it appears.',
199 200
        'type' => 'int',
        'not null' => TRUE,
201
        'default' => 0,
202
      ),
203
    ),
204
    'indexes' => array(
205 206 207 208 209 210 211 212
      'node_changed' => array('changed'),
      'node_created' => array('created'),
      'node_default_langcode' => array('default_langcode'),
      'node_langcode' => array('langcode'),
      'node_frontpage' => array('promote', 'status', 'sticky', 'created'),
      'node_status_type' => array('status', 'type', 'nid'),
      'node_title_type' => array('title', array('type', 4)),
      'node_type' => array(array('type', 4)),
213
      'vid' => array('vid'),
214
      'uid' => array('uid'),
215
    ),
216
    'foreign keys' => array(
217 218 219
      'node_base' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
220 221 222 223 224
      ),
      'node_author' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
225
    ),
226
    'primary key' => array('nid', 'langcode'),
227
  );
228

229
  $schema['node_field_revision'] = array(
230
    'description' => 'Revision table for node base fields.',
231
    'fields' => array(
232
      'nid' => array(
233
        'description' => 'The {node} this version belongs to.',
234 235 236
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
237 238 239
      ),
      'vid' => array(
        'description' => 'The primary identifier for this version.',
240
        'type' => 'int',
241 242
        'unsigned' => TRUE,
        'not null' => TRUE,
243
      ),
244
      'langcode' => array(
245
        'description' => 'The {language}.langcode of this version.',
246 247 248 249 250
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
251 252
      'default_langcode' => array(
        'description' => 'Boolean indicating whether the property values of this version are in the {language}.langcode of this node.',
253 254 255 256
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
257 258
      'title' => array(
        'description' => 'The title of this version, always treated as non-markup plain text.',
259 260 261
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
262 263
        'default' => '',
      ),
264 265
      'uid' => array(
        'description' => 'The {users}.uid that created this node.',
266 267 268 269
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
270
      ),
271 272 273 274 275 276 277 278
      '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,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the node was created.',
279 280 281
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
282
      ),
283 284 285 286 287 288 289 290 291 292 293 294 295 296
      'changed' => array(
        'description' => 'The Unix timestamp when the version was most recently saved.',
        '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,
      ),
      '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.',
297 298 299 300
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
301
    ),
302 303 304 305 306
    'indexes' => array(
      'uid' => array('uid'),
      'node_default_langcode' => array('default_langcode'),
      'node_langcode' => array('langcode'),
    ),
307
    'foreign keys' => array(
308
      'versioned_node' => array(
309 310 311
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
312
      'node_author' => array(
313 314 315 316
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
    ),
317
    'primary key' => array('vid', 'langcode'),
318
  );
319

320 321
  $schema['node_access'] = array(
    'description' => 'Identifies which realm/grant pairs a user must possess in order to view, update, or delete specific nodes.',
322
    'fields' => array(
323
      'nid' => array(
324
        'description' => 'The {node}.nid this record affects.',
325 326 327
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
328 329
        'default' => 0,
      ),
330 331 332 333 334 335 336 337 338 339
      'langcode' => array(
        'description' => 'The {language}.langcode of this node.',
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
      'fallback' => array(
        'description' => 'Boolean indicating whether this record should be used as a fallback if a language condition is not provided.',
        'type' => 'int',
340
        'unsigned' => TRUE,
341
        'not null' => TRUE,
342
        'default' => 1,
343
      ),
344 345
      'gid' => array(
        'description' => "The grant ID a user must possess in the specified realm to gain this row's privileges on the node.",
346
        'type' => 'int',
347
        'unsigned' => TRUE,
348
        'not null' => TRUE,
349 350
        'default' => 0,
      ),
351 352
      'realm' => array(
        'description' => 'The realm in which the user must possess the grant ID. Each node access node can define one or more realms.',
353 354 355
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
356 357
        'default' => '',
      ),
358 359
      'grant_view' => array(
        'description' => 'Boolean indicating whether a user with the realm/grant pair can view this node.',
360
        'type' => 'int',
361
        'unsigned' => TRUE,
362 363
        'not null' => TRUE,
        'default' => 0,
364
        'size' => 'tiny',
365
      ),
366 367
      'grant_update' => array(
        'description' => 'Boolean indicating whether a user with the realm/grant pair can edit this node.',
368
        'type' => 'int',
369
        'unsigned' => TRUE,
370 371
        'not null' => TRUE,
        'default' => 0,
372
        'size' => 'tiny',
373
      ),
374 375
      'grant_delete' => array(
        'description' => 'Boolean indicating whether a user with the realm/grant pair can delete this node.',
376
        'type' => 'int',
377
        'unsigned' => TRUE,
378 379
        'not null' => TRUE,
        'default' => 0,
380
        'size' => 'tiny',
381
      ),
382
    ),
383
    'primary key' => array('nid', 'gid', 'realm', 'langcode'),
384
    'foreign keys' => array(
385
      'affected_node' => array(
386 387 388
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
389
    ),
390
  );
391 392 393

  return $schema;
}
394

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
/**
 * Implements hook_install().
 */
function node_install() {
  // Populate the node access table.
  db_insert('node_access')
    ->fields(array(
      'nid' => 0,
      'gid' => 0,
      'realm' => 'all',
      'grant_view' => 1,
      'grant_update' => 0,
      'grant_delete' => 0,
    ))
    ->execute();
}

412 413
/**
 * Implements hook_uninstall().
414 415 416
 *
 * @see node_ranking()
 * @see _node_rankings()
417 418 419
 */
function node_uninstall() {
  // Delete node type variables.
420 421
  $types = config_get_storage_names_with_prefix('node.type.');
  foreach ($types as $config_name) {
422 423
    $type = \Drupal::config($config_name)->get('type');
    \Drupal::config('language.settings')->clear('node. ' . $type . '.language.default_configuration')->save();
424 425 426 427 428 429 430 431 432
  }

  // Delete node search ranking variables.
  variable_del('node_rank_relevance');
  variable_del('node_rank_sticky');
  variable_del('node_rank_promote');
  variable_del('node_rank_recent');

  // Delete remaining general module variables.
433
  \Drupal::state()->delete('node.node_access_needs_rebuild');
434
  variable_del('node_recent_block_count');
435 436

  // Delete any stored state.
437
  \Drupal::state()->delete('node.cron_last');
438 439
}

440
/**
441
 * Fetches node types directly from the database.
442
 *
443
 * @ingroup update_api
444
 */
445
function _update_7000_node_get_types() {
446 447 448 449 450 451 452
  $node_types = db_query('SELECT * FROM {node_type}')->fetchAllAssoc('type', PDO::FETCH_OBJ);

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

  foreach ($extra_types as $type) {
453
    $type_object = new stdClass();
454 455 456 457 458 459 460 461 462 463 464
    $type_object->type = $type;
    // In Drupal 6, whether you have a body field or not is a flag in the node
    // type table. If it's enabled, nodes may or may not have an empty string
    // for the bodies. As we can't detect what this setting should be in
    // Drupal 7 without access to the Drupal 6 node type settings, we assume
    // the default, which is to enable the body field.
    $type_object->has_body = 1;
    $type_object->body_label = 'Body';
    $node_types[$type_object->type] = $type_object;
  }
  return $node_types;
465
}
466 467 468 469 470 471

/**
 * @addtogroup updates-7.x-to-8.x
 * @{
 */

472 473 474 475 476 477 478 479 480
/**
 * Implements hook_update_dependency().
 */
function node_update_dependency() {
  $dependencies['node'][8013] = array(
    'user' => 8002,
  );
}

481 482 483 484
/**
 * Rename node type language variable names.
 *
 * @see http://drupal.org/node/540294
485 486
 *
 * @ingroup config_upgrade
487 488 489 490
 */
function node_update_8001() {
  $types = db_query('SELECT type FROM {node_type}')->fetchCol();
  foreach ($types as $type) {
491
    $node_type_language = update_variable_get('language_content_type_' . $type);
492
    if (isset($node_type_language)) {
493
      update_variable_set('node_type_language_' . $type, $node_type_language);
494
    }
495
    update_variable_del('language_content_type_' . $type);
496 497 498
  }
}

499 500 501 502 503 504 505 506 507 508 509 510 511 512
/**
 * Rename node.language field to node.langcode.
 */
function node_update_8002() {
  $spec = array(
    'description' => 'The {language}.langcode of this node.',
    'type' => 'varchar',
    'length' => 12,
    'not null' => TRUE,
    'default' => '',
  );
  db_change_field('node', 'language', 'langcode', $spec);
}

513
/**
514 515 516 517 518
 * Rename node type language variable names.
 */
function node_update_8003() {
  $types = db_query('SELECT type FROM {node_type}')->fetchCol();
  foreach ($types as $type) {
519
    update_variable_set('node_type_language_default_' . $type, Language::LANGCODE_NOT_SPECIFIED);
520
    $node_type_language = update_variable_get('node_type_language_' . $type, 0);
521
    if ($node_type_language == 0) {
522
      update_variable_set('node_type_language_show_' . $type, FALSE);
523 524 525 526
    }
    if ($node_type_language == 2) {
      // Translation was enabled, so enable it again and
      // unhide the language selector. Because if language is
527
      // Language::LANGCODE_NOT_SPECIFIED and the selector hidden, translation
528
      // cannot be enabled.
529
      update_variable_set('node_type_language_show_' . $type, TRUE);
530
      update_variable_set('node_type_language_translation_enabled_' . $type, TRUE);
531
    }
532
    update_variable_del('node_type_language_' . $type);
533 534 535
  }
}

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
/**
 * Create a UUID column for nodes.
 */
function node_update_8004() {
  $spec = array(
    'description' => 'Unique Key: Universally unique identifier for this entity.',
    'type' => 'varchar',
    'length' => 128,
    'not null' => FALSE,
  );
  $keys = array(
    'unique keys' => array(
      'uuid' => array('uuid'),
    ),
  );
  // Account for sites having the contributed UUID module installed.
  if (db_field_exists('node', 'uuid')) {
    db_change_field('node', 'uuid', 'uuid', $spec, $keys);
  }
  else {
    db_add_field('node', 'uuid', $spec, $keys);
  }
}

560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
/**
 * Make *id fields unsigned.
 */
function node_update_8005() {
  db_drop_index('node', 'uid');
  db_change_field('node', 'uid', 'uid',
    array(
      'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    ),
    array('indexes' => array(
      'uid' => array('uid'),
    ))
  );

  db_drop_index('node_revision', 'uid');
  db_change_field('node_revision', 'uid', 'uid',
    array(
      'description' => 'The {users}.uid that created this version.',
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    ),
    array('indexes' => array(
      'uid' => array('uid'),
    ))
  );

  db_drop_primary_key('history');
  db_drop_index('history', 'nid');
  db_change_field('history', 'uid', 'uid',
    array(
      'description' => 'The {users}.uid that read the {node} nid.',
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    )
  );
  db_change_field('history', 'nid', 'nid',
    array(
      'description' => 'The {node}.nid that was read.',
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    ),
    array('primary key' => array('uid', 'nid'), 'indexes' => array(
      'nid' => array('nid'),
    ))
  );
}

617
/**
618
 * Generate a UUID for all nodes.
619 620 621 622 623 624 625 626 627 628 629 630 631 632
 */
function node_update_8006(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['last'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(nid) FROM {node} WHERE uuid IS NULL')->fetchField();
  }

  $nids = db_query_range('SELECT nid FROM {node} WHERE nid > :nid AND uuid IS NULL ORDER BY nid ASC', 0, 10, array(':nid' => $sandbox['last']))->fetchCol();
  update_add_uuids($sandbox, 'node', 'nid', $nids);

  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
}

633 634 635 636 637 638 639
/**
 * Move the language default values to config.
 */
function node_update_8007() {
  $types = db_query('SELECT type FROM {node_type}')->fetchCol();
  foreach ($types as $type) {
    $language_default = update_variable_get('node_type_language_default_' . $type, NULL);
640 641 642
    $language_show = update_variable_get('node_type_language_show_' . $type, NULL);
    if (isset($language_default) || isset($language_show)) {
      $values = array('langcode' => $language_default, 'language_show' => $language_show);
643
      \Drupal::config('language.settings')->set('node.' . $type . '.language.default_configuration', $values)->save();
644 645 646 647
    }
  }
}

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
/**
 * Rename default menu names.
 */
function node_update_8008() {
  // System menu's new block deltas are prefixed with 'menu-'.
  $map = array(
    'navigation' => 'menu-tools',
    'management' => 'menu-admin',
    'user-menu' => 'menu-account',
    'main-menu' => 'menu-main',
  );
  foreach ($map as $old => $new) {
    db_update('block_node_type')
      ->condition('module', 'system')
      ->condition('delta', $old)
      ->fields(array('delta' => $new))
      ->execute();
  }
}

668 669 670 671 672 673 674 675 676
/**
 * Coverts default_nodes_main variable to config.
 *
 * @ingroup config_upgrade
 */
function node_update_8009() {
  update_variables_to_config('node.settings', array('default_nodes_main' => 'items_per_page'));
}

677 678 679 680 681
/**
 * Moves node_access_needs_rebuild from variable to state.
 *
 * @ingroup config_upgrade
 */
webchick's avatar
webchick committed
682
function node_update_8010() {
683 684 685
  update_variables_to_state(array('node_access_needs_rebuild' => 'node.node_access_needs_rebuild'));
}

686 687 688 689 690 691 692 693 694
/**
 * Moves node cron last run time from variable to state.
 *
 * @ingroup config_upgrade
 */
function node_update_8011() {
  update_variables_to_state(array('node_cron_last' =>'node.cron_last'));
}

695 696 697 698 699
/**
 * Enable History module.
 */
function node_update_8012() {
  // Enable the history module without re-installing the schema.
700
  \Drupal::moduleHandler()->install(array('history'));
701 702
}

703 704 705 706
/**
 * Renames global revision permissions to use the word 'all'.
 */
function node_update_8013() {
707 708 709 710 711
  update_replace_permissions(array(
    'view revisions' => array('view all revisions'),
    'revert revisions' => array('revert all revisions'),
    'delete revisions' => array('delete all revisions'),
  ));
712 713
}

714
/**
715
 * Empty update. See https://drupal.org/node/2006484.
716 717 718 719
 */
function node_update_8014() {
}

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
/**
 * Add language support to the {node_access} table.
 */
function node_update_8015() {
  // Add the langcode field.
  $langcode_field = array(
    'type' => 'varchar',
    'length' => 12,
    'not null' => TRUE,
    'default' => '',
    'description' => 'The {language}.langcode of this node.',
  );
  db_add_field('node_access', 'langcode', $langcode_field);

  // Add the fallback field.
  $fallback_field = array(
    'description' => 'Boolean indicating whether this record should be used as a fallback if a language condition is not provided.',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 1,
  );
  db_add_field('node_access', 'fallback', $fallback_field);

  db_drop_primary_key('node_access');
  db_add_primary_key('node_access', array('nid', 'gid', 'realm', 'langcode'));
}

748 749 750 751 752 753
/**
 * Upgrade node schema to the standard entity SQL schema: schema definition.
 */
function _node_update_8016_schema() {
  $schema = array();

754
  // Node field storage.
755
  $schema['node_field_data'] = array(
756
    'description' => 'Data table for node base fields.',
757 758 759
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a node.',
760
        'type' => 'int',
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'description' => 'The current {node_field_revision}.vid version identifier.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'The {node_type}.type of this node.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'langcode' => array(
        'description' => 'The {language}.langcode of these node property values.',
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
      'default_langcode' => array(
        'description' => 'Boolean indicating whether the property values are in the {language}.langcode of this node.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'title' => array(
        'description' => 'The title of this node, always treated as non-markup plain text.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'uid' => array(
        'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'status' => array(
        'description' => 'Boolean indicating whether the node translation is published (visible to non-administrators).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the node translation was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the node translation was most recently saved.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'promote' => array(
        'description' => 'Boolean indicating whether the node translation should be displayed on the front page.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'sticky' => array(
        'description' => 'Boolean indicating whether the node translation should be displayed at the top of lists in which it appears.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'indexes' => array(
      'node_changed' => array('changed'),
      'node_created' => array('created'),
838 839
      'node_default_langcode' => array('default_langcode'),
      'node_langcode' => array('langcode'),
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
      'node_frontpage' => array('promote', 'status', 'sticky', 'created'),
      'node_status_type' => array('status', 'type', 'nid'),
      'node_title_type' => array('title', array('type', 4)),
      'node_type' => array(array('type', 4)),
      'vid' => array('vid'),
      'uid' => array('uid'),
    ),
    'foreign keys' => array(
      'node_base' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
      'node_author' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
    ),
857
    'primary key' => array('nid', 'langcode'),
858 859 860
  );

  $schema['node_field_revision'] = array(
861
    'description' => 'Revision table for node base fields.',
862 863 864 865 866 867 868 869 870
    'fields' => array(
      'nid' => array(
        'description' => 'The {node} this version belongs to.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'vid' => array(
        'description' => 'The primary identifier for this version.',
871
        'type' => 'int',
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'langcode' => array(
        'description' => 'The {language}.langcode of this version.',
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
      'default_langcode' => array(
        'description' => 'Boolean indicating whether the property values of this version are in the {language}.langcode of this node.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'title' => array(
        'description' => 'The title of this version, always treated as non-markup plain text.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'uid' => array(
        'description' => 'The {users}.uid that created this node.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      '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,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the node was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'changed' => array(
        'description' => 'The Unix timestamp when the version was most recently saved.',
        '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,
      ),
      '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,
      ),
    ),
    'indexes' => array(
      'uid' => array('uid'),
935 936
      'node_default_langcode' => array('default_langcode'),
      'node_langcode' => array('langcode'),
937 938 939 940 941 942
    ),
    'foreign keys' => array(
      'versioned_node' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
943
      'node_author' => array(
944 945 946 947
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
    ),
948
    'primary key' => array('vid', 'langcode'),
949 950 951 952 953 954
  );

  return $schema;
}

/**
955
 * Upgrade node schema to the standard entity SQL schema: prepare schema.
956 957 958 959 960
 */
function node_update_8016() {
  foreach (_node_update_8016_schema() as $table => $table_schema) {
    db_create_table($table, $table_schema);
  }
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989

  $spec = array(
    'description' => 'The {language}.langcode of this version.',
    'type' => 'varchar',
    'length' => 12,
    'not null' => TRUE,
    'default' => '',
  );
  $keys_new = array('indexes' => array('node_langcode' => array('langcode')));
  db_add_field('node_revision', 'langcode', $spec, $keys_new);

  $spec = array(
    'description' => 'The Unix timestamp when the version was created.',
    'type' => 'int',
    'not null' => TRUE,
    'default' => 0,
  );
  db_change_field('node_revision', 'timestamp', 'revision_timestamp', $spec);

  db_drop_index('node_revision', 'uid');
  $spec = array(
    'description' => 'The {users}.uid that created this version.',
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0,
  );
  db_change_field('node_revision', 'uid', 'revision_uid', $spec);
  db_add_index('node_revision', 'revision_uid', array('revision_uid'));
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
}

/**
 * Upgrade node schema to the standard entity SQL schema: migrate data.
 */
function node_update_8017(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    // Initialize the batch status.
    $sandbox['progress'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(vid) FROM {node_revision}')->fetchField();
  }

  // Prepare the new records.
  $queries = array();
  $schema = _node_update_8016_schema();
1005
  $result = db_query_range('SELECT nr.*, nr.revision_timestamp, nr.revision_uid, 1 AS default_langcode, n.langcode, n.vid = nr.vid AS default_revision, n.uid, n.changed, n.created, n.type FROM {node_revision} nr JOIN {node} n ON nr.nid = n.nid ORDER BY nr.nid ASC, nr.vid ASC', $sandbox['progress'], 50);
1006 1007 1008

  foreach ($result as $row) {
    $sandbox['progress']++;
1009

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    foreach ($schema as $table => $table_schema) {
      // We need to store the data table record only when dealing with the
      // default revision.
      if ($row->default_revision || $table == 'node_field_revision') {
        $fields = array_keys($table_schema['fields']);
        $record = array();
        foreach ($fields as $field) {
          if (isset($row->{$field})) {
            $record[$field] = $row->{$field};
          }
        }
        if (!isset($queries[$table])) {
          $queries[$table] = db_insert($table)->fields($fields);
        }
        $queries[$table]->values($record);
      }
    }
1027 1028 1029 1030 1031 1032 1033

    // Populate the langcode column with the same value for each revision as we
    // have no other data available.
    db_update('node_revision')
      ->fields(array('langcode' => $row->langcode))
      ->condition('vid', $row->vid)
      ->execute();
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
  }

  // Store the new records.
  foreach ($queries as $query) {
    $query->execute();
  }

  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
}

/**
 * Upgrade node schema to the standard entity SQL schema: delete old fields.
 */
function node_update_8018() {
  $indexes = array('node_changed', 'node_created', 'node_frontpage', 'node_status_type', 'node_title_type', 'uid');
  foreach ($indexes as $index) {
    db_drop_index('node', $index);
  }
1052
  $fields = array('title', 'uid', 'status', 'created', 'changed', 'promote', 'sticky', 'langcode');
1053 1054 1055
  foreach ($fields as $field) {
    db_drop_field('node', $field);
  }
1056 1057 1058 1059
  $fields = array('title', 'status', 'comment', 'promote', 'sticky');
  foreach ($fields as $field) {
    db_drop_field('node_revision', $field);
  }
1060 1061
}

1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
/**
 * Convert the 'module' column in {node_type} to the maximum shortname length.
 */
function node_update_8019() {
  if (db_field_exists('node_type', 'module')) {
    $spec = array(
      'description' => 'The module defining this node type.',
      'type' => 'varchar',
      'length' => 50,
      'not null' => TRUE,
    );
    db_change_field('node_type', 'module', 'module', $spec);
  }
}

1077 1078 1079 1080 1081 1082
/**
 * Convert node types into configuration.
 *
 * @ingroup config_upgrade
*/
function node_update_8020() {
1083
  $uuid = \Drupal::service('uuid');
1084 1085 1086 1087 1088 1089
  // Properties to drop: custom, disabled.
  $locked = array();
  // Note: {node_type}.name was the label, .type the machine name.
  $result = db_query('SELECT * FROM {node_type}')
    ->fetchAllAssoc('type', PDO::FETCH_ASSOC);
  foreach ($result as $id => $node_type) {
1090
    $config = \Drupal::config('node.type.' . $id);
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
    // Node type.
    $config->setData($node_type);
    $config->set('uuid', $uuid->generate());
    $config->set('langcode', Language::LANGCODE_NOT_SPECIFIED);

    // Node type settings.
    $variables = db_query('SELECT name, value FROM {variable} WHERE name IN (:names)', array(
      ':names' => array(
        'node_submitted_' . $id,
        'node_preview_' . $id,
        'node_options_' . $id,
      ),
    ))->fetchAllKeyed();
    $variables = array_map('unserialize', $variables);
    // There are not necessarily values for all settings, so pollute defaults.
    $variables += array(
      'node_submitted_' . $id => TRUE,
      'node_preview_' . $id => 1, // DRUPAL_OPTIONAL
      'node_options_' . $id => array('status', 'promote'),
    );
    foreach ($variables as $name => $value) {
      // Turn e.g. 'node_submitted_ID' into 'submitted'.
      $name = str_replace(array('node_', '_' . $id), '', $name);
      $config->set('settings.node.' . $name, $value);

      update_variable_del($name);
    }
    $config->save();
    // Convert the 'base' property to state. Note that we could have disabled
    // modules at this time but we still need to preserve the relation.
    if ($node_type['base'] !== 'node_content') {
      $locked[$id] = $node_type['base'];
    }
  }
1125
  \Drupal::state()->set('node.type.locked', $locked);
1126 1127
}

1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
/**
 * Converts node_admin_theme variable to config.
 *
 * @ingroup config_upgrade
 */
function node_update_8021() {
  update_variables_to_config('node.settings', array(
    'node_admin_theme' => 'use_admin_theme'
  ));
}

1139 1140
/**
 * @} End of "addtogroup updates-7.x-to-8.x"
1141 1142
 * The next series of updates should start at 9000.
 */