Projet

Général

Profil

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

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

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
        $requirements['token-' . $problem_key] = array(
25
          'title' => $problem['label'],
26
          'value' => theme('item_list', array('items' => $problems)),
27
          'severity' => $problem['severity'],
28
        );
29
      }
30
    }
31
  }
32

    
33
  return $requirements;
34
}
35

    
36
/**
37
 * Implements hook_schema().
38
 */
39
function token_schema() {
40
  $schema['cache_token'] = drupal_get_schema_unprocessed('system', 'cache');
41
  $schema['cache_token']['description'] = 'Cache table for token information.';
42
  return $schema;
43
}
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
/**
57
 * Deprecate and disable the token_actions module.
58
 */
59
function token_update_7001() {
60
  module_disable(array('token_actions'));
61
  return 'The Token actions module has been deprecated by the updated system module actions that support tokens.';
62
}
63

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

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

    
102
    // Comment tokens
103
    'comment-cid' => 'comment:cid',
104
    'comment-nid' => 'comment:node:nid',
105
    'comment-title' => 'comment:title',
106
    'comment-body' => 'comment:body',
107
    'comment-author-name' => 'comment:author:name',
108
    'comment-author-mail' => 'comment:author:mail',
109
    //'comment-body-format' => '',
110
    //'comment-' => '', // Date tokens expanded below
111
    'comment-node-title' => 'comment:node',
112

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

    
137
    // Book tokens
138
    //'book' => 'node:book',
139
    //'book_id' => 'node:book:bid',
140
    //'bookpath' => 'node:book:path',
141

    
142
    // Taxonomy tokens
143
    'tid' => 'term:tid',
144
    'cat' => 'term:name',
145
    'cat-description' => 'term:description',
146
    'vid' => 'term:vocabulary:vid',
147
    'vocab' => 'term:vocabulary',
148
    'vocab-description' => 'term:vocabulary:description',
149

    
150
    // User tokens
151
    'user' => 'user:name',
152
    'uid' => 'user:uid',
153
    'mail' => 'user:mail',
154
    'reg-date' => 'user:created',
155
    'reg-since' => 'user:created:since',
156
    //'user-created' => '', // Date tokens expanded below
157
    'log-date' => 'user:last-login',
158
    'log-since' => 'user:last-login:since',
159
    //'user-last-login' => '', // Date tokens expanded below
160
    //'date-in-tz' => '',
161
    'account-url' => 'user:url',
162
    'account-edit' => 'user:edit-url',
163
  );
164

    
165
  // Account for date tokens which need to be expanded.
166
  $tokens += _token_upgrade_token_date_list('site-', 'site:date');
167
  $tokens += _token_upgrade_token_date_list('', 'node:created');
168
  $tokens += _token_upgrade_token_date_list('mod-', 'node:changed');
169
  //$tokens += _token_upgrade_token_date_list('node-revision-', 'node:changed');
170
  $tokens += _token_upgrade_token_date_list('comment-', 'comment:created');
171
  $tokens += _token_upgrade_token_date_list('user-register-', 'user:created');
172
  $tokens += _token_upgrade_token_date_list('user-last-login-', 'user:last-login');
173

    
174
  return $tokens;
175
}
176

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

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

    
224
  foreach ($matches[1] as $index => $old_token) {
225
    if (isset($updates[$old_token])) {
226
      $new_token = $updates[$old_token];
227
      $text = str_replace("{$leading}{$old_token}{$trailing}", "[$new_token]", $text);
228
      // Also replace any tokens that have a -raw suffix.
229
      $text = str_replace("{$leading}{$old_token}-raw{$trailing}", "[$new_token]", $text);
230
    }
231
  }
232

    
233
  return $text;
234
}
235

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

    
266
  $token_info = token_info();
267
  $token_problems = array(
268
    'not-array' => array(
269
      'label' => t('Tokens or token types not defined as arrays'),
270
      'severity' => REQUIREMENT_ERROR,
271
    ),
272
    'missing-info' => array(
273
      'label' => t('Tokens or token types missing name property'),
274
      'severity' => REQUIREMENT_WARNING,
275
    ),
276
    'type-no-tokens' => array(
277
      'label' => t('Token types do not have any tokens defined'),
278
      'severity' => REQUIREMENT_INFO,
279
    ),
280
    'tokens-no-type' => array(
281
      'label' => t('Token types are not defined but have tokens'),
282
      'severity' => REQUIREMENT_INFO,
283
    ),
284
    'duplicate' => array(
285
      'label' => t('Token or token types are defined by multiple modules'),
286
      'severity' => REQUIREMENT_ERROR,
287
    ),
288
  );
289

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

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

    
333
  return $token_problems;
334
}