You are here

function node_import_list_files in Node import 6

Returns a list of available files.

Related topics

6 calls to node_import_list_files()
node_import_add_form in ./node_import.admin.inc
Creates a new import task by letting the user fill in a wizard.
node_import_add_form_submit_reload in ./node_import.admin.inc
node_import_add_form_validate_next in ./node_import.admin.inc
node_import_list_files_form in ./node_import.admin.inc
node_import_list_files_form_submit_delete in ./node_import.admin.inc

... See full list

File

./node_import.inc, line 1600
Public API of the Node import module.

Code

function node_import_list_files($reset = FALSE) {
  global $user;
  static $files;
  if (!isset($files) || $reset) {
    $files = array();
    $path = node_import_directory();

    // If FTP uploads of files is allowed, rescan the directory.
    if (variable_get('node_import:ftp:enabled', 0)) {
      $ftp_user = user_load(array(
        'name' => variable_get('node_import:ftp:user', ''),
      ));

      //TODO: use the $validators functionality of file_save_upload() ?
      $extensions = array_map('drupal_strtolower', array_filter(explode(' ', variable_get('node_import:ftp:extensions', 'csv tsv txt'))));
      foreach ($extensions as $extension) {
        $extensions[] = drupal_strtoupper($extension);
      }
      if (!empty($extensions)) {
        $existing_files = array();
        $result = db_query("SELECT filepath FROM {files} WHERE filepath LIKE '%s%%' AND status = %d", $path, FILE_STATUS_PERMANENT);
        while ($file = db_fetch_object($result)) {
          $existing_files[$file->filepath] = TRUE;
        }
        foreach (file_scan_directory($path, '.*\\.((' . implode(')|(', $extensions) . '))') as $filename => $file) {
          if (!isset($existing_files[$file->filename])) {
            $record = (object) array(
              'uid' => $ftp_user->uid,
              'filename' => $file->basename,
              'filepath' => $file->filename,
              'filemime' => 'text/plain',
              //TODO: how to get real MIME?
              'filesize' => filesize($file->filename),
              'status' => FILE_STATUS_PERMANENT,
              'timestamp' => time(),
            );
            drupal_write_record('files', $record);
            drupal_set_message(t('A new file %name was detected in %path.', array(
              '%name' => $record->filename,
              '%path' => $path,
            )));
          }
        }
      }
    }

    // Users with 'administer imports' permission can see all files.

    //TODO: we should also filter out files that are already in use by another task.
    if (user_access('administer imports')) {
      $result = db_query("SELECT * FROM {files} WHERE filepath LIKE '%s%%' AND status = %d ORDER BY filename, timestamp", $path, FILE_STATUS_PERMANENT);
    }
    else {
      $result = db_query("SELECT * FROM {files} WHERE filepath LIKE '%s%%' AND (uid = %d OR uid = 0) AND status = %d ORDER BY filename, timestamp", $path, $user->uid, FILE_STATUS_PERMANENT);
    }

    // Create the list, keep only the still existing files.
    while ($file = db_fetch_object($result)) {
      if (!file_exists(file_create_path($file->filepath))) {
        drupal_set_message(t('The previously uploaded file %name is no longer present. Removing it from the list of uploaded files.', array(
          '%name' => $file->filepath,
        )));
        file_set_status($file, FILE_STATUS_TEMPORARY);
      }
      else {
        $files[$file->fid] = $file;
      }
    }
  }
  return $files;
}