transliteration_data.php.txt 21.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
<?php

/**
 * @file
 * Unifies formats of transliteration data from various sources.
 *
 * A few notes about this script:
 * - The functions in this file are NOT SECURE, because they use PHP functions
 *   like eval(). Absolutely do not run this script unless you trust the data
 *   files used for input.
 * - You will need to change the name of this file to remove the .txt extension
 *   before running it (it has been given this name so that you cannot run it
 *   by mistake). When you do that, move it out of your web root as well so
 *   that it cannot be run via a URL, and run the script via the PHP command
 *   at a command prompt.
 * - This script, depending on which portions of it you run, depends on having
 *   input data from various sources in sub-directories below where this file
 *   is located. The data inputs are as follows:
 *   - Existing Drupal Core transliteration data: Sub-directory 'data'; comes
 *     from core/lib/Drupal/Component/Transliteration/data
 *   - Midgardmvc data: Sub-directory 'utf8_to_ascii_db'; download from
 *     https://github.com/bergie/midgardmvc_helper_urlize/downloads
 *   - CPAN Text-Unidecode data: Sub-directory 'Unidecode'; download from
 *     http://search.cpan.org/~sburke/Text-Unidecode-0.04/lib/Text/Unidecode.pm
 *   - Node.js project: Sub-directory 'unidecoder_data'; download from
 *     https://github.com/bitwalker/stringex/downloads
 *   - JUnidecode project: Sub-directory 'junidecode'; download source from
 *     http://www.ippatsuman.com/projects/junidecode/index.html
 * - You will also need to make directory 'outdata' to hold output.
 * - If you plan to use the 'intl' data, you will also need to have the PECL
 *   packages 'yaml' and 'intl' installed.  See
 *   http://php.net/manual/install.pecl.downloads.php for generic PECL
 *   package installation instructions. The following commands on Ubuntu Linux
 *   will install yaml and intl packages:
 *   @code
 *   sudo apt-get install libyaml-dev
 *   sudo pecl install yaml
 *   sudo apt-get install php5-intl
 *   sudo apt-get install libicu-dev
 *   sudo pecl install intl
 *   @endcode
 *   After running these commands, you will need to make sure
 *   'extension=intl.so' and 'extension=yaml.so' are added to the php.ini file
 *   that is in use for the PHP command-line command.
 * - When you have collected all of the data and installed the required
 *   packages, you will need to find the specific commands below that you want
 *   to use and un-comment them. The preferred data source for Drupal Core is
 *   the PECL 'intl' package, and the line that needs to be un-commented in
 *   order to make a Drupal Core patch is:
 *   @code
 *   patch_drupal('outdata');
 *   @endcode
 * - The functions are documented in more detail in their headers where they
 *   are defined. Many have parameters that you can use to change the output.
 */

// Commands to read various data sources:
// $data = read_drupal_data();
// $data = read_midgard_data();
// $data = read_cpan_data();
// $data = read_nodejs_data();
// $data = read_intl_data();
// $data = read_junidecode_data();

// After running a read_*_data() function, you can print out the data
// (it will make a LOT of output):
// print_r($data);

// Command to read in all of data sources and output in CSV format, explaining
// the differences:
// read_all_to_csv();

// Command to patch Drupal Core data, using the intl data set, and put the
// resulting changed data files in the 'outdata' directory:
patch_drupal('outdata');

/**
 * Reads in all transliteration data and outputs differences in CSV format.
 *
 * Each data set is compared to the Drupal Core reference data set, and the
 * differences are noted. The data must be in the locations noted in the
 * file header above. The CSV output has several columns. The first one is the
 * Unicode character code. The next columns contain the transliteration of
 * that character in each of the data sets. The last column, tells what the
 * differences are between the Drupal Core reference set and the other data
 * sets:
 * - missing: The target set is missing data that the Drupal set has.
 * - provided: The target set has provided data that Drupal does not have.
 * - case: The target and Drupal set output differ only in upper/lower case.
 * - different: The target and Drupal set output differ in more than just case.
 *
 * @param bool $print_all
 *   TRUE to print all data; FALSE (default) to print just data where there
 *   are differences between the Drupal set and other data sources.
 * @param bool $print_missing
 *   TRUE to print cases where one of the non-Drupal sets is missing information
 *   and that is the only difference; FALSE (default) to include these rows.
 */
