4f6f3b2 by Brion Vibber at 2009-12-07 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
2c9c9f9 by Brion Vibber at 2009-12-07 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
c88ecaa by Brion Vibber at 2009-12-08 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
    $data = <<<END
84
---
85
BASIC:
86
  id: out-statusnet-{$plugin}
87
  label: StatusNet - {$plugin}
88
  description: "{{int:bw-desc-statusnet-plugin-{$plugin}}}"
89
  namespace: NS_STATUSNET
90
  display: out/statusnet/{$plugin}
91
  class: GettextMessageGroup
92
93
FILES:
94
  class: GettextFFS
95
  sourcePattern: %GROUPROOT%/plugins/{$plugin}/locale/%CODE%/LC_MESSAGES/{$plugin}.po
96
  targetPattern: {$plugin}.po
97
  codeMap:
98
    en-gb: en_GB
99
    no: nb
100
    pt-br: pt_BR
101
    zh-hans: zh_CN
102
    zh-hant: zh_TW
103
104
MANGLER
105
  class: StringMatcher
106
  prefix: {$plugin}-
107
  patterns:
108
    - "*"
109
110
END;
111
    file_put_contents($outfile, $data);
112
}
113
4f6f3b2 by Brion Vibber at 2009-12-07 114
function get_plugins($dir)
115
{
116
    $plugins = array();
117
    $dirs = new DirectoryIterator("$dir/plugins");
118
    foreach ($dirs as $item) {
119
        if ($item->isDir() && !$item->isDot()) {
120
            $name = $item->getBasename();
121
            if (file_exists("$dir/plugins/$name/{$name}Plugin.php")) {
122
                $plugins[] = $name;
123
            }
124
        }
125
    }
126
    return $plugins;
127
}
128
2c9c9f9 by Brion Vibber at 2009-12-07 129
function get_plugin_sources($dir)
4f6f3b2 by Brion Vibber at 2009-12-07 130
{
2c9c9f9 by Brion Vibber at 2009-12-07 131
    $files = array();
132
4f6f3b2 by Brion Vibber at 2009-12-07 133
    $dirs = new RecursiveDirectoryIterator($dir);
134
    $iter = new RecursiveIteratorIterator($dirs);
135
    foreach ($iter as $pathname => $item) {
2c9c9f9 by Brion Vibber at 2009-12-07 136
        if ($item->isFile() && preg_match('/\.php$/', $item->getBaseName())) {
137
            $files[] = $pathname;
138
        }
139
    }
140
    return $files;
141
}
142
143
function plugin_using_gettext($dir)
144
{
145
    $files = get_plugin_sources($dir);
146
    foreach ($files as $pathname) {
147
        // Check if the file is using our _m gettext wrapper
148
        $code = file_get_contents($pathname);
149
        if (preg_match('/\b_m\(/', $code)) {
150
            return true;
4f6f3b2 by Brion Vibber at 2009-12-07 151
        }
152
    }
153
154
    return false;
155
}
156
157
function update_plugin($basedir, $name)
158
{
159
    $dir = "$basedir/plugins/$name";
160
    if (plugin_using_gettext($dir)) {
2c9c9f9 by Brion Vibber at 2009-12-07 161
        do_update_plugin($dir, $name);
c88ecaa by Brion Vibber at 2009-12-08 162
        do_translatewiki_plugin($basedir, $name);
4f6f3b2 by Brion Vibber at 2009-12-07 163
        return true;
164
    } else {
165
        return false;
166
    }
167
}
168
b35805b by Brion Vibber at 2009-12-08 169
$args = $_SERVER['argv'];
170
array_shift($args);
171
172
$all = false;
173
$core = false;
174
$allplugins = false;
175
$plugins = array();
176
if (count($args) == 0) {
177
    $all = true;
178
}
179
foreach ($args as $arg) {
180
    if ($arg == '--all') {
181
        $all = true;
182
    } elseif ($arg == "--core") {
183
        $core = true;
184
    } elseif ($arg == "--plugins") {
185
        $allplugins = true;
186
    } elseif (substr($arg, 0, 9) == "--plugin=") {
187
        $plugins[] = substr($arg, 9);
4f6f3b2 by Brion Vibber at 2009-12-07 188
    }
189
}
190
191
b35805b by Brion Vibber at 2009-12-08 192
193
if ($all || $core) {
194
    echo "core...";
195
    update_core(INSTALLDIR, 'statusnet');
196
    echo " ok\n";
197
}
198
if ($all || $allplugins) {
199
    $plugins = get_plugins(INSTALLDIR);
200
}
201
if ($plugins) {
202
    foreach ($plugins as $plugin) {
203
        echo "$plugin...";
204
        if (update_plugin(INSTALLDIR, $plugin)) {
205
            echo " ok\n";
206
        } else {
207
            echo " not localized\n";
208
        }
209
    }
210
}
211