diff.module 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. $rows = _diff_body_rows($old_node, $node);
  438. $header = _diff_default_header(t('Original'), t('Changes'));
  439. $changes = theme('table__diff__preview', array(
  440. 'header' => $header,
  441. 'rows' => $rows,
  442. 'attributes' => array('class' => array('diff')),
  443. 'colgroups' => _diff_default_cols(),
  444. 'sticky' => FALSE,
  445. ));
  446. // Prepend diff to edit form.
  447. $form_state['node_preview'] = $changes;
  448. $form_state['rebuild'] = TRUE;
  449. }
  450. /**
  451. * Implementation of hook_features_pipe_COMPONENT_alter().
  452. */
  453. function diff_features_pipe_node_alter(&$pipe, $data, $export) {
  454. if (!empty($data)) {
  455. $variables = array(
  456. 'diff_show_preview_changes_node',
  457. 'diff_enable_revisions_page_node',
  458. 'diff_view_mode_preview_node',
  459. );
  460. foreach ($data as $node_type) {
  461. foreach ($variables as $variable_name) {
  462. $pipe['variable'][] = $variable_name . '_' . $node_type;
  463. }
  464. }
  465. }
  466. }
  467. /**
  468. * Implements hook_theme().
  469. */
  470. function diff_theme() {
  471. return array(
  472. 'diff_node_revisions' => array(
  473. 'render element' => 'form',
  474. 'file' => 'diff.theme.inc',
  475. ),
  476. 'diff_header_line' => array(
  477. 'arguments' => array('lineno' => NULL),
  478. 'file' => 'diff.theme.inc',
  479. ),
  480. 'diff_content_line' => array(
  481. 'arguments' => array('line' => NULL),
  482. 'file' => 'diff.theme.inc',
  483. ),
  484. 'diff_empty_line' => array(
  485. 'arguments' => array('line' => NULL),
  486. 'file' => 'diff.theme.inc',
  487. ),
  488. 'diff_inline_form' => array(
  489. 'render element' => 'form',
  490. 'file' => 'diff.theme.inc',
  491. ),
  492. 'diff_inline_metadata' => array(
  493. 'arguments' => array('node' => NULL),
  494. 'file' => 'diff.theme.inc',
  495. ),
  496. 'diff_inline_chunk' => array(
  497. 'arguments' => array('text' => '', 'type' => NULL),
  498. 'file' => 'diff.theme.inc',
  499. ),
  500. );
  501. }
  502. /**
  503. * Render the table rows for theme('table').
  504. *
  505. * @param string $a
  506. * The source string to compare from.
  507. * @param string $b
  508. * The target string to compare to.
  509. * @param bool $show_header
  510. * Display diff context headers. For example, "Line x".
  511. * @param array $line_stats
  512. * This structure tracks line numbers across multiple calls to DiffFormatter.
  513. *
  514. * @return array
  515. * Array of rows usable with theme('table').
  516. */
  517. function diff_get_rows($a, $b, $show_header = FALSE, &$line_stats = NULL) {
  518. $a = is_array($a) ? $a : explode("\n", $a);
  519. $b = is_array($b) ? $b : explode("\n", $b);
  520. if (!isset($line_stats)) {
  521. $line_stats = array(
  522. 'counter' => array('x' => 0, 'y' => 0),
  523. 'offset' => array('x' => 0, 'y' => 0),
  524. );
  525. }
  526. $formatter = new DrupalDiffFormatter();
  527. // Header is the line counter.
  528. $formatter->show_header = $show_header;
  529. $formatter->line_stats = &$line_stats;
  530. $diff = new Diff($a, $b);
  531. return $formatter->format($diff);
  532. }
  533. /**
  534. * Render and markup a diff of two strings into HTML markup.
  535. *
  536. * @param string $a
  537. * The source string to compare from.
  538. * @param string $b
  539. * The target string to compare to.
  540. *
  541. * @return string
  542. * String containing HTML markup.
  543. */
  544. function diff_get_inline($a, $b) {
  545. $diff = new DrupalDiffInline($a, $b);
  546. return $diff->render();
  547. }
  548. /**
  549. * Form builder: Inline diff controls.
  550. */
  551. function diff_inline_form($form, $form_state, $node, $revisions) {
  552. $form = array();
  553. $form['node'] = array(
  554. '#type' => 'value',
  555. '#value' => $node,
  556. );
  557. $form['revision'] = array(
  558. '#type' => 'select',
  559. '#options' => array(0 => t('- No highlighting -')),
  560. '#default_value' => (arg(2) === 'revisions' && arg(3) === $node->vid) ? $node->vid : 0,
  561. '#ajax' => array(
  562. 'callback' => 'diff_inline_ajax',
  563. 'wrapper' => "node-{$node->nid}",
  564. 'method' => 'replace',
  565. ),
  566. );
  567. foreach ($revisions as $revision) {
  568. $form['revision']['#options'][$revision->vid] = t('@revision by @name', array(
  569. '@revision' => format_date($revision->timestamp, 'short'),
  570. '@name' => format_username($revision),
  571. ));
  572. }
  573. $form['submit'] = array(
  574. '#type' => 'submit',
  575. '#value' => t('View'),
  576. '#submit' => array('diff_inline_form_submit'),
  577. '#attributes' => array('class' => array('diff-js-hidden')),
  578. );
  579. return $form;
  580. }
  581. /**
  582. * AHAH callback for rendering the inline diff of a node.
  583. */
  584. function diff_inline_ajax($form, $form_state) {
  585. module_load_include('inc', 'diff', 'diff.pages');
  586. $node = $form['node']['#value'];
  587. $vid = isset($form_state['values']['revision']) ? $form_state['values']['revision'] : 0;
  588. return "<div id='node-{$node->nid}'>" . diff_inline_show($node, $vid) . "</div>";
  589. }
  590. /**
  591. * Form submission handler for diff_inline_form() for JS-disabled clients.
  592. */
  593. function diff_inline_form_submit(&$form, &$form_state) {
  594. if (isset($form_state['values']['revision'], $form_state['values']['node'])) {
  595. $node = $form_state['values']['node'];
  596. $vid = $form_state['values']['revision'];
  597. $form_state['redirect'] = "node/{$node->nid}/revisions/{$vid}/view";
  598. }
  599. }
  600. /**
  601. * A helper function to normalise system differences.
  602. *
  603. * This handles differences in:
  604. * - line endings: Mac, Windows and UNIX all use different line endings.
  605. */
  606. function diff_normalise_text($text) {
  607. $text = str_replace(array("\r\n", "\r"), "\n", $text);
  608. return $text;
  609. }
  610. /**
  611. * A wrapper function for filter_xss() to exclude all tags.
  612. */
  613. function diff_filter_xss($string) {
  614. return filter_xss($string, array());
  615. }
  616. /**
  617. * Helper function to load any CSS or JScript files required by a page or form.
  618. */
  619. function diff_build_attachments($jscript = FALSE) {
  620. $attachments = array();
  621. $theme = variable_get('diff_theme', 'default');
  622. if ($theme) {
  623. $attachments['css'] = array(
  624. drupal_get_path('module', 'diff') . "/css/diff.{$theme}.css",
  625. );
  626. }
  627. $type = variable_get('diff_radio_behavior', 'simple');
  628. if ($jscript && $type) {
  629. $attachments['js'] = array(
  630. drupal_get_path('module', 'diff') . "/js/diff.js",
  631. array(
  632. 'data' => array('diffRevisionRadios' => $type),
  633. 'type' => 'setting',
  634. ),
  635. );
  636. }
  637. return $attachments;
  638. }
  639. /**
  640. * Implements hook_entity_diff() on behalf of the Node module.
  641. */
  642. function node_entity_diff($old_node, $new_node, $context) {
  643. $result = array();
  644. if ($context['entity_type'] == 'node') {
  645. module_load_include('inc', 'diff', 'includes/node');
  646. $options = variable_get('diff_additional_options_node', array('title' => 'title'));
  647. foreach (node_entity_diff_options('node') as $key => $option_label) {
  648. if (!empty($options[$key])) {
  649. $func = '_node_entity_diff_additional_options_' . $key;
  650. $result[$key] = $func($old_node, $new_node, $context);
  651. }
  652. }
  653. }
  654. return $result;
  655. }
  656. /**
  657. * Implements hook_entity_diff_options() on behalf of the Node module.
  658. */
  659. function node_entity_diff_options($entity_type) {
  660. if ($entity_type == 'node') {
  661. $options = array(
  662. 'title' => t('Title field'),
  663. // Author field is either the owner or revision creator, neither capture
  664. // a change in the author field.
  665. 'author' => t('Author'),
  666. 'revision_author' => t('Revision author'),
  667. 'type' => t('Node type'),
  668. 'publishing_flags' => t('Publishing options'),
  669. // More fields that currently can not be tracked.
  670. 'created' => t('Created date'),
  671. 'changed' => t('Updated date'),
  672. 'revision_timestamp' => t('Revision timestamp'),
  673. );
  674. if (module_exists('comment')) {
  675. $options['comment'] = t('Comment setting');
  676. }
  677. return $options;
  678. }
  679. }
  680. /**
  681. * Implements hook_entity_diff() on behalf of the User module.
  682. */
  683. function user_entity_diff($old_user, $new_user, $context) {
  684. $result = array();
  685. if ($context['entity_type'] == 'user') {
  686. module_load_include('inc', 'diff', 'includes/user');
  687. $options = variable_get('diff_additional_options_user', array(
  688. 'name' => 'name',
  689. 'mail' => 'mail',
  690. 'status' => 'status',
  691. ));
  692. foreach (user_entity_diff_options('user') as $key => $option_label) {
  693. if (!empty($options[$key])) {
  694. $func = '_user_entity_diff_additional_options_' . $key;
  695. $result[$key] = $func($old_user, $new_user, $context);
  696. }
  697. }
  698. }
  699. return $result;
  700. }
  701. /**
  702. * Implements hook_entity_diff_options() on behalf of the User module.
  703. */
  704. function user_entity_diff_options($entity_type) {
  705. if ($entity_type == 'user') {
  706. $options = array(
  707. 'name' => t('Username'),
  708. 'mail' => t('E-mail address'),
  709. 'roles' => t('Roles'),
  710. 'status' => t('Status'),
  711. 'timezone' => t('Time zone'),
  712. 'password' => t('Password Hash'),
  713. );
  714. return $options;
  715. }
  716. }