diff.module 24 KB

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