node.install 30 KB

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