Projet

Général

Profil

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

root / drupal7 / sites / all / modules / diff / diff.pages.inc @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Menu callbacks for hook_menu().
6
 */
7

    
8
/**
9
 * Menu callback - show latest diff for a given node.
10
 */
11
function diff_latest($node) {
12
  $revisions = node_revision_list($node);
13
  $new = array_shift($revisions);
14
  $old = array_shift($revisions);
15
  drupal_goto("node/{$node->nid}/revisions/view/{$old->vid}/{$new->vid}");
16
}
17

    
18
/**
19
 * Menu callback - an overview table of older revisions.
20
 *
21
 * Generate an overview table of older revisions of a node and provide
22
 * an input form to select two revisions for a comparison.
23
 */
24
function diff_diffs_overview($node) {
25
  drupal_set_title(t('Revisions for %title', array('%title' => $node->title)), PASS_THROUGH);
26
  return drupal_get_form('diff_node_revisions', $node);
27
}
28

    
29
/**
30
 * Input form to select two revisions.
31
 */
32
function diff_node_revisions($form, $form_state, $node) {
33
  $form['nid'] = array(
34
    '#type' => 'hidden',
35
    '#value' => $node->nid,
36
  );
37

    
38
  $revision_list = node_revision_list($node);
39

    
40
  if (count($revision_list) > REVISION_LIST_SIZE) {
41
    // If the list of revisions is longer than the number shown on one page
42
    // split the array.
43
    $page = isset($_GET['page']) ? $_GET['page'] : '0';
44
    $revision_chunks = array_chunk(node_revision_list($node), REVISION_LIST_SIZE);
45
    $revisions = $revision_chunks[$page];
46
    // Set up global pager variables as would 'pager_query' do.
47
    // These variables are then used in the theme('pager') call later.
48
    global $pager_page_array, $pager_total, $pager_total_items;
49
    $pager_total_items[0] = count($revision_list);
50
    $pager_total[0] = ceil(count($revision_list) / REVISION_LIST_SIZE);
51
    $pager_page_array[0] = max(0, min($page, ((int) $pager_total[0]) - 1));
52
  }
53
  else {
54
    $revisions = $revision_list;
55
  }
56

    
57
  $revert_permission = FALSE;
58
  if ((user_access('revert revisions') || user_access('administer nodes')) && node_access('update', $node)) {
59
    $revert_permission = TRUE;
60
  }
61
  $delete_permission = FALSE;
62
  if ((user_access('delete revisions') || user_access('administer nodes')) && node_access('delete', $node)) {
63
    $delete_permission = TRUE;
64
  }
65

    
66
  foreach ($revisions as $revision) {
67
    $operations = array();
68
    $revision_ids[$revision->vid] = '';
69

    
70
    $revision_log = ($revision->log != '') ? '<p class="revision-log">' . filter_xss($revision->log) . '</p>' : '';
71
    if ($revision->current_vid > 0) {
72
      $form['info'][$revision->vid] = array(
73
        '#markup' => t('!date by !username', array(
74
          '!date' => l(format_date($revision->timestamp, 'small'), "node/$node->nid"),
75
          '!username' => theme('username', array('account' => $revision)))) . $revision_log,
76
      );
77
    }
78
    else {
79
      $diff_date = l(format_date($revision->timestamp, 'small'), "node/$node->nid/revisions/$revision->vid/view");
80
      $form['info'][$revision->vid] = array(
81
        '#markup' => t('!date by !username', array(
82
          '!date' => $diff_date,
83
          '!username' => theme('username', array('account' => $revision)))
84
        ) . $revision_log,
85
      );
86
      if ($revert_permission) {
87
        $operations[] = array(
88
          '#markup' => l(t('Revert'), "node/$node->nid/revisions/$revision->vid/revert"),
89
        );
90
      }
91
      if ($delete_permission) {
92
        $operations[] = array(
93
          '#markup' => l(t('Delete'), "node/$node->nid/revisions/$revision->vid/delete"),
94
        );
95
      }
96
      // Set a dummy, even if the user has no permission for the other
97
      // operations, so that we can check if the operations array is
98
      // empty to know if the row denotes the current revision.
99
      $operations[] = array();
100
    }
101
    $form['operations'][$revision->vid] = $operations;
102

    
103
  }
104
  $new_vid = key($revision_ids);
105
  next($revision_ids);
106
  $old_vid = key($revision_ids);
107
  $form['diff']['old'] = array(
108
    '#type' => 'radios',
109
    '#options' => $revision_ids,
110
    '#default_value' => $old_vid,
111
  );
112
  $form['diff']['new'] = array(
113
    '#type' => 'radios',
114
    '#options' => $revision_ids,
115
    '#default_value' => $new_vid,
116
  );
117

    
118
  $form['submit'] = array('#type' => 'submit', '#value' => t('Compare'));
119

    
120
  if (count($revision_list) > REVISION_LIST_SIZE) {
121
    $form['#suffix'] = theme('pager');
122
  }
123
  $form['#attached'] = diff_build_attachments(TRUE);
124
  return $form;
125
}
126

    
127
/**
128
 * Submit code for input form to select two revisions.
129
 */
130
function diff_node_revisions_submit($form, &$form_state) {
131
  // The ids are ordered so the old revision is always on the left.
132
  $old_vid = min($form_state['values']['old'], $form_state['values']['new']);
133
  $new_vid = max($form_state['values']['old'], $form_state['values']['new']);
134
  $form_state['redirect'] = 'node/' . $form_state['values']['nid'] . '/revisions/view/' . $old_vid . '/' . $new_vid;
135
}
136

    
137
/**
138
 * Validation for input form to select two revisions.
139
 */
140
function diff_node_revisions_validate($form, &$form_state) {
141
  $old_vid = $form_state['values']['old'];
142
  $new_vid = $form_state['values']['new'];
143
  if ($old_vid == $new_vid || !$old_vid || !$new_vid) {
144
    form_set_error('diff', t('Select different revisions to compare.'));
145
  }
146
}
147

    
148
/**
149
 * Create a comparison for the node between versions 'old_vid' and 'new_vid'.
150
 *
151
 * @param object $node
152
 *   Node on which to perform comparison
153
 * @param integer $old_vid
154
 *   Version ID of the old revision.
155
 * @param integer $new_vid
156
 *   Version ID of the new revision.
157
 */
158
function diff_diffs_show($node, $old_vid, $new_vid, $state = NULL) {
159
  // Attaches the CSS.
160
  $build['#attached'] = diff_build_attachments();
161

    
162
  $default_state = variable_get('diff_default_state_node', 'raw');
163
  if (empty($state)) {
164
  	$state = $default_state;
165
  }
166
  $state = str_replace('-', '_', $state);
167
  if (!array_key_exists($state, diff_available_states())) {
168
    $state = $default_state;
169
  }
170

    
171
  // Same title as the 'Revisions' tab. Blocked by non-page requests.
172
  if (node_is_page($node)) {
173
    drupal_set_title(t('Revisions for %title', array('%title' => $node->title)), PASS_THROUGH);
174
  }
175
  $node_revisions = node_revision_list($node);
176

    
177
  $old_node = node_load($node->nid, $old_vid);
178
  $new_node = node_load($node->nid, $new_vid);
179

    
180
  // Generate table header (date, username, log message).
181
  $old_header = t('!date by !username', array(
182
    '!date' => l(format_date($old_node->revision_timestamp), "node/$node->nid/revisions/$old_node->vid/view", array('absolute' => 1)),
183
    '!username' => theme('username', array('account' => $node_revisions[$old_vid])),
184
  ));
185
  $new_header = t('!date by !username', array(
186
    '!date' => l(format_date($new_node->revision_timestamp), "node/$node->nid/revisions/$new_node->vid/view", array('absolute' => 1)),
187
    '!username' => theme('username', array('account' => $node_revisions[$new_vid])),
188
  ));
189

    
190
  $old_log = $old_node->log != '' ? '<p class="revision-log">' . filter_xss($old_node->log) . '</p>' : '';
191
  $new_log = $new_node->log != '' ? '<p class="revision-log">' . filter_xss($new_node->log) . '</p>' : '';
192

    
193
  // Generate previous diff/next diff links.
194
  $nav_suffix = ($default_state != $state) ? '/' . str_replace('_', '-', $state) : '';
195
  $next_vid = _diff_get_next_vid($node_revisions, $new_vid);
196
  if ($next_vid) {
197
    $next_link = l(t('Next difference >'), 'node/' . $node->nid . '/revisions/view/' . $new_vid . '/' . $next_vid . $nav_suffix, array('absolute' => 1));
198
  }
199
  else {
200
    $next_link = '';
201
  }
202
  $prev_vid = _diff_get_previous_vid($node_revisions, $old_vid);
203
  if ($prev_vid) {
204
    $prev_link = l(t('< Previous difference'), 'node/' . $node->nid . '/revisions/view/' . $prev_vid . '/' . $old_vid . $nav_suffix, array('absolute' => 1));
205
  }
206
  else {
207
    $prev_link = '';
208
  }
209

    
210
  $header = _diff_default_header($old_header, $new_header);
211
  $rows = array();
212
  if ($old_log || $new_log) {
213
    $rows['logs'] = array(
214
      array(
215
        'data' => $old_log,
216
        'colspan' => 2,
217
      ),
218
      array(
219
        'data' => $new_log,
220
        'colspan' => 2,
221
      ),
222
    );
223
  }
224
  $rows['navigation'] = array(
225
    array(
226
      'data' => $prev_link,
227
      'class' => array('diff-prevlink'),
228
      'colspan' => 2,
229
    ),
230
    array(
231
      'data' => $next_link,
232
      'class' => array('diff-nextlink'),
233
      'colspan' => 2,
234
    ),
235
  );
236

    
237
  $links = array();
238
  foreach (diff_available_states('node') as $alternative_state => $label) {
239
    if ($alternative_state == $state) {
240
      // @todo: Should we show both or just alternatives?
241
    }
242
    $links[$alternative_state] = array(
243
      'title' => $label,
244
      'href' => "node/{$node->nid}/revisions/view/{$old_vid}/{$new_vid}" . ($alternative_state == $default_state ? '' : '/' . str_replace('_', '-', $alternative_state)),
245
    );
246
  }
247
  if (count($links) > 1) {
248
    $state_links = theme('links', array(
249
      'links' => $links,
250
      'attributes' => array('class' => array('links', 'inline')),
251
    ));
252
    $rows['states'] = array(
253
      array(
254
        'data' => $state_links,
255
        'class' => 'diff-links',
256
        'colspan' => 4,
257
      ),
258
    );
259
  }
260
  $rows = array_merge($rows, _diff_body_rows($old_node, $new_node, $state));
261

    
262
  $build['diff_table'] = array(
263
    '#theme' => 'table__diff__standard',
264
    '#header' => $header,
265
    '#rows' => $rows,
266
    '#attributes' => array('class' => array('diff')),
267
    '#colgroups' => _diff_default_cols(),
268
    '#sticky' => FALSE,
269
  );
270

    
271
  // Allow users to hide or set the display mode of the preview.
272
  if (node_is_page($node) && $view_mode = variable_get('diff_view_mode_preview_node_' . $new_node->type, 'full')) {
273
    $header = '';
274
    if ($node->vid == $new_vid) {
275
      $header .= '<div class="diff-section-title">' . t('Current revision:') . '</div>';
276
    }
277
    else {
278
      $header .= '<div class="diff-section-title">' . t('Revision of @new_date:', array('@new_date' => format_date($new_node->revision_timestamp))) . '</div>';
279
    }
280
    $build['diff_preview']['header']['#markup'] = $header;
281
    // Don't include node links or comments when viewing the diff.
282
    $build['diff_preview']['content'] = node_view($new_node, $view_mode);
283
    if (isset($build['diff_preview']['content']['links'])) {
284
      unset($build['diff_preview']['content']['links']);
285
    }
286
    if (isset($build['diff_preview']['content']['comments'])) {
287
      unset($build['diff_preview']['content']['comments']);
288
    }
289
  }
290
  return $build;
291
}
292

    
293
/**
294
 * Creates an array of rows which represent the difference between nodes.
295
 *
296
 * @param object $old_node
297
 *   Node for comparison which will be displayed on the left side.
298
 * @param object $new_node
299
 *   Node for comparison which will be displayed on the right side.
300
 * @param boolean $state
301
 *   The state to render for the diff.
302
 */
