~ubuntu-fr/ubuntu-fr-website/www.dev

« back to all changes in this revision

Viewing changes to sites/all/modules/token/token.module

  • Committer: Vincent Liefooghe
  • Date: 2010-11-16 20:51:54 UTC
  • Revision ID: vl@ubuntovo-20101116205154-fuyfka524rm2zgni
Mise à jour des modules Backup & Migrate, Image API, Token et Pathauto

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?php
2
 
// $Id: token.module,v 1.7.4.33 2010/08/10 03:33:32 davereid Exp $
 
2
// $Id: token.module,v 1.7.4.38 2010/09/30 19:39:43 davereid Exp $
3
3
 
4
4
/**
5
5
 * @file
36
36
}
37
37
 
38
38
/**
 
39
 * Return an array of the core modules supported by token.module.
 
40
 */
 
41
function _token_core_supported_modules() {
 
42
  return array('node', 'user', 'taxonomy', 'comment');
 
43
}
 
44
 
 
45
/**
 
46
 * Implements hook_menu().
 
47
 */
 
48
function token_menu() {
 
49
  $items = array();
 
50
 
 
51
  // Devel token pages.
 
52
  if (module_exists('devel')) {
 
53
    $items['node/%node/devel/token'] = array(
 
54
      'title' => 'Tokens',
 
55
      'page callback' => 'token_devel_token_object',
 
56
      'page arguments' => array('node', 1),
 
57
      'access arguments' => array('access devel information'),
 
58
      'type' => MENU_LOCAL_TASK,
 
59
      'file' => 'token.pages.inc',
 
60
      'weight' => 5,
 
61
    );
 
62
    $items['user/%user/devel/token'] = array(
 
63
      'title' => 'Tokens',
 
64
      'page callback' => 'token_devel_token_object',
 
65
      'page arguments' => array('user', 1),
 
66
      'access arguments' => array('access devel information'),
 
67
      'type' => MENU_LOCAL_TASK,
 
68
      'file' => 'token.pages.inc',
 
69
      'weight' => 5,
 
70
    );
 
71
  }
 
72
 
 
73
  return $items;
 
74
}
 
75
 
 
76
/**
39
77
 * Implements hook_theme().
40
78
 */
41
79
function token_theme() {
52
90
}
53
91
 
54
92
/**
55
 
 * Sample implementation of hook_token_values().
56
 
 *
57
 
 * @param type
58
 
 *   A flag indicating the class of substitution tokens to use. If an
59
 
 *   object is passed in the second param, 'type' should contain the
60
 
 *   object's type. For example, 'node', 'comment', or 'user'. If your
61
 
 *   implementation of the hook inserts globally applicable tokens that
62
 
 *   do not depend on a particular object, it should only return values
63
 
 *   when $type is 'global'.
64
 
 * @param object
65
 
 *   Optionally, the object to use for building substitution values.
66
 
 *   A node, comment, user, etc.
67
 
 * @return
68
 
 *   A keyed array containing the substitution tokens and the substitution
69
 
 *   values for the passed-in type and object.
 
93
 * Implements hook_token_values().
70
94
 */
71
95
function token_token_values($type, $object = NULL) {
72
96
  global $user;
107
131
}
108
132
 
109
133
/**
110
 
 * Sample implementation of hook_token_list(). Documents the individual
111
 
 * tokens handled by your module.
112
 
 *
113
 
 * @param type
114
 
 *   A flag indicating the class of substitution tokens to return
115
 
 *   information on. If this is set to 'all', a complete list is being
116
 
 *   built and your module should return its full list, regardless of
117
 
 *   type. Global tokens should always be returned, regardless of the
118
 
 *   $type passed in.
119
 
 * @return
120
 
 *   A keyed array listing the substitution tokens. Elements should be
121
 
 *   in the form of: $list[$type][$token] = $description
 
134
 * Implements hook_token_list().
122
135
 */
