update_po_templates.php 4.4 KB

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