303
function _diff_body_rows($old_node, $new_node, $state = 'raw') {
304
  // This is an unique index only, so no need for drupal_static().
305
  static $table_row_counter = 0;
306

    
307
  if ($theme = variable_get('diff_theme', 'default')) {
308
    drupal_add_css(drupal_get_path('module', 'diff') . "/css/diff.{$theme}.css");
309
  }
310
  module_load_include('inc', 'diff', 'includes/node');
311

    
312
  $rows = array();
313
  $any_visible_change = FALSE;
314
  $context = array(
315
    'entity_type' => 'node',
316
    'states' => array($state),
317
    'view_mode' => 'diff_standard',
318
  );
319

    
320
  $node_diffs = diff_compare_entities($old_node, $new_node, $context);
321

    
322
  // Track line numbers between multiple diffs.
323
  $line_stats = array(
324
    'counter' => array('x' => 0, 'y' => 0),
325
    'offset' => array('x' => 0, 'y' => 0),
326
  );
327

    
328
  // Render diffs for each.
329
  foreach ($node_diffs as $node_diff) {
330
    $show_header = !empty($node_diff['#name']);
331
    // These are field level settings.
332
    if ($show_header && isset($node_diff['#settings']['show_header'])) {
333
      $show_header = $show_header && $node_diff['#settings']['show_header'];
334
    }
335

    
336
    // Line counting and line header options.
337
    if (empty($node_diff['#settings']['line_counter'])) {
338
      $line_counter = FALSE;
339
    }
340
    else {
341
      $line_counter = $node_diff['#settings']['line_counter'];
342
    }
343
    // Every call to 'line' resets the counters.
344
    if ($line_counter) {
345
      $line_stats['counter']['x'] = 0;
346
      $line_stats['counter']['y'] = 0;
347
      if ($line_counter == 'line' && 0) {
348
        $line_stats['offset']['x'] = 0;
349
        $line_stats['offset']['y'] = 0;
350
      }
351
      $line_stats_ref = $line_stats;
352
    }
353
    else {
354
      $line_stats_ref = NULL;
355
    }
356

    
357
    list($old, $new) = diff_extract_state($node_diff, $state);
358
    if ($node_diff_rows = diff_get_rows($old, $new, $line_counter && $line_counter != 'hidden', $line_stats_ref)) {
359
      if ($line_counter && $line_counter != 'line') {
360
        $line_stats['offset']['x'] += $line_stats_ref['counter']['x'];
361
        $line_stats['offset']['y'] += $line_stats_ref['counter']['y'];
362
      }
363
      if ($show_header) {
364
        $rows['diff-header-' . $table_row_counter++] = array(
365
          array(
366
            'data' => t('Changes to %name', array('%name' => $node_diff['#name'])),
367
            'class' => 'diff-section-title',
368
            'colspan' => 4,
369
          ),
370
        );
371
      }
372
      // To avoid passing counter to the Diff engine, index rows manually here
373
      // to allow modules to interact with the table. i.e. no array_merge().
374
      foreach ($node_diff_rows as $row) {
375
        $rows['diff-row-' . $table_row_counter++] = $row;
376
      }
377
      $any_visible_change = TRUE;
378
    }
379
  }
380
  if (!$any_visible_change) {
381
    $rows['diff-empty-' . $table_row_counter++] = array(
382
      array(
383
        'data' => t('No visible changes'),
384
        'class' => 'diff-section-title',
385
        'colspan' => 4,
386
      ),
387
    );
388
    // @todo: revise this.
389
    // Needed to keep safari happy.
390
    $rows['diff-empty-' . $table_row_counter++] = array(
391
      array('data' => ''),
392
      array('data' => ''),
393
      array('data' => ''),
394
      array('data' => ''),
395
    );
396
  }
397

    
398
  return $rows;
399
}
400

    
401
/**
402
 * Generic callback to compare two entities.
403
 */
