token.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the token module.
  5. */
  6. /**
  7. * Implements hook_requirements().
  8. */
  9. function token_requirements($phase = 'runtime') {
  10. $requirements = array();
  11. $t = get_t();
  12. if ($phase == 'runtime') {
  13. // Check for various token definition problems.
  14. $token_problems = token_get_token_problems();
  15. // Format and display each token problem.
  16. foreach ($token_problems as $problem_key => $problem) {
  17. if (!empty($problem['problems'])) {
  18. $problems = array_unique($problem['problems']);
  19. $problems = array_map('check_plain', $problems);
  20. $token_problems[$problem_key] = $problem['label'] . theme('item_list', array('items' => $problems));
  21. $requirements['token-' . $problem_key] = array(
  22. 'title' => $problem['label'],
  23. 'value' => theme('item_list', array('items' => $problems)),
  24. 'severity' => $problem['severity'],
  25. );
  26. }
  27. }
  28. }
  29. return $requirements;
  30. }
  31. /**
  32. * Implements hook_schema().
  33. */
  34. function token_schema() {
  35. $schema['cache_token'] = drupal_get_schema_unprocessed('system', 'cache');
  36. $schema['cache_token']['description'] = 'Cache table for token information.';
  37. return $schema;
  38. }
  39. /**
  40. * Add the cache_token table.
  41. */
  42. function token_update_7000() {
  43. if (!db_table_exists('cache_token')) {
  44. $schema = drupal_get_schema_unprocessed('system', 'cache');
  45. $schema['description'] = 'Cache table for token information.';
  46. db_create_table('cache_token', $schema);
  47. }
  48. }
  49. /**
  50. * Deprecate and disable the token_actions module.
  51. */
  52. function token_update_7001() {
  53. module_disable(array('token_actions'));
  54. return 'The Token actions module has been deprecated by the updated system module actions that support tokens.';
  55. }
  56. /**
  57. * Migrate old token_actions module actions to system actions.
  58. */
  59. //function token_update_700X() {
  60. // $actions = db_query("SELECT aid, type, callback, parameters, label FROM {actions} WHERE type = 'system' AND callback IN ('token_actions_message_action', 'token_actions_send_email_action', 'token_actions_goto_action')")->fetchAll();
  61. // foreach ($actions as $action) {
  62. // $action->parameters = unserialize($action->parameters);
  63. // foreach ($action->parameters as $key => $value) {
  64. // if (is_string($value)) {
  65. // $action->parameters[$key] = token_update_token_text($value);
  66. // }
  67. // }
  68. // $action->callback = str_replace('token_actions_', '', $action->callback);
  69. // actions_save($action->callback, $action->type, $action->parameters, $action->label, $action->aid);
  70. // }
  71. // return 'Token actions module actions migrated to system module actions. You may still want to verify that the actions were upgraded correctly.';
  72. //}
  73. /**
  74. * Build a list of Drupal 6 tokens and their Drupal 7 token names.
  75. */
  76. function _token_upgrade_token_list() {
  77. $tokens = array(
  78. // Global tokens
  79. 'user-name' => 'current-user:name',
  80. 'user-id' => 'current-user:id',
  81. 'user-mail' => 'current-user:mail',
  82. 'site-url' => 'site:url',
  83. 'site-name' => 'site:name',
  84. 'site-slogan' => 'site:slogan',
  85. 'site-mission' => 'site:mission',
  86. 'site-mail' => 'site:mail',
  87. 'site-date' => 'date:short',
  88. //'site-date-' => '', // Date tokens expanded below
  89. 'current-page-path' => 'current-page:path',
  90. 'current-page-url' => 'current-page:url',
  91. 'page-number' => 'current-page:page-number',
  92. // Comment tokens
  93. 'comment-cid' => 'comment:cid',
  94. 'comment-nid' => 'comment:node:nid',
  95. 'comment-title' => 'comment:title',
  96. 'comment-body' => 'comment:body',
  97. 'comment-author-name' => 'comment:author:name',
  98. 'comment-author-mail' => 'comment:author:mail',
  99. //'comment-body-format' => '',
  100. //'comment-' => '', // Date tokens expanded below
  101. 'comment-node-title' => 'comment:node',
  102. // Node tokens
  103. 'nid' => 'node:nid',
  104. 'type' => 'node:type',
  105. 'type-name' => 'node:type-name',
  106. 'language' => 'node:language',
  107. 'title' => 'node:title',
  108. 'author-uid' => 'node:author:uid',
  109. 'author-name' => 'node:author:name',
  110. 'author-mail' => 'node:author:mail',
  111. 'node_comment_count' => 'node:comment-count',
  112. 'unread_comment_count' => 'node:comment-count-new',
  113. 'log' => 'node:log',
  114. //'' => '', // Date tokens expanded below
  115. //'mod-' => '', // Date tokens expanded below
  116. 'menupath' => 'node:menu-link:parent:path][node:menu-link',
  117. 'menu' => 'node:menu-link:menu-name',
  118. 'menu-link-title' => 'node:menu-link',
  119. 'menu-link-mlid' => 'node:menu-link:mlid',
  120. 'menu-link-plid' => 'node:menu-link:parent:mlid',
  121. //'term' => 'node:term',
  122. //'term-id' => 'node:term:tid',
  123. //'vocab' => 'node:term:vocabulary',
  124. //'vocab-id' => 'node:term:vocabulary:vid',
  125. // Book tokens
  126. //'book' => 'node:book',
  127. //'book_id' => 'node:book:bid',
  128. //'bookpath' => 'node:book:path',
  129. // Taxonomy tokens
  130. 'tid' => 'term:tid',
  131. 'cat' => 'term:name',
  132. 'cat-description' => 'term:description',
  133. 'vid' => 'term:vocabulary:vid',
  134. 'vocab' => 'term:vocabulary',
  135. 'vocab-description' => 'term:vocabulary:description',
  136. // User tokens
  137. 'user' => 'user:name',
  138. 'uid' => 'user:uid',
  139. 'mail' => 'user:mail',
  140. 'reg-date' => 'user:created',
  141. 'reg-since' => 'user:created:since',
  142. //'user-created' => '', // Date tokens expanded below
  143. 'log-date' => 'user:last-login',
  144. 'log-since' => 'user:last-login:since',
  145. //'user-last-login' => '', // Date tokens expanded below
  146. //'date-in-tz' => '',
  147. 'account-url' => 'user:url',
  148. 'account-edit' => 'user:edit-url',
  149. );
  150. // Account for date tokens which need to be expanded.
  151. $tokens += _token_upgrade_token_date_list('site-', 'site:date');
  152. $tokens += _token_upgrade_token_date_list('', 'node:created');
  153. $tokens += _token_upgrade_token_date_list('mod-', 'node:changed');
  154. //$tokens += _token_upgrade_token_date_list('node-revision-', 'node:changed');
  155. $tokens += _token_upgrade_token_date_list('comment-', 'comment:created');
  156. $tokens += _token_upgrade_token_date_list('user-register-', 'user:created');
  157. $tokens += _token_upgrade_token_date_list('user-last-login-', 'user:last-login');
  158. return $tokens;
  159. }
  160. /**
  161. * Build a list of Drupal 6 date tokens and their Drupal 7 token names.
  162. */
  163. function _token_upgrade_token_date_list($old_token, $new_token) {
  164. $tokens = array();
  165. $formats = array(
  166. 'yyyy' => 'Y',
  167. 'yy' => 'y',
  168. 'month' => 'F',
  169. 'mon' => 'M',
  170. 'mm' => 'm',
  171. 'm' => 'n',
  172. 'ww' => 'W',
  173. 'date' => 'N',
  174. 'day' => 'l',
  175. 'ddd' => 'D',
  176. 'dd' => 'd',
  177. 'd' => 'j',
  178. );
  179. foreach ($formats as $token_format => $date_format) {
  180. $tokens[$old_token . $token_format] = "$new_token:custom:$date_format";
  181. }
  182. $tokens[$old_token . 'raw'] = "$new_token:raw";
  183. $tokens[$old_token . 'since'] = "$new_token:since";
  184. return $tokens;
  185. }
  186. /**
  187. * Update a string containing Drupal 6 style tokens to Drupal 7 style tokens.
  188. *
  189. * @param $text
  190. * A string containing tokens.
  191. * @param $updates
  192. * An optional array of Drupal 7 tokens keyed by their Drupal 6 token name.
  193. * The default tokens will be merged into this array. Note neither the old
  194. * or new token names should include the surrounding bracket ([ and ])
  195. * characters.
  196. * @return
  197. * A string with the tokens upgraded
  198. *
  199. * @see _token_upgrade_token_list()
  200. */
  201. function token_update_token_text($text, $updates = array(), $leading = '[', $trailing = ']') {
  202. $updates += _token_upgrade_token_list();
  203. $regex = '/' . preg_quote($leading, '/') . '([^\s]*)' . preg_quote($trailing, '/') . '/';
  204. preg_match_all($regex, $text, $matches);
  205. foreach ($matches[1] as $index => $old_token) {
  206. if (isset($updates[$old_token])) {
  207. $new_token = $updates[$old_token];
  208. $text = str_replace("{$leading}{$old_token}{$trailing}", "[$new_token]", $text);
  209. // Also replace any tokens that have a -raw suffix.
  210. $text = str_replace("{$leading}{$old_token}-raw{$trailing}", "[$new_token]", $text);
  211. }
  212. }
  213. return $text;
  214. }
  215. /**
  216. * Get token problems.
  217. */
  218. function token_get_token_problems() {
  219. // @todo Improve the duplicate checking to report which modules are the offenders.
  220. //$token_info = array();
  221. //foreach (module_implements('token_info') as $module) {
  222. // $module_token_info = module_invoke($module, 'token_info');
  223. // if (in_array($module, _token_core_supported_modules())) {
  224. // $module .= '/token';
  225. // }
  226. // if (isset($module_token_info['types'])) {
  227. // if (is_array($module_token_info['types'])) {
  228. // foreach (array_keys($module_token_info['types']) as $type) {
  229. // if (is_array($module_token_info['types'][$type])) {
  230. // $module_token_info['types'][$type] += array('module' => $module);
  231. // }
  232. // }
  233. // }
  234. // }
  235. // if (isset($module_token_info['tokens'])) {
  236. // if (is_array($module_token_info['tokens'])) {
  237. //
  238. // }
  239. // }
  240. // if (is_array($module_token_info)) {
  241. // $token_info = array_merge_recursive($token_info, $module_token_info);
  242. // }
  243. //}
  244. $token_info = token_info();
  245. $token_problems = array(
  246. 'not-array' => array(
  247. 'label' => t('Tokens or token types not defined as arrays'),
  248. 'severity' => REQUIREMENT_ERROR,
  249. ),
  250. 'missing-info' => array(
  251. 'label' => t('Tokens or token types missing name property'),
  252. 'severity' => REQUIREMENT_WARNING,
  253. ),
  254. 'type-no-tokens' => array(
  255. 'label' => t('Token types do not have any tokens defined'),
  256. 'severity' => REQUIREMENT_INFO,
  257. ),
  258. 'tokens-no-type' => array(
  259. 'label' => t('Token types are not defined but have tokens'),
  260. 'severity' => REQUIREMENT_INFO,
  261. ),
  262. 'duplicate' => array(
  263. 'label' => t('Token or token types are defined by multiple modules'),
  264. 'severity' => REQUIREMENT_ERROR,
  265. ),
  266. );
  267. // Check token types for problems.
  268. foreach ($token_info['types'] as $type => $type_info) {
  269. $real_type = !empty($type_info['type']) ? $type_info['type'] : $type;
  270. if (!is_array($type_info)) {
  271. $token_problems['not-array']['problems'][] = "\$info['types']['$type']";
  272. continue;
  273. }
  274. elseif (!isset($type_info['name'])) {
  275. $token_problems['missing-info']['problems'][] = "\$info['types']['$type']";
  276. }
  277. elseif (is_array($type_info['name'])) {
  278. $token_problems['duplicate']['problems'][] = "\$info['types']['$type']";
  279. }
  280. elseif (empty($token_info['tokens'][$real_type])) {
  281. $token_problems['type-no-tokens']['problems'][] = "\$info['tokens']['$real_type']";
  282. }
  283. }
  284. // Check tokens for problems.
  285. foreach ($token_info['tokens'] as $type => $tokens) {
  286. if (!is_array($tokens)) {
  287. $token_problems['not-array']['problems'][] = "\$info['tokens']['$type']";
  288. continue;
  289. }
  290. else {
  291. foreach (array_keys($tokens) as $token) {
  292. if (!is_array($tokens[$token])) {
  293. $token_problems['not-array']['problems'][] = "\$info['tokens']['$type']['$token']";
  294. continue;
  295. }
  296. elseif (!isset($tokens[$token]['name'])) {
  297. $token_problems['missing-info']['problems'][] = "\$info['tokens']['$type']['$token']";
  298. }
  299. elseif (is_array($tokens[$token]['name'])) {
  300. $token_problems['duplicate']['problems'][] = "\$info['tokens']['$type']['$token']";
  301. }
  302. }
  303. }
  304. if (!isset($token_info['types'][$type])) {
  305. $token_problems['tokens-no-type']['problems'][] = "\$info['types']['$type']";
  306. }
  307. }
  308. return $token_problems;
  309. }