1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Console application, which adds textdomain argument |
4
|
|
|
* to all i18n function calls |
5
|
|
|
* |
6
|
|
|
* @package wordpress-i18n |
7
|
|
|
*/ |
8
|
|
|
error_reporting(E_ALL); |
9
|
|
|
|
10
|
|
|
require_once dirname( __FILE__ ) . '/makepot.php'; |
11
|
|
|
|
12
|
|
|
class AddTextdomain { |
13
|
|
|
|
14
|
|
|
var $modified_contents = ''; |
15
|
|
|
var $funcs; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Constructor. |
19
|
|
|
*/ |
20
|
|
|
public function __construct() { |
21
|
|
|
$makepot = new MakePOT; |
22
|
|
|
$this->funcs = array_keys( $makepot->rules ); |
23
|
|
|
$this->funcs[] = 'translate_nooped_plural'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Prints CLI usage. |
28
|
|
|
*/ |
29
|
|
|
public function usage() { |
30
|
|
|
$usage = "Usage: php add-textdomain.php [-i] <domain> <file>\n\nAdds the string <domain> as a last argument to all i18n function calls in <file>\nand prints the modified php file on standard output.\n\nOptions:\n -i Modifies the PHP file in place, instead of printing it to standard output.\n"; |
31
|
|
|
fwrite(STDERR, $usage); |
32
|
|
|
exit(1); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Adds textdomain to a single file. |
37
|
|
|
* |
38
|
|
|
* @see AddTextdomain::process_string() |
39
|
|
|
* |
40
|
|
|
* @param string $domain Text domain. |
41
|
|
|
* @param string $source_filename Filename with optional path. |
42
|
|
|
* @param bool $inplace True to modifies the PHP file in place. False to print to standard output. |
43
|
|
|
*/ |
44
|
|
|
public function process_file( $domain, $source_filename, $inplace ) { |
45
|
|
|
$new_source = $this->process_string( $domain, file_get_contents( $source_filename ) ); |
46
|
|
|
|
47
|
|
|
if ( $inplace ) { |
48
|
|
|
$f = fopen( $source_filename, 'w' ); |
49
|
|
|
fwrite( $f, $new_source ); |
50
|
|
|
fclose( $f ); |
51
|
|
|
} else { |
52
|
|
|
echo $new_source; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Adds textdomain to a string of PHP. |
58
|
|
|
* |
59
|
|
|
* Functions calls should be wrapped in opening and closing PHP delimiters as usual. |
60
|
|
|
* |
61
|
|
|
* @see AddTextdomain::process_tokens() |
62
|
|
|
* |
63
|
|
|
* @param string $domain Text domain. |
64
|
|
|
* @param string $string PHP code to parse. |
65
|
|
|
* @return string Modified source. |
66
|
|
|
*/ |
67
|
|
|
public function process_string( $domain, $string ) { |
68
|
|
|
$tokens = token_get_all( $string ); |
69
|
|
|
return $this->process_tokens( $domain, $tokens ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Adds textdomain to a set of PHP tokens. |
74
|
|
|
* |
75
|
|
|
* @param string $domain Text domain. |
76
|
|
|
* @param array $tokens PHP tokens. An array of token identifiers. Each individual token identifier is either a |
77
|
|
|
* single character (i.e.: ;, ., >, !, etc.), or a three element array containing the token |
78
|
|
|
* index in element 0, the string content of the original token in element 1 and the line |
79
|
|
|
* number in element 2. |
80
|
|
|
* @return string Modified source. |
81
|
|
|
*/ |
82
|
|
|
public function process_tokens( $domain, $tokens ) { |
83
|
|
|
$this->modified_contents = ''; |
84
|
|
|
$domain = addslashes( $domain ); |
85
|
|
|
|
86
|
|
|
$in_func = false; |
87
|
|
|
$args_started = false; |
88
|
|
|
$parens_balance = 0; |
89
|
|
|
$found_domain = false; |
90
|
|
|
|
91
|
|
|
foreach($tokens as $index => $token) { |
92
|
|
|
$string_success = false; |
93
|
|
|
if (is_array($token)) { |
94
|
|
|
list($id, $text) = $token; |
95
|
|
|
if (T_STRING == $id && in_array($text, $this->funcs)) { |
96
|
|
|
$in_func = true; |
97
|
|
|
$parens_balance = 0; |
98
|
|
|
$args_started = false; |
99
|
|
|
$found_domain = false; |
100
|
|
|
} elseif (T_CONSTANT_ENCAPSED_STRING == $id && ("'$domain'" == $text || "\"$domain\"" == $text)) { |
101
|
|
|
if ($in_func && $args_started) { |
102
|
|
|
$found_domain = true; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
$token = $text; |
106
|
|
|
} elseif ('(' == $token){ |
107
|
|
|
$args_started = true; |
108
|
|
|
++$parens_balance; |
109
|
|
|
} elseif (')' == $token) { |
110
|
|
|
--$parens_balance; |
111
|
|
|
if ($in_func && 0 == $parens_balance) { |
112
|
|
|
if ( ! $found_domain ) { |
113
|
|
|
$token = ", '$domain'"; |
114
|
|
|
if ( T_WHITESPACE == $tokens[ $index - 1 ][0] ) { |
115
|
|
|
$token .= ' '; // Maintain code standards if previously present |
116
|
|
|
// Remove previous whitespace token to account for it. |
117
|
|
|
$this->modified_contents = trim( $this->modified_contents ); |
118
|
|
|
} |
119
|
|
|
$token .= ')'; |
120
|
|
|
} |
121
|
|
|
$in_func = false; |
122
|
|
|
$args_started = false; |
123
|
|
|
$found_domain = false; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
$this->modified_contents .= $token; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->modified_contents; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Run the CLI only if the file wasn't included. |
134
|
|
|
$included_files = get_included_files(); |
135
|
|
|
if ($included_files[0] == __FILE__) { |
136
|
|
|
$adddomain = new AddTextdomain(); |
137
|
|
|
|
138
|
|
|
if (!isset($argv[1]) || !isset($argv[2])) { |
139
|
|
|
$adddomain->usage(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$inplace = false; |
143
|
|
|
if ('-i' == $argv[1]) { |
144
|
|
|
$inplace = true; |
145
|
|
|
if (!isset($argv[3])) $adddomain->usage(); |
146
|
|
|
array_shift($argv); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
if ( is_dir( $argv[2] ) ) { |
150
|
|
|
$directory = new RecursiveDirectoryIterator( $argv[2], RecursiveDirectoryIterator::SKIP_DOTS ); |
151
|
|
|
$files = new RecursiveIteratorIterator( $directory ); |
152
|
|
|
foreach ( $files as $file ) { |
153
|
|
|
if ( 'php' === $file->getExtension() ) { |
154
|
|
|
$adddomain->process_file( $argv[1], $file->getPathname(), $inplace ); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
} else { |
158
|
|
|
$adddomain->process_file( $argv[1], $argv[2], $inplace ); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.