404
function diff_compare_entities($left_entity, $right_entity, $context) {
405
  $entity_type = $context['entity_type'];
406
  list(, , $bundle) = entity_extract_ids($entity_type, $right_entity);
407
  $context['bundle'] = $bundle;
408
  $context['old_entity'] = $left_entity;
409
  $context['new_entity'] = $right_entity;
410
  $context += array(
411
    'states' => array('raw'),
412
    'view_mode' => FALSE,
413
    'language' => LANGUAGE_NONE,
414
  );
415

    
416
  $diff = module_invoke_all('entity_diff', $left_entity, $right_entity, $context);
417

    
418
  // Allow other modules to interact directly with the results.
419
  drupal_alter('entity_diff', $diff, $context);
420

    
421
  // We start off assuming all form elements are in the correct order.
422
  $diff['#sorted'] = TRUE;
423

    
424
  // Field rows. Recurse through all child elements.
425
  $count = 0;
426
  foreach (element_children($diff) as $key) {
427
    if (!isset($diff[$key]['#states'])) {
428
      $diff[$key]['#states'] = array();
429
    }
430

    
431
    // Ensure that the element follows the new #states format.
432
    if (isset($diff[$key]['#old'])) {
433
      $diff[$key]['#states']['raw']['#old'] = $diff[$key]['#old'];
434
      unset($diff[$key]['#old']);
435
    }
436
    if (isset($diff[$key]['#new'])) {
437
      $diff[$key]['#states']['raw']['#new'] = $diff[$key]['#new'];
438
      unset($diff[$key]['#new']);
439
    }
440

    
441
    // If requested, we can convert the .
442
    foreach (array('raw', 'rendered') as $state) {
443
      if (in_array($state . '_plain', $context['states'])) {
444
        diff_markdown_state($diff[$key], $state);
445
      }
446
    }
447

    
448
    // Assign a decimal placeholder weight to preserve original array order.
449
    if (!isset($diff[$key]['#weight'])) {
450
      $diff[$key]['#weight'] = $count / 1000;
451
    }
452
    else {
453
      // If one child element has a weight then we will need to sort later.
454
      unset($diff['#sorted']);
455
    }
456
    $count++;
457
  }
458

    
459
  // One of the children has a #weight.
460
  if (!isset($diff['#sorted'])) {
461
    uasort($diff, 'element_sort');
462
  }
463

    
464
  // Process the array and get line counts per field.
465
  array_walk($diff, 'diff_process_state_lines');
466

    
467
  return $diff;
468
}
469

    
470
function diff_process_state_lines(&$diff, $key) {
471
  foreach ($diff['#states'] as $state => $data) {
472
    if (isset($data['#old'])) {
473
      if (is_string($data['#old'])) {
474
        $diff['#states'][$state]['#old'] = explode("\n", $data['#old']);
475
      }
476
      $diff['#states'][$state]['#count_old'] = count($diff['#states'][$state]['#old']);
477
    }
478
    else {
479
      $diff['#states'][$state]['#count_old'] = 0;
480
    }
481
    if (isset($data['#new'])) {
482
      if (is_string($data['#new'])) {
483
        $diff['#states'][$state]['#new'] = explode("\n", $data['#new']);
484
      }
485
      $diff['#states'][$state]['#count_new'] = count($diff['#states'][$state]['#new']);
486
    }
487
    else {
488
      $diff['#states'][$state]['#count_new'] = 0;
489
    }
490
  }
491
}
492

    
493
/**
494
 * Helper function to render plain states from the corresponding raw state.
495
 *
496
 * @param array $diff
497
 *   The Diff Engine output array.
498
 * @param string $state
499
 *   The state to markdown.
500
 */
