sfMessageSource_Database.class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * sfMessageSource_Database class file.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the BSD License.
  7. *
  8. * Copyright(c) 2004 by Qiang Xue. All rights reserved.
  9. *
  10. * To contact the author write to {@link mailto:qiang.xue@gmail.com Qiang Xue}
  11. * The latest version of PRADO can be obtained from:
  12. * {@link http://prado.sourceforge.net/}
  13. *
  14. * @author Wei Zhuo <weizhuo[at]gmail[dot]com>
  15. * @version $Id: sfMessageSource_Database.class.php 9128 2008-05-21 00:58:19Z Carl.Vondrick $
  16. * @package symfony
  17. * @subpackage i18n
  18. */
  19. /**
  20. * sfMessageSource_Database class.
  21. *
  22. * This is the base class for database based message sources like MySQL or SQLite.
  23. *
  24. * @author Xiang Wei Zhuo <weizhuo[at]gmail[dot]com>
  25. * @version v1.0, last update on Fri Dec 24 16:18:44 EST 2004
  26. * @package symfony
  27. * @subpackage i18n
  28. */
  29. abstract class sfMessageSource_Database extends sfMessageSource
  30. {
  31. /**
  32. * For a given DSN (database connection string), return some information about the DSN.
  33. *
  34. * This function comes from PEAR's DB package.
  35. *
  36. * @param string $dns DSN format, similar to PEAR's DB
  37. * @return array DSN information.
  38. */
  39. protected function parseDSN($dsn)
  40. {
  41. if (is_array($dsn))
  42. {
  43. return $dsn;
  44. }
  45. $parsed = array(
  46. 'phptype' => false,
  47. 'dbsyntax' => false,
  48. 'username' => false,
  49. 'password' => false,
  50. 'protocol' => false,
  51. 'hostspec' => false,
  52. 'port' => false,
  53. 'socket' => false,
  54. 'database' => false
  55. );
  56. // Find phptype and dbsyntax
  57. if (($pos = strpos($dsn, '://')) !== false)
  58. {
  59. $str = substr($dsn, 0, $pos);
  60. $dsn = substr($dsn, $pos + 3);
  61. }
  62. else
  63. {
  64. $str = $dsn;
  65. $dsn = NULL;
  66. }
  67. // Get phptype and dbsyntax
  68. // $str => phptype(dbsyntax)
  69. if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr))
  70. {
  71. $parsed['phptype'] = $arr[1];
  72. $parsed['dbsyntax'] = (empty($arr[2])) ? $arr[1] : $arr[2];
  73. }
  74. else
  75. {
  76. $parsed['phptype'] = $str;
  77. $parsed['dbsyntax'] = $str;
  78. }
  79. if (empty($dsn))
  80. {
  81. return $parsed;
  82. }
  83. // Get (if found): username and password
  84. // $dsn => username:password@protocol+hostspec/database
  85. if (($at = strrpos($dsn,'@')) !== false)
  86. {
  87. $str = substr($dsn, 0, $at);
  88. $dsn = substr($dsn, $at + 1);
  89. if (($pos = strpos($str, ':')) !== false)
  90. {
  91. $parsed['username'] = rawurldecode(substr($str, 0, $pos));
  92. $parsed['password'] = rawurldecode(substr($str, $pos + 1));
  93. }
  94. else
  95. {
  96. $parsed['username'] = rawurldecode($str);
  97. }
  98. }
  99. // Find protocol and hostspec
  100. // $dsn => proto(proto_opts)/database
  101. if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match))
  102. {
  103. $proto = $match[1];
  104. $proto_opts = (!empty($match[2])) ? $match[2] : false;
  105. $dsn = $match[3];
  106. // $dsn => protocol+hostspec/database (old format)
  107. }
  108. else
  109. {
  110. if (strpos($dsn, '+') !== false)
  111. {
  112. list($proto, $dsn) = explode('+', $dsn, 2);
  113. }
  114. if (strpos($dsn, '/') !== false)
  115. {
  116. list($proto_opts, $dsn) = explode('/', $dsn, 2);
  117. }
  118. else
  119. {
  120. $proto_opts = $dsn;
  121. $dsn = null;
  122. }
  123. }
  124. // process the different protocol options
  125. $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
  126. $proto_opts = rawurldecode($proto_opts);
  127. if ($parsed['protocol'] == 'tcp')
  128. {
  129. if (strpos($proto_opts, ':') !== false)
  130. {
  131. list($parsed['hostspec'], $parsed['port']) = explode(':', $proto_opts);
  132. }
  133. else
  134. {
  135. $parsed['hostspec'] = $proto_opts;
  136. }
  137. }
  138. else if ($parsed['protocol'] == 'unix')
  139. {
  140. $parsed['socket'] = $proto_opts;
  141. }
  142. // Get dabase if any
  143. // $dsn => database
  144. if (!empty($dsn))
  145. {
  146. // /database
  147. if (($pos = strpos($dsn, '?')) === false)
  148. {
  149. $parsed['database'] = $dsn;
  150. // /database?param1=value1&param2=value2
  151. }
  152. else
  153. {
  154. $parsed['database'] = substr($dsn, 0, $pos);
  155. $dsn = substr($dsn, $pos + 1);
  156. if (strpos($dsn, '&') !== false)
  157. {
  158. $opts = explode('&', $dsn);
  159. }
  160. else
  161. { // database?param1=value1
  162. $opts = array($dsn);
  163. }
  164. foreach ($opts as $opt)
  165. {
  166. list($key, $value) = explode('=', $opt);
  167. if (!isset($parsed[$key]))
  168. { // don't allow params overwrite
  169. $parsed[$key] = rawurldecode($value);
  170. }
  171. }
  172. }
  173. }
  174. return $parsed;
  175. }
  176. /**
  177. * Gets all the variants of a particular catalogue.
  178. *
  179. * @param string $catalogue catalogue name
  180. * @return array list of all variants for this catalogue.
  181. */
  182. public function getCatalogueList($catalogue)
  183. {
  184. $variants = explode('_', $this->culture);
  185. $catalogues = array($catalogue);
  186. $variant = null;
  187. for ($i = 0, $max = count($variants); $i < $max; $i++)
  188. {
  189. if (strlen($variants[$i]) > 0)
  190. {
  191. $variant .= $variant ? '_'.$variants[$i] : $variants[$i];
  192. $catalogues[] = $catalogue.'.'.$variant;
  193. }
  194. }
  195. return array_reverse($catalogues);
  196. }
  197. public function getId()
  198. {
  199. return md5($this->source);
  200. }
  201. }