add-textdomain.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. require_once dirname( __FILE__ ) . '/makepot.php';
  10. class AddTextdomain {
  11. var $modified_contents = '';
  12. var $funcs;
  13. function AddTextdomain() {
  14. $makepot = new MakePOT;
  15. $this->funcs = array_keys( $makepot->rules );
  16. }
  17. function usage() {
  18. $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";
  19. fwrite(STDERR, $usage);
  20. exit(1);
  21. }
  22. function process_token($token_text, $inplace) {
  23. if ($inplace)
  24. $this->modified_contents .= $token_text;
  25. else
  26. echo $token_text;
  27. }
  28. function process_file($domain, $source_filename, $inplace) {
  29. $this->modified_contents = '';
  30. $domain = addslashes($domain);
  31. $source = file_get_contents($source_filename);
  32. $tokens = token_get_all($source);
  33. $in_func = false;
  34. $args_started = false;
  35. $parens_balance = 0;
  36. $found_domain = false;
  37. foreach($tokens as $token) {
  38. $string_success = false;
  39. if (is_array($token)) {
  40. list($id, $text) = $token;
  41. if (T_STRING == $id && in_array($text, $this->funcs)) {
  42. $in_func = true;
  43. $parens_balance = 0;
  44. $args_started = false;
  45. $found_domain = false;
  46. } elseif (T_CONSTANT_ENCAPSED_STRING == $id && ("'$domain'" == $text || "\"$domain\"" == $text)) {
  47. if ($in_func && $args_started) {
  48. $found_domain = true;
  49. }
  50. }
  51. $token = $text;
  52. } elseif ('(' == $token){
  53. $args_started = true;
  54. ++$parens_balance;
  55. } elseif (')' == $token) {
  56. --$parens_balance;
  57. if ($in_func && 0 == $parens_balance) {
  58. $token = $found_domain? ')' : ", '$domain')";
  59. $in_func = false;
  60. $args_started = false;
  61. $found_domain = false;
  62. }
  63. }
  64. $this->process_token($token, $inplace);
  65. }
  66. if ($inplace) {
  67. $f = fopen($source_filename, 'w');
  68. fwrite($f, $this->modified_contents);
  69. fclose($f);
  70. }
  71. }
  72. }
  73. // run the CLI only if the file
  74. // wasn't included
  75. $included_files = get_included_files();
  76. if ($included_files[0] == __FILE__) {
  77. $adddomain = new AddTextdomain;
  78. if (!isset($argv[1]) || !isset($argv[2])) {
  79. $adddomain->usage();
  80. }
  81. $inplace = false;
  82. if ('-i' == $argv[1]) {
  83. $inplace = true;
  84. if (!isset($argv[3])) $adddomain->usage();
  85. array_shift($argv);
  86. }
  87. $adddomain->process_file($argv[1], $argv[2], $inplace);
  88. }
  89. ?>