node.install 35.7 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 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/**
 * Implements hook_requirements().
 */
function node_requirements($phase) {
  $requirements = array();
  if ($phase === 'runtime') {
    // Only show rebuild button if there are either 0, or 2 or more, rows
    // in the {node_access} table, or if there are modules that
    // implement hook_node_grants().
    $grant_count = \Drupal::entityManager()->getAccessController('node')->countGrants();
    if ($grant_count != 1 || count(\Drupal::moduleHandler()->getImplementations('node_grants')) > 0) {
      $value = format_plural($grant_count, 'One permission in use', '@count permissions in use', array('@count' => $grant_count));
    }
    else {
      $value = t('Disabled');
    }
    $description = t('If the site is experiencing problems with permissions to content, you may have to rebuild the permissions cache. Rebuilding will remove all privileges to content and replace them with permissions based on the current modules and settings. Rebuilding may take some time if there is a lot of content or complex permission settings. After rebuilding has completed, content will automatically use the new permissions.');

    $requirements['node_access'] = array(
      'title' => t('Node Access Permissions'),
      'value' => $value,
      'description' => $description . ' ' . l(t('Rebuild permissions'), 'admin/reports/status/rebuild'),
    );
  }
  return $requirements;
}

38
/**
39
 * Implements hook_schema().
40 41 42
 */
function node_schema() {
  $schema['node'] = array(
43
    'description' => 'The base table for nodes.',
44
    'fields' => array(
45
      'nid' => array(
46
        'description' => 'The primary identifier for a node.',
47 48
        'type' => 'serial',
        'unsigned' => TRUE,
49 50
        'not null' => TRUE,
      ),
51 52 53 54 55 56
      'uuid' => array(
        'description' => 'Unique Key: Universally unique identifier for this entity.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => FALSE,
      ),
57 58
      // Defaults to NULL in order to avoid a brief period of potential
      // deadlocks on the index.
59
      'vid' => array(
60
        'description' => 'The current {node_field_revision}.vid version identifier.',
61 62
        'type' => 'int',
        'unsigned' => TRUE,
63 64
        'not null' => FALSE,
        'default' => NULL,
65
      ),
66
      'type' => array(
67
        'description' => 'The type of this node.',
68 69 70
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
71 72
        'default' => '',
      ),
73 74 75 76 77 78 79 80 81 82
    ),
    'indexes' => array(
      'node_type' => array(array('type', 4)),
    ),
    'unique keys' => array(
      'vid' => array('vid'),
      'uuid' => array('uuid'),
    ),
    'foreign keys' => array(
      'node_revision' => array(
83
        'table' => 'node_revision',
84 85 86 87 88 89
        'columns' => array('vid' => 'vid'),
      ),
    ),
    'primary key' => array('nid'),
  );

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  $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'),
  );

150 151
  // Node field storage.
  $schema['node_field_data'] = array(
152
    'description' => 'Data table for node base fields.',
153 154 155
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a node.',
156
        'type' => 'int',
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        '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,
      ),
186
      'title' => array(
187
        'description' => 'The title of this node, always treated as non-markup plain text.',
188 189 190
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
191 192
        'default' => '',
      ),
193
      'uid' => array(
194
        'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
195
        'type' => 'int',
196
        'unsigned' => TRUE,
197
        'not null' => TRUE,
198 199
        'default' => 0,
      ),
200
      'status' => array(
201
        'description' => 'Boolean indicating whether the node translation is published (visible to non-administrators).',
202 203
        'type' => 'int',
        'not null' => TRUE,
204 205
        'default' => 1,
      ),
206
      'created' => array(
207
        'description' => 'The Unix timestamp when the node translation was created.',
208 209
        'type' => 'int',
        'not null' => TRUE,
210 211
        'default' => 0,
      ),
212
      'changed' => array(
213
        'description' => 'The Unix timestamp when the node translation was most recently saved.',
214 215
        'type' => 'int',
        'not null' => TRUE,
216 217
        'default' => 0,
      ),
218
      'promote' => array(
219
        'description' => 'Boolean indicating whether the node translation should be displayed on the front page.',
220 221
        'type' => 'int',
        'not null' => TRUE,
222 223
        'default' => 0,
      ),
