Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
4
/**
5
 * Contains the Translation2_Container_mdb class
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 * 3. The name of the author may not be used to endorse or promote products
17
 *    derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
23
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * @category  Internationalization
31
 * @package   Translation2
32
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
33
 * @copyright 2004-2008 Lorenzo Alberton
34
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
35
 * @version   CVS: $Id: mdb.php 305985 2010-12-05 22:55:33Z clockwerx $
36
 * @link      http://pear.php.net/package/Translation2
37
 */
38
 
39
/**
40
 * require Translation2_Container class
41
 */
42
require_once 'Translation2/Container.php';
43
 
44
/**
45
 * Storage driver for fetching data from a database
46
 *
47
 * This storage driver can use all databases which are supported
48
 * by the PEAR::MDB abstraction layer to fetch data.
49
 *
50
 * @category  Internationalization
51
 * @package   Translation2
52
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
53
 * @copyright 2004-2008 Lorenzo Alberton
54
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
55
 * @version   CVS: $Id: mdb.php 305985 2010-12-05 22:55:33Z clockwerx $
56
 * @link      http://pear.php.net/package/Translation2
57
 */
58
class Translation2_Container_mdb extends Translation2_Container
59
{
60
 
61
    // {{{ class vars
62
 
63
    /**
64
     * MDB object
65
     * @var object
66
     */
67
    var $db = null;
68
 
69
    /**
70
     * query counter
71
     * @var integer
72
     * @access private
73
     */
74
    var $_queries = 0;
75
 
76
    // }}}
77
    // {{{ init
78
 
79
    /**
80
     * Initialize the container
81
     *
82
     * @param string &$db Connection data or MDB object
83
     *
84
     * @return boolean|PEAR_Error object if something went wrong
85
     */
86
    function init(&$db)
87
    {
88
        $this->_setDefaultOptions();
89
        if (PEAR::isError($err = $this->_connect($db))) {
90
            return $err;
91
        }
92
        return true;
93
    }
94
 
95
    // }}}
96
    // {{{ _connect()
97
 
98
    /**
99
     * Connect to database by using the given DSN string
100
     *
101
     * @param mixed &$db DSN string | array | mdb object
102
     *
103
     * @return boolean|PEAR_Error on error
104
     * @access private
105
     */
106
    function _connect(&$db)
107
    {
108
        if (is_object($db) && is_a($db, 'MDB_Common')) {
109
            $this->db = &$db;
110
        } elseif (is_string($db) || is_array($db)) {
111
            include_once 'MDB.php';
112
            $this->db =& MDB::connect($db);
113
        } elseif (is_object($db) && MDB::isError($db)) {
114
            return PEAR::raiseError($db->getMessage(), $db->code);
115
        } else {
116
            return PEAR::raiseError('The given dsn was not valid in file '
117
                                    . __FILE__ . ' at line ' . __LINE__,
118
                                    TRANSLATION2_ERROR_CANNOT_CONNECT,
119
                                    PEAR_ERROR_RETURN);
120
        }
121
 
122
        if (PEAR::isError($this->db)) {
123
            return $this->db;
124
        }
125
        return true;
126
    }
127
 
128
    // }}}
129
    // {{{ _setDefaultOptions()
130
 
131
    /**
132
     * Set some default options
133
     *
134
     * @access private
135
     * @return void
136
     */
137
    function _setDefaultOptions()
138
    {
139
        $this->options['langs_avail_table'] = 'langs';
140
        $this->options['lang_id_col']       = 'id';
141
        $this->options['lang_name_col']     = 'name';
142
        $this->options['lang_meta_col']     = 'meta';
143
        $this->options['lang_errmsg_col']   = 'error_text';
144
        $this->options['lang_encoding_col'] = 'encoding';
145
 
146
        $this->options['strings_default_table'] = 'i18n';
147
        $this->options['strings_tables']        = array(); // 'lang_id' => 'table_name'
148
        $this->options['string_id_col']         = 'id';
149
        $this->options['string_page_id_col']    = 'page_id';
150
        $this->options['string_page_id_col_length'] = 50;
151
        $this->options['string_text_col']       = '%s'; // col_name if one table per lang is used,
152
                                                        // or a pattern (i.e. "tr_%s" => "tr_EN_US")
153
    }
154
 
155
    // }}}
156
    // {{{ fetchLangs()
157
 
158
    /**
159
     * Fetch the available langs if they're not cached yet.
160
     *
161
     * @return PEAR_Error on error
162
     */
163
    function fetchLangs()
164
    {
165
        $query = sprintf('SELECT %s AS id, %s AS name, %s AS meta, %s AS error_text, %s AS encoding FROM %s',
166
            $this->db->quoteIdentifier($this->options['lang_id_col']),
167
            $this->db->quoteIdentifier($this->options['lang_name_col']),
168
            $this->db->quoteIdentifier($this->options['lang_meta_col']),
169
            $this->db->quoteIdentifier($this->options['lang_errmsg_col']),
170
            $this->db->quoteIdentifier($this->options['lang_encoding_col']),
171
            $this->db->quoteIdentifier($this->options['langs_avail_table'])
172
        );
173
 
174
        ++$this->_queries;
175
        $res = $this->db->getAll($query, null, array(), null, MDB_FETCHMODE_ASSOC);
176
        if (PEAR::isError($res)) {
177
            return $res;
178
        }
179
        foreach ($res as $row) {
180
            $row = array_change_key_case($row, CASE_LOWER);
181
            $this->langs[$row['id']] = $row;
182
        }
183
    }
184
 
185
    // }}}
186
    // {{{ getPage()
187
 
188
    /**
189
     * Returns an array of the strings in the selected page
190
     *
191
     * @param string $pageID page/group ID
192
     * @param string $langID language ID
193
     *
194
     * @return array|PEAR_Error on error
195
     */
196
    function getPage($pageID = null, $langID = null)
197
    {
198
        $langID = $this->_getLangID($langID);
199
        if (PEAR::isError($langID)) {
200
            return $langID;
201
        }
202
        $lang_col = $this->_getLangCol($langID);
203
        $table    = $this->_getLangTable($langID);
204
 
205
        $query = sprintf('SELECT %s, %s FROM %s WHERE %s ',
206
             $this->db->quoteIdentifier($this->options['string_id_col']),
207
             $this->db->quoteIdentifier($lang_col),
208
             $this->db->quoteIdentifier($table),
209
             $this->db->quoteIdentifier($this->options['string_page_id_col'])
210
        );
211
 
212
        if (is_null($pageID)) {
213
            $query .= 'IS NULL';
214
        } else {
215
            $query .= ' = ' . $this->db->getTextValue($pageID);
216
        }
217
 
218
        ++$this->_queries;
219
        $res = $this->db->getAssoc($query);
220
        return $res;
221
    }
222
 
223
    // }}}
224
    // {{{ getOne()
225
 
226
    /**
227
     * Get a single item from the container
228
     *
229
     * @param string $stringID string ID
230
     * @param string $pageID   page/group ID
231
     * @param string $langID   language ID
232
     *
233
     * @return string
234
     */
235
    function getOne($stringID, $pageID = null, $langID = null)
236
    {
237
        $langID = $this->_getLangID($langID);
238
        if (PEAR::isError($langID)) {
239
            return $langID;
240
        }
241
        $lang_col = $this->_getLangCol($langID);
242
        $table    = $this->_getLangTable($langID);
243
 
244
        $query = sprintf('SELECT %s FROM %s WHERE %s = %s AND %s',
245
             $this->db->quoteIdentifier($lang_col),
246
             $this->db->quoteIdentifier($table),
247
             $this->db->quoteIdentifier($this->options['string_id_col']),
248
             $this->db->getTextValue($stringID),
249
             $this->db->quoteIdentifier($this->options['string_page_id_col'])
250
        );
251
 
252
        if (is_null($pageID)) {
253
            $query .= ' IS NULL';
254
        } else {
255
            $query .= ' = ' . $this->db->getTextValue($pageID);
256
        }
257
 
258
        ++$this->_queries;
259
        return $this->db->getOne($query);
260
    }
261
 
262
    // }}}
263
    // {{{ getStringID()
264
 
265
    /**
266
     * Get the stringID for the given string
267
     *
268
     * @param string $string string
269
     * @param string $pageID page/group ID
270
     *
271
     * @return string
272
     */
273
    function getStringID($string, $pageID = null)
274
    {
275
        $lang_col = $this->_getLangCol($this->currentLang['id']);
276
        $table = $this->_getLangTable($this->currentLang['id']);
277
        $query = sprintf('SELECT %s FROM %s WHERE %s = %s AND %s',
278
             $this->db->quoteIdentifier($this->options['string_id_col']),
279
             $this->db->quoteIdentifier($table),
280
             $this->db->quoteIdentifier($lang_col),
281
             $this->db->getTextValue($string),
282
             $this->db->quoteIdentifier($this->options['string_page_id_col'])
283
        );
284
        if (is_null($pageID)) {
285
            $query .= ' IS NULL';
286
        } else {
287
            $query .= ' = ' . $this->db->getTextValue($pageID);
288
        }
289
        ++$this->_queries;
290
        return $this->db->getOne($query);
291
    }
292
 
293
    // }}}
294
    // {{{ _getLangTable()
295
 
296
    /**
297
     * Get the table a language is stored in
298
     *
299
     * @param string $langID language ID
300
     *
301
     * @return string table $langID is stored in
302
     * @access private
303
     */
304
    function _getLangTable($langID)
305
    {
306
        if (isset($this->options['strings_tables'][$langID])) {
307
            return $this->options['strings_tables'][$langID];
308
        }
309
        return str_replace('%s', $langID, $this->options['strings_default_table']);
310
    }
311
 
312
    // }}}
313
    // {{{ _getLangCol()
314
 
315
    /**
316
     * Get the column a language's string is stored in
317
     *
318
     * @param string $langID language ID
319
     *
320
     * @return string column $langID is stored in
321
     * @access private
322
     */
323
    function _getLangCol($langID)
324
    {
325
        static $cols;
326
        if (!isset($cols[$langID])) {
327
            if (isset($this->options['string_text_col']) &&
328
                !empty($this->options['string_text_col'])) {
329
                $cols[$langID] = str_replace('%s', $langID, $this->options['string_text_col']);
330
            } else {
331
                $cols[$langID] = $langID;
332
            }
333
        }
334
        return $cols[$langID];
335
    }
336
 
337
    // }}}
338
}
339
?>