PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/package/app/app/symfony/i18n/sfMessageSource_MySQL.class.php

https://bitbucket.org/pandaos/kaltura
PHP | 453 lines | 250 code | 72 blank | 131 comment | 31 complexity | 40a6b8c8ac27495d8863a403033b904f MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, BSD-3-Clause, LGPL-2.1, GPL-2.0, LGPL-3.0, JSON, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?php
  2. /**
  3. * sfMessageSource_MySQL class file.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the BSD License.
  7. *
  8. * Copyright(c) 2004 by Qiang Xue. All rights reserved.
  9. *
  10. * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
  11. * The latest version of PRADO can be obtained from:
  12. * {@link http://prado.sourceforge.net/}
  13. *
  14. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  15. * @version $Id: sfMessageSource_MySQL.class.php 3148 2007-01-04 19:34:28Z fabien $
  16. * @package symfony
  17. * @subpackage i18n
  18. */
  19. /**
  20. * Get the I18N utility file, contains the DSN parser.
  21. */
  22. require_once(dirname(__FILE__).'/util.php');
  23. /**
  24. * sfMessageSource_MySQL class.
  25. *
  26. * Retrieve the message translation from a MySQL database.
  27. *
  28. * See the MessageSource::factory() method to instantiate this class.
  29. *
  30. * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
  31. * @version v1.0, last update on Fri Dec 24 16:58:58 EST 2004
  32. * @package System.I18N.core
  33. */
  34. class sfMessageSource_MySQL extends sfMessageSource
  35. {
  36. /**
  37. * The datasource string, full DSN to the database.
  38. * @var string
  39. */
  40. protected $source;
  41. /**
  42. * The DSN array property, parsed by PEAR's DB DSN parser.
  43. * @var array
  44. */
  45. protected $dns;
  46. /**
  47. * A resource link to the database
  48. * @var db
  49. */
  50. protected $db;
  51. /**
  52. * Constructor.
  53. * Create a new message source using MySQL.
  54. *
  55. * @param string MySQL datasource, in PEAR's DB DSN format.
  56. * @see MessageSource::factory();
  57. */
  58. function __construct($source)
  59. {
  60. $this->source = (string)$source;
  61. $this->dns = parseDSN($this->source);
  62. $this->db = $this->connect();
  63. }
  64. /**
  65. * Destructor, close the database connection.
  66. */
  67. function __destruct()
  68. {
  69. @mysql_close($this->db);
  70. }
  71. /**
  72. * Connect to the MySQL datasource
  73. *
  74. * @return resource MySQL connection.
  75. * @throws sfException, connection and database errors.
  76. */
  77. protected function connect()
  78. {
  79. $dsninfo = $this->dns;
  80. if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix')
  81. {
  82. $dbhost = ':'.$dsninfo['socket'];
  83. }
  84. else
  85. {
  86. $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
  87. if (!empty($dsninfo['port']))
  88. {
  89. $dbhost = ':' . $dsninfo['socket'];
  90. }
  91. }
  92. $user = $dsninfo['username'];
  93. $pw = $dsninfo['password'];
  94. $connect_function = 'mysql_connect';
  95. if ($dbhost && $user && $pw)
  96. {
  97. $conn = @$connect_function($dbhost, $user, $pw);
  98. }
  99. elseif ($dbhost && $user)
  100. {
  101. $conn = @$connect_function($dbhost, $user);
  102. }
  103. elseif ($dbhost)
  104. {
  105. $conn = @$connect_function($dbhost);
  106. }
  107. else
  108. {
  109. $conn = false;
  110. }
  111. if (empty($conn))
  112. {
  113. throw new sfException('Error in connecting to '.$dsninfo);
  114. }
  115. if ($dsninfo['database'])
  116. {
  117. if (!@mysql_select_db($dsninfo['database'], $conn))
  118. {
  119. throw new sfException('Error in connecting database, dns:'.$dsninfo);
  120. }
  121. }
  122. else
  123. {
  124. throw new sfException('Please provide a database for message translation.');
  125. }
  126. return $conn;
  127. }
  128. /**
  129. * Get the database connection.
  130. *
  131. * @return db database connection.
  132. */
  133. public function connection()
  134. {
  135. return $this->db;
  136. }
  137. /**
  138. * Get an array of messages for a particular catalogue and cultural
  139. * variant.
  140. *
  141. * @param string the catalogue name + variant
  142. * @return array translation messages.
  143. */
  144. protected function &loadData($variant)
  145. {
  146. $variant = mysql_escape_string($variant);
  147. $statement =
  148. "SELECT t.id, t.source, t.target, t.comments
  149. FROM trans_unit t, catalogue c
  150. WHERE c.cat_id = t.cat_id
  151. AND c.name = '{$variant}'
  152. ORDER BY id ASC";
  153. $rs = mysql_query($statement,$this->db);
  154. $result = array();
  155. while ($row = mysql_fetch_array($rs, MYSQL_NUM))
  156. {
  157. $source = $row[1];
  158. $result[$source][] = $row[2]; //target
  159. $result[$source][] = $row[0]; //id
  160. $result[$source][] = $row[3]; //comments
  161. }
  162. return $result;
  163. }
  164. /**
  165. * Get the last modified unix-time for this particular catalogue+variant.
  166. * We need to query the database to get the date_modified.
  167. *
  168. * @param string catalogue+variant
  169. * @return int last modified in unix-time format.
  170. */
  171. protected function getLastModified($source)
  172. {
  173. $source = mysql_escape_string($source);
  174. $rs = mysql_query("SELECT date_modified FROM catalogue WHERE name = '{$source}'", $this->db);
  175. $result = $rs ? intval(mysql_result($rs, 0)) : 0;
  176. return $result;
  177. }
  178. /**
  179. * Check if a particular catalogue+variant exists in the database.
  180. *
  181. * @param string catalogue+variant
  182. * @return boolean true if the catalogue+variant is in the database, false otherwise.
  183. */
  184. protected function isValidSource($variant)
  185. {
  186. $variant = mysql_escape_string ($variant);
  187. $rs = mysql_query("SELECT COUNT(*) FROM catalogue WHERE name = '{$variant}'", $this->db);
  188. $row = mysql_fetch_array($rs,MYSQL_NUM);
  189. $result = $row && $row[0] == '1';
  190. return $result;
  191. }
  192. /**
  193. * Get all the variants of a particular catalogue.
  194. *
  195. * @param string catalogue name
  196. * @return array list of all variants for this catalogue.
  197. */
  198. protected function getCatalogueList($catalogue)
  199. {
  200. $variants = explode('_', $this->culture);
  201. $catalogues = array($catalogue);
  202. $variant = null;
  203. for ($i = 0, $max = count($variants); $i < $max; $i++)
  204. {
  205. if (strlen($variants[$i]) > 0)
  206. {
  207. $variant .= $variant ? '_'.$variants[$i] : $variants[$i];
  208. $catalogues[] = $catalogue.'.'.$variant;
  209. }
  210. }
  211. return array_reverse($catalogues);
  212. }
  213. /**
  214. * Retrieve catalogue details, array($cat_id, $variant, $count).
  215. *
  216. * @param string catalogue
  217. * @return array catalogue details, array($cat_id, $variant, $count).
  218. */
  219. protected function getCatalogueDetails($catalogue = 'messages')
  220. {
  221. if (empty($catalogue))
  222. {
  223. $catalogue = 'messages';
  224. }
  225. $variant = $catalogue.'.'.$this->culture;
  226. $name = mysql_escape_string($this->getSource($variant));
  227. $rs = mysql_query("SELECT cat_id FROM catalogue WHERE name = '{$name}'", $this->db);
  228. if (mysql_num_rows($rs) != 1)
  229. {
  230. return false;
  231. }
  232. $cat_id = intval(mysql_result($rs, 0));
  233. // first get the catalogue ID
  234. $rs = mysql_query("SELECT count(msg_id) FROM trans_unit WHERE cat_id = {$cat_id}", $this->db);
  235. $count = intval(mysql_result($rs, 0));
  236. return array($cat_id, $variant, $count);
  237. }
  238. /**
  239. * Update the catalogue last modified time.
  240. *
  241. * @return boolean true if updated, false otherwise.
  242. */
  243. protected function updateCatalogueTime($cat_id, $variant)
  244. {
  245. $time = time();
  246. $result = mysql_query("UPDATE catalogue SET date_modified = {$time} WHERE cat_id = {$cat_id}", $this->db);
  247. if (!empty($this->cache))
  248. {
  249. $this->cache->clean($variant, $this->culture);
  250. }
  251. return $result;
  252. }
  253. /**
  254. * Save the list of untranslated blocks to the translation source.
  255. * If the translation was not found, you should add those
  256. * strings to the translation source via the <b>append()</b> method.
  257. *
  258. * @param string the catalogue to add to
  259. * @return boolean true if saved successfuly, false otherwise.
  260. */
  261. function save($catalogue = 'messages')
  262. {
  263. $messages = $this->untranslated;
  264. if (count($messages) <= 0)
  265. {
  266. return false;
  267. }
  268. $details = $this->getCatalogueDetails($catalogue);
  269. if ($details)
  270. {
  271. list($cat_id, $variant, $count) = $details;
  272. }
  273. else
  274. {
  275. return false;
  276. }
  277. if ($cat_id <= 0)
  278. {
  279. return false;
  280. }
  281. $inserted = 0;
  282. $time = time();
  283. foreach ($messages as $message)
  284. {
  285. $count++;
  286. $inserted++;
  287. $message = mysql_escape_string($message);
  288. $statement = "INSERT INTO trans_unit
  289. (cat_id,id,source,date_added) VALUES
  290. ({$cat_id}, {$count},'{$message}',$time)";
  291. mysql_query($statement, $this->db);
  292. }
  293. if ($inserted > 0)
  294. {
  295. $this->updateCatalogueTime($cat_id, $variant);
  296. }
  297. return $inserted > 0;
  298. }
  299. /**
  300. * Delete a particular message from the specified catalogue.
  301. *
  302. * @param string the source message to delete.
  303. * @param string the catalogue to delete from.
  304. * @return boolean true if deleted, false otherwise.
  305. */
  306. function delete($message, $catalogue = 'messages')
  307. {
  308. $details = $this->getCatalogueDetails($catalogue);
  309. if ($details)
  310. {
  311. list($cat_id, $variant, $count) = $details;
  312. }
  313. else
  314. {
  315. return false;
  316. }
  317. $text = mysql_escape_string($message);
  318. $statement = "DELETE FROM trans_unit WHERE cat_id = {$cat_id} AND source = '{$message}'";
  319. $deleted = false;
  320. mysql_query($statement, $this->db);
  321. if (mysql_affected_rows($this->db) == 1)
  322. {
  323. $deleted = $this->updateCatalogueTime($cat_id, $variant);
  324. }
  325. return $deleted;
  326. }
  327. /**
  328. * Update the translation.
  329. *
  330. * @param string the source string.
  331. * @param string the new translation string.
  332. * @param string comments
  333. * @param string the catalogue of the translation.
  334. * @return boolean true if translation was updated, false otherwise.
  335. */
  336. function update($text, $target, $comments, $catalogue = 'messages')
  337. {
  338. $details = $this->getCatalogueDetails($catalogue);
  339. if ($details)
  340. {
  341. list($cat_id, $variant, $count) = $details;
  342. }
  343. else
  344. {
  345. return false;
  346. }
  347. $comments = mysql_escape_string($comments);
  348. $target = mysql_escape_string($target);
  349. $text = mysql_escape_string($text);
  350. $time = time();
  351. $statement = "UPDATE trans_unit SET target = '{$target}', comments = '{$comments}', date_modified = '{$time}' WHERE cat_id = {$cat_id} AND source = '{$text}'";
  352. $updated = false;
  353. mysql_query($statement, $this->db);
  354. if (mysql_affected_rows($this->db) == 1)
  355. {
  356. $updated = $this->updateCatalogueTime($cat_id, $variant);
  357. }
  358. return $updated;
  359. }
  360. /**
  361. * Returns a list of catalogue as key and all it variants as value.
  362. *
  363. * @return array list of catalogues
  364. */
  365. function catalogues()
  366. {
  367. $statement = 'SELECT name FROM catalogue ORDER BY name';
  368. $rs = mysql_query($statement, $this->db);
  369. $result = array();
  370. while($row = mysql_fetch_array($rs, MYSQL_NUM))
  371. {
  372. $details = explode('.', $row[0]);
  373. if (!isset($details[1]))
  374. {
  375. $details[1] = null;
  376. }
  377. $result[] = $details;
  378. }
  379. return $result;
  380. }
  381. }