function read_all_to_csv($print_all = FALSE, $print_missing = FALSE) {
  $data = array();
  $types = array('drupal', 'midgard', 'cpan', 'nodejs', 'junidecode', 'intl');

  // Alternatively, if you just want to compare a couple of data sets, you can
  // uncomment and edit the following line:
  // $types = array('drupal', 'intl');

  // Read in all the data.
  foreach ($types as $type) {
    $data[$type] = call_user_func('read_' . $type . '_data');
  }

  // Print CSV header row.
  print "character,";
  print implode(',', $types);
  print ",why\n";

  // Go through all the banks of character data.
  for ($bank = 0; $bank < 256; $bank++) {

    // Go through characters in bank; skip pure ASCII characters.
    $start = ($bank == 0) ? 0x80 : 0;
    for ($chr = $start; $chr < 256; $chr++) {

      // Gather the data together for this character.
      $row = array();
      foreach ($types as $type) {
        $row[$type] = (isset($data[$type][$bank][$chr]) && is_string($data[$type][$bank][$chr])) ? $data[$type][$bank][$chr] : '';
      }

      // Only print if there are differences or we are printing all data.
      $print = $print_all;
      $ref = $row['drupal'];
      $why = array();
      foreach ($types as $type) {
        // Try to characterize what the differences are.
        if ($row[$type] != $ref) {
          if ($row[$type] == '') {
            $why['missing'] = 'missing';
            if ($print_missing) {
              $print = TRUE;
            }
          }
          elseif ($ref == '') {
            $why['provided'] = 'provided';
            $print = TRUE;
          }
          elseif ($row[$type] == strtolower($ref) || $row[$type] == strtoupper($ref)) {
            $why['case'] = 'case';
            $print = TRUE;
          }
          else {
            $why['different'] = 'different';
            $print = TRUE;
          }
        }
      }

      // Print the data line.
      if ($print) {
        print '0x' . sprintf('%04x', 256 * $bank + $chr) . ',';
        foreach ($row as $out) {
          print '"' . addcslashes($out, '"') . '", ';
        }
        print implode(':', $why);
        print "\n";
      }
    }
  }
}

/**
 * Reads in 'intl' transliteration data and writes out changed Drupal files.
 *
 * Writes out the Drupal data files that would have to change to make our data
 * match the intl data set.
 *
 * @param string $outdir
 *   Directory to put the patched data files in (under where the script is
 *   being run).
 */
function patch_drupal($outdir) {
  $data = array();

  // Note that this is hard-wired below. Changing this line will have no
  // effect except to break this function.
  $types = array('drupal', 'intl');

  // Read in all the data.
  foreach ($types as $type) {
    $data[$type] = call_user_func('read_' . $type . '_data');
  }

  // Go through all the banks of character data.
  for ($bank = 0; $bank < 256; $bank++) {
    $print_bank = FALSE;

    // Go through characters in bank; skip pure ASCII characters.
    $start = ($bank == 0) ? 0x80 : 0;
    $newdata = array();
    for ($chr = 0; $chr < 256; $chr++) {
      // Fill up the start of the ASCII range.
      if ($chr < $start) {
        $newdata[$chr] = chr($chr);
        continue;
      }

      // Figure out what characters we actually have.
      $drupal = isset($data['drupal'][$bank][$chr]) ? $data['drupal'][$bank][$chr] : NULL;
      // Note that for intl, we only want to keep the transliteration if it
      // has something other than '' in it.
      $intl = isset($data['intl'][$bank][$chr]) && $data['intl'][$bank][$chr] != '' ? $data['intl'][$bank][$chr] : NULL;
      // Make sure we have something in the Drupal data set, in case we need
      // to print.
      $newdata[$chr] = $drupal;

      if (!isset($intl)) {
        continue;
      }
      if (!isset($drupal) || $drupal != $intl) {
        $print_bank = TRUE;
        $newdata[$chr] = $intl;
      }
    }

    // If we found a difference, output a data file.
    if ($print_bank) {
      write_data_file($newdata, $bank, $outdir);
    }
  }
}

/**
 * Reads in the Drupal Core generic transliteration data set.
 *
 * The data is expected to be in files xNN.php in directory 'data' under
 * this file's directory.
 *
 * @return array
 *   Nested array of transliteration data. Outer keys are the first two
 *   bytes of Unicode characters (or 0 for base ASCII characters). The next
 *   level is the other two bytes, and the values are the transliterations.
 *
243
 * @see PhpTransliteration::readGenericData()
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
 */
function read_drupal_data() {
  $dir = __DIR__ . '/data';
  $out = array();

  // Read data files.
  for ($bank = 0; $bank < 256; $bank++) {
    $base = array();
    $file = $dir . '/x' . sprintf('%02x', $bank) . '.php';
    if (is_file($file)) {
      include($file);
    }
    $out[$bank] = $base;
  }

  return $out;
}

