taxonomy.install 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the taxonomy module.
  5. */
  6. /**
  7. * Implements hook_uninstall().
  8. */
  9. function taxonomy_uninstall() {
  10. // Remove variables.
  11. variable_del('taxonomy_override_selector');
  12. variable_del('taxonomy_terms_per_page_admin');
  13. // Remove taxonomy_term bundles.
  14. $vocabularies = db_query("SELECT machine_name FROM {taxonomy_vocabulary}")->fetchCol();
  15. foreach ($vocabularies as $vocabulary) {
  16. field_attach_delete_bundle('taxonomy_term', $vocabulary);
  17. }
  18. }
  19. /**
  20. * Implements hook_schema().
  21. */
  22. function taxonomy_schema() {
  23. $schema['taxonomy_term_data'] = array(
  24. 'description' => 'Stores term information.',
  25. 'fields' => array(
  26. 'tid' => array(
  27. 'type' => 'serial',
  28. 'unsigned' => TRUE,
  29. 'not null' => TRUE,
  30. 'description' => 'Primary Key: Unique term ID.',
  31. ),
  32. 'vid' => array(
  33. 'type' => 'int',
  34. 'unsigned' => TRUE,
  35. 'not null' => TRUE,
  36. 'default' => 0,
  37. 'description' => 'The {taxonomy_vocabulary}.vid of the vocabulary to which the term is assigned.',
  38. ),
  39. 'name' => array(
  40. 'type' => 'varchar',
  41. 'length' => 255,
  42. 'not null' => TRUE,
  43. 'default' => '',
  44. 'description' => 'The term name.',
  45. 'translatable' => TRUE,
  46. ),
  47. 'description' => array(
  48. 'type' => 'text',
  49. 'not null' => FALSE,
  50. 'size' => 'big',
  51. 'description' => 'A description of the term.',
  52. 'translatable' => TRUE,
  53. ),
  54. 'format' => array(
  55. 'type' => 'varchar',
  56. 'length' => 255,
  57. 'not null' => FALSE,
  58. 'description' => 'The {filter_format}.format of the description.',
  59. ),
  60. 'weight' => array(
  61. 'type' => 'int',
  62. 'not null' => TRUE,
  63. 'default' => 0,
  64. 'description' => 'The weight of this term in relation to other terms.',
  65. ),
  66. ),
  67. 'primary key' => array('tid'),
  68. 'foreign keys' => array(
  69. 'vocabulary' => array(
  70. 'table' => 'taxonomy_vocabulary',
  71. 'columns' => array('vid' => 'vid'),
  72. ),
  73. ),
  74. 'indexes' => array(
  75. 'taxonomy_tree' => array('vid', 'weight', 'name'),
  76. 'vid_name' => array('vid', 'name'),
  77. 'name' => array('name'),
  78. ),
  79. );
  80. $schema['taxonomy_term_hierarchy'] = array(
  81. 'description' => 'Stores the hierarchical relationship between terms.',
  82. 'fields' => array(
  83. 'tid' => array(
  84. 'type' => 'int',
  85. 'unsigned' => TRUE,
  86. 'not null' => TRUE,
  87. 'default' => 0,
  88. 'description' => 'Primary Key: The {taxonomy_term_data}.tid of the term.',
  89. ),
  90. 'parent' => array(
  91. 'type' => 'int',
  92. 'unsigned' => TRUE,
  93. 'not null' => TRUE,
  94. 'default' => 0,
  95. 'description' => "Primary Key: The {taxonomy_term_data}.tid of the term's parent. 0 indicates no parent.",
  96. ),
  97. ),
  98. 'indexes' => array(
  99. 'parent' => array('parent'),
  100. ),
  101. 'foreign keys' => array(
  102. 'taxonomy_term_data' => array(
  103. 'table' => 'taxonomy_term_data',
  104. 'columns' => array('tid' => 'tid'),
  105. ),
  106. ),
  107. 'primary key' => array('tid', 'parent'),
  108. );
  109. $schema['taxonomy_vocabulary'] = array(
  110. 'description' => 'Stores vocabulary information.',
  111. 'fields' => array(
  112. 'vid' => array(
  113. 'type' => 'serial',
  114. 'unsigned' => TRUE,
  115. 'not null' => TRUE,
  116. 'description' => 'Primary Key: Unique vocabulary ID.',
  117. ),
  118. 'name' => array(
  119. 'type' => 'varchar',
  120. 'length' => 255,
  121. 'not null' => TRUE,
  122. 'default' => '',
  123. 'description' => 'Name of the vocabulary.',
  124. 'translatable' => TRUE,
  125. ),
  126. 'machine_name' => array(
  127. 'type' => 'varchar',
  128. 'length' => 255,
  129. 'not null' => TRUE,
  130. 'default' => '',
  131. 'description' => 'The vocabulary machine name.',
  132. ),
  133. 'description' => array(
  134. 'type' => 'text',
  135. 'not null' => FALSE,
  136. 'size' => 'big',
  137. 'description' => 'Description of the vocabulary.',
  138. 'translatable' => TRUE,
  139. ),
  140. 'hierarchy' => array(
  141. 'type' => 'int',
  142. 'unsigned' => TRUE,
  143. 'not null' => TRUE,
  144. 'default' => 0,
  145. 'size' => 'tiny',
  146. 'description' => 'The type of hierarchy allowed within the vocabulary. (0 = disabled, 1 = single, 2 = multiple)',
  147. ),
  148. 'module' => array(
  149. 'type' => 'varchar',
  150. 'length' => 255,
  151. 'not null' => TRUE,
  152. 'default' => '',
  153. 'description' => 'The module which created the vocabulary.',
  154. ),
  155. 'weight' => array(
  156. 'type' => 'int',
  157. 'not null' => TRUE,
  158. 'default' => 0,
  159. 'description' => 'The weight of this vocabulary in relation to other vocabularies.',
  160. ),
  161. ),
  162. 'primary key' => array('vid'),
  163. 'indexes' => array(
  164. 'list' => array('weight', 'name'),
  165. ),
  166. 'unique keys' => array(
  167. 'machine_name' => array('machine_name'),
  168. ),
  169. );
  170. $schema['taxonomy_index'] = array(
  171. 'description' => 'Maintains denormalized information about node/term relationships.',
  172. 'fields' => array(
  173. 'nid' => array(
  174. 'description' => 'The {node}.nid this record tracks.',
  175. 'type' => 'int',
  176. 'unsigned' => TRUE,
  177. 'not null' => TRUE,
  178. 'default' => 0,
  179. ),
  180. 'tid' => array(
  181. 'description' => 'The term ID.',
  182. 'type' => 'int',
  183. 'unsigned' => TRUE,
  184. 'not null' => TRUE,
  185. 'default' => 0,
  186. ),
  187. 'sticky' => array(
  188. 'description' => 'Boolean indicating whether the node is sticky.',
  189. 'type' => 'int',
  190. 'not null' => FALSE,
  191. 'default' => 0,
  192. 'size' => 'tiny',
  193. ),
  194. 'created' => array(
  195. 'description' => 'The Unix timestamp when the node was created.',
  196. 'type' => 'int',
  197. 'not null' => TRUE,
  198. 'default'=> 0,
  199. ),
  200. ),
  201. 'indexes' => array(
  202. 'term_node' => array('tid', 'sticky', 'created'),
  203. 'nid' => array('nid'),
  204. ),
  205. 'foreign keys' => array(
  206. 'tracked_node' => array(
  207. 'table' => 'node',
  208. 'columns' => array('nid' => 'nid'),
  209. ),
  210. 'term' => array(
  211. 'table' => 'taxonomy_term_data',
  212. 'columns' => array('tid' => 'tid'),
  213. ),
  214. ),
  215. );
  216. return $schema;
  217. }
  218. /**
  219. * Implements hook_field_schema().
  220. */
  221. function taxonomy_field_schema($field) {
  222. return array(
  223. 'columns' => array(
  224. 'tid' => array(
  225. 'type' => 'int',
  226. 'unsigned' => TRUE,
  227. 'not null' => FALSE,
  228. ),
  229. ),
  230. 'indexes' => array(
  231. 'tid' => array('tid'),
  232. ),
  233. 'foreign keys' => array(
  234. 'tid' => array(
  235. 'table' => 'taxonomy_term_data',
  236. 'columns' => array('tid' => 'tid'),
  237. ),
  238. ),
  239. );
  240. }
  241. /**
  242. * Implements hook_update_dependencies().
  243. */
  244. function taxonomy_update_dependencies() {
  245. // taxonomy_update_7004() migrates taxonomy term data to fields and therefore
  246. // must run after all Field modules have been enabled, which happens in
  247. // system_update_7027().
  248. $dependencies['taxonomy'][7004] = array(
  249. 'system' => 7027,
  250. );
  251. return $dependencies;
  252. }
  253. /**
  254. * Utility function: get the list of vocabularies directly from the database.
  255. *
  256. * This function is valid for a database schema version 7002.
  257. *
  258. * @ingroup update_api
  259. */
  260. function _update_7002_taxonomy_get_vocabularies() {
  261. return db_query('SELECT v.* FROM {taxonomy_vocabulary} v ORDER BY v.weight, v.name')->fetchAllAssoc('vid', PDO::FETCH_OBJ);
  262. }
  263. /**
  264. * Rename taxonomy tables.
  265. */
  266. function taxonomy_update_7001() {
  267. db_rename_table('term_data', 'taxonomy_term_data');
  268. db_rename_table('term_hierarchy', 'taxonomy_term_hierarchy');
  269. db_rename_table('term_node', 'taxonomy_term_node');
  270. db_rename_table('term_relation', 'taxonomy_term_relation');
  271. db_rename_table('term_synonym', 'taxonomy_term_synonym');
  272. db_rename_table('vocabulary', 'taxonomy_vocabulary');
  273. db_rename_table('vocabulary_node_types', 'taxonomy_vocabulary_node_type');
  274. }
  275. /**
  276. * Add {vocabulary}.machine_name column.
  277. */
  278. function taxonomy_update_7002() {
  279. $field = array(
  280. 'type' => 'varchar',
  281. 'length' => 255,
  282. 'not null' => TRUE,
  283. 'default' => '',
  284. 'description' => 'The vocabulary machine name.',
  285. );
  286. db_add_field('taxonomy_vocabulary', 'machine_name', $field);
  287. // Do a direct query here, rather than calling taxonomy_get_vocabularies(),
  288. // in case Taxonomy module is disabled.
  289. $vids = db_query('SELECT vid FROM {taxonomy_vocabulary}')->fetchCol();
  290. foreach ($vids as $vid) {
  291. $machine_name = 'vocabulary_' . $vid;
  292. db_update('taxonomy_vocabulary')
  293. ->fields(array('machine_name' => $machine_name))
  294. ->condition('vid', $vid)
  295. ->execute();
  296. }
  297. // The machine_name unique key can only be added after we ensure the
  298. // machine_name column contains unique values.
  299. db_add_unique_key('taxonomy_vocabulary', 'machine_name', array('machine_name'));
  300. }
  301. /**
  302. * Remove the related terms setting from vocabularies.
  303. *
  304. * This setting has not been used since Drupal 6. The {taxonomy_relations} table
  305. * itself is retained to allow for data to be upgraded.
  306. */
  307. function taxonomy_update_7003() {
  308. db_drop_field('taxonomy_vocabulary', 'relations');
  309. }
  310. /**
  311. * Move taxonomy vocabulary associations for nodes to fields and field instances.
  312. */
  313. function taxonomy_update_7004() {
  314. $taxonomy_index = array(
  315. 'description' => 'Maintains denormalized information about node/term relationships.',
  316. 'fields' => array(
  317. 'nid' => array(
  318. 'description' => 'The {node}.nid this record tracks.',
  319. 'type' => 'int',
  320. 'unsigned' => TRUE,
  321. 'not null' => TRUE,
  322. 'default' => 0,
  323. ),
  324. 'tid' => array(
  325. 'description' => 'The term ID.',
  326. 'type' => 'int',
  327. 'unsigned' => TRUE,
  328. 'not null' => TRUE,
  329. 'default' => 0,
  330. ),
  331. 'sticky' => array(
  332. 'description' => 'Boolean indicating whether the node is sticky.',
  333. 'type' => 'int',
  334. 'not null' => FALSE,
  335. 'default' => 0,
  336. 'size' => 'tiny',
  337. ),
  338. 'created' => array(
  339. 'description' => 'The Unix timestamp when the node was created.',
  340. 'type' => 'int',
  341. 'unsigned' => TRUE,
  342. 'not null' => TRUE,
  343. 'default'=> 0,
  344. ),
  345. ),
  346. 'indexes' => array(
  347. 'term_node' => array('tid', 'sticky', 'created'),
  348. 'nid' => array('nid'),
  349. ),
  350. 'foreign keys' => array(
  351. 'tracked_node' => array(
  352. 'table' => 'node',
  353. 'columns' => array('nid' => 'nid'),
  354. ),
  355. 'term' => array(
  356. 'table' => 'taxonomy_term_data',
  357. 'columns' => array('tid' => 'tid'),
  358. ),
  359. ),
  360. );
  361. db_create_table('taxonomy_index', $taxonomy_index);
  362. // Use an inline version of Drupal 6 taxonomy_get_vocabularies() here since
  363. // we can no longer rely on $vocabulary->nodes from the API function.
  364. $result = db_query('SELECT v.*, n.type FROM {taxonomy_vocabulary} v LEFT JOIN {taxonomy_vocabulary_node_type} n ON v.vid = n.vid ORDER BY v.weight, v.name');
  365. $vocabularies = array();
  366. foreach ($result as $record) {
  367. // If no node types are associated with a vocabulary, the LEFT JOIN will
  368. // return a NULL value for type.
  369. if (isset($record->type)) {
  370. $node_types[$record->vid][$record->type] = $record->type;
  371. unset($record->type);
  372. $record->nodes = $node_types[$record->vid];
  373. }
  374. elseif (!isset($record->nodes)) {
  375. $record->nodes = array();
  376. }
  377. $vocabularies[$record->vid] = $record;
  378. }
  379. foreach ($vocabularies as $vocabulary) {
  380. $field_name = 'taxonomy_' . $vocabulary->machine_name;
  381. $field = array(
  382. 'field_name' => $field_name,
  383. 'module' => 'taxonomy',
  384. 'type' => 'taxonomy_term_reference',
  385. 'cardinality' => $vocabulary->multiple || $vocabulary->tags ? FIELD_CARDINALITY_UNLIMITED : 1,
  386. 'settings' => array(
  387. 'required' => $vocabulary->required ? TRUE : FALSE,
  388. 'allowed_values' => array(
  389. array(
  390. 'vocabulary' => $vocabulary->machine_name,
  391. 'parent' => 0,
  392. ),
  393. ),
  394. ),
  395. );
  396. _update_7000_field_create_field($field);
  397. foreach ($vocabulary->nodes as $bundle) {
  398. $instance = array(
  399. 'label' => $vocabulary->name,
  400. 'field_name' => $field_name,
  401. 'bundle' => $bundle,
  402. 'entity_type' => 'node',
  403. 'settings' => array(),
  404. 'description' => $vocabulary->help,
  405. 'required' => $vocabulary->required,
  406. 'widget' => array(),
  407. 'display' => array(
  408. 'default' => array(
  409. 'type' => 'taxonomy_term_reference_link',
  410. 'weight' => 10,
  411. ),
  412. 'teaser' => array(
  413. 'type' => 'taxonomy_term_reference_link',
  414. 'weight' => 10,
  415. ),
  416. ),
  417. );
  418. if ($vocabulary->tags) {
  419. $instance['widget'] = array(
  420. 'type' => 'taxonomy_autocomplete',
  421. 'module' => 'taxonomy',
  422. 'settings' => array(
  423. 'size' => 60,
  424. 'autocomplete_path' => 'taxonomy/autocomplete',
  425. ),
  426. );
  427. }
  428. else {
  429. $instance['widget'] = array(
  430. 'type' => 'select',
  431. 'module' => 'options',
  432. 'settings' => array(),
  433. );
  434. }
  435. _update_7000_field_create_instance($field, $instance);
  436. }
  437. }
  438. // Some contrib projects stored term node associations without regard for the
  439. // selections in the taxonomy_vocabulary_node_types table, or have more terms
  440. // for a single node than the vocabulary allowed. We construct the
  441. // taxonomyextra field to store all the extra stuff.
  442. // Allowed values for this extra vocabs field is every vocabulary.
  443. $allowed_values = array();
  444. foreach (_update_7002_taxonomy_get_vocabularies() as $vocabulary) {
  445. $allowed_values[] = array(
  446. 'vocabulary' => $vocabulary->machine_name,
  447. 'parent' => 0,
  448. );
  449. }
  450. $field_name = 'taxonomyextra';
  451. $field = array(
  452. 'field_name' => $field_name,
  453. 'module' => 'taxonomy',
  454. 'type' => 'taxonomy_term_reference',
  455. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  456. 'settings' => array(
  457. 'required' => FALSE,
  458. 'allowed_values' => $allowed_values,
  459. ),
  460. );
  461. _update_7000_field_create_field($field);
  462. foreach (_update_7000_node_get_types() as $bundle) {
  463. $instance = array(
  464. 'label' => 'Taxonomy upgrade extras',
  465. 'field_name' => $field_name,
  466. 'entity_type' => 'node',
  467. 'bundle' => $bundle->type,
  468. 'settings' => array(),
  469. 'description' => 'Debris left over after upgrade from Drupal 6',
  470. 'required' => FALSE,
  471. 'widget' => array(
  472. 'type' => 'taxonomy_autocomplete',
  473. 'module' => 'taxonomy',
  474. 'settings' => array(),
  475. ),
  476. 'display' => array(
  477. 'default' => array(
  478. 'type' => 'taxonomy_term_reference_link',
  479. 'weight' => 10,
  480. ),
  481. 'teaser' => array(
  482. 'type' => 'taxonomy_term_reference_link',
  483. 'weight' => 10,
  484. ),
  485. ),
  486. );
  487. _update_7000_field_create_instance($field, $instance);
  488. }
  489. $fields = array('help', 'multiple', 'required', 'tags');
  490. foreach ($fields as $field) {
  491. db_drop_field('taxonomy_vocabulary', $field);
  492. }
  493. }
  494. /**
  495. * Migrate {taxonomy_term_node} table to field storage.
  496. *
  497. * @todo: This function can possibly be made much faster by wrapping a
  498. * transaction around all the inserts.
  499. */
  500. function taxonomy_update_7005(&$sandbox) {
  501. // $sandbox contents:
  502. // - total: The total number of term_node relationships to migrate.
  503. // - count: The number of term_node relationships that have been
  504. // migrated so far.
  505. // - last: The db_query_range() offset to use when querying
  506. // term_node; this field is incremented in quantities of $batch
  507. // (1000) but at the end of each call to this function, last and
  508. // count are the same.
  509. // - vocabularies: An associative array mapping vocabulary id and node
  510. // type to field name. If a voc id/node type pair does not appear
  511. // in this array but a term_node relationship exists mapping a
  512. // term in voc id to node of that type, the relationship is
  513. // assigned to the taxonomymyextra field which allows terms of all
  514. // vocabularies.
  515. // - cursor[values], cursor[deltas]: The contents of $values and
  516. // $deltas at the end of the previous call to this function. These
  517. // need to be preserved across calls because a single batch of
  518. // 1000 rows from term_node may end in the middle of the terms for
  519. // a single node revision.
  520. //
  521. // $values is the array of values about to be/most recently inserted
  522. // into the SQL data table for the taxonomy_term_reference
  523. // field. Before $values is constructed for each record, the
  524. // $values from the previous insert is checked to see if the two
  525. // records are for the same node revision id; this enables knowing
  526. // when to reset the delta counters which are incremented across all
  527. // terms for a single field on a single revision, but reset for each
  528. // new field and revision.
  529. //
  530. // $deltas is an associative array mapping field name to the number
  531. // of term references stored so far for the current revision, which
  532. // provides the delta value for each term reference data insert. The
  533. // deltas are reset for each new revision.
  534. $conditions = array(
  535. 'type' => 'taxonomy_term_reference',
  536. 'deleted' => 0,
  537. );
  538. $field_info = _update_7000_field_read_fields($conditions, 'field_name');
  539. // This is a multi-pass update. On the first call we need to initialize some
  540. // variables.
  541. if (!isset($sandbox['total'])) {
  542. $sandbox['last'] = 0;
  543. $sandbox['count'] = 0;
  544. // Run the same joins as the query that is used later to retrieve the
  545. // term_node data, this ensures that bad records in that table - for
  546. // tids which aren't in taxonomy_term_data or nids which aren't in {node}
  547. // are not included in the count.
  548. $sandbox['total'] = db_query('SELECT COUNT(*) FROM {taxonomy_term_data} td INNER JOIN {taxonomy_term_node} tn ON td.tid = tn.tid INNER JOIN {node} n ON tn.nid = n.nid LEFT JOIN {node} n2 ON tn.vid = n2.vid')->fetchField();
  549. // Use an inline version of Drupal 6 taxonomy_get_vocabularies() here since
  550. // we can no longer rely on $vocabulary->nodes from the API function.
  551. $result = db_query('SELECT v.vid, v.machine_name, n.type FROM {taxonomy_vocabulary} v INNER JOIN {taxonomy_vocabulary_node_type} n ON v.vid = n.vid');
  552. $vocabularies = array();
  553. foreach ($result as $record) {
  554. // If no node types are associated with a vocabulary, the LEFT JOIN will
  555. // return a NULL value for type.
  556. if (isset($record->type)) {
  557. $vocabularies[$record->vid][$record->type] = 'taxonomy_'. $record->machine_name;
  558. }
  559. }
  560. if (!empty($vocabularies)) {
  561. $sandbox['vocabularies'] = $vocabularies;
  562. }
  563. db_create_table('taxonomy_update_7005', array(
  564. 'description' => 'Stores temporary data for taxonomy_update_7005.',
  565. 'fields' => array(
  566. 'n' => array(
  567. 'description' => 'Preserve order.',
  568. 'type' => 'serial',
  569. 'unsigned' => TRUE,
  570. 'not null' => TRUE,
  571. ),
  572. 'vocab_id' => array(
  573. 'type' => 'int',
  574. 'unsigned' => TRUE,
  575. 'not null' => TRUE,
  576. 'default' => 0,
  577. ),
  578. 'tid' => array(
  579. 'type' => 'int',
  580. 'unsigned' => TRUE,
  581. 'not null' => TRUE,
  582. ),
  583. 'nid' => array(
  584. 'type' => 'int',
  585. 'unsigned' => TRUE,
  586. 'not null' => TRUE,
  587. ),
  588. 'vid' => array(
  589. 'type' => 'int',
  590. 'unsigned' => TRUE,
  591. 'not null' => FALSE,
  592. 'default' => NULL,
  593. ),
  594. 'type' => array(
  595. 'type' => 'varchar',
  596. 'length' => 32,
  597. 'not null' => TRUE,
  598. 'default' => '',
  599. ),
  600. 'created' => array(
  601. 'type' => 'int',
  602. 'not null' => FALSE,
  603. ),
  604. 'sticky' => array(
  605. 'type' => 'int',
  606. 'not null' => FALSE,
  607. ),
  608. 'status' => array(
  609. 'type' => 'int',
  610. 'not null' => FALSE,
  611. ),
  612. 'is_current' => array(
  613. 'type' => 'int',
  614. 'unsigned' => TRUE,
  615. 'not null' => FALSE,
  616. ),
  617. ),
  618. 'primary key' => array('n'),
  619. ));
  620. // Query selects all revisions at once and processes them in revision and
  621. // term weight order.
  622. $query = db_select('taxonomy_term_data', 'td');
  623. // We are migrating term-node relationships. If there are none for a
  624. // term, we do not need the term_data row.
  625. $query->join('taxonomy_term_node', 'tn', 'td.tid = tn.tid');
  626. // If a term-node relationship exists for a nid that does not exist, we
  627. // cannot migrate it as we have no node to relate it to; thus we do not
  628. // need that row from term_node.
  629. $query->join('node', 'n', 'tn.nid = n.nid');
  630. // If the current term-node relationship is for the current revision of
  631. // the node, this left join will match and is_current will be non-NULL
  632. // (we also get the current sticky and created in this case). This
  633. // tells us whether to insert into the current data tables in addition
  634. // to the revision data tables.
  635. $query->leftJoin('node', 'n2', 'tn.vid = n2.vid');
  636. $query->addField('td', 'vid', 'vocab_id');
  637. $query->addField('td', 'tid');
  638. $query->addField('tn', 'nid');
  639. $query->addField('tn', 'vid');
  640. $query->addField('n', 'type');
  641. $query->addField('n2', 'created');
  642. $query->addField('n2', 'sticky');
  643. $query->addField('n2', 'status');
  644. $query->addField('n2', 'nid', 'is_current');
  645. // This query must return a consistent ordering across multiple calls.
  646. // We need them ordered by node vid (since we use that to decide when
  647. // to reset the delta counters) and by term weight so they appear
  648. // within each node in weight order. However, tn.vid,td.weight is not
  649. // guaranteed to be unique, so we add tn.tid as an additional sort key
  650. // because tn.tid,tn.vid is the primary key of the D6 term_node table
  651. // and so is guaranteed unique. Unfortunately it also happens to be in
  652. // the wrong order which is less efficient, but c'est la vie.
  653. $query->orderBy('tn.vid');
  654. $query->orderBy('td.weight');
  655. $query->orderBy('tn.tid');
  656. // Work around a bug in the PostgreSQL driver that would result in fatal
  657. // errors when this subquery is used in the insert query below. See
  658. // https://drupal.org/node/2057693.
  659. $fields = &$query->getFields();
  660. unset($fields['td.weight']);
  661. unset($fields['tn.tid']);
  662. db_insert('taxonomy_update_7005')
  663. ->from($query)
  664. ->execute();
  665. }
  666. else {
  667. // We do each pass in batches of 1000.
  668. $batch = 1000;
  669. $result = db_query_range('SELECT vocab_id, tid, nid, vid, type, created, sticky, status, is_current FROM {taxonomy_update_7005} ORDER BY n', $sandbox['last'], $batch);
  670. if (isset($sandbox['cursor'])) {
  671. $values = $sandbox['cursor']['values'];
  672. $deltas = $sandbox['cursor']['deltas'];
  673. }
  674. else {
  675. $deltas = array();
  676. }
  677. foreach ($result as $record) {
  678. $sandbox['count'] += 1;
  679. // Use the valid field for this vocabulary and node type or use the
  680. // overflow vocabulary if there is no valid field.
  681. $field_name = isset($sandbox['vocabularies'][$record->vocab_id][$record->type]) ? $sandbox['vocabularies'][$record->vocab_id][$record->type] : 'taxonomyextra';
  682. $field = $field_info[$field_name];
  683. // Start deltas from 0, and increment by one for each term attached to a
  684. // node.
  685. if (!isset($deltas[$field_name])) {
  686. $deltas[$field_name] = 0;
  687. }
  688. if (isset($values)) {
  689. // If the last inserted revision_id is the same as the current record,
  690. // use the previous deltas to calculate the next delta.
  691. if ($record->vid == $values[2]) {
  692. // For limited cardinality fields, the delta must not be allowed to
  693. // exceed the cardinality during the update. So ensure that the
  694. // delta about to be inserted is within this limit.
  695. // @see field_default_validate().
  696. if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ($deltas[$field_name] + 1) > $field['cardinality']) {
  697. // For excess values of a single-term vocabulary, switch over to
  698. // the overflow field.
  699. $field_name = 'taxonomyextra';
  700. $field = $field_info[$field_name];
  701. if (!isset($deltas[$field_name])) {
  702. $deltas[$field_name] = 0;
  703. }
  704. }
  705. }
  706. else {
  707. // When the record is a new revision, empty the deltas array.
  708. $deltas = array($field_name => 0);
  709. }
  710. }
  711. // Table and column found in the field's storage details. During upgrades,
  712. // it's always SQL.
  713. $table_name = "field_data_{$field_name}";
  714. $revision_name = "field_revision_{$field_name}";
  715. $value_column = $field_name . '_tid';
  716. // Column names and values in field storage are the same for current and
  717. // revision.
  718. $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'language', 'delta', $value_column);
  719. $values = array('node', $record->nid, $record->vid, $record->type, LANGUAGE_NONE, $deltas[$field_name]++, $record->tid);
  720. // Insert rows into the revision table.
  721. db_insert($revision_name)->fields($columns)->values($values)->execute();
  722. // is_current column is a node ID if this revision is also current.
  723. if ($record->is_current) {
  724. db_insert($table_name)->fields($columns)->values($values)->execute();
  725. // Only insert a record in the taxonomy index if the node is published.
  726. if ($record->status) {
  727. // Update the {taxonomy_index} table.
  728. db_insert('taxonomy_index')
  729. ->fields(array('nid', 'tid', 'sticky', 'created',))
  730. ->values(array($record->nid, $record->tid, $record->sticky, $record->created))
  731. ->execute();
  732. }
  733. }
  734. }
  735. // Store the set of inserted values and the current revision's deltas in the
  736. // sandbox.
  737. $sandbox['cursor'] = array(
  738. 'values' => $values,
  739. 'deltas' => $deltas,
  740. );
  741. $sandbox['last'] += $batch;
  742. }
  743. if ($sandbox['count'] < $sandbox['total']) {
  744. $sandbox['#finished'] = FALSE;
  745. }
  746. else {
  747. db_drop_table('taxonomy_vocabulary_node_type');
  748. db_drop_table('taxonomy_term_node');
  749. // If there are no vocabs, we're done.
  750. db_drop_table('taxonomy_update_7005');
  751. $sandbox['#finished'] = TRUE;
  752. // Determine necessity of taxonomyextras field.
  753. $field = $field_info['taxonomyextra'];
  754. $revision_name = 'field_revision_' . $field['field_name'];
  755. $node_types = db_select($revision_name)->distinct()->fields($revision_name, array('bundle'))
  756. ->execute()->fetchCol();
  757. if (empty($node_types)) {
  758. // Delete the overflow field if there are no rows in the revision table.
  759. _update_7000_field_delete_field('taxonomyextra');
  760. }
  761. else {
  762. // Remove instances which are not actually used.
  763. $bundles = db_query('SELECT bundle FROM {field_config_instance} WHERE field_name = :field_name', array(':field_name' => 'taxonomyextra'))->fetchCol();
  764. $bundles = array_diff($bundles, $node_types);
  765. foreach ($bundles as $bundle) {
  766. _update_7000_field_delete_instance('taxonomyextra', 'node', $bundle);
  767. }
  768. }
  769. }
  770. }
  771. /**
  772. * Add {taxonomy_term_data}.format column.
  773. */
  774. function taxonomy_update_7006() {
  775. db_add_field('taxonomy_term_data', 'format', array(
  776. 'type' => 'int',
  777. 'unsigned' => TRUE,
  778. 'not null' => FALSE,
  779. 'description' => 'The {filter_format}.format of the description.',
  780. ));
  781. }
  782. /**
  783. * Add index on {taxonomy_term_data}.name column to speed up taxonomy_get_term_by_name().
  784. */
  785. function taxonomy_update_7007() {
  786. db_add_index('taxonomy_term_data', 'name', array('name'));
  787. }
  788. /**
  789. * Change the weight columns to normal int.
  790. */
  791. function taxonomy_update_7008() {
  792. db_drop_index('taxonomy_term_data', 'taxonomy_tree');
  793. db_change_field('taxonomy_term_data', 'weight', 'weight', array(
  794. 'type' => 'int',
  795. 'not null' => TRUE,
  796. 'default' => 0,
  797. 'description' => 'The weight of this term in relation to other terms.',
  798. ), array(
  799. 'indexes' => array(
  800. 'taxonomy_tree' => array('vid', 'weight', 'name'),
  801. ),
  802. ));
  803. db_drop_index('taxonomy_vocabulary', 'list');
  804. db_change_field('taxonomy_vocabulary', 'weight', 'weight', array(
  805. 'type' => 'int',
  806. 'not null' => TRUE,
  807. 'default' => 0,
  808. 'description' => 'The weight of this vocabulary in relation to other vocabularies.',
  809. ), array(
  810. 'indexes' => array(
  811. 'list' => array('weight', 'name'),
  812. ),
  813. ));
  814. }
  815. /**
  816. * Change {taxonomy_term_data}.format into varchar.
  817. */
  818. function taxonomy_update_7009() {
  819. db_change_field('taxonomy_term_data', 'format', 'format', array(
  820. 'type' => 'varchar',
  821. 'length' => 255,
  822. 'not null' => FALSE,
  823. 'description' => 'The {filter_format}.format of the description.',
  824. ));
  825. }
  826. /**
  827. * Change {taxonomy_index}.created to support signed int.
  828. */
  829. function taxonomy_update_7010() {
  830. db_change_field('taxonomy_index', 'created', 'created', array(
  831. 'description' => 'The Unix timestamp when the node was created.',
  832. 'type' => 'int',
  833. 'unsigned' => FALSE,
  834. 'not null' => TRUE,
  835. 'default'=> 0,
  836. ));
  837. }
  838. /**
  839. * @addtogroup updates-7.x-extra
  840. * @{
  841. */
  842. /**
  843. * Drop unpublished nodes from the index.
  844. */
  845. function taxonomy_update_7011(&$sandbox) {
  846. // Initialize information needed by the batch update system.
  847. if (!isset($sandbox['progress'])) {
  848. $sandbox['progress'] = 0;
  849. $sandbox['max'] = db_query('SELECT COUNT(DISTINCT n.nid) FROM {node} n INNER JOIN {taxonomy_index} t ON n.nid = t.nid WHERE n.status = :status', array(':status' => NODE_NOT_PUBLISHED))->fetchField();
  850. // If there's no data, don't bother with the extra work.
  851. if (empty($sandbox['max'])) {
  852. return;
  853. }
  854. }
  855. // Process records in groups of 5000.
  856. $limit = 5000;
  857. $nids = db_query_range('SELECT DISTINCT n.nid FROM {node} n INNER JOIN {taxonomy_index} t ON n.nid = t.nid WHERE n.status = :status', 0, $limit, array(':status' => NODE_NOT_PUBLISHED))->fetchCol();
  858. if (!empty($nids)) {
  859. db_delete('taxonomy_index')
  860. ->condition('nid', $nids)
  861. ->execute();
  862. }
  863. // Update our progress information for the batch update.
  864. $sandbox['progress'] += $limit;
  865. // Indicate our current progress to the batch update system, if the update is
  866. // not yet complete.
  867. if ($sandbox['progress'] < $sandbox['max']) {
  868. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  869. }
  870. }
  871. /**
  872. * @} End of "addtogroup updates-7.x-extra".
  873. */