Good evening. I have suggestions for function getTemplate(). I was faced with the header encoding. As they unloaded in UTF-8 Russian titles are not supported. I propose to do as follows,

  public function getTemplate() {
    $mappings = feeds_importer($this->id)->processor->config['mappings'];
    $sources = $uniques = array();
    $encoding = $this->config['encoding'];

    foreach ($mappings as $mapping) {
      if (in_array($mapping['source'], $uniques) || in_array($mapping['source'], $sources)) {
        // Skip columns we've already seen.
        continue;
      }

      if (!empty($mapping['unique'])) {
        $uniques[] = $mapping['source'];
      }
      else {
        $sources[] = $mapping['source'];
      }
    }

    $sep = $this->getDelimiterChar($this->config);
    $columns = array();

    foreach (array_merge($uniques, $sources) as $col) {
      if (strpos($col, $sep) !== FALSE) {
        $col = '"' . str_replace('"', '""', $col) . '"';
      }

      $columns[] = mb_convert_encoding($col, $encoding, 'utf-8');
    }

    $template_file_details = $this->getTemplateFileDetails($this->config);

    $filename = "{$this->id}_template.{$template_file_details['extension']}";
    $cache_control = 'max-age=60, must-revalidate';
    $content_disposition = 'attachment; filename="' . $filename . '"';
    $content_type = "{$template_file_details['mime_type']}; charset=utf-8";

    drupal_add_http_header('Cache-Control', $cache_control);
    drupal_add_http_header('Content-Disposition', $content_disposition);
    drupal_add_http_header('Content-type', $content_type);

    print implode($sep, $columns);
  }
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

aman1792 created an issue. See original summary.

Demidov Sergey’s picture

FileSize
11.65 KB
Demidov Sergey’s picture

Status: Fixed » Active
MegaChriz’s picture

Title: Not correctly unloaded headlines » Support non-UTF-8 column headers in CSV template
Status: Active » Needs work

Hi, can you post your changes in the form of a patch? This makes it more clear at what exactly you changed in plugins/FeedsCSVParser.inc. Also, the file could get changed by other issues and then overwriting plugins/FeedsCSVParser.inc with your file would undo these changes. Also, when posting a patch and set the issue status to "Needs review", automated tests are ran against your code, which could catch bugs.

Glancing over the code posted in the issue summary, I see you are using the function mb_convert_encoding(). This function requires the mbstring extension, which may not be available.
From http://php.net/manual/en/mbstring.installation.php:

mbstring is a non-default extension. This means it is not enabled by default. You must explicitly enable the module with the configure option.

You should therefore check if the extension is available before calling a function from it:

if (extension_loaded('mbstring')) {
  // (your code here)
}
Demidov Sergey’s picture

Sorry, first time doing an issue on d.org here fixed version.

Demidov Sergey’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
Demidov Sergey’s picture

Demidov Sergey’s picture

Status: Needs work » Needs review
MegaChriz’s picture

Thanks for posting the changes as a patch!

+++ b/plugins/FeedsCSVParser.inc
@@ -286,7 +287,12 @@ class FeedsCSVParser extends FeedsParser {
+      if (extension_loaded('mbstring')) {
+        $columns[] = mb_convert_encoding($col, $encoding, 'utf-8');
+      }

You could add a check here to see if $encoding is already 'utf-8'. Because if so, there is no need to convert it.

As there was no test coverage yet for the CSV template, I've added some in #2771803: Tests for the CSV template. I've let the testbot test your patch again with these tests included.

Demidov Sergey’s picture