| 1 |
<?php
|
| 2 |
// $Id: system_table_cleaner.module,v 1.2 2009/03/03 18:08:58 mikeyp Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* System table cleaner module, a tool to remove old items from system table.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_theme().
|
| 11 |
*/
|
| 12 |
function system_table_cleaner_theme($existing, $type, $theme, $path) {
|
| 13 |
return array(
|
| 14 |
'system_table_cleaner_list' => array(
|
| 15 |
'arguments' => array('form' => NULL),
|
| 16 |
),
|
| 17 |
);
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Implementation of hook_menu().
|
| 22 |
*/
|
| 23 |
function system_table_cleaner_menu() {
|
| 24 |
$items['admin/settings/systems-table-cleaner'] = array(
|
| 25 |
'title' => 'System Table Cleaner',
|
| 26 |
'description' => 'Remove old items from your system table.',
|
| 27 |
'page callback' => 'drupal_get_form',
|
| 28 |
'page arguments' => array('system_table_cleaner'),
|
| 29 |
'access arguments' => array('administer site configuraiton'),
|
| 30 |
'type' => MENU_NORMAL_ITEM,
|
| 31 |
);
|
| 32 |
$items['admin/settings/systems-table-cleaner/delete'] = array(
|
| 33 |
'page callback' => 'drupal_get_form',
|
| 34 |
'page arguments' => array('system_table_cleaner_delete_form'),
|
| 35 |
'access arguments' => array('administer site configuraiton'),
|
| 36 |
'type' => MENU_CALLBACK,
|
| 37 |
);
|
| 38 |
return $items;
|
| 39 |
}
|
| 40 |
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Menu callback for the admin page.
|
| 44 |
*/
|
| 45 |
function system_table_cleaner(&$form_state) {
|
| 46 |
if ($form_state['values']['op'] == t('Remove items') && $form_state['storage']['validate'] != FALSE) {
|
| 47 |
return system_table_cleaner_delete_confirm($form_state);
|
| 48 |
}
|
| 49 |
return system_table_cleaner_list();
|
| 50 |
}
|
| 51 |
|
| 52 |
/**
|
| 53 |
* Helper function to return a list of all items from system table missing files.
|
| 54 |
*/
|
| 55 |
function system_table_cleaner_find_missing() {
|
| 56 |
$result = db_query("SELECT * from {system}");
|
| 57 |
|
| 58 |
$items = array();
|
| 59 |
while ($record = db_fetch_object($result)) {
|
| 60 |
$record->info = unserialize($record->info);
|
| 61 |
$items[$record->name] = $record;
|
| 62 |
}
|
| 63 |
|
| 64 |
foreach ($items as $item) {
|
| 65 |
if (file_exists($item->filename)) {
|
| 66 |
unset($items[$item->name]);
|
| 67 |
}
|
| 68 |
}
|
| 69 |
|
| 70 |
return $items;
|
| 71 |
}
|
| 72 |
|
| 73 |
/**
|
| 74 |
* Theme function for overview page.
|
| 75 |
*/
|
| 76 |
function theme_system_table_cleaner_list($form) {
|
| 77 |
$output .= drupal_render($form['instructions']);
|
| 78 |
|
| 79 |
if (isset($form['no_items'])) {
|
| 80 |
$output .= '<p>'. drupal_render($form['no_items']) .'</p>';
|
| 81 |
}
|
| 82 |
else {
|
| 83 |
$header = array('', t('Name'), t('Type'), t('Status'));
|
| 84 |
$rows = array();
|
| 85 |
|
| 86 |
foreach (element_children($form['title']) as $key) {
|
| 87 |
$row = array();
|
| 88 |
$row[] = drupal_render($form['items'][$key]);
|
| 89 |
$row[] = '<strong>'. drupal_render($form['title'][$key]) .'</strong><div class="description">'. drupal_render($form['description'][$key]) .'</div>';
|
| 90 |
$row[] = drupal_render($form['type'][$key]);
|
| 91 |
$row[] = drupal_render($form['status'][$key]);
|
| 92 |
$rows[] = $row;
|
| 93 |
}
|
| 94 |
$output .= theme('table', $header, $rows);
|
| 95 |
}
|
| 96 |
$output .= drupal_render($form);
|
| 97 |
return $output;
|
| 98 |
}
|
| 99 |
|
| 100 |
/**
|
| 101 |
* Generate the form for the list of all unused items.
|
| 102 |
*/
|
| 103 |
function system_table_cleaner_list() {
|
| 104 |
$form = array();
|
| 105 |
|
| 106 |
$form['instructions'] = array(
|
| 107 |
'#value' => t('System Table Cleaner searches the system table for unused items. For an item to be unused its corresponding module or theme files must be missing.'),
|
| 108 |
);
|
| 109 |
|
| 110 |
$missing_items = system_table_cleaner_find_missing();
|
| 111 |
|
| 112 |
if (empty($missing_items)) {
|
| 113 |
$form['no_items'] = array(
|
| 114 |
'#value' => t('No unused items were found.'),
|
| 115 |
);
|
| 116 |
}
|
| 117 |
else {
|
| 118 |
$form['submit'] = array(
|
| 119 |
'#type' => 'submit',
|
| 120 |
'#value' => t('Remove items'),
|
| 121 |
'#submit' => array('system_table_cleaner_list_submit'),
|
| 122 |
'#validate' => array('system_table_cleaner_list_validate'),
|
| 123 |
);
|
| 124 |
}
|
| 125 |
|
| 126 |
$items = array();
|
| 127 |
foreach ($missing_items as $item) {
|
| 128 |
$items[$item->filename] = '';
|
| 129 |
$form['title'][$item->filename] = array('#value' => $item->info['name']);
|
| 130 |
$form['description'][$item->filename] = array('#value' => $item->info['description']);
|
| 131 |
$form['type'][$item->filename] = array('#value' => $item->type);
|
| 132 |
if ($item->schema_version != '-1') {
|
| 133 |
$form['status'][$item->filename] = array(
|
| 134 |
'#value' => theme('image', 'misc/watchdog-warning.png') .' '. t('Previously installed'),
|
| 135 |
);
|
| 136 |
}
|
| 137 |
else {
|
| 138 |
$form['status'][$item->filename] = array(
|
| 139 |
'#value' => t('Never installed'),
|
| 140 |
);
|
| 141 |
}
|
| 142 |
}
|
| 143 |
$form['items'] = array('#type' => 'checkboxes', '#options' => $items);
|
| 144 |
$form['#theme'] = 'system_table_cleaner_list';
|
| 145 |
|
| 146 |
return $form;
|
| 147 |
}
|
| 148 |
|
| 149 |
/**
|
| 150 |
* Validation handler for overview page.
|
| 151 |
*/
|
| 152 |
function system_table_cleaner_list_validate($form, &$form_state) {
|
| 153 |
$items = array_filter($form_state['values']['items']);
|
| 154 |
if (empty($items)) {
|
| 155 |
drupal_set_message(t('You must specify at least one item to remove.'));
|
| 156 |
$form_state['storage']['validate'] = FALSE;
|
| 157 |
return;
|
| 158 |
}
|
| 159 |
$form_state['storage']['validate'] = TRUE;
|
| 160 |
}
|
| 161 |
|
| 162 |
/**
|
| 163 |
* Submit handler for overview page.
|
| 164 |
*/
|
| 165 |
function system_table_cleaner_list_submit($form, &$form_state) {
|
| 166 |
// Set rebuild so that we refresh the form.
|
| 167 |
// Don't need to set other values here as a switch was set in
|
| 168 |
$form_state['rebuild'] = TRUE;
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Delete confirmation form.
|
| 173 |
*/
|
| 174 |
function system_table_cleaner_delete_confirm(&$form_state) {
|
| 175 |
drupal_set_title(t('Are you sure?'));
|
| 176 |
$form['instructions'] = array('#value' => 'Are you sure you want to remove the following items from your system table?');
|
| 177 |
|
| 178 |
// Copy the submitted items out of values, because they won't be the values of *this* form
|
| 179 |
// when it is submitted.
|
| 180 |
$form_state['storage']['items'] = $form_state['values']['items'];
|
| 181 |
|
| 182 |
$items = array();
|
| 183 |
foreach ($form_state['values']['items'] as $filename) {
|
| 184 |
$result = db_query("SELECT * FROM {system} where filename = '%s'", $filename);
|
| 185 |
$item = db_fetch_object($result);
|
| 186 |
$item->info = unserialize($item->info);
|
| 187 |
$items[] = $item->info['name'];
|
| 188 |
}
|
| 189 |
$form['items'] = array('#value' => theme('item_list', $items));
|
| 190 |
$form['submit'] = array('#type' => 'submit', '#value' => 'Remove items');
|
| 191 |
$form['#submit'] = array('system_table_cleaner_delete_confirm_submit');
|
| 192 |
|
| 193 |
return $form;
|
| 194 |
}
|
| 195 |
|
| 196 |
/**
|
| 197 |
* Submit handler for delete form.
|
| 198 |
*/
|
| 199 |
function system_table_cleaner_delete_confirm_submit($form, &$form_state) {
|
| 200 |
system_table_cleaner_delete($form_state['storage']['items']);
|
| 201 |
$form_state['storage']['validate'] = FALSE;
|
| 202 |
}
|
| 203 |
|
| 204 |
/**
|
| 205 |
* Helper function to delete items from system table.
|
| 206 |
*/
|
| 207 |
function system_table_cleaner_delete($filenames = array()) {
|
| 208 |
foreach ($filenames as $item) {
|
| 209 |
db_query("DELETE FROM {system} WHERE filename = '%s'", $item);
|
| 210 |
}
|
| 211 |
}
|