taxonomy.install 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. 'widget' => array(
  471. 'type' => 'taxonomy_autocomplete',
  472. 'module' => 'taxonomy',
  473. 'settings' => array(),
  474. ),
  475. 'display' => array(
  476. 'default' => array(
  477. 'type' => 'taxonomy_term_reference_link',
  478. 'weight' => 10,
  479. ),
  480. 'teaser' => array(
  481. 'type' => 'taxonomy_term_reference_link',
  482. 'weight' => 10,
  483. ),
  484. ),
  485. );
  486. _update_7000_field_create_instance($field, $instance);
  487. }
  488. $fields = array('help', 'multiple', 'required', 'tags');
  489. foreach ($fields as $field) {
  490. db_drop_field('taxonomy_vocabulary', $field);
  491. }
  492. }
  493. /**
  494. * Migrate {taxonomy_term_node} table to field storage.
  495. *
  496. * @todo: This function can possibly be made much faster by wrapping a
  497. * transaction around all the inserts.
  498. */
  499. function taxonomy_update_7005(&$sandbox) {
  500. // $sandbox contents:
  501. // - total: The total number of term_node relationships to migrate.
  502. // - count: The number of term_node relationships that have been
  503. // migrated so far.
  504. // - last: The db_query_range() offset to use when querying
  505. // term_node; this field is incremented in quantities of $batch
  506. // (1000) but at the end of each call to this function, last and
  507. // count are the same.
  508. // - vocabularies: An associative array mapping vocabulary id and node
  509. // type to field name. If a voc id/node type pair does not appear
  510. // in this array but a term_node relationship exists mapping a
  511. // term in voc id to node of that type, the relationship is
  512. // assigned to the taxonomymyextra field which allows terms of all
  513. // vocabularies.
  514. // - cursor[values], cursor[deltas]: The contents of $values and
  515. // $deltas at the end of the previous call to this function. These
  516. // need to be preserved across calls because a single batch of
  517. // 1000 rows from term_node may end in the middle of the terms for
  518. // a single node revision.
  519. //
  520. // $values is the array of values about to be/most recently inserted
  521. // into the SQL data table for the taxonomy_term_reference
  522. // field. Before $values is constructed for each record, the
  523. // $values from the previous insert is checked to see if the two
  524. // records are for the same node revision id; this enables knowing
  525. // when to reset the delta counters which are incremented across all
  526. // terms for a single field on a single revision, but reset for each
  527. // new field and revision.
  528. //
  529. // $deltas is an associative array mapping field name to the number
  530. // of term references stored so far for the current revision, which
  531. // provides the delta value for each term reference data insert. The
  532. // deltas are reset for each new revision.
  533. $conditions = array(
  534. 'type' => 'taxonomy_term_reference',
  535. 'deleted' => 0,
  536. );
  537. $field_info = _update_7000_field_read_fields($conditions, 'field_name');
  538. // This is a multi-pass update. On the first call we need to initialize some
  539. // variables.
  540. if (!isset($sandbox['total'])) {
  541. $sandbox['last'] = 0;
  542. $sandbox['count'] = 0;
  543. // Run the same joins as the query that is used later to retrieve the
  544. // term_node data, this ensures that bad records in that table - for
  545. // tids which aren't in taxonomy_term_data or nids which aren't in {node}
  546. // are not included in the count.
  547. $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();
  548. // Use an inline version of Drupal 6 taxonomy_get_vocabularies() here since
  549. // we can no longer rely on $vocabulary->nodes from the API function.
  550. $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');
  551. $vocabularies = array();
  552. foreach ($result as $record) {
  553. // If no node types are associated with a vocabulary, the LEFT JOIN will
  554. // return a NULL value for type.
  555. if (isset($record->type)) {
  556. $vocabularies[$record->vid][$record->type] = 'taxonomy_'. $record->machine_name;
  557. }
  558. }
  559. if (!empty($vocabularies)) {
  560. $sandbox['vocabularies'] = $vocabularies;
  561. }
  562. db_create_table('taxonomy_update_7005', array(
  563. 'description' => 'Stores temporary data for taxonomy_update_7005.',
  564. 'fields' => array(
  565. 'n' => array(
  566. 'description' => 'Preserve order.',
  567. 'type' => 'serial',
  568. 'unsigned' => TRUE,
  569. 'not null' => TRUE,
  570. ),
  571. 'vocab_id' => array(
  572. 'type' => 'int',
  573. 'unsigned' => TRUE,
  574. 'not null' => TRUE,
  575. 'default' => 0,
  576. ),
  577. 'tid' => array(
  578. 'type' => 'int',
  579. 'unsigned' => TRUE,
  580. 'not null' => TRUE,
  581. ),
  582. 'nid' => array(
  583. 'type' => 'int',
  584. 'unsigned' => TRUE,
  585. 'not null' => TRUE,
  586. ),
  587. 'vid' => array(
  588. 'type' => 'int',
  589. 'unsigned' => TRUE,
  590. 'not null' => FALSE,
  591. 'default' => NULL,
  592. ),
  593. 'type' => array(
  594. 'type' => 'varchar',
  595. 'length' => 32,
  596. 'not null' => TRUE,
  597. 'default' => '',
  598. ),
  599. 'created' => array(
  600. 'type' => 'int',
  601. 'not null' => FALSE,
  602. ),
  603. 'sticky' => array(
  604. 'type' => 'int',
  605. 'not null' => FALSE,
  606. ),
  607. 'status' => array(
  608. 'type' => 'int',
  609. 'not null' => FALSE,
  610. ),
  611. 'is_current' => array(
  612. 'type' => 'int',
  613. 'unsigned' => TRUE,
  614. 'not null' => FALSE,
  615. ),
  616. ),
  617. 'primary key' => array('n'),
  618. ));
  619. // Query selects all revisions at once and processes them in revision and
  620. // term weight order.
  621. $query = db_select('taxonomy_term_data', 'td');
  622. // We are migrating term-node relationships. If there are none for a
  623. // term, we do not need the term_data row.
  624. $query->join('taxonomy_term_node', 'tn', 'td.tid = tn.tid');
  625. // If a term-node relationship exists for a nid that does not exist, we
  626. // cannot migrate it as we have no node to relate it to; thus we do not
  627. // need that row from term_node.
  628. $query->join('node', 'n', 'tn.nid = n.nid');
  629. // If the current term-node relationship is for the current revision of
  630. // the node, this left join will match and is_current will be non-NULL
  631. // (we also get the current sticky and created in this case). This
  632. // tells us whether to insert into the current data tables in addition
  633. // to the revision data tables.
  634. $query->leftJoin('node', 'n2', 'tn.vid = n2.vid');
  635. $query->addField('td', 'vid', 'vocab_id');
  636. $query->addField('td', 'tid');
  637. $query->addField('tn', 'nid');
  638. $query->addField('tn', 'vid');
  639. $query->addField('n', 'type');
  640. $query->addField('n2', 'created');
  641. $query->addField('n2', 'sticky');
  642. $query->addField('n2', 'status');
  643. $query->addField('n2', 'nid', 'is_current');
  644. // This query must return a consistent ordering across multiple calls.
  645. // We need them ordered by node vid (since we use that to decide when
  646. // to reset the delta counters) and by term weight so they appear
  647. // within each node in weight order. However, tn.vid,td.weight is not
  648. // guaranteed to be unique, so we add tn.tid as an additional sort key
  649. // because tn.tid,tn.vid is the primary key of the D6 term_node table
  650. // and so is guaranteed unique. Unfortunately it also happens to be in
  651. // the wrong order which is less efficient, but c'est la vie.
  652. $query->orderBy('tn.vid');
  653. $query->orderBy('td.weight');
  654. $query->orderBy('tn.tid');
  655. // Work around a bug in the PostgreSQL driver that would result in fatal
  656. // errors when this subquery is used in the insert query below. See
  657. // https://drupal.org/node/2057693.
  658. $fields = &$query->getFields();
  659. unset($fields['td.weight']);
  660. unset($fields['tn.tid']);
  661. db_insert('taxonomy_update_7005')
  662. ->from($query)
  663. ->execute();
  664. }
  665. else {
  666. // We do each pass in batches of 1000.
  667. $batch = 1000;
  668. $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);
  669. if (isset($sandbox['cursor'])) {
  670. $values = $sandbox['cursor']['values'];
  671. $deltas = $sandbox['cursor']['deltas'];
  672. }
  673. else {
  674. $deltas = array();
  675. }
  676. foreach ($result as $record) {
  677. $sandbox['count'] += 1;
  678. // Use the valid field for this vocabulary and node type or use the
  679. // overflow vocabulary if there is no valid field.
  680. $field_name = isset($sandbox['vocabularies'][$record->vocab_id][$record->type]) ? $sandbox['vocabularies'][$record->vocab_id][$record->type] : 'taxonomyextra';
  681. $field = $field_info[$field_name];
  682. // Start deltas from 0, and increment by one for each term attached to a
  683. // node.
  684. if (!isset($deltas[$field_name])) {
  685. $deltas[$field_name] = 0;
  686. }
  687. if (isset($values)) {
  688. // If the last inserted revision_id is the same as the current record,
  689. // use the previous deltas to calculate the next delta.
  690. if ($record->vid == $values[2]) {
  691. // For limited cardinality fields, the delta must not be allowed to
  692. // exceed the cardinality during the update. So ensure that the
  693. // delta about to be inserted is within this limit.
  694. // @see field_default_validate().
  695. if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ($deltas[$field_name] + 1) > $field['cardinality']) {
  696. // For excess values of a single-term vocabulary, switch over to
  697. // the overflow field.
  698. $field_name = 'taxonomyextra';
  699. $field = $field_info[$field_name];
  700. if (!isset($deltas[$field_name])) {
  701. $deltas[$field_name] = 0;
  702. }
  703. }
  704. }
  705. else {
  706. // When the record is a new revision, empty the deltas array.
  707. $deltas = array($field_name => 0);
  708. }
  709. }
  710. // Table and column found in the field's storage details. During upgrades,
  711. // it's always SQL.
  712. $table_name = "field_data_{$field_name}";
  713. $revision_name = "field_revision_{$field_name}";
  714. $value_column = $field_name . '_tid';
  715. // Column names and values in field storage are the same for current and
  716. // revision.
  717. $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'language', 'delta', $value_column);
  718. $values = array('node', $record->nid, $record->vid, $record->type, LANGUAGE_NONE, $deltas[$field_name]++, $record->tid);
  719. // Insert rows into the revision table.
  720. db_insert($revision_name)->fields($columns)->values($values)->execute();
  721. // is_current column is a node ID if this revision is also current.
  722. if ($record->is_current) {
  723. db_insert($table_name)->fields($columns)->values($values)->execute();
  724. // Only insert a record in the taxonomy index if the node is published.
  725. if ($record->status) {
  726. // Update the {taxonomy_index} table.
  727. db_insert('taxonomy_index')
  728. ->fields(array('nid', 'tid', 'sticky', 'created',))
  729. ->values(array($record->nid, $record->tid, $record->sticky, $record->created))
  730. ->execute();
  731. }
  732. }
  733. }
  734. // Store the set of inserted values and the current revision's deltas in the
  735. // sandbox.
  736. $sandbox['cursor'] = array(
  737. 'values' => $values,
  738. 'deltas' => $deltas,
  739. );
  740. $sandbox['last'] += $batch;
  741. }
  742. if ($sandbox['count'] < $sandbox['total']) {
  743. $sandbox['#finished'] = FALSE;
  744. }
  745. else {
  746. db_drop_table('taxonomy_vocabulary_node_type');
  747. db_drop_table('taxonomy_term_node');
  748. // If there are no vocabs, we're done.
  749. db_drop_table('taxonomy_update_7005');
  750. $sandbox['#finished'] = TRUE;
  751. // Determine necessity of taxonomyextras field.
  752. $field = $field_info['taxonomyextra'];
  753. $revision_name = 'field_revision_' . $field['field_name'];
  754. $node_types = db_select($revision_name)->distinct()->fields($revision_name, array('bundle'))
  755. ->execute()->fetchCol();
  756. if (empty($node_types)) {
  757. // Delete the overflow field if there are no rows in the revision table.
  758. _update_7000_field_delete_field('taxonomyextra');
  759. }
  760. else {
  761. // Remove instances which are not actually used.
  762. $bundles = db_query('SELECT bundle FROM {field_config_instance} WHERE field_name = :field_name', array(':field_name' => 'taxonomyextra'))->fetchCol();
  763. $bundles = array_diff($bundles, $node_types);
  764. foreach ($bundles as $bundle) {
  765. _update_7000_field_delete_instance('taxonomyextra', 'node', $bundle);
  766. }
  767. }
  768. }
  769. }
  770. /**
  771. * Add {taxonomy_term_data}.format column.
  772. */
  773. function taxonomy_update_7006() {
  774. db_add_field('taxonomy_term_data', 'format', array(
  775. 'type' => 'int',
  776. 'unsigned' => TRUE,
  777. 'not null' => FALSE,
  778. 'description' => 'The {filter_format}.format of the description.',
  779. ));
  780. }
  781. /**
  782. * Add index on {taxonomy_term_data}.name column to speed up taxonomy_get_term_by_name().
  783. */
  784. function taxonomy_update_7007() {
  785. db_add_index('taxonomy_term_data', 'name', array('name'));
  786. }
  787. /**
  788. * Change the weight columns to normal int.
  789. */
  790. function taxonomy_update_7008() {
  791. db_drop_index('taxonomy_term_data', 'taxonomy_tree');
  792. db_change_field('taxonomy_term_data', 'weight', 'weight', array(
  793. 'type' => 'int',
  794. 'not null' => TRUE,
  795. 'default' => 0,
  796. 'description' => 'The weight of this term in relation to other terms.',
  797. ), array(
  798. 'indexes' => array(
  799. 'taxonomy_tree' => array('vid', 'weight', 'name'),
  800. ),
  801. ));
  802. db_drop_index('taxonomy_vocabulary', 'list');
  803. db_change_field('taxonomy_vocabulary', 'weight', 'weight', array(
  804. 'type' => 'int',
  805. 'not null' => TRUE,
  806. 'default' => 0,
  807. 'description' => 'The weight of this vocabulary in relation to other vocabularies.',
  808. ), array(
  809. 'indexes' => array(
  810. 'list' => array('weight', 'name'),
  811. ),
  812. ));
  813. }
  814. /**
  815. * Change {taxonomy_term_data}.format into varchar.
  816. */
  817. function taxonomy_update_7009() {
  818. db_change_field('taxonomy_term_data', 'format', 'format', array(
  819. 'type' => 'varchar',
  820. 'length' => 255,
  821. 'not null' => FALSE,
  822. 'description' => 'The {filter_format}.format of the description.',
  823. ));
  824. }
  825. /**
  826. * Change {taxonomy_index}.created to support signed int.
  827. */
  828. function taxonomy_update_7010() {
  829. db_change_field('taxonomy_index', 'created', 'created', array(
  830. 'description' => 'The Unix timestamp when the node was created.',
  831. 'type' => 'int',
  832. 'unsigned' => FALSE,
  833. 'not null' => TRUE,
  834. 'default'=> 0,
  835. ));
  836. }
  837. /**
  838. * @addtogroup updates-7.x-extra
  839. * @{
  840. */
  841. /**
  842. * Drop unpublished nodes from the index.
  843. */
  844. function taxonomy_update_7011(&$sandbox) {
  845. // Initialize information needed by the batch update system.
  846. if (!isset($sandbox['progress'])) {
  847. $sandbox['progress'] = 0;
  848. $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();
  849. // If there's no data, don't bother with the extra work.
  850. if (empty($sandbox['max'])) {
  851. return;
  852. }
  853. }
  854. // Process records in groups of 5000.
  855. $limit = 5000;
  856. $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();
  857. if (!empty($nids)) {
  858. db_delete('taxonomy_index')
  859. ->condition('nid', $nids)
  860. ->execute();
  861. }
  862. // Update our progress information for the batch update.
  863. $sandbox['progress'] += $limit;
  864. // Indicate our current progress to the batch update system, if the update is
  865. // not yet complete.
  866. if ($sandbox['progress'] < $sandbox['max']) {
  867. $sandbox['#finished'] = $sandbox['progress'] / $sandbox['max'];
  868. }
  869. }
  870. /**
  871. * @} End of "addtogroup updates-7.x-extra".
  872. */