Project

General

Profile

1
<?php
2
/**
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
/**
19
 * WbDatabaseHelper.php
20
 *
21
 * @category     Core
22
 * @package      Core_database
23
 * @author       Manuela v.d.Decken <manuela@isteam.de>
24
 * @author       Dietmar W. <dietmar.woellbrink@websitebaker.org>
25
 * @copyright    Manuela v.d.Decken <manuela@isteam.de>
26
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
27
 * @version      0.0.9
28
 * @revision     $Revision: 2105 $
29
 * @lastmodified $Date: 2014-11-24 18:41:13 +0100 (Mo, 24 Nov 2014) $
30
 * @deprecated   from WB version number 2.9
31
 * @description  Mysql database wrapper for use with websitebaker up to version 2.8.4
32
 */
33

    
34
abstract class WbDatabaseHelper {
35

    
36

    
37
/**
38
 * Alias for isField()
39
 * @deprecated from WB-2.8.5 and higher
40
 */
41
    public function field_exists($table_name, $field_name)
42
    {
43
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
44
        return $this->isField($table_name, $field_name);
45
    }
46
/*
47
 * @param string full name of the table (incl. TABLE_PREFIX)
48
 * @param string name of the field to seek for
49
 * @return bool true if field exists
50
 */
51
    public function isField($table_name, $field_name)
52
    {
53
        $sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` ';
54
        $query = $this->doQuery($sql);
55
        return ($query->numRows() != 0);
56
    }
57
/**
58
 * Alias for isIndex()
59
 * @deprecated from WB-2.8.5 and higher
60
 */
61
    public function index_exists($table_name, $index_name, $number_fields = 0)
62
    {
63
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
64
        return $this->isIndex($table_name, $index_name, $number_fields = 0);
65
    }
66
/*
67
 * isIndex
68
 * @param string full name of the table (incl. TABLE_PREFIX)
69
 * @param string name of the index to seek for
70
 * @return bool true if field exists
71
 */
72
    public function isIndex($table_name, $index_name, $number_fields = 0)
73
    {
74
        $number_fields = intval($number_fields);
75
        $keys = 0;
76
        $sql = 'SHOW INDEX FROM `'.$table_name.'`';
77
        if (($res_keys = $this->doQuery($sql))) {
78
            while (($rec_key = $res_keys->fetchAssoc())) {
79
                if ( $rec_key['Key_name'] == $index_name ) {
80
                    $keys++;
81
                }
82
            }
83

    
84
        }
85
        if ( $number_fields == 0 ) {
86
            return ($keys != $number_fields);
87
        } else {
88
            return ($keys == $number_fields);
89
        }
90
    }
91
/**
92
 * Alias for addField()
93
 * @deprecated from WB-2.8.5 and higher
94
 */
95
    public function field_add($table_name, $field_name, $description)
96
    {
97
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
98
        return $this->addField($table_name, $field_name, $description);
99
    }
100
/*
101
 * @param string full name of the table (incl. TABLE_PREFIX)
102
 * @param string name of the field to add
103
 * @param string describes the new field like ( INT NOT NULL DEFAULT '0')
104
 * @return bool true if successful, otherwise false and error will be set
105
 */
106
    public function addField($table_name, $field_name, $description)
107
    {
108
        if (!$this->isField($table_name, $field_name)) {
109
        // add new field into a table
110
            $sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' ';
111
            $query = $this->doQuery($sql);
112
            $this->set_error(mysqli_error($this->oDbHandle));
113
            if (!$this->isError()) {
114
                return ( $this->isField($table_name, $field_name) ) ? true : false;
115
            }
116
        } else {
117
            $this->setError('field \''.$field_name.'\' already exists');
118
        }
119
        return false;
120
    }
121
/**
122
 * Alias for modifyField()
123
 * @deprecated from WB-2.8.5 and higher
124
 */
125
    public function field_modify($table_name, $field_name, $description)
126
    {
127
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
128
        return $this->modifyField($table_name, $field_name, $description);
129
    }
130
/*
131
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
132
 * @param string $field_name: name of the field to add
133
 * @param string $description: describes the new field like ( INT NOT NULL DEFAULT '0')
134
 * @return bool: true if successful, otherwise false and error will be set
135
 */
136
    public function modifyField($table_name, $field_name, $description)
137
    {
138
        $retval = false;
139
        if ($this->isField($table_name, $field_name)) {
140
        // modify a existing field in a table
141
            $sql  = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description;
142
            $retval = ( $this->doQuery($sql) ? true : false);
143
            $this->setError(mysqli_error($this->oDbHandle));
144
        }
145
        return $retval;
146
    }
147
/**
148
 * Alias for removeField()
149
 * @deprecated from WB-2.8.5 and higher
150
 */
151
    public function field_remove($table_name, $field_name)
152
    {
153
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
154
        return $this->removeField($table_name, $field_name);
155
    }
156
/*
157
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
158
 * @param string $field_name: name of the field to remove
159
 * @return bool: true if successful, otherwise false and error will be set
160
 */
161
    public function removeField($table_name, $field_name)
162
    {
163
        $retval = false;
164
        if ($this->isField($table_name, $field_name)) {
165
        // modify a existing field in a table
166
            $sql  = 'ALTER TABLE `'.$table_name.'` DROP `'.$field_name.'`';
167
            $retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false );
168
        }
169
        return $retval;
170
    }
171
/**
172
 * Alias for addIndex()
173
 * @deprecated from WB-2.8.5 and higher
174
 */
175
    public function index_add($table_name, $index_name, $field_list, $index_type = 'KEY')
176
    {
177
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
178
        return $this->addIndex($table_name, $index_name, $field_list, $index_type);
179
    }
180
/*
181
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
182
 * @param string $index_name: name of the new index (empty string for PRIMARY)
183
 * @param string $field_list: comma seperated list of fields for this index
184
 * @param string $index_type: kind of index (PRIMARY, UNIQUE, KEY, FULLTEXT)
185
 * @return bool: true if successful, otherwise false and error will be set
186
 */
187
    public function addIndex($table_name, $index_name, $field_list, $index_type = 'KEY')
188
    {
189
       $retval = false;
190
       $field_list = explode(',', (str_replace(' ', '', $field_list)));
191
       $number_fields = sizeof($field_list);
192
       $field_list = '`'.implode('`,`', $field_list).'`';
193
       $index_name = $index_type == 'PRIMARY' ? $index_type : $index_name;
194
       if ( $this->isIndex($table_name, $index_name, $number_fields) ||
195
            $this->isIndex($table_name, $index_name))
196
       {
197
           $sql  = 'ALTER TABLE `'.$table_name.'` ';
198
           $sql .= 'DROP INDEX `'.$index_name.'`';
199
           if (!$this->doQuery($sql)) { return false; }
200
       }
201
       $sql  = 'ALTER TABLE `'.$table_name.'` ';
202
       $sql .= 'ADD '.$index_type.' ';
203
       $sql .= $index_type == 'PRIMARY' ? 'KEY ' : '`'.$index_name.'` ';
204
       $sql .= '( '.$field_list.' ); ';
205
       if ($this->doQuery($sql)) { $retval = true; }
206
       return $retval;
207
   }
208
/**
209
 * Alias for removeIndex()
210
 * @deprecated from WB-2.8.5 and higher
211
 */
212
    public function index_remove($table_name, $index_name)
213
    {
214
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
215
        return $this->removeIndex($table_name, $index_name);
216
    }
217
/*
218
 * @param string $table_name: full name of the table (incl. TABLE_PREFIX)
219
 * @param string $field_name: name of the field to remove
220
 * @return bool: true if successful, otherwise false and error will be set
221
 */
222
    public function removeIndex($table_name, $index_name)
223
    {
224
        $retval = false;
225
        if ($this->isIndex($table_name, $index_name)) {
226
        // modify a existing field in a table
227
            $sql  = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`';
228
            $retval = ( $this->doQuery($sql) ? true : false );
229
        }
230
        return $retval;
231
    }
