]> git.mxchange.org Git - friendica.git/blob - library/simplepie/create.php
Merge pull request #2185 from tobiasd/20151214-translations
[friendica.git] / library / simplepie / create.php
1 <?php
2
3 require_once 'simplepie.inc';
4
5 function normalize_character_set($charset)
6 {
7         return strtolower(preg_replace('/(?:[^a-zA-Z0-9]+|([^0-9])0+)/', '\1', $charset));
8 }
9
10 function build_character_set_list()
11 {
12         $file = new SimplePie_File('http://www.iana.org/assignments/character-sets');
13         if (!$file->success && !($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
14         {
15                 return false;
16         }
17         else
18         {
19                 $data = explode("\n", $file->body);
20                 unset($file);
21                 
22                 foreach ($data as $line)
23                 {
24                         // New character set
25                         if (substr($line, 0, 5) === 'Name:')
26                         {
27                                 // If we already have one, push it on to the array
28                                 if (isset($aliases))
29                                 {
30                                         for ($i = 0, $count = count($aliases); $i < $count; $i++)
31                                         {
32                                                 $aliases[$i] = normalize_character_set($aliases[$i]);
33                                         }
34                                         $charsets[$preferred] = array_unique($aliases);
35                                         natsort($charsets[$preferred]);
36                                 }
37                                 
38                                 $start = 5 + strspn($line, "\x09\x0A\x0B\xC\x0D\x20", 5);
39                                 $chars = strcspn($line, "\x09\x0A\x0B\xC\x0D\x20", $start);
40                                 $aliases = array(substr($line, $start, $chars));
41                                 $preferred = end($aliases);
42                         }
43                         // Another alias
44                         elseif(substr($line, 0, 6) === 'Alias:')
45                         {
46                                 $start = 7 + strspn($line, "\x09\x0A\x0B\xC\x0D\x20", 7);
47                                 $chars = strcspn($line, "\x09\x0A\x0B\xC\x0D\x20", $start);
48                                 $aliases[] = substr($line, $start, $chars);
49                                 
50                                 if (end($aliases) === 'None')
51                                 {
52                                         array_pop($aliases);
53                                 }
54                                 elseif (substr($line, 7 + $chars + 1, 21) === '(preferred MIME name)')
55                                 {
56                                         $preferred = end($aliases);
57                                 }
58                         }
59                 }
60                 
61                 // Compatibility replacements
62                 $compat = array(
63                         'EUC-KR' => 'windows-949',
64                         'GB2312' => 'GBK',
65                         'GB_2312-80' => 'GBK',
66                         'ISO-8859-1' => 'windows-1252',
67                         'ISO-8859-9' => 'windows-1254',
68                         'ISO-8859-11' => 'windows-874',
69                         'KS_C_5601-1987' => 'windows-949',
70                         'TIS-620' => 'windows-874',
71                         //'US-ASCII' => 'windows-1252',
72                         'x-x-big5' => 'Big5',
73                 );
74                 
75                 foreach ($compat as $real => $replace)
76                 {
77                         if (isset($charsets[$real]) && isset($charsets[$replace]))
78                         {
79                                 $charsets[$replace] = array_merge($charsets[$replace], $charsets[$real]);
80                                 unset($charsets[$real]);
81                         }
82                         elseif (isset($charsets[$real]))
83                         {
84                                 $charsets[$replace] = $charsets[$real];
85                                 $charsets[$replace][] = normalize_character_set($replace);
86                                 unset($charsets[$real]);
87                         }
88                         else
89                         {
90                                 $charsets[$replace][] = normalize_character_set($real);
91                         }
92                         $charsets[$replace] = array_unique($charsets[$replace]);
93                         natsort($charsets[$replace]);
94                 }
95                 
96                 // Sort it
97                 uksort($charsets, 'strnatcasecmp');
98                 
99                 // Check that nothing matches more than one
100                 $all = call_user_func_array('array_merge', $charsets);
101                 $all_count = array_count_values($all);
102                 if (max($all_count) > 1)
103                 {
104                         echo "Duplicated charsets:\n";
105                         foreach ($all_count as $charset => $count)
106                         {
107                                 if ($count > 1)
108                                 {
109                                         echo "$charset\n";
110                                 }
111                         }
112                 }
113                 
114                 // And we're done!
115                 return $charsets;
116         }
117 }
118
119 function charset($charset)
120 {
121         $normalized_charset = normalize_character_set($charset);
122         if ($charsets = build_character_set_list())
123         {
124                 foreach ($charsets as $preferred => $aliases)
125                 {
126                         if (in_array($normalized_charset, $aliases))
127                         {
128                                 return $preferred;
129                         }
130                 }
131                 return $charset;
132         }
133         else
134         {
135                 return false;
136         }
137 }
138
139 function build_function()
140 {
141         if ($charsets = build_character_set_list())
142         {
143                 $return = <<<EOF
144 function charset(\$charset)
145 {
146         // Normalization from UTS #22
147         switch (strtolower(preg_replace('/(?:[^a-zA-Z0-9]+|([^0-9])0+)/', '\\1', \$charset)))
148         {
149
150 EOF;
151                 foreach ($charsets as $preferred => $aliases)
152                 {
153                         foreach ($aliases as $alias)
154                         {
155                                 $return .= "\t\tcase " . var_export($alias, true) . ":\n";
156                         }
157                         $return .= "\t\t\treturn " . var_export($preferred, true) . ";\n\n";
158                 }
159                 $return .= <<<EOF
160                 default:
161                         return \$charset;
162         }
163 }
164 EOF;
165                 return $return;
166         }
167         else
168         {
169                 return false;
170         }
171 }
172
173 if (php_sapi_name() === 'cli' && realpath($_SERVER['argv'][0]) === __FILE__)
174 {
175         echo build_function();
176 }
177
178 ?>