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