232
/**
233
 * Alias for importSql()
234
 * @deprecated from WB-2.8.5 and higher
235
 */
236
    public function SqlImport($sSqlDump,
237
                              $sTablePrefix = '',
238
                              $sAction      = 'install',
239
                              $sEngine      = 'MyISAM',
240
                              $sCollation   = 'utf8_unicode_ci')
241
    {
242
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
243
        $oImport = new SqlImport($this, $sSqlDump);
244
        return $oImport->doImport($sAction);
245
    }
246

    
247
/**
248
 * retuns the type of the engine used for requested table
249
 * @param string $table name of the table, including prefix
250
 * @return boolean/string false on error, or name of the engine (myIsam/InnoDb)
251
 */
252
    public function getTableEngine($table)
253
    {
254
        trigger_error('Deprecated function call: '.__CLASS__.'::'.__METHOD__, E_USER_DEPRECATED);
255
        $retVal = false;
256
        $mysqlVersion = mysqli_get_server_info($this->oDbHandle);
257
        $engineValue = (version_compare($mysqlVersion, '5.0') < 0) ? 'Type' : 'Engine';
258
        $sql = 'SHOW TABLE STATUS FROM `' . $this->sDbName . '` LIKE \'' . $table . '\'';
259
        if (($result = $this->doQuery($sql))) {
260
            if (($row = $result->fetchAssoc())) {
261
                $retVal = $row[$engineValue];
262
            }
263
        }
264
        return $retVal;
265
    }
