You are here

transliteration_data.php.txt in Drupal 8

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:

  • 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:

  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
  

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:
patch_drupal('outdata');
  • 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.

File

core/scripts/transliteration_data.php.txt
View source
  1. /**
  2. * @file
  3. * Unifies formats of transliteration data from various sources.
  4. *
  5. * A few notes about this script:
  6. * - The functions in this file are NOT SECURE, because they use PHP functions
  7. * like eval(). Absolutely do not run this script unless you trust the data
  8. * files used for input.
  9. * - You will need to change the name of this file to remove the .txt extension
  10. * before running it (it has been given this name so that you cannot run it
  11. * by mistake). When you do that, move it out of your web root as well so
  12. * that it cannot be run via a URL, and run the script via the PHP command
  13. * at a command prompt.
  14. * - This script, depending on which portions of it you run, depends on having
  15. * input data from various sources in sub-directories below where this file
  16. * is located. The data inputs are as follows:
  17. * - Existing Drupal Core transliteration data: Sub-directory 'data'; comes
  18. * from core/lib/Drupal/Component/Transliteration/data
  19. * - Midgardmvc data: Sub-directory 'utf8_to_ascii_db'; download from
  20. * https://github.com/bergie/midgardmvc_helper_urlize/downloads
  21. * - CPAN Text-Unidecode data: Sub-directory 'Unidecode'; download from
  22. * http://search.cpan.org/~sburke/Text-Unidecode-0.04/lib/Text/Unidecode.pm
  23. * - Node.js project: Sub-directory 'unidecoder_data'; download from
  24. * https://github.com/bitwalker/stringex/downloads
  25. * - JUnidecode project: Sub-directory 'junidecode'; download source from
  26. * http://www.ippatsuman.com/projects/junidecode/index.html
  27. * - You will also need to make directory 'outdata' to hold output.
  28. * - If you plan to use the 'intl' data, you will also need to have the PECL
  29. * packages 'yaml' and 'intl' installed. See
  30. * http://php.net/manual/install.pecl.downloads.php for generic PECL
  31. * package installation instructions. The following commands on Ubuntu Linux
  32. * will install yaml and intl packages:
  33. * @code
  34. * sudo apt-get install libyaml-dev
  35. * sudo pecl install yaml
  36. * sudo apt-get install php5-intl
  37. * sudo apt-get install libicu-dev
  38. * sudo pecl install intl
  39. * @endcode
  40. * After running these commands, you will need to make sure
  41. * 'extension=intl.so' and 'extension=yaml.so' are added to the php.ini file
  42. * that is in use for the PHP command-line command.
  43. * - When you have collected all of the data and installed the required
  44. * packages, you will need to find the specific commands below that you want
  45. * to use and un-comment them. The preferred data source for Drupal Core is
  46. * the PECL 'intl' package, and the line that needs to be un-commented in
  47. * order to make a Drupal Core patch is:
  48. * @code
  49. * patch_drupal('outdata');
  50. * @endcode
  51. * - The functions are documented in more detail in their headers where they
  52. * are defined. Many have parameters that you can use to change the output.
  53. */
  54. // Commands to read various data sources:
  55. // $data = read_drupal_data();
  56. // $data = read_midgard_data();
  57. // $data = read_cpan_data();
  58. // $data = read_nodejs_data();
  59. // $data = read_intl_data();
  60. // $data = read_junidecode_data();
  61. // After running a read_*_data() function, you can print out the data
  62. // (it will make a LOT of output):
  63. // print_r($data);
  64. // Command to read in all of data sources and output in CSV format, explaining
  65. // the differences:
  66. // read_all_to_csv();
  67. // Command to patch Drupal Core data, using the intl data set, and put the
  68. // resulting changed data files in the 'outdata' directory:
  69. patch_drupal('outdata');
  70. /**
  71. * Reads in all transliteration data and outputs differences in CSV format.
  72. *
  73. * Each data set is compared to the Drupal Core reference data set, and the
  74. * differences are noted. The data must be in the locations noted in the
  75. * file header above. The CSV output has several columns. The first one is the
  76. * Unicode character code. The next columns contain the transliteration of
  77. * that character in each of the data sets. The last column, tells what the
  78. * differences are between the Drupal Core reference set and the other data
  79. * sets:
  80. * - missing: The target set is missing data that the Drupal set has.
  81. * - provided: The target set has provided data that Drupal does not have.
  82. * - case: The target and Drupal set output differ only in upper/lower case.
  83. * - different: The target and Drupal set output differ in more than just case.
  84. *
  85. * @param bool $print_all
  86. * TRUE to print all data; FALSE (default) to print just data where there
  87. * are differences between the Drupal set and other data sources.
  88. * @param bool $print_missing
  89. * TRUE to print cases where one of the non-Drupal sets is missing information
  90. * and that is the only difference; FALSE (default) to include these rows.
  91. */
  92. function read_all_to_csv($print_all = FALSE, $print_missing = FALSE) {
  93. $data = array();
  94. $types = array('drupal', 'midgard', 'cpan', 'nodejs', 'junidecode', 'intl');
  95. // Alternatively, if you just want to compare a couple of data sets, you can
  96. // uncomment and edit the following line:
  97. // $types = array('drupal', 'intl');
  98. // Read in all the data.
  99. foreach ($types as $type) {
  100. $data[$type] = call_user_func('read_' . $type . '_data');
  101. }
  102. // Print CSV header row.
  103. print "character,";
  104. print implode(',', $types);
  105. print ",why\n";
  106. // Go through all the banks of character data.
  107. for ($bank = 0; $bank < 256; $bank++) {
  108. // Go through characters in bank; skip pure ASCII characters.
  109. $start = ($bank == 0) ? 0x80 : 0;
  110. for ($chr = $start; $chr < 256; $chr++) {
  111. // Gather the data together for this character.
  112. $row = array();
  113. foreach ($types as $type) {
  114. $row[$type] = (isset($data[$type][$bank][$chr]) && is_string($data[$type][$bank][$chr])) ? $data[$type][$bank][$chr] : '';
  115. }
  116. // Only print if there are differences or we are printing all data.
  117. $print = $print_all;
  118. $ref = $row['drupal'];
  119. $why = array();
  120. foreach ($types as $type) {
  121. // Try to characterize what the differences are.
  122. if ($row[$type] != $ref) {
  123. if ($row[$type] == '') {
  124. $why['missing'] = 'missing';
  125. if ($print_missing) {
  126. $print = TRUE;
  127. }
  128. }
  129. elseif ($ref == '') {
  130. $why['provided'] = 'provided';
  131. $print = TRUE;
  132. }
  133. elseif ($row[$type] == strtolower($ref) || $row[$type] == strtoupper($ref)) {
  134. $why['case'] = 'case';
  135. $print = TRUE;
  136. }
  137. else {
  138. $why['different'] = 'different';
  139. $print = TRUE;
  140. }
  141. }
  142. }
  143. // Print the data line.
  144. if ($print) {
  145. print '0x' . sprintf('%04x', 256 * $bank + $chr) . ',';
  146. foreach ($row as $out) {
  147. print '"' . addcslashes($out, '"') . '", ';
  148. }
  149. print implode(':', $why);
  150. print "\n";
  151. }
  152. }
  153. }
  154. }
  155. /**
  156. * Reads in 'intl' transliteration data and writes out changed Drupal files.
  157. *
  158. * Writes out the Drupal data files that would have to change to make our data
  159. * match the intl data set.
  160. *
  161. * @param string $outdir
  162. * Directory to put the patched data files in (under where the script is
  163. * being run).
  164. */
  165. function patch_drupal($outdir) {
  166. $data = array();
  167. // Note that this is hard-wired below. Changing this line will have no
  168. // effect except to break this function.
  169. $types = array('drupal', 'intl');
  170. // Read in all the data.
  171. foreach ($types as $type) {
  172. $data[$type] = call_user_func('read_' . $type . '_data');
  173. }
  174. // Go through all the banks of character data.
  175. for ($bank = 0; $bank < 256; $bank++) {
  176. $print_bank = FALSE;
  177. // Go through characters in bank; skip pure ASCII characters.
  178. $start = ($bank == 0) ? 0x80 : 0;
  179. $newdata = array();
  180. for ($chr = 0; $chr < 256; $chr++) {
  181. // Fill up the start of the ASCII range.
  182. if ($chr < $start) {
  183. $newdata[$chr] = chr($chr);
  184. continue;
  185. }
  186. // Figure out what characters we actually have.
  187. $drupal = isset($data['drupal'][$bank][$chr]) ? $data['drupal'][$bank][$chr] : NULL;
  188. // Note that for intl, we only want to keep the transliteration if it
  189. // has something other than '' in it.
  190. $intl = isset($data['intl'][$bank][$chr]) && $data['intl'][$bank][$chr] != '' ? $data['intl'][$bank][$chr] : NULL;
  191. // Make sure we have something in the Drupal data set, in case we need
  192. // to print.
  193. $newdata[$chr] = $drupal;
  194. if (!isset($intl)) {
  195. continue;
  196. }
  197. if (!isset($drupal) || $drupal != $intl) {
  198. $print_bank = TRUE;
  199. $newdata[$chr] = $intl;
  200. }
  201. }
  202. // If we found a difference, output a data file.
  203. if ($print_bank) {
  204. write_data_file($newdata, $bank, $outdir);
  205. }
  206. }
  207. }
  208. /**
  209. * Reads in the Drupal Core generic transliteration data set.
  210. *
  211. * The data is expected to be in files xNN.php in directory 'data' under
  212. * this file's directory.
  213. *
  214. * @return array
  215. * Nested array of transliteration data. Outer keys are the first two
  216. * bytes of Unicode characters (or 0 for base ASCII characters). The next
  217. * level is the other two bytes, and the values are the transliterations.
  218. *
  219. * @see PhpTransliteration::readGenericData()
  220. */
  221. function read_drupal_data() {
  222. $dir = __DIR__ . '/data';
  223. $out = array();
  224. // Read data files.
  225. for ($bank = 0; $bank < 256; $bank++) {
  226. $base = array();
  227. $file = $dir . '/x' . sprintf('%02x', $bank) . '.php';
  228. if (is_file($file)) {
  229. include($file);
  230. }
  231. $out[$bank] = $base;
  232. }
  233. return $out;
  234. }
  235. /**
  236. * Reads in the MidgardMVC transliteration data.
  237. *
  238. * The data is expected to be in files xNN.php in directory utf8_to_ascii_db
  239. * under the directory where this file resides. It can be downloaded from
  240. * https://github.com/bergie/midgardmvc_helper_urlize/downloads.
  241. *
  242. * @return array
  243. * Nested array of transliteration data. Outer keys are the first two
  244. * bytes of Unicode characters (or 0 for base ASCII characters). The next
  245. * level is the other two bytes, and the values are the transliterations.
  246. */
  247. function read_midgard_data() {
  248. $dir = __DIR__ . '/utf8_to_ascii_db';
  249. $out = array();
  250. // Read data files.
  251. for ($bank = 0; $bank < 256; $bank++) {
  252. $UTF8_TO_ASCII = array($bank => array());
  253. $file = $dir . '/x' . sprintf('%02x', $bank) . '.php';
  254. if (is_file($file)) {
  255. include($file);
  256. }
  257. $base = $UTF8_TO_ASCII[$bank];
  258. // For unknown characters, these files have '[?]' in them. Replace with
  259. // NULL for compatibility with our data.
  260. $base = array_map('_replace_question_with_null', $base);
  261. $out[$bank] = $base;
  262. }
  263. return $out;
  264. }
  265. /**
  266. * Reads in the CPAN Text::Unidecode data set.
  267. *
  268. * The data is expected to be in files xNN.pm in directory 'Unidecode' under
  269. * this file's directory. It can be downloaded from
  270. * http://search.cpan.org/~sburke/Text-Unidecode-0.04/lib/Text/Unidecode.pm.
  271. *
  272. * @return array
  273. * Nested array of transliteration data. Outer keys are the first two
  274. * bytes of Unicode characters (or 0 for base ASCII characters). The next
  275. * level is the other two bytes, and the values are the transliterations.
  276. */
  277. function read_cpan_data() {
  278. $dir = __DIR__ . '/Unidecode';
  279. $out = array();
  280. // Read data files.
  281. for ($bank = 0; $bank < 256; $bank++) {
  282. $base = array();
  283. $file = $dir . '/x' . sprintf('%02x', $bank) . '.pm';
  284. if (is_file($file)) {
  285. $base = _cpan_read_file($file);
  286. }
  287. $out[$bank] = $base;
  288. }
  289. return $out;
  290. }
  291. /**
  292. * Reads in the data in a single file from the Text::Unidecode CPAN project.
  293. *
  294. * @param string $file
  295. * File to read from.
  296. *
  297. * @return array
  298. * Data read from the file.
  299. *
  300. * @see read_cpan_data()
  301. */
  302. function _cpan_read_file($file) {
  303. $contents = file($file);
  304. $save = '';
  305. foreach ($contents as $line) {
  306. // Discard lines starting with # or $. The first line seems to have a
  307. // comment starting with #, the second has a Perl line like
  308. // $Text::Unidecode::Char[0x04] = [, -- and we do not want either.
  309. if (preg_match('|^\s*[#\$]|', $line)) {
  310. continue;
  311. }
  312. // Discard lines ending with semi-colons, which we also don't want
  313. // (there seem to be two of these lines at the end of the files).
  314. if (preg_match('|;\s*$|', $line)) {
  315. continue;
  316. }
  317. // Replace '[?]' with nothing (that means "don't know how to
  318. // transliterate"). In some files, this is encoded as qq{[?]} or
  319. // qq{[?] } instead.
  320. $line = str_replace('qq{[?]}', 'NULL', $line);
  321. $line = str_replace('qq{[?] }', 'NULL', $line);
  322. $line = str_replace("'[?]'", 'NULL', $line);
  323. // Replace qq{} with either "" or '' or nothing, depending on what is
  324. // inside it.
  325. $line = str_replace('qq{\{}', "'{'", $line);
  326. $line = str_replace('qq{\}}', "'}'", $line);
  327. $line = str_replace('qq{\} }', "'} '", $line);
  328. $line = str_replace("qq{\\\\}", '"\\\\"', $line);
  329. $line = str_replace("qq{\\", "qq{'", $line);
  330. $line = str_replace("qq{\"'}", "\"\\\"'\"", $line);
  331. $line = preg_replace('|qq\{([^\'\}]+)\}|', "'$1'", $line);
  332. $line = preg_replace('|qq\{([^\}]+)\}|', '"$1"', $line);
  333. $save .= $line;
  334. }
  335. // Now we should have a string that looks like:
  336. // 'a', 'b', ...
  337. // Evaluate as an array.
  338. $save = 'return array(' . $save . ');';
  339. $data = @eval($save);
  340. if (isset($data) && is_array($data)) {
  341. $data = array_map('_replace_hex_with_character', $data);
  342. }
  343. else {
  344. // There was a problem, so throw an error and exit.
  345. print "Problem in evaluating $file\n";
  346. print $save;
  347. eval($save);
  348. exit();
  349. }
  350. // For unknown characters, these files may still have '[?]' in them. Replace
  351. // with NULL for compatibility with our data.
  352. $data = array_map('_replace_question_with_null', $data);
  353. return $data;
  354. }
  355. /**
  356. * Reads in the Node.js transliteration data.
  357. *
  358. * The data is expected to be in files xNN.yml in directory unidecoder_data
  359. * under the directory where this file resides. It can be downloaded from
  360. * https://github.com/bitwalker/stringex/downloads. You also need the PECL
  361. * 'yaml' extension installed for this function to work.
  362. *
  363. * @return array
  364. * Nested array of transliteration data. Outer keys are the first two
  365. * bytes of Unicode characters (or 0 for base ASCII characters). The next
  366. * level is the other two bytes, and the values are the transliterations.
  367. */
  368. function read_nodejs_data() {
  369. $dir = __DIR__ . '/unidecoder_data';
  370. $out = array();
  371. // Read data files.
  372. for ($bank = 0; $bank < 256; $bank++) {
  373. $base = array();
  374. $file = $dir . '/x' . sprintf('%02x', $bank) . '.yml';
  375. if (is_file($file)) {
  376. $base = yaml_parse_file($file);
  377. // For unknown characters, these files have '[?]' in them. Replace with
  378. // NULL for compatibility with our data.
  379. $base = array_map('_replace_question_with_null', $base);
  380. }
  381. $out[$bank] = $base;
  382. }
  383. return $out;
  384. }
  385. /**
  386. * Loads the PECL 'intl' Transliterator class's transliteration data.
  387. *
  388. * You need to have the PECL 'intl' package installed for this to work.
  389. *
  390. * @return array
  391. * Nested array of transliteration data. Outer keys are the first two
  392. * bytes of Unicode characters (or 0 for base ASCII characters). The next
  393. * level is the other two bytes, and the values are the transliterations.
  394. */
  395. function read_intl_data() {
  396. // In order to transliterate, you first have to create a transliterator
  397. // object. This needs a list of transliteration operations. You can get a
  398. // list of available operations with:
  399. // print_r(Transliterator::listIDs()); exit();
  400. // And a few of these are documented on
  401. // http://userguide.icu-project.org/transforms/general and
  402. // http://www.unicode.org/reports/tr15/ (for normalizations).
  403. // There are also maps to the Unicode characters at:
  404. // http://www.unicode.org/roadmaps/bmp/
  405. // http://www.unicode.org/charts/nameslist/
  406. $ops = '';
  407. // The first step in any transform: separate out accents and remove them.
  408. $ops .= 'NFD; [:Nonspacing Mark:] Remove; NFC;';
  409. // Then you need to do a bunch of language-specific or script-specific
  410. // transliterations. Here is hopefully a representative set. There are
  411. // quite a few scripts that don't appear to have rules currently, such
  412. // as Etheopian.
  413. $ops .= 'Greek-Latin; ';
  414. $ops .= 'Cyrillic-Latin; ';
  415. $ops .= 'Armenian-Latin; ';
  416. $ops .= 'Hebrew-Latin; ';
  417. $ops .= 'Arabic-Latin; ';
  418. $ops .= 'Syriac-Latin; ';
  419. $ops .= 'Thaana-Latin; ';
  420. $ops .= 'Devanagari-Latin; ';
  421. $ops .= 'Bengali-Latin; ';
  422. $ops .= 'Gurmukhi-Latin; ';
  423. $ops .= 'Gujarati-Latin; ';
  424. $ops .= 'Oriya-Latin; ';
  425. $ops .= 'Tamil-Latin; ';
  426. $ops .= 'Telugu-Latin; ';
  427. $ops .= 'Kannada-Latin; ';
  428. $ops .= 'Malayalam-Latin; ';
  429. $ops .= 'Thai-Latin; ';
  430. $ops .= 'Georgian-Latin; ';
  431. $ops .= 'Hangul-Latin; ';
  432. $ops .= 'Mongolian-Latin/BGN; ';
  433. $ops .= 'Jamo-Latin; ';
  434. $ops .= 'Katakana-Latin; ';
  435. $ops .= 'Any-Latin; ';
  436. // Finally, after transforming to Latin, transform to ASCII.
  437. $ops .= 'Latin-ASCII; ';
  438. // Remove any remaining accents and recompose.
  439. $ops .= 'NFD; [:Nonspacing Mark:] Remove; NFC;';
  440. $trans = Transliterator::create($ops);
  441. $out = array();
  442. // Transliterate all possible characters.
  443. for ($bank = 0; $bank < 256; $bank++) {
  444. $data = array();
  445. for ($chr = 0; $chr < 256; $chr++) {
  446. // Skip the UTF-16 and "private use" ranges completely.
  447. $OK = ($bank <= 0xd8 || $bank > 0xf8);
  448. $result = $OK ? $trans->transliterate(mb_convert_encoding(pack('n', 256 * $bank + $chr), 'UTF-8', 'UTF-16BE')) : '';
  449. // See if we have managed to transliterate this to ASCII or not. If not,
  450. // return NULL instead of this character.
  451. $max = chr(127);
  452. foreach (preg_split('//u', $result, 0, PREG_SPLIT_NO_EMPTY) as $character) {
  453. if ($character > $max) {
  454. $OK = $OK && FALSE;
  455. break;
  456. }
  457. }
  458. $data[$chr] = ($OK) ? $result : NULL;
  459. }
  460. $out[$bank] = $data;
  461. }
  462. return $out;
  463. }
  464. /**
  465. * Reads in the JUnidecode data set.
  466. *
  467. * The data is expected to be in files XNN.java in directory 'junidecode' under
  468. * this file's directory. It can be downloaded from
  469. * http://www.ippatsuman.com/projects/junidecode/index.html
  470. *
  471. * @return array
  472. * Nested array of transliteration data. Outer keys are the first two
  473. * bytes of Unicode characters (or 0 for base ASCII characters). The next
  474. * level is the other two bytes, and the values are the transliterations.
  475. */
  476. function read_junidecode_data() {
  477. $dir = __DIR__ . '/junidecode';
  478. $out = array();
  479. // Read data files.
  480. for ($bank = 0; $bank < 256; $bank++) {
  481. $base = array();
  482. $file = $dir . '/X' . sprintf('%02x', $bank) . '.java';
  483. if (is_file($file)) {
  484. $base = _junidecode_read_file($file);
  485. }
  486. $out[$bank] = $base;
  487. }
  488. return $out;
  489. }
  490. /**
  491. * Reads in the data in a single file from the JUnidecode project.
  492. *
  493. * @param string $file
  494. * File to read from.
  495. *
  496. * @return array
  497. * Data read from the file.
  498. *
  499. * @see read_junidecode_data()
  500. */
  501. function _junidecode_read_file($file) {
  502. $contents = file($file);
  503. $save = '';
  504. foreach ($contents as $line) {
  505. // Discard lines starting with * or / or package or class or public or },
  506. // to get rid of comments and Java code.
  507. if (preg_match('|^\s*[\*/\}]|', $line)) {
  508. continue;
  509. }
  510. if (preg_match('/^\s*package|public|class/', $line)) {
  511. continue;
  512. }
  513. // Some of the lines look like this:
  514. // new String("" + (char) 0x00), // 0x00
  515. // Transform to be '0x00,'
  516. $line = preg_replace('|^\s*new\s+String\s*\(\s*""\s*\+\s*\(char\)\s+0x([0-9]+).*$|', '0x$1,', $line);
  517. // Strings are in double quotes, yet many have \' in them.
  518. $line = str_replace("\'", "'", $line);
  519. // Everything else should probably be OK -- the lines are like:
  520. // "Ie", // 0x00
  521. $save .= $line;
  522. }
  523. // Evaluate as an array.
  524. $save = 'return array(' . $save . ');';
  525. $data = @eval($save);
  526. if (isset($data) && is_array($data)) {
  527. $data = array_map('_replace_hex_with_character', $data);
  528. $data = array_map('_replace_question_with_null', $data);
  529. }
  530. else {
  531. // There was a problem, so throw an error and exit.
  532. print "Problem in evaluating $file\n";
  533. print $save;
  534. eval($save);
  535. exit();
  536. }
  537. return $data;
  538. }
  539. /**
  540. * Callback for array_map(): Returns $data, with '[?]' replaced with NULL.
  541. */
  542. function _replace_question_with_null($data) {
  543. return ($data == '[?]' || $data == '[?] ') ? NULL : $data;
  544. }
  545. /**
  546. * Callback for array_map(): Replaces '\xNN' with the actual character.
  547. */
  548. function _replace_hex_with_character($item) {
  549. if (strpos($item, '\x') === 0) {
  550. $item = eval($item);
  551. }
  552. return $item;
  553. }
  554. /**
  555. * Writes a data file out in the standard Drupal Core data format.
  556. *
  557. * @param array $data
  558. * Array of data to write out.
  559. * @param string $bank
  560. * Bank of characters it belongs to.
  561. * @param string $dir
  562. * Output directory.
  563. */
  564. function write_data_file($data, $bank, $outdir) {
  565. $dir = __DIR__ . '/' . $outdir;
  566. $file = $dir . '/x' . sprintf('%02x', $bank) . '.php';
  567. $out = '';
  568. $out .= "
  569. // The 00 file skips the ASCII range
  570. $start = 0;
  571. if ($bank == 0) {
  572. $start = 0x80;
  573. $out .= " // Note: to save memory plain ASCII mappings have been left out.\n";
  574. }
  575. for ($line = $start; $line <= 0xf0; $line += 0x10) {
  576. $out .= ' 0x' . sprintf('%02X', $line) . ' =>';
  577. $elems = array_values(array_slice($data, $line, 16));
  578. for ($i = 0; $i < 16; $i++ ) {
  579. if (isset($elems[$i])) {
  580. $out .= " '" . addcslashes($elems[$i], "'\\") . "',";
  581. }
  582. else {
  583. $out .= ' NULL,';
  584. }
  585. }
  586. $out .= "\n";
  587. }
  588. $out .= ");\n";
  589. file_put_contents($file, $out);
  590. }