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