123
136
function token_token_list($type = 'all') {
124
137
  $tokens = array();
157
170
  if (!$run) {
158
171
    $run = TRUE;
159
172
    $modules_enabled = array_keys(module_list());
160
 
    $modules = array('node', 'user', 'taxonomy', 'comment');
161
 
    $modules = array_intersect($modules, $modules_enabled);
 
173
    $modules = array_intersect(_token_core_supported_modules(), $modules_enabled);
162
174
    foreach ($modules as $module) {
163
175
      module_load_include('inc', 'token', "token_$module");
164
176
    }
166
178
}
167
179
 
168
180
/**
169
 
 * Return the value of $original, with all instances of placeholder
170
 
 * tokens replaced by their proper values. To replace multiple types
171
 
 * at once see token_replace_multiple().
172
 
 *
173
 
 * @param original
174
 
 *  A string, or an array of strings, to perform token substitutions
175
 
 *  on.
176
 
 * @param type
177
 
 *   A flag indicating the class of substitution tokens to use. If an
178
 
 *   object is passed in the second param, 'type' should contain the
179
 
 *   object's type. For example, 'node', 'comment', or 'user'. If no
180
 
 *   type is specified, only 'global' site-wide substitution tokens are
181
 
 *   built.
182
 
 * @param object
183
 
 *   Optionally, the object to use for building substitution values.
184
 
 *   A node, comment, user, etc.
185
 
 * @param leading
186
 
 *    Character(s) to prepend to the token key before searching for
187
 
 *    matches. Defaults to an open-bracket.
188
 
 * @param trailing
189
 
 *    Character(s) to append to the token key before searching for
190
 
 *    matches. Defaults to a close-bracket.
191
 
 * @param flush
192
 
 *    A flag indicating whether or not to flush the token cache.
193
 
 *    Useful for processes that need to slog through huge numbers
194
 
 *    of tokens in a single execution cycle. Flushing it will
195
 
 *    keep them from burning through memory.
196
 
 *    The default is FALSE.
197
 
 * @return The modified version of $original, with all substitutions
198
 
 *   made.
 
181
 * Replace all tokens in a given string with appropriate values.
 
182
 *
 
183
 * @param $text
 
184
 *   A string potentially containing replaceable tokens.
 
185
 * @param $type
 
186
 *   (optional) A flag indicating the class of substitution tokens to use. If
 
187
 *   an object is passed in the second param, 'type' should contain the
 
188
 *   object's type. For example, 'node', 'comment', or 'user'. If no type is
 
189
 *   specified, only 'global' site-wide substitution tokens are built.
 
190
 * @param $object
 
191
 *   (optional) An object to use for building substitution values (e.g. a node
 
192
 *   comment, or user object).
 
193
 * @param $leading
 
194
 *   (optional) Character(s) to prepend to the token key before searching for
 
195
 *   matches. Defaults to TOKEN_PREFIX.
 
196
 * @param $trailing
 
197
 *   (optional) Character(s) to append to the token key before searching for
 
198
 *   matches. Defaults to TOKEN_SUFFIX.
 
199
 * @param $options
 
200
 *   (optional) A keyed array of settings and flags to control the token
 
201
 *   generation and replacement process.
 
202
 * @param $flush
 
203
 *   (optional) A flag indicating whether or not to flush the token cache.
 
204
 *   Useful for processes that need to slog through huge numbers of tokens
 
205
 *   in a single execution cycle. Flushing it will keep them from burning
 
206
 *   through memory. Defaults to FALSE.
 
207
 *
 
208
 * @return
 
209
 *   Text with tokens replaced.
199
210
 */
200
 
function token_replace($original, $type = 'global', $object = NULL, $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX, $options = array(), $flush = FALSE) {
 
211
function token_replace($text, $type = 'global', $object = NULL, $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX, $options = array(), $flush = FALSE) {
201
212
  $full = token_get_values($type, $object, $flush, $options);
202
 
  return _token_replace_tokens($original, $full->tokens, $full->values, $leading, $trailing);
 
213
  $tokens = token_prepare_tokens($full->tokens, $leading, $trailing);
 
214
  return str_replace($tokens, $full->values, $text);
203
215
}
204
216
 
205
217
/**
206
 
 * Return the value of $original, with all instances of placeholder
207
 
 * tokens replaced by their proper values. Contrary to token_replace(),
208
 
 * this function supports replacing multiple types.
209
 
 *
210
 
 * @param original
211
 
 *  A string, or an array of strings, to perform token substitutions
212
 
 *  on.
213
 
 * @param types
214
 
 *   An array of substitution classes and optional objects. The key is
215
 
 *   a flag indicating the class of substitution tokens to use.
216
 
 *   If an object is passed as value, the key should contain the
217
 
 *   object's type. For example, 'node', 'comment', or 'user'. The
218
 
 *   object will be used for building substitution values. If no type
219
 
 *   is specified, only 'global' site-wide substitution tokens are built.
220
 
 * @param leading
221
 
 *    Character(s) to prepend to the token key before searching for
222
 
 *    matches. Defaults to an open-bracket.
223
 
 * @param trailing
224
 
 *    Character(s) to append to the token key before searching for
225
 
 *    matches. Defaults to a close-bracket.
226
 
 * @param flush
227
 
 *    A flag indicating whether or not to flush the token cache.
228
 
 *    Useful for processes that need to slog through huge numbers
229
 
 *    of tokens in a single execution cycle. Flushing it will
230
 
 *    keep them from burning through memory.
231
 
 *    The default is FALSE.
232
 
 * @return The modified version of $original, with all substitutions
233
 
 *   made.
 
218
 * Replace all tokens in a given string with appropriate values.
 
219
 *
 
220
 * Contrary to token_replace() this function supports replacing multiple types.
 
221
 *
 
222
 * @param $text
 
223
 *   A string potentially containing replaceable tokens.
 
224
 * @param $types
 
225
 *   (optional) An array of substitution classes and optional objects. The key
 
226
 *   is a flag indicating the class of substitution tokens to use. If an object
 
227
 *   is passed as value, the key should contain the object's type. For example,
 
228
 *   'node', 'comment', or 'user'. The object will be used for building
 
229
 *   substitution values. If no type is specified, only 'global' site-wide
 
230
 *   substitution tokens are built.
 
231
 * @param $leading
 
232
 *   (optional) Character(s) to prepend to the token key before searching for
 
233
 *   matches. Defaults to TOKEN_PREFIX.
 
234
 * @param $trailing
 
235
 *   (optional) Character(s) to append to the token key before searching for
 
236
 *   matches. Defaults to TOKEN_SUFFIX.
 
237
 * @param $options
 
238
 *   (optional) A keyed array of settings and flags to control the token
 
239
 *   generation and replacement process.
 
240
 * @param $flush
 
241
 *   (optional) A flag indicating whether or not to flush the token cache.
 
242
 *   Useful for processes that need to slog through huge numbers of tokens
 
243
 *   in a single execution cycle. Flushing it will keep them from burning
 
244
 *   through memory. Defaults to FALSE.
 
245
 *
 
246
 * @return
 
247
 *   Text with tokens replaced.
234
248
 */
235
 
function token_replace_multiple($original, $types = array('global' => NULL), $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX, $options = array(), $flush = FALSE) {
 
249
function token_replace_multiple($text, $types = array(), $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX, $options = array(), $flush = FALSE) {
236
250
  $full = new stdClass();
237
251
  $full->tokens = $full->values = array();
 
252
 
 
253
  // Allow global token replacement by default.
 
254
  if (empty($types) || !is_array($types)) {
 
255
    $types = array('global' => NULL);
 
256
  }
 
257
 
238
258
  foreach ($types as $type => $object) {
239
259
    $temp = token_get_values($type, $object, $flush, $options);
240
260
    $full->tokens = array_merge($full->tokens, $temp->tokens);
241
261
    $full->values = array_merge($full->values, $temp->values);
242
262
  }
243
 
  return _token_replace_tokens($original, $full->tokens, $full->values, $leading, $trailing);
244
 
}
245
263
 
246
 
// Internally used function to replace tokens.
247
 
function _token_replace_tokens($original, $tokens, $values, $leading, $trailing) {
248
 
  $tokens = token_prepare_tokens($tokens, $leading, $trailing);
249
 
  return str_replace($tokens, $values, $original);
 
264
  $tokens = token_prepare_tokens($full->tokens, $leading, $trailing);
 
265
  return str_replace($tokens, $full->values, $text);
250
266
}
251
267
 
252
268
/**
253
269
 * Return a list of valid substitution tokens and their values for
254
270
 * the specified type.
255
271
 *
256
 
 * @param type
257
 
 *   A flag indicating the class of substitution tokens to use. If an
 
272
 * @param $type
 
273
 *   (optional) A flag indicating the class of substitution tokens to use. If an
258
274
 *   object is passed in the second param, 'type' should contain the
259
275
 *   object's type. For example, 'node', 'comment', or 'user'. If no
260
276
 *   type is specified, only 'global' site-wide substitution tokens are
261
277
 *   built.
262
 
 * @param object
263
 
 *   Optionally, the object to use for building substitution values.
264
 
 *   A node, comment, user, etc.
265
 
 * @param flush
266
 
 *   A flag indicating whether or not to flush the token cache.
267
 
 *   Useful for processes that need to slog through huge numbers
268
 
 *   of tokens in a single execution cycle. Flushing it will
269
 
 *   keep them from burning through memory.
270
 
 *   The default is FALSE.
 
278
 * @param $object
 
279
 *   (optional) An object to use for building substitution values (e.g. a node
 
280
 *   comment, or user object).
 
281
 * @param $flush
 
282
 *   (optional) A flag indicating whether or not to flush the token cache.
 
283
 *   Useful for processes that need to slog through huge numbers of tokens
 
284
 *   in a single execution cycle. Flushing it will keep them from burning
 
285
 *   through memory. Defaults to FALSE.
 
286
 * @param $options
 
287
 *   (optional) A keyed array of settings and flags to control the token
 
288
 *   generation process.
 
289
 *
271
290
 * @return
272
 
 *   A keyed array containing the substitution tokens and the substitution
273
 
 *   values for the passed-in type and object.
 
291
 *   An object with two properties:
 
292
 *   - tokens: All the possible tokens names generated.
 
293
 *   - values: The corresponding values for the tokens.
 
294
 *
 
295
 * Note that before performing actual token replacement that the token names
 
296
 * should be run through token_prepare_tokens().
274
297
 */
275
298
function token_get_values($type = 'global', $object = NULL, $flush = FALSE, $options = array()) {
276
299
  static $tokens;
320
343
    $tokens['global']['default'] = module_invoke_all('token_values', 'global');
321
344
  }
322
345
 
323
 
  $all = _token_array_merge($tokens['global']['default'], $tokens[$type][$id]);
 
346
  $all = array_merge($tokens['global']['default'], $tokens[$type][$id]);
324
347
 
325
348
  $result = new stdClass();
326
349
  $result->tokens = array_keys($all);
332
355
}
333
356
 
334
357
/**
335
 
 * Merge two arrays of token values, checking and warning for duplication.
336
 
 *
337
 
 * Because array_merge will combinue elements that have the same key into an
338
 
 * array instead of overriding the existing element, this causes problems since
339
 
 * we need the values to always be a string.
340
 
 *
341
 
 * @param $array1
342
 
 *   An array of token values keyed by token name.
343
 
 * @param $array2
344
 
 *   An array of token values keyed by token name.
345
 
 * @return
346
 
 *   A merged array of token values keyed by token name.
347
 
 */
348
 
function _token_array_merge($array1, $array2) {
349
 
  static $warned = array();
350
 
 
351
 
  $merged = array_merge($array1, $array2);
352
 
  foreach ($merged as $key => $value) {
353
 
    if (is_array($value)) {
354
 
      if (empty($warned[$key])) {
355
 
        // Only warn once about duplicate token.
356
 
        watchdog('token', 'More than one module has defined the %key token.', array('%key' => $key), WATCHDOG_WARNING);
357
 
        $warned[$key] = TRUE;
358
 
      }
359
 
      // Use the last-defined value (module with lowest weight).
360
 
      $merged[$key] = array_pop($value);
361
 
    }
362
 
  }
363
 
 
364
 
  return $merged;
365
 
}
366
 
 
367
 
/**
368
358
 * A helper function that retrieves all currently exposed tokens,
369
359
 * and merges them recursively. This is only necessary when building
370
360
 * the token listing -- during actual value replacement, only tokens
371
361
 * in a particular domain are requested and a normal array_marge() is
372
362
 * sufficient.
373
363
 *
374
 
 * @param types
 
364
 * @param $types
375
365
 *   A flag indicating the class of substitution tokens to use. If an
376
366
 *   object is passed in the second param, 'types' should contain the
377
367
 *   object's type. For example, 'node', 'comment', or 'user'. 'types'
378
368
 *   may also be an array of types of the form array('node','user'). If no
379
369
 *   type is specified, only 'global' site-wide substitution tokens are
380
370
 *   built.
 
371
 *
381
372
 * @return
382
373
 *   The array of usable tokens and their descriptions, organized by
383
374
 *   token type.
387
378
  $return = array();
388
379
  settype($types, 'array');
389
380
  foreach (module_implements('token_list') as $module) {
390
 
    $function = $module .'_token_list';
391
381
    foreach ($types as $type) {
392
 
      $result = $function($type);
393
 
      if (is_array($result)) {
394
 
        foreach ($result as $category => $tokens) {
 
382
      $module_token_list = module_invoke($module, 'token_list', $type);
 
383
      if (isset($module_token_list) && is_array($module_token_list)) {
 
384
        foreach ($module_token_list as $category => $tokens) {
395
385
          foreach ($tokens as $token => $title) {
396
386
            // Automatically append a raw token warning.
397
387
            if (substr($token, -4) === '-raw' && strpos($title, t('raw user input')) === FALSE && strpos($title, '1269441371') === FALSE) {
407
397
}
408
398
 
409
399
/**
410
 
 * A helper function that transforms all the elements of an
411
 
 * array. Used to change the delimiter style from brackets to
412
 
 * percent symbols etc.
413
 
 *
414
 
 * @param tokens
415
 
 *    The array of tokens keys with no delimiting characters
416
 
 * @param leading
417
 
 *    Character(s) to prepend to the token key before searching for
418
 
 *    matches. Defaults to an open-bracket.
419
 
 * @param trailing
420
 
 *    Character(s) to append to the token key before searching for
421
 
 *    matches. Defaults to a close-bracket.
422
 
 *  @return
423
 
 *    The array of token keys, each wrapped in the specified
424
 
 *    delimiter style.
 
400
 * A helper function to prepare raw tokens for replacement.
 
401
 *
 
402
 * @param $tokens
 
403
 *   The array of tokens names with no delimiting characters.
 
404
 * @param $leading
 
405
 *   String to prepend to the token. Default is TOKEN_PREFIX.
 
406
 * @param $trailing
 
407
 *   String to append to the token. Default is TOKEN_SUFFIX.
 
408
 *
 
409
 * @return
 
410
 *   An array of the formatted tokens.
425
411
 */
426
412
function token_prepare_tokens($tokens = array(), $leading = TOKEN_PREFIX, $trailing = TOKEN_SUFFIX) {
427
413
  foreach ($tokens as $key => $value) {
430
416
  return $tokens;
431
417
}
432
418
 
433
 
// Internal utility function used for static caching. There are
434
 
// almost certainly better ways to do this, but for the moment it's
435
 
// sufficient.
 
419
/**
 
420
 * A helper function to return an object's ID for use in static caching.
 
421
 */
436
422
function _token_get_id($type = 'global', $object = NULL) {
437
423
  if (!isset($object)) {
438
424
    return "default";
527
513
 *   A string with the raw text containing the raw tokens.
528
514
 * @param $valid_types
529
515
 *   An array of token types to validage against.
530
 
 * @param leading
531
 
 *    Character(s) to prepend to the token key before searching for
532
 
 *    matches. Defaults to an open-bracket.
533
 
 * @param trailing
534
 
 *    Character(s) to append to the token key before searching for
535
 
 *    matches. Defaults to a close-bracket.
 
516
 * @param $leading
 
517
 *   Character(s) to prepend to the token key before searching for
 
518
 *   matches. Defaults to TOKEN_PREFIX.
 
519
 * @param $trailing
 
520
 *   Character(s) to append to the token key before searching for
 
521
 *   matches. Defaults to TOKEN_SUFFIX.
 
522
 *
536
523
 * @return
537
524
 *   An array with the invalid tokens in their original raw forms.
538
525
 */
549
536
        $found = TRUE;
550
537
        break;
551
538
      }
 
539
      elseif (preg_match('/^(.*[_-])\d+$/', $token, $result)) {
 
540
        // Some modules use a 'wildcard' token, e.g. Location.module defines
 
541
        // [location-country_N] but users actually use [location-country_0].
 
542
        // If the token has a digit on the end and there is a 'wildcard'
 
543
        // token definition, we should allow it.
 
544
        if (isset($tokens[$result[1] . 'N']) || isset($tokens[$result[1] . '?'])) {
 
545
          $found = TRUE;
 
546
          break;
 
547
        }
 
548
      }
552
549
    }
553
550
    if (!$found) {
554
551
      $invalid_tokens[] = $token;
565
562
 *
566
563
 * @param $text
567
564
 *   The text to be scanned for possible tokens.
568
 
 * @param leading
569
 
 *    Character(s) to prepend to the token key before searching for
570
 
 *    matches. Defaults to an open-bracket.
571
 
 * @param trailing
572
 
 *    Character(s) to append to the token key before searching for
573
 
 *    matches. Defaults to a close-bracket.
 
565
 * @param $leading
 
566
 *   Character(s) to prepend to the token key before searching for
 
567
 *   matches. Defaults to TOKEN_PREFIX.
 
568
 * @param $trailing
 
569
 *   Character(s) to append to the token key before searching for
 
570
 *   matches. Defaults to TOKEN_SUFFIX.
 
571
 *
574
572
 * @return
575
573
 *   An array of discovered tokens.
576
574
 */
607
605
  }
608
606
  return $element;
609
607
}
 
608
 
 
609
/**
 
610
 * Find tokens that have been declared twice by different modules.
 
611
 */
 
612
function token_find_duplicate_tokens() {
 
613
  token_include();
 
614
  $all_tokens = array();
 
615
 
 
616
  foreach (module_implements('token_list') as $module) {
 
617
    $module_token_list = module_invoke($module, 'token_list', 'all');
 
618
    if (!isset($module_token_list) || !is_array($module_token_list)) {
 
619
      // Skip modules that do not return an array as that is a valid return
 
620
      // value.
 
621
      continue;
 
622
    }
 
623
    if (in_array($module, _token_core_supported_modules())) {
 
624
      $module = 'token';
 
625
    }
 
626
    foreach ($module_token_list as $type => $tokens) {
 
627
      foreach (array_keys($tokens) as $token) {
 
628
        $all_tokens[$type . ':' . $token][] = $module;
 
629
      }
 
630
    }
 
631
  }
 
632
 
 
633
  foreach ($all_tokens as $token => $modules) {
 
634
    if (count($modules) < 2) {
 
635
      unset($all_tokens[$token]);
 
636
    }
 
637
  }
 
638
 
 
639
  return $all_tokens;
 
640
}