224
      'sticky' => array(
225
        'description' => 'Boolean indicating whether the node translation should be displayed at the top of lists in which it appears.',
226 227
        'type' => 'int',
        'not null' => TRUE,
228
        'default' => 0,
229
      ),
230
    ),
231
    'indexes' => array(
232 233 234 235 236 237 238 239
      '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)),
240
      'vid' => array('vid'),
241
      'uid' => array('uid'),
242
    ),
243
    'foreign keys' => array(
244 245 246
      'node_base' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
247 248 249 250 251
      ),
      'node_author' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
252
    ),
253
    'primary key' => array('nid', 'langcode'),
254
  );
255

256
  $schema['node_field_revision'] = array(
257
    'description' => 'Revision table for node base fields.',
258
    'fields' => array(
259
      'nid' => array(
260
        'description' => 'The {node} this version belongs to.',
261 262 263
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
264 265 266
      ),
      'vid' => array(
        'description' => 'The primary identifier for this version.',
267
        'type' => 'int',
268 269
        'unsigned' => TRUE,
        'not null' => TRUE,
270
      ),
271
      'langcode' => array(
272
        'description' => 'The {language}.langcode of this version.',
273 274 275 276 277
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
278 279
      'default_langcode' => array(
        'description' => 'Boolean indicating whether the property values of this version are in the {language}.langcode of this node.',
280 281 282 283
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
284 285
      'title' => array(
        'description' => 'The title of this version, always treated as non-markup plain text.',
286 287 288
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
289 290
        'default' => '',
      ),
291 292
      'uid' => array(
        'description' => 'The {users}.uid that created this node.',
293 294 295 296
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
297
      ),
298 299 300 301 302 303 304 305
      '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.',
306 307 308
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
309
      ),
310 311 312 313 314 315 316 317 318 319 320 321 322 323
      '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.',
324 325 326 327
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
328
    ),
329 330 331 332 333
    'indexes' => array(
      'uid' => array('uid'),
      'node_default_langcode' => array('default_langcode'),
      'node_langcode' => array('langcode'),
    ),
334
    'foreign keys' => array(
335
      'versioned_node' => array(
336 337 338
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
339
      'node_author' => array(
340 341 342 343
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
    ),
344
    'primary key' => array('vid', 'langcode'),
345
  );
346

347 348
  $schema['node_access'] = array(
    'description' => 'Identifies which realm/grant pairs a user must possess in order to view, update, or delete specific nodes.',
349
    'fields' => array(
350
      'nid' => array(
351
        'description' => 'The {node}.nid this record affects.',
352 353 354
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
355 356
        'default' => 0,
      ),
357 358 359 360 361 362 363 364 365 366
      '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',
367
        'unsigned' => TRUE,
368
        'not null' => TRUE,
369
        'default' => 1,
370
      ),
371 372
      'gid' => array(
        'description' => "The grant ID a user must possess in the specified realm to gain this row's privileges on the node.",
373
        'type' => 'int',
374
        'unsigned' => TRUE,
375
        'not null' => TRUE,
376 377
        'default' => 0,
      ),
378 379
      'realm' => array(
        'description' => 'The realm in which the user must possess the grant ID. Each node access node can define one or more realms.',
380 381 382
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
383 384
        'default' => '',
      ),
385 386
      'grant_view' => array(
        'description' => 'Boolean indicating whether a user with the realm/grant pair can view this node.',
387
        'type' => 'int',
388
        'unsigned' => TRUE,
389 390
        'not null' => TRUE,
        'default' => 0,
391
        'size' => 'tiny',
392
      ),
393 394
      'grant_update' => array(
        'description' => 'Boolean indicating whether a user with the realm/grant pair can edit this node.',
395
        'type' => 'int',
396
        'unsigned' => TRUE,
397 398
        'not null' => TRUE,
        'default' => 0,
399
        'size' => 'tiny',
400
      ),
401 402
      'grant_delete' => array(
        'description' => 'Boolean indicating whether a user with the realm/grant pair can delete this node.',
403
        'type' => 'int',
404
        'unsigned' => TRUE,
405 406
        'not null' => TRUE,
        'default' => 0,
407
        'size' => 'tiny',
408
      ),
409
    ),
410
    'primary key' => array('nid', 'gid', 'realm', 'langcode'),
411
    'foreign keys' => array(
412
      'affected_node' => array(
413 414 415
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
416
    ),
417
  );
418 419 420

  return $schema;
}
421

422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
/**
 * 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();
}

439 440
/**
 * Implements hook_uninstall().
441 442 443
 *
 * @see node_ranking()
 * @see _node_rankings()
444 445 446
 */
function node_uninstall() {
  // Delete node type variables.
447 448
  $types = config_get_storage_names_with_prefix('node.type.');
  foreach ($types as $config_name) {
449 450
    $type = \Drupal::config($config_name)->get('type');
    \Drupal::config('language.settings')->clear('node. ' . $type . '.language.default_configuration')->save();
451 452 453
  }

  // Delete remaining general module variables.
454
  \Drupal::state()->delete('node.node_access_needs_rebuild');
455 456

  // Delete any stored state.
457
  \Drupal::state()->delete('node.cron_last');
458 459
}

460
/**
461
 * Fetches node types directly from the database.
462
 *
463
 * @ingroup update_api
464
 */
465
function _update_7000_node_get_types() {
466 467 468 469 470 471 472
  $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) {
473
    $type_object = new stdClass();
474 475 476 477 478 479 480 481 482 483 484
    $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;
485
}
486 487 488 489 490 491

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

492 493 494 495 496 497 498 499 500
/**
 * Implements hook_update_dependency().
 */
function node_update_dependency() {
  $dependencies['node'][8013] = array(
    'user' => 8002,
  );
}

501 502 503 504
/**
 * Rename node type language variable names.
 *
 * @see http://drupal.org/node/540294
505 506
 *
 * @ingroup config_upgrade
507 508 509 510
 */
function node_update_8001() {
  $types = db_query('SELECT type FROM {node_type}')->fetchCol();
  foreach ($types as $type) {
511
    $node_type_language = update_variable_get('language_content_type_' . $type);
512
    if (isset($node_type_language)) {
513
      update_variable_set('node_type_language_' . $type, $node_type_language);
514
    }
515
    update_variable_del('language_content_type_' . $type);
516 517 518
  }
}

519 520 521 522 523 524 525 526 527 528 529 530 531 532
/**
 * 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);
}

533
/**
534 535 536 537 538
 * Rename node type language variable names.
 */
function node_update_8003() {
  $types = db_query('SELECT type FROM {node_type}')->fetchCol();
  foreach ($types as $type) {
539
    update_variable_set('node_type_language_default_' . $type, Language::LANGCODE_NOT_SPECIFIED);
540
    $node_type_language = update_variable_get('node_type_language_' . $type, 0);
541
    if ($node_type_language == 0) {
542
      update_variable_set('node_type_language_show_' . $type, FALSE);
543 544 545 546
    }
    if ($node_type_language == 2) {
      // Translation was enabled, so enable it again and
      // unhide the language selector. Because if language is
547
      // Language::LANGCODE_NOT_SPECIFIED and the selector hidden, translation
548
      // cannot be enabled.
549
      update_variable_set('node_type_language_show_' . $type, TRUE);
550
      update_variable_set('node_type_language_translation_enabled_' . $type, TRUE);
551
    }
552
    update_variable_del('node_type_language_' . $type);
553 554 555
  }
}

556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
/**
 * 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);
  }
}

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 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
/**
 * 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'),
    ))
  );
}

637
/**
638
 * Generate a UUID for all nodes.
639 640 641 642 643 644 645 646 647 648 649 650 651 652
 */
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']);
}

653 654 655 656 657 658 659
/**
 * 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);
660 661 662
    $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);
663
      \Drupal::config('language.settings')->set('node.' . $type . '.language.default_configuration', $values)->save();
664 665 666 667
    }
  }
}

668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
/**
 * 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();
  }
}

688 689 690 691 692 693 694 695 696
/**
 * 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'));
}

697 698 699 700 701
/**
 * Moves node_access_needs_rebuild from variable to state.
 *
 * @ingroup config_upgrade
 */
webchick's avatar
webchick committed
702
function node_update_8010() {
703 704 705
  update_variables_to_state(array('node_access_needs_rebuild' => 'node.node_access_needs_rebuild'));
}

706 707 708 709 710 711 712 713 714
/**
 * 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'));
}

715 716 717 718 719
/**
 * Enable History module.
 */
function node_update_8012() {
  // Enable the history module without re-installing the schema.
720
  \Drupal::moduleHandler()->install(array('history'));
721 722
}

723 724 725 726
/**
 * Renames global revision permissions to use the word 'all'.
 */
function node_update_8013() {
727 728 729 730 731
  update_replace_permissions(array(
    'view revisions' => array('view all revisions'),
    'revert revisions' => array('revert all revisions'),
    'delete revisions' => array('delete all revisions'),
  ));
732 733
}

734
/**
735
 * Empty update. See https://drupal.org/node/2006484.
736 737 738 739
 */
function node_update_8014() {
}

740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
/**
 * 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'));
}

768 769 770 771 772 773
/**
 * Upgrade node schema to the standard entity SQL schema: schema definition.
 */
function _node_update_8016_schema() {
  $schema = array();

774
  // Node field storage.
775
  $schema['node_field_data'] = array(
776
    'description' => 'Data table for node base fields.',
777 778 779
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a node.',
780
        'type' => 'int',
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 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
        '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'),
858 859
      'node_default_langcode' => array('default_langcode'),
      'node_langcode' => array('langcode'),
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
      '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'),
      ),
    ),
