node.install 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. 'not null' => TRUE,
  407. 'default' => 0,
  408. ),
  409. 'timestamp' => array(
  410. 'description' => 'The Unix timestamp at which the read occurred.',
  411. 'type' => 'int',
  412. 'not null' => TRUE,
  413. 'default' => 0,
  414. ),
  415. ),
  416. 'primary key' => array('uid', 'nid'),
  417. 'indexes' => array(
  418. 'nid' => array('nid'),
  419. ),
  420. );
  421. return $schema;
  422. }
  423. /**
  424. * Implements hook_install().
  425. */
  426. function node_install() {
  427. // Populate the node access table.
  428. db_insert('node_access')
  429. ->fields(array(
  430. 'nid' => 0,
  431. 'gid' => 0,
  432. 'realm' => 'all',
  433. 'grant_view' => 1,
  434. 'grant_update' => 0,
  435. 'grant_delete' => 0,
  436. ))
  437. ->execute();
  438. }
  439. /**
  440. * Implements hook_update_dependencies().
  441. */
  442. function node_update_dependencies() {
  443. // node_update_7006() migrates node data to fields and therefore must run
  444. // after all Field modules have been enabled, which happens in
  445. // system_update_7027(). It also needs to query the {filter_format} table to
  446. // get a list of existing text formats, so it must run after
  447. // filter_update_7000(), which creates that table.
  448. $dependencies['node'][7006] = array(
  449. 'system' => 7027,
  450. 'filter' => 7000,
  451. );
  452. // node_update_7008() migrates role permissions and therefore must run after
  453. // the {role} and {role_permission} tables are properly set up, which happens
  454. // in user_update_7007().
  455. $dependencies['node'][7008] = array(
  456. 'user' => 7007,
  457. );
  458. return $dependencies;
  459. }
  460. /**
  461. * Utility function: fetch the node types directly from the database.
  462. *
  463. * This function is valid for a database schema version 7000.
  464. *
  465. * @ingroup update_api
  466. */
  467. function _update_7000_node_get_types() {
  468. $node_types = db_query('SELECT * FROM {node_type}')->fetchAllAssoc('type', PDO::FETCH_OBJ);
  469. // Create default settings for orphan nodes.
  470. $all_types = db_query('SELECT DISTINCT type FROM {node}')->fetchCol();
  471. $extra_types = array_diff($all_types, array_keys($node_types));
  472. foreach ($extra_types as $type) {
  473. $type_object = new stdClass();
  474. $type_object->type = $type;
  475. // In Drupal 6, whether you have a body field or not is a flag in the node
  476. // type table. If it's enabled, nodes may or may not have an empty string
  477. // for the bodies. As we can't detect what this setting should be in
  478. // Drupal 7 without access to the Drupal 6 node type settings, we assume
  479. // the default, which is to enable the body field.
  480. $type_object->has_body = 1;
  481. $type_object->body_label = 'Body';
  482. $node_types[$type_object->type] = $type_object;
  483. }
  484. return $node_types;
  485. }
  486. /**
  487. * @addtogroup updates-6.x-to-7.x
  488. * @{
  489. */
  490. /**
  491. * Upgrade the node type table and fix node type 'module' attribute to avoid name-space conflicts.
  492. */
  493. function node_update_7000() {
  494. // Rename the module column to base.
  495. db_change_field('node_type', 'module', 'base', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE));
  496. db_add_field('node_type', 'module', array(
  497. 'description' => 'The module defining this node type.',
  498. 'type' => 'varchar',
  499. 'default' => '',
  500. 'length' => 255,
  501. 'not null' => TRUE,
  502. ));
  503. db_add_field('node_type', 'disabled', array(
  504. 'description' => 'A boolean indicating whether the node type is disabled.',
  505. 'type' => 'int',
  506. 'not null' => TRUE,
  507. 'default' => 0,
  508. 'size' => 'tiny'
  509. ));
  510. $modules = db_select('system', 's')
  511. ->fields('s', array('name'))
  512. ->condition('type', 'module');
  513. db_update('node_type')
  514. ->expression('module', 'base')
  515. ->condition('base', $modules, 'IN')
  516. ->execute();
  517. db_update('node_type')
  518. ->fields(array('base' => 'node_content'))
  519. ->condition('base', 'node')
  520. ->execute();
  521. }
  522. /**
  523. * Rename {node_revisions} table to {node_revision}.
  524. */
  525. function node_update_7001() {
  526. db_rename_table('node_revisions', 'node_revision');
  527. }
  528. /**
  529. * Extend the node_promote_status index to include all fields required for the node page query.
  530. */
  531. function node_update_7002() {
  532. db_drop_index('node', 'node_promote_status');
  533. db_add_index('node', 'node_frontpage', array('promote', 'status', 'sticky', 'created'));
  534. }
  535. /**
  536. * Remove the node_counter if the statistics module is uninstalled.
  537. */
  538. function node_update_7003() {
  539. if (drupal_get_installed_schema_version('statistics') == SCHEMA_UNINSTALLED) {
  540. db_drop_table('node_counter');
  541. }
  542. }
  543. /**
  544. * Extend the existing default preview and teaser settings to all node types.
  545. */
  546. function node_update_7004() {
  547. // Get original settings and all types.
  548. $original_length = variable_get('teaser_length', 600);
  549. $original_preview = variable_get('node_preview', 0);
  550. // Map old preview setting to new values order.
  551. $original_preview ? $original_preview = 2 : $original_preview = 1;
  552. node_type_cache_reset();
  553. // Apply original settings to all types.
  554. foreach (_update_7000_node_get_types() as $type => $type_object) {
  555. variable_set('teaser_length_' . $type, $original_length);
  556. variable_set('node_preview_' . $type, $original_preview);
  557. }
  558. // Delete old variable but leave 'teaser_length' for aggregator module upgrade.
  559. variable_del('node_preview');
  560. }
  561. /**
  562. * Add status/comment/promote and sticky columns to the {node_revision} table.
  563. */
  564. function node_update_7005() {
  565. foreach (array('status', 'comment', 'promote', 'sticky') as $column) {
  566. db_add_field('node_revision', $column, array(
  567. 'type' => 'int',
  568. 'not null' => TRUE,
  569. 'default' => 0,
  570. ));
  571. }
  572. }
  573. /**
  574. * Convert body and teaser from node properties to fields, and migrate status/comment/promote and sticky columns to the {node_revision} table.
  575. */
  576. function node_update_7006(&$sandbox) {
  577. $sandbox['#finished'] = 0;
  578. // Get node type info for every invocation.
  579. node_type_cache_reset();
  580. if (!isset($sandbox['total'])) {
  581. // Initial invocation.
  582. // First, create the body field.
  583. $body_field = array(
  584. 'field_name' => 'body',
  585. 'type' => 'text_with_summary',
  586. 'module' => 'text',
  587. 'cardinality' => 1,
  588. 'entity_types' => array('node'),
  589. 'translatable' => TRUE,
  590. );
  591. _update_7000_field_create_field($body_field);
  592. $default_trim_length = variable_get('teaser_length', 600);
  593. // Get node type info, specifically the body field settings.
  594. $node_types = _update_7000_node_get_types();
  595. // Add body field instances for existing node types.
  596. foreach ($node_types as $node_type) {
  597. if ($node_type->has_body) {
  598. $trim_length = variable_get('teaser_length_' . $node_type->type, $default_trim_length);
  599. $instance = array(
  600. 'entity_type' => 'node',
  601. 'bundle' => $node_type->type,
  602. 'label' => $node_type->body_label,
  603. 'description' => isset($node_type->description) ? $node_type->description : '',
  604. 'required' => (isset($node_type->min_word_count) && $node_type->min_word_count > 0) ? 1 : 0,
  605. 'widget' => array(
  606. 'type' => 'text_textarea_with_summary',
  607. 'settings' => array(
  608. 'rows' => 20,
  609. 'summary_rows' => 5,
  610. ),
  611. 'weight' => -4,
  612. 'module' => 'text',
  613. ),
  614. 'settings' => array('display_summary' => TRUE),
  615. 'display' => array(
  616. 'default' => array(
  617. 'label' => 'hidden',
  618. 'type' => 'text_default',
  619. ),
  620. 'teaser' => array(
  621. 'label' => 'hidden',
  622. 'type' => 'text_summary_or_trimmed',
  623. 'trim_length' => $trim_length,
  624. ),
  625. ),
  626. );
  627. _update_7000_field_create_instance($body_field, $instance);
  628. variable_del('teaser_length_' . $node_type->type);
  629. }
  630. // Leave 'teaser_length' variable for aggregator module upgrade.
  631. $sandbox['node_types_info'][$node_type->type] = array(
  632. 'has_body' => $node_type->has_body,
  633. );
  634. }
  635. // Used below when updating the stored text format of each node body.
  636. $sandbox['existing_text_formats'] = db_query("SELECT format FROM {filter_format}")->fetchCol();
  637. // Initialize state for future calls.
  638. $sandbox['last'] = 0;
  639. $sandbox['count'] = 0;
  640. $query = db_select('node', 'n');
  641. $query->join('node_revision', 'nr', 'n.nid = nr.nid');
  642. $sandbox['total'] = $query->countQuery()->execute()->fetchField();
  643. $sandbox['body_field_id'] = $body_field['id'];
  644. }
  645. else {
  646. // Subsequent invocations.
  647. $found = FALSE;
  648. if ($sandbox['total']) {
  649. // Operate on every revision of every node (whee!), in batches.
  650. $batch_size = 200;
  651. $query = db_select('node_revision', 'nr');
  652. $query->innerJoin('node', 'n', 'n.nid = nr.nid');
  653. $query
  654. ->fields('nr', array('nid', 'vid', 'body', 'teaser', 'format'))
  655. ->fields('n', array('type', 'status', 'comment', 'promote', 'sticky', 'language'))
  656. ->condition('nr.vid', $sandbox['last'], '>')
  657. ->orderBy('nr.vid', 'ASC')
  658. ->range(0, $batch_size);
  659. $revisions = $query->execute();
  660. // Load each revision of each node, set up 'body'
  661. // appropriately, and save the node's field data. Note that
  662. // node_load() will not return the body or teaser values from
  663. // {node_revision} because those columns have been removed from the
  664. // schema structure in memory (but not yet from the database),
  665. // so we get the values from the explicit query of the table
  666. // instead.
  667. foreach ($revisions as $revision) {
  668. $found = TRUE;
  669. if ($sandbox['node_types_info'][$revision->type]['has_body']) {
  670. $node = (object) array(
  671. 'nid' => $revision->nid,
  672. 'vid' => $revision->vid,
  673. 'type' => $revision->type,
  674. );
  675. // After node_update_7009() we will always have LANGUAGE_NONE as
  676. // language neutral language code, but here we still have empty
  677. // strings.
  678. $langcode = empty($revision->language) ? LANGUAGE_NONE : $revision->language;
  679. if (!empty($revision->teaser) && $revision->teaser != text_summary($revision->body)) {
  680. $node->body[$langcode][0]['summary'] = $revision->teaser;
  681. }
  682. // Do this after text_summary() above.
  683. $break = '<!--break-->';
  684. if (substr($revision->body, 0, strlen($break)) == $break) {
  685. $revision->body = substr($revision->body, strlen($break));
  686. }
  687. $node->body[$langcode][0]['value'] = $revision->body;
  688. // Update the revision's text format for the changes to the Drupal 7
  689. // filter system. This uses the same kind of logic that occurs, for
  690. // example, in user_update_7010(), but we do this here rather than
  691. // via a separate set of database queries, since we are already
  692. // migrating the data.
  693. if (empty($revision->body) && empty($revision->format)) {
  694. $node->body[$langcode][0]['format'] = NULL;
  695. }
  696. elseif (!in_array($revision->format, $sandbox['existing_text_formats'])) {
  697. $node->body[$langcode][0]['format'] = variable_get('filter_default_format', 1);
  698. }
  699. else {
  700. $node->body[$langcode][0]['format'] = $revision->format;
  701. }
  702. // This is a core update and no contrib modules are enabled yet, so
  703. // we can assume default field storage for a faster update.
  704. _update_7000_field_sql_storage_write('node', $node->type, $node->nid, $node->vid, 'body', $node->body);
  705. }
  706. // Migrate the status columns to the {node_revision} table.
  707. db_update('node_revision')
  708. ->fields(array(
  709. 'status' => $revision->status,
  710. 'comment' => $revision->comment,
  711. 'promote' => $revision->promote,
  712. 'sticky' => $revision->sticky,
  713. ))
  714. ->condition('vid', $revision->vid)
  715. ->execute();
  716. $sandbox['last'] = $revision->vid;
  717. $sandbox['count'] += 1;
  718. }
  719. $sandbox['#finished'] = min(0.99, $sandbox['count'] / $sandbox['total']);
  720. }
  721. if (!$found) {
  722. // All nodes are processed.
  723. // Remove the now-obsolete body info from node_revision.
  724. db_drop_field('node_revision', 'body');
  725. db_drop_field('node_revision', 'teaser');
  726. db_drop_field('node_revision', 'format');
  727. // Remove node_type properties related to the former 'body'.
  728. db_drop_field('node_type', 'has_body');
  729. db_drop_field('node_type', 'body_label');
  730. // We're done.
  731. $sandbox['#finished'] = 1;
  732. }
  733. }
  734. }
  735. /**
  736. * Remove column min_word_count.
  737. */
  738. function node_update_7007() {
  739. db_drop_field('node_type', 'min_word_count');
  740. }
  741. /**
  742. * Split the 'administer nodes' permission from 'access content overview'.
  743. */
  744. function node_update_7008() {
  745. $roles = user_roles(FALSE, 'administer nodes');
  746. foreach ($roles as $rid => $role) {
  747. _update_7000_user_role_grant_permissions($rid, array('access content overview'), 'node');
  748. }
  749. }
  750. /**
  751. * Convert node languages from the empty string to LANGUAGE_NONE.
  752. */
  753. function node_update_7009() {
  754. db_update('node')
  755. ->fields(array('language' => LANGUAGE_NONE))
  756. ->condition('language', '')
  757. ->execute();
  758. }
  759. /**
  760. * Add the {block_node_type} table.
  761. */
  762. function node_update_7010() {
  763. $schema['block_node_type'] = array(
  764. 'description' => 'Sets up display criteria for blocks based on content types',
  765. 'fields' => array(
  766. 'module' => array(
  767. 'type' => 'varchar',
  768. 'length' => 64,
  769. 'not null' => TRUE,
  770. 'description' => "The block's origin module, from {block}.module.",
  771. ),
  772. 'delta' => array(
  773. 'type' => 'varchar',
  774. 'length' => 32,
  775. 'not null' => TRUE,
  776. 'description' => "The block's unique delta within module, from {block}.delta.",
  777. ),
  778. 'type' => array(
  779. 'type' => 'varchar',
  780. 'length' => 32,
  781. 'not null' => TRUE,
  782. 'description' => "The machine-readable name of this type from {node_type}.type.",
  783. ),
  784. ),
  785. 'primary key' => array('module', 'delta', 'type'),
  786. 'indexes' => array(
  787. 'type' => array('type'),
  788. ),
  789. );
  790. db_create_table('block_node_type', $schema['block_node_type']);
  791. }
  792. /**
  793. * @} End of "addtogroup updates-6.x-to-7.x".
  794. */
  795. /**
  796. * @addtogroup updates-7.x-extra
  797. * @{
  798. */
  799. /**
  800. * Update the database from Drupal 6 to match the schema.
  801. */
  802. function node_update_7011() {
  803. // Drop node moderation field.
  804. db_drop_field('node', 'moderate');
  805. db_drop_index('node', 'node_moderate');
  806. // Change {node_revision}.status field to default to 1.
  807. db_change_field('node_revision', 'status', 'status', array(
  808. 'type' => 'int',
  809. 'not null' => TRUE,
  810. 'default' => 1,
  811. ));
  812. // Change {node_type}.module field default.
  813. db_change_field('node_type', 'module', 'module', array(
  814. 'type' => 'varchar',
  815. 'length' => 255,
  816. 'not null' => TRUE,
  817. ));
  818. }
  819. /**
  820. * Switches body fields to untranslatable while upgrading from D6 and makes them language neutral.
  821. */
  822. function node_update_7012() {
  823. // If we are upgrading from D6, then body fields should be set back to
  824. // untranslatable, as D6 did not know about the idea of translating fields,
  825. // but only nodes. If a D7 > D7 update is running we need to skip this update,
  826. // as it is a valid use case to have translatable body fields in this context.
  827. if (variable_get('update_d6', FALSE)) {
  828. // Make node bodies untranslatable: field_update_field() cannot be used
  829. // throughout the upgrade process and we do not have an update counterpart
  830. // for _update_7000_field_create_field(). Hence we are forced to update the
  831. // 'field_config' table directly. This is a safe operation since it is
  832. // being performed while upgrading from D6. Perfoming the same operation
  833. // during a D7 update is highly discouraged.
  834. db_update('field_config')
  835. ->fields(array('translatable' => 0))
  836. ->condition('field_name', 'body')
  837. ->execute();
  838. // Switch field languages to LANGUAGE_NONE, since initially they were
  839. // assigned the node language.
  840. foreach (array('field_data_body', 'field_revision_body') as $table) {
  841. db_update($table)
  842. ->fields(array('language' => LANGUAGE_NONE))
  843. ->execute();
  844. }
  845. node_type_cache_reset();
  846. }
  847. }
  848. /**
  849. * Change {node}.vid default value from 0 to NULL to avoid deadlock issues on MySQL.
  850. */
  851. function node_update_7013() {
  852. db_drop_unique_key('node', 'vid');
  853. db_change_field('node', 'vid', 'vid', array(
  854. 'description' => 'The current {node_revision}.vid version identifier.',
  855. 'type' => 'int',
  856. 'unsigned' => TRUE,
  857. 'not null' => FALSE,
  858. 'default' => NULL,
  859. ));
  860. db_add_unique_key('node', 'vid', array('vid'));
  861. }
  862. /**
  863. * Add an index on {node}.language.
  864. */
  865. function node_update_7014() {
  866. db_add_index('node', 'language', array('language'));
  867. }
  868. /**
  869. * @} End of "addtogroup updates-7.x-extra".
  870. */