View source
<?php
function delete_all_menu() {
$items = array();
$items['admin/content/delete_content'] = array(
'title' => '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.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'delete_all_content',
),
'access arguments' => array(
'administer nodes',
),
'type' => MENU_NORMAL_ITEM,
);
$items['admin/content/delete_content/confirm'] = array(
'title' => 'Confirm deletion of all content',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'delete_all_content_confirm',
),
'access arguments' => array(
'administer nodes',
),
'type' => MENU_CALLBACK,
);
$items['admin/content/delete_users'] = array(
'title' => 'Delete all 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.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'delete_all_users',
),
'access arguments' => array(
'administer users',
),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function delete_all_content() {
$result = db_query("SELECT type, COUNT(*) AS num FROM {node} GROUP BY type");
$count = array();
while ($data = db_fetch_object($result)) {
$count[$data->type] = $data->num;
}
$types = array();
foreach (node_get_types() as $type => $info) {
if ($count[$type] > 0) {
$types[$type] = $info->name . ' (' . $count[$type] . ')';
}
}
asort($types);
if (empty($types)) {
$form = array();
$form['no_content_types'] = array(
'#prefix' => '<p>',
'#suffix' => '</p>',
'#value' => t('There are no content types with content available to delete. You must <a href="@node-add">create some content</a> in order to delete it.', array(
'@node-add' => url('node/add'),
)),
);
if (module_exists('devel')) {
$form['generate_content_suggestion'] = array(
'#prefix' => '<p>',
'#suffix' => '</p>',
'#value' => t('You can generate content quickly at the <a href="@generate-content-page">generate content page</a>.', array(
'@generate-content-page' => url('admin/generate/content'),
)),
);
}
return $form;
}
drupal_add_js(drupal_get_path('module', 'delete_all') . '/delete_all.js');
$form = array();
$form['all'] = array(
'#type' => 'checkbox',
'#default_value' => TRUE,
'#title' => t('Delete All Content'),
'#description' => t('Select to delete all content'),
'#attributes' => array(
'class' => 'delete-all',
),
);
$form['type-fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Types'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'types' => array(
'#type' => 'checkboxes',
'#options' => $types,
'#description' => t('Select the types of content to delete'),
'#theme' => 'delete_all_checkboxes',
),
);
$form['method-fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Method'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'method' => array(
'#type' => 'radios',
'#title' => t('Method'),
'#options' => array(
'normal' => t('Normal'),
'quick' => t('Quick'),
),
'#default_value' => 'normal',
'#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.'),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
);
$form['#action'] = url('admin/content/delete_content/confirm');
return $form;
}
function delete_all_theme() {
return array(
'theme_delete_all_checkboxes' => array(
'arguments' => array(
'form' => NULL,
),
),
);
}
function theme_delete_all_checkboxes($form) {
$total = 0;
foreach ($form as $element_id => $element) {
if ($element_id[0] != '#') {
$total++;
}
}
$total = (int) ($total % 3 ? ($total + 2) / 3 : $total / 3);
$pos = 0;
$rows = array();
foreach ($form as $element_id => $element) {
if ($element_id[0] != '#') {
$pos++;
$row = $pos % $total;
$col = $pos / $total;
if (!isset($rows[$row])) {
$rows[$row] = array();
}
$rows[$row][$col] = drupal_render($element);
}
}
return theme('table', array(), $rows);
}
function delete_all_content_confirm() {
$results = $_POST;
$form = array();
$form['method'] = array(
'#type' => 'hidden',
'#value' => $results['method'],
);
$form['all'] = array(
'#type' => 'hidden',
'#value' => $results['all'],
);
if ($results['all']) {
$form['all_warning'] = array(
'#prefix' => '<p>',
'#suffix' => '<p>',
'#value' => t('All content in all content types will be deleted. Be sure to have a backup of any important data!'),
);
}
if (is_array($results['types'])) {
foreach ($results['types'] as $key_type => $type) {
$form['type_' . $key_type] = array(
'#type' => 'hidden',
'#value' => $type,
);
$info = node_get_types('type', $type);
$form[$type . '_warning'] = array(
'#prefix' => '<p>',
'#suffix' => '<p>',
'#value' => t('All content in the %type content type will be deleted. Be sure to have a backup of any important data!', array(
'%type' => $info->name,
)),
);
}
}
$keys = array_filter(array_keys($results), "_delete_all_type_keys");
foreach ($keys as $key) {
$form[$key] = array(
'#type' => 'hidden',
'#value' => $results[$key],
);
}
return confirm_form($form, t('Are you sure you wish to delete content?'), 'admin/content/delete_content', NULL, t('Delete all content now'), t('Cancel delete of all content'));
}
function delete_all_content_confirm_submit($form, &$form_state) {
$types = array();
$form_state['values']['types'] = array();
if (!$form_state['values']['all']) {
$keys = array_filter(array_keys($form_state['values']), "_delete_all_type_keys");
foreach ($keys as $key) {
$types[] = $form_state['values'][$key];
}
}
switch ($form_state['values']['method']) {
case 'normal':
$count = _delete_all_normal($form_state['values']['all'], $types);
break;
case 'quick':
if ($form_state['values']['all']) {
$result = db_query("SELECT DISTINCT type FROM {node}");
while ($data = db_fetch_object($result)) {
$types[] = $type;
}
}
$count = _delete_all_quick($types);
break;
}
if ($form_state['values']['all']) {
db_query("DELETE FROM {url_alias} WHERE src LIKE 'node/%%'");
drupal_set_message(t('All nodes, comments and URL aliases have been deleted. Number of nodes deleted: !count.', array(
'!count' => $count,
)));
}
else {
drupal_set_message(t('Nodes and comments of type @type have been deleted. Number of nodes deleted: !count.', array(
'!count' => $count,
'@type' => implode(', ', $types),
)));
}
drupal_goto('admin');
}
function _delete_all_normal($all, $types) {
if ($all) {
$result = db_query('SELECT nid FROM {node}' . $where);
}
else {
$placeholders = implode(',', array_fill(0, count($types), "'%s'"));
$result = db_query('SELECT nid FROM {node} WHERE type IN (' . $placeholders . ')', $types);
}
$deleted = 0;
while ($data = db_fetch_object($result)) {
set_time_limit(30);
node_delete($data->nid);
$deleted++;
}
return $deleted;
}
function _delete_all_quick($types) {
$deleted = 0;
foreach ($types as $type) {
set_time_limit(240);
$count = db_result(db_query("SELECT COUNT(*) FROM {node} WHERE type = '%s'", $type));
if ($count) {
$nid_vid = array(
'nid',
'vid',
);
$nid = array(
'nid',
);
$tables = array(
'node_revisions' => $nid_vid,
'comments' => $nid,
);
$tables[_content_tablename($type, CONTENT_DB_STORAGE_PER_CONTENT_TYPE)] = $nid_vid;
$content = content_types($type);
if (count($content['fields'])) {
foreach ($content['fields'] as $field) {
$field_info = content_database_info($field);
$tables[$field_info['table']] = $nid_vid;
}
}
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$result_tables = db_query("SHOW TABLES");
while ($data = db_fetch_array($result_tables)) {
$table = array_pop($data);
if (isset($tables[$table]) || substr($table, 0, 8) == 'content_') {
continue;
}
$result_cols = db_query("SHOW COLUMNS FROM %s", $table);
$cols = array();
while ($data = db_fetch_array($result_cols)) {
$cols[$data['Field']] = $data;
}
if (isset($cols['nid'])) {
$tables[$table] = isset($cols['vid']) ? $nid_vid : $nid;
}
}
break;
case 'pgsql':
break;
}
$sql = array(
'delete' => array(),
'from' => array(),
'where' => array(),
);
$index = 0;
foreach ($tables as $table => $cols) {
$table = '{' . $table . '}';
$sql['cols'][] = "t{$index}.*";
$on = array();
foreach ($cols as $col) {
$on[] = "t{$index}.{$col} = n.{$col}";
}
$sql['join'][] = " LEFT JOIN {$table} t{$index} ON " . implode(' AND ', $on);
$index++;
}
$delete_sql = "DELETE n.*, " . implode(', ', $sql['cols']) . " FROM {node} n " . implode(' ', $sql['join']);
db_query($delete_sql . " WHERE n.type = '%s'", $type);
$deleted += $count;
}
}
return $deleted;
}
function delete_all_users() {
return confirm_form($form, t('Are you sure you want to delete all users?'), 'admin', '', t('Delete'), t('Cancel'), 'delete_all_content');
}
function delete_all_users_submit($form, &$form_state) {
$result = db_query('SELECT uid FROM {users} WHERE uid > 1');
$count = 0;
while ($data = db_fetch_object($result)) {
user_delete(array(), $data->uid);
$count++;
}
db_query("DELETE FROM {url_alias} WHERE src LIKE 'user/%%'");
db_query("UPDATE {sequences} SET id = 1 WHERE name = 'users_uid'");
drupal_set_message(t('All users have been deleted. Number of users deleted: !count.', array(
'!count' => $count,
)));
drupal_goto('admin');
}
function _delete_all_type_keys($var) {
return strpos($var, 'type_') === 0 ? TRUE : FALSE;
}