Projet

Général

Profil

Paste
Télécharger (11,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / token / token.install @ d756b39a

1
<?php
2

    
3
/**
4
 * @file
5
 * Install, update and uninstall functions for the token module.
6
 */
7

    
8
/**
9
 * Implements hook_requirements().
10
 */
11
function token_requirements($phase = 'runtime') {
12
  $requirements = array();
13
  $t = get_t();
14

    
15
  if ($phase == 'runtime') {
16
    // Check for various token definition problems.
17
    $token_problems = token_get_token_problems();
18
    // Format and display each token problem.
19
    foreach ($token_problems as $problem_key => $problem) {
20
      if (!empty($problem['problems'])) {
21
        $problems = array_unique($problem['problems']);
22
        $problems = array_map('check_plain', $problems);
23
        $token_problems[$problem_key] = $problem['label'] . theme('item_list', array('items' => $problems));
24
      }
25
      else {
26
        unset($token_problems[$problem_key]);
27
      }
28
    }
29
    if (!empty($token_problems)) {
30
      $requirements['token_problems'] = array(
31
        'title' => $t('Tokens'),
32
        'value' => $t('Problems detected'),
33
        'severity' => REQUIREMENT_WARNING,
34
        'description' => '<p>' . implode('</p><p>', $token_problems) . '</p>', //theme('item_list', array('items' => $token_problems)),
35
      );
36
    }
37
  }
38

    
39
  return $requirements;
40
}
41

    
42
/**
43
 * Implements hook_schema().
44
 */
45
function token_schema() {
46
  $schema['cache_token'] = drupal_get_schema_unprocessed('system', 'cache');
47
  $schema['cache_token']['description'] = 'Cache table for token information.';
48
  return $schema;
49
}
50

    
51
/**
52
 * Add the cache_token table.
53
 */
54
function token_update_7000() {
55
  if (!db_table_exists('cache_token')) {
56
    $schema = drupal_get_schema_unprocessed('system', 'cache');
57
    $schema['description'] = 'Cache table for token information.';
58
    db_create_table('cache_token', $schema);
59
  }
60
}
61

    
62
/**
63
 * Deprecate and disable the token_actions module.
64
 */
65
function token_update_7001() {
66
  module_disable(array('token_actions'));
67
  return 'The Token actions module has been deprecated by the updated system module actions that support tokens.';
68
}
69

    
70
/**
71
 * Migrate old token_actions module actions to system actions.
72
 */
73
//function token_update_700X() {
74
//  $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();
75
//  foreach ($actions as $action) {
76
//    $action->parameters = unserialize($action->parameters);
77
//    foreach ($action->parameters as $key => $value) {
78
//      if (is_string($value)) {
79
//        $action->parameters[$key] = token_update_token_text($value);
80
//      }
81
//    }
82
//    $action->callback = str_replace('token_actions_', '', $action->callback);
83
//    actions_save($action->callback, $action->type, $action->parameters, $action->label, $action->aid);
84
//  }
85
//  return 'Token actions module actions migrated to system module actions. You may still want to verify that the actions were upgraded correctly.';
86
//}
87

    
88
/**
89
 * Build a list of Drupal 6 tokens and their Drupal 7 token names.
90
 */
91
function _token_upgrade_token_list() {
92
  $tokens = array(
93
    // Global tokens
94
    'user-name' => 'current-user:name',
95
    'user-id' => 'current-user:id',
96
    'user-mail' => 'current-user:mail',
97
    'site-url' => 'site:url',
98
    'site-name' => 'site:name',
99
    'site-slogan' => 'site:slogan',
100
    'site-mission' => 'site:mission',
101
    'site-mail' => 'site:mail',
102
    'site-date' => 'date:short',
103
    //'site-date-' => '', // Date tokens expanded below
104
    'current-page-path' => 'current-page:path',
105
    'current-page-url' => 'current-page:url',
106
    'page-number' => 'current-page:page-number',
107

    
108
    // Comment tokens
109
    'comment-cid' => 'comment:cid',
110
    'comment-nid' => 'comment:node:nid',
111
    'comment-title' => 'comment:title',
112
    'comment-body' => 'comment:body',
113
    'comment-author-name' => 'comment:author:name',
114
    'comment-author-mail' => 'comment:author:mail',
115
    //'comment-body-format' => '',
116
    //'comment-' => '', // Date tokens expanded below
117
    'comment-node-title' => 'comment:node',
118

    
119
    // Node tokens
120
    'nid' => 'node:nid',
121
    'type' => 'node:type',
122
    'type-name' => 'node:type-name',
123
    'language' => 'node:language',
124
    'title' => 'node:title',
125
    'author-uid' => 'node:author:uid',
126
    'author-name' => 'node:author:name',
127
    'author-mail' => 'node:author:mail',
128
    'node_comment_count' => 'node:comment-count',
129
    'unread_comment_count' => 'node:comment-count-new',
130
    'log' => 'node:log',
131
    //'' => '', // Date tokens expanded below
132
    //'mod-' => '', // Date tokens expanded below
133
    'menupath' => 'node:menu-link:parent:path][node:menu-link',
134
    'menu' => 'node:menu-link:menu-name',
135
    'menu-link-title' => 'node:menu-link',
136
    'menu-link-mlid' => 'node:menu-link:mlid',
137
    'menu-link-plid' => 'node:menu-link:parent:mlid',
138
    //'term' => 'node:term',
139
    //'term-id' => 'node:term:tid',
140
    //'vocab' => 'node:term:vocabulary',
141
    //'vocab-id' => 'node:term:vocabulary:vid',
142

    
143
    // Book tokens
144
    //'book' => 'node:book',
145
    //'book_id' => 'node:book:bid',
146
    //'bookpath' => 'node:book:path',
147

    
148
    // Taxonomy tokens
149
    'tid' => 'term:tid',
150
    'cat' => 'term:name',
151
    'cat-description' => 'term:description',
152
    'vid' => 'term:vocabulary:vid',
153
    'vocab' => 'term:vocabulary',
154
    'vocab-description' => 'term:vocabulary:description',
155

    
156
    // User tokens
157
    'user' => 'user:name',
158
    'uid' => 'user:uid',
159
    'mail' => 'user:mail',
160
    'reg-date' => 'user:created',
161
    'reg-since' => 'user:created:since',
162
    //'user-created' => '', // Date tokens expanded below
163
    'log-date' => 'user:last-login',
164
    'log-since' => 'user:last-login:since',
165
    //'user-last-login' => '', // Date tokens expanded below
166
    //'date-in-tz' => '',
167
    'account-url' => 'user:url',
168
    'account-edit' => 'user:edit-url',
169
  );
170

    
171
  // Account for date tokens which need to be expanded.
172
  $tokens += _token_upgrade_token_date_list('site-', 'site:date');
173
  $tokens += _token_upgrade_token_date_list('', 'node:created');
174
  $tokens += _token_upgrade_token_date_list('mod-', 'node:changed');
175
  //$tokens += _token_upgrade_token_date_list('node-revision-', 'node:changed');
176
  $tokens += _token_upgrade_token_date_list('comment-', 'comment:created');
177
  $tokens += _token_upgrade_token_date_list('user-register-', 'user:created');
178
  $tokens += _token_upgrade_token_date_list('user-last-login-', 'user:last-login');
179

    
180
  return $tokens;
181
}
182

    
183
/**
184
 * Build a list of Drupal 6 date tokens and their Drupal 7 token names.
185
 */
186
function _token_upgrade_token_date_list($old_token, $new_token) {
187
  $tokens = array();
188
  $formats = array(
189
    'yyyy' => 'Y',
190
    'yy' => 'y',
191
    'month' => 'F',
192
    'mon' => 'M',
193
    'mm' => 'm',
194
    'm' => 'n',
195
    'ww' => 'W',
196
    'date' => 'N',
197
    'day' => 'l',
198
    'ddd' => 'D',
199
    'dd' => 'd',
200
    'd' => 'j',
201
  );
202
  foreach ($formats as $token_format => $date_format) {
203
    $tokens[$old_token . $token_format] = "$new_token:custom:$date_format";
204
  }
205
  $tokens[$old_token . 'raw'] = "$new_token:raw";
206
  $tokens[$old_token . 'since'] = "$new_token:since";
207
  return $tokens;
208
}
209

    
210
/**
211
 * Update a string containing Drupal 6 style tokens to Drupal 7 style tokens.
212
 *
213
 * @param $text
214
 *   A string containing tokens.
215
 * @param $updates
216
 *   An optional array of Drupal 7 tokens keyed by their Drupal 6 token name.
217
 *   The default tokens will be merged into this array. Note neither the old
218
 *   or new token names should include the surrounding bracket ([ and ])
219
 *   characters.
220
 * @return
221
 *   A string with the tokens upgraded
222
 *
223
 * @see _token_upgrade_token_list()
224
 */
225
function token_update_token_text($text, $updates = array(), $leading = '[', $trailing = ']') {
226
  $updates += _token_upgrade_token_list();
227
  $regex = '/' . preg_quote($leading, '/') . '([^\s]*)' . preg_quote($trailing, '/') . '/';
228
  preg_match_all($regex, $text, $matches);
229

    
230
  foreach ($matches[1] as $index => $old_token) {
231
    if (isset($updates[$old_token])) {
232
      $new_token = $updates[$old_token];
233
      $text = str_replace("{$leading}{$old_token}{$trailing}", "[$new_token]", $text);
234
      // Also replace any tokens that have a -raw suffix.
235
      $text = str_replace("{$leading}{$old_token}-raw{$trailing}", "[$new_token]", $text);
236
    }
237
  }
238

    
239
  return $text;
240
}
241

    
242
/**
243
 * Get token problems.
244
 */
245
function token_get_token_problems() {
246
  // @todo Improve the duplicate checking to report which modules are the offenders.
247
  //$token_info = array();
248
  //foreach (module_implements('token_info') as $module) {
249
  //  $module_token_info = module_invoke($module, 'token_info');
250
  //  if (in_array($module, _token_core_supported_modules())) {
251
  //    $module .= '/token';
252
  //  }
253
  //  if (isset($module_token_info['types'])) {
254
  //    if (is_array($module_token_info['types'])) {
255
  //      foreach (array_keys($module_token_info['types']) as $type) {
256
  //        if (is_array($module_token_info['types'][$type])) {
257
  //          $module_token_info['types'][$type] += array('module' => $module);
258
  //        }
259
  //      }
260
  //    }
261
  //  }
262
  //  if (isset($module_token_info['tokens'])) {
263
  //    if (is_array($module_token_info['tokens'])) {
264
  //
265
  //    }
266
  //  }
267
  //  if (is_array($module_token_info)) {
268
  //    $token_info = array_merge_recursive($token_info, $module_token_info);
269
  //  }
270
  //}
271

    
272
  $token_info = token_info();
273
  $token_problems = array(
274
    'not-array' => array(
275
      'label' => t('The following tokens or token types are not defined as arrays:'),
276
    ),
277
    'missing-info' => array(
278
      'label' => t('The following tokens or token types are missing required name and/or description information:'),
279
    ),
280
    'type-no-tokens' => array(
281
      'label' => t('The following token types do not have any tokens defined:'),
282
    ),
283
    'tokens-no-type' => array(
284
      'label' => t('The following token types are not defined but have tokens:'),
285
    ),
286
    'duplicate' => array(
287
      'label' => t('The following token or token types are defined by multiple modules:')
288
    ),
289
  );
290

    
291
  // Check token types for problems.
292
  foreach ($token_info['types'] as $type => $type_info) {
293
    $real_type = !empty($type_info['type']) ? $type_info['type'] : $type;
294
    if (!is_array($type_info)) {
295
      $token_problems['not-array']['problems'][] = "\$info['types']['$type']";
296
      continue;
297
    }
298
    elseif (!isset($type_info['name']) || !isset($type_info['description'])) {
299
      $token_problems['missing-info']['problems'][] = "\$info['types']['$type']";
300
    }
301
    elseif (empty($token_info['tokens'][$real_type])) {
302
      $token_problems['type-no-tokens']['problems'][] = "\$info['tokens']['$real_type']";
303
    }
304
  }
305

    
306
  // Check tokens for problems.
307
  foreach ($token_info['tokens'] as $type => $tokens) {
308
    if (!is_array($tokens)) {
309
      $token_problems['not-array']['problems'][] = "\$info['tokens']['$type']";
310
      continue;
311
    }
312
    else {
313
      foreach (array_keys($tokens) as $token) {
314
        if (!is_array($tokens[$token])) {
315
          $token_problems['not-array']['problems'][] = "\$info['tokens']['$type']['$token']";
316
          continue;
317
        }
318
        elseif (!isset($tokens[$token]['name']) || !isset($tokens[$token]['description'])) {
319
          $token_problems['missing-info']['problems'][] = "\$info['tokens']['$type']['$token']";
320
        }
321
        elseif (is_array($tokens[$token]['name']) || is_array($tokens[$token]['description'])) {
322
          $token_problems['duplicate']['problems'][] = "\$info['tokens']['$type']['$token']";
323
        }
324
      }
325
    }
326
    if (!isset($token_info['types'][$type])) {
327
      $token_problems['tokens-no-type']['problems'][] = "\$info['types']['$type']";
328
    }
329
  }
330

    
331
  return $token_problems;
332
}