Projet

Général

Profil

Paste
Télécharger (19,5 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / diff / diff.module @ a5572547

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Provides functionality to show a diff between two node revisions.
6
 */
7
8
/**
9
 * Number of items on one page of the revision list.
10
 */
11
define('REVISION_LIST_SIZE', 50);
12
13
/**
14
 * Exposed sorting options.
15
 *
16
 * No sorting means sorting by delta value for fields.
17
 */
18
define('DIFF_SORT_NONE', '0');
19
20
/**
21
 * Exposed sorting options.
22
 *
23
 * This normally sorts by the rendered comparison.
24
 */
25
define('DIFF_SORT_VALUE', '1');
26
27
/**
28
 * Exposed sorting options.
29
 *
30
 * It is up to the field / entity to decide how to handle the sort.
31
 */
32
define('DIFF_SORT_CUSTOM', '-1');
33
34
/**
35
 * Implements hook_help().
36
 */
37
function diff_help($path, $arg) {
38
  switch ($path) {
39
    case 'admin/help#diff':
40
      $output = '<p>' . t('The Diff module replaces the normal <em>Revisions</em> node tab. Diff enhances the listing of revisions with an option to view the differences between any two content revisions. Access to this feature is controlled with the <em>View revisions</em> permission. The feature can be disabled for an entire content type on the content type configuration page. Diff also provides an optional <em>View changes</em> button while editing a node.') . '</p>';
41
      return $output;
42
    case 'node/%/revisions/%/view':
43
      // The translated strings should match node_help('node/%/revisions').
44
      return '<p>' . t('Revisions allow you to track differences between multiple versions of your content, and revert back to older versions.') . '</p>';
45
    case 'node/%/revisions/view/%/%':
46
      return '<p>' . t('Comparing two revisions:') . '</p>';
47
  }
48
}
49
50
/**
51
 * The various states that are available.
52
 */
53
function diff_available_states($entity_type = NULL) {
54
  $states = array(
55
    'raw' => t('Standard'),
56
    'raw_plain' => t('Marked down'),
57
  );
58
59
  return $states;
60
}
61
62
/**
63
 * Implements hook_menu().
64
 *
65
 * @todo: Review this.
66
 */
67
function diff_menu() {
68
  /*
69
   * By using MENU_LOCAL_TASK (and 'tab_parent') we can get the various
70
   * revision-views to show the View|Edit|Revision-tabs of the node on top,
71
   * and have the Revisions-tab open. To avoid creating/showing any extra tabs
72
   * or sub-tabs (tasks below top level) for the various paths (i.e. "Diff",
73
   * "Show latest" and "Show a specific revision") that need a revision-id (vid)
74
   * parameter, we make sure to set 'tab_parent' a bit odd. This solution may
75
   * not be the prettiest one, but by avoiding having two _LOCAL_TASKs sharing
76
   * a parent that can be accessed by its full path, it seems to work as
77
   * desired. Breadcrumbs work decently, at least the node link is among the
78
   * crumbs. For some reason any breadcrumbs "before/above" the node is only
79
   * seen at 'node/%node/revisions/%/view'.
80
   */
81
82
  // Not used directly, but was created to get the other menu items to work.
83
  $items['node/%node/revisions/list'] = array(
84
    'title' => 'List revisions',
85
    'page callback' => 'diff_diffs_overview',
86
    'type' => MENU_DEFAULT_LOCAL_TASK,
87
    'access callback' => 'diff_node_revision_access',
88
    'access arguments' => array(1),
89
    'file' => 'diff.pages.inc',
90
  );
91
  $items['node/%node/revisions/view'] = array(
92
    'title' => 'Compare revisions',
93
    'page callback' => 'diff_diffs_show',
94
    'page arguments' => array(1, 4, 5, 6),
95
    'type' => MENU_LOCAL_TASK,
96
    'access callback' => 'diff_node_revision_access',
97
    'access arguments' => array(1),
98
    'tab_parent' => 'node/%/revisions/list',
99
    'file' => 'diff.pages.inc',
100
  );
101
102
  $items['node/%node/revisions/view/latest'] = array(
103
    'title' => 'Show latest difference',
104
    'page callback' => 'diff_latest',
105
    'page arguments' => array(1),
106
    'type' => MENU_LOCAL_TASK,
107
    'access callback' => 'diff_node_revision_access',
108
    'access arguments' => array(1),
109
    'tab_parent' => 'node/%/revisions/view',
110
    'file' => 'diff.pages.inc',
111
  );
112
113
  // Administrative settings.
114
  $items['admin/config/content/diff'] = array(
115
    'title' => 'Diff',
116
    'description' => 'Diff settings.',
117
    'file' => 'diff.admin.inc',
118
    'page callback' => 'drupal_get_form',
119
    'page arguments' => array('diff_admin_settings'),
120
    'access arguments' => array('administer site configuration'),
121
  );
122
  $items['admin/config/content/diff/settings'] = array(
123
    'title' => 'Settings',
124
    'type' => MENU_DEFAULT_LOCAL_TASK,
125
    'weight' => -10,
126
  );
127
  $items['admin/config/content/diff/fields'] = array(
128
    'title' => 'Fields',
129
    'description' => 'Field support and settings overview.',
130
    'file' => 'diff.admin.inc',
131
    'page callback' => 'diff_admin_field_overview',
132
    'access arguments' => array('administer site configuration'),
133
    'type' => MENU_LOCAL_TASK,
134
  );
135
  $items['admin/config/content/diff/fields/%'] = array(
136
    'title' => 'Global field settings',
137
    'page callback' => 'drupal_get_form',
138
    'page arguments' => array('diff_admin_global_field_settings', 5),
139
    'access arguments' => array('administer site configuration'),
140
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
141
    'file' => 'diff.admin.inc',
142
  );
143
144
  $items['admin/config/content/diff/entities'] = array(
145
    'title' => 'Entities',
146
    'description' => 'Entity settings.',
147
    'file' => 'diff.admin.inc',
148
    'page callback' => 'drupal_get_form',
149
    'page arguments' => array('diff_admin_global_entity_settings', 'node'),
150
    'access arguments' => array('administer site configuration'),
151
    'type' => MENU_LOCAL_TASK,
152
  );
153
154
  $items['admin/config/content/diff/entities/node'] = array(
155
    'title' => 'Node',
156
    'type' => MENU_DEFAULT_LOCAL_TASK,
157
    'weight' => -10,
158
  );
159
160
  return $items;
161
}
162
163
/**
164
 * Implements hook_menu_alter().
165
 */
166
function diff_menu_alter(&$callbacks) {
167
  // Overwrite the default 'Revisions' page.
168
  $callbacks['node/%node/revisions']['page callback'] = 'diff_diffs_overview';
169
  $callbacks['node/%node/revisions']['module'] = 'diff';
170
  $callbacks['node/%node/revisions']['file'] = 'diff.pages.inc';
171
172
  $callbacks['node/%node/revisions/%/view']['tab_parent'] = 'node/%/revisions/list';
173
  $callbacks['node/%node/revisions/%/revert']['tab_parent'] = 'node/%/revisions/%/view';
174
  $callbacks['node/%node/revisions/%/delete']['tab_parent'] = 'node/%/revisions/%/view';
175
176
  $callbacks['node/%node/revisions']['access callback']
177
      = $callbacks['node/%node/revisions/%/view']['access callback']
178
      = $callbacks['node/%node/revisions/%/revert']['access callback']
179
      = $callbacks['node/%node/revisions/%/delete']['access callback'] = 'diff_node_revision_access';
180
}
181
182
/**
183
 * Implements hook_admin_paths_alter().
184
 */
185
function diff_admin_paths_alter(&$paths) {
186
  // By default, treat all diff pages as administrative.
187
  if (variable_get('diff_admin_path_node', 1)) {
188
    $paths['node/*/revisions/view/*/*'] = TRUE;
189
  }
190
}
191
192
/**
193
 * Access callback for the node revisions page.
194
 */
195
function diff_node_revision_access($node, $op = 'view') {
196
  $may_revision_this_type = variable_get('diff_enable_revisions_page_node_' . $node->type, TRUE) || user_access('administer nodes');
197
  return $may_revision_this_type && _node_revision_access($node, $op);
198
}
199
200
/**
201
 * Implements hook_hook_info().
202
 */
203
function diff_hook_info() {
204
  $hooks['entity_diff'] = array(
205
    'group' => 'diff',
206
  );
207
  $hooks['diff'] = array(
208
    'group' => 'diff',
209
  );
210
  $hooks['field_diff_view_prepare_alter'] = array(
211
    'group' => 'diff',
212
  );
213
  $hooks['field_diff_view_alter'] = array(
214
    'group' => 'diff',
215
  );
216
217
  return $hooks;
218
}
219
220
/**
221
 * Implements hook_entity_info_alter().
222
 *
223
 * Although the module only provides an UI for comparing nodes, it has an
224
 * extendable API for any entity, so we supply two view modes for all entities.
225
 * - diff_standard: This view mode is used to tell the module how to compare
226
 *                  individual fields. This is used on the revisions page.
227
 */
228
function diff_entity_info_alter(&$entity_info) {
229
  foreach (array_keys($entity_info) as $entity_type) {
230
    if (!empty($entity_info[$entity_type]['view modes'])) {
231
      $entity_info[$entity_type]['view modes'] += array(
232
        'diff_standard' => array(
233
          'label' => t('Revision comparison'),
234
          'custom settings' => FALSE,
235
        ),
236
      );
237
    }
238
  }
239
}
240
241
/**
242
 * Implements hook_block_info().
243
 */
244
function diff_block_info() {
245
  return array(
246
    'inline' => array(
247
      'info' => t('Inline differences'),
248
    ),
249
  );
250
}
251
252
/**
253
 * Implements hook_block_configure().
254
 */
255
function diff_block_configure($delta = '') {
256
  $form = array();
257
  switch ($delta) {
258
    case 'inline':
259
      $form['bundles'] = array(
260
        '#type' => 'checkboxes',
261
        '#title' => t('Enabled content types'),
262
        '#default_value' => variable_get('diff_show_diff_inline_node_bundles', array()),
263
        '#options' => node_type_get_names(),
264
        '#description' => t('Show this block only on pages that display content of the given type(s).'),
265
      );
266
      break;
267
  }
268
  return $form;
269
}
270
271
/**
272
 * Implements hook_block_save().
273
 */
274
function diff_block_save($delta = '', $edit = array()) {
275
  switch ($delta) {
276
    case 'inline':
277
      variable_set('diff_show_diff_inline_node_bundles', $edit['bundles']);
278
      break;
279
  }
280
}
281
282
/**
283
 * Implements hook_block_view().
284
 */
285
function diff_block_view($delta) {
286
  if ($delta === 'inline' && user_access('view revisions') && ($node = menu_get_object()) && arg(2) !== 'edit') {
287
    $enabled_types = variable_get('diff_show_diff_inline_node_bundles', array());
288
    if (!empty($enabled_types[$node->type])) {
289
      $block = array();
290
      $revisions = node_revision_list($node);
291
      if (count($revisions) > 1) {
292
        $block['subject'] = t('Highlight changes');
293
        $block['content'] = drupal_get_form('diff_inline_form', $node, $revisions);
294
      }
295
      return $block;
296
    }
297
  }
298
}
299
300
/**
301
 * Implements hook_node_view_alter().
302
 */
303
function diff_node_view_alter(&$build) {
304
  $node = $build['#node'];
305
  if (user_access('view revisions') && in_array($node->type, variable_get('diff_show_diff_inline_node_bundles', array()))) {
306
    // Ugly but cheap way to check that we are viewing a node's revision page.
307
    if (arg(2) === 'revisions' && arg(3) === $node->vid) {
308
      module_load_include('inc', 'diff', 'diff.pages');
309
      $old_vid = _diff_get_previous_vid(node_revision_list($node), $node->vid);
310
      $build = array('#markup' => diff_inline_show($node, $old_vid));
311
    }
312
    $build['#prefix'] = isset($build['#prefix']) ? "<div id='diff-inline-{$node->nid}'>" . $build['#prefix'] : "<div id='diff-inline-{$node->nid}'>";
313
    $build['#suffix'] = isset($build['#suffix']) ? $build['#suffix'] . "</div>" : "</div>";
314
  }
315
}
316
317
/**
318
 * Implements hook_form_BASE_FORM_ID_alter().
319
 */
320
function diff_form_node_form_alter(&$form, $form_state) {
321
  // Add a 'View changes' button on the node edit form.
322
  $node = $form['#node'];
323
  if (variable_get('diff_show_preview_changes_node_' . $node->type, TRUE) && !empty($node->nid)) {
324
    $form['actions']['preview_changes'] = array(
325
      '#type' => 'submit',
326
      '#value' => t('View changes'),
327
      '#weight' => 12,
328
      '#submit' => array('diff_node_form_build_preview_changes'),
329
    );
330
  }
331
}
332
333
/**
334
 * Implements hook_form_BASE_FORM_ID_alter().
335
 */
336
function diff_form_node_type_form_alter(&$form, $form_state) {
337
  if (isset($form['type'])) {
338
    $type = $form['#node_type'];
339
    $form['diff'] = array(
340
      '#title' => t('Compare revisions'),
341
      '#type' => 'fieldset',
342
      '#group' => 'additional_settings',
343
      '#tree' => FALSE,
344
    );
345
    $form['diff']['diff_show_preview_changes_node'] = array(
346
      '#type' => 'checkbox',
347
      '#title' => t('Show <em>View changes</em> button on node edit form'),
348
      '#weight' => 10,
349
      '#default_value' => variable_get('diff_show_preview_changes_node_' . $type->type, TRUE),
350
    );
351
    $form['diff']['diff_enable_revisions_page_node'] = array(
352
      '#type' => 'checkbox',
353
      '#title' => t('Enable the <em>Revisions</em> page for this content type'),
354
      '#weight' => 11,
355
      '#default_value' => variable_get('diff_enable_revisions_page_node_' . $type->type, TRUE),
356
    );
357
    $options = array();
358
    $info = entity_get_info('node');
359
    foreach ($info['view modes'] as $view_mode => $view_mode_info) {
360
      $options[$view_mode] = $view_mode_info['label'];
361
    }
362
    $form['diff']['diff_view_mode_preview_node'] = array(
363
      '#type' => 'select',
364
      '#title' => t('Standard comparison preview'),
365
      '#description' => t('Governs the <em>Current revision</em> view mode when doing standard comparisons.'),
366
      '#options' => $options,
367
      '#weight' => 13,
368
      '#default_value' => variable_get('diff_view_mode_preview_node_' . $type->type, 'full'),
369
      '#empty_value' => '',
370
      '#empty_option' => t('- Do not display -'),
371
    );
372
  }
373
}
374
375
/**
376
 * Implements hook_node_type_update().
377
 *
378
 * This tracks the diff settings in case the node content type is renamed.
379
 */
380
function diff_node_type_update($info) {
381
  if (!empty($info->old_type) && $info->old_type != $info->type) {
382
    $type_variables = array(
383
      'diff_show_preview_changes_node',
384
      'diff_enable_revisions_page_node',
385
      'diff_view_mode_preview_node',
386
    );
387
    foreach ($type_variables as $prefix) {
388
      $setting = variable_get($prefix . '_' . $info->old_type, NULL);
389
      if (isset($setting)) {
390
        variable_del($prefix . '_' . $info->old_type);
391
        variable_set($prefix . '_' . $info->type, $setting);
392
      }
393
    }
394
395
    // Block settings are combined in a single variable.
396
    $inline_block_types = variable_get('diff_show_diff_inline_node_bundles', array());
397
    if (isset($inline_block_types[$info->old_type])) {
398
      if (!empty($inline_block_types[$info->old_type])) {
399
        $inline_block_types[$info->type] = $info->type;
400
      }
401
      unset($inline_block_types[$info->old_type]);
402
      variable_set('diff_show_diff_inline_node_bundles', $inline_block_types);
403
    }
404
  }
405
}
406
407
/**
408
 * Implements hook_node_type_delete().
409
 */
410
function diff_node_type_delete($info) {
411
  variable_del('diff_show_preview_changes_node_' . $info->type);
412
  variable_del('diff_enable_revisions_page_node_' . $info->type);
413
  variable_del('diff_view_mode_preview_node_' . $info->type);
414
}
415
416
/**
417
 * Submit handler for the 'View changes' action.
418
 *
419
 * @see node_form_build_preview()
420
 */
421
function diff_node_form_build_preview_changes($form, &$form_state) {
422
  module_load_include('inc', 'diff', 'diff.pages');
423
  $old_node = clone node_load($form_state['values']['nid']);
424
  $node = node_form_submit_build_node($form, $form_state);
425
426
  // Create diff of old node and edited node.
427
  $rows = _diff_body_rows($old_node, $node);
428
429
  $header = _diff_default_header(t('Original'), t('Changes'));
430
  $changes = theme('table__diff__preview', array(
431
    'header' => $header,
432
    'rows' => $rows,
433
    'attributes' => array('class' => 'diff'),
434
    'colgroups' => _diff_default_cols(),
435
    'sticky' => FALSE,
436
  ));
437
438
  // Prepend diff to edit form.
439
  $form_state['node_preview'] = $changes;
440
  $form_state['rebuild'] = TRUE;
441
}
442
443
/**
444
 * Implements hook_theme().
445
 */
446
function diff_theme() {
447
  return array(
448
    'diff_node_revisions' => array(
449
      'render element' => 'form',
450
      'file' => 'diff.theme.inc',
451
    ),
452
    'diff_header_line' => array(
453
      'arguments' => array('lineno' => NULL),
454
      'file' => 'diff.theme.inc',
455
    ),
456
    'diff_content_line' => array(
457
      'arguments' => array('line' => NULL),
458
      'file' => 'diff.theme.inc',
459
    ),
460
    'diff_empty_line' => array(
461
      'arguments' => array('line' => NULL),
462
      'file' => 'diff.theme.inc',
463
    ),
464
    'diff_inline_form' => array(
465
      'render element' => 'form',
466
      'file' => 'diff.theme.inc',
467
    ),
468
    'diff_inline_metadata' => array(
469
      'arguments' => array('node' => NULL),
470
      'file' => 'diff.theme.inc',
471
    ),
472
    'diff_inline_chunk' => array(
473
      'arguments' => array('text' => '', 'type' => NULL),
474
      'file' => 'diff.theme.inc',
475
    ),
476
  );
477
}
478
479
/**
480
 * Render the table rows for theme('table').
481
 *
482
 * @param string $a
483
 *   The source string to compare from.
484
 * @param string $b
485
 *   The target string to compare to.
486
 * @param boolean $show_header
487
 *   Display diff context headers. For example, "Line x".
488
 * @param array $line_stats
489
 *   This structure tracks line numbers across multiple calls to DiffFormatter.
490
 *
491
 * @return array
492
 *   Array of rows usable with theme('table').
493
 */
494
function diff_get_rows($a, $b, $show_header = FALSE, &$line_stats = NULL) {
495
  $a = is_array($a) ? $a : explode("\n", $a);
496
  $b = is_array($b) ? $b : explode("\n", $b);
497
498
  if (!isset($line_stats)) {
499
    $line_stats = array(
500
      'counter' => array('x' => 0, 'y' => 0),
501
      'offset' => array('x' => 0, 'y' => 0),
502
    );
503
  }
504
  $formatter = new DrupalDiffFormatter();
505
  // Header is the line counter.
506
  $formatter->show_header = $show_header;
507
  $formatter->line_stats = &$line_stats;
508
  $diff = new Diff($a, $b);
509
  return $formatter->format($diff);
510
}
511
512
/**
513
 * Render and markup a diff of two strings into HTML markup.
514
 *
515
 * @param string $a
516
 *   The source string to compare from.
517
 * @param string $b
518
 *   The target string to compare to.
519
 *
520
 * @return string
521
 *   String containing HTML markup.
522
 */
523
function diff_get_inline($a, $b) {
524
  $diff = new DrupalDiffInline($a, $b);
525
  return $diff->render();
526
}
527
528
/**
529
 * Form builder: Inline diff controls.
530
 */
531
function diff_inline_form($form, $form_state, $node, $revisions) {
532
  $form = array();
533
  $form['node'] = array(
534
    '#type' => 'value',
535
    '#value' => $node,
536
  );
537
  $form['revision'] = array(
538
    '#type' => 'select',
539
    '#options' => array(0 => t('- No highlighting -')),
540
    '#default_value' => (arg(2) === 'revisions' && arg(3) === $node->vid) ? $node->vid : 0,
541
    '#ajax' => array(
542
      'callback' => 'diff_inline_ajax',
543
      'wrapper' => "node-{$node->nid}",
544
      'method' => 'replace',
545
    ),
546
  );
547
  foreach ($revisions as $revision) {
548
    $form['revision']['#options'][$revision->vid] = t('@revision by @name', array(
549
      '@revision' => format_date($revision->timestamp, 'short'),
550
      '@name' => format_username($revision),
551
    ));
552
  }
553
  $form['submit'] = array(
554
    '#type' => 'submit',
555
    '#value' => t('View'),
556
    '#submit' => array('diff_inline_form_submit'),
557
    '#attributes' => array('class' => array('diff-js-hidden')),
558
  );
559
  return $form;
560
}
561
562
/**
563
 * AHAH callback for rendering the inline diff of a node.
564
 */
565
function diff_inline_ajax($form, $form_state) {
566
  module_load_include('inc', 'diff', 'diff.pages');
567
  $node = $form['node']['#value'];
568
  $vid = isset($form_state['values']['revision']) ? $form_state['values']['revision'] : 0;
569
  return "<div id='node-{$node->nid}'>" . diff_inline_show($node, $vid) . "</div>";
570
}
571
572
/**
573
 * Form submission handler for diff_inline_form() for JS-disabled clients.
574
 */
575
function diff_inline_form_submit(&$form, &$form_state) {
576
  if (isset($form_state['values']['revision'], $form_state['values']['node'])) {
577
    $node = $form_state['values']['node'];
578
    $vid = $form_state['values']['revision'];
579
    $form_state['redirect'] = "node/{$node->nid}/revisions/{$vid}/view";
580
  }
581
}
582
583
/**
584
 * A helper function to normalise system differences.
585
 *
586
 * This handles differences in:
587
 * - line endings: Mac, Windows and UNIX all use different line endings.
588
 */
589
function diff_normalise_text($text) {
590
  $text = str_replace(array("\r\n", "\r"), "\n", $text);
591
  return $text;
592
}
593
594
/**
595
 * A wrapper function for filter_xss() to exclude all tags.
596
 */
597
function diff_filter_xss($string) {
598
  return filter_xss($string, array());
599
}
600
601
/**
602
 * Helper function to load any CSS or JScript files required by a page or form.
603
 */
604
function diff_build_attachments($jscript = FALSE) {
605
  $attachments = array();
606
  $theme = variable_get('diff_theme', 'default');
607
  if ($theme) {
608
    $attachments['css'] = array(
609
      drupal_get_path('module', 'diff') . "/css/diff.{$theme}.css",
610
    );
611
  }
612
  $type = variable_get('diff_radio_behavior', 'simple');
613
  if ($jscript && $type) {
614
    $attachments['js'] = array(
615
      drupal_get_path('module', 'diff') . "/js/diff.js",
616
      array(
617
        'data' => array('diffRevisionRadios' => $type),
618
        'type' => 'setting',
619
      ),
620
    );
621
  }
622
  return $attachments;
623
}