PageRenderTime 20ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Gallary/modules/gallery/helpers/l10n_scanner.php

https://bitbucket.org/JakePratt/kupcakz.com
PHP | 178 lines | 131 code | 14 blank | 33 comment | 46 complexity | 0d8caab68156e340c077aeffa01193bf MD5 | raw file
  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. /**
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2013 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /**
  21. * Scans all source code for messages that need to be localized.
  22. */
  23. class l10n_scanner_Core {
  24. // Based on Drupal's potx module, originally written by:
  25. // G‡bor Hojtsy http://drupal.org/user/4166
  26. public static $cache;
  27. static function process_message($message, &$cache) {
  28. if (empty($cache)) {
  29. foreach (db::build()
  30. ->select("key")
  31. ->from("incoming_translations")
  32. ->where("locale", "=", "root")
  33. ->execute() as $row) {
  34. $cache[$row->key] = true;
  35. }
  36. }
  37. $key = Gallery_I18n::get_message_key($message);
  38. if (array_key_exists($key, $cache)) {
  39. return $cache[$key];
  40. }
  41. $entry = ORM::factory("incoming_translation")->where("key", "=", $key)->find();
  42. if (!$entry->loaded()) {
  43. $entry->key = $key;
  44. $entry->message = serialize($message);
  45. $entry->locale = "root";
  46. $entry->save();
  47. }
  48. }
  49. static function scan_php_file($file, &$cache) {
  50. $code = file_get_contents($file);
  51. $raw_tokens = token_get_all($code);
  52. unset($code);
  53. $tokens = array();
  54. $func_token_list = array("t" => array(), "t2" => array());
  55. $token_number = 0;
  56. // Filter out HTML / whitespace, and build a lookup for global function calls.
  57. foreach ($raw_tokens as $token) {
  58. if ((!is_array($token)) || (($token[0] != T_WHITESPACE) && ($token[0] != T_INLINE_HTML))) {
  59. if (is_array($token)) {
  60. if ($token[0] == T_STRING && in_array($token[1], array("t", "t2"))) {
  61. $func_token_list[$token[1]][] = $token_number;
  62. }
  63. }
  64. $tokens[] = $token;
  65. $token_number++;
  66. }
  67. }
  68. unset($raw_tokens);
  69. if (!empty($func_token_list["t"])) {
  70. $errors = l10n_scanner::_parse_t_calls($tokens, $func_token_list["t"], $cache);
  71. foreach ($errors as $line => $error) {
  72. Kohana_Log::add(
  73. "error", "Translation scanner error. " .
  74. "file: " . substr($file, strlen(DOCROOT)) . ", line: $line, context: $error");
  75. }
  76. }
  77. if (!empty($func_token_list["t2"])) {
  78. $errors = l10n_scanner::_parse_plural_calls($tokens, $func_token_list["t2"], $cache);
  79. foreach ($errors as $line => $error) {
  80. Kohana_Log::add(
  81. "error", "Translation scanner error. " .
  82. "file: " . substr($file, strlen(DOCROOT)) . ", line: $line, context: $error");
  83. }
  84. }
  85. }
  86. static function scan_info_file($file, &$cache) {
  87. $info = new ArrayObject(parse_ini_file($file), ArrayObject::ARRAY_AS_PROPS);
  88. foreach (array('name', 'description') as $property) {
  89. if (isset($info->$property)) {
  90. l10n_scanner::process_message($info->$property, $cache);
  91. }
  92. }
  93. }
  94. private static function _parse_t_calls(&$tokens, &$call_list, &$cache) {
  95. $errors = array();
  96. foreach ($call_list as $index) {
  97. $function_name = $tokens[$index++];
  98. $parens = $tokens[$index++];
  99. $first_param = $tokens[$index++];
  100. $next_token = $tokens[$index];
  101. if ($parens == "(") {
  102. if (in_array($next_token, array(")", ","))
  103. && (is_array($first_param) && ($first_param[0] == T_CONSTANT_ENCAPSED_STRING))) {
  104. $message = self::_escape_quoted_string($first_param[1]);
  105. l10n_scanner::process_message($message, $cache);
  106. } else {
  107. if (is_array($first_param) && ($first_param[0] == T_CONSTANT_ENCAPSED_STRING)) {
  108. // Malformed string literals; escalate this
  109. $errors[$first_param[2]] =
  110. var_export(array($function_name, $parens, $first_param, $next_token), 1);
  111. } else {
  112. // t() found, but inside is something which is not a string literal. That's fine.
  113. }
  114. }
  115. }
  116. }
  117. return $errors;
  118. }
  119. private static function _parse_plural_calls(&$tokens, &$call_list, &$cache) {
  120. $errors = array();
  121. foreach ($call_list as $index) {
  122. $function_name = $tokens[$index++];
  123. $parens = $tokens[$index++];
  124. $first_param = $tokens[$index++];
  125. $first_separator = $tokens[$index++];
  126. $second_param = $tokens[$index++];
  127. $next_token = $tokens[$index];
  128. if ($parens == "(") {
  129. if ($first_separator == "," && $next_token == ","
  130. && is_array($first_param) && $first_param[0] == T_CONSTANT_ENCAPSED_STRING
  131. && is_array($second_param) && $second_param[0] == T_CONSTANT_ENCAPSED_STRING) {
  132. $singular = self::_escape_quoted_string($first_param[1]);
  133. $plural = self::_escape_quoted_string($second_param[1]);
  134. l10n_scanner::process_message(array("one" => $singular, "other" => $plural), $cache);
  135. } else {
  136. if (is_array($first_param) && $first_param[0] == T_CONSTANT_ENCAPSED_STRING) {
  137. $errors[$first_param[2]] = var_export(
  138. array($function_name, $parens, $first_param,
  139. $first_separator, $second_param, $next_token), 1);
  140. } else {
  141. // t2() found, but inside is something which is not a string literal. That's fine.
  142. }
  143. }
  144. }
  145. }
  146. return $errors;
  147. }
  148. /**
  149. * Escape quotes in a strings depending on the surrounding
  150. * quote type used.
  151. *
  152. * @param $str The strings to escape
  153. */
  154. private static function _escape_quoted_string($str) {
  155. $quo = substr($str, 0, 1);
  156. $str = substr($str, 1, -1);
  157. if ($quo == '"') {
  158. $str = stripcslashes($str);
  159. } else {
  160. $str = strtr($str, array("\\'" => "'", "\\\\" => "\\"));
  161. }
  162. return $str;
  163. }
  164. }