You are here

function auditfiles_notonserver in Audit Files 5

Same name and namespace in other branches
  1. 6.3 notonserver.admin.inc \auditfiles_notonserver()
  2. 6.2 notonserver.admin.inc \auditfiles_notonserver()

Menu callback: audit files not on the server.

1 string reference to 'auditfiles_notonserver'
auditfiles_menu in ./auditfiles.module
Implementation of hook_menu().

File

./auditfiles.module, line 89

Code

function auditfiles_notonserver() {
  drupal_set_title(t('Audit of files not on the server'));

  // Initialise table header to allow sorting
  $header = array(
    array(
      'data' => t('Node'),
      'field' => 'nid',
      'sort' => 'asc',
    ),
    array(
      'data' => t('File'),
      'field' => 'filepath',
    ),
    array(
      'data' => t('Operations'),
    ),
  );

  // Get all the files from the files table using defined sort order
  $sql = 'SELECT nid, filepath FROM {files}';
  $table_sort = tablesort_sql($header);
  $result = db_query($sql . $table_sort);

  // Initialise array to hold rows of table
  $rows = array();

  // Iterate through the results
  while ($file = db_fetch_object($result)) {

    // Construct a valid drupal path for the named file
    $target = file_create_path($file->filepath);

    // Check to see if the file exists
    if (!file_exists($target)) {

      // If it doesn't strip out the directory path and store the result
      $file->filepath = preg_replace('@^' . preg_quote(file_directory_path()) . '/@', '', $file->filepath);
      $rows[] = array(
        array(
          'data' => l($file->nid, 'node/' . $file->nid),
        ),
        array(
          'data' => $file->filepath,
        ),
        array(
          'data' => l(t('edit'), 'node/' . $file->nid . '/edit'),
        ),
      );
    }
  }

  // Create output string
  if ($rows) {
    $output .= format_plural(count($rows), '1 file found.', '@count files found.');
    $output .= theme('table', $header, $rows);
  }
  else {
    $output .= t('No files found.');
  }

  // Return the results
  return $output;
}