token.install 11 KB

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