/[drupal]/contributions/modules/delete_all/delete_all.module
ViewVC logotype

Diff of /contributions/modules/delete_all/delete_all.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Sun Dec 23 03:22:05 2007 UTC revision 1.2, Mon Aug 4 00:20:17 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2    // $Id: delete_all.module,v 1.1.2.9 2008/05/27 22:16:07 kbahey Exp $
3    
4  function delete_all_menu($maycache) {  function delete_all_menu() {
5    $items = array();    $items = array();
6    
7    $items[] = array(    $items['admin/content/delete_content'] = array(
8      'path'               => 'admin/content/delete_content',      'title'               => 'Delete all content',
9      'title'              => t('Delete all content'),      'description'         => 'Delete all nodes and comments on this site. This is useful for development sites, or prior to launching the site. Otherwise it destroys all data on a site.',
10      'description'        => t('Delete all nodes and comments on this site. This is useful for development sites, or prior to launching the site. Otherwise it destroys all data on a site.'),      'page callback'       => 'drupal_get_form',
11      'callback'           => 'drupal_get_form',      'page arguments'      => array('delete_all_content'),
12      'callback arguments' => array('delete_all_content'),      'access arguments'    => array('administer nodes'),
13      'access'             => user_access('administer content'),      'type'                => MENU_NORMAL_ITEM);
14      'type'               => MENU_NORMAL_ITEM);  
15      $items['admin/content/delete_users'] = array(
16    $items[] = array(      'title'               => 'Delete all users',
17      'path'               => 'admin/user/delete_users',      'description'         => 'Delete all users on this site. This is useful for development sites, or prior to launching the site. Otherwise it destroys all data on a site.',
18      'title'              => t('Delete all users'),      'page callback'       => 'drupal_get_form',
19      'description'        => t('Delete all users on this site. This is useful for development sites, or prior to launching the site. Otherwise it destroys all data on a site.'),      'page arguments'      => array('delete_all_users'),
20      'callback'           => 'drupal_get_form',      'access arguments'    => array('administer users'),
     'callback arguments' => array('delete_all_users'),  
     'access'             => user_access('administer users'),  
21      'type'               => MENU_NORMAL_ITEM);      'type'               => MENU_NORMAL_ITEM);
22    
23    return $items;    return $items;
24  }  }
25    
26  function delete_all_content() {  function delete_all_content() {
27    $form['action'] = array(    drupal_add_js(drupal_get_path('module', 'delete_all') .'/delete_all.js');
28      '#type'  => 'hidden',    $form = array();
29      '#value' => 'content',    $form['all'] = array(
30        '#type' => 'checkbox',
31        '#default_value' => TRUE,
32        '#title' => t('Delete All Content'),
33        '#description' => t('Select to delete all content'),
34        '#attributes' => array('class' => 'delete-all'),
35    );    );
36    return confirm_form(  
37      $form,    // count how many nodes we have of each type
38      t('Are you sure you want to delete all content?'),    $result = db_query("SELECT type, COUNT(*) AS num FROM {node} GROUP BY type");
39      'admin',    $count = array();
40      '',    while ($data = db_fetch_object($result)) {
41      t('Delete'),      $count[$data->type] = $data->num;
42      t('Cancel'),    }
43      'delete_all_content');  
44      // add the types to the form
45      $types = array();
46      foreach (node_get_types() as $type => $info) {
47        if ($count[$type] > 0) {
48          $types[$type] = $info->name .' ('. $count[$type] .')';
49        }
50      }
51      asort($types);
52      $form['type-fieldset'] = array(
53        '#type' => 'fieldset',
54        '#title' => t('Types'),
55        '#collapsible' => TRUE,
56        '#collapsed' => TRUE,
57        'types' => array(
58          '#type' => 'checkboxes',
59          '#options' => $types,
60          '#description' => t('Select the types of content to delete'),
61          '#theme' => 'delete_all_checkboxes',
62        ),
63      );
64      $form['method-fieldset'] = array(
65        '#type' => 'fieldset',
66        '#title' => t('Method'),
67        '#collapsible' => TRUE,
68        '#collapsed' => TRUE,
69        'method' => array(
70          '#type' => 'radios',
71          '#title' => t('Method'),
72          '#options' => array('normal' => t('Normal'), 'quick' => t('Quick')),
73          '#default_value' => 'normal',
74          '#description' => t('Normal node delete calls node_delete() on every node in the database.  If you have only a few hundred nodes, this can take a very long time.  Use the quick node delete method to get around this problem.  This method deletes directly from the database, skipping the extra php processing.  The downside is that it can miss related tables that are normally handled by module hook_delete\'s.'),
75        ),
76      );
77      $form['confirm'] = array(
78        '#type' => 'checkbox',
79        '#title' => t('Delete Confirmation'),
80        '#description' => t('Please check this box to confirm that you understand you are deleting node content.  This should only be done in a development environment.'),
81        '#required' => TRUE,
82      );
83      $form['submit'] = array(
84        '#type' => 'submit',
85        '#value' => t('Delete'),
86      );
87      $form['#submit'] = array('delete_all_content_submit');
88      return $form;
89  }  }
90    
91  function delete_all_content_submit() {  /**
92    $result = db_query('SELECT nid FROM {node}');   * Implementation of hook_theme for Drupal 6 theme registry
93     */
94    function delete_all_theme() {
95      return array(
96        'theme_delete_all_checkboxes' => array(
97          'arguments' => array('form' => NULL),
98        ),
99      );
100    }
101    
102    $count = 0;  function theme_delete_all_checkboxes($form) {
103    while($data = db_fetch_object($result)) {    $total = 0;
104      node_delete($data->nid);    foreach ($form as $element_id => $element) {
105      $count++;      if ($element_id[0] != '#') {
106          $total ++;
107        }
108    }    }
109      $total = (int) (($total % 3) ? (($total + 2) / 3) : ($total / 3));
110      $pos = 0;
111      $rows = array();
112      foreach ($form as $element_id => $element) {
113        if ($element_id[0] != '#') {
114          $pos ++;
115          $row = $pos % $total;
116          $col = $pos / $total;
117          if (!isset($rows[$row])) {
118            $rows[$row] = array();
119          }
120          $rows[$row][$col] = drupal_render($element);
121        }
122      }
123      return theme('table', array(), $rows);
124    }
125    
126    db_query("UPDATE {sequences} SET id = 1 WHERE name = 'node_nid'");  function delete_all_content_submit($form, &$form_state) {
127    db_query("UPDATE {sequences} SET id = 1 WHERE name = 'comment_cid'");    $types = array();
128      if (!$form_state['values']['all']) {
129        foreach ($form_state['values']['types'] as $type => $checked) {
130          if ($checked) {
131            $types[] = $type;
132          }
133        }
134      }
135    
136      switch ($form_state['values']['method']) {
137        case 'normal':
138          $count = _delete_all_normal($form_state['values']['all'], $types);
139          break;
140    
141        case 'quick':
142          // the quick method doesn't support an all option,
143          // so just get all the content types and delete all of those
144          if ($form_state['values']['all']) {
145            $result = db_query("SELECT DISTINCT type FROM {node}");
146            while ($data = db_fetch_object($result)) {
147              $types[] = $type;
148            }
149          }
150          $count = _delete_all_quick($types);
151          break;
152      }
153    
154    drupal_set_message(t('All nodes and comments have been deleted. Number of nodes deleted: !count.', array('!count' => $count)));    if ($form_state['values']['all']) {
155        // Delete the URL aliases
156        db_query("DELETE FROM {url_alias} WHERE src LIKE 'node/%%'");
157    
158        drupal_set_message(t('All nodes, comments and URL aliases have been deleted. Number of nodes deleted: !count.', array('!count' => $count)));
159      }
160      else {
161        drupal_set_message(t('Nodes and comments of type @type have been deleted. Number of nodes deleted: !count.', array('!count' => $count, '@type' => implode(', ', $types))));
162      }
163    
164    drupal_goto('admin');    drupal_goto('admin');
165  }  }
166    
167    function _delete_all_normal($all, $types) {
168      if ($all) {
169        $result = db_query('SELECT nid FROM {node}'. $where);
170      }
171      else {
172        $placeholders = implode(',', array_fill(0, count($types), "'%s'"));
173        $result = db_query('SELECT nid FROM {node} WHERE type IN ('. $placeholders .')', $types);
174      }
175    
176      $deleted = 0;
177      while ($data = db_fetch_object($result)) {
178        set_time_limit(30);
179        node_delete($data->nid);
180        $deleted ++;
181      }
182      return $deleted;
183    }
184    
185    function _delete_all_quick($types) {
186      $deleted = 0;
187      foreach ($types as $type) {
188        // keep this alive
189        set_time_limit(240);
190    
191        // determine how many items will be deleted
192        $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = '%s'", $type));
193        if ($count) { // should always be positive
194          /**
195           * build a list of tables that need to be deleted from
196           *
197           * The tables array is of the format table_name => array('col1', 'col2', ...)
198           * where "col1, col2" are using "nid, vid", but could simply be "nid".
199           */
200    
201          $nid_vid = array('nid', 'vid');
202          $nid = array('nid');
203          $tables = array('node_revisions' => $nid_vid, 'comments' => $nid);
204          $tables[_content_tablename($type, CONTENT_DB_STORAGE_PER_CONTENT_TYPE)] = $nid_vid;
205          $content = content_types($type);
206          if (count($content['fields'])) {
207            foreach ($content['fields'] as $field) {
208              $field_info = content_database_info($field);
209              $tables[$field_info['table']] = $nid_vid;
210            }
211          }
212    
213          // find all other tables that might be related
214          switch ($GLOBALS['db_type']) {
215            case 'mysql':
216            case 'mysqli':
217              $result_tables = db_query("SHOW TABLES");
218              while ($data = db_fetch_array($result_tables)) {
219                $table = array_pop($data);
220                if (isset($tables[$table]) || substr($table, 0, 8) == 'content_') {
221                  continue;
222                }
223                $result_cols = db_query("SHOW COLUMNS FROM %s", $table);
224                $cols = array();
225                while ($data = db_fetch_array($result_cols)) {
226                  $cols[$data['Field']] = $data;
227                }
228                if (isset($cols['nid'])) {
229                  $tables[$table] = isset($cols['vid']) ? $nid_vid : $nid;
230                }
231              }
232              break;
233    
234            case 'pgsql':
235              // @TODO: inspect the database and look for nid fields
236              break;
237          }
238    
239          // @todo: update all node related nid references
240    
241          // delete from all of the content tables in one sql statement
242          $sql = array('delete' => array(), 'from' => array(), 'where' => array());
243          $index = 0;
244          foreach ($tables as $table => $cols) {
245            $table = '{'. $table .'}';
246            $sql['cols'][] = "t$index.*";
247    
248            // build the ON clause
249            $on = array();
250            foreach ($cols as $col) {
251              $on[] = "t$index.$col = n.$col";
252            }
253    
254            // now that we have the ON clause, build the join clause
255            $sql['join'][] = " LEFT JOIN $table t$index ON ". implode(' AND ', $on);
256            $index ++;
257          }
258          $delete_sql = "DELETE n.*, ". implode(', ', $sql['cols']) ." FROM {node} n ". implode(' ', $sql['join']);
259          db_query($delete_sql ." WHERE n.type = '%s'", $type);
260    
261          $deleted += $count;
262        }
263      }
264      return $deleted;
265    }
266    
267  function delete_all_users() {  function delete_all_users() {
   $form['action'] = array(  
     '#type'  => 'hidden',  
     '#value' => 'users',  
   );  
268    return confirm_form(    return confirm_form(
269      $form,      $form,
270      t('Are you sure you want to delete all users?'),      t('Are you sure you want to delete all users?'),
# Line 71  function delete_all_users() { Line 275  function delete_all_users() {
275      'delete_all_content');      'delete_all_content');
276  }  }
277    
278  function delete_all_users_submit() {  function delete_all_users_submit($form, &$form_state) {
279    $result = db_query('SELECT uid FROM {users} WHERE uid > 1');    $result = db_query('SELECT uid FROM {users} WHERE uid > 1');
280    
281    $count = 0;    $count = 0;
282    while($data = db_fetch_object($result)) {    while ($data = db_fetch_object($result)) {
283      user_delete(array(), $data->uid);      user_delete(array(), $data->uid);
284      $count++;      $count++;
285    }    }
286    
287      // Delete the URL aliases
288      db_query("DELETE FROM {url_alias} WHERE src LIKE 'user/%%'");
289    
290    db_query("UPDATE {sequences} SET id = 1 WHERE name = 'users_uid'");    db_query("UPDATE {sequences} SET id = 1 WHERE name = 'users_uid'");
291    
292    drupal_set_message(t('All users have been deleted. Number of users deleted: !count.', array('!count' => $count)));    drupal_set_message(t('All users have been deleted. Number of users deleted: !count.', array('!count' => $count)));
293    
294    drupal_goto('admin');    drupal_goto('admin');
295  }  }
296    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.2