Development

/branches/1.0/lib/i18n/sfMessageSource_SQLite.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/i18n/sfMessageSource_SQLite.class.php

Revision 6806, 10.8 kB (checked in by fabien, 7 years ago)

fixed phpdoc package/subpackage entries

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /**
4  * sfMessageSource_SQLite class file.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the BSD License.
8  *
9  * Copyright(c) 2004 by Qiang Xue. All rights reserved.
10  *
11  * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
12  * The latest version of PRADO can be obtained from:
13  * {@link http://prado.sourceforge.net/}
14  *
15  * @author     Wei Zhuo <weizhuo[at]gmail[dot]com>
16  * @version    $Id$
17  * @package    symfony
18  * @subpackage i18n
19  */
20
21 /**
22  * Get the I18N utility file, contains the DSN parser.
23  */
24 require_once(dirname(__FILE__).'/util.php');
25
26 /**
27  * sfMessageSource_SQLite class.
28  *
29  * Retrieve the message translation from a SQLite database.
30  *
31  * See the MessageSource::factory() method to instantiate this class.
32  *
33  * SQLite schema:
34  *
35  * CREATE TABLE catalogue (
36  *   cat_id INTEGER PRIMARY KEY,
37  *   name VARCHAR NOT NULL,
38  *   source_lang VARCHAR ,
39  *   target_lang VARCHAR ,
40  *   date_created INT,
41  *   date_modified INT,
42  *   author VARCHAR);
43  *
44  * CREATE TABLE trans_unit (
45  *   msg_id INTEGER PRIMARY KEY,
46  *   cat_id INTEGER NOT NULL DEFAULT '1',
47  *   id VARCHAR,
48  *   source TEXT,
49  *   target TEXT,
50  *   comments TEXT,
51  *   date_added INT,
52  *   date_modified INT,
53  *   author VARCHAR,
54  *   translated INT(1) NOT NULL DEFAULT '0');
55  *
56  * Propel schema (in .xml format):
57  *
58  *  <database ...>
59  *    ...
60  *    <table name="catalogue">
61  *     <column name="cat_id" type="integer" required="true" primaryKey="true" autoincrement="true" />
62  *     <column name="name" type="varchar" size="100" />
63  *     <column name="source_lang" type="varchar" size="100" />
64  *     <column name="target_lang" type="varchar" size="100" />
65  *     <column name="date_created" type="timestamp" />
66  *     <column name="date_modified" type="timestamp" />
67  *     <column name="author" type="varchar" size="255" />
68  *    </table>
69  *
70  *    <table name="trans_unit">
71  *     <column name="msg_id" type="integer" required="true" primaryKey="true" autoincrement="true" />
72  *     <column name="cat_id" type="integer" />
73  *       <foreign-key foreignTable="catalogue" onDelete="cascade">
74  *         <reference local="cat_id" foreign="cat_id"/>
75  *       </foreign-key>
76  *     <column name="id" type="varchar" size="255" />
77  *     <column name="source" type="longvarchar" />
78  *     <column name="target" type="longvarchar" />
79  *     <column name="comments" type="longvarchar" />
80  *     <column name="date_created" type="timestamp" />
81  *     <column name="date_modified" type="timestamp" />
82  *     <column name="author" type="varchar" size="255" />
83  *     <column name="translated" type="integer" />
84  *    </table>
85  *    ...
86  *  </database>
87  *
88  * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
89  * @version v1.0, last update on Fri Dec 24 16:58:58 EST 2004
90  * @package    symfony
91  * @subpackage i18n
92  */
93 class sfMessageSource_SQLite extends sfMessageSource
94 {
95   /**
96    * The SQLite datasource, the filename of the database.
97    * @var string
98    */
99   protected $source;
100
101   /**
102    * Constructor.
103    * Creates a new message source using SQLite.
104    * @see MessageSource::factory();
105    * @param string SQLite datasource, in PEAR's DB DSN format.
106    */
107   function __construct($source)
108   {
109     $dsn = parseDSN((string) $source);
110     $this->source = $dsn['database'];
111   }
112
113   /**
114    * Gets an array of messages for a particular catalogue and cultural variant.
115    *
116    * @param string the catalogue name + variant
117    * @return array translation messages.
118    */
119   protected function &loadData($variant)
120   {
121     $variant = sqlite_escape_string($variant);
122
123     $statement =
124       "SELECT t.id, t.source, t.target, t.comments
125         FROM trans_unit t, catalogue c
126         WHERE c.cat_id =  t.cat_id
127           AND c.name = '{$variant}'
128         ORDER BY id ASC";
129
130     $db = sqlite_open($this->source);
131     $rs = sqlite_query($statement, $db);
132
133     $result = array();
134
135     while($row = sqlite_fetch_array($rs, SQLITE_NUM))
136     {
137       $source = $row[1];
138       $result[$source][] = $row[2]; //target
139       $result[$source][] = $row[0]; //id
140       $result[$source][] = $row[3]; //comments
141     }
142
143     sqlite_close($db);
144
145     return $result;
146   }
147
148   /**
149    * Gets the last modified unix-time for this particular catalogue+variant.
150    * We need to query the database to get the date_modified.
151    *
152    * @param string catalogue+variant
153    * @return int last modified in unix-time format.
154    */
155   protected function getLastModified($source)
156   {
157     $source = sqlite_escape_string($source);
158
159     $db = sqlite_open($this->source);
160
161     $rs = sqlite_query("SELECT date_modified FROM catalogue WHERE name = '{$source}'", $db);
162
163     $result = $rs ? intval(sqlite_fetch_single($rs)) : 0;
164
165     sqlite_close($db);
166
167     return $result;
168   }
169
170   /**
171    * Checks if a particular catalogue+variant exists in the database.
172    *
173    * @param string catalogue+variant
174    * @return boolean true if the catalogue+variant is in the database, false otherwise.
175    */
176   protected function isValidSource($variant)
177   {
178     $variant = sqlite_escape_string($variant);
179     $db = sqlite_open($this->source);
180     $rs = sqlite_query("SELECT COUNT(*) FROM catalogue WHERE name = '{$variant}'", $db);
181     $result = $rs && intval(sqlite_fetch_single($rs));
182     sqlite_close($db);
183
184     return $result;
185   }
186
187   /**
188    * Gets all the variants of a particular catalogue.
189    *
190    * @param string catalogue name
191    * @return array list of all variants for this catalogue.
192    */
193   protected function getCatalogueList($catalogue)
194   {
195     $variants = explode('_', $this->culture);
196
197     $catalogues = array($catalogue);
198
199     $variant = null;
200
201     for ($i = 0, $max = count($variants); $i < $max; $i++)
202     {
203       if (strlen($variants[$i]) > 0)
204       {
205         $variant .= ($variant) ? '_'.$variants[$i] : $variants[$i];
206         $catalogues[] = $catalogue.'.'.$variant;
207       }
208     }
209
210     return array_reverse($catalogues);
211   }
212
213   /**
214    * Retrieves 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
226     $variant = $catalogue.'.'.$this->culture;
227
228     $name = sqlite_escape_string($this->getSource($variant));
229
230     $db = sqlite_open($this->source);
231
232     $rs = sqlite_query("SELECT cat_id FROM catalogue WHERE name = '{$name}'", $db);
233
234     if (sqlite_num_rows($rs) != 1)
235     {
236       return false;
237     }
238
239     $cat_id = intval(sqlite_fetch_single($rs));
240
241     //first get the catalogue ID
242     $rs = sqlite_query("SELECT count(msg_id) FROM trans_unit WHERE cat_id = {$cat_id}", $db);
243
244     $count = intval(sqlite_fetch_single($rs));
245
246     sqlite_close($db);
247
248     return array($cat_id, $variant, $count);
249   }
250
251   /**
252    * Updates the catalogue last modified time.
253    *
254    * @return boolean true if updated, false otherwise.
255    */
256   protected function updateCatalogueTime($cat_id, $variant, $db)
257   {
258     $time = time();
259
260     $result = sqlite_query("UPDATE catalogue SET date_modified = {$time} WHERE cat_id = {$cat_id}", $db);
261
262     if (!empty($this->cache))
263     {
264       $this->cache->clean($variant, $this->culture);
265     }
266
267     return $result;
268   }
269
270   /**
271    * Saves the list of untranslated blocks to the translation source.
272    * If the translation was not found, you should add those
273    * strings to the translation source via the <b>append()</b> method.
274    *
275    * @param string the catalogue to add to
276    * @return boolean true if saved successfuly, false otherwise.
277    */
278   function save($catalogue='messages')
279   {
280     $messages = $this->untranslated;
281
282     if (count($messages) <= 0)
283     {
284       return false;
285     }
286
287     $details = $this->getCatalogueDetails($catalogue);
288
289     if ($details)
290     {
291       list($cat_id, $variant, $count) = $details;
292     }
293     else
294     {
295       return false;
296     }
297
298     if ($cat_id <= 0)
299     {
300       return false;
301     }
302     $inserted = 0;
303
304     $db = sqlite_open($this->source);
305     $time = time();
306
307     foreach ($messages as $message)
308     {
309       $message = sqlite_escape_string($message);
310       $statement = "INSERT INTO trans_unit (cat_id,id,source,date_added) VALUES ({$cat_id}, {$count},'{$message}',$time)";
311       if (sqlite_query($statement, $db))
312       {
313         $count++;
314         $inserted++;
315       }
316     }
317     if ($inserted > 0)
318     {
319       $this->updateCatalogueTime($cat_id, $variant, $db);
320     }
321
322     sqlite_close($db);
323
324     return $inserted > 0;
325   }
326
327   /**
328    * Updates 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
348     $comments = sqlite_escape_string($comments);
349     $target = sqlite_escape_string($target);
350     $text = sqlite_escape_string($text);
351
352     $time = time();
353
354     $db = sqlite_open($this->source);
355
356     $statement = "UPDATE trans_unit SET target = '{$target}', comments = '{$comments}', date_modified = '{$time}' WHERE cat_id = {$cat_id} AND source = '{$text}'";
357
358     $updated = false;
359
360     if (sqlite_query($statement, $db))
361     {
362       $updated = $this->updateCatalogueTime($cat_id, $variant, $db);
363     }
364
365     sqlite_close($db);
366
367     return $updated;
368   }
369
370   /**
371    * Deletes a particular message from the specified catalogue.
372    *
373    * @param string the source message to delete.
374    * @param string the catalogue to delete from.
375    * @return boolean true if deleted, false otherwise.
376    */
377   function delete($message, $catalogue = 'messages')
378   {
379     $details = $this->getCatalogueDetails($catalogue);
380     if ($details)
381     {
382       list($cat_id, $variant, $count) = $details;
383     }
384     else
385     {
386       return false;
387     }
388
389     $db = sqlite_open($this->source);
390     $text = sqlite_escape_string($message);
391
392     $statement = "DELETE FROM trans_unit WHERE cat_id = {$cat_id} AND source = '{$message}'";
393     $deleted = false;
394
395     if (sqlite_query($statement, $db))
396     {
397       $deleted = $this->updateCatalogueTime($cat_id, $variant, $db);
398     }
399
400     sqlite_close($db);
401
402     return $deleted;
403   }
404
405   /**
406    * Returns a list of catalogue as key and all it variants as value.
407    *
408    * @return array list of catalogues
409    */
410   function catalogues()
411   {
412     $db = sqlite_open($this->source);
413     $statement = 'SELECT name FROM catalogue ORDER BY name';
414     $rs = sqlite_query($statement, $db);
415     $result = array();
416     while ($row = sqlite_fetch_array($rs, SQLITE_NUM))
417     {
418       $details = explode('.',$row[0]);
419       if (!isset($details[1]))
420       {
421         $details[1] = null;
422       }
423
424       $result[] = $details;
425     }
426     sqlite_close($db);
427
428     return $result;
429   }
430 }
431
Note: See TracBrowser for help on using the browser.