]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/update_po_templates.php
Merge branch '0.9.x' of git@gitorious.org:statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / scripts / update_po_templates.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - the distributed open-source microblogging tool
5  * Copyright (C) 2008, 2009, StatusNet, Inc.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 // Abort if called from a web server
22 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
23     print "This script must be run from the command line\n";
24     exit();
25 }
26
27 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
28
29 function update_core($dir, $domain)
30 {
31     $old = getcwd();
32     chdir($dir);
33     passthru(<<<END
34 xgettext \
35     --from-code=UTF-8 \
36     --default-domain=$domain \
37     --output=locale/$domain.po \
38     --language=PHP \
39     --keyword="_m:1" \
40     --keyword="pgettext:1c,2" \
41     --keyword="npgettext:1c,2,3" \
42     actions/*.php \
43     classes/*.php \
44     lib/*.php \
45     scripts/*.php
46 END
47 );
48     chdir($old);
49 }
50
51 function do_update_plugin($dir, $domain)
52 {
53     $old = getcwd();
54     chdir($dir);
55     if (!file_exists('locale')) {
56         mkdir('locale');
57     }
58     $files = get_plugin_sources(".");
59     $cmd = <<<END
60 xgettext \
61     --from-code=UTF-8 \
62     --default-domain=$domain \
63     --output=locale/$domain.po \
64     --language=PHP \
65     --keyword='' \
66     --keyword="_m:1" \
67
68 END;
69     foreach ($files as $file) {
70       $cmd .= ' ' . escapeshellarg($file);
71     }
72     passthru($cmd);
73     chdir($old);
74 }
75
76 function do_translatewiki_plugin($basedir, $plugin)
77 {
78     $yamldir = "$basedir/locale/TranslateWiki";
79     if (!file_exists($yamldir)) {
80         mkdir($yamldir);
81     }
82     $outfile = "$yamldir/StatusNet-{$plugin}.yml";
83     $pluginlc = strtolower( $plugin );
84     $data = <<<END
85 ---
86 BASIC:
87   id: out-statusnet-{$pluginlc}
88   label: StatusNet - {$plugin}
89   description: "{{int:bw-desc-statusnet-plugin-{$pluginlc}}}"
90   namespace: NS_STATUSNET
91   display: out/statusnet/{$pluginlc}
92   class: GettextMessageGroup
93
94 FILES:
95   class: GettextFFS
96   sourcePattern: %GROUPROOT%/plugins/{$plugin}/locale/%CODE%/LC_MESSAGES/{$plugin}.po
97   targetPattern: plugins/{$plugin}/locale/%CODE%/LC_MESSAGES/{$plugin}.po
98   codeMap:
99     en-gb: en_GB
100     no: nb
101     pt-br: pt_BR
102     zh-hans: zh_CN
103     zh-hant: zh_TW
104
105 MANGLER
106   class: StringMatcher
107   prefix: {$pluginlc}-
108   patterns:
109     - "*"
110
111 END;
112     file_put_contents($outfile, $data);
113 }
114
115 function get_plugins($dir)
116 {
117     $plugins = array();
118     $dirs = new DirectoryIterator("$dir/plugins");
119     foreach ($dirs as $item) {
120         if ($item->isDir() && !$item->isDot()) {
121             $name = $item->getBasename();
122             if (file_exists("$dir/plugins/$name/{$name}Plugin.php")) {
123                 $plugins[] = $name;
124             }
125         }
126     }
127     return $plugins;
128 }
129
130 function get_plugin_sources($dir)
131 {
132     $files = array();
133
134     $dirs = new RecursiveDirectoryIterator($dir);
135     $iter = new RecursiveIteratorIterator($dirs);
136     foreach ($iter as $pathname => $item) {
137         if ($item->isFile() && preg_match('/\.php$/', $item->getBaseName())) {
138             $files[] = $pathname;
139         }
140     }
141     return $files;
142 }
143
144 function plugin_using_gettext($dir)
145 {
146     $files = get_plugin_sources($dir);
147     foreach ($files as $pathname) {
148         // Check if the file is using our _m gettext wrapper
149         $code = file_get_contents($pathname);
150         if (preg_match('/\b_m\(/', $code)) {
151             return true;
152         }
153     }
154
155     return false;
156 }
157
158 function update_plugin($basedir, $name)
159 {
160     $dir = "$basedir/plugins/$name";
161     if (plugin_using_gettext($dir)) {
162         do_update_plugin($dir, $name);
163         do_translatewiki_plugin($basedir, $name);
164         return true;
165     } else {
166         return false;
167     }
168 }
169
170 $args = $_SERVER['argv'];
171 array_shift($args);
172
173 $all = false;
174 $core = false;
175 $allplugins = false;
176 $plugins = array();
177 if (count($args) == 0) {
178     $all = true;
179 }
180 foreach ($args as $arg) {
181     if ($arg == '--all') {
182         $all = true;
183     } elseif ($arg == "--core") {
184         $core = true;
185     } elseif ($arg == "--plugins") {
186         $allplugins = true;
187     } elseif (substr($arg, 0, 9) == "--plugin=") {
188         $plugins[] = substr($arg, 9);
189     }
190 }
191
192
193
194 if ($all || $core) {
195     echo "core...";
196     update_core(INSTALLDIR, 'statusnet');
197     echo " ok\n";
198 }
199 if ($all || $allplugins) {
200     $plugins = get_plugins(INSTALLDIR);
201 }
202 if ($plugins) {
203     foreach ($plugins as $plugin) {
204         echo "$plugin...";
205         if (update_plugin(INSTALLDIR, $plugin)) {
206             echo " ok\n";
207         } else {
208             echo " not localized\n";
209         }
210     }
211 }