501
function diff_markdown_state(&$diff, $state) {
502
  list($plain_old, $plain_new) = diff_extract_state($diff, $state . '_plain');
503
  list($old, $new) = diff_extract_state($diff, $state);
504
  $markdown = FALSE;
505
  if (isset($diff['#settings']) && !empty($diff['#settings']['markdown'])) {
506
    if (function_exists($diff['#settings']['markdown'])) {
507
      $markdown = $diff['#settings']['markdown'];
508
    }
509
  }
510

    
511
  if (!isset($plain_old) && isset($old)) {
512
    if (is_array($old)) {
513
      $diff['#states'][$state . '_plain']['#old'] = $markdown ? array_map($markdown, $old) : $old;
514
    }
515
    else {
516
      $diff['#states'][$state . '_plain']['#old'] = $markdown ? $markdown($old) : $old;
517
    }
518
  }
519
  if (!isset($plain_new) && isset($new)) {
520
    if (is_array($new)) {
521
      $diff['#states'][$state . '_plain']['#new'] = $markdown ? array_map($markdown, $new) : $new;
522
    }
523
    else {
524
      $diff['#states'][$state . '_plain']['#new'] = $markdown ? $markdown($new) : $new;
525
    }
526
  }
527
}
528

    
529
/**
530
 * Get the entry in the revisions list after $vid.
531
 *
532
 * @param array $node_revisions
533
 *   Array of node revision IDs in descending order.
534
 * @param int $vid
535
 *   Version ID to look for.
536
 *
537
 * @return boolean|integer
538
 *   Returns FALSE if $vid is the last entry.
539
 */
