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_Admin_Container_mdb2 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-2007 Lorenzo Alberton
34
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
35
 * @version   CVS: $Id: mdb2.php 305985 2010-12-05 22:55:33Z clockwerx $
36
 * @link      http://pear.php.net/package/Translation2
37
 */
38
 
39
/**
40
 * require Translation2_Container_mdb2 class
41
 */
42
require_once 'Translation2/Container/mdb2.php';
43
 
44
/**
45
 * Storage driver for storing/fetching data to/from a database
46
 *
47
 * This storage driver can use all databases which are supported
48
 * by the PEAR::MDB2 abstraction layer to store and fetch data.
49
 *
50
 * @category  Internationalization
51
 * @package   Translation2
52
 * @author    Lorenzo Alberton <l.alberton@quipo.it>
53
 * @copyright 2004-2007 Lorenzo Alberton
54
 * @license   http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
55
 * @link      http://pear.php.net/package/Translation2
56
 */
57
class Translation2_Admin_Container_mdb2 extends Translation2_Container_mdb2
58
{
59
    // {{{
60
 
61
    /**
62
     * Fetch the table names from the db
63
     *
64
     * @access private
65
     * @return array|PEAR_Error
66
     */
67
    function _fetchTableNames()
68
    {
69
        $this->db->loadModule('Manager');
70
        return $this->db->manager->listTables();
71
    }
72
 
73
    // }}}
74
    // {{{ addLang()
75
 
76
    /**
77
     * Creates a new table to store the strings in this language.
78
     * If the table is shared with other langs, it is ALTERed to
79
     * hold strings in this lang too.
80
     *
81
     * @param array $langData array('lang_id'    => 'en',
82
     *                              'table_name' => 'i18n',
83
     *                              'name'       => 'english',
84
     *                              'meta'       => 'some meta info',
85
     *                              'error_text' => 'not available');
86
     * @param array $options  array('charset'   => 'utf8',
87
     *                              'collation' => 'utf8_general_ci');
88
     *
89
     * @return true|PEAR_Error
90
     */
91
    function addLang($langData, $options = array())
92
    {
93
        $tables = $this->_fetchTableNames();
94
        if (PEAR::isError($tables)) {
95
            return $tables;
96
        }
97
 
98
        $lang_col  = $this->_getLangCol($langData['lang_id']);
99
        $charset   = empty($options['charset'])   ? null : $options['charset'];
100
        $collation = empty($options['collation']) ? null : $options['collation'];
101
        $this->db->loadModule('Manager');
102
 
103
        if (in_array($langData['table_name'], $tables)) {
104
            //table exists
105
            $table_changes = array(
106
                'add' => array(
107
                    $lang_col => array(
108
                        'type' => 'text',
109
                        'charset'   => $charset,
110
                        'collation' => $collation,
111
                    )
112
                )
113
            );
114
            ++$this->_queries;
115
            return $this->db->manager->alterTable($langData['table_name'], $table_changes, false);
116
        }
117
 
118
        //table does not exist
119
        $table_definition = array(
120
            $this->options['string_page_id_col'] => array(
121
                'type'      => 'text',
122
                'length'    => $this->options['string_page_id_col_length'],
123
                'default'   => null,
124
                'charset'   => $charset,
125
                'collation' => $collation,
126
            ),
127
            $this->options['string_id_col'] => array(
128
                'type'      => 'text',
129
                'notnull'   => 1,
130
                'charset'   => $charset,
131
                'collation' => $collation,
132
            ),
133
            $lang_col => array(
134
                'type'      => 'text',
135
                'charset'   => $charset,
136
                'collation' => $collation,
137
            ),
138
        );
139
        ++$this->_queries;
140
        $table_options = array(
141
            'charset' => $charset,
142
            'collate' => $collation,
143
        );
144
        $res = $this->db->manager->createTable($langData['table_name'], $table_definition, $table_options);
145
        if (PEAR::isError($res)) {
146
            return $res;
147
        }
148
        $mysqlClause = ($this->db->phptype == 'mysql') ? '(255)' : '';
149
 
150
        $constraint_name = $langData['table_name']
151
            .'_'. $this->options['string_page_id_col']
152
            .'_'. $this->options['string_id_col'];
153
        $constraint_definition = array(
154
            'fields' => array(
155
                $this->options['string_page_id_col'] => array(),
156
                $this->options['string_id_col'].$mysqlClause => array(),
157
            ),
158
            'unique' => true,
159
        );
160
        ++$this->_queries;
161
        $res = $this->db->manager->createConstraint($langData['table_name'], $constraint_name, $constraint_definition);
162
        if (PEAR::isError($res)) {
163
            return $res;
164
        }
165
 
166
        $index_name = $langData['table_name'] .'_'. $this->options['string_page_id_col'];
167
        $index_definition = array(
168
            'fields' => array($this->options['string_page_id_col'] => array())
169
        );
170
        ++$this->_queries;
171
        $res = $this->db->manager->createIndex($langData['table_name'], $index_name, $index_definition);
172
        if (PEAR::isError($res)) {
173
            return $res;
174
        }
175
 
176
        $index_name = $langData['table_name'] .'_'. $this->options['string_id_col'];
177
        $index_definition = array(
178
            'fields' => array($this->options['string_id_col'] => array('length' => 255))
179
        );
180
        ++$this->_queries;
181
        $res = $this->db->manager->createIndex($langData['table_name'], $index_name, $index_definition);
182
        if (PEAR::isError($res)) {
183
            return $res;
184
        }
185
 
186
        return true;
187
    }
188
 
189
    // }}}
190
    // {{{ addLangToList()
191
 
192
    /**
193
     * Creates a new entry in the langsAvail table.
194
     * If the table doesn't exist yet, it is created.
195
     *
196
     * @param array $langData array('lang_id'    => 'en',
197
     *                              'table_name' => 'i18n',
198
     *                              'name'       => 'english',
199
     *                              'meta'       => 'some meta info',
200
     *                              'error_text' => 'not available',
201
     *                              'encoding'   => 'iso-8859-1');
202
     *
203
     * @return true|PEAR_Error
204
     */
205
    function addLangToList($langData)
206
    {
207
        $tables = $this->_fetchTableNames();
208
        if (PEAR::isError($tables)) {
209
            return $tables;
210
        }
211
 
212
        if (!in_array($this->options['langs_avail_table'], $tables)) {
213
            $queries   = array();
214
            $queries[] = sprintf('CREATE TABLE %s ('
215
                                .'%s VARCHAR(16), '
216
                                .'%s VARCHAR(200), '
217
                                .'%s TEXT, '
218
                                .'%s VARCHAR(250), '
219
                                .'%s VARCHAR(16) )',
220
                $this->db->quoteIdentifier($this->options['langs_avail_table'], true),
221
                $this->db->quoteIdentifier($this->options['lang_id_col'], true),
222
                $this->db->quoteIdentifier($this->options['lang_name_col'], true),
223
                $this->db->quoteIdentifier($this->options['lang_meta_col'], true),
224
                $this->db->quoteIdentifier($this->options['lang_errmsg_col'], true),
225
                $this->db->quoteIdentifier($this->options['lang_encoding_col'], true)
226
            );
227
            $queries[] = sprintf('CREATE UNIQUE INDEX %s_%s_index ON %s (%s)',
228
                $this->db->quoteIdentifier($this->options['langs_avail_table'], true),
229
                $this->db->quoteIdentifier($this->options['lang_id_col'], true),
230
                $this->db->quoteIdentifier($this->options['langs_avail_table'], true),
231
                $this->db->quoteIdentifier($this->options['lang_id_col'], true)
232
            );
233
 
234
            foreach ($queries as $query) {
235
                ++$this->_queries;
236
                $res = $this->db->exec($query);
237
                if (PEAR::isError($res)) {
238
                    return $res;
239
                }
240
            }
241
        }
242
 
243
        $query = sprintf('INSERT INTO %s (%s, %s, %s, %s, %s) VALUES (%s, %s, %s, %s, %s)',
244
            $this->db->quoteIdentifier($this->options['langs_avail_table'], true),
245
            $this->db->quoteIdentifier($this->options['lang_id_col'], true),
246
            $this->db->quoteIdentifier($this->options['lang_name_col'], true),
247
            $this->db->quoteIdentifier($this->options['lang_meta_col'], true),
248
            $this->db->quoteIdentifier($this->options['lang_errmsg_col'], true),
249
            $this->db->quoteIdentifier($this->options['lang_encoding_col'], true),
250
            $this->db->quote($langData['lang_id']),
251
            $this->db->quote($langData['name']),
252
            $this->db->quote($langData['meta']),
253
            $this->db->quote($langData['error_text']),
254
            $this->db->quote($langData['encoding'])
255
        );
256
 
257
        ++$this->_queries;
258
        $res = $this->db->exec($query);
259
        $this->options['strings_tables'][$langData['lang_id']] = $langData['table_name'];
260
        if (PEAR::isError($res)) {
261
            return $res;
262
        }
263
        return true;
264
    }
265
 
266
    // }}}
267
    // {{{ removeLang()
268
 
269
    /**
270
     * Remove the lang from the langsAvail table and drop the strings table.
271
     * If the strings table holds other langs and $force==false, then
272
     * only the lang column is dropped. If $force==true the whole
273
     * table is dropped without any check
274
     *
275
     * @param string  $langID language ID
276
     * @param boolean $force  if true, the whole table is dropped without checks
277
     *
278
     * @return true|PEAR_Error
279
     */
280
    function removeLang($langID, $force)
281
    {
282
        //remove from langsAvail
283
        $query = sprintf('DELETE FROM %s WHERE %s = %s',
284
            $this->db->quoteIdentifier($this->options['langs_avail_table'], true),
285
            $this->db->quoteIdentifier($this->options['lang_id_col'], true),
286
            $this->db->quote($langID, 'text')
287
        );
288
        ++$this->_queries;
289
        $res = $this->db->exec($query);
290
        if (PEAR::isError($res)) {
291
            return $res;
292
        }
293
 
294
        $this->db->loadModule('Manager');
295
        $lang_table = $this->_getLangTable($langID);
296
        if ($force) {
297
            //remove the whole table
298
            ++$this->_queries;
299
            return $this->db->manager->dropTable($lang_table);
300
        }
301
 
302
        //drop only the column for this lang
303
        $table_changes = array(
304
            'remove' => array($this->_getLangCol($langID) => array())
305
        );
306
        ++$this->_queries;
307
        return $this->db->manager->alterTable($lang_table, $table_changes, false);
308
    }
309
 
310
    // }}}
311
    // {{{ updateLang()
312
 
313
    /**
314
     * Update the lang info in the langsAvail table
315
     *
316
     * @param array $langData language data
317
     *
318
     * @return true|PEAR_Error
319
     */
320
    function updateLang($langData)
321
    {
322
        $allFields = array(
323
            //'lang_id'    => 'lang_id_col',
324
            'name'       => 'lang_name_col',
325
            'meta'       => 'lang_meta_col',
326
            'error_text' => 'lang_errmsg_col',
327
            'encoding'   => 'lang_encoding_col',
328
        );
329
        $updateFields = array_keys($langData);
330
        $langSet  = array();
331
        foreach ($allFields as $field => $col) {
332
            if (in_array($field, $updateFields)) {
333
                $langSet[] = $this->db->quoteIdentifier($this->options[$col], true) . ' = ' .
334
                             $this->db->quote($langData[$field]);
335
            }
336
        }
337
        $query = sprintf('UPDATE %s SET %s WHERE %s=%s',
338
            $this->db->quoteIdentifier($this->options['langs_avail_table'], true),
339
            implode(', ', $langSet),
340
            $this->db->quoteIdentifier($this->options['lang_id_col'], true),
341
            $this->db->quote($langData['lang_id'])
342
        );
343
 
344
        ++$this->_queries;
345
        $res = $this->db->exec($query);
346
        $this->fetchLangs();  //update memory cache
347
        if (PEAR::isError($res)) {
348
            return $res;
349
        }
350
        return true;
351
    }
352
 
353
    // }}}
354
    // {{{ add()
355
 
356
    /**
357
     * Add a new entry in the strings table.
358
     *
359
     * @param string $stringID    string ID
360
     * @param string $pageID      page/group ID
361
     * @param array  $stringArray Associative array with string translations.
362
     *               Sample format: array('en' => 'sample', 'it' => 'esempio')
363
     *
364
     * @return true|PEAR_Error
365
     */
366
    function add($stringID, $pageID, $stringArray)
367
    {
368
        $langs = array_intersect(
369
            array_keys($stringArray),
370
            $this->getLangs('ids')
371
        );
372
 
373
        if (!count($langs)) {
374
            //return error: no valid lang provided
375
            return true;
376
        }
377
 
378
        // Langs may be in different tables - we need to split up queries along
379
        // table lines, so we can keep DB traffic to a minimum.
380
 
381
        $unquoted_stringID = $stringID;
382
        $unquoted_pageID   = $pageID;
383
        $stringID          = $this->db->quote($stringID, 'text');
384
        $pageID            = is_null($pageID) ? 'NULL' : $this->db->quote($pageID, 'text');
385
        // Loop over the tables we need to insert into.
386
        foreach ($this->_tableLangs($langs) as $table => $tableLangs) {
387
            $exists = $this->_recordExists($unquoted_stringID, $unquoted_pageID, $table);
388
            if (PEAR::isError($exists)) {
389
                return $exists;
390
            }
391
            $func  = $exists ? '_getUpdateQuery' : '_getInsertQuery';
392
            $query = $this->$func($table, $tableLangs, $stringID, $pageID, $stringArray);
393
 
394
            ++$this->_queries;
395
            $res = $this->db->exec($query);
396
            if (PEAR::isError($res)) {
397
                return $res;
398
            }
399
        }
400
 
401
        return true;
402
    }
403
 
404
    // }}}
405
    // {{{ update()
406
 
407
    /**
408
     * Update an existing entry in the strings table.
409
     *
410
     * @param string $stringID    string ID
411
     * @param string $pageID      page/group ID
412
     * @param array  $stringArray Associative array with string translations.
413
     *               Sample format: array('en' => 'sample', 'it' => 'esempio')
414
     *
415
     * @return true|PEAR_Error
416
     */
417
    function update($stringID, $pageID, $stringArray)
418
    {
419
        return $this->add($stringID, $pageID, $stringArray);
420
    }
421
 
422
    // }}}
423
    // {{{ _getInsertQuery()
424
 
425
    /**
426
     * Build a SQL query to INSERT a record
427
     *
428
     * @param string $table        table name
429
     * @param array  &$tableLangs  tables containing the languages
430
     * @param string $stringID     string ID
431
     * @param string $pageID       page/group ID
432
     * @param array  &$stringArray array of strings
433
     *
434
     * @return string INSERT query
435
     * @access private
436
     */
437
    function _getInsertQuery($table, &$tableLangs, $stringID, $pageID, &$stringArray)
438
    {
439
        $tableCols = $this->_getLangCols($tableLangs);
440
        $langData = array();
441
        foreach ($tableLangs as $lang) {
442
            $langData[$lang] = $this->db->quote($stringArray[$lang], 'text');
443
        }
444
        foreach (array_keys($tableCols) as $k) {
445
            $tableCols[$k] = $this->db->quoteIdentifier($tableCols[$k], true);
446
        }
447
 
448
        return sprintf('INSERT INTO %s (%s, %s, %s) VALUES (%s, %s, %s)',
449
            $this->db->quoteIdentifier($table, true),
450
            $this->db->quoteIdentifier($this->options['string_id_col'], true),
451
            $this->db->quoteIdentifier($this->options['string_page_id_col'], true),
452
            implode(', ', $tableCols),
453
            $stringID,
454
            $pageID,
455
            implode(', ', $langData)
456
        );
457
    }
458
 
459
    // }}}
460
    // {{{ _getUpdateQuery()
461
 
462
    /**
463
     * Build a SQL query to UPDATE a record
464
     *
465
     * @param string $table        table name
466
     * @param array  &$tableLangs  tables containing the languages
467
     * @param string $stringID     string ID
468
     * @param string $pageID       page/group ID
469
     * @param array  &$stringArray array of strings
470
     *
471
     * @return string UPDATE query
472
     * @access private
473
     */
474
    function _getUpdateQuery($table, &$tableLangs, $stringID, $pageID, &$stringArray)
475
    {
476
        $tableCols = $this->_getLangCols($tableLangs);
477
        $langSet = array();
478
        foreach ($tableLangs as $lang) {
479
            $langSet[] = $this->db->quoteIdentifier($tableCols[$lang], true) . ' = ' .
480
                         $this->db->quote($stringArray[$lang], 'text');
481
        }
482
 
483
        return sprintf('UPDATE %s SET %s WHERE %s = %s AND %s = %s',
484
            $this->db->quoteIdentifier($table, true),
485
            implode(', ', $langSet),
486
            $this->db->quoteIdentifier($this->options['string_id_col'], true),
487
            $stringID,
488
            $this->db->quoteIdentifier($this->options['string_page_id_col'], true),
489
            $pageID
490
        );
491
    }
492
 
493
    // }}}
494
    // {{{ remove()
495
 
496
    /**
497
     * Remove an entry from the strings table.
498
     *
499
     * @param string $stringID string ID
500
     * @param string $pageID   page/group ID
501
     *
502
     * @return mixed true on success, PEAR_Error on failure
503
     */
504
    function remove($stringID, $pageID)
505
    {
506
        $tables = array_unique($this->_getLangTables());
507
 
508
        $stringID = $this->db->quote($stringID, 'text');
509
        // get the tables and skip the non existent ones
510
        $dbTables = $this->_fetchTableNames();
511
        foreach ($tables as $table) {
512
            if (!in_array($table, $dbTables)) {
513
                continue;
514
            }
515
            $query = sprintf('DELETE FROM %s WHERE %s = %s AND %s',
516
                 $this->db->quoteIdentifier($table, true),
517
                 $this->db->quoteIdentifier($this->options['string_id_col'], true),
518
                 $stringID,
519
                 $this->db->quoteIdentifier($this->options['string_page_id_col'], true)
520
            );
521
            if (is_null($pageID)) {
522
                $query .= ' IS NULL';
523
            } else {
524
                $query .= ' = ' . $this->db->quote($pageID, 'text');
525
            }
526
 
527
            ++$this->_queries;
528
            $res = $this->db->exec($query);
529
            if (PEAR::isError($res)) {
530
                return $res;
531
            }
532
        }
533
 
534
        return true;
535
    }
536
 
537
    // }}}
538
    // {{{ removePage
539
 
540
    /**
541
     * Remove all the strings in the given page/group
542
     *
543
     * @param string $pageID page/group ID
544
     *
545
     * @return mixed true on success, PEAR_Error on failure
546
     */
547
    function removePage($pageID = null)
548
    {
549
        $tables = array_unique($this->_getLangTables());
550
 
551
        // get the tables and skip the non existent ones
552
        $dbTables = $this->_fetchTableNames();
553
        foreach ($tables as $table) {
554
            if (!in_array($table, $dbTables)) {
555
                continue;
556
            }
557
            $query = sprintf('DELETE FROM %s WHERE %s',
558
                 $this->db->quoteIdentifier($table, true),
559
                 $this->db->quoteIdentifier($this->options['string_page_id_col'], true)
560
            );
561
            if (is_null($pageID)) {
562
                $query .= ' IS NULL';
563
            } else {
564
                $query .= ' = ' . $this->db->quote($pageID, 'text');
565
            }
566
 
567
            ++$this->_queries;
568
            $res = $this->db->exec($query);
569
            if (PEAR::isError($res)) {
570
                return $res;
571
            }
572
        }
573
 
574
        return true;
575
    }
576
 
577
    // }}}
578
    // {{{ getPageNames()
579
 
580
    /**
581
     * Get a list of all the pageIDs in any table.
582
     *
583
     * @return array
584
     */
585
    function getPageNames()
586
    {
587
        $pages = array();
588
        foreach ($this->_getLangTables() as $table) {
589
            $query = sprintf('SELECT DISTINCT %s FROM %s',
590
                 $this->db->quoteIdentifier($this->options['string_page_id_col'], true),
591
                 $this->db->quoteIdentifier($table, true)
592
            );
593
            ++$this->_queries;
594
            $res = $this->db->queryCol($query);
595
            if (PEAR::isError($res)) {
596
                return $res;
597
            }
598
            $pages = array_merge($pages, $res);
599
        }
600
        return array_unique($pages);
601
    }
602
 
603
    // }}}
604
    // {{{ _tableLangs()
605
 
606
    /**
607
     * Get table -> language mapping
608
     *
609
     * The key of the array is the table that a language is stored in;
610
     * the value is an /array/ of languages stored in that table.
611
     *
612
     * @param array $langs Languages to get mapping for
613
     *
614
     * @return array Table -> language mapping
615
     * @access private
616
     * @see    Translation2_Container_MDB2::_getLangTable()
617
     */
618
    function &_tableLangs($langs)
619
    {
620
        $tables = array();
621
        foreach ($langs as $lang) {
622
            $table = $this->_getLangTable($lang);
623
            $tables[$table][] = $lang;
624
        }
625
        return $tables;
626
    }
627
 
628
    // }}}
629
    // {{{ _getLangTables()
630
 
631
    /**
632
     * Get tables for languages
633
     *
634
     * This is like _getLangTable(), but it returns an array of the tables for
635
     * multiple languages.
636
     *
637
     * @param array $langs Languages to get tables for
638
     *
639
     * @return array
640
     * @access private
641
     */
642
    function &_getLangTables($langs = null)
643
    {
644
        $tables = array();
645
        $langs  = !is_array($langs) ? $this->getLangs('ids') : $langs;
646
        foreach ($langs as $lang) {
647
            $tables[] = $this->_getLangTable($lang);
648
        }
649
        $tables = array_unique($tables);
650
        return $tables;
651
    }
652
 
653
    // }}}
654
    // {{{ _getLangCols()
655
 
656
    /**
657
     * Get table columns strings are stored in
658
     *
659
     * This is like _getLangCol(), except it returns an array which contains
660
     * the mapping for multiple languages.
661
     *
662
     * @param array $langs Languages to get mapping for
663
     *
664
     * @return array  Language -> column mapping
665
     * @access private
666
     * @see    Translation2_Container_DB::_getLangCol()
667
     */
668
    function &_getLangCols($langs)
669
    {
670
        $cols = array();
671
        foreach ($langs as $lang) {
672
            $cols[$lang] = $this->_getLangCol($lang);
673
        }
674
        return $cols;
675
    }
676
 
677
    // }}}
678
    // {{{ _recordExists()
679
 
680
    /**
681
     * Check if there's already a record in the table with the
682
     * given (pageID, stringID) pair.
683
     *
684
     * @param string $stringID string ID
685
     * @param string $pageID   page/group ID
686
     * @param string $table    table name
687
     *
688
     * @return boolean|PEAR_Error
689
     * @access private
690
     */
691
    function _recordExists($stringID, $pageID, $table)
692
    {
693
        $stringID = $this->db->quote($stringID, 'text');
694
        $pageID   = is_null($pageID) ? ' IS NULL' : ' = ' . $this->db->quote($pageID, 'text');
695
        $query    = sprintf('SELECT COUNT(*) FROM %s WHERE %s=%s AND %s%s',
696
            $this->db->quoteIdentifier($table, true),
697
            $this->db->quoteIdentifier($this->options['string_id_col'], true),
698
            $stringID,
699
            $this->db->quoteIdentifier($this->options['string_page_id_col'], true),
700
            $pageID
701
        );
702
        ++$this->_queries;
703
        $res = $this->db->queryOne($query);
704
        if (PEAR::isError($res)) {
705
            return $res;
706
        }
707
        return ($res > 0);
708
    }
709
 
710
    // }}}
711
    // {{{ _filterStringsByTable()
712
 
713
    /**
714
     * Get only the strings for the langs in the given table
715
     *
716
     * @param array  $stringArray Associative array with string translations.
717
     *               Sample format: array('en' => 'sample', 'it' => 'esempio')
718
     * @param string $table       table name
719
     *
720
     * @return array strings
721
     * @access private
722
     */
723
    function &_filterStringsByTable($stringArray, $table)
724
    {
725
        $strings = array();
726
        foreach ($stringArray as $lang => $string) {
727
            if ($table == $this->_getLangTable($lang)) {
728
                $strings[$lang] = $string;
729
            }
730
        }
731
        return $strings;
732
    }
733
 
734
    // }}}
735
    // {{{ _getLangsInTable()
736
 
737
    /**
738
     * Get the languages sharing the given table
739
     *
740
     * @param string $table table name
741
     *
742
     * @return array
743
     */
744
    function &_getLangsInTable($table)
745
    {
746
        $this->fetchLangs(); // force cache refresh
747
        $langsInTable = array();
748
        foreach (array_keys($this->langs) as $lang) {
749
            if ($table == $this->_getLangTable($lang)) {
750
                $langsInTable[] = $lang;
751
            }
752
        }
753
        return $langsInTable;
754
    }
755
 
756
    // }}}
757
}
758
?>