/**
 * Reads in the MidgardMVC transliteration data.
 *
 * The data is expected to be in files xNN.php in directory utf8_to_ascii_db
 * under the directory where this file resides. It can be downloaded from
 * https://github.com/bergie/midgardmvc_helper_urlize/downloads.
 *
 * @return array
 *   Nested array of transliteration data. Outer keys are the first two
 *   bytes of Unicode characters (or 0 for base ASCII characters). The next
 *   level is the other two bytes, and the values are the transliterations.
 */
function read_midgard_data() {
  $dir = __DIR__ . '/utf8_to_ascii_db';
  $out = array();

  // Read data files.
  for ($bank = 0; $bank < 256; $bank++) {
    $UTF8_TO_ASCII = array($bank => array());
    $file = $dir . '/x' . sprintf('%02x', $bank) . '.php';
    if (is_file($file)) {
      include($file);
    }
    $base = $UTF8_TO_ASCII[$bank];

    // For unknown characters, these files have '[?]' in them. Replace with
    // NULL for compatibility with our data.
    $base = array_map('_replace_question_with_null', $base);
    $out[$bank] = $base;
  }

  return $out;
}

/**
 * Reads in the CPAN Text::Unidecode data set.
 *
 * The data is expected to be in files xNN.pm in directory 'Unidecode' under
 * this file's directory. It can be downloaded from
 * http://search.cpan.org/~sburke/Text-Unidecode-0.04/lib/Text/Unidecode.pm.
 *
 * @return array
 *   Nested array of transliteration data. Outer keys are the first two
 *   bytes of Unicode characters (or 0 for base ASCII characters). The next
 *   level is the other two bytes, and the values are the transliterations.
 */
function read_cpan_data() {
  $dir = __DIR__ . '/Unidecode';
  $out = array();

  // Read data files.
  for ($bank = 0; $bank < 256; $bank++) {
    $base = array();
    $file = $dir . '/x' . sprintf('%02x', $bank) . '.pm';
    if (is_file($file)) {
      $base = _cpan_read_file($file);
    }
    $out[$bank] = $base;
  }

  return $out;
}

/**
 * Reads in the data in a single file from the Text::Unidecode CPAN project.
 *
 * @param string $file
 *   File to read from.
 *
 * @return array
 *   Data read from the file.
 *
 * @see read_cpan_data()
 */
function _cpan_read_file($file) {

  $contents = file($file);
  $save = '';
  foreach ($contents as $line) {
    // Discard lines starting with # or $. The first line seems to have a
    // comment starting with #, the second has a Perl line like
    // $Text::Unidecode::Char[0x04] = [, -- and we do not want either.
    if (preg_match('|^\s*[#\$]|', $line)) {
      continue;
    }

    // Discard lines ending with semi-colons, which we also don't want
    // (there seem to be two of these lines at the end of the files).
    if (preg_match('|;\s*$|', $line)) {
      continue;
    }

    // Replace '[?]' with nothing (that means "don't know how to
    // transliterate"). In some files, this is encoded as qq{[?]} or
    // qq{[?] } instead.
    $line = str_replace('qq{[?]}', 'NULL', $line);
    $line = str_replace('qq{[?] }', 'NULL', $line);
    $line = str_replace("'[?]'", 'NULL', $line);

    // Replace qq{} with either "" or '' or nothing, depending on what is
    // inside it.
    $line = str_replace('qq{\{}', "'{'", $line);
    $line = str_replace('qq{\}}', "'}'", $line);
    $line = str_replace('qq{\} }', "'} '", $line);
    $line = str_replace("qq{\\\\}", '"\\\\"', $line);
    $line = str_replace("qq{\\", "qq{'", $line);
    $line = str_replace("qq{\"'}", "\"\\\"'\"", $line);
    $line = preg_replace('|qq\{([^\'\}]+)\}|', "'$1'", $line);
    $line = preg_replace('|qq\{([^\}]+)\}|', '"$1"', $line);

    $save .= $line;
  }

  // Now we should have a string that looks like:
  // 'a', 'b', ...
  // Evaluate as an array.
  $save = 'return array(' . $save . ');';

  $data = @eval($save);
  if (isset($data) && is_array($data)) {
    $data = array_map('_replace_hex_with_character', $data);
  }
  else {
    // There was a problem, so throw an error and exit.
    print "Problem in evaluating $file\n";
    print $save;
    eval($save);
    exit();
  }

  // For unknown characters, these files may still have '[?]' in them. Replace
  // with NULL for compatibility with our data.
  $data = array_map('_replace_question_with_null', $data);

  return $data;
}

