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

Debug toolbar