Projet

Général

Profil

Révision a192dc0b

Ajouté par Assos Assos il y a environ 8 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/feeds/libraries/ParserCSV.inc
66 66
 */
67 67
class ParserCSV {
68 68
  private $delimiter;
69
  private $fromEncoding;
70
  private $toEncoding;
69 71
  private $skipFirstLine;
70 72
  private $columnNames;
71 73
  private $timeout;
......
73 75
  private $startByte;
74 76
  private $lineLimit;
75 77
  private $lastLinePos;
78
  private $useMbString;
76 79

  
77 80
  public function __construct() {
78 81
    $this->delimiter = ',';
82
    $this->fromEncoding = 'UTF-8';
83
    $this->toEncoding = 'UTF-8';
79 84
    $this->skipFirstLine = FALSE;
80 85
    $this->columnNames = FALSE;
81 86
    $this->timeout = FALSE;
......
84 89
    $this->lineLimit = 0;
85 90
    $this->lastLinePos = 0;
86 91
    ini_set('auto_detect_line_endings', TRUE);
92
    if (extension_loaded('mbstring') && variable_get('feeds_use_mbstring', TRUE)) {
93
      $this->useMbString = TRUE;
94
    }
87 95
  }
88 96

  
89 97
  /**
......
94 102
    $this->delimiter = $delimiter;
95 103
  }
96 104

  
105
  /**
106
   * Sets the source file encoding.
107
   *
108
   * By default, the encoding is UTF-8.
109
   *
110
   * @param string $encoding
111
   *   The encoding to set.
112
   */
113
  public function setEncoding($encoding) {
114
    $this->fromEncoding = $encoding;
115
  }
116

  
97 117
  /**
98 118
   * Set this to TRUE if the parser should skip the first line of the CSV text,
99 119
   * which might be desired if the first line contains the column names.
......
197 217
    for ($lineIterator->rewind($this->startByte); $lineIterator->valid(); $lineIterator->next()) {
198 218

  
199 219
      // Make really sure we've got lines without trailing newlines.
200
      $line = trim($lineIterator->current(), "\r\n");
220
      $line = trim($this->fixEncoding($lineIterator->current()), "\r\n");
201 221

  
202 222
      // Skip empty lines.
203 223
      if (empty($line)) {
......
237 257
            }
238 258
            // Ok, so, on with fetching the next line, as mentioned above.
239 259
            $currentField .= "\n";
240
            $line = trim($lineIterator->current(), "\r\n");
260
            $line = trim($this->fixEncoding($lineIterator->current()), "\r\n");
241 261
            $currentIndex = 0;
242 262
            continue;
243 263
          }
......
325 345
    }
326 346
    return $rows;
327 347
  }
348

  
349
  /**
350
   * Converts encoding of input data.
351
   *
352
   * @param string $data
353
   *   A chunk of data.
354
   *
355
   * @return string
356
   *   The encoded data.
357
   *
358
   * @throws ParserCSVEncodingException
359
   *   Thrown when a given encoding does not match.
360
   */
361
  public function fixEncoding($data) {
362
    if ($this->useMbString) {
363
      if (mb_check_encoding($data, $this->fromEncoding)) {
364
        if ($this->toEncoding != $this->fromEncoding) {
365
          // Convert encoding. The conversion is to UTF-8 by default to prevent
366
          // SQL errors.
367
          $data = mb_convert_encoding($data, $this->toEncoding, $this->fromEncoding);
368
        }
369
      }
370
      else {
371
        throw new ParserCSVEncodingException(t('Source file is not in %encoding encoding.', array('%encoding' => $this->fromEncoding)));
372
      }
373
    }
374

  
375
    return $data;
376
  }
328 377
}
378

  
379
/**
380
 * Exception thrown when an encoding error occurs during parsing.
381
 */
382
class ParserCSVEncodingException extends Exception {}

Formats disponibles : Unified diff