877
    'primary key' => array('nid', 'langcode'),
878 879 880
  );

  $schema['node_field_revision'] = array(
881
    'description' => 'Revision table for node base fields.',
882 883 884 885 886 887 888 889 890
    '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.',
891
        'type' => 'int',
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 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
        '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'),
955 956
      'node_default_langcode' => array('default_langcode'),
      'node_langcode' => array('langcode'),
957 958 959 960 961 962
    ),
    'foreign keys' => array(
      'versioned_node' => array(
        'table' => 'node',
        'columns' => array('nid' => 'nid'),
      ),
963
      'node_author' => array(
964 965 966 967
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
    ),
968
    'primary key' => array('vid', 'langcode'),
969 970 971 972 973 974
  );

  return $schema;
}

/**
975
 * Upgrade node schema to the standard entity SQL schema: prepare schema.
976 977 978 979 980
 */
function node_update_8016() {
  foreach (_node_update_8016_schema() as $table => $table_schema) {
    db_create_table($table, $table_schema);
  }
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

  $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'));
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
}

/**
 * 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();
1025
  $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);
1026 1027 1028

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

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
    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);
      }
    }
1047 1048 1049 1050 1051 1052 1053

    // 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();
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
  }

  // 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);
  }
1072
  $fields = array('title', 'uid', 'status', 'created', 'changed', 'promote', 'sticky', 'langcode');
1073 1074 1075
  foreach ($fields as $field) {
    db_drop_field('node', $field);
  }
1076 1077 1078 1079
  $fields = array('title', 'status', 'comment', 'promote', 'sticky');
  foreach ($fields as $field) {
    db_drop_field('node_revision', $field);
  }
1080 1081
}

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
/**
 * 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);
  }
}

1097 1098 1099 1100 1101 1102
/**
 * Convert node types into configuration.
 *
 * @ingroup config_upgrade
*/
function node_update_8020() {
1103
  $uuid = \Drupal::service('uuid');
1104 1105 1106 1107 1108 1109
  // 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) {
1110
    $config = \Drupal::config('node.type.' . $id);
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
    // 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'];
    }
  }
1145
  \Drupal::state()->set('node.type.locked', $locked);
1146 1147
}

1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
/**
 * 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'
  ));
}

1159 1160
/**
 * @} End of "addtogroup updates-7.x-to-8.x"
1161 1162
 * The next series of updates should start at 9000.
 */