PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/wordpress/praized-community/includes/php/praized-wp-core/tools/makepot-praized.php

http://praized.googlecode.com/
PHP | 208 lines | 166 code | 27 blank | 15 comment | 22 complexity | 5396bc4923ae61bffe57c810dc863197 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * Copied from http://svn.automattic.com/wordpress-i18n/tools/trunk/makepot.php (rev 4551).
  4. *
  5. * Modified by Stephane Daury, for Praized Media, to include our WordPress plugins custom
  6. * convenience gettext methods/functions ($this->__(), $this->_e(), pzdc__(), pzdc_e()).
  7. *
  8. * CLI use EG:
  9. * php makepot-praized.php wp-plugin /path/to/wordpress/wp-content/plugins/praized-tools/
  10. *
  11. * Note: requires xgettext http://www.gnu.org/software/gettext/
  12. */
  13. class MakePOT {
  14. var $use_advanced_xgettext_args = true;
  15. var $max_header_lines = 30;
  16. var $projects = array(
  17. 'generic',
  18. 'wp',
  19. 'wp-plugin',
  20. 'bb',
  21. 'mu',
  22. );
  23. var $keywords = array(
  24. '__', '_e', '_c', '__ngettext:1,2', '__n:1,2',
  25. '__ngettext_noop:1,2', '$this->__', '$this->_e',
  26. 'pzdc__', 'pzdc_e'
  27. );
  28. var $xgettext_options = array(
  29. 'default' => array(
  30. 'from-code' => 'utf-8',
  31. 'msgid-bugs-address' => 'wp-polyglots@lists.automattic.com',
  32. 'language' => 'php',
  33. ),
  34. 'generic' => array(),
  35. 'wp' => array(
  36. 'copyright-holder' => 'WordPress',
  37. 'package-name' => 'WordPress',
  38. 'package-version' => '{version}',
  39. ),
  40. 'bb' => array(
  41. 'copyright-holder' => 'bbPress',
  42. 'package-name' => 'bbPress',
  43. 'package-version' => '{version}',
  44. ),
  45. 'wp-plugin' => array(
  46. 'msgid-bugs-address' => 'http://wordpress.org/tag/{slug}',
  47. 'copyright-holder' => '{author}',
  48. 'package-name' => '{plugin-name}',
  49. 'package-version' => '{version}',
  50. ),
  51. );
  52. function MakePOT($use_advanced_xgettext_args = true) {
  53. $this->use_advanced_xgettext_args = $use_advanced_xgettext_args;
  54. }
  55. function realpath_missing($path) {
  56. return realpath(dirname($path)).DIRECTORY_SEPARATOR.basename($path);
  57. }
  58. function xgettext($project, $dir, $output_file, $placeholders = array()) {
  59. $options = array_merge($this->xgettext_options['default'], $this->xgettext_options[$project]);
  60. $options['output'] = $this->realpath_missing($output_file);
  61. $placeholder_keys = array_map(create_function('$x', 'return "{".$x."}";'), array_keys($placeholders));
  62. $placeholder_values = array_values($placeholders);
  63. foreach($options as $key => $value)
  64. $options[$key] = str_replace($placeholder_keys, $placeholder_values, $value);
  65. if (!$this->use_advanced_xgettext_args) {
  66. unset($options['package-name']);
  67. unset($options['package-version']);
  68. }
  69. $long_options = array();
  70. foreach($this->keywords as $keyword)
  71. $long_options[] = "--keyword=$keyword";
  72. foreach($options as $key => $value)
  73. $long_options[] = is_string($value)? "--$key=$value" : "--$key";
  74. $long_options = array_map('escapeshellarg', $long_options);
  75. $string_options = implode(" \\\n", $long_options);
  76. // change dirs, so that we have nice relative references
  77. $old_dir = getcwd();
  78. chdir($dir);
  79. $cmd = "
  80. find . -name '*.php' -print \\
  81. | sed -e 's,^\./,,' \\
  82. | sort \\
  83. | xargs xgettext \\
  84. $string_options";
  85. system($cmd, $exit_code);
  86. chdir($old_dir);
  87. return $exit_code;
  88. }
  89. function wp($dir, $output) {
  90. $placeholders = array();
  91. if (preg_match('/\$wp_version\s*=\s*\'(.*?)\';/', file_get_contents($dir.'/wp-includes/version.php'), $matches)) {
  92. $placeholders['version'] = $matches[1];
  93. }
  94. $output = is_null($output)? 'wordpress.pot' : $output;
  95. return $this->xgettext('wp', $dir, $output, $placeholders);
  96. }
  97. function mu($dir, $output) {
  98. $placeholders = array();
  99. if (preg_match('/\$wpmu_version\s*=\s*\'(.*?)\';/', file_get_contents($dir.'/wp-includes/version.php'), $matches)) {
  100. $placeholders['version'] = $matches[1];
  101. }
  102. $output = is_null($output)? 'wordpress.pot' : $output;
  103. return $this->xgettext('wp', $dir, $output, $placeholders);
  104. }
  105. function bb($dir, $output) {
  106. $placeholders = array();
  107. if (preg_match('/case\s+\'version\'.*?return\s+\'(.*?)\';/s', file_get_contents($dir.'/bb-includes/functions.php'), $matches)) {
  108. $placeholders['version'] = $matches[1];
  109. }
  110. $output = is_null($output)? 'bbpress.pot' : $output;
  111. return $this->xgettext('bb', $dir, $output, $placeholders);
  112. }
  113. function get_first_lines($filename, $lines = 30) {
  114. $extf = fopen($filename, 'r');
  115. if (!$extf) return false;
  116. $first_lines = '';
  117. foreach(range(1, $lines) as $x) {
  118. if (feof($extf)) break;
  119. $line = fgets($extf);
  120. if (false === $line) {
  121. return false;
  122. }
  123. $first_lines .= $line;
  124. }
  125. return $first_lines;
  126. }
  127. function get_addon_header($header, &$source) {
  128. if (preg_match('|'.$header.':(.*)$|mi', $source, $matches))
  129. return trim($matches[1]);
  130. else
  131. return false;
  132. }
  133. function generic($dir, $output) {
  134. $output = is_null($output)? "generic.pot" : $output;
  135. return $this->xgettext('generic', $dir, $output, array());
  136. }
  137. function guess_plugin_slug($dir) {
  138. if ('trunk' == basename($dir)) {
  139. $slug = basename(dirname($dir));
  140. } elseif (in_array(basename(dirname($dir)), array('branches', 'tags'))) {
  141. $slug = basename(dirname(dirname($dir)));
  142. } else {
  143. $slug = basename($dir);
  144. }
  145. return $slug;
  146. }
  147. function wp_plugin($dir, $output, $slug = null) {
  148. $placeholders = array();
  149. // guess plugin slug
  150. if (is_null($slug)) {
  151. $slug = $this->guess_plugin_slug($dir);
  152. }
  153. $main_file = $dir.'/'.$slug.'.php';
  154. $source = $this->get_first_lines($main_file, $this->max_header_lines);
  155. $placeholders['version'] = $this->get_addon_header('Version', $source);
  156. $placeholders['author'] = $this->get_addon_header('Author', $source);
  157. $placeholders['plugin-name'] = $this->get_addon_header('Plugin Name', $source);
  158. $placeholders['slug'] = $slug;
  159. $output = is_null($output)? "$slug.pot" : $output;
  160. return $this->xgettext('wp-plugin', $dir, $output, $placeholders);
  161. }
  162. }
  163. // run the CLI only if the file
  164. // wasn't included
  165. $included_files = get_included_files();
  166. if ($included_files[0] == __FILE__) {
  167. $makepot = new MakePOT;
  168. if ((3 == count($argv) || 4 == count($argv)) && in_array($method = str_replace('-', '_', $argv[1]), get_class_methods($makepot))) {
  169. $res = call_user_func(array(&$makepot, $method), realpath($argv[2]), isset($argv[3])? $argv[3] : null);
  170. if (0 != $res) {
  171. fwrite(STDERR, "xgettext returned exit code $res!\n");
  172. }
  173. } else {
  174. $usage = "Usage: php makepot.php PROJECT DIRECTORY [OUTPUT]\n\n";
  175. $usage .= "Generate POT file from the files in DIRECTORY [OUTPUT]\n";
  176. $usage .= "Avaialbale projects: ".implode(', ', $makepot->projects)."\n";
  177. fwrite(STDERR, $usage);
  178. exit(1);
  179. }
  180. }
  181. ?>