node.install 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the node module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function node_schema() {
  10. $schema['node'] = array(
  11. 'description' => 'The base table for nodes.',
  12. 'fields' => array(
  13. 'nid' => array(
  14. 'description' => 'The primary identifier for a node.',
  15. 'type' => 'serial',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE,
  18. ),
  19. // Defaults to NULL in order to avoid a brief period of potential
  20. // deadlocks on the index.
  21. 'vid' => array(
  22. 'description' => 'The current {node_revision}.vid version identifier.',
  23. 'type' => 'int',
  24. 'unsigned' => TRUE,
  25. 'not null' => FALSE,
  26. 'default' => NULL,
  27. ),
  28. 'type' => array(
  29. 'description' => 'The {node_type}.type of this node.',
  30. 'type' => 'varchar',
  31. 'length' => 32,
  32. 'not null' => TRUE,
  33. 'default' => '',
  34. ),
  35. 'language' => array(
  36. 'description' => 'The {languages}.language of this node.',
  37. 'type' => 'varchar',
  38. 'length' => 12,
  39. 'not null' => TRUE,
  40. 'default' => '',
  41. ),
  42. 'title' => array(
  43. 'description' => 'The title of this node, always treated as non-markup plain text.',
  44. 'type' => 'varchar',
  45. 'length' => 255,
  46. 'not null' => TRUE,
  47. 'default' => '',
  48. ),
  49. 'uid' => array(
  50. 'description' => 'The {users}.uid that owns this node; initially, this is the user that created it.',
  51. 'type' => 'int',
  52. 'not null' => TRUE,
  53. 'default' => 0,
  54. ),
  55. 'status' => array(
  56. 'description' => 'Boolean indicating whether the node is published (visible to non-administrators).',
  57. 'type' => 'int',
  58. 'not null' => TRUE,
  59. 'default' => 1,
  60. ),
  61. 'created' => array(
  62. 'description' => 'The Unix timestamp when the node was created.',
  63. 'type' => 'int',
  64. 'not null' => TRUE,
  65. 'default' => 0,
  66. ),
  67. 'changed' => array(
  68. 'description' => 'The Unix timestamp when the node was most recently saved.',
  69. 'type' => 'int',
  70. 'not null' => TRUE,
  71. 'default' => 0,
  72. ),
  73. 'comment' => array(
  74. 'description' => 'Whether comments are allowed on this node: 0 = no, 1 = closed (read only), 2 = open (read/write).',
  75. 'type' => 'int',
  76. 'not null' => TRUE,
  77. 'default' => 0,
  78. ),
  79. 'promote' => array(
  80. 'description' => 'Boolean indicating whether the node should be displayed on the front page.',
  81. 'type' => 'int',
  82. 'not null' => TRUE,
  83. 'default' => 0,
  84. ),
  85. 'sticky' => array(
  86. 'description' => 'Boolean indicating whether the node should be displayed at the top of lists in which it appears.',
  87. 'type' => 'int',
  88. 'not null' => TRUE,
  89. 'default' => 0,
  90. ),
  91. 'tnid' => array(
  92. 'description' => 'The translation set id for this node, which equals the node id of the source post in each set.',
  93. 'type' => 'int',
  94. 'unsigned' => TRUE,
  95. 'not null' => TRUE,
  96. 'default' => 0,
  97. ),
  98. 'translate' => array(
  99. 'description' => 'A boolean indicating whether this translation page needs to be updated.',
  100. 'type' => 'int',
  101. 'not null' => TRUE,
  102. 'default' => 0,
  103. ),
  104. ),
  105. 'indexes' => array(
  106. 'node_changed' => array('changed'),
  107. 'node_created' => array('created'),
  108. 'node_frontpage' => array('promote', 'status', 'sticky', 'created'),
  109. 'node_status_type' => array('status', 'type', 'nid'),
  110. 'node_title_type' => array('title', array('type', 4)),
  111. 'node_type' => array(array('type', 4)),
  112. 'uid' => array('uid'),
  113. 'tnid' => array('tnid'),
  114. 'translate' => array('translate'),
  115. 'language' => array('language'),
  116. ),
  117. 'unique keys' => array(
  118. 'vid' => array('vid'),
  119. ),
  120. 'foreign keys' => array(
  121. 'node_revision' => array(
  122. 'table' => 'node_revision',
  123. 'columns' => array('vid' => 'vid'),
  124. ),
  125. 'node_author' => array(
  126. 'table' => 'users',
  127. 'columns' => array('uid' => 'uid'),
  128. ),
  129. ),
  130. 'primary key' => array('nid'),
  131. );
  132. $schema['node_access'] = array(
  133. 'description' => 'Identifies which realm/grant pairs a user must possess in order to view, update, or delete specific nodes.',
  134. 'fields' => array(
  135. 'nid' => array(
  136. 'description' => 'The {node}.nid this record affects.',
  137. 'type' => 'int',
  138. 'unsigned' => TRUE,
  139. 'not null' => TRUE,
  140. 'default' => 0,
  141. ),
  142. 'gid' => array(
  143. 'description' => "The grant ID a user must possess in the specified realm to gain this row's privileges on the node.",
  144. 'type' => 'int',
  145. 'unsigned' => TRUE,
  146. 'not null' => TRUE,
  147. 'default' => 0,
  148. ),
  149. 'realm' => array(
  150. 'description' => 'The realm in which the user must possess the grant ID. Each node access node can define one or more realms.',
  151. 'type' => 'varchar',
  152. 'length' => 255,
  153. 'not null' => TRUE,
  154. 'default' => '',
  155. ),
  156. 'grant_view' => array(
  157. 'description' => 'Boolean indicating whether a user with the realm/grant pair can view this node.',
  158. 'type' => 'int',
  159. 'unsigned' => TRUE,
  160. 'not null' => TRUE,
  161. 'default' => 0,
  162. 'size' => 'tiny',
  163. ),
  164. 'grant_update' => array(
  165. 'description' => 'Boolean indicating whether a user with the realm/grant pair can edit this node.',
  166. 'type' => 'int',
  167. 'unsigned' => TRUE,
  168. 'not null' => TRUE,
  169. 'default' => 0,
  170. 'size' => 'tiny',
  171. ),
  172. 'grant_delete' => array(
  173. 'description' => 'Boolean indicating whether a user with the realm/grant pair can delete this node.',
  174. 'type' => 'int',
  175. 'unsigned' => TRUE,
  176. 'not null' => TRUE,
  177. 'default' => 0,
  178. 'size' => 'tiny',
  179. ),
  180. ),
  181. 'primary key' => array('nid', 'gid', 'realm'),
  182. 'foreign keys' => array(
  183. 'affected_node' => array(
  184. 'table' => 'node',
  185. 'columns' => array('nid' => 'nid'),
  186. ),
  187. ),
  188. );
  189. $schema['node_revision'] = array(
  190. 'description' => 'Stores information about each saved version of a {node}.',
  191. 'fields' => array(
  192. 'nid' => array(
  193. 'description' => 'The {node} this version belongs to.',
  194. 'type' => 'int',
  195. 'unsigned' => TRUE,
  196. 'not null' => TRUE,
  197. 'default' => 0,
  198. ),
  199. 'vid' => array(
  200. 'description' => 'The primary identifier for this version.',
  201. 'type' => 'serial',
  202. 'unsigned' => TRUE,
  203. 'not null' => TRUE,
  204. ),
  205. 'uid' => array(
  206. 'description' => 'The {users}.uid that created this version.',
  207. 'type' => 'int',
  208. 'not null' => TRUE,
  209. 'default' => 0,
  210. ),
  211. 'title' => array(
  212. 'description' => 'The title of this version.',
  213. 'type' => 'varchar',
  214. 'length' => 255,
  215. 'not null' => TRUE,
  216. 'default' => '',
  217. ),
  218. 'log' => array(
  219. 'description' => 'The log entry explaining the changes in this version.',
  220. 'type' => 'text',
  221. 'not null' => TRUE,
  222. 'size' => 'big',
  223. ),
  224. 'timestamp' => array(
  225. 'description' => 'A Unix timestamp indicating when this version was created.',
  226. 'type' => 'int',
  227. 'not null' => TRUE,
  228. 'default' => 0,
  229. ),
  230. 'status' => array(
  231. 'description' => 'Boolean indicating whether the node (at the time of this revision) is published (visible to non-administrators).',
  232. 'type' => 'int',
  233. 'not null' => TRUE,
  234. 'default' => 1,
  235. ),
  236. 'comment' => array(
  237. 'description' => 'Whether comments are allowed on this node (at the time of this revision): 0 = no, 1 = closed (read only), 2 = open (read/write).',
  238. 'type' => 'int',
  239. 'not null' => TRUE,
  240. 'default' => 0,
  241. ),
  242. 'promote' => array(
  243. 'description' => 'Boolean indicating whether the node (at the time of this revision) should be displayed on the front page.',
  244. 'type' => 'int',
  245. 'not null' => TRUE,
  246. 'default' => 0,
  247. ),
  248. 'sticky' => array(
  249. 'description' => 'Boolean indicating whether the node (at the time of this revision) should be displayed at the top of lists in which it appears.',
  250. 'type' => 'int',
  251. 'not null' => TRUE,
  252. 'default' => 0,
  253. ),
  254. ),
  255. 'indexes' => array(
  256. 'nid' => array('nid'),
  257. 'uid' => array('uid'),
  258. ),
  259. 'primary key' => array('vid'),
  260. 'foreign keys' => array(
  261. 'versioned_node' => array(
  262. 'table' => 'node',
  263. 'columns' => array('nid' => 'nid'),
  264. ),
  265. 'version_author' => array(
  266. 'table' => 'users',
  267. 'columns' => array('uid' => 'uid'),
  268. ),
  269. ),
  270. );
  271. $schema['node_type'] = array(
  272. 'description' => 'Stores information about all defined {node} types.',
  273. 'fields' => array(
  274. 'type' => array(
  275. 'description' => 'The machine-readable name of this type.',
  276. 'type' => 'varchar',
  277. 'length' => 32,
  278. 'not null' => TRUE,
  279. ),
  280. 'name' => array(
  281. 'description' => 'The human-readable name of this type.',
  282. 'type' => 'varchar',
  283. 'length' => 255,
  284. 'not null' => TRUE,
  285. 'default' => '',
  286. 'translatable' => TRUE,
  287. ),
  288. 'base' => array(
  289. 'description' => 'The base string used to construct callbacks corresponding to this node type.',
  290. 'type' => 'varchar',
  291. 'length' => 255,
  292. 'not null' => TRUE,
  293. ),
  294. 'module' => array(
  295. 'description' => 'The module defining this node type.',
  296. 'type' => 'varchar',
  297. 'length' => 255,
  298. 'not null' => TRUE,
  299. ),
  300. 'description' => array(
  301. 'description' => 'A brief description of this type.',
  302. 'type' => 'text',
  303. 'not null' => TRUE,
  304. 'size' => 'medium',
  305. 'translatable' => TRUE,
  306. ),
  307. 'help' => array(
  308. 'description' => 'Help information shown to the user when creating a {node} of this type.',
  309. 'type' => 'text',
  310. 'not null' => TRUE,
  311. 'size' => 'medium',
  312. 'translatable' => TRUE,
  313. ),
  314. 'has_title' => array(
  315. 'description' => 'Boolean indicating whether this type uses the {node}.title field.',
  316. 'type' => 'int',
  317. 'unsigned' => TRUE,
  318. 'not null' => TRUE,
  319. 'size' => 'tiny',
  320. ),
  321. 'title_label' => array(
  322. 'description' => 'The label displayed for the title field on the edit form.',
  323. 'type' => 'varchar',
  324. 'length' => 255,
  325. 'not null' => TRUE,
  326. 'default' => '',
  327. 'translatable' => TRUE,
  328. ),
  329. 'custom' => array(
  330. 'description' => 'A boolean indicating whether this type is defined by a module (FALSE) or by a user via Add content type (TRUE).',
  331. 'type' => 'int',
  332. 'not null' => TRUE,
  333. 'default' => 0,
  334. 'size' => 'tiny',
  335. ),
  336. 'modified' => array(
  337. 'description' => 'A boolean indicating whether this type has been modified by an administrator; currently not used in any way.',
  338. 'type' => 'int',
  339. 'not null' => TRUE,
  340. 'default' => 0,
  341. 'size' => 'tiny',
  342. ),
  343. 'locked' => array(
  344. 'description' => 'A boolean indicating whether the administrator can change the machine name of this type.',
  345. 'type' => 'int',
  346. 'not null' => TRUE,
  347. 'default' => 0,
  348. 'size' => 'tiny',
  349. ),
  350. 'disabled' => array(
  351. 'description' => 'A boolean indicating whether the node type is disabled.',
  352. 'type' => 'int',
  353. 'not null' => TRUE,
  354. 'default' => 0,
  355. 'size' => 'tiny'
  356. ),
  357. 'orig_type' => array(
  358. '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.',
  359. 'type' => 'varchar',
  360. 'length' => 255,
  361. 'not null' => TRUE,
  362. 'default' => '',
  363. ),
  364. ),
  365. 'primary key' => array('type'),
  366. );
  367. $schema['block_node_type'] = array(
  368. 'description' => 'Sets up display criteria for blocks based on content types',
  369. 'fields' => array(
  370. 'module' => array(
  371. 'type' => 'varchar',
  372. 'length' => 64,
  373. 'not null' => TRUE,
  374. 'description' => "The block's origin module, from {block}.module.",
  375. ),
  376. 'delta' => array(
  377. 'type' => 'varchar',
  378. 'length' => 32,
  379. 'not null' => TRUE,
  380. 'description' => "The block's unique delta within module, from {block}.delta.",
  381. ),
  382. 'type' => array(
  383. 'type' => 'varchar',
  384. 'length' => 32,
  385. 'not null' => TRUE,
  386. 'description' => "The machine-readable name of this type from {node_type}.type.",
  387. ),
  388. ),
  389. 'primary key' => array('module', 'delta', 'type'),
  390. 'indexes' => array(
  391. 'type' => array('type'),
  392. ),
  393. );
  394. $schema['history'] = array(
  395. 'description' => 'A record of which {users} have read which {node}s.',
  396. 'fields' => array(
  397. 'uid' => array(
  398. 'description' => 'The {users}.uid that read the {node} nid.',
  399. 'type' => 'int',
  400. 'not null' => TRUE,
  401. 'default' => 0,
  402. ),
  403. 'nid' => array(
  404. 'description' => 'The {node}.nid that was read.',
  405. 'type' => 'int',
  406. 'unsigned' => TRUE,
  407. 'not null' => TRUE,
  408. 'default' => 0,
  409. ),
  410. 'timestamp' => array(
  411. 'description' => 'The Unix timestamp at which the read occurred.',
  412. 'type' => 'int',
  413. 'not null' => TRUE,
  414. 'default' => 0,
  415. ),
  416. ),
  417. 'primary key' => array('uid', 'nid'),
  418. 'indexes' => array(
  419. 'nid' => array('nid'),
  420. ),
  421. );
  422. return $schema;
  423. }
  424. /**
  425. * Implements hook_install().
  426. */
  427. function node_install() {
  428. // Populate the node access table.
  429. db_insert('node_access')
  430. ->fields(array(
  431. 'nid' => 0,
  432. 'gid' => 0,
  433. 'realm' => 'all',
  434. 'grant_view' => 1,
  435. 'grant_update' => 0,
  436. 'grant_delete' => 0,
  437. ))
  438. ->execute();
  439. }
  440. /**
  441. * Implements hook_update_dependencies().
  442. */
  443. function node_update_dependencies() {
  444. // node_update_7006() migrates node data to fields and therefore must run
  445. // after all Field modules have been enabled, which happens in
  446. // system_update_7027(). It also needs to query the {filter_format} table to
  447. // get a list of existing text formats, so it must run after
  448. // filter_update_7000(), which creates that table.
  449. $dependencies['node'][7006] = array(
  450. 'system' => 7027,
  451. 'filter' => 7000,
  452. );
  453. // node_update_7008() migrates role permissions and therefore must run after
  454. // the {role} and {role_permission} tables are properly set up, which happens
  455. // in user_update_7007().
  456. $dependencies['node'][7008] = array(
  457. 'user' => 7007,
  458. );
  459. return $dependencies;
  460. }
  461. /**
  462. * Utility function: fetch the node types directly from the database.
  463. *
  464. * This function is valid for a database schema version 7000.
  465. *
  466. * @ingroup update_api
  467. */
  468. function _update_7000_node_get_types() {
  469. $node_types = db_query('SELECT * FROM {node_type}')->fetchAllAssoc('type', PDO::FETCH_OBJ);
  470. // Create default settings for orphan nodes.
  471. $all_types = db_query('SELECT DISTINCT type FROM {node}')->fetchCol();
  472. $extra_types = array_diff($all_types, array_keys($node_types));
  473. foreach ($extra_types as $type) {
  474. $type_object = new stdClass();
  475. $type_object->type = $type;
  476. // In Drupal 6, whether you have a body field or not is a flag in the node
  477. // type table. If it's enabled, nodes may or may not have an empty string
  478. // for the bodies. As we can't detect what this setting should be in
  479. // Drupal 7 without access to the Drupal 6 node type settings, we assume
  480. // the default, which is to enable the body field.
  481. $type_object->has_body = 1;
  482. $type_object->body_label = 'Body';
  483. $node_types[$type_object->type] = $type_object;
  484. }
  485. return $node_types;
  486. }
  487. /**
  488. * @addtogroup updates-6.x-to-7.x
  489. * @{
  490. */
  491. /**
  492. * Upgrade the node type table and fix node type 'module' attribute to avoid name-space conflicts.
  493. */
  494. function node_update_7000() {
  495. // Rename the module column to base.
  496. db_change_field('node_type', 'module', 'base', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE));
  497. db_add_field('node_type', 'module', array(
  498. 'description' => 'The module defining this node type.',
  499. 'type' => 'varchar',
  500. 'default' => '',
  501. 'length' => 255,
  502. 'not null' => TRUE,
  503. ));
  504. db_add_field('node_type', 'disabled', array(
  505. 'description' => 'A boolean indicating whether the node type is disabled.',
  506. 'type' => 'int',
  507. 'not null' => TRUE,
  508. 'default' => 0,
  509. 'size' => 'tiny'
  510. ));
  511. $modules = db_select('system', 's')
  512. ->fields('s', array('name'))
  513. ->condition('type', 'module');
  514. db_update('node_type')
  515. ->expression('module', 'base')
  516. ->condition('base', $modules, 'IN')
  517. ->execute();
  518. db_update('node_type')
  519. ->fields(array('base' => 'node_content'))
  520. ->condition('base', 'node')
  521. ->execute();
  522. }
  523. /**
  524. * Rename {node_revisions} table to {node_revision}.
  525. */
  526. function node_update_7001() {
  527. db_rename_table('node_revisions', 'node_revision');
  528. }
  529. /**
  530. * Extend the node_promote_status index to include all fields required for the node page query.
  531. */
  532. function node_update_7002() {
  533. db_drop_index('node', 'node_promote_status');
  534. db_add_index('node', 'node_frontpage', array('promote', 'status', 'sticky', 'created'));
  535. }
  536. /**
  537. * Remove the node_counter if the statistics module is uninstalled.
  538. */
  539. function node_update_7003() {
  540. if (drupal_get_installed_schema_version('statistics') == SCHEMA_UNINSTALLED) {
  541. db_drop_table('node_counter');
  542. }
  543. }
  544. /**
  545. * Extend the existing default preview and teaser settings to all node types.
  546. */
  547. function node_update_7004() {
  548. // Get original settings and all types.
  549. $original_length = variable_get('teaser_length', 600);
  550. $original_preview = variable_get('node_preview', 0);
  551. // Map old preview setting to new values order.
  552. $original_preview ? $original_preview = 2 : $original_preview = 1;
  553. node_type_cache_reset();
  554. // Apply original settings to all types.
  555. foreach (_update_7000_node_get_types() as $type => $type_object) {
  556. variable_set('teaser_length_' . $type, $original_length);
  557. variable_set('node_preview_' . $type, $original_preview);
  558. }
  559. // Delete old variable but leave 'teaser_length' for aggregator module upgrade.
  560. variable_del('node_preview');
  561. }
  562. /**
  563. * Add status/comment/promote and sticky columns to the {node_revision} table.
  564. */
  565. function node_update_7005() {
  566. foreach (array('status', 'comment', 'promote', 'sticky') as $column) {
  567. db_add_field('node_revision', $column, array(
  568. 'type' => 'int',
  569. 'not null' => TRUE,
  570. 'default' => 0,
  571. ));
  572. }
  573. }
  574. /**
  575. * Convert body and teaser from node properties to fields, and migrate status/comment/promote and sticky columns to the {node_revision} table.
  576. */
  577. function node_update_7006(&$sandbox) {
  578. $sandbox['#finished'] = 0;
  579. // Get node type info for every invocation.
  580. node_type_cache_reset();
  581. if (!isset($sandbox['total'])) {
  582. // Initial invocation.
  583. // First, create the body field.
  584. $body_field = array(
  585. 'field_name' => 'body',
  586. 'type' => 'text_with_summary',
  587. 'module' => 'text',
  588. 'cardinality' => 1,
  589. 'entity_types' => array('node'),
  590. 'translatable' => TRUE,
  591. );
  592. _update_7000_field_create_field($body_field);
  593. $default_trim_length = variable_get('teaser_length', 600);
  594. // Get node type info, specifically the body field settings.
  595. $node_types = _update_7000_node_get_types();
  596. // Add body field instances for existing node types.
  597. foreach ($node_types as $node_type) {
  598. if ($node_type->has_body) {
  599. $trim_length = variable_get('teaser_length_' . $node_type->type, $default_trim_length);
  600. $instance = array(
  601. 'entity_type' => 'node',
  602. 'bundle' => $node_type->type,
  603. 'label' => $node_type->body_label,
  604. 'description' => isset($node_type->description) ? $node_type->description : '',
  605. 'required' => (isset($node_type->min_word_count) && $node_type->min_word_count > 0) ? 1 : 0,
  606. 'widget' => array(
  607. 'type' => 'text_textarea_with_summary',
  608. 'settings' => array(
  609. 'rows' => 20,
  610. 'summary_rows' => 5,
  611. ),
  612. 'weight' => -4,
  613. 'module' => 'text',
  614. ),
  615. 'settings' => array('display_summary' => TRUE),
  616. 'display' => array(
  617. 'default' => array(
  618. 'label' => 'hidden',
  619. 'type' => 'text_default',
  620. ),
  621. 'teaser' => array(
  622. 'label' => 'hidden',
  623. 'type' => 'text_summary_or_trimmed',
  624. 'trim_length' => $trim_length,
  625. ),
  626. ),
  627. );
  628. _update_7000_field_create_instance($body_field, $instance);
  629. variable_del('teaser_length_' . $node_type->type);
  630. }
  631. // Leave 'teaser_length' variable for aggregator module upgrade.
  632. $sandbox['node_types_info'][$node_type->type] = array(
  633. 'has_body' => $node_type->has_body,
  634. );
  635. }
  636. // Used below when updating the stored text format of each node body.
  637. $sandbox['existing_text_formats'] = db_query("SELECT format FROM {filter_format}")->fetchCol();
  638. // Initialize state for future calls.
  639. $sandbox['last'] = 0;
  640. $sandbox['count'] = 0;
  641. $query = db_select('node', 'n');
  642. $query->join('node_revision', 'nr', 'n.nid = nr.nid');
  643. $sandbox['total'] = $query->countQuery()->execute()->fetchField();
  644. $sandbox['body_field_id'] = $body_field['id'];
  645. }
  646. else {
  647. // Subsequent invocations.
  648. $found = FALSE;
  649. if ($sandbox['total']) {
  650. // Operate on every revision of every node (whee!), in batches.
  651. $batch_size = 200;
  652. $query = db_select('node_revision', 'nr');
  653. $query->innerJoin('node', 'n', 'n.nid = nr.nid');
  654. $query
  655. ->fields('nr', array('nid', 'vid', 'body', 'teaser', 'format'))
  656. ->fields('n', array('type', 'status', 'comment', 'promote', 'sticky', 'language'))
  657. ->condition('nr.vid', $sandbox['last'], '>')
  658. ->orderBy('nr.vid', 'ASC')
  659. ->range(0, $batch_size);
  660. $revisions = $query->execute();
  661. // Load each revision of each node, set up 'body'
  662. // appropriately, and save the node's field data. Note that
  663. // node_load() will not return the body or teaser values from
  664. // {node_revision} because those columns have been removed from the
  665. // schema structure in memory (but not yet from the database),
  666. // so we get the values from the explicit query of the table
  667. // instead.
  668. foreach ($revisions as $revision) {
  669. $found = TRUE;
  670. if ($sandbox['node_types_info'][$revision->type]['has_body']) {
  671. $node = (object) array(
  672. 'nid' => $revision->nid,
  673. 'vid' => $revision->vid,
  674. 'type' => $revision->type,
  675. );
  676. // After node_update_7009() we will always have LANGUAGE_NONE as
  677. // language neutral language code, but here we still have empty
  678. // strings.
  679. $langcode = empty($revision->language) ? LANGUAGE_NONE : $revision->language;
  680. if (!empty($revision->teaser) && $revision->teaser != text_summary($revision->body)) {
  681. $node->body[$langcode][0]['summary'] = $revision->teaser;
  682. }
  683. // Do this after text_summary() above.
  684. $break = '<!--break-->';
  685. if (substr($revision->body, 0, strlen($break)) == $break) {
  686. $revision->body = substr($revision->body, strlen($break));
  687. }
  688. $node->body[$langcode][0]['value'] = $revision->body;
  689. // Update the revision's text format for the changes to the Drupal 7
  690. // filter system. This uses the same kind of logic that occurs, for
  691. // example, in user_update_7010(), but we do this here rather than
  692. // via a separate set of database queries, since we are already
  693. // migrating the data.
  694. if (empty($revision->body) && empty($revision->format)) {
  695. $node->body[$langcode][0]['format'] = NULL;
  696. }
  697. elseif (!in_array($revision->format, $sandbox['existing_text_formats'])) {
  698. $node->body[$langcode][0]['format'] = variable_get('filter_default_format', 1);
  699. }
  700. else {
  701. $node->body[$langcode][0]['format'] = $revision->format;
  702. }
  703. // This is a core update and no contrib modules are enabled yet, so
  704. // we can assume default field storage for a faster update.
  705. _update_7000_field_sql_storage_write('node', $node->type, $node->nid, $node->vid, 'body', $node->body);
  706. }
  707. // Migrate the status columns to the {node_revision} table.
  708. db_update('node_revision')
  709. ->fields(array(
  710. 'status' => $revision->status,
  711. 'comment' => $revision->comment,
  712. 'promote' => $revision->promote,
  713. 'sticky' => $revision->sticky,
  714. ))
  715. ->condition('vid', $revision->vid)
  716. ->execute();
  717. $sandbox['last'] = $revision->vid;
  718. $sandbox['count'] += 1;
  719. }
  720. $sandbox['#finished'] = min(0.99, $sandbox['count'] / $sandbox['total']);
  721. }
  722. if (!$found) {
  723. // All nodes are processed.
  724. // Remove the now-obsolete body info from node_revision.
  725. db_drop_field('node_revision', 'body');
  726. db_drop_field('node_revision', 'teaser');
  727. db_drop_field('node_revision', 'format');
  728. // Remove node_type properties related to the former 'body'.
  729. db_drop_field('node_type', 'has_body');
  730. db_drop_field('node_type', 'body_label');
  731. // We're done.
  732. $sandbox['#finished'] = 1;
  733. }
  734. }
  735. }
  736. /**
  737. * Remove column min_word_count.
  738. */
  739. function node_update_7007() {
  740. db_drop_field('node_type', 'min_word_count');
  741. }
  742. /**
  743. * Split the 'administer nodes' permission from 'access content overview'.
  744. */
  745. function node_update_7008() {
  746. $roles = user_roles(FALSE, 'administer nodes');
  747. foreach ($roles as $rid => $role) {
  748. _update_7000_user_role_grant_permissions($rid, array('access content overview'), 'node');
  749. }
  750. }
  751. /**
  752. * Convert node languages from the empty string to LANGUAGE_NONE.
  753. */
  754. function node_update_7009() {
  755. db_update('node')
  756. ->fields(array('language' => LANGUAGE_NONE))
  757. ->condition('language', '')
  758. ->execute();
  759. }
  760. /**
  761. * Add the {block_node_type} table.
  762. */
  763. function node_update_7010() {
  764. $schema['block_node_type'] = array(
  765. 'description' => 'Sets up display criteria for blocks based on content types',
  766. 'fields' => array(
  767. 'module' => array(
  768. 'type' => 'varchar',
  769. 'length' => 64,
  770. 'not null' => TRUE,
  771. 'description' => "The block's origin module, from {block}.module.",
  772. ),
  773. 'delta' => array(
  774. 'type' => 'varchar',
  775. 'length' => 32,
  776. 'not null' => TRUE,
  777. 'description' => "The block's unique delta within module, from {block}.delta.",
  778. ),
  779. 'type' => array(
  780. 'type' => 'varchar',
  781. 'length' => 32,
  782. 'not null' => TRUE,
  783. 'description' => "The machine-readable name of this type from {node_type}.type.",
  784. ),
  785. ),
  786. 'primary key' => array('module', 'delta', 'type'),
  787. 'indexes' => array(
  788. 'type' => array('type'),
  789. ),
  790. );
  791. db_create_table('block_node_type', $schema['block_node_type']);
  792. }
  793. /**
  794. * @} End of "addtogroup updates-6.x-to-7.x".
  795. */
  796. /**
  797. * @addtogroup updates-7.x-extra
  798. * @{
  799. */
  800. /**
  801. * Update the database from Drupal 6 to match the schema.
  802. */
  803. function node_update_7011() {
  804. // Drop node moderation field.
  805. db_drop_field('node', 'moderate');
  806. db_drop_index('node', 'node_moderate');
  807. // Change {node_revision}.status field to default to 1.
  808. db_change_field('node_revision', 'status', 'status', array(
  809. 'type' => 'int',
  810. 'not null' => TRUE,
  811. 'default' => 1,
  812. ));
  813. // Change {node_type}.module field default.
  814. db_change_field('node_type', 'module', 'module', array(
  815. 'type' => 'varchar',
  816. 'length' => 255,
  817. 'not null' => TRUE,
  818. ));
  819. }
  820. /**
  821. * Switches body fields to untranslatable while upgrading from D6 and makes them language neutral.
  822. */
  823. function node_update_7012() {
  824. // If we are upgrading from D6, then body fields should be set back to
  825. // untranslatable, as D6 did not know about the idea of translating fields,
  826. // but only nodes. If a D7 > D7 update is running we need to skip this update,
  827. // as it is a valid use case to have translatable body fields in this context.
  828. if (variable_get('update_d6', FALSE)) {
  829. // Make node bodies untranslatable: field_update_field() cannot be used
  830. // throughout the upgrade process and we do not have an update counterpart
  831. // for _update_7000_field_create_field(). Hence we are forced to update the
  832. // 'field_config' table directly. This is a safe operation since it is
  833. // being performed while upgrading from D6. Perfoming the same operation
  834. // during a D7 update is highly discouraged.
  835. db_update('field_config')
  836. ->fields(array('translatable' => 0))
  837. ->condition('field_name', 'body')
  838. ->execute();
  839. // Switch field languages to LANGUAGE_NONE, since initially they were
  840. // assigned the node language.
  841. foreach (array('field_data_body', 'field_revision_body') as $table) {
  842. db_update($table)
  843. ->fields(array('language' => LANGUAGE_NONE))
  844. ->execute();
  845. }
  846. node_type_cache_reset();
  847. }
  848. }
  849. /**
  850. * Change {node}.vid default value from 0 to NULL to avoid deadlock issues on MySQL.
  851. */
  852. function node_update_7013() {
  853. db_drop_unique_key('node', 'vid');
  854. db_change_field('node', 'vid', 'vid', array(
  855. 'description' => 'The current {node_revision}.vid version identifier.',
  856. 'type' => 'int',
  857. 'unsigned' => TRUE,
  858. 'not null' => FALSE,
  859. 'default' => NULL,
  860. ));
  861. db_add_unique_key('node', 'vid', array('vid'));
  862. }
  863. /**
  864. * Add an index on {node}.language.
  865. */
  866. function node_update_7014() {
  867. db_add_index('node', 'language', array('language'));
  868. }
  869. /**
  870. * Enable node types that may have been erroneously disabled in Drupal 7.36.
  871. */
  872. function node_update_7015() {
  873. db_update('node_type')
  874. ->fields(array('disabled' => 0))
  875. ->condition('base', 'node_content')
  876. ->execute();
  877. }
  878. /**
  879. * Change {history}.nid to an unsigned int in order to match {node}.nid.
  880. */
  881. function node_update_7016() {
  882. db_drop_primary_key('history');
  883. db_drop_index('history', 'nid');
  884. db_change_field('history', 'nid', 'nid', array(
  885. 'description' => 'The {node}.nid that was read.',
  886. 'type' => 'int',
  887. 'unsigned' => TRUE,
  888. 'not null' => TRUE,
  889. 'default' => 0,
  890. ));
  891. db_add_primary_key('history', array('uid', 'nid'));
  892. db_add_index('history', 'nid', array('nid'));
  893. }
  894. /**
  895. * @} End of "addtogroup updates-7.x-extra".
  896. */