Projet

Général

Profil

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

root / drupal7 / sites / all / modules / pathauto / pathauto.pathauto.inc @ a45e4bc1

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Pathauto integration for core modules.
6
 *
7
 * @ingroup pathauto
8
 */
9
10
/**
11
 * Implements hook_path_alias_types().
12
 *
13
 * Used primarily by the bulk delete form.
14
 */
15
function pathauto_path_alias_types() {
16
  $objects['user/'] = t('Users');
17
  $objects['node/'] = t('Content');
18
  if (module_exists('blog')) {
19
    $objects['blog/'] = t('User blogs');
20
  }
21
  if (module_exists('taxonomy')) {
22
    $objects['taxonomy/term/'] = t('Taxonomy terms');
23
  }
24
  if (module_exists('forum')) {
25
    $objects['forum/'] = t('Forums');
26
  }
27
  return $objects;
28
}
29
30
/**
31
 * Implements hook_pathauto().
32
 *
33
 * This function is empty so that the other core module implementations can be
34
 * defined in this file. This is because in pathauto_module_implements_alter()
35
 * we add pathauto to be included first. The module system then peforms a
36
 * check on any subsequent run if this function still exists. If this does not
37
 * exist, than this file will not get included and the core implementations
38
 * will never get run.
39
 *
40
 * @see pathauto_module_implements_alter().
41
 */
42
function pathauto_pathauto() {
43
  // Empty hook; see the above comment.
44
}
45
46
/**
47
 * Implements hook_pathauto().
48
 */
49
function node_pathauto($op) {
50
  switch ($op) {
51
    case 'settings':
52
      $settings = array();
53
      $settings['module'] = 'node';
54
      $settings['token_type'] = 'node';
55
      $settings['groupheader'] = t('Content paths');
56
      $settings['patterndescr'] = t('Default path pattern (applies to all content types with blank patterns below)');
57
      $settings['patterndefault'] = 'content/[node:title]';
58
      $settings['batch_update_callback'] = 'node_pathauto_bulk_update_batch_process';
59
      $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
60
61
      $languages = array();
62
      if (module_exists('locale')) {
63
        $languages = array(LANGUAGE_NONE => t('language neutral')) + locale_language_list('name');
64
      }
65
66
      foreach (node_type_get_names() as $node_type => $node_name) {
67
        if (count($languages) && variable_get('language_content_type_' . $node_type, 0)) {
68
          $settings['patternitems'][$node_type] = t('Default path pattern for @node_type (applies to all @node_type content types with blank patterns below)', array('@node_type' => $node_name));
69
          foreach ($languages as $lang_code => $lang_name) {
70
            $settings['patternitems'][$node_type . '_' . $lang_code] = t('Pattern for all @language @node_type paths', array('@node_type' => $node_name, '@language' => $lang_name));
71
          }
72
        }
73
        else {
74
          $settings['patternitems'][$node_type] = t('Pattern for all @node_type paths', array('@node_type' => $node_name));
75
        }
76
      }
77
      return (object) $settings;
78
    default:
79
      break;
80
  }
81
}
82
83
/**
84
 * Batch processing callback; Generate aliases for nodes.
85
 */
86
function node_pathauto_bulk_update_batch_process(&$context) {
87
  if (!isset($context['sandbox']['current'])) {
88
    $context['sandbox']['count'] = 0;
89
    $context['sandbox']['current'] = 0;
90
  }
91
92
  $query = db_select('node', 'n');
93
  $query->leftJoin('url_alias', 'ua', "CONCAT('node/', n.nid) = ua.source");
94
  $query->addField('n', 'nid');
95
  $query->isNull('ua.source');
96
  $query->condition('n.nid', $context['sandbox']['current'], '>');
97
  $query->orderBy('n.nid');
98
  $query->addTag('pathauto_bulk_update');
99
  $query->addMetaData('entity', 'node');
100
101
  // Get the total amount of items to process.
102
  if (!isset($context['sandbox']['total'])) {
103
    $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
104
105
    // If there are no nodes to update, the stop immediately.
106
    if (!$context['sandbox']['total']) {
107
      $context['finished'] = 1;
108
      return;
109
    }
110
  }
111
112
  $query->range(0, 25);
113
  $nids = $query->execute()->fetchCol();
114
115
  pathauto_node_update_alias_multiple($nids, 'bulkupdate');
116
  $context['sandbox']['count'] += count($nids);
117
  $context['sandbox']['current'] = max($nids);
118
  $context['message'] = t('Updated alias for node @nid.', array('@nid' => end($nids)));
119
120
  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
121
    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
122
  }
123
}
124
125
/**
126
 * Implements hook_pathauto().
127
 */
