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