266
}
267

    
268
// end of class WbDatabaseHelper
269
// //////////////////////////////////////////////////////////////////////////////////// //
270
/* this functions are placed inside this file temporarely until a better place is found */
271
/* function to update a var/value-pair(s) in table ****************************
272
 * nonexisting keys are inserted
273
 * @param string $table: name of table to use (without prefix)
274
 * @param mixed $key:    a array of key->value pairs to update
275
 *                       or a string with name of the key to update
276
 * @param string $value: a sting with needed value, if $key is a string too
277
 * @return bool:  true if any keys are updated, otherwise false
278
 */
279
    function updateDbKeyValue($table, $key, $value = '')
280
    {
281
        $oDb = WbDatabase::getInstance();
282
        $table = preg_replace('/^'.preg_quote($oDb->TablePrefix, '/').'/s', '', $table);
283
        if (!is_array($key)) {
284
            if (trim($key) != '') {
285
                $key = array( trim($key) => trim($value) );
286
            } else {
287
                $key = array();
288
            }
289
        }
290
        $retval = true;
291
        $sNameValPairs = '';
292
        foreach ($key as $index => $val) {
293
            $sNameValPairs .= ', (\''.$index.'\', \''.$oDb->escapeString($val).'\')';
294
        }
295
        $sValues = ltrim($sNameValPairs, ', ');
296
        if ($sValues != '') {
297
            $sql = 'REPLACE INTO `'.$oDb->TablePrefix.$table.'` (`name`, `value`) '
298
                 . 'VALUES '.$sValues;
299
            if (!$oDb->doQuery($sql)) {
300
                $retval = false;
301
            }
302
        }
303
        return $retval;
304
    }
305
/**
306
 * Alias for updateDbKeyValue()
307
 * @param string $table: name of table to use (without prefix)
308
 * @param mixed $key:    a array of key->value pairs to update
309
 *                        or a string with name of the key to update
310
 * @param string $value: a sting with needed value, if $key is a string too
311
 * @return bool:  true if any keys are updated, otherwise false
312
 * @deprecated from 2.8.4
313
 */
314
    function db_update_key_value($table, $key, $value = '')
315
    {
316
        trigger_error('Deprecated function call: '.basename(__FILE__).'::'.__FUNCTION__, E_USER_DEPRECATED);
317
        return updateDbKeyValue($table, $key, $value);
318
    }
(21-21/39)