128
function taxonomy_pathauto($op) {
129
  switch ($op) {
130
    case 'settings':
131
      $settings = array();
132
      $settings['module'] = 'taxonomy_term';
133
      $settings['token_type'] = 'term';
134
      $settings['groupheader'] = t('Taxonomy term paths');
135
      $settings['patterndescr'] = t('Default path pattern (applies to all vocabularies with blank patterns below)');
136
      $settings['patterndefault'] = '[term:vocabulary]/[term:name]';
137
      $settings['batch_update_callback'] = 'taxonomy_pathauto_bulk_update_batch_process';
138
      $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
139
140
      $vocabularies = taxonomy_get_vocabularies();
141
      if (count($vocabularies)) {
142
        $settings['patternitems'] = array();
143
        foreach ($vocabularies as $vid => $vocabulary) {
144
          if ($vid == variable_get('forum_nav_vocabulary', '')) {
145
            // Skip the forum vocabulary.
146
            continue;
147
          }
148
          $settings['patternitems'][$vocabulary->machine_name] = t('Pattern for all %vocab-name paths', array('%vocab-name' => $vocabulary->name));
149
        }
150
      }
151
      return (object) $settings;
152
    default:
153
      break;
154
  }
155
}
156
157
/**
158
 * Batch processing callback; Generate aliases for taxonomy terms.
159
 */
160
function taxonomy_pathauto_bulk_update_batch_process(&$context) {
161
  if (!isset($context['sandbox']['current'])) {
162
    $context['sandbox']['count'] = 0;
163
    $context['sandbox']['current'] = 0;
164
  }
165
166
  $query = db_select('taxonomy_term_data', 'td');
167
  $query->leftJoin('url_alias', 'ua', "CONCAT('taxonomy/term/', td.tid) = ua.source");
168
  $query->addField('td', 'tid');
169
  $query->isNull('ua.source');
170
  $query->condition('td.tid', $context['sandbox']['current'], '>');
171
  // Exclude the forums terms.
172
  if ($forum_vid = variable_get('forum_nav_vocabulary', '')) {
173
    $query->condition('td.vid', $forum_vid, '<>');
174
  }
175
  $query->orderBy('td.tid');
176
  $query->addTag('pathauto_bulk_update');
177
  $query->addMetaData('entity', 'taxonomy_term');
178
179
  // Get the total amount of items to process.
180
  if (!isset($context['sandbox']['total'])) {
181
    $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
182
183
    // If there are no nodes to update, the stop immediately.
184
    if (!$context['sandbox']['total']) {
185
      $context['finished'] = 1;
186
      return;
187
    }
188
  }
189
190
  $query->range(0, 25);
191
  $tids = $query->execute()->fetchCol();
192
193
  pathauto_taxonomy_term_update_alias_multiple($tids, 'bulkupdate');
194
  $context['sandbox']['count'] += count($tids);
195
  $context['sandbox']['current'] = max($tids);
196
  $context['message'] = t('Updated alias for term @tid.', array('@tid' => end($tids)));
197
198
  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
199
    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
200
  }
201
}
202
203
/**
204
 * Implements hook_pathauto() for forum module.
205
 */
206
function forum_pathauto($op) {
207
  switch ($op) {
208
    case 'settings':
209
      $settings = array();
210
      $settings['module'] = 'forum';
211
      $settings['token_type'] = 'term';
212
      $settings['groupheader'] = t('Forum paths');
213
      $settings['patterndescr'] = t('Pattern for forums and forum containers');
214
      $settings['patterndefault'] = '[term:vocabulary]/[term:name]';
215
      $settings['batch_update_callback'] = 'forum_pathauto_bulk_update_batch_process';
216
      $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
217
      return (object) $settings;
218
    default:
219
      break;
220
  }
221
}
222
223
/**
224
 * Batch processing callback; Generate aliases for forums.
225
 */
226
function forum_pathauto_bulk_update_batch_process(&$context) {
227
  if (!isset($context['sandbox']['current'])) {
228
    $context['sandbox']['count'] = 0;
229
    $context['sandbox']['current'] = 0;
230
  }
231
232
  $query = db_select('taxonomy_term_data', 'td');
233
  $query->leftJoin('url_alias', 'ua', "CONCAT('forum/', td.tid) = ua.source");
234
  $query->addField('td', 'tid');
235
  $query->isNull('ua.source');
236
  $query->condition('td.tid', $context['sandbox']['current'], '>');
237
  $query->condition('td.vid', variable_get('forum_nav_vocabulary', ''));
238
  $query->orderBy('td.tid');
239
  $query->addTag('pathauto_bulk_update');
240
  $query->addMetaData('entity', 'taxonomy_term');
241
242
  // Get the total amount of items to process.
243
  if (!isset($context['sandbox']['total'])) {
244
    $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
245
246
    // If there are no nodes to update, the stop immediately.
247
    if (!$context['sandbox']['total']) {
248
      $context['finished'] = 1;
249
      return;
250
    }
251
  }
252
253
  $query->range(0, 25);
254
  $tids = $query->execute()->fetchCol();
255
256
  pathauto_taxonomy_term_update_alias_multiple($tids, 'bulkupdate');
257
  $context['sandbox']['count'] += count($tids);
258
  $context['sandbox']['current'] = max($tids);
259
  $context['message'] = t('Updated alias for forum @tid.', array('@tid' => end($tids)));
260
261
  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
262
    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
263
  }
