]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/update_po_templates.php
Merge commit 'refs/merge-requests/166' of git://gitorious.org/statusnet/mainline...
[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.pot \
38     --language=PHP \
39     --add-comments=TRANS \
40     --keyword="_m:1,1t" \
41     --keyword="_m:1c,2,2t" \
42     --keyword="_m:1,2,3t" \
43     --keyword="_m:1c,2,3,4t" \
44     --keyword="pgettext:1c,2" \
45     --keyword="npgettext:1c,2,3" \
46     index.php \
47     actions/*.php \
48     classes/*.php \
49     lib/*.php \
50     scripts/*.php
51 END
52 );
53     chdir($old);
54 }
55
56 function do_update_plugin($dir, $domain)
57 {
58     $old = getcwd();
59     chdir($dir);
60     if (!file_exists('locale')) {
61         mkdir('locale');
62     }
63     $files = get_plugin_sources(".");
64     $cmd = <<<END
65 xgettext \
66     --from-code=UTF-8 \
67     --default-domain=$domain \
68     --output=locale/$domain.pot \
69     --language=PHP \
70     --add-comments=TRANS \
71     --keyword='' \
72     --keyword="_m:1,1t" \
73     --keyword="_m:1c,2,2t" \
74     --keyword="_m:1,2,3t" \
75     --keyword="_m:1c,2,3,4t" \
76
77 END;
78     foreach ($files as $file) {
79       $cmd .= ' ' . escapeshellarg($file);
80     }
81     passthru($cmd);
82     chdir($old);
83 }
84
85 function get_plugins($dir)
86 {
87     $plugins = array();
88     $dirs = new DirectoryIterator("$dir/plugins");
89     foreach ($dirs as $item) {
90         if ($item->isDir() && !$item->isDot()) {
91             $name = $item->getBasename();
92             if (file_exists("$dir/plugins/$name/{$name}Plugin.php")) {
93                 $plugins[] = $name;
94             }
95         }
96     }
97     return $plugins;
98 }
99
100 function get_plugin_sources($dir)
101 {
102     $files = array();
103
104     $dirs = new RecursiveDirectoryIterator($dir);
105     $iter = new RecursiveIteratorIterator($dirs);
106     foreach ($iter as $pathname => $item) {
107         if ($item->isFile() && preg_match('/\.php$/', $item->getBaseName())) {
108             $files[] = $pathname;
109         }
110     }
111     return $files;
112 }
113
114 function plugin_using_gettext($dir)
115 {
116     $files = get_plugin_sources($dir);
117     foreach ($files as $pathname) {
118         // Check if the file is using our _m gettext wrapper
119         $code = file_get_contents($pathname);
120         if (preg_match('/\b_m\(/', $code)) {
121             return true;
122         }
123     }
124
125     return false;
126 }
127
128 function update_plugin($basedir, $name)
129 {
130     $dir = "$basedir/plugins/$name";
131     if (plugin_using_gettext($dir)) {
132         do_update_plugin($dir, $name);
133         return true;
134     } else {
135         return false;
136     }
137 }
138
139 $args = $_SERVER['argv'];
140 array_shift($args);
141
142 $all = false;
143 $core = false;
144 $allplugins = false;
145 $plugins = array();
146 if (count($args) == 0) {
147     $all = true;
148 }
149 foreach ($args as $arg) {
150     if ($arg == '--all') {
151         $all = true;
152     } elseif ($arg == "--core") {
153         $core = true;
154     } elseif ($arg == "--plugins") {
155         $allplugins = true;
156     } elseif (substr($arg, 0, 9) == "--plugin=") {
157         $plugins[] = substr($arg, 9);
158     } elseif ($arg == '--help') {
159         echo "options: --all --core --plugins --plugin=Foo\n\n";
160         exit(0);
161     }
162 }
163
164 if ($all || $core) {
165     echo "core...";
166     update_core(INSTALLDIR, 'statusnet');
167     echo " ok\n";
168 }
169 if ($all || $allplugins) {
170     $plugins = get_plugins(INSTALLDIR);
171 }
172 if ($plugins) {
173     foreach ($plugins as $plugin) {
174         echo "$plugin...";
175         if (update_plugin(INSTALLDIR, $plugin)) {
176             echo " ok\n";
177         } else {
178             echo " not localized\n";
179         }
180     }
181 }