Changeset 1694
- Timestamp:
- 02/12/2007 11:52:54 PM (22 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
branches/3.0/framework/I18N/core/MessageSource_MySQL.php
r1397 r1694 30 30 /** 31 31 * MessageSource_MySQL class. 32 * 32 * 33 33 * Retrive the message translation from a MySQL database. 34 34 * … … 43 43 /** 44 44 * The datasource string, full DSN to the database. 45 * @var string 45 * @var string 46 46 */ 47 47 protected $source; 48 48 49 49 /** 50 50 * The DSN array property, parsed by PEAR's DB DSN parser. 51 * @var array 51 * @var array 52 52 */ 53 53 protected $dns; 54 54 55 55 /** 56 56 * A resource link to the database 57 * @var db 57 * @var db 58 58 */ 59 59 protected $db; … … 66 66 function __construct($source) 67 67 { 68 $this->source = (string)$source; 68 $this->source = (string)$source; 69 69 $this->dns = parseDSN($this->source); 70 70 $this->db = $this->connect(); 71 71 } 72 72 73 73 /** 74 74 * Destructor, close the database connection. 75 75 */ 76 function __destruct() 76 function __destruct() 77 77 { 78 78 @mysql_close($this->db); 79 79 } 80 80 81 81 /** 82 82 * Connect to the MySQL datasource 83 * @return resource MySQL connection. 83 * @return resource MySQL connection. 84 84 * @throws Exception, connection and database errors. 85 85 */ … … 87 87 { 88 88 /*static $conn; 89 89 90 90 if(!is_null($conn)) 91 91 return $conn; 92 92 */ 93 93 $dsninfo = $this->dns; 94 95 if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix') 94 95 if (isset($dsninfo['protocol']) && $dsninfo['protocol'] == 'unix') 96 96 $dbhost = ':' . $dsninfo['socket']; 97 else 97 else 98 98 { 99 99 $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost'; 100 if (!empty($dsninfo['port'])) 100 if (!empty($dsninfo['port'])) 101 101 $dbhost .= ':' . $dsninfo['port']; 102 102 } 103 103 $user = $dsninfo['username']; 104 $pw = $dsninfo['password']; 105 104 $pw = $dsninfo['password']; 105 106 106 $connect_function = 'mysql_connect'; 107 107 … … 113 113 $conn = @$connect_function($dbhost); 114 114 else 115 $conn = false; 116 117 if (empty($conn)) 115 $conn = false; 116 117 if (empty($conn)) 118 118 { 119 119 throw new Exception('Error in connecting to '.$dsninfo); 120 120 } 121 122 if ($dsninfo['database']) 121 122 if ($dsninfo['database']) 123 123 { 124 if (!@mysql_select_db($dsninfo['database'], $conn)) 124 if (!@mysql_select_db($dsninfo['database'], $conn)) 125 125 throw new Exception('Error in connecting database, dns:'. 126 126 $dsninfo); … … 128 128 else 129 129 throw new Exception('Please provide a database for message'. 130 ' translation.'); 130 ' translation.'); 131 131 return $conn; 132 132 } 133 133 134 134 /** 135 135 * Get the database connection. 136 * @return db database connection. 136 * @return db database connection. 137 137 */ 138 138 public function connection() … … 140 140 return $this->db; 141 141 } 142 143 /** 144 * Get an array of messages for a particular catalogue and cultural 142 143 /** 144 * Get an array of messages for a particular catalogue and cultural 145 145 * variant. 146 146 * @param string the catalogue name + variant 147 147 * @return array translation messages. 148 */ 148 */ 149 149 protected function &loadData($variant) 150 { 151 $variant = mysql_ escape_string($variant);152 153 $statement = 150 { 151 $variant = mysql_real_escape_string($variant); 152 153 $statement = 154 154 "SELECT t.id, t.source, t.target, t.comments 155 155 FROM trans_unit t, catalogue c 156 156 WHERE c.cat_id = t.cat_id 157 AND c.name = '{$variant}' 157 AND c.name = '{$variant}' 158 158 ORDER BY id ASC"; 159 159 160 160 $rs = mysql_query($statement,$this->db); 161 161 162 162 $result = array(); 163 163 164 164 while($row = mysql_fetch_array($rs,MYSQL_NUM)) 165 165 { … … 169 169 $result[$source][] = $row[3]; //comments 170 170 } 171 171 172 172 return $result; 173 173 } 174 174 175 175 /** 176 176 * Get the last modified unix-time for this particular catalogue+variant. … … 178 178 * @param string catalogue+variant 179 179 * @return int last modified in unix-time format. 180 */ 180 */ 181 181 protected function getLastModified($source) 182 182 { 183 $source = mysql_ escape_string($source);183 $source = mysql_real_escape_string($source); 184 184 185 185 $rs = mysql_query( 186 186 "SELECT date_modified FROM catalogue WHERE name = '{$source}'", 187 187 $this->db); 188 188 189 189 $result = $rs ? intval(mysql_result($rs,0)) : 0; 190 191 return $result; 192 } 193 190 191 return $result; 192 } 193 194 194 /** 195 195 * Check if a particular catalogue+variant exists in the database. 196 196 * @param string catalogue+variant 197 * @return boolean true if the catalogue+variant is in the database, 197 * @return boolean true if the catalogue+variant is in the database, 198 198 * false otherwise. 199 */ 199 */ 200 200 protected function isValidSource($variant) 201 201 { 202 $variant = mysql_ escape_string ($variant);203 204 $rs = mysql_query( 202 $variant = mysql_real_escape_string ($variant); 203 204 $rs = mysql_query( 205 205 "SELECT COUNT(*) FROM catalogue WHERE name = '{$variant}'", 206 206 $this->db); 207 207 208 208 $row = mysql_fetch_array($rs,MYSQL_NUM); 209 209 210 210 $result = $row && $row[0] == '1'; 211 211 212 212 return $result; 213 213 } 214 214 215 215 /** 216 216 * Get all the variants of a particular catalogue. 217 217 * @param string catalogue name 218 * @return array list of all variants for this catalogue. 219 */ 218 * @return array list of all variants for this catalogue. 219 */ 220 220 protected function getCatalogueList($catalogue) 221 221 { 222 222 $variants = explode('_',$this->culture); 223 223 224 224 $catalogues = array($catalogue); 225 225 226 226 $variant = null; 227 227 228 228 for($i = 0, $k = count($variants); $i < $k; ++$i) 229 { 229 { 230 230 if(isset($variants[$i]{0})) 231 231 { … … 234 234 } 235 235 } 236 return array_reverse($catalogues); 237 } 238 236 return array_reverse($catalogues); 237 } 238 239 239 /** 240 240 * Retrive catalogue details, array($cat_id, $variant, $count). 241 241 * @param string catalogue 242 * @return array catalogue details, array($cat_id, $variant, $count). 242 * @return array catalogue details, array($cat_id, $variant, $count). 243 243 */ 244 244 private function getCatalogueDetails($catalogue='messages') … … 248 248 249 249 $variant = $catalogue.'.'.$this->culture; 250 251 $name = mysql_ escape_string($this->getSource($variant));252 250 251 $name = mysql_real_escape_string($this->getSource($variant)); 252 253 253 $rs = mysql_query("SELECT cat_id 254 254 FROM catalogue WHERE name = '{$name}'", $this->db); 255 255 256 256 if(mysql_num_rows($rs) != 1) 257 257 return false; 258 258 259 259 $cat_id = intval(mysql_result($rs,0)); 260 260 261 261 //first get the catalogue ID 262 262 $rs = mysql_query( … … 266 266 267 267 $count = intval(mysql_result($rs,0)); 268 268 269 269 return array($cat_id, $variant, $count); 270 } 271 270 } 271 272 272 /** 273 273 * Update the catalogue last modified time. 274 * @return boolean true if updated, false otherwise. 274 * @return boolean true if updated, false otherwise. 275 275 */ 276 276 private function updateCatalogueTime($cat_id, $variant) 277 277 { 278 278 $time = time(); 279 280 $result = mysql_query("UPDATE catalogue 279 280 $result = mysql_query("UPDATE catalogue 281 281 SET date_modified = {$time} 282 282 WHERE cat_id = {$cat_id}", $this->db); 283 283 284 284 if(!empty($this->cache)) 285 $this->cache->clean($variant, $this->culture); 286 285 $this->cache->clean($variant, $this->culture); 286 287 287 return $result; 288 } 289 290 /** 291 * Save the list of untranslated blocks to the translation source. 288 } 289 290 /** 291 * Save the list of untranslated blocks to the translation source. 292 292 * If the translation was not found, you should add those 293 293 * strings to the translation source via the <b>append()</b> method. … … 298 298 { 299 299 $messages = $this->untranslated; 300 301 if(count($messages) <= 0) return false; 302 303 $details = $this->getCatalogueDetails($catalogue); 304 300 301 if(count($messages) <= 0) return false; 302 303 $details = $this->getCatalogueDetails($catalogue); 304 305 305 if($details) 306 306 list($cat_id, $variant, $count) = $details; 307 307 else 308 return false; 309 308 return false; 309 310 310 if($cat_id <= 0) return false; 311 311 $inserted = 0; … … 316 316 { 317 317 $count++; $inserted++; 318 $message = mysql_ escape_string($message);318 $message = mysql_real_escape_string($message); 319 319 $statement = "INSERT INTO trans_unit 320 320 (cat_id,id,source,date_added) VALUES … … 323 323 } 324 324 if($inserted > 0) 325 $this->updateCatalogueTime($cat_id, $variant); 325 $this->updateCatalogueTime($cat_id, $variant); 326 326 327 327 return $inserted > 0; 328 } 329 328 } 329 330 330 /** 331 331 * Delete a particular message from the specified catalogue. 332 332 * @param string the source message to delete. 333 333 * @param string the catalogue to delete from. 334 * @return boolean true if deleted, false otherwise. 334 * @return boolean true if deleted, false otherwise. 335 335 */ 336 336 function delete($message, $catalogue='messages') … … 341 341 else 342 342 return false; 343 344 $text = mysql_ escape_string($message);345 343 344 $text = mysql_real_escape_string($message); 345 346 346 $statement = "DELETE FROM trans_unit WHERE 347 347 cat_id = {$cat_id} AND source = '{$message}'"; 348 348 $deleted = false; 349 349 350 350 mysql_query($statement, $this->db); 351 351 352 352 if(mysql_affected_rows($this->db) == 1) 353 $deleted = $this->updateCatalogueTime($cat_id, $variant); 354 353 $deleted = $this->updateCatalogueTime($cat_id, $variant); 354 355 355 return $deleted; 356 356 357 357 } 358 358 359 359 /** 360 360 * Update the translation. … … 363 363 * @param string comments 364 364 * @param string the catalogue of the translation. 365 * @return boolean true if translation was updated, false otherwise. 366 */ 365 * @return boolean true if translation was updated, false otherwise. 366 */ 367 367 function update($text, $target, $comments, $catalogue='messages') 368 368 { … … 372 372 else 373 373 return false; 374 375 $comments = mysql_ escape_string($comments);376 $target = mysql_ escape_string($target);377 $text = mysql_ escape_string($text);378 374 375 $comments = mysql_real_escape_string($comments); 376 $target = mysql_real_escape_string($target); 377 $text = mysql_real_escape_string($text); 378 379 379 $time = time(); 380 380 381 381 $statement = "UPDATE trans_unit SET 382 382 target = '{$target}', 383 383 comments = '{$comments}', 384 384 date_modified = '{$time}' 385 WHERE cat_id = {$cat_id} 385 WHERE cat_id = {$cat_id} 386 386 AND source = '{$text}'"; 387 387 388 388 $updated = false; 389 389 390 390 mysql_query($statement, $this->db); 391 391 if(mysql_affected_rows($this->db) == 1) 392 392 $updated = $this->updateCatalogueTime($cat_id, $variant); 393 393 394 394 return $updated; 395 395 } 396 396 397 397 /** 398 398 * Returns a list of catalogue as key and all it variants as value. 399 * @return array list of catalogues 399 * @return array list of catalogues 400 400 */ 401 401 function catalogues() … … 408 408 $details = explode('.',$row[0]); 409 409 if(!isset($details[1])) $details[1] = null; 410 410 411 411 $result[] = $details; 412 412 } 413 413 return $result; 414 414 } 415 415 416 416 } 417 417
