1. sfMessageSource_SQLite.class.php
  2. /** * sfMessageSource_SQLite class. * * Retrieve the message translation from a SQLite database. * * See the MessageSource::factory() method to instantiate this class. * * SQLite schema: * * CREATE TABLE catalogue ( * cat_id INTEGER PRIMARY KEY, * name VARCHAR NOT NULL, * source_lang VARCHAR, * target_lang VARCHAR, * date_created INT, * date_modified INT, * author VARCHAR); * * CREATE TABLE trans_unit ( * msg_id INTEGER PRIMARY KEY, * cat_id INTEGER NOT NULL DEFAULT '1', * id VARCHAR, * source TEXT, * target TEXT, * comments TEXT, * date_added INT, * date_modified INT, * author VARCHAR, * translated INT(1) NOT NULL DEFAULT '0'); * * Propel schema (in .xml format): * * * ... * * * * * * * * *
    * * * * * * * * * * * * * * * *
    * ... *
    * * @author Xiang Wei Zhuo * @version v1.0, last update on Fri Dec 24 16:58:58 EST 2004 * @package symfony * @subpackage i18n */
  3. class sfMessageSource_SQLite extends sfMessageSource_Database
  4. {
  5. /**
  6. * The SQLite datasource, the filename of the database.
  7. * @var string
  8. */
  9. protected $source;
  10. /**
  11. * Constructor.
  12. * Creates a new message source using SQLite.
  13. * @see MessageSource::factory();
  14. * @param string $source SQLite datasource, in PEAR's DB DSN format.
  15. */
  16. function __construct($source)
  17. {
  18. $dsn = $this->parseDSN((string) $source);
  19. $this->source = $dsn['database'];
  20. }
  21. /**
  22. * Gets an array of messages for a particular catalogue and cultural variant.
  23. *
  24. * @param string $variant the catalogue name + variant
  25. * @return array translation messages.
  26. */
  27. public function &loadData($variant)
  28. {
  29. $variant = sqlite_escape_string($variant);
  30. $statement =
  31. "SELECT t.id, t.source, t.target, t.comments
  32. FROM trans_unit t, catalogue c
  33. WHERE c.cat_id = t.cat_id
  34. AND c.name = '{$variant}'
  35. ORDER BY id ASC";
  36. $db = sqlite_open($this->source);
  37. $rs = sqlite_query($statement, $db);
  38. $result = array();
  39. while($row = sqlite_fetch_array($rs, SQLITE_NUM))
  40. {
  41. $source = $row[1];
  42. $result[$source][] = $row[2]; //target
  43. $result[$source][] = $row[0]; //id
  44. $result[$source][] = $row[3]; //comments
  45. }
  46. sqlite_close($db);
  47. return $result;
  48. }
  49. /**
  50. * Gets the last modified unix-time for this particular catalogue+variant.
  51. * We need to query the database to get the date_modified.
  52. *
  53. * @param string $source catalogue+variant
  54. * @return int last modified in unix-time format.
  55. */
  56. protected function getLastModified($source)
  57. {
  58. $source = sqlite_escape_string($source);
  59. $db = sqlite_open($this->source);
  60. $rs = sqlite_query("SELECT date_modified FROM catalogue WHERE name = '{$source}'", $db);
  61. $result = $rs ? intval(sqlite_fetch_single($rs)) : 0;
  62. sqlite_close($db);
  63. return $result;
  64. }
  65. /**
  66. * Checks if a particular catalogue+variant exists in the database.
  67. *
  68. * @param string $variant catalogue+variant
  69. * @return boolean true if the catalogue+variant is in the database, false otherwise.
  70. */
  71. public function isValidSource($variant)
  72. {
  73. $variant = sqlite_escape_string($variant);
  74. $db = sqlite_open($this->source);
  75. $rs = sqlite_query("SELECT COUNT(*) FROM catalogue WHERE name = '{$variant}'", $db);
  76. $result = $rs && intval(sqlite_fetch_single($rs));
  77. sqlite_close($db);
  78. return $result;
  79. }
  80. /**
  81. * Retrieves catalogue details, array($cat_id, $variant, $count).
  82. *
  83. * @param string $catalogue catalogue
  84. * @return array catalogue details, array($cat_id, $variant, $count).
  85. */
  86. protected function getCatalogueDetails($catalogue = 'messages')
  87. {
  88. if (empty($catalogue))
  89. {
  90. $catalogue = 'messages';
  91. }
  92. $variant = $catalogue.'.'.$this->culture;
  93. $name = sqlite_escape_string($this->getSource($variant));
  94. $db = sqlite_open($this->source);
  95. $rs = sqlite_query("SELECT cat_id FROM catalogue WHERE name = '{$name}'", $db);
  96. if (sqlite_num_rows($rs) != 1)
  97. {
  98. return false;
  99. }
  100. $cat_id = intval(sqlite_fetch_single($rs));
  101. // first get the catalogue ID
  102. $rs = sqlite_query("SELECT count(msg_id) FROM trans_unit WHERE cat_id = {$cat_id}", $db);
  103. $count = intval(sqlite_fetch_single($rs));
  104. sqlite_close($db);
  105. return array($cat_id, $variant, $count);
  106. }
  107. /**
  108. * Updates the catalogue last modified time.
  109. *
  110. * @return boolean true if updated, false otherwise.
  111. */
  112. protected function updateCatalogueTime($cat_id, $variant, $db)
  113. {
  114. $time = time();
  115. $result = sqlite_query("UPDATE catalogue SET date_modified = {$time} WHERE cat_id = {$cat_id}", $db);
  116. if ($this->cache)
  117. {
  118. $this->cache->remove($variant.':'.$this->culture);
  119. }
  120. return $result;
  121. }
  122. /**
  123. * Saves the list of untranslated blocks to the translation source.
  124. * If the translation was not found, you should add those
  125. * strings to the translation source via the <b>append()</b> method.
  126. *
  127. * @param string $catalogue the catalogue to add to
  128. * @return boolean true if saved successfuly, false otherwise.
  129. */
  130. function save($catalogue = 'messages')
  131. {
  132. $messages = $this->untranslated;
  133. if (count($messages) <= 0)
  134. {
  135. return false;
  136. }
  137. $details = $this->getCatalogueDetails($catalogue);
  138. if ($details)
  139. {
  140. list($cat_id, $variant, $count) = $details;
  141. }
  142. else
  143. {
  144. return false;
  145. }
  146. if ($cat_id <= 0)
  147. {
  148. return false;
  149. }
  150. $inserted = 0;
  151. $db = sqlite_open($this->source);
  152. $time = time();
  153. foreach ($messages as $message)
  154. {
  155. $message = sqlite_escape_string($message);
  156. if (sqlite_query("INSERT INTO trans_unit (cat_id, id, source, date_added) VALUES ({$cat_id}, {$count}, '{$message}', $time)", $db))
  157. {
  158. $count++;
  159. $inserted++;
  160. }
  161. }
  162. if ($inserted > 0)
  163. {
  164. $this->updateCatalogueTime($cat_id, $variant, $db);
  165. }
  166. sqlite_close($db);
  167. return $inserted > 0;
  168. }
  169. /**
  170. * Updates the translation.
  171. *
  172. * @param string $text the source string.
  173. * @param string $target the new translation string.
  174. * @param string $comments comments
  175. * @param string $catalogue the catalogue of the translation.
  176. * @return boolean true if translation was updated, false otherwise.
  177. */
  178. function update($text, $target, $comments, $catalogue = 'messages')
  179. {
  180. $details = $this->getCatalogueDetails($catalogue);
  181. if ($details)
  182. {
  183. list($cat_id, $variant, $count) = $details;
  184. }
  185. else
  186. {
  187. return false;
  188. }
  189. $comments = sqlite_escape_string($comments);
  190. $target = sqlite_escape_string($target);
  191. $text = sqlite_escape_string($text);
  192. $time = time();
  193. $db = sqlite_open($this->source);
  194. sqlite_query("UPDATE trans_unit SET target = '{$target}', comments = '{$comments}', date_modified = '{$time}' WHERE cat_id = {$cat_id} AND source = '{$text}'", $db);
  195. if (sqlite_changes($db))
  196. {
  197. $this->updateCatalogueTime($cat_id, $variant, $db);
  198. $updated = true;
  199. }
  200. else
  201. {
  202. $updated = false;
  203. }
  204. sqlite_close($db);
  205. return $updated;
  206. }
  207. /**
  208. * Deletes a particular message from the specified catalogue.
  209. *
  210. * @param string $message the source message to delete.
  211. * @param string $catalogue the catalogue to delete from.
  212. * @return boolean true if deleted, false otherwise.
  213. */
  214. function delete($message, $catalogue = 'messages')
  215. {
  216. $details = $this->getCatalogueDetails($catalogue);
  217. if ($details)
  218. {
  219. list($cat_id, $variant, $count) = $details;
  220. }
  221. else
  222. {
  223. return false;
  224. }
  225. $db = sqlite_open($this->source);
  226. $text = sqlite_escape_string($message);
  227. sqlite_query("DELETE FROM trans_unit WHERE cat_id = {$cat_id} AND source = '{$message}'", $db);
  228. if (sqlite_changes($db))
  229. {
  230. $this->updateCatalogueTime($cat_id, $variant, $db);
  231. $deleted = true;
  232. }
  233. else
  234. {
  235. $deleted = false;
  236. }
  237. sqlite_close($db);
  238. return $deleted;
  239. }
  240. /**
  241. * Returns a list of catalogue as key and all it variants as value.
  242. *
  243. * @return array list of catalogues
  244. */
  245. function catalogues()
  246. {
  247. $db = sqlite_open($this->source);
  248. $statement = 'SELECT name FROM catalogue ORDER BY name';
  249. $rs = sqlite_query($statement, $db);
  250. $result = array();
  251. while ($row = sqlite_fetch_array($rs, SQLITE_NUM))
  252. {
  253. $details = explode('.', $row[0]);
  254. if (!isset($details[1]))
  255. {
  256. $details[1] = null;
  257. }
  258. $result[] = $details;
  259. }
  260. sqlite_close($db);
  261. return $result;
  262. }
  263. }

Debug toolbar