4 * MessageSource_MySQL class file.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the BSD License.
9 * Copyright(c) 2004 by Qiang Xue. All rights reserved.
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/}
15 * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
16 * @version $Revision: 1.4 $ $Date: 2005/02/25 09:59:40 $
17 * @package System.I18N.core
21 * Get the MessageSource class file.
23 require_once(dirname(__FILE__).'/MessageSource.php');
26 * Get the I18N utility file, contains the DSN parser.
28 require_once(dirname(__FILE__).'/util.php');
31 * MessageSource_MySQL class.
33 * Retrive the message translation from a MySQL database.
35 * See the MessageSource::factory() method to instantiate this class.
37 * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
38 * @version v1.0, last update on Fri Dec 24 16:58:58 EST 2004
39 * @package System.I18N.core
41 class MessageSource_MySQL extends MessageSource
44 * The datasource string, full DSN to the database.
50 * The DSN array property, parsed by PEAR's DB DSN parser.
56 * A resource link to the database
62 * Create a new message source using MySQL.
63 * @param string MySQL datasource, in PEAR's DB DSN format.
64 * @see MessageSource::factory();
66 function __construct($source)
68 $this->source = (string)$source;
69 $this->dns = parseDSN($this->source);
70 $this->db = $this->connect();
74 * Destructor, close the database connection.
78 @mysql_close($this->db);
82 * Connect to the MySQL datasource
83 * @return resource MySQL connection.
84 * @throws Exception, connection and database errors.
86 protected function connect()
93 $dsninfo = $this->dns;
95 if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix')
96 $dbhost = ':' . $dsninfo['socket'];
99 $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
100 if (!empty($dsninfo['port']))
101 $dbhost .= ':' . $dsninfo['port'];
103 $user = $dsninfo['username'];
104 $pw = $dsninfo['password'];
106 $connect_function = 'mysql_connect';
108 if ($dbhost && $user && $pw)
109 $conn = @$connect_function($dbhost, $user, $pw);
110 elseif ($dbhost && $user)
111 $conn = @$connect_function($dbhost, $user);
113 $conn = @$connect_function($dbhost);
119 throw new Exception('Error in connecting to '.$dsninfo);
122 if ($dsninfo['database'])
124 if (!@mysql_select_db($dsninfo['database'], $conn))
125 throw new Exception('Error in connecting database, dns:'.
129 throw new Exception('Please provide a database for message'.
135 * Get the database connection.
136 * @return db database connection.
138 public function connection()
144 * Get an array of messages for a particular catalogue and cultural
146 * @param string the catalogue name + variant
147 * @return array translation messages.
149 protected function &loadData($variant)
151 $variant = mysql_real_escape_string($variant);
154 "SELECT t.id, t.source, t.target, t.comments
155 FROM trans_unit t, catalogue c
156 WHERE c.cat_id = t.cat_id
157 AND c.name = '{$variant}'
160 $rs = mysql_query($statement,$this->db);
164 while($row = mysql_fetch_array($rs,MYSQL_NUM))
167 $result[$source][] = $row[2]; //target
168 $result[$source][] = $row[0]; //id
169 $result[$source][] = $row[3]; //comments
176 * Get the last modified unix-time for this particular catalogue+variant.
177 * We need to query the database to get the date_modified.
178 * @param string catalogue+variant
179 * @return int last modified in unix-time format.
181 protected function getLastModified($source)
183 $source = mysql_real_escape_string($source);
186 "SELECT date_modified FROM catalogue WHERE name = '{$source}'",
189 $result = $rs ? (int)mysql_result($rs,0) : 0;
195 * Check if a particular catalogue+variant exists in the database.
196 * @param string catalogue+variant
197 * @return boolean true if the catalogue+variant is in the database,
200 protected function isValidSource($variant)
202 $variant = mysql_real_escape_string ($variant);
205 "SELECT COUNT(*) FROM catalogue WHERE name = '{$variant}'",
208 $row = mysql_fetch_array($rs,MYSQL_NUM);
210 $result = $row && $row[0] == '1';
216 * Get all the variants of a particular catalogue.
217 * @param string catalogue name
218 * @return array list of all variants for this catalogue.
220 protected function getCatalogueList($catalogue)
222 $variants = explode('_',$this->culture);
224 $catalogues = array($catalogue);
228 for($i = 0, $k = count($variants); $i < $k; ++$i)
230 if(isset($variants[$i]{0}))
232 $variant .= ($variant)?'_'.$variants[$i]:$variants[$i];
233 $catalogues[] = $catalogue.'.'.$variant;
236 return array_reverse($catalogues);
240 * Retrive catalogue details, array($cat_id, $variant, $count).
241 * @param string catalogue
242 * @return array catalogue details, array($cat_id, $variant, $count).
244 private function getCatalogueDetails($catalogue='messages')
246 if(empty($catalogue))
247 $catalogue = 'messages';
249 $variant = $catalogue.'.'.$this->culture;
251 $name = mysql_real_escape_string($this->getSource($variant));
253 $rs = mysql_query("SELECT cat_id
254 FROM catalogue WHERE name = '{$name}'", $this->db);
256 if(mysql_num_rows($rs) != 1)
259 $cat_id = (int)mysql_result($rs,0);
261 //first get the catalogue ID
263 "SELECT count(msg_id)
265 WHERE cat_id = {$cat_id}", $this->db);
267 $count = (int)mysql_result($rs,0);
269 return array($cat_id, $variant, $count);
273 * Update the catalogue last modified time.
274 * @return boolean true if updated, false otherwise.
276 private function updateCatalogueTime($cat_id, $variant)
280 $result = mysql_query("UPDATE catalogue
281 SET date_modified = {$time}
282 WHERE cat_id = {$cat_id}", $this->db);
284 if(!empty($this->cache))
285 $this->cache->clean($variant, $this->culture);
291 * Save the list of untranslated blocks to the translation source.
292 * If the translation was not found, you should add those
293 * strings to the translation source via the <b>append()</b> method.
294 * @param string the catalogue to add to
295 * @return boolean true if saved successfuly, false otherwise.
297 function save($catalogue='messages')
299 $messages = $this->untranslated;
301 if(count($messages) <= 0) return false;
303 $details = $this->getCatalogueDetails($catalogue);
306 list($cat_id, $variant, $count) = $details;
310 if($cat_id <= 0) return false;
315 foreach($messages as $message)
317 $count++; $inserted++;
318 $message = mysql_real_escape_string($message);
319 $statement = "INSERT INTO trans_unit
320 (cat_id,id,source,date_added) VALUES
321 ({$cat_id}, {$count},'{$message}',$time)";
322 mysql_query($statement, $this->db);
325 $this->updateCatalogueTime($cat_id, $variant);
327 return $inserted > 0;
331 * Delete a particular message from the specified catalogue.
332 * @param string the source message to delete.
333 * @param string the catalogue to delete from.
334 * @return boolean true if deleted, false otherwise.
336 function delete($message, $catalogue='messages')
338 $details = $this->getCatalogueDetails($catalogue);
340 list($cat_id, $variant, $count) = $details;
344 $text = mysql_real_escape_string($message);
346 $statement = "DELETE FROM trans_unit WHERE
347 cat_id = {$cat_id} AND source = '{$message}'";
350 mysql_query($statement, $this->db);
352 if(mysql_affected_rows($this->db) == 1)
353 $deleted = $this->updateCatalogueTime($cat_id, $variant);
360 * Update the translation.
361 * @param string the source string.
362 * @param string the new translation string.
363 * @param string comments
364 * @param string the catalogue of the translation.
365 * @return boolean true if translation was updated, false otherwise.
367 function update($text, $target, $comments, $catalogue='messages')
369 $details = $this->getCatalogueDetails($catalogue);
371 list($cat_id, $variant, $count) = $details;
375 $comments = mysql_real_escape_string($comments);
376 $target = mysql_real_escape_string($target);
377 $text = mysql_real_escape_string($text);
381 $statement = "UPDATE trans_unit SET
382 target = '{$target}',
383 comments = '{$comments}',
384 date_modified = '{$time}'
385 WHERE cat_id = {$cat_id}
386 AND source = '{$text}'";
390 mysql_query($statement, $this->db);
391 if(mysql_affected_rows($this->db) == 1)
392 $updated = $this->updateCatalogueTime($cat_id, $variant);
398 * Returns a list of catalogue as key and all it variants as value.
399 * @return array list of catalogues
401 function catalogues()
403 $statement = 'SELECT name FROM catalogue ORDER BY name';
404 $rs = mysql_query($statement, $this->db);
406 while($row = mysql_fetch_array($rs,MYSQL_NUM))
408 $details = explode('.',$row[0]);
409 if(!isset($details[1])) $details[1] = null;
411 $result[] = $details;