You are here

function path_redirect_import_read_file in Path redirect import 7

Function that processes the file

5 calls to path_redirect_import_read_file()
drush_path_redirect_import in ./path_redirect_import.drush.inc
Implements the path-redirect-import drush command.
PathRedirectImportExportTest::testReadRedirectExportFile in tests/path_redirect_import.test
Performs the basic tests
PathRedirectImportLanguageTest::testReadRedirectLanguageFile in tests/path_redirect_import.test
Performs the basic tests
PathRedirectImportTest::testReadRedirectFile in tests/path_redirect_import.test
Performs the basic tests
path_redirect_import_form_submit in ./path_redirect_import.admin.inc
Submit form function

File

./path_redirect_import.module, line 36
Page and form to import drupal path redirects

Code

function path_redirect_import_read_file($file, $options = array()) {
  $options += array(
    'delimiter' => ',',
    'no_headers' => TRUE,
    'override' => FALSE,
    'status_code' => '301',
    'language' => LANGUAGE_NONE,
  );
  if (!($f = fopen($file, 'r'))) {
    return array(
      'success' => FALSE,
      'message' => array(
        t('Unable to read the file'),
      ),
    );
  }
  $line_no = 0;
  $count = 0;
  $messages = array();
  $success = FALSE;
  while ($line = fgetcsv($f, 0, $options['delimiter'])) {
    $message = array();
    $line_no++;
    if ($line_no == 1 && !$options['no_headers']) {
      drupal_set_message(t('Skipping the header row.'));
      continue;
    }
    if (!is_array($line)) {
      $messages[] = t('Line @line_no is invalid.', array(
        '@line_no' => $line_no,
      ));
      continue;
    }
    if (empty($line[0]) || empty($line[1])) {
      $messages[] = t('Line @line_no contains invalid data.', array(
        '@line_no' => $line_no,
      ));
      continue;
    }
    if (empty($line[2])) {
      $line[2] = $options['status_code'];
    }
    else {
      $redirect_options = redirect_status_code_options();
      if (!isset($redirect_options[$line[2]])) {
        $messages[] = t('Line @line_no contains invalid status code', array(
          '@line_no' => $line_no,
        ));
        continue;
      }
    }
    if (module_exists('locale')) {
      if (empty($line[3])) {
        $line[3] = $options['language'];
      }
      else {
        $language_options = locale_language_list('name');
        if (!isset($language_options[$line[3]])) {
          $messages[] = t('Line @line_no contains invalid language code', array(
            '@line_no' => $line_no,
          ));
          continue;
        }
      }
    }
    $source_parts = redirect_parse_url($line[0]);
    $data = array(
      'line_no' => $line_no,
      'source' => $source_parts['url'],
      'redirect' => isset($line[1]) ? $line[1] : NULL,
      'status_code' => $line[2],
      'override' => $options['override'],
      'language' => isset($line[3]) ? $line[3] : LANGUAGE_NONE,
    );
    if (!empty($source_parts['query'])) {
      $data['source_options']['query'] = $source_parts['query'];
    }
    $insert_row = path_redirect_import_save_data($data);
    if (!$insert_row['success']) {
      $messages[] = $insert_row['message'];
    }
    else {
      $count++;
    }
  }
  fclose($f);
  if ($count > 0) {
    $messages[] = t('@count row(s) imported.', array(
      '@count' => $count,
    ));
    $success = TRUE;
  }
  return array(
    'success' => $success,
    'message' => $messages,
  );
}