Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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