540
function _diff_get_next_vid($node_revisions, $vid) {
541
  $previous = NULL;
542
  foreach ($node_revisions as $revision) {
543
    if ($revision->vid == $vid) {
544
      return ($previous ? $previous->vid : FALSE);
545
    }
546
    $previous = $revision;
547
  }
548
  return FALSE;
549
}
550

    
551
/**
552
 * Get the entry in the revision list before $vid.
553
 *
554
 * @param array $node_revisions
555
 *   Array of node revision IDs in descending order.
556
 * @param integer $vid
557
 *   Version ID to look for.
558
 *
559
 * @return boolean|integer
560
 *   Returns FALSE if $vid is the first entry.
561
 */
562
function _diff_get_previous_vid($node_revisions, $vid) {
563
  $previous = NULL;
564
  foreach ($node_revisions as $revision) {
565
    if ($previous && $previous->vid == $vid) {
566
      return $revision->vid;
567
    }
568
    $previous = $revision;
569
  }
570
  return FALSE;
571
}
572

    
573
/**
574
 * Helper function to create default 'cols' array for diff table.
575
 */
576
function _diff_default_cols() {
577
  return array(
578
    array(
579
      array(
580
        'class' => 'diff-marker',
581
      ),
582
      array(
583
        'class' => 'diff-content',
584
      ),
585
      array(
586
        'class' => 'diff-marker',
587
      ),
588
      array(
589
        'class' => 'diff-content',
590
      ),
591
    ),
592
  );
593
}
594

    
595
/**
596
 * Helper function to create default 'header' array for diff table.
597
 */
598
function _diff_default_header($old_header = '', $new_header = '') {
599
  return array(
600
    array(
601
      'data' => $old_header,
602
      'colspan' => 2,
603
    ),
604
    array(
605
      'data' => $new_header,
606
      'colspan' => 2,
607
    ),
608
  );
609
}
610

    
611
/**
612
 * Show the inline diff for a given node, vid.
613
 *
614
 * If vid = 0 or no previous vid exists for the given revision returns the
615
 * normally rendered content of the specified revision.
616
 */
617
function diff_inline_show($node, $vid = 0, $metadata = TRUE) {
618
  $new_node = $vid ? node_load($node->nid, $vid, TRUE) : clone $node;
619
  node_build_content($new_node);
620
  $new = drupal_render($new_node->content);
621

    
622
  $old = $vid ? _diff_get_previous_vid(node_revision_list($node), $vid) : 0;
623
  if ($old) {
624
    $old_node = node_load($node->nid, $old, TRUE);
625
    node_build_content($old_node);
626
    $old = drupal_render($old_node->content);
627
    $output = $metadata ? theme('diff_inline_metadata', array('node' => $new_node)) : '';
628
    $output .= diff_get_inline($old, $new);
629
    return $output;
630
  }
631
  return $new;
632
}