flag.install 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. <?php
  2. /**
  3. * @file
  4. * Flag module install/schema/update hooks.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function flag_schema() {
  10. $schema = array();
  11. $schema['flag'] = array(
  12. 'description' => 'All available flags in the system.',
  13. 'fields' => array(
  14. 'fid' => array(
  15. 'description' => 'The unique ID for this particular flag.',
  16. 'type' => 'serial',
  17. 'size' => 'small',
  18. 'unsigned' => TRUE,
  19. 'not null' => TRUE,
  20. ),
  21. 'entity_type' => array(
  22. 'description' => 'The flag type, such as one of "node", "comment", or "user".',
  23. 'type' => 'varchar',
  24. 'length' => '128',
  25. 'not null' => TRUE,
  26. 'default' => '',
  27. ),
  28. 'name' => array(
  29. 'description' => 'The machine-name for this flag.',
  30. 'type' => 'varchar',
  31. 'length' => '32',
  32. 'not null' => FALSE,
  33. 'default' => '',
  34. ),
  35. 'title' => array(
  36. 'description' => 'The human-readable title for this flag.',
  37. 'type' => 'varchar',
  38. 'length' => '255',
  39. 'not null' => FALSE,
  40. 'default' => '',
  41. ),
  42. 'global' => array(
  43. 'description' => 'Whether this flag state should act as a single toggle to all users across the site.',
  44. 'type' => 'int',
  45. 'size' => 'tiny',
  46. 'not null' => FALSE,
  47. 'default' => 0,
  48. ),
  49. 'options' => array(
  50. 'description' => 'The options and configuration of this flag.',
  51. 'type' => 'text',
  52. 'not null' => FALSE,
  53. ),
  54. ),
  55. 'primary key' => array('fid'),
  56. 'unique keys' => array(
  57. 'name' => array('name'),
  58. ),
  59. );
  60. $schema['flagging'] = array(
  61. 'description' => 'Objects that have been flagged.',
  62. 'fields' => array(
  63. 'flagging_id' => array(
  64. 'description' => 'The unique ID for this particular tag.',
  65. 'type' => 'serial',
  66. 'unsigned' => TRUE,
  67. 'not null' => TRUE,
  68. ),
  69. 'fid' => array(
  70. 'description' => 'The unique flag ID this object has been flagged with, from {flag}.',
  71. 'type' => 'int',
  72. 'size' => 'small',
  73. 'unsigned' => TRUE,
  74. 'not null' => TRUE,
  75. 'default' => 0,
  76. ),
  77. 'entity_type' => array(
  78. 'description' => 'The flag type, eg "node", "comment", "user".',
  79. 'type' => 'varchar',
  80. 'length' => '128',
  81. 'not null' => TRUE,
  82. 'default' => '',
  83. ),
  84. 'entity_id' => array(
  85. 'description' => 'The unique ID of the object, such as either the {cid}, {uid}, or {nid}.',
  86. 'type' => 'int',
  87. 'unsigned' => TRUE,
  88. 'not null' => TRUE,
  89. 'default' => 0,
  90. ),
  91. 'uid' => array(
  92. 'description' => 'The user ID by whom this object was flagged.',
  93. 'type' => 'int',
  94. 'unsigned' => TRUE,
  95. 'not null' => TRUE,
  96. 'default' => 0,
  97. ),
  98. 'sid' => array(
  99. 'description' => "The user's numeric sid from the session_api table.",
  100. 'type' => 'int',
  101. 'unsigned' => TRUE,
  102. 'not null' => TRUE,
  103. 'default' => 0,
  104. ),
  105. 'timestamp' => array(
  106. 'description' => 'The UNIX time stamp representing when the flag was set.',
  107. 'type' => 'int',
  108. 'unsigned' => TRUE,
  109. 'not null' => TRUE,
  110. 'default' => 0,
  111. 'disp-size' => 11,
  112. ),
  113. ),
  114. 'primary key' => array('flagging_id'),
  115. 'unique keys' => array(
  116. 'fid_entity_id_uid_sid' => array('fid', 'entity_id', 'uid', 'sid'),
  117. ),
  118. 'indexes' => array(
  119. 'entity_type_uid_sid' => array('entity_type', 'uid', 'sid'),
  120. 'entity_type_entity_id_uid_sid' => array(
  121. 'entity_type',
  122. 'entity_id',
  123. 'uid',
  124. 'sid',
  125. ),
  126. 'entity_id_fid' => array('entity_id', 'fid'),
  127. ),
  128. );
  129. $schema['flag_types'] = array(
  130. 'description' => 'The entity bundles that are affected by a flag.',
  131. 'fields' => array(
  132. 'fid' => array(
  133. 'description' => 'The unqiue flag ID as defined for the flag in {flag}.',
  134. 'type' => 'int',
  135. 'size' => 'small',
  136. 'unsigned' => TRUE,
  137. 'not null' => TRUE,
  138. 'default' => 0,
  139. ),
  140. 'type' => array(
  141. 'description' => 'The entity bundles that can be flagged by this fid.',
  142. 'type' => 'varchar',
  143. 'length' => '128',
  144. 'not null' => TRUE,
  145. 'default' => '',
  146. ),
  147. ),
  148. 'indexes' => array(
  149. 'fid' => array('fid'),
  150. ),
  151. );
  152. $schema['flag_counts'] = array(
  153. 'description' => 'The number of times an item has been flagged.',
  154. 'fields' => array(
  155. 'fid' => array(
  156. 'type' => 'int',
  157. 'size' => 'small',
  158. 'unsigned' => TRUE,
  159. 'not null' => TRUE,
  160. 'default' => 0,
  161. ),
  162. 'entity_type' => array(
  163. 'description' => 'The flag type, usually one of "node", "comment", "user".',
  164. 'type' => 'varchar',
  165. 'length' => '128',
  166. 'not null' => TRUE,
  167. 'default' => '',
  168. ),
  169. 'entity_id' => array(
  170. 'description' => 'The unique ID of the content, usually either the {cid}, {uid}, or {nid}.',
  171. 'type' => 'int',
  172. 'unsigned' => TRUE,
  173. 'not null' => TRUE,
  174. 'default' => 0,
  175. 'disp-width' => '10',
  176. ),
  177. 'count' => array(
  178. 'description' => 'The number of times this object has been flagged for this flag.',
  179. 'type' => 'int',
  180. 'unsigned' => TRUE,
  181. 'not null' => TRUE,
  182. 'default' => 0,
  183. 'disp-width' => '10',
  184. ),
  185. 'last_updated' => array(
  186. 'description' => 'The UNIX time stamp representing when the flag was last updated.',
  187. 'type' => 'int',
  188. 'unsigned' => TRUE,
  189. 'not null' => TRUE,
  190. 'default' => 0,
  191. 'disp-size' => 11,
  192. ),
  193. ),
  194. 'primary key' => array('fid', 'entity_id'),
  195. 'indexes' => array(
  196. 'fid_entity_type' => array('fid', 'entity_type'),
  197. 'entity_type_entity_id' => array('entity_type', 'entity_id'),
  198. 'fid_count' => array('fid', 'count'),
  199. 'fid_last_updated' => array('fid', 'last_updated'),
  200. ),
  201. );
  202. return $schema;
  203. }
  204. /**
  205. * Implements hook_uninstall().
  206. */
  207. function flag_uninstall() {
  208. $result = db_select('variable', 'v')
  209. ->fields('v', array('name'))
  210. ->condition('name', 'flag_%', 'LIKE')
  211. ->execute();
  212. foreach ($result as $row) {
  213. variable_del($row->name);
  214. }
  215. drupal_set_message(t('Flag has been uninstalled.'));
  216. }
  217. /**
  218. * Implements hook_requirements().
  219. */
  220. function flag_requirements($phase) {
  221. $requirements = array();
  222. $t = get_t();
  223. if ($phase == 'runtime') {
  224. if (module_exists('translation') && !module_exists('translation_helpers')) {
  225. $requirements['flag_translation'] = array(
  226. 'title' => $t('Flag'),
  227. 'severity' => REQUIREMENT_ERROR,
  228. 'description' => $t('To have the flag module work with translations, you need to install and enable the <a href="http://drupal.org/project/translation_helpers">Translation helpers</a> module.'),
  229. 'value' => $t('Translation helpers module not found.'),
  230. );
  231. }
  232. if (module_exists('session_api')) {
  233. if (file_exists('./robots.txt')) {
  234. $flag_path = url('flag') . '/';
  235. // We don't use url() because this may return an absolute URL when
  236. // language negotiation is set to 'domain'.
  237. $flag_path = parse_url($flag_path, PHP_URL_PATH);
  238. $robots_string = 'Disallow: ' . $flag_path;
  239. $contents = file_get_contents('./robots.txt');
  240. if (strpos($contents, $robots_string) === FALSE) {
  241. $requirements['flag_robots'] = array(
  242. 'title' => $t('Flag robots.txt problem'),
  243. 'severity' => REQUIREMENT_WARNING,
  244. 'description' => $t('Flag module may currently be used with anonymous users, however the robots.txt file does not exclude the "@flag-path" path, which may cause search engines to randomly flag and unflag content when they index the site. It is highly recommended to add "@robots-string" to your robots.txt file (located in the root of your Drupal installation).', array('@flag-path' => $flag_path, '@robots-string' => $robots_string)),
  245. 'value' => $t('Search engines flagging content'),
  246. );
  247. }
  248. }
  249. }
  250. }
  251. return $requirements;
  252. }
  253. function flag_update_last_removed() {
  254. return 6004;
  255. }
  256. /**
  257. * Convert role access to have separate "flag" and "unflag" permissions.
  258. */
  259. function flag_update_6200() {
  260. if (db_field_exists('flags', 'roles')) {
  261. $result = db_select('flags', 'f')
  262. ->fields('f')
  263. ->execute();
  264. foreach ($result as $flag) {
  265. $roles = array_filter(explode(',', $flag->roles));
  266. $options = unserialize($flag->options);
  267. $options['roles'] = array(
  268. 'flag' => $roles,
  269. 'unflag' => $roles,
  270. );
  271. db_update('flags')
  272. ->fields(array(
  273. 'options' => serialize($options),
  274. ))
  275. ->condition('fid', $flag->fid)
  276. ->execute();
  277. }
  278. db_drop_field('flags', 'roles');
  279. }
  280. }
  281. /**
  282. * Refine the indexes.
  283. *
  284. * The content type inclusion actually slowed down on unique key. And a count
  285. * index would be helpful for sorting by counts.
  286. */
  287. function flag_update_6201() {
  288. // Remove "content type" from one key, see http://drupal.org/node/612602.
  289. db_drop_unique_key('flag_content', 'fid_content_type_content_id_uid');
  290. db_add_unique_key('flag_content', 'fid_content_id_uid', array(
  291. 'fid',
  292. 'content_id',
  293. 'uid',
  294. ));
  295. // Add a count index, see http://drupal.org/node/489610.
  296. db_add_index('flag_counts', 'count', array('count'));
  297. }
  298. /**
  299. * Add the sid column and unique index on the flag_content table.
  300. */
  301. function flag_update_6202() {
  302. // Drop the keys affected by the addition of the SID column.
  303. db_drop_unique_key('flag_content', 'fid_content_id_uid');
  304. db_drop_index('flag_content', 'content_type_uid');
  305. // Add the column.
  306. db_add_field('flag_content', 'sid', array(
  307. 'type' => 'int',
  308. 'unsigned' => TRUE,
  309. 'not null' => TRUE,
  310. 'default' => 0,
  311. ));
  312. // Re-add the removed keys.
  313. db_add_unique_key('flag_content', 'fid_content_id_uid_sid', array(
  314. 'fid',
  315. 'content_id',
  316. 'uid',
  317. 'sid',
  318. ));
  319. db_add_index('flag_content', 'content_type_uid_sid', array(
  320. 'content_type',
  321. 'uid',
  322. 'sid',
  323. ));
  324. }
  325. /**
  326. * Remove count = 0 rows from the count tables.
  327. */
  328. function flag_update_6203() {
  329. db_delete('flag_counts')
  330. ->condition('count', 0)
  331. ->execute();
  332. }
  333. /**
  334. * Remove "content type" from the flag_counts primary key.
  335. */
  336. function flag_update_6204() {
  337. db_drop_primary_key('flag_counts');
  338. db_add_primary_key('flag_counts', array('fid', 'content_id'));
  339. }
  340. /**
  341. * Provide a better index on the flag_content table including 'uid' and 'sid'.
  342. */
  343. function flag_update_6205() {
  344. // This update has been removed and corrected in flag_update_6206.
  345. // See http://drupal.org/node/1105490.
  346. }
  347. /**
  348. * Correction to flag_update_6205(). Convert unique key to an index.
  349. */
  350. function flag_update_6206() {
  351. // Remove the old index that did not include UID or SID.
  352. if (db_index_exists('flag_content', 'content_type_content_id')) {
  353. db_drop_index('flag_content', 'content_type_content_id');
  354. }
  355. // Remove the erroneous unique key that was added in flag_update_6205().
  356. if (db_index_exists('flag_content', 'content_type_content_id_uid_sid')) {
  357. db_drop_unique_key('flag_content', 'content_type_content_id_uid_sid');
  358. }
  359. db_add_index('flag_content', 'content_type_content_id_uid_sid', array(
  360. 'content_type',
  361. 'content_id',
  362. 'uid',
  363. 'sid',
  364. ));
  365. }
  366. /**
  367. * Adds column last_updated to flag_counts table.
  368. */
  369. function flag_update_6207() {
  370. db_add_field('flag_counts', 'last_updated', array(
  371. 'type' => 'int',
  372. 'unsigned' => TRUE,
  373. 'not null' => TRUE,
  374. 'default' => 0,
  375. 'disp-size' => 11),
  376. array('indexes' => array('last_updated' => array('last_updated'))));
  377. }
  378. /**
  379. * Convert flag_count indexes to include FID for more efficient indexing.
  380. */
  381. function flag_update_6208() {
  382. db_drop_index('flag_counts', 'count');
  383. db_drop_index('flag_counts', 'last_updated');
  384. db_add_index('flag_counts', 'fid_count', array('fid', 'count'));
  385. db_add_index('flag_counts', 'fid_last_updated', array('fid', 'last_updated'));
  386. }
  387. /**
  388. * Clear caches.
  389. */
  390. function flag_update_7201() {
  391. // Do nothing. Update.php is going to clear caches for us.
  392. }
  393. /**
  394. * Clean-up flag records for deleted nodes and comments.
  395. */
  396. function flag_update_7202() {
  397. // These queries can't use db_delete() because that doesn't support a
  398. // subquery: see http://drupal.org/node/1267508.
  399. // Clean-up for nodes.
  400. db_query("DELETE FROM {flag_content} WHERE content_type = 'node' AND NOT EXISTS (SELECT 1 FROM {node} n WHERE content_id = n.nid)");
  401. db_query("DELETE FROM {flag_counts} WHERE content_type = 'node' AND NOT EXISTS (SELECT 1 FROM {node} n WHERE content_id = n.nid)");
  402. // Clean-up for comments. Do not use module_exists() because comment module
  403. // could be disabled.
  404. if (db_table_exists('comment')) {
  405. db_query("DELETE FROM {flag_content} WHERE content_type = 'comment' AND NOT EXISTS (SELECT 1 FROM {comment} c WHERE content_id = c.cid)");
  406. db_query("DELETE FROM {flag_counts} WHERE content_type = 'comment' AND NOT EXISTS (SELECT 1 FROM {comment} c WHERE content_id = c.cid)");
  407. }
  408. }
  409. /**
  410. * Add an index to help with view's flag_handler_relationship when not required.
  411. */
  412. function flag_update_7203() {
  413. // Skip if this index was also added by the 6.x-2.x branch.
  414. if (!db_index_exists('flag_content', 'content_id_fid')) {
  415. db_add_index('flag_content', 'content_id_fid', array('content_id', 'fid'));
  416. }
  417. }
  418. /**
  419. * Rebuild the class registry due to classes moving files.
  420. */
  421. function flag_update_7300() {
  422. registry_rebuild();
  423. }
  424. /**
  425. * Rename {flag_content} table to {flagging} and {flags} table to {flag}.
  426. */
  427. function flag_update_7301() {
  428. db_rename_table('flag_content', 'flagging');
  429. db_rename_table('flags', 'flag');
  430. // A second cache clear appears to be required here...
  431. cache_clear_all();
  432. // ...which in fact isn't enough, as the schema cache appears to need explicit
  433. // clearing to prevent the next updates failing to get the schema for the new
  434. // table names.
  435. drupal_get_schema(NULL, TRUE);
  436. }
  437. /**
  438. * Rename database columns on the {flag} table.
  439. */
  440. function flag_update_7302() {
  441. // No keys or indexes are affected.
  442. // Change field 'content_type' to 'entity_type'.
  443. db_change_field('flag', 'content_type', 'entity_type',
  444. // Spec of the field. Identical to our current hook_schema(): we're not
  445. // changing anything except the name.
  446. array(
  447. 'description' => 'The flag type, such as one of "node", "comment", or "user".',
  448. 'type' => 'varchar',
  449. 'length' => '32',
  450. 'not null' => TRUE,
  451. 'default' => '',
  452. )
  453. // No keys to re-add.
  454. );
  455. }
  456. /**
  457. * Rename database columns on the {flagging} table.
  458. */
  459. function flag_update_7303() {
  460. // Drop affected keys and indexes.
  461. db_drop_unique_key('flagging', 'fid_content_id_uid_sid');
  462. db_drop_index('flagging', 'content_type_uid_sid');
  463. db_drop_index('flagging', 'content_type_content_id_uid_sid');
  464. db_drop_index('flagging', 'content_id_fid');
  465. // Change field 'content_type' to 'entity_type'.
  466. db_change_field('flagging', 'content_type', 'entity_type',
  467. // Spec of the field. Identical to our current hook_schema(): we're not
  468. // changing anything except the name.
  469. array(
  470. 'description' => 'The flag type, eg "node", "comment", "user".',
  471. 'type' => 'varchar',
  472. 'length' => '32',
  473. 'not null' => TRUE,
  474. 'default' => '',
  475. ),
  476. // Keys spec. Some are short-lived, as they are about to be dropped again
  477. // and have hybrid names that refer to 'content_id' still.
  478. array(
  479. 'unique keys' => array(
  480. 'fid_content_id_uid_sid' => array('fid', 'content_id', 'uid', 'sid'),
  481. ),
  482. 'indexes' => array(
  483. 'entity_type_uid_sid' => array('entity_type', 'uid', 'sid'),
  484. 'entity_type_content_id_uid_sid' => array(
  485. 'entity_type',
  486. 'content_id',
  487. 'uid',
  488. 'sid',
  489. ),
  490. 'content_id_fid' => array('content_id', 'fid'),
  491. ),
  492. )
  493. );
  494. // Now we have to drop keys and indexes all over again!
  495. db_drop_unique_key('flagging', 'fid_content_id_uid_sid');
  496. db_drop_index('flagging', 'entity_type_content_id_uid_sid');
  497. db_drop_index('flagging', 'content_id_fid');
  498. // Change field 'content_id' to 'entity_id'.
  499. db_change_field('flagging', 'content_id', 'entity_id',
  500. // Spec of the field. Identical to our current hook_schema(): we're not
  501. // changing anything except the name.
  502. array(
  503. 'description' => 'The unique ID of the content, such as either the {cid}, {uid}, or {nid}.',
  504. 'type' => 'int',
  505. 'unsigned' => TRUE,
  506. 'not null' => TRUE,
  507. 'default' => 0,
  508. ),
  509. // Keys spec. Identical to current hook_schema().
  510. array(
  511. 'unique keys' => array(
  512. 'fid_entity_id_uid_sid' => array('fid', 'entity_id', 'uid', 'sid'),
  513. ),
  514. 'indexes' => array(
  515. 'entity_type_entity_id_uid_sid' => array(
  516. 'entity_type',
  517. 'entity_id',
  518. 'uid',
  519. 'sid',
  520. ),
  521. 'entity_id_fid' => array('entity_id', 'fid'),
  522. ),
  523. )
  524. );
  525. // A serial field must be defined as a key, so make a temporary index on
  526. // 'fcid' so we can safely drop the primary key.
  527. // @see http://drupal.org/node/190027
  528. db_add_index('flagging', 'temp', array('fcid'));
  529. // Drop the primary key so we can rename the field.
  530. db_drop_primary_key('flagging');
  531. // Change field 'fcid' to 'flagging_id'.
  532. db_change_field('flagging', 'fcid', 'flagging_id',
  533. // Spec of the field. Identical to our current hook_schema(): we're not
  534. // changing anything except the name.
  535. array(
  536. 'description' => 'The unique ID for this particular tag.',
  537. 'type' => 'serial',
  538. 'unsigned' => TRUE,
  539. 'not null' => TRUE,
  540. ),
  541. // Keys spec. Identical to current hook_schema().
  542. array(
  543. 'primary key' => array('flagging_id'),
  544. )
  545. );
  546. // Drop our temporary index.
  547. db_drop_index('flagging', 'temp');
  548. cache_clear_all();
  549. }
  550. /**
  551. * Rename database columns on the {flag_counts} table.
  552. */
  553. function flag_update_7304() {
  554. // Drop keys and indexes using 'content_type'.
  555. db_drop_index('flag_counts', 'fid_content_type');
  556. db_drop_index('flag_counts', 'content_type_content_id');
  557. // Change field 'content_type' to 'entity_type'.
  558. db_change_field('flag_counts', 'content_type', 'entity_type',
  559. // Spec of the field. Identical to our current hook_schema(): we're not
  560. // changing anything except the name.
  561. array(
  562. 'description' => 'The flag type, usually one of "node", "comment", "user".',
  563. 'type' => 'varchar',
  564. 'length' => '32',
  565. 'not null' => TRUE,
  566. 'default' => '',
  567. ),
  568. // Keys spec. Some are short-lived, as they are about to be dropped again.
  569. // Note the hybrid names refer to 'content_id' still.
  570. array(
  571. 'indexes' => array(
  572. 'fid_entity_type' => array('fid', 'entity_type'),
  573. 'entity_type_content_id' => array('entity_type', 'content_id'),
  574. ),
  575. )
  576. );
  577. // Now drop keys and indexes using 'content_id'.
  578. db_drop_primary_key('flag_counts');
  579. db_drop_index('flag_counts', 'entity_type_content_id');
  580. // Change field 'content_id' to 'entity_id'.
  581. db_change_field('flag_counts', 'content_id', 'entity_id',
  582. // Spec of the field. Identical to our current hook_schema(): we're not
  583. // changing anything except the name.
  584. array(
  585. 'description' => 'The unique ID of the content, usually either the {cid}, {uid}, or {nid}.',
  586. 'type' => 'int',
  587. 'unsigned' => TRUE,
  588. 'not null' => TRUE,
  589. 'default' => 0,
  590. 'disp-width' => '10',
  591. ),
  592. // Keys spec. Identical to current hook_schema() now we're finished.
  593. array(
  594. 'primary key' => array('fid', 'entity_id'),
  595. 'indexes' => array(
  596. 'entity_type_entity_id' => array('entity_type', 'entity_id'),
  597. ),
  598. )
  599. );
  600. }
  601. /**
  602. * Convert flag roles to permissions.
  603. */
  604. function flag_update_7305() {
  605. // We can't use flag_get_flags() to get all flags to act on, because that
  606. // now looks for user permissions and we want the old roles array to convert.
  607. // Hence we need to get flags directly from the database.
  608. // Flags defined in code are saved in the database by flag_get_flags(), so
  609. // this will get them too, unless the module providing them was *only just*
  610. // installed before update.php was run. This edge case is not covered.
  611. $result = db_query("SELECT name, options FROM {flag}");
  612. $flag_data = $result->fetchAllKeyed();
  613. // Note we don't call hook_flag_alter() because we don't have a complete flag.
  614. // If your custom module does something to flag roles, it is your
  615. // responsibility to handle upgrading your extra role data.
  616. foreach ($flag_data as $flag_name => $flag_options) {
  617. $flag_options = unserialize($flag_options);
  618. $flag_roles = $flag_options['roles'];
  619. foreach ($flag_roles['flag'] as $rid) {
  620. $permission = "flag $flag_name";
  621. user_role_grant_permissions($rid, array($permission));
  622. }
  623. foreach ($flag_roles['unflag'] as $rid) {
  624. $permission = "unflag $flag_name";
  625. user_role_grant_permissions($rid, array($permission));
  626. }
  627. // Save the flag options with the roles array removed.
  628. unset($flag_options['roles']);
  629. db_update('flag')
  630. ->fields(array(
  631. 'options' => serialize($flag_options),
  632. ))
  633. ->condition('name', $flag_name)
  634. ->execute();
  635. }
  636. // Flags in code will now report as overridden because the roles option is no
  637. // longer output. Notify the user that they should update them.
  638. if (count(module_implements('flag_default_flags'))) {
  639. drupal_set_message(t('Flags which are defined in code with hook_flag_default_flags() or Features need to be re-exported.'));
  640. }
  641. // Direct the user to read the change notice, which has more details of how
  642. // access to flag objects has been affected.
  643. return t('Flag roles have been converted to user permissions. Permissions have been granted to each flag based on flag roles. You should review the consequences of this in the <a href="!url">change record</a>.', array(
  644. '!url' => 'http://drupal.org/node/1724256',
  645. ));
  646. }
  647. /**
  648. * Convert flag view modes settings.
  649. */
  650. function flag_update_7306() {
  651. foreach (flag_get_flags() as $flag) {
  652. // Update show_on_teaser property to use new view mode settings.
  653. if (!empty($flag->show_on_teaser)) {
  654. $flag->show_in_links['teaser'] = TRUE;
  655. unset($flag->show_on_teaser);
  656. }
  657. // Update show_on_page property to use new view mode settings.
  658. if (!empty($flag->show_on_page)) {
  659. $flag->show_in_links['full'] = TRUE;
  660. unset($flag->show_on_page);
  661. }
  662. // Update show_on_comment and show_on_entity properties to use new view
  663. // mode settings. Since the old logic was to show on all view modes, do
  664. // that.
  665. if (!empty($flag->show_on_entity) || !empty($flag->show_on_comment)) {
  666. if ($entity_info = entity_get_info($flag->entity_type)) {
  667. foreach ($entity_info['view modes'] as $view_mode => $value) {
  668. $flag->show_in_links[$view_mode] = TRUE;
  669. }
  670. }
  671. unset($flag->show_on_entity, $flag->show_on_comment);
  672. }
  673. $flag->save();
  674. }
  675. }