PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/gallery/helpers/l10n_client.php

https://github.com/zcopley/gallery3
PHP | 323 lines | 221 code | 36 blank | 66 comment | 23 complexity | 12511fd0b9ded8d5e1b01c0ffc6ab7e2 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-2010 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. class l10n_client_Core {
  21. private static function _server_url($path) {
  22. return "http://gallery.menalto.com/translations/$path";
  23. }
  24. static function server_api_key_url() {
  25. return self::_server_url("userkey/" . self::client_token());
  26. }
  27. static function client_token() {
  28. return md5("l10n_client_client_token" . access::private_key());
  29. }
  30. static function api_key($api_key=null) {
  31. if ($api_key !== null) {
  32. module::set_var("gallery", "l10n_client_key", $api_key);
  33. }
  34. return module::get_var("gallery", "l10n_client_key", "");
  35. }
  36. static function server_uid($api_key=null) {
  37. $api_key = $api_key == null ? l10n_client::api_key() : $api_key;
  38. $parts = explode(":", $api_key);
  39. return empty($parts) ? 0 : $parts[0];
  40. }
  41. private static function _sign($payload, $api_key=null) {
  42. $api_key = $api_key == null ? l10n_client::api_key() : $api_key;
  43. return md5($api_key . $payload . l10n_client::client_token());
  44. }
  45. static function validate_api_key($api_key) {
  46. $version = "1.0";
  47. $url = self::_server_url("status");
  48. $signature = self::_sign($version, $api_key);
  49. try {
  50. list ($response_data, $response_status) = remote::post(
  51. $url, array("version" => $version,
  52. "client_token" => l10n_client::client_token(),
  53. "signature" => $signature,
  54. "uid" => l10n_client::server_uid($api_key)));
  55. } catch (ErrorException $e) {
  56. // Log the error, but then return a "can't make connection" error
  57. Kohana_Log::add("error", $e->getMessage() . "\n" . $e->getTraceAsString());
  58. }
  59. if (!isset($response_data) && !isset($response_status)) {
  60. return array(false, false);
  61. }
  62. if (!remote::success($response_status)) {
  63. return array(true, false);
  64. }
  65. return array(true, true);
  66. }
  67. /**
  68. * Fetches translations for l10n messages. Must be called repeatedly
  69. * until 0 is returned (which is a countdown indicating progress).
  70. *
  71. * @param $num_fetched in/out parameter to specify which batch of
  72. * messages to fetch translations for.
  73. * @return The number of messages for which we didn't fetch
  74. * translations for.
  75. */
  76. static function fetch_updates(&$num_fetched) {
  77. $request = new stdClass();
  78. $request->locales = array();
  79. $request->messages = new stdClass();
  80. $locales = locales::installed();
  81. foreach ($locales as $locale => $locale_data) {
  82. $request->locales[] = $locale;
  83. }
  84. // See the server side code for how we arrive at this
  85. // number as a good limit for #locales * #messages.
  86. $max_messages = 2000 / count($locales);
  87. $num_messages = 0;
  88. $rows = db::build()
  89. ->select("key", "locale", "revision", "translation")
  90. ->from("incoming_translations")
  91. ->order_by("key")
  92. ->limit(1000000) // ignore, just there to satisfy SQL syntax
  93. ->offset($num_fetched)
  94. ->execute();
  95. $num_remaining = $rows->count();
  96. foreach ($rows as $row) {
  97. if (!isset($request->messages->{$row->key})) {
  98. if ($num_messages >= $max_messages) {
  99. break;
  100. }
  101. $request->messages->{$row->key} = 1;
  102. $num_messages++;
  103. }
  104. if (!empty($row->revision) && !empty($row->translation) &&
  105. isset($locales[$row->locale])) {
  106. if (!is_object($request->messages->{$row->key})) {
  107. $request->messages->{$row->key} = new stdClass();
  108. }
  109. $request->messages->{$row->key}->{$row->locale} = (int) $row->revision;
  110. }
  111. $num_fetched++;
  112. $num_remaining--;
  113. }
  114. // @todo Include messages from outgoing_translations?
  115. if (!$num_messages) {
  116. return $num_remaining;
  117. }
  118. $request_data = json_encode($request);
  119. $url = self::_server_url("fetch");
  120. list ($response_data, $response_status) = remote::post($url, array("data" => $request_data));
  121. if (!remote::success($response_status)) {
  122. throw new Exception("@todo TRANSLATIONS_FETCH_REQUEST_FAILED " . $response_status);
  123. }
  124. if (empty($response_data)) {
  125. return $num_remaining;
  126. }
  127. $response = json_decode($response_data);
  128. // Response format (JSON payload):
  129. // [{key:<key_1>, translation: <JSON encoded translation>, rev:<rev>, locale:<locale>},
  130. // {key:<key_2>, ...}
  131. // ]
  132. foreach ($response as $message_data) {
  133. // @todo Better input validation
  134. if (empty($message_data->key) || empty($message_data->translation) ||
  135. empty($message_data->locale) || empty($message_data->rev)) {
  136. throw new Exception("@todo TRANSLATIONS_FETCH_REQUEST_FAILED: Invalid response data");
  137. }
  138. $key = $message_data->key;
  139. $locale = $message_data->locale;
  140. $revision = $message_data->rev;
  141. $translation = json_decode($message_data->translation);
  142. if (!is_string($translation)) {
  143. // Normalize stdclass to array
  144. $translation = (array) $translation;
  145. }
  146. $translation = serialize($translation);
  147. // @todo Should we normalize the incoming_translations table into messages(id, key, message)
  148. // and incoming_translations(id, translation, locale, revision)? Or just allow
  149. // incoming_translations.message to be NULL?
  150. $locale = $message_data->locale;
  151. $entry = ORM::factory("incoming_translation")
  152. ->where("key", "=", $key)
  153. ->where("locale", "=", $locale)
  154. ->find();
  155. if (!$entry->loaded()) {
  156. // @todo Load a message key -> message (text) dict into memory outside of this loop
  157. $root_entry = ORM::factory("incoming_translation")
  158. ->where("key", "=", $key)
  159. ->where("locale", "=", "root")
  160. ->find();
  161. $entry->key = $key;
  162. $entry->message = $root_entry->message;
  163. $entry->locale = $locale;
  164. }
  165. $entry->revision = $revision;
  166. $entry->translation = $translation;
  167. $entry->save();
  168. }
  169. return $num_remaining;
  170. }
  171. static function submit_translations() {
  172. // Request format (HTTP POST):
  173. // client_token = <client_token>
  174. // uid = <l10n server user id>
  175. // signature = md5(user_api_key($uid, $client_token) . $data . $client_token))
  176. // data = // JSON payload
  177. //
  178. // {<key_1>: {message: <JSON encoded message>
  179. // translations: {<locale_1>: <JSON encoded translation>,
  180. // <locale_2>: ...}},
  181. // <key_2>: {...}
  182. // }
  183. // @todo Batch requests (max request size)
  184. // @todo include base_revision in submission / how to handle resubmissions / edit fights?
  185. $request = new stdClass();
  186. foreach (db::build()
  187. ->select("key", "message", "locale", "base_revision", "translation")
  188. ->from("outgoing_translations")
  189. ->execute() as $row) {
  190. $key = $row->key;
  191. if (!isset($request->{$key})) {
  192. $request->{$key} = new stdClass();
  193. $request->{$key}->translations = new stdClass();
  194. $request->{$key}->message = json_encode(unserialize($row->message));
  195. }
  196. $request->{$key}->translations->{$row->locale} = json_encode(unserialize($row->translation));
  197. }
  198. // @todo reduce memory consumption, e.g. free $request
  199. $request_data = json_encode($request);
  200. $url = self::_server_url("submit");
  201. $signature = self::_sign($request_data);
  202. list ($response_data, $response_status) = remote::post(
  203. $url, array("data" => $request_data,
  204. "client_token" => l10n_client::client_token(),
  205. "signature" => $signature,
  206. "uid" => l10n_client::server_uid()));
  207. if (!remote::success($response_status)) {
  208. throw new Exception("@todo TRANSLATIONS_SUBMISSION_FAILED " . $response_status);
  209. }
  210. if (empty($response_data)) {
  211. return;
  212. }
  213. $response = json_decode($response_data);
  214. // Response format (JSON payload):
  215. // [{key:<key_1>, locale:<locale_1>, rev:<rev_1>, status:<rejected|accepted|pending>},
  216. // {key:<key_2>, ...}
  217. // ]
  218. // @todo Move messages out of outgoing into incoming, using new rev?
  219. // @todo show which messages have been rejected / are pending?
  220. }
  221. /**
  222. * Plural forms.
  223. */
  224. static function plural_forms($locale) {
  225. $parts = explode('_', $locale);
  226. $language = $parts[0];
  227. // Data from CLDR 1.6 (http://unicode.org/cldr/data/common/supplemental/plurals.xml).
  228. // Docs: http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html
  229. switch ($language) {
  230. case 'az':
  231. case 'fa':
  232. case 'hu':
  233. case 'ja':
  234. case 'ko':
  235. case 'my':
  236. case 'to':
  237. case 'tr':
  238. case 'vi':
  239. case 'yo':
  240. case 'zh':
  241. case 'bo':
  242. case 'dz':
  243. case 'id':
  244. case 'jv':
  245. case 'ka':
  246. case 'km':
  247. case 'kn':
  248. case 'ms':
  249. case 'th':
  250. return array('other');
  251. case 'ar':
  252. return array('zero', 'one', 'two', 'few', 'many', 'other');
  253. case 'lv':
  254. return array('zero', 'one', 'other');
  255. case 'ga':
  256. case 'se':
  257. case 'sma':
  258. case 'smi':
  259. case 'smj':
  260. case 'smn':
  261. case 'sms':
  262. return array('one', 'two', 'other');
  263. case 'ro':
  264. case 'mo':
  265. case 'lt':
  266. case 'cs':
  267. case 'sk':
  268. case 'pl':
  269. return array('one', 'few', 'other');
  270. case 'hr':
  271. case 'ru':
  272. case 'sr':
  273. case 'uk':
  274. case 'be':
  275. case 'bs':
  276. case 'sh':
  277. case 'mt':
  278. return array('one', 'few', 'many', 'other');
  279. case 'sl':
  280. return array('one', 'two', 'few', 'other');
  281. case 'cy':
  282. return array('one', 'two', 'many', 'other');
  283. default: // en, de, etc.
  284. return array('one', 'other');
  285. }
  286. }
  287. }