264
}
265
266
/**
267
 * Implements hook_pathauto().
268
 */
269
function user_pathauto($op) {
270
  switch ($op) {
271
    case 'settings':
272
      $settings = array();
273
      $settings['module'] = 'user';
274
      $settings['token_type'] = 'user';
275
      $settings['groupheader'] = t('User paths');
276
      $settings['patterndescr'] = t('Pattern for user account page paths');
277
      $settings['patterndefault'] = 'users/[user:name]';
278
      $settings['batch_update_callback'] = 'user_pathauto_bulk_update_batch_process';
279
      $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
280
      return (object) $settings;
281
    default:
282
      break;
283
  }
284
}
285
286
/**
287
 * Batch processing callback; Generate aliases for users.
288
 */
289
function user_pathauto_bulk_update_batch_process(&$context) {
290
  if (!isset($context['sandbox']['current'])) {
291
    $context['sandbox']['count'] = 0;
292
    $context['sandbox']['current'] = 0;
293
  }
294
295
  $query = db_select('users', 'u');
296
  $query->leftJoin('url_alias', 'ua', "CONCAT('user/', u.uid) = ua.source");
297
  $query->addField('u', 'uid');
298
  $query->isNull('ua.source');
299
  $query->condition('u.uid', $context['sandbox']['current'], '>');
300
  $query->orderBy('u.uid');
301
  $query->addTag('pathauto_bulk_update');
302
  $query->addMetaData('entity', 'user');
303
304
  // Get the total amount of items to process.
305
  if (!isset($context['sandbox']['total'])) {
306
    $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
307
308
    // If there are no nodes to update, the stop immediately.
309
    if (!$context['sandbox']['total']) {
310
      $context['finished'] = 1;
311
      return;
312
    }
313
  }
314
315
  $query->range(0, 25);
316
  $uids = $query->execute()->fetchCol();
317
318
  pathauto_user_update_alias_multiple($uids, 'bulkupdate', array('alias blog' => FALSE));
319
  $context['sandbox']['count'] += count($uids);
320
  $context['sandbox']['current'] = max($uids);
321
  $context['message'] = t('Updated alias for user @uid.', array('@uid' => end($uids)));
322
323
  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
324
    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
325
  }
326
}
327
328
/**
329
 * Implements hook_pathauto().
330
 */
331
function blog_pathauto($op) {
332
  switch ($op) {
333
    case 'settings':
334
      $settings = array();
335
      $settings['module'] = 'blog';
336
      $settings['token_type'] = 'user';
337
      $settings['groupheader'] = t('Blog paths');
338
      $settings['patterndescr'] = t('Pattern for blog page paths');
339
      $settings['patterndefault'] = 'blogs/[user:name]';
340
      $settings['batch_update_callback'] = 'blog_pathauto_bulk_update_batch_process';
341
      $settings['batch_file'] = drupal_get_path('module', 'pathauto') . '/pathauto.pathauto.inc';
342
      return (object) $settings;
343
    default:
344
      break;
345
  }
346
}
347
348
/**
349
 * Batch processing callback; Generate aliases for blogs.
350
 */
351
function blog_pathauto_bulk_update_batch_process(&$context) {
352
  if (!isset($context['sandbox']['current'])) {
353
    $context['sandbox']['count'] = 0;
354
    $context['sandbox']['current'] = 0;
355
  }
356
357
  $query = db_select('users', 'u');
358
  $query->leftJoin('url_alias', 'ua', "CONCAT('blog/', u.uid) = ua.source");
359
  $query->addField('u', 'uid');
360
  $query->isNull('ua.source');
361
  $query->condition('u.uid', $context['sandbox']['current'], '>');
362
  $query->orderBy('u.uid');
363
  $query->addTag('pathauto_bulk_update');
364
  $query->addMetaData('entity', 'user');
365
366
  // Get the total amount of items to process.
367
  if (!isset($context['sandbox']['total'])) {
368
    $context['sandbox']['total'] = $query->countQuery()->execute()->fetchField();
369
370
    // If there are no nodes to update, the stop immediately.
371
    if (!$context['sandbox']['total']) {
372
      $context['finished'] = 1;
373
      return;
374
    }
375
  }
376
377
  $query->range(0, 25);
378
  $uids = $query->execute()->fetchCol();
379
380
  $accounts = user_load_multiple($uids);
381
  foreach ($accounts as $account) {
382
    pathauto_blog_update_alias($account, 'bulkupdate');
383
  }
384
385
  $context['sandbox']['count'] += count($uids);
386
  $context['sandbox']['current'] = max($uids);
387
  $context['message'] = t('Updated alias for blog user @uid.', array('@uid' => end($uids)));
388
389
  if ($context['sandbox']['count'] != $context['sandbox']['total']) {
390
    $context['finished'] = $context['sandbox']['count'] / $context['sandbox']['total'];
391
  }
392
}