[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/i18n/ -> sfMessageSource_SQLite.class.php (source)

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


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7