sfMessageSource_SQLite.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. /**
  3. * sfMessageSource_SQLite 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_SQLite.class.php 9128 2008-05-21 00:58:19Z Carl.Vondrick $
  16. * @package symfony
  17. * @subpackage i18n
  18. */
  19. /**
  20. * sfMessageSource_SQLite class.
  21. *
  22. * Retrieve the message translation from a SQLite database.
  23. *
  24. * See the MessageSource::factory() method to instantiate this class.
  25. *
  26. * SQLite schema:
  27. *
  28. * CREATE TABLE catalogue (
  29. * cat_id INTEGER PRIMARY KEY,
  30. * name VARCHAR NOT NULL,
  31. * source_lang VARCHAR,
  32. * target_lang VARCHAR,
  33. * date_created INT,
  34. * date_modified INT,
  35. * author VARCHAR);
  36. *
  37. * CREATE TABLE trans_unit (
  38. * msg_id INTEGER PRIMARY KEY,
  39. * cat_id INTEGER NOT NULL DEFAULT '1',
  40. * id VARCHAR,
  41. * source TEXT,
  42. * target TEXT,
  43. * comments TEXT,
  44. * date_added INT,
  45. * date_modified INT,
  46. * author VARCHAR,
  47. * translated INT(1) NOT NULL DEFAULT '0');
  48. *
  49. * Propel schema (in .xml format):
  50. *
  51. * <database ...>
  52. * ...
  53. * <table name="catalogue">
  54. * <column name="cat_id" type="integer" required="true" primaryKey="true" autoincrement="true" />
  55. * <column name="name" type="varchar" size="100" />
  56. * <column name="source_lang" type="varchar" size="100" />
  57. * <column name="target_lang" type="varchar" size="100" />
  58. * <column name="date_created" type="timestamp" />
  59. * <column name="date_modified" type="timestamp" />
  60. * <column name="author" type="varchar" size="255" />
  61. * </table>
  62. *
  63. * <table name="trans_unit">
  64. * <column name="msg_id" type="integer" required="true" primaryKey="true" autoincrement="true" />
  65. * <column name="cat_id" type="integer" />
  66. * <foreign-key foreignTable="catalogue" onDelete="cascade">
  67. * <reference local="cat_id" foreign="cat_id"/>
  68. * </foreign-key>
  69. * <column name="id" type="varchar" size="255" />
  70. * <column name="source" type="longvarchar" />
  71. * <column name="target" type="longvarchar" />
  72. * <column name="comments" type="longvarchar" />
  73. * <column name="date_created" type="timestamp" />
  74. * <column name="date_modified" type="timestamp" />
  75. * <column name="author" type="varchar" size="255" />
  76. * <column name="translated" type="integer" />
  77. * </table>
  78. * ...
  79. * </database>
  80. *
  81. * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
  82. * @version v1.0, last update on Fri Dec 24 16:58:58 EST 2004
  83. * @package symfony
  84. * @subpackage i18n
  85. */
  86. class sfMessageSource_SQLite extends sfMessageSource_Database
  87. {
  88. /**
  89. * The SQLite datasource, the filename of the database.
  90. * @var string
  91. */
  92. protected $source;
  93. /**
  94. * Constructor.
  95. * Creates a new message source using SQLite.
  96. * @see MessageSource::factory();
  97. * @param string $source SQLite datasource, in PEAR's DB DSN format.
  98. */
  99. function __construct($source)
  100. {
  101. $dsn = $this->parseDSN((string) $source);
  102. $this->source = $dsn['database'];
  103. }
  104. /**
  105. * Gets an array of messages for a particular catalogue and cultural variant.
  106. *
  107. * @param string $variant the catalogue name + variant
  108. * @return array translation messages.
  109. */
  110. public function &loadData($variant)
  111. {
  112. $variant = sqlite_escape_string($variant);
  113. $statement =
  114. "SELECT t.id, t.source, t.target, t.comments
  115. FROM trans_unit t, catalogue c
  116. WHERE c.cat_id = t.cat_id
  117. AND c.name = '{$variant}'
  118. ORDER BY id ASC";
  119. $db = sqlite_open($this->source);
  120. $rs = sqlite_query($statement, $db);
  121. $result = array();
  122. while($row = sqlite_fetch_array($rs, SQLITE_NUM))
  123. {
  124. $source = $row[1];
  125. $result[$source][] = $row[2]; //target
  126. $result[$source][] = $row[0]; //id
  127. $result[$source][] = $row[3]; //comments
  128. }
  129. sqlite_close($db);
  130. return $result;
  131. }
  132. /**
  133. * Gets the last modified unix-time for this particular catalogue+variant.
  134. * We need to query the database to get the date_modified.
  135. *
  136. * @param string $source catalogue+variant
  137. * @return int last modified in unix-time format.
  138. */
  139. protected function getLastModified($source)
  140. {
  141. $source = sqlite_escape_string($source);
  142. $db = sqlite_open($this->source);
  143. $rs = sqlite_query("SELECT date_modified FROM catalogue WHERE name = '{$source}'", $db);
  144. $result = $rs ? intval(sqlite_fetch_single($rs)) : 0;
  145. sqlite_close($db);
  146. return $result;
  147. }
  148. /**
  149. * Checks if a particular catalogue+variant exists in the database.
  150. *
  151. * @param string $variant catalogue+variant
  152. * @return boolean true if the catalogue+variant is in the database, false otherwise.
  153. */
  154. public function isValidSource($variant)
  155. {
  156. $variant = sqlite_escape_string($variant);
  157. $db = sqlite_open($this->source);
  158. $rs = sqlite_query("SELECT COUNT(*) FROM catalogue WHERE name = '{$variant}'", $db);
  159. $result = $rs && intval(sqlite_fetch_single($rs));
  160. sqlite_close($db);
  161. return $result;
  162. }
  163. /**
  164. * Retrieves catalogue details, array($cat_id, $variant, $count).
  165. *
  166. * @param string $catalogue catalogue
  167. * @return array catalogue details, array($cat_id, $variant, $count).
  168. */
  169. protected function getCatalogueDetails($catalogue = 'messages')
  170. {
  171. if (empty($catalogue))
  172. {
  173. $catalogue = 'messages';
  174. }
  175. $variant = $catalogue.'.'.$this->culture;
  176. $name = sqlite_escape_string($this->getSource($variant));
  177. $db = sqlite_open($this->source);
  178. $rs = sqlite_query("SELECT cat_id FROM catalogue WHERE name = '{$name}'", $db);
  179. if (sqlite_num_rows($rs) != 1)
  180. {
  181. return false;
  182. }
  183. $cat_id = intval(sqlite_fetch_single($rs));
  184. // first get the catalogue ID
  185. $rs = sqlite_query("SELECT count(msg_id) FROM trans_unit WHERE cat_id = {$cat_id}", $db);
  186. $count = intval(sqlite_fetch_single($rs));
  187. sqlite_close($db);
  188. return array($cat_id, $variant, $count);
  189. }
  190. /**
  191. * Updates the catalogue last modified time.
  192. *
  193. * @return boolean true if updated, false otherwise.
  194. */
  195. protected function updateCatalogueTime($cat_id, $variant, $db)
  196. {
  197. $time = time();
  198. $result = sqlite_query("UPDATE catalogue SET date_modified = {$time} WHERE cat_id = {$cat_id}", $db);
  199. if ($this->cache)
  200. {
  201. $this->cache->remove($variant.':'.$this->culture);
  202. }
  203. return $result;
  204. }
  205. /**
  206. * Saves the list of untranslated blocks to the translation source.
  207. * If the translation was not found, you should add those
  208. * strings to the translation source via the <b>append()</b> method.
  209. *
  210. * @param string $catalogue the catalogue to add to
  211. * @return boolean true if saved successfuly, false otherwise.
  212. */
  213. function save($catalogue = 'messages')
  214. {
  215. $messages = $this->untranslated;
  216. if (count($messages) <= 0)
  217. {
  218. return false;
  219. }
  220. $details = $this->getCatalogueDetails($catalogue);
  221. if ($details)
  222. {
  223. list($cat_id, $variant, $count) = $details;
  224. }
  225. else
  226. {
  227. return false;
  228. }
  229. if ($cat_id <= 0)
  230. {
  231. return false;
  232. }
  233. $inserted = 0;
  234. $db = sqlite_open($this->source);
  235. $time = time();
  236. foreach ($messages as $message)
  237. {
  238. $message = sqlite_escape_string($message);
  239. if (sqlite_query("INSERT INTO trans_unit (cat_id, id, source, date_added) VALUES ({$cat_id}, {$count}, '{$message}', $time)", $db))
  240. {
  241. $count++;
  242. $inserted++;
  243. }
  244. }
  245. if ($inserted > 0)
  246. {
  247. $this->updateCatalogueTime($cat_id, $variant, $db);
  248. }
  249. sqlite_close($db);
  250. return $inserted > 0;
  251. }
  252. /**
  253. * Updates the translation.
  254. *
  255. * @param string $text the source string.
  256. * @param string $target the new translation string.
  257. * @param string $comments comments
  258. * @param string $catalogue the catalogue of the translation.
  259. * @return boolean true if translation was updated, false otherwise.
  260. */
  261. function update($text, $target, $comments, $catalogue = 'messages')
  262. {
  263. $details = $this->getCatalogueDetails($catalogue);
  264. if ($details)
  265. {
  266. list($cat_id, $variant, $count) = $details;
  267. }
  268. else
  269. {
  270. return false;
  271. }
  272. $comments = sqlite_escape_string($comments);
  273. $target = sqlite_escape_string($target);
  274. $text = sqlite_escape_string($text);
  275. $time = time();
  276. $db = sqlite_open($this->source);
  277. sqlite_query("UPDATE trans_unit SET target = '{$target}', comments = '{$comments}', date_modified = '{$time}' WHERE cat_id = {$cat_id} AND source = '{$text}'", $db);
  278. if (sqlite_changes($db))
  279. {
  280. $this->updateCatalogueTime($cat_id, $variant, $db);
  281. $updated = true;
  282. }
  283. else
  284. {
  285. $updated = false;
  286. }
  287. sqlite_close($db);
  288. return $updated;
  289. }
  290. /**
  291. * Deletes a particular message from the specified catalogue.
  292. *
  293. * @param string $source the source message to delete.
  294. * @param string $catalogue the catalogue to delete from.
  295. * @return boolean true if deleted, false otherwise.
  296. */
  297. function delete($message, $catalogue = 'messages')
  298. {
  299. $details = $this->getCatalogueDetails($catalogue);
  300. if ($details)
  301. {
  302. list($cat_id, $variant, $count) = $details;
  303. }
  304. else
  305. {
  306. return false;
  307. }
  308. $db = sqlite_open($this->source);
  309. $text = sqlite_escape_string($message);
  310. sqlite_query("DELETE FROM trans_unit WHERE cat_id = {$cat_id} AND source = '{$message}'", $db);
  311. if (sqlite_changes($db))
  312. {
  313. $this->updateCatalogueTime($cat_id, $variant, $db);
  314. $deleted = true;
  315. }
  316. else
  317. {
  318. $deleted = false;
  319. }
  320. sqlite_close($db);
  321. return $deleted;
  322. }
  323. /**
  324. * Returns a list of catalogue as key and all it variants as value.
  325. *
  326. * @return array list of catalogues
  327. */
  328. function catalogues()
  329. {
  330. $db = sqlite_open($this->source);
  331. $statement = 'SELECT name FROM catalogue ORDER BY name';
  332. $rs = sqlite_query($statement, $db);
  333. $result = array();
  334. while ($row = sqlite_fetch_array($rs, SQLITE_NUM))
  335. {
  336. $details = explode('.', $row[0]);
  337. if (!isset($details[1]))
  338. {
  339. $details[1] = null;
  340. }
  341. $result[] = $details;
  342. }
  343. sqlite_close($db);
  344. return $result;
  345. }
  346. }