| [ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 3 /** 4 * MessageSource_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 $Revision: 1.4 $ $Date: 2005/02/25 09:59:40 $ 17 * @package System.I18N.core 18 */ 19 20 /** 21 * Get the MessageSource class file. 22 */ 23 require_once(dirname(__FILE__).'/MessageSource.php'); 24 25 /** 26 * Get the I18N utility file, contains the DSN parser. 27 */ 28 require_once(dirname(__FILE__).'/util.php'); 29 30 /** 31 * MessageSource_SQLite class. 32 * 33 * Retrive the message translation from a SQLite database. 34 * 35 * See the MessageSource::factory() method to instantiate this class. 36 * 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 40 */ 41 class MessageSource_SQLite extends MessageSource 42 { 43 /** 44 * The SQLite datasource, the filename of the database. 45 * @var string 46 */ 47 protected $source; 48 49 /** 50 * Constructor. 51 * Create a new message source using SQLite. 52 * @see MessageSource::factory(); 53 * @param string SQLite datasource, in PEAR's DB DSN format. 54 */ 55 function __construct($source) 56 { 57 $dsn = parseDSN((string)$source); 58 $this->source = $dsn['database']; 59 } 60 61 /** 62 * Get an array of messages for a particular catalogue and cultural 63 * variant. 64 * @param string the catalogue name + variant 65 * @return array translation messages. 66 */ 67 protected function &loadData($variant) 68 { 69 $variant = sqlite_escape_string($variant); 70 71 $statement = 72 "SELECT t.id, t.source, t.target, t.comments 73 FROM trans_unit t, catalogue c 74 WHERE c.cat_id = t.cat_id 75 AND c.name = '{$variant}' 76 ORDER BY id ASC"; 77 78 $db = sqlite_open($this->source); 79 $rs = sqlite_query($statement, $db); 80 81 $result = array(); 82 83 while($row = sqlite_fetch_array($rs,SQLITE_NUM)) 84 { 85 $source = $row[1]; 86 $result[$source][] = $row[2]; //target 87 $result[$source][] = $row[0]; //id 88 $result[$source][] = $row[3]; //comments 89 } 90 91 sqlite_close($db); 92 93 return $result; 94 } 95 96 /** 97 * Get the last modified unix-time for this particular catalogue+variant. 98 * We need to query the database to get the date_modified. 99 * @param string catalogue+variant 100 * @return int last modified in unix-time format. 101 */ 102 protected function getLastModified($source) 103 { 104 $source = sqlite_escape_string($source); 105 106 $db = sqlite_open($this->source); 107 108 $rs = sqlite_query( 109 "SELECT date_modified FROM catalogue WHERE name = '{$source}'", 110 $db); 111 112 $result = $rs ? intval(sqlite_fetch_single($rs)) : 0; 113 114 sqlite_close($db); 115 116 return $result; 117 } 118 119 /** 120 * Check if a particular catalogue+variant exists in the database. 121 * @param string catalogue+variant 122 * @return boolean true if the catalogue+variant is in the database, 123 * false otherwise. 124 */ 125 protected function isValidSource($variant) 126 { 127 $variant = sqlite_escape_string($variant); 128 $db = sqlite_open($this->source); 129 $rs = sqlite_query( 130 "SELECT COUNT(*) FROM catalogue WHERE name = '{$variant}'", 131 $db); 132 $result = $rs && intval(sqlite_fetch_single($rs)); 133 sqlite_close($db); 134 135 return $result; 136 } 137 138 /** 139 * Get all the variants of a particular catalogue. 140 * @param string catalogue name 141 * @return array list of all variants for this catalogue. 142 */ 143 protected function getCatalogueList($catalogue) 144 { 145 $variants = explode('_',$this->culture); 146 147 $catalogues = array($catalogue); 148 149 $variant = null; 150 151 for($i = 0, $k = count($variants); $i < $k; ++$i) 152 { 153 if(isset($variants[$i]{0})) 154 { 155 $variant .= ($variant)?'_'.$variants[$i]:$variants[$i]; 156 $catalogues[] = $catalogue.'.'.$variant; 157 } 158 } 159 return array_reverse($catalogues); 160 } 161 162 /** 163 * Retrive catalogue details, array($cat_id, $variant, $count). 164 * @param string catalogue 165 * @return array catalogue details, array($cat_id, $variant, $count). 166 */ 167 private function getCatalogueDetails($catalogue='messages') 168 { 169 if(empty($catalogue)) 170 $catalogue = 'messages'; 171 172 $variant = $catalogue.'.'.$this->culture; 173 174 $name = sqlite_escape_string($this->getSource($variant)); 175 176 $db = sqlite_open($this->source); 177 178 $rs = sqlite_query("SELECT cat_id 179 FROM catalogue WHERE name = '{$name}'", $db); 180 181 if(sqlite_num_rows($rs) != 1) 182 return false; 183 184 $cat_id = intval(sqlite_fetch_single($rs)); 185 186 //first get the catalogue ID 187 $rs = sqlite_query( 188 "SELECT count(msg_id) 189 FROM trans_unit 190 WHERE cat_id = {$cat_id}", $db); 191 192 $count = intval(sqlite_fetch_single($rs)); 193 194 sqlite_close($db); 195 196 return array($cat_id, $variant, $count); 197 } 198 199 /** 200 * Update the catalogue last modified time. 201 * @return boolean true if updated, false otherwise. 202 */ 203 private function updateCatalogueTime($cat_id, $variant, $db) 204 { 205 $time = time(); 206 207 $result = sqlite_query("UPDATE catalogue 208 SET date_modified = {$time} 209 WHERE cat_id = {$cat_id}", $db); 210 211 if(!empty($this->cache)) 212 $this->cache->clean($variant, $this->culture); 213 214 return $result; 215 } 216 217 /** 218 * Save the list of untranslated blocks to the translation source. 219 * If the translation was not found, you should add those 220 * strings to the translation source via the <b>append()</b> method. 221 * @param string the catalogue to add to 222 * @return boolean true if saved successfuly, false otherwise. 223 */ 224 function save($catalogue='messages') 225 { 226 $messages = $this->untranslated; 227 228 if(count($messages) <= 0) return false; 229 230 $details = $this->getCatalogueDetails($catalogue); 231 232 if($details) 233 list($cat_id, $variant, $count) = $details; 234 else 235 return false; 236 237 if($cat_id <= 0) return false; 238 $inserted = 0; 239 240 $db = sqlite_open($this->source); 241 $time = time(); 242 243 foreach($messages as $message) 244 { 245 $message = sqlite_escape_string($message); 246 $statement = "INSERT INTO trans_unit 247 (cat_id,id,source,date_added) VALUES 248 ({$cat_id}, {$count},'{$message}',$time)"; 249 if(sqlite_query($statement, $db)) 250 { 251 $count++; $inserted++; 252 } 253 } 254 if($inserted > 0) 255 $this->updateCatalogueTime($cat_id, $variant, $db); 256 257 sqlite_close($db); 258 259 return $inserted > 0; 260 } 261 262 /** 263 * Update the translation. 264 * @param string the source string. 265 * @param string the new translation string. 266 * @param string comments 267 * @param string the catalogue of the translation. 268 * @return boolean true if translation was updated, false otherwise. 269 */ 270 function update($text, $target, $comments, $catalogue='messages') 271 { 272 $details = $this->getCatalogueDetails($catalogue); 273 if($details) 274 list($cat_id, $variant, $count) = $details; 275 else 276 return false; 277 278 $comments = sqlite_escape_string($comments); 279 $target = sqlite_escape_string($target); 280 $text = sqlite_escape_string($text); 281 282 $time = time(); 283 284 $db = sqlite_open($this->source); 285 286 $statement = "UPDATE trans_unit SET 287 target = '{$target}', 288 comments = '{$comments}', 289 date_modified = '{$time}' 290 WHERE cat_id = {$cat_id} 291 AND source = '{$text}'"; 292 293 $updated = false; 294 295 if(sqlite_query($statement, $db)) 296 $updated = $this->updateCatalogueTime($cat_id, $variant, $db); 297 298 sqlite_close($db); 299 300 return $updated; 301 } 302 303 /** 304 * Delete a particular message from the specified catalogue. 305 * @param string the source message to delete. 306 * @param string the catalogue to delete from. 307 * @return boolean true if deleted, false otherwise. 308 */ 309 function delete($message, $catalogue='messages') 310 { 311 $details = $this->getCatalogueDetails($catalogue); 312 if($details) 313 list($cat_id, $variant, $count) = $details; 314 else 315 return false; 316 317 $db = sqlite_open($this->source); 318 $text = sqlite_escape_string($message); 319 320 $statement = "DELETE FROM trans_unit WHERE 321 cat_id = {$cat_id} AND source = '{$message}'"; 322 $deleted = false; 323 324 if(sqlite_query($statement, $db)) 325 $deleted = $this->updateCatalogueTime($cat_id, $variant, $db); 326 327 sqlite_close($db); 328 329 return $deleted; 330 } 331 332 /** 333 * Returns a list of catalogue as key and all it variants as value. 334 * @return array list of catalogues 335 */ 336 function catalogues() 337 { 338 $db = sqlite_open($this->source); 339 $statement = 'SELECT name FROM catalogue ORDER BY name'; 340 $rs = sqlite_query($statement, $db); 341 $result = array(); 342 while($row = sqlite_fetch_array($rs,SQLITE_NUM)) 343 { 344 $details = explode('.',$row[0]); 345 if(!isset($details[1])) $details[1] = null; 346 347 $result[] = $details; 348 } 349 sqlite_close($db); 350 return $result; 351 } 352 } 353 354 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |