aboutsummaryrefslogtreecommitdiff
path: root/node_truncate.module
blob: 9583ccf272dfd3ac1a9ce413e4f20b40c9df6ad4 (plain)
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
<?php
// $Id$

/**
 * @file
 * Node Truncate module.
 *
 * Use this module to delete all posts from a given content type.
 */

/**
 * Implements hook_menu().
 */
function node_truncate_menu() {
  $items['admin/config/content/node_truncate'] = array(
    'title'            => 'Node Truncate',
    'description'      => 'Mass deletion page.',
    'page callback'    => 'node_truncate_page',
    'access arguments' => array('administer nodes'),
    'type'             => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Menu callback.
 */
function node_truncate_page() {
  $output  = t('Choose a content type to truncate.');
  $output .= drupal_render(drupal_get_form('node_truncate_form'));
  return $output;
}

/**
 * Mass deletion form.
 */
function node_truncate_form($form, &$form_state) {
  $node_types = node_type_get_types();
  $node_codes = array();

  foreach ($node_types as $item) {
    $node_codes[$item->type] = $item->name;
  }

  $form['node_types'] = array(
    '#type'          => 'checkboxes',
    '#title'         => t('Node types'),
    '#options'       => $node_codes,
    '#default_value' => variable_get('node_truncate_node_types', array()),
    '#description'   => t('Select nodetypes which should be truncated.'),
  );

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

  return $form;
}

/**
 * Implements hook_validate().
 */
function node_truncate_form_validate($form, $form_state) {
  if (!isset($form_state['values']['node_types'])) {
    form_set_error('mass_delete', t('Please choose at least one node type.'));
  }
}

/**
 * Handle post-validation form-submission.
 */
function node_truncate_form_submit($form, &$form_state) {
  $operations = array();
  $types      = array_filter($form_state['values']['node_types']);
  variable_set('node_truncate_node_types', $types);

  foreach ($types as $type) {
    $operations[] = array('node_truncate', array($type));
  }

  $batch = array(
    'title'      => t('Deleting old data.'),
    'operations' => $operations,
    'finished'   => 'node_truncate_callback',
  );

  batch_set($batch);
  batch_process();
}

/**
 * Mass deletion of content.
 */
function node_truncate($type, &$context) {
  if (empty($context['sandbox'])) {
    $query = db_select('node', 'n');
    $query
      ->condition('n.type', $type, '=')
      ->addExpression('COUNT(DISTINCT(nid))');

    $context['sandbox']['progress']     = 0;
    $context['sandbox']['max']          = $query->execute()->fetchField();
  }

  $limit  = 5;
  $query  = db_select('node', 'n');
  $query
    ->condition('n.type', $type, '=')
    ->fields('n', array('nid'))
    ->range(0, $limit);
  $result = $query->execute();

  while ($row = $result->fetchAssoc()) {
    $node = node_delete($row['nid'], NULL, TRUE);
    $context['sandbox']['progress']++;
    $context['results'][] = $row['nid'];
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}

/**
 * Callback for node_truncate();
 */
function node_truncate_callback($success, $results, $operations) {
  if ($success) {
    if ($results > 0) {
      $message = format_plural(count($results), t('One old post processed.'), t('@count old posts processed.'));
    }
  }
  else {
    $message = t('Finished with an error.');
  }
  drupal_set_message($message);
}