/**
 * Reads in the Node.js transliteration data.
 *
 * The data is expected to be in files xNN.yml in directory unidecoder_data
 * under the directory where this file resides. It can be downloaded from
 * https://github.com/bitwalker/stringex/downloads. You also need the PECL
 * 'yaml' extension installed for this function to work.
 *
 * @return array
 *   Nested array of transliteration data. Outer keys are the first two
 *   bytes of Unicode characters (or 0 for base ASCII characters). The next
 *   level is the other two bytes, and the values are the transliterations.
 */
function read_nodejs_data() {
  $dir = __DIR__ . '/unidecoder_data';
  $out = array();

  // Read data files.
  for ($bank = 0; $bank < 256; $bank++) {
    $base = array();
    $file = $dir . '/x' . sprintf('%02x', $bank) . '.yml';
    if (is_file($file)) {
      $base = yaml_parse_file($file);
      // For unknown characters, these files have '[?]' in them. Replace with
      // NULL for compatibility with our data.
      $base = array_map('_replace_question_with_null', $base);
    }
    $out[$bank] = $base;
  }

  return $out;
}

/**
 * Loads the PECL 'intl' Transliterator class's transliteration data.
 *
 * You need to have the PECL 'intl' package installed for this to work.
 *
 * @return array
 *   Nested array of transliteration data. Outer keys are the first two
 *   bytes of Unicode characters (or 0 for base ASCII characters). The next
 *   level is the other two bytes, and the values are the transliterations.
 */
function read_intl_data() {
  // In order to transliterate, you first have to create a transliterator
  // object. This needs a list of transliteration operations. You can get a
  // list of available operations with:
  //   print_r(Transliterator::listIDs()); exit();
  // And a few of these are documented on
  // http://userguide.icu-project.org/transforms/general and
  // http://www.unicode.org/reports/tr15/ (for normalizations).
  // There are also maps to the Unicode characters at:
  //  http://www.unicode.org/roadmaps/bmp/
  //  http://www.unicode.org/charts/nameslist/
  $ops = '';

  // The first step in any transform: separate out accents and remove them.
  $ops .= 'NFD; [:Nonspacing Mark:] Remove; NFC;';

  // Then you need to do a bunch of language-specific or script-specific
  // transliterations. Here is hopefully a representative set. There are
  // quite a few scripts that don't appear to have rules currently, such
  // as Etheopian.
  $ops .= 'Greek-Latin; ';
  $ops .= 'Cyrillic-Latin; ';
  $ops .= 'Armenian-Latin; ';
  $ops .= 'Hebrew-Latin; ';
  $ops .= 'Arabic-Latin; ';
  $ops .= 'Syriac-Latin; ';
  $ops .= 'Thaana-Latin; ';
  $ops .= 'Devanagari-Latin; ';
  $ops .= 'Bengali-Latin; ';
  $ops .= 'Gurmukhi-Latin; ';
  $ops .= 'Gujarati-Latin; ';
  $ops .= 'Oriya-Latin; ';
  $ops .= 'Tamil-Latin; ';
  $ops .= 'Telugu-Latin; ';
  $ops .= 'Kannada-Latin; ';
  $ops .= 'Malayalam-Latin; ';
  $ops .= 'Thai-Latin; ';
  $ops .= 'Georgian-Latin; ';
  $ops .= 'Hangul-Latin; ';
  $ops .= 'Mongolian-Latin/BGN; ';
  $ops .= 'Jamo-Latin; ';
  $ops .= 'Katakana-Latin; ';
  $ops .= 'Any-Latin; ';

  // Finally, after transforming to Latin, transform to ASCII.
  $ops .= 'Latin-ASCII; ';

  // Remove any remaining accents and recompose.
  $ops .= 'NFD; [:Nonspacing Mark:] Remove; NFC;';

  $trans = Transliterator::create($ops);
  $out = array();

  // Transliterate all possible characters.
  for ($bank = 0; $bank < 256; $bank++) {
    $data = array();
    for ($chr = 0; $chr < 256; $chr++) {
      // Skip the UTF-16 and "private use" ranges completely.
      $OK = ($bank <= 0xd8 || $bank > 0xf8);

      $result = $OK ? $trans->transliterate(mb_convert_encoding(pack('n', 256 * $bank + $chr), 'UTF-8', 'UTF-16BE')) : '';

      // See if we have managed to transliterate this to ASCII or not. If not,
      // return NULL instead of this character.
      $max = chr(127);
      foreach (preg_split('//u', $result, 0, PREG_SPLIT_NO_EMPTY) as $character) {
        if ($character > $max) {
          $OK = $OK && FALSE;
          break;
        }
      }
      $data[$chr] = ($OK) ? $result : NULL;
    }
    $out[$bank] = $data;
  }

  return $out;
}

