| 1 |
<?php
|
| 2 |
// $Id: notindb.admin.inc,v 1.1 2007/12/05 22:33:19 stuartgreenfield Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Callback and functions to generate unreferenced files report
|
| 7 |
*/
|
| 8 |
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Menu callback: audit files not in the database.
|
| 12 |
*/
|
| 13 |
function auditfiles_unreferenced() {
|
| 14 |
return drupal_get_form('auditfiles_unreferenced_form');
|
| 15 |
}
|
| 16 |
|
| 17 |
|
| 18 |
/**
|
| 19 |
* Form definition for audit files not in database
|
| 20 |
*/
|
| 21 |
function auditfiles_unreferenced_form(&$form_state) {
|
| 22 |
if (isset($form_state['storage']['confirm'])) {
|
| 23 |
return auditfiles_unreferenced_form_confirm($form_state);
|
| 24 |
}
|
| 25 |
|
| 26 |
// Get the list of files that aren't in the database
|
| 27 |
$filesunreferenced = _auditfiles_filesunreferenced();
|
| 28 |
|
| 29 |
// Output count of files not in the database
|
| 30 |
if ($filesunreferenced) {
|
| 31 |
$form['count'] = array(
|
| 32 |
'#value' => format_plural(count($filesunreferenced), '1 file found.', '@count files found.'),
|
| 33 |
);
|
| 34 |
}
|
| 35 |
else {
|
| 36 |
$form['count'] = array(
|
| 37 |
'#value' => t('No files found.'),
|
| 38 |
);
|
| 39 |
}
|
| 40 |
|
| 41 |
// Action button
|
| 42 |
$form['options'] = array(
|
| 43 |
'#type' => 'fieldset',
|
| 44 |
'#title' => t('Action'),
|
| 45 |
'#prefix' => '<div class="container-inline">',
|
| 46 |
'#suffix' => '</div>',
|
| 47 |
);
|
| 48 |
|
| 49 |
$options = array(
|
| 50 |
'donothing' => t('Do nothing'),
|
| 51 |
'delete' => t('Delete checked files'),
|
| 52 |
);
|
| 53 |
|
| 54 |
$form['options']['operation'] = array(
|
| 55 |
'#type' => 'select',
|
| 56 |
'#options' => $options,
|
| 57 |
'#default_value' => 'donothing',
|
| 58 |
);
|
| 59 |
|
| 60 |
$form['options']['submit'] = array(
|
| 61 |
'#type' => 'submit',
|
| 62 |
'#value' => t('Update'),
|
| 63 |
);
|
| 64 |
|
| 65 |
|
| 66 |
// Process each result in turn and build check box list
|
| 67 |
$files_dir = file_directory_path();
|
| 68 |
$pathlen = strlen($files_dir)+1;
|
| 69 |
$files=array();
|
| 70 |
foreach ($filesunreferenced as $file) {
|
| 71 |
$files[$file] = '';
|
| 72 |
$filepath = substr($file, $pathlen);
|
| 73 |
// Can't use file_create_url as the links fail if the site uses private transfers
|
| 74 |
// Force a public url instead
|
| 75 |
$basename = basename($file);
|
| 76 |
$form['basename'][$file] = array('#value' => l($basename, 'admin/reports/auditfiles/unreferenced/'.$basename));
|
| 77 |
$form['file'][$file] = array('#value' => l($filepath, $GLOBALS['base_url'] .'/'. str_replace('\\', '/', $file)));
|
| 78 |
}
|
| 79 |
|
| 80 |
// Add list of files to checkboxes
|
| 81 |
$form['files'] = array('#type' => 'checkboxes', '#options' => $files);
|
| 82 |
|
| 83 |
// Return form
|
| 84 |
return $form;
|
| 85 |
}
|
| 86 |
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Theme auditfiles_unreferenced_form
|
| 90 |
*/
|
| 91 |
function theme_auditfiles_unreferenced_form($form) {
|
| 92 |
// Render count
|
| 93 |
$output = drupal_render($form['count']);
|
| 94 |
|
| 95 |
// If there are files found
|
| 96 |
if (isset($form['file']) && is_array($form['file'])) {
|
| 97 |
|
| 98 |
// Render actions
|
| 99 |
$output .= drupal_render($form['options']);
|
| 100 |
|
| 101 |
// Construct table of files
|
| 102 |
$header = array(
|
| 103 |
t('Select'),
|
| 104 |
t('Basename'),
|
| 105 |
t('Path'),
|
| 106 |
);
|
| 107 |
|
| 108 |
foreach (element_children($form['file']) as $key) {
|
| 109 |
$row = array();
|
| 110 |
$row[] = drupal_render($form['files'][$key]);
|
| 111 |
$row[] = drupal_render($form['basename'][$key]);
|
| 112 |
$row[] = drupal_render($form['file'][$key]);
|
| 113 |
$rows[] = $row;
|
| 114 |
}
|
| 115 |
|
| 116 |
// Render themed table
|
| 117 |
$output .= theme('table', $header, $rows);
|
| 118 |
|
| 119 |
}
|
| 120 |
|
| 121 |
$output .= drupal_render($form);
|
| 122 |
|
| 123 |
// Return output
|
| 124 |
return $output;
|
| 125 |
}
|
| 126 |
|
| 127 |
function auditfiles_unreferenced_form_submit($form, &$form_state) {
|
| 128 |
// drupal_set_message("submit form_state: ".dpr($form_state, TRUE));
|
| 129 |
if ($form_state['clicked_button']['#id'] == 'edit-submit' &&
|
| 130 |
$form_state['values']['options']['operation'] <> 'donothing') {
|
| 131 |
$form_state['storage']['confirm'] = TRUE;
|
| 132 |
$form_state['storage']['values'] = $form_state['values'];
|
| 133 |
}
|
| 134 |
}
|
| 135 |
|
| 136 |
function auditfiles_unreferenced_form_confirm(&$form_state) {
|
| 137 |
$values = $form_state['storage']['values'];
|
| 138 |
drupal_set_message("form_confirm: values: ".dpr($values, TRUE));
|
| 139 |
|
| 140 |
$form['changelist'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
|
| 141 |
|
| 142 |
// Note we iterate over fidvals, since no operation makes sense without a valid file
|
| 143 |
$count = 0;
|
| 144 |
foreach ($values['files'] as $key => $file) {
|
| 145 |
if ($file) {
|
| 146 |
$count++;
|
| 147 |
// Limit what's displayed (also helps avoid max_allowed_packet errors)
|
| 148 |
if ($count == 50) {
|
| 149 |
$message = '...and many more...';
|
| 150 |
} elseif ($count < 50) {
|
| 151 |
$message = "Deleting file <strong>$file</strong>";
|
| 152 |
}
|
| 153 |
if ($message) {
|
| 154 |
$form['changelist'][$key] = array('#type' => 'hidden', '#value' => $message,
|
| 155 |
'#prefix' => '<li>', '#suffix' => $message ."</li>\n");
|
| 156 |
unset($message);
|
| 157 |
}
|
| 158 |
}
|
| 159 |
}
|
| 160 |
$form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
|
| 161 |
$form['#submit'][] = 'auditfiles_unreferenced_form_confirm_submit';
|
| 162 |
|
| 163 |
return confirm_form(
|
| 164 |
$form,
|
| 165 |
t('Are you sure you want to delete these files?'),
|
| 166 |
'admin/reports/auditfiles/unreferenced',
|
| 167 |
'<strong>'. t('This action cannot be undone.') .'</strong>',
|
| 168 |
t('Process all'),
|
| 169 |
t('Cancel')
|
| 170 |
);
|
| 171 |
}
|
| 172 |
|
| 173 |
|
| 174 |
function auditfiles_unreferenced_form_confirm_submit($form, &$form_state) {
|
| 175 |
// drupal_set_message('confirm_submit, form: '.dpr($form, TRUE));
|
| 176 |
// drupal_set_message('confirm_submit, form_state: '.dpr($form_state, TRUE));
|
| 177 |
if ($form_state['values']['confirm']) {
|
| 178 |
$values = $form_state['storage']['values'];
|
| 179 |
drupal_set_message("in confirm_submit, values: ".dpr($values,TRUE));
|
| 180 |
foreach ($values['files'] as $filename) {
|
| 181 |
if ($filename) {
|
| 182 |
db_query("DELETE FROM {files}
|
| 183 |
WHERE filepath='%s'",
|
| 184 |
$filename);
|
| 185 |
if (file_delete(file_create_path($filename))) {
|
| 186 |
watchdog('audit', '%file was deleted', array('%file' => $filename));
|
| 187 |
}
|
| 188 |
else {
|
| 189 |
drupal_set_message(t('Failed to delete %file', array('%file' => $filename)));
|
| 190 |
}
|
| 191 |
}
|
| 192 |
}
|
| 193 |
// Clear so our return to the primary form doesn't think we're going to the confirmation form
|
| 194 |
unset($form_state['storage']['confirm']);
|
| 195 |
}
|
| 196 |
}
|
| 197 |
|
| 198 |
|
| 199 |
/**
|
| 200 |
* Helper function - retrieve sorted list of files that are in {files}
|
| 201 |
* but not in {upload}
|
| 202 |
*/
|
| 203 |
function _auditfiles_filesunreferenced() {
|
| 204 |
|
| 205 |
// Prepare array to hold results
|
| 206 |
$filesunreferenced = array();
|
| 207 |
|
| 208 |
// Get all the files out the {files} table and store as qualified path
|
| 209 |
$result = db_query('SELECT f.filepath
|
| 210 |
FROM {files} f
|
| 211 |
LEFT JOIN {upload} u ON f.fid = u.fid
|
| 212 |
WHERE u.fid IS NULL
|
| 213 |
ORDER BY filepath ASC');
|
| 214 |
while ($file = db_fetch_object($result)) {
|
| 215 |
$filesunreferenced[] = file_create_path($file->filepath);
|
| 216 |
}
|
| 217 |
|
| 218 |
return $filesunreferenced;
|
| 219 |
}
|
| 220 |
|
| 221 |
function auditfiles_node_references_form($form_state, $basename) {
|
| 222 |
$form['description'] = array(
|
| 223 |
'#prefix' => '<div>',
|
| 224 |
'#value' => t("The nodes listed below reference the filename <strong>$basename</strong>,
|
| 225 |
but there is no record of the reference in the Drupal database. Review these nodes to
|
| 226 |
determine how they're referencing the file.
|
| 227 |
"),
|
| 228 |
'#suffix' => '</div>',
|
| 229 |
);
|
| 230 |
$result = db_query("SELECT nr.nid,nr.title
|
| 231 |
FROM {node_revisions} nr
|
| 232 |
WHERE nr.body LIKE '%%%s%%'",
|
| 233 |
$basename);
|
| 234 |
while ($node = db_fetch_object($result)) {
|
| 235 |
drupal_set_message("nid=$node->nid, title=$node->title");
|
| 236 |
$form['node'][$file] = array('#value' => l($node->title, 'node/'.$node->nid));
|
| 237 |
}
|
| 238 |
return $form;
|
| 239 |
}
|
| 240 |
|
| 241 |
/**
|
| 242 |
* Theme auditfiles_unreferenced_form
|
| 243 |
*/
|
| 244 |
function theme_auditfiles_node_references_form($form) {
|
| 245 |
// Render count
|
| 246 |
$output = drupal_render($form['description']);
|
| 247 |
|
| 248 |
// If there are files found
|
| 249 |
if (isset($form['node']) && is_array($form['node'])) {
|
| 250 |
// Construct table of files
|
| 251 |
$header = array(
|
| 252 |
t('Matching nodes'),
|
| 253 |
);
|
| 254 |
|
| 255 |
foreach (element_children($form['node']) as $key) {
|
| 256 |
$row = array();
|
| 257 |
$row[] = drupal_render($form['node'][$key]);
|
| 258 |
$rows[] = $row;
|
| 259 |
}
|
| 260 |
|
| 261 |
// Render themed table
|
| 262 |
$output .= theme('table', $header, $rows);
|
| 263 |
|
| 264 |
}
|
| 265 |
|
| 266 |
$output .= drupal_render($form);
|
| 267 |
|
| 268 |
// Return output
|
| 269 |
return $output;
|
| 270 |
}
|
| 271 |
|