-
Notifications
You must be signed in to change notification settings - Fork 1
/
node_revision_history.module
382 lines (329 loc) · 11.2 KB
/
node_revision_history.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
/**
* @file
* This module allows administrators to review the number of node
* revisions current in the site and make use of Backdrop's
* function node_revision_delete for selective deletion of them.
*/
/**
* Implements hook_menu().
*/
function node_revision_history_menu() {
$items['admin/content/node_revision_history'] = array(
'title' => 'Node Revision History',
'description' => 'Listing of node revisions.',
'page callback' => 'backdrop_get_form',
'page arguments' => array('node_revision_history_listing_form'),
'access callback' => 'user_access',
'access arguments' => array('administer node_revision_history'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_permission().
*/
function node_revision_history_permission() {
return array(
'administer node_revision_history' => array(
'title' => t('Administer Node History'),
'description' => t('Allow access to use the module.'),
),
);
}
/**
* The Node Revision History initial form.
*/
function node_revision_history_listing_form($form, &$form_state) {
// Display page 2 if $form_state['page_num'] == 2
if (!empty($form_state['page_num']) && $form_state['page_num'] == 2) {
return node_revision_history_listing_form_page_two($form, $form_state);
}
// Display page 3 if $form_state['page_num'] == 3
if (!empty($form_state['page_num']) && $form_state['page_num'] == 3) {
return node_revision_history_listing_form_page_three($form, $form_state);
}
// Otherwise we build page 1.
$form_state['page_num'] = 1;
$form['help'] = array(
'#type' => 'help',
'#suffix' => t('Important: make sure you have backed up your database before using this utilty.'),
'#markup' => t('This utility provides a quick way for admin to review the number of revisions
stored in the system for individual nodes and to delete some or all of them.'),
);
$form['description'] = array(
'#type' => 'item',
'#title' => t('Specify either a single node identity number to process one node or use zero to get a full listing'),
'#description' => t('Submitting zero will produce a table listing current node types and number of revisions.'),
);
$form['nid'] = array(
'#type' => 'textfield',
'#title' => t('Node identity - NID'),
'#default_value' => 0,
'#size' => 10,
'#maxlength' => 10,
);
$form['threshold'] = array(
'#type' => 'textfield',
'#title' => t('Revision threshold'),
'#description' => t('When generating the table of results only include nodes
which have more revisions than this threshold.'),
'#default_value' => 1,
'#size' => 10,
'#maxlength' => 10,
);
$form['next'] = array(
'#type' => 'submit',
'#value' => 'Next >>',
'#submit' => array('node_revision_history_listing_form_next_submit'),
);
return $form;
}
/**
* Submit handler for node_revision_history_listing_form() next button.
*
* Capture the values from page one and store them away so they can be used
* at final submit time.
*/
function node_revision_history_listing_form_next_submit($form, &$form_state) {
$form_state['page_values'][1] = $form_state['values'];
if (!empty($form_state['page_values'][2])) {
$form_state['values'] = $form_state['page_values'][2];
}
// When form rebuilds, it will look at this to figure which page to build.
if ($form_state['values']['nid'] == 0) {
$form_state['page_num'] = 3;
}
else {
$form_state['page_num'] = 2;
}
$form_state['rebuild'] = TRUE;
}
/**
* The form for the second page of form_node_revision_history_listing().
* This form is for reviewing a specified node.
*/
function node_revision_history_listing_form_page_two($form, &$form_state) {
$this_nid = $form_state['values']['nid'];
$this_node = node_load($this_nid);
$revisions = node_revision_list($this_node);
// Produce a table of results.
$headers = array(
'VID',
'Author',
'Created',
);
$options = array();
foreach ($revisions as $vid => $data) {
$options[$vid] = array(
array('data' => $data -> vid),
array('data' => $data-> name),
array('data' => date('Y-m-d H:i', $data-> timestamp)),
);
// For each revision identify the current one, others are given value 0.
$form_state['current_vid'][$vid] = $data -> vid == $data -> current_vid ? $data -> current_vid : 0;
}
$form['description'] = array(
'#type' => 'item',
'#title' => "Select revisions of node $this_nid to delete.",
'#description' => 'NB. The procedure will prevent the deletion of the current revision (the first in this list).',
);
$form['revisions'] = array(
'#type' => 'tableselect',
'#header' => $headers,
'#options' => $options,
'#empty' => t('No content available.'),
);
$form['actions'] = array('#type' => 'actions');
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('node_revision_history_listing_form_page_two_submit'),
);
$form['back'] = array(
'#type' => 'submit',
'#value' => t('<< Back'),
'#submit' => array('node_revision_history_listing_form_page_two_back'),
'#limit_validation_errors' => array(),
);
return $form;
}
function node_revision_history_listing_form_page_two_submit($form, &$form_state) {
// Get node identities.
$revisions = $form_state['values']['revisions'];
$vids = $form_state['current_vid'];
$deleted_vids = '';
foreach($revisions as $revision => $value){
if($value > 0){
node_revision_delete($value);
$deleted_vids = trim($value) . ', ' . $deleted_vids;
}
}
backdrop_set_message(t('The following revisions have been deleted, VIDs = @deleted_vids',
array(
'@deleted_vids' => $deleted_vids,
)
));
}
/**
* Back button handler submit handler.
*
* Since #limit_validation_errors = array() is set, values from page 2
* will be discarded. We load the page 1 values instead.
*/
function node_revision_history_listing_form_page_two_back($form, &$form_state) {
$form_state['values'] = $form_state['page_values'][1];
$form_state['page_num'] = 1;
$form_state['rebuild'] = TRUE;
}
/**
* The form for the third page of form_node_revision_history_listing().
* This lists the number of revisions for every node in the system
* where that number exceeds the threshold.
* It provides a checkbox for specifying which nodes should have some
* revisions deleted and when submitted processes the selection.
*/
function node_revision_history_listing_form_page_three($form, &$form_state) {
$revision_count = array();
$threshold = $form_state['values']['threshold'];
//for each node type count how many revision exist.
foreach (node_type_get_names() as $type => $name) {
$revisions = node_revision_history_candidates($type, $threshold);
if(!empty($revisions)){
$revision_count[$name] = $revisions;
}
}
$rows = array();
foreach ($revision_count as $name => $data) {
foreach ($data as $identity => $revisions) {
$rows[$identity] = array(
'name' => $name,
'identity' => $identity,
'number' => $revisions,
);
}
}
$form_state['revision_data'] = $rows;
$form_state['threshold'] = $threshold;
$options = array();
foreach ($rows as $rid => $revisions) {
$options[$rid] = array(
array('data' => $revisions['name']),
array('data' => $revisions['identity']),
array('data' => $revisions['number']),
);
}
// Produce a table of results.
$headers = array(
'Node type',
'Node identity',
'Number of revisions',
);
$form['description'] = array(
'#type' => 'item',
'#title' => t('Table of existing revisions with number greater than threshold.'),
'#description' => t('Choose one or more nodes to process. The more recent revisions
will not be deleted, depending on the value of the threshold.'),
);
$form['d_threshold'] = array(
'#type' => 'textfield',
'#title' => t('Deletion threshold'),
'#description' => t('Delete revisions in excess of this threshold.'),
'#default_value' => $threshold,
'#size' => 10,
'#maxlength' => 10,
);
$form['revisions'] = array(
'#type' => 'tableselect',
'#header' => $headers,
'#options' => $options,
'#empty' => t('No content available.'),
);
$form['back'] = array(
'#type' => 'submit',
'#value' => t('<< Back'),
'#submit' => array('node_revision_history_listing_form_page_three_back'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('node_revision_history_listing_form_page_three_submit'),
);
return $form;
}
/**
* Submit function for the third page of form_node_revision_history_listing().
*/
function node_revision_history_listing_form_page_three_submit($form, &$form_state) {
// Get IDs of nodes to process from $form_state['values']['revisions'] > 0.
// Get corresponding node data from $form_state['revision_data'].
// Build an array of data for selected nodes ready for use of
// function node_revision_delete() limited by value of threshold in
// $form_state['threshold'].
$data = $form_state['revision_data'];
$d_threshold = $form_state['values']['d_threshold'];
// List nodes with revisions to delete
$nodes = array();
$no_nodes = TRUE;
foreach ($form_state['values']['revisions'] as $nid => $value) {
if ($value > 0) {
$nodes[$nid] = $nid;
$no_nodes = FALSE;
}
}
if ($no_nodes) {
// return to initial page.
$form_state['page_num'] = 1;
$form_state['rebuild'] = TRUE;
backdrop_set_message(t('No nodes to process.'));
return;
}
foreach ($nodes as $node => $item) {
// Now obtain the number of revisions for each node listed.
$nid = $data[$item]['identity'];
$this_node = node_load($nid);
$revisions_list = node_revision_list($this_node);
// step through revisions_list with a count,
// do nothing until count reaches threshold
// then delete each revision
// this relies on revisions being listed in descending order
// $revisions_list is an array of node objects.
// Note: function node_revision_delete includes a check that
// prevents deleting the current revision.
$n = 0;
foreach ($revisions_list as $vid => $revision) {
if ($n >= $d_threshold){
node_revision_delete($vid);
}
$n++;
}
}
backdrop_set_message(t('Processing complete.'));
}
function node_revision_history_listing_form_page_three_back($form, &$form_state) {
$form_state['values'] = $form_state['page_values'][1];
$form_state['page_num'] = 1;
$form_state['rebuild'] = TRUE;
}
/**
* Helper function to return the list of nids with revisions.
*
* @param string $content_type
* A content type machine name.
*
* @return array
* Array of nids with number of revisions for each
*/
function node_revision_history_candidates($content_type, $threshold) {
$params = array(
':content_type' => $content_type,
':threshold' => $threshold,
);
$result = db_query("SELECT n.nid, COUNT(r.nid)
FROM {node} n
INNER JOIN {node_revision} r ON r.nid = n.nid
WHERE n.type = :content_type
GROUP BY n.nid
HAVING count(r.nid) > :threshold", $params)->fetchAllKeyed();
return $result;
}