/**
 * Reads in the JUnidecode data set.
 *
 * The data is expected to be in files XNN.java in directory 'junidecode' under
 * this file's directory. It can be downloaded from
 * http://www.ippatsuman.com/projects/junidecode/index.html
 *
 * @return array
 *   Nested array of transliteration data. Outer keys are the first two
 *   bytes of Unicode characters (or 0 for base ASCII characters). The next
 *   level is the other two bytes, and the values are the transliterations.
 */
function read_junidecode_data() {
  $dir = __DIR__ . '/junidecode';
  $out = array();

  // Read data files.
  for ($bank = 0; $bank < 256; $bank++) {
    $base = array();
    $file = $dir . '/X' . sprintf('%02x', $bank) . '.java';
    if (is_file($file)) {
      $base = _junidecode_read_file($file);
    }
    $out[$bank] = $base;
  }

  return $out;
}

/**
 * Reads in the data in a single file from the JUnidecode project.
 *
 * @param string $file
 *   File to read from.
 *
 * @return array
 *   Data read from the file.
 *
 * @see read_junidecode_data()
 */
function _junidecode_read_file($file) {
  $contents = file($file);
  $save = '';
  foreach ($contents as $line) {
    // Discard lines starting with * or / or package or class or public or },
    // to get rid of comments and Java code.
    if (preg_match('|^\s*[\*/\}]|', $line)) {
      continue;
    }
    if (preg_match('/^\s*package|public|class/', $line)) {
      continue;
    }

    // Some of the lines look like this:
    //      new String("" + (char) 0x00), // 0x00
    // Transform to be '0x00,'
    $line = preg_replace('|^\s*new\s+String\s*\(\s*""\s*\+\s*\(char\)\s+0x([0-9]+).*$|', '0x$1,', $line);

    // Strings are in double quotes, yet many have \' in them.
    $line = str_replace("\'", "'", $line);

    // Everything else should probably be OK -- the lines are like:
    //  "Ie", // 0x00
    $save .= $line;
  }

  // Evaluate as an array.
  $save = 'return array(' . $save . ');';

  $data = @eval($save);
  if (isset($data) && is_array($data)) {
    $data = array_map('_replace_hex_with_character', $data);
    $data = array_map('_replace_question_with_null', $data);
  }
  else {
    // There was a problem, so throw an error and exit.
    print "Problem in evaluating $file\n";
    print $save;
    eval($save);
    exit();
  }

  return $data;
}

/**
 * Callback for array_map(): Returns $data, with '[?]' replaced with NULL.
 */
function _replace_question_with_null($data) {
  return ($data == '[?]' || $data == '[?] ') ? NULL : $data;
}

/**
 * Callback for array_map(): Replaces '\xNN' with the actual character.
 */
function _replace_hex_with_character($item) {
  if (strpos($item, '\x') === 0) {
    $item = eval($item);
  }
  return $item;
}

/**
 * Writes a data file out in the standard Drupal Core data format.
 *
 * @param array $data
 *   Array of data to write out.
 * @param string $bank
 *   Bank of characters it belongs to.
 * @param string $dir
 *   Output directory.
 */
function write_data_file($data, $bank, $outdir) {
  $dir = __DIR__ . '/' . $outdir;
  $file = $dir . '/x' . sprintf('%02x', $bank) . '.php';

  $out = '';
638
  $out .= "<?php\n\n/**\n * @file\n * Generic transliteration data for the PhpTransliteration class.\n */\n\n\$base = array(\n";
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664

  // The 00 file skips the ASCII range
  $start = 0;
  if ($bank == 0) {
    $start = 0x80;
    $out .= "  // Note: to save memory plain ASCII mappings have been left out.\n";
  }

  for ($line = $start; $line <= 0xf0; $line += 0x10) {
    $out .= '  0x' . sprintf('%02X', $line) . ' =>';
    $elems = array_values(array_slice($data, $line, 16));
    for ($i = 0; $i < 16; $i++ ) {
      if (isset($elems[$i])) {
        $out .= " '" . addcslashes($elems[$i], "'\\") . "',";
      }
      else {
        $out .= ' NULL,';
      }
    }
    $out .= "\n";
  }

  $out .= ");\n";

  file_put_contents($file, $out);
}