/[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.2.3, Mon Dec 24 03:23:55 2007 UTC revision 1.1.2.4, Wed Dec 26 21:59:09 2007 UTC
# Line 25  function delete_all_menu($maycache) { Line 25  function delete_all_menu($maycache) {
25  }  }
26    
27  function delete_all_content() {  function delete_all_content() {
28    $form['action'] = array(    drupal_add_js(drupal_get_path('module', 'delete_all') .'/delete_all.js');
29      '#type'  => 'hidden',    $form = array();
30      '#value' => 'content',    $form['all'] = array(
31        '#type' => 'checkbox',
32        '#default_value' => TRUE,
33        '#title' => t('Delete All Content'),
34        '#description' => t('Select to delete all content'),
35        '#attributes' => array('class' => 'delete-all'),
36    );    );
   return confirm_form(  
     $form,  
     t('Are you sure you want to delete all content?'),  
     'admin',  
     '',  
     t('Delete'),  
     t('Cancel'),  
     'delete_all_content');  
 }  
37    
38  function delete_all_content_submit() {    // count how many nodes we have of each type
39    $result = db_query('SELECT nid FROM {node}');    $result = db_query("SELECT type, COUNT(*) AS num FROM {node} GROUP BY type");
40      $count = array();
41      while ($data = db_fetch_object($result)) {
42        $count[$data->type] = $data->num;
43      }
44    
45    $count = 0;    // add the types to the form
46    while($data = db_fetch_object($result)) {    $types = array();
47      node_delete($data->nid);    foreach (node_get_types() as $type => $info) {
48      $count++;      if ($count[$type] > 0) {
49          $types[$type] = $info->name .' ('. $count[$type]. ')';
50        }
51    }    }
52      asort($types);
53      $form['type-fieldset'] = array(
54        '#type' => 'fieldset',
55        '#title' => t('Types'),
56        '#collapsible' => TRUE,
57        '#collapsed' => TRUE,
58        'types' => array(
59          '#type' => 'checkboxes',
60          '#options' => $types,
61          '#description' => t('Select the types of content to delete'),
62          '#theme' => 'delete_all_checkboxes',
63        ),
64      );
65      $form['method'] = array(
66        '#type' => 'select',
67        '#title' => t('Method'),
68        '#options' => array('normal' => t('Normal'), 'quick' => t('Quick')),
69        '#default_value' => 'normal',
70        '#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.'),
71      );
72      $form['confirm'] = array(
73        '#type' => 'checkbox',
74        '#title' => t('Delete Confirmation'),
75        '#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.'),
76        '#required' => TRUE,
77      );
78      $form['submit'] = array(
79        '#type' => 'submit',
80        '#value' => t('Delete'),
81      );
82      $form['#submit'] = array('delete_all_content_submit' => array());
83      return $form;
84    }
85    
86    // Delete the URL aliases  function theme_delete_all_checkboxes($form) {
87    db_query("DELETE FROM {url_alias} WHERE src LIKE 'node/%%'");    $total = 0;
88      foreach ($form as $element_id => $element) {
89        if ($element_id[0] != '#') {
90          $total ++;
91        }
92      }
93      $total = (int) (($total % 3) ? (($total + 2) / 3) : ($total / 3));
94      $pos = 0;
95      $rows = array();
96      foreach ($form as $element_id => $element) {
97        if ($element_id[0] != '#') {
98          $pos ++;
99          $row = $pos % $total;
100          $col = $pos / $total;
101          if (!isset($rows[$row])) {
102            $rows[$row] = array();
103          }
104          $rows[$row][$col] = drupal_render($element);
105        }
106      }
107      return theme('table', array(), $rows);
108    }
109    
110    function delete_all_content_submit($form_id, &$form) {
111      $types = array();
112      if (!$form['all']) {
113        foreach ($form['types'] as $type => $checked) {
114          if ($checked) {
115            $types[] = $type;
116          }
117        }
118      }
119      switch ($form['method']) {
120        case 'normal':
121          $count = _delete_all_normal($form['all'], $types);
122          break;
123    
124        case 'quick':
125          // the quick method doesn't support an all option,
126          // so just get all the content types and delete all of those
127          if ($form['all']) {
128            $result = db_query("SELECT DISTINCT type FROM {node}");
129            while ($data = db_fetch_object($result)) {
130              $types[] = $type;
131            }
132          }
133          $count = _delete_all_quick($types);
134          break;
135      }
136    
137    // Reset the sequences    if ($form['all']) {
138    db_query("UPDATE {sequences} SET id = 1 WHERE name = 'node_nid'");      // Delete the URL aliases
139    db_query("UPDATE {sequences} SET id = 1 WHERE name = 'comment_cid'");      db_query("DELETE FROM {url_alias} WHERE src LIKE 'node/%%'");
140    
141        // Reset the sequences
142        db_query("UPDATE {sequences} SET id = 1 WHERE name = 'node_nid'");
143        db_query("UPDATE {sequences} SET id = 1 WHERE name = 'comment_cid'");
144    
145    drupal_set_message(t('All nodes, comments and URL aliases have been deleted. Number of nodes deleted: !count.', array('!count' => $count)));      drupal_set_message(t('All nodes, comments and URL aliases have been deleted. Number of nodes deleted: !count.', array('!count' => $count)));
146      }
147      else {
148        drupal_set_message(t('Nodes and comments of type @type have been deleted. Number of nodes deleted: !count.', array('!count' => $count, '@type' => implode(', ', $types))));
149      }
150    
151    drupal_goto('admin');    drupal_goto('admin');
152  }  }
153    
154    function _delete_all_normal($all, $types) {
155      if ($form['all']) {
156        $result = db_query('SELECT nid FROM {node}'. $where);
157      }
158      else {
159        $placeholders = implode(',', array_fill(0, count($types), "'%s'"));
160        $result = db_query('SELECT nid FROM {node} WHERE type IN ('. $placeholders .')', $types);
161      }
162    
163      $deleted = 0;
164      while ($data = db_fetch_object($result)) {
165        set_time_limit(30);
166        node_delete($data->nid);
167        $deleted ++;
168      }
169      return $deleted;
170    }
171    
172    function _delete_all_quick($types) {
173      $deleted = 0;
174      foreach ($types as $type) {
175        // determine how many items will be deleted
176        $count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = '%s'", $type));
177        if ($count) {
178          /**
179           * build a list of tables that need to be deleted from
180           *
181           * The tables array is of the format table_name => array('col1', 'col2', ...)
182           * where "col1, col2" are using "nid, vid", but could simply be "nid".
183           */
184    
185          $nid_vid = array('nid', 'vid');
186          $nid = array('nid');
187          $tables = array('node_revisions' => $nid_vid, $comments => $nid);
188          $tables[_content_tablename($type, CONTENT_DB_STORAGE_PER_CONTENT_TYPE)] = $nid_vid;
189          $content = content_types($type);
190          if (count($content->fields)) {
191            foreach ($content->fields as $field) {
192              $field_info = content_database_info($field);
193              $tables[$field_info['table']] = $nid_vid;
194            }
195          }
196    
197          // find all other related modules that might be related
198          // @TODO: inspect the database and look for nid fields
199          if (module_exists('og')) {
200            $tables['og'] = $nid;
201            $tables['og_uid'] = $nid;
202            $tables['og_ancestry'] = $nid;
203          }
204    
205          // delete from all of the content tables in one sql statement
206          $sql = array('delete' => array(), 'from' => array(), 'where' => array());
207          $index = 0;
208          foreach (array_unique($tables) as $table => $cols) {
209            $table = '{'. $table .'}';
210            $sql['cols'][] = "t$index.*";
211    
212            // build the ON clause
213            $on = array();
214            foreach ($cols as $col) {
215              $on[] = "t$index.$col = n.$col";
216            }
217    
218            // now that we have the ON clause, build the join clause
219            $sql['join'][] = " LEFT JOIN $table t$index ON ". implode(' AND ', $on);
220            $index ++;
221          }
222          db_query("DELETE n.*, ". implode(', ', $sql['cols']) ." FROM {node} n ". implode(' ', $sql['join']) ." WHERE n.type = '%s'", $type);
223    
224          $deleted += $count;
225        }
226      }
227      return $deleted;
228    }
229    
230  function delete_all_users() {  function delete_all_users() {
   $form['action'] = array(  
     '#type'  => 'hidden',  
     '#value' => 'users',  
   );  
231    return confirm_form(    return confirm_form(
232      $form,      $form,
233      t('Are you sure you want to delete all users?'),      t('Are you sure you want to delete all users?'),
# Line 75  function delete_all_users() { Line 238  function delete_all_users() {
238      'delete_all_content');      'delete_all_content');
239  }  }
240    
241  function delete_all_users_submit() {  function delete_all_users_submit($form_id, &$form) {
242    $result = db_query('SELECT uid FROM {users} WHERE uid > 1');    $result = db_query('SELECT uid FROM {users} WHERE uid > 1');
243    
244    $count = 0;    $count = 0;
# Line 93  function delete_all_users_submit() { Line 256  function delete_all_users_submit() {
256    
257    drupal_goto('admin');    drupal_goto('admin');
258  }  }
259    

Legend:
Removed from v.1.1.2.3  
changed lines
  Added in v.1.1.2.4

  ViewVC Help
Powered by ViewVC 1.1.2