aboutsummaryrefslogtreecommitdiffstats
path: root/tools/missing_keys.pl
blob: 1b83dacb10a920a28149f58b84922de7f84380b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/perl

use strict;
use warnings;

use File::Find;

our %used_keys = ();
our %registered_keys = ();

my $piwigo_dir = $ARGV[0]; # '/home/pierrick/public_html/piwigo/dev/trunk';
my $type = $ARGV[1];       # common, admin, install, upgrade

find(\&used_keys, $piwigo_dir);
load_registered_keys($type);

foreach my $key (sort keys %used_keys) {
    # print "{".$key."}", ' is used', "\n";

    if (not defined $registered_keys{$key}) {
        # print "{".$key."}", ' is missing', "\n";
        print '$lang[\''.$key.'\'] = \''.$key.'\';', "\n";
    }
}

my %ignore_keys = (
    '%d new image' => 1,
    '%d new images' => 1,
    '%d category updated' => 1,
    '%d categories updated' => 1,
    '%d new comment' => 1,
    '%d new comments' => 1,
    '%d comment to validate' => 1,
    '%d comments to validate' => 1,
    '%d new user' => 1,
    '%d new users' => 1,
    '%d waiting element' => 1,
    '%d waiting elements' => 1,
    'user_status_admin' => '',
    'user_status_generic' => '',
    'user_status_guest' => '',
    'user_status_normal' => '',
    'user_status_webmaster' => '',
    'Level 0' => '',
    'Level 1' => '',
    'Level 2' => '',
    'Level 4' => '',
    'Level 8' => '',
    'ACCESS_0' => '',
    'ACCESS_1' => '',
    'ACCESS_2' => '',
    'ACCESS_3' => '',
    'ACCESS_4' => '',
    'ACCESS_5' => '',
    'month' => '',
    'day' => '',
    'chronology_monthly_calendar' => '',
    'chronology_monthly_list' => '',
    'chronology_weekly_list' => '',
    'public' => '',
    'private' => '',
    'none' => '',
    'other' => '',
    'high' => '',
    'Waiting page: %s' => '',
    'Admin: %s' => '',
    'Manage this user comment: %s' => '',
    'Main "guest" user does not exist' => '',
    'Main "guest" user status is incorrect' => '',
    'Main "webmaster" user does not exist' => '',
    'Main "webmaster" user status is incorrect' => '',
    'Default user does not exist' => '',
    '(!) This comment requires validation' => '',
);


# foreach my $key (sort keys %registered_keys) {
#     if (not defined $used_keys{$key} and not defined $ignore_keys{$key}) {
#         print "{".$key."}", ' is not used anywhere', "\n";
#     }
# }

sub used_keys {
    if ($File::Find::name !~ m/(tpl|php)$/) {
        return 0;
    }

    if ($File::Find::name =~ m{/(plugins|language|_data)/}) {
        return 0;
    }

    if ('upgrade' eq $type) {
        if ($File::Find::name !~ m{upgrade\.(tpl|php)$}) {
            # return 0;
        }
    }

    if ('install' eq $type) {
        if ($File::Find::name =~ m{upgrade\.(tpl|php)$}) {
            return 0;
        }
        if ($File::Find::name !~ m{/install(\.tpl|\.php|/)}) {
            return 0;
        }
    }

    if ('admin' eq $type) {
        if ($File::Find::name =~ m{upgrade\.(tpl|php)$}) {
            return 0;
        }
        if ($File::Find::name =~ m{/install(\.tpl|\.php|/)}) {
            return 0;
        }

        my $is_admin = 0;

        if ($File::Find::name =~ m{themes/default/template/mail}) {
            $is_admin = 1;
        }
        if ($File::Find::name =~ m{/admin/}) {
            $is_admin = 1;
        }
        if ($File::Find::name =~ m{/admin\.php$}) {
            $is_admin = 1;
        }

        if (not $is_admin) {
            return 0;
        }
    }

    if ('common' eq $type) {
        if ($File::Find::name =~ m{upgrade\.(tpl|php)$}) {
            return 0;
        }
        if ($File::Find::name =~ m{/install(\.tpl|\.php|/)}) {
            return 0;
        }
        if ($File::Find::name =~ m{/admin(/|\.php)} or $File::Find::name =~ m{themes/default/template/mail}) {
            return 0;
        }
    }

    if (-f) {
        my $big_string = '';
        open(my $fhi, '<', $File::Find::name);
        while (<$fhi>) {
            chomp;
            s{//.*$}{};
            $big_string.= $_;
        }
        close($fhi);

        while ($big_string =~ m/\{(['"])(.+?)\1\|\@translate/g) {
            $used_keys{$2}++;
        }

        while ($big_string =~ m/l10n \s* \( \s* (['"]) (.+?) \1 \s* \)/xg) {
            $used_keys{$2}++;
        }

        while ($big_string =~ m/l10n_args \s* \( \s* (['"]) (.+?) \1 \s* ,/xg) {
            $used_keys{$2}++;
        }

        while ($big_string =~ m/l10n_dec \s* \( \s* (['"]) (.+?) \1 \s* ,\s* (['"]) (.+?) \3 \s* ,/xg) {
            $used_keys{$2}++;
            $used_keys{$4}++;
        }
    }
}

sub load_registered_keys {
    my ($type) = @_;

    my %files_for_type = (
        common  => [qw/common/],
        admin   => [qw/common admin/],
        install => [qw/common admin install/],
        upgrade => [qw/common admin install upgrade/],
    );

    foreach my $file_code (@{$files_for_type{$type}}) {
        my $filepath = $piwigo_dir.'/language/en_UK/'.$file_code.'.lang.php';

        open(my $fhi, '<', $filepath);
        while (<$fhi>) {
            if (m/\$lang\[ \s* (['"]) (.+?) \1 \s* \]/x) {
                $registered_keys{$2}++;
            }
        }
    }
}