[4325] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[4325] | 4 | // +-----------------------------------------------------------------------+ |
---|
[12922] | 5 | // | Copyright(C) 2008-2012 Piwigo Team http://piwigo.org | |
---|
[4325] | 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
| 23 | |
---|
[4367] | 24 | define('DB_ENGINE', 'MySQL'); |
---|
[5782] | 25 | define('REQUIRED_MYSQL_VERSION', '5.0.0'); |
---|
[4367] | 26 | |
---|
| 27 | define('DB_REGEX_OPERATOR', 'REGEXP'); |
---|
| 28 | define('DB_RANDOM_FUNCTION', 'RAND'); |
---|
| 29 | |
---|
[4325] | 30 | /** |
---|
| 31 | * |
---|
| 32 | * simple functions |
---|
| 33 | * |
---|
| 34 | */ |
---|
| 35 | |
---|
[5236] | 36 | function pwg_db_connect($host, $user, $password, $database) |
---|
[4325] | 37 | { |
---|
[5236] | 38 | $link = @mysql_connect($host, $user, $password); |
---|
| 39 | if (!$link) |
---|
| 40 | { |
---|
| 41 | throw new Exception("Can't connect to server"); |
---|
| 42 | } |
---|
| 43 | if (mysql_select_db($database, $link)) |
---|
| 44 | { |
---|
| 45 | return $link; |
---|
| 46 | } |
---|
| 47 | else |
---|
| 48 | { |
---|
| 49 | throw new Exception('Connection to server succeed, but it was impossible to connect to database'); |
---|
| 50 | } |
---|
[4325] | 51 | } |
---|
| 52 | |
---|
| 53 | function pwg_db_check_charset() |
---|
| 54 | { |
---|
[5982] | 55 | $db_charset = 'utf8'; |
---|
| 56 | if (defined('DB_CHARSET') and DB_CHARSET != '') |
---|
[4325] | 57 | { |
---|
[5982] | 58 | $db_charset = DB_CHARSET; |
---|
[4325] | 59 | } |
---|
[5982] | 60 | pwg_query('SET NAMES "'.$db_charset.'"'); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | function pwg_db_check_version() |
---|
| 64 | { |
---|
| 65 | $current_mysql = pwg_get_db_version(); |
---|
| 66 | if (version_compare($current_mysql, REQUIRED_MYSQL_VERSION, '<')) |
---|
[4325] | 67 | { |
---|
[5982] | 68 | fatal_error( |
---|
| 69 | sprintf( |
---|
| 70 | 'your MySQL version is too old, you have "%s" and you need at least "%s"', |
---|
| 71 | $current_mysql, |
---|
| 72 | REQUIRED_MYSQL_VERSION |
---|
| 73 | ) |
---|
| 74 | ); |
---|
[4325] | 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | function pwg_get_db_version() |
---|
| 79 | { |
---|
[8844] | 80 | return mysql_get_server_info(); |
---|
[4325] | 81 | } |
---|
| 82 | |
---|
| 83 | function pwg_query($query) |
---|
| 84 | { |
---|
| 85 | global $conf,$page,$debug,$t2; |
---|
| 86 | |
---|
[13038] | 87 | $start = microtime(true); |
---|
[4325] | 88 | ($result = mysql_query($query)) or my_error($query, $conf['die_on_sql_error']); |
---|
| 89 | |
---|
[13038] | 90 | $time = microtime(true) - $start; |
---|
[4325] | 91 | |
---|
| 92 | if (!isset($page['count_queries'])) |
---|
| 93 | { |
---|
| 94 | $page['count_queries'] = 0; |
---|
| 95 | $page['queries_time'] = 0; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | $page['count_queries']++; |
---|
| 99 | $page['queries_time']+= $time; |
---|
| 100 | |
---|
| 101 | if ($conf['show_queries']) |
---|
| 102 | { |
---|
| 103 | $output = ''; |
---|
| 104 | $output.= '<pre>['.$page['count_queries'].'] '; |
---|
| 105 | $output.= "\n".$query; |
---|
| 106 | $output.= "\n".'(this query time : '; |
---|
| 107 | $output.= '<b>'.number_format($time, 3, '.', ' ').' s)</b>'; |
---|
| 108 | $output.= "\n".'(total SQL time : '; |
---|
| 109 | $output.= number_format($page['queries_time'], 3, '.', ' ').' s)'; |
---|
| 110 | $output.= "\n".'(total time : '; |
---|
| 111 | $output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)'; |
---|
| 112 | if ( $result!=null and preg_match('/\s*SELECT\s+/i',$query) ) |
---|
| 113 | { |
---|
| 114 | $output.= "\n".'(num rows : '; |
---|
| 115 | $output.= mysql_num_rows($result).' )'; |
---|
| 116 | } |
---|
| 117 | elseif ( $result!=null |
---|
| 118 | and preg_match('/\s*INSERT|UPDATE|REPLACE|DELETE\s+/i',$query) ) |
---|
| 119 | { |
---|
| 120 | $output.= "\n".'(affected rows : '; |
---|
| 121 | $output.= mysql_affected_rows().' )'; |
---|
| 122 | } |
---|
| 123 | $output.= "</pre>\n"; |
---|
| 124 | |
---|
| 125 | $debug .= $output; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | return $result; |
---|
| 129 | } |
---|
| 130 | |
---|
[4367] | 131 | function pwg_db_nextval($column, $table) |
---|
| 132 | { |
---|
| 133 | $query = ' |
---|
| 134 | SELECT IF(MAX('.$column.')+1 IS NULL, 1, MAX('.$column.')+1) |
---|
| 135 | FROM '.$table; |
---|
| 136 | list($next) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 137 | |
---|
| 138 | return $next; |
---|
| 139 | } |
---|
| 140 | |
---|
[4325] | 141 | function pwg_db_changes($result) |
---|
| 142 | { |
---|
[5192] | 143 | return mysql_affected_rows(); |
---|
[4325] | 144 | } |
---|
| 145 | |
---|
| 146 | function pwg_db_num_rows($result) |
---|
| 147 | { |
---|
| 148 | return mysql_num_rows($result); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | function pwg_db_fetch_assoc($result) |
---|
| 152 | { |
---|
| 153 | return mysql_fetch_assoc($result); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | function pwg_db_fetch_row($result) |
---|
| 157 | { |
---|
| 158 | return mysql_fetch_row($result); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | function pwg_db_fetch_object($result) |
---|
| 162 | { |
---|
| 163 | return mysql_fetch_object($result); |
---|
| 164 | } |
---|
| 165 | |
---|
[4332] | 166 | function pwg_db_free_result($result) |
---|
[4325] | 167 | { |
---|
| 168 | return mysql_free_result($result); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | function pwg_db_real_escape_string($s) |
---|
| 172 | { |
---|
| 173 | return mysql_real_escape_string($s); |
---|
| 174 | } |
---|
| 175 | |
---|
[4892] | 176 | function pwg_db_insert_id($table=null, $column='id') |
---|
[4325] | 177 | { |
---|
| 178 | return mysql_insert_id(); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * |
---|
| 183 | * complex functions |
---|
| 184 | * |
---|
| 185 | */ |
---|
| 186 | |
---|
| 187 | /** |
---|
| 188 | * creates an array based on a query, this function is a very common pattern |
---|
| 189 | * used here |
---|
| 190 | * |
---|
| 191 | * @param string $query |
---|
| 192 | * @param string $fieldname |
---|
| 193 | * @return array |
---|
| 194 | */ |
---|
| 195 | function array_from_query($query, $fieldname) |
---|
| 196 | { |
---|
| 197 | $array = array(); |
---|
| 198 | |
---|
| 199 | $result = pwg_query($query); |
---|
| 200 | while ($row = mysql_fetch_assoc($result)) |
---|
| 201 | { |
---|
[13038] | 202 | $array[] = $row[$fieldname]; |
---|
[4325] | 203 | } |
---|
| 204 | |
---|
| 205 | return $array; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | define('MASS_UPDATES_SKIP_EMPTY', 1); |
---|
| 209 | /** |
---|
| 210 | * updates multiple lines in a table |
---|
| 211 | * |
---|
| 212 | * @param string table_name |
---|
| 213 | * @param array dbfields |
---|
| 214 | * @param array datas |
---|
| 215 | * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones |
---|
| 216 | * @return void |
---|
| 217 | */ |
---|
| 218 | function mass_updates($tablename, $dbfields, $datas, $flags=0) |
---|
| 219 | { |
---|
| 220 | if (count($datas) == 0) |
---|
| 221 | return; |
---|
[11485] | 222 | |
---|
[4325] | 223 | // depending on the MySQL version, we use the multi table update or N update queries |
---|
[8844] | 224 | if (count($datas) < 10) |
---|
| 225 | { |
---|
[4325] | 226 | foreach ($datas as $data) |
---|
| 227 | { |
---|
| 228 | $query = ' |
---|
| 229 | UPDATE '.$tablename.' |
---|
| 230 | SET '; |
---|
| 231 | $is_first = true; |
---|
| 232 | foreach ($dbfields['update'] as $key) |
---|
| 233 | { |
---|
| 234 | $separator = $is_first ? '' : ",\n "; |
---|
| 235 | |
---|
[11485] | 236 | if (isset($data[$key]) and $data[$key] != '') |
---|
[4325] | 237 | { |
---|
| 238 | $query.= $separator.$key.' = \''.$data[$key].'\''; |
---|
| 239 | } |
---|
| 240 | else |
---|
| 241 | { |
---|
[11485] | 242 | if ( $flags & MASS_UPDATES_SKIP_EMPTY ) |
---|
[4325] | 243 | continue; // next field |
---|
| 244 | $query.= "$separator$key = NULL"; |
---|
| 245 | } |
---|
| 246 | $is_first = false; |
---|
| 247 | } |
---|
| 248 | if (!$is_first) |
---|
| 249 | {// only if one field at least updated |
---|
| 250 | $query.= ' |
---|
| 251 | WHERE '; |
---|
| 252 | $is_first = true; |
---|
| 253 | foreach ($dbfields['primary'] as $key) |
---|
| 254 | { |
---|
| 255 | if (!$is_first) |
---|
| 256 | { |
---|
| 257 | $query.= ' AND '; |
---|
| 258 | } |
---|
| 259 | if ( isset($data[$key]) ) |
---|
| 260 | { |
---|
| 261 | $query.= $key.' = \''.$data[$key].'\''; |
---|
| 262 | } |
---|
| 263 | else |
---|
| 264 | { |
---|
| 265 | $query.= $key.' IS NULL'; |
---|
| 266 | } |
---|
| 267 | $is_first = false; |
---|
| 268 | } |
---|
| 269 | pwg_query($query); |
---|
| 270 | } |
---|
| 271 | } // foreach update |
---|
| 272 | } // if mysql_ver or count<X |
---|
| 273 | else |
---|
| 274 | { |
---|
| 275 | // creation of the temporary table |
---|
| 276 | $query = ' |
---|
| 277 | SHOW FULL COLUMNS FROM '.$tablename; |
---|
| 278 | $result = pwg_query($query); |
---|
| 279 | $columns = array(); |
---|
| 280 | $all_fields = array_merge($dbfields['primary'], $dbfields['update']); |
---|
| 281 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 282 | { |
---|
| 283 | if (in_array($row['Field'], $all_fields)) |
---|
| 284 | { |
---|
| 285 | $column = $row['Field']; |
---|
| 286 | $column.= ' '.$row['Type']; |
---|
| 287 | |
---|
| 288 | $nullable = true; |
---|
| 289 | if (!isset($row['Null']) or $row['Null'] == '' or $row['Null']=='NO') |
---|
| 290 | { |
---|
| 291 | $column.= ' NOT NULL'; |
---|
| 292 | $nullable = false; |
---|
| 293 | } |
---|
| 294 | if (isset($row['Default'])) |
---|
| 295 | { |
---|
| 296 | $column.= " default '".$row['Default']."'"; |
---|
| 297 | } |
---|
| 298 | elseif ($nullable) |
---|
| 299 | { |
---|
| 300 | $column.= " default NULL"; |
---|
| 301 | } |
---|
| 302 | if (isset($row['Collation']) and $row['Collation'] != 'NULL') |
---|
| 303 | { |
---|
| 304 | $column.= " collate '".$row['Collation']."'"; |
---|
| 305 | } |
---|
| 306 | array_push($columns, $column); |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | $temporary_tablename = $tablename.'_'.micro_seconds(); |
---|
| 311 | |
---|
| 312 | $query = ' |
---|
| 313 | CREATE TABLE '.$temporary_tablename.' |
---|
| 314 | ( |
---|
| 315 | '.implode(",\n ", $columns).', |
---|
| 316 | UNIQUE KEY the_key ('.implode(',', $dbfields['primary']).') |
---|
| 317 | )'; |
---|
| 318 | |
---|
| 319 | pwg_query($query); |
---|
| 320 | mass_inserts($temporary_tablename, $all_fields, $datas); |
---|
| 321 | if ( $flags & MASS_UPDATES_SKIP_EMPTY ) |
---|
| 322 | $func_set = create_function('$s', 'return "t1.$s = IFNULL(t2.$s, t1.$s)";'); |
---|
| 323 | else |
---|
| 324 | $func_set = create_function('$s', 'return "t1.$s = t2.$s";'); |
---|
| 325 | |
---|
| 326 | // update of images table by joining with temporary table |
---|
| 327 | $query = ' |
---|
| 328 | UPDATE '.$tablename.' AS t1, '.$temporary_tablename.' AS t2 |
---|
| 329 | SET '. |
---|
| 330 | implode( |
---|
| 331 | "\n , ", |
---|
| 332 | array_map($func_set,$dbfields['update']) |
---|
| 333 | ).' |
---|
| 334 | WHERE '. |
---|
| 335 | implode( |
---|
| 336 | "\n AND ", |
---|
| 337 | array_map( |
---|
| 338 | create_function('$s', 'return "t1.$s = t2.$s";'), |
---|
| 339 | $dbfields['primary'] |
---|
| 340 | ) |
---|
| 341 | ); |
---|
| 342 | pwg_query($query); |
---|
| 343 | $query = ' |
---|
| 344 | DROP TABLE '.$temporary_tablename; |
---|
| 345 | pwg_query($query); |
---|
| 346 | } |
---|
| 347 | } |
---|
| 348 | |
---|
[11485] | 349 | /** |
---|
| 350 | * updates one line in a table |
---|
| 351 | * |
---|
| 352 | * @param string table_name |
---|
[11991] | 353 | * @param array set_fields |
---|
| 354 | * @param array where_fields |
---|
[11485] | 355 | * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones |
---|
| 356 | * @return void |
---|
| 357 | */ |
---|
[11991] | 358 | function single_update($tablename, $set_fields, $where_fields, $flags=0) |
---|
[11485] | 359 | { |
---|
[11991] | 360 | if (count($set_fields) == 0) |
---|
| 361 | { |
---|
[11485] | 362 | return; |
---|
[11991] | 363 | } |
---|
[4325] | 364 | |
---|
[11485] | 365 | $query = ' |
---|
| 366 | UPDATE '.$tablename.' |
---|
| 367 | SET '; |
---|
| 368 | $is_first = true; |
---|
[11991] | 369 | foreach ($set_fields as $key => $value) |
---|
[11485] | 370 | { |
---|
| 371 | $separator = $is_first ? '' : ",\n "; |
---|
| 372 | |
---|
[11991] | 373 | if (isset($value) and $value != '') |
---|
[11485] | 374 | { |
---|
[11991] | 375 | $query.= $separator.$key.' = \''.$value.'\''; |
---|
[11485] | 376 | } |
---|
| 377 | else |
---|
| 378 | { |
---|
| 379 | if ( $flags & MASS_UPDATES_SKIP_EMPTY ) |
---|
| 380 | continue; // next field |
---|
| 381 | $query.= "$separator$key = NULL"; |
---|
| 382 | } |
---|
| 383 | $is_first = false; |
---|
| 384 | } |
---|
| 385 | if (!$is_first) |
---|
| 386 | {// only if one field at least updated |
---|
| 387 | $query.= ' |
---|
| 388 | WHERE '; |
---|
| 389 | $is_first = true; |
---|
[11991] | 390 | foreach ($where_fields as $key => $value) |
---|
[11485] | 391 | { |
---|
| 392 | if (!$is_first) |
---|
| 393 | { |
---|
| 394 | $query.= ' AND '; |
---|
| 395 | } |
---|
[11991] | 396 | if ( isset($value) ) |
---|
[11485] | 397 | { |
---|
[11991] | 398 | $query.= $key.' = \''.$value.'\''; |
---|
[11485] | 399 | } |
---|
| 400 | else |
---|
| 401 | { |
---|
| 402 | $query.= $key.' IS NULL'; |
---|
| 403 | } |
---|
| 404 | $is_first = false; |
---|
| 405 | } |
---|
| 406 | pwg_query($query); |
---|
| 407 | } |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | |
---|
[4325] | 411 | /** |
---|
| 412 | * inserts multiple lines in a table |
---|
| 413 | * |
---|
| 414 | * @param string table_name |
---|
| 415 | * @param array dbfields |
---|
| 416 | * @param array inserts |
---|
| 417 | * @return void |
---|
| 418 | */ |
---|
[12748] | 419 | function mass_inserts($table_name, $dbfields, $datas, $options=array()) |
---|
[4325] | 420 | { |
---|
[12748] | 421 | $ignore = ''; |
---|
| 422 | if (isset($options['ignore']) and $options['ignore']) |
---|
| 423 | { |
---|
| 424 | $ignore = 'IGNORE'; |
---|
| 425 | } |
---|
| 426 | |
---|
[4325] | 427 | if (count($datas) != 0) |
---|
| 428 | { |
---|
| 429 | $first = true; |
---|
| 430 | |
---|
| 431 | $query = 'SHOW VARIABLES LIKE \'max_allowed_packet\''; |
---|
| 432 | list(, $packet_size) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 433 | $packet_size = $packet_size - 2000; // The last list of values MUST not exceed 2000 character*/ |
---|
| 434 | $query = ''; |
---|
| 435 | |
---|
| 436 | foreach ($datas as $insert) |
---|
| 437 | { |
---|
| 438 | if (strlen($query) >= $packet_size) |
---|
| 439 | { |
---|
| 440 | pwg_query($query); |
---|
| 441 | $first = true; |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | if ($first) |
---|
| 445 | { |
---|
| 446 | $query = ' |
---|
[12748] | 447 | INSERT '.$ignore.' INTO '.$table_name.' |
---|
[4325] | 448 | ('.implode(',', $dbfields).') |
---|
| 449 | VALUES'; |
---|
| 450 | $first = false; |
---|
| 451 | } |
---|
| 452 | else |
---|
| 453 | { |
---|
| 454 | $query .= ' |
---|
| 455 | , '; |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | $query .= '('; |
---|
| 459 | foreach ($dbfields as $field_id => $dbfield) |
---|
| 460 | { |
---|
| 461 | if ($field_id > 0) |
---|
| 462 | { |
---|
| 463 | $query .= ','; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | if (!isset($insert[$dbfield]) or $insert[$dbfield] === '') |
---|
| 467 | { |
---|
| 468 | $query .= 'NULL'; |
---|
| 469 | } |
---|
| 470 | else |
---|
| 471 | { |
---|
| 472 | $query .= "'".$insert[$dbfield]."'"; |
---|
| 473 | } |
---|
| 474 | } |
---|
| 475 | $query .= ')'; |
---|
| 476 | } |
---|
| 477 | pwg_query($query); |
---|
| 478 | } |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | /** |
---|
[11991] | 482 | * inserts one line in a table |
---|
[11485] | 483 | * |
---|
| 484 | * @param string table_name |
---|
| 485 | * @param array dbfields |
---|
| 486 | * @param array insert |
---|
| 487 | * @return void |
---|
| 488 | */ |
---|
[11991] | 489 | function single_insert($table_name, $data) |
---|
[11485] | 490 | { |
---|
[11991] | 491 | if (count($data) != 0) |
---|
[11485] | 492 | { |
---|
| 493 | $query = ' |
---|
| 494 | INSERT INTO '.$table_name.' |
---|
[11991] | 495 | ('.implode(',', array_keys($data)).') |
---|
[11485] | 496 | VALUES'; |
---|
| 497 | |
---|
| 498 | $query .= '('; |
---|
[11991] | 499 | $is_first = true; |
---|
| 500 | foreach ($data as $key => $value) |
---|
[11485] | 501 | { |
---|
[11991] | 502 | if (!$is_first) |
---|
[11485] | 503 | { |
---|
| 504 | $query .= ','; |
---|
| 505 | } |
---|
[11991] | 506 | else |
---|
[11485] | 507 | { |
---|
[11991] | 508 | $is_first = false; |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | if ($value === '') |
---|
| 512 | { |
---|
[11485] | 513 | $query .= 'NULL'; |
---|
| 514 | } |
---|
| 515 | else |
---|
| 516 | { |
---|
[11991] | 517 | $query .= "'".$value."'"; |
---|
[11485] | 518 | } |
---|
| 519 | } |
---|
| 520 | $query .= ')'; |
---|
| 521 | |
---|
| 522 | pwg_query($query); |
---|
| 523 | } |
---|
| 524 | } |
---|
| 525 | |
---|
| 526 | /** |
---|
[4325] | 527 | * Do maintenance on all PWG tables |
---|
| 528 | * |
---|
| 529 | * @return none |
---|
| 530 | */ |
---|
| 531 | function do_maintenance_all_tables() |
---|
| 532 | { |
---|
| 533 | global $prefixeTable, $page; |
---|
| 534 | |
---|
| 535 | $all_tables = array(); |
---|
| 536 | |
---|
| 537 | // List all tables |
---|
| 538 | $query = 'SHOW TABLES LIKE \''.$prefixeTable.'%\''; |
---|
| 539 | $result = pwg_query($query); |
---|
[4335] | 540 | while ($row = pwg_db_fetch_row($result)) |
---|
[4325] | 541 | { |
---|
| 542 | array_push($all_tables, $row[0]); |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | // Repair all tables |
---|
| 546 | $query = 'REPAIR TABLE '.implode(', ', $all_tables); |
---|
| 547 | $mysql_rc = pwg_query($query); |
---|
| 548 | |
---|
| 549 | // Re-Order all tables |
---|
| 550 | foreach ($all_tables as $table_name) |
---|
| 551 | { |
---|
| 552 | $all_primary_key = array(); |
---|
| 553 | |
---|
| 554 | $query = 'DESC '.$table_name.';'; |
---|
| 555 | $result = pwg_query($query); |
---|
| 556 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 557 | { |
---|
| 558 | if ($row['Key'] == 'PRI') |
---|
| 559 | { |
---|
| 560 | array_push($all_primary_key, $row['Field']); |
---|
| 561 | } |
---|
| 562 | } |
---|
| 563 | |
---|
| 564 | if (count($all_primary_key) != 0) |
---|
| 565 | { |
---|
| 566 | $query = 'ALTER TABLE '.$table_name.' ORDER BY '.implode(', ', $all_primary_key).';'; |
---|
| 567 | $mysql_rc = $mysql_rc && pwg_query($query); |
---|
| 568 | } |
---|
| 569 | } |
---|
| 570 | |
---|
| 571 | // Optimize all tables |
---|
| 572 | $query = 'OPTIMIZE TABLE '.implode(', ', $all_tables); |
---|
| 573 | $mysql_rc = $mysql_rc && pwg_query($query); |
---|
| 574 | if ($mysql_rc) |
---|
| 575 | { |
---|
| 576 | array_push( |
---|
| 577 | $page['infos'], |
---|
[5036] | 578 | l10n('All optimizations have been successfully completed.') |
---|
[4325] | 579 | ); |
---|
| 580 | } |
---|
| 581 | else |
---|
| 582 | { |
---|
| 583 | array_push( |
---|
| 584 | $page['errors'], |
---|
[5036] | 585 | l10n('Optimizations have been completed with some errors.') |
---|
[4325] | 586 | ); |
---|
| 587 | } |
---|
| 588 | } |
---|
| 589 | |
---|
[4833] | 590 | function pwg_db_concat($array) |
---|
| 591 | { |
---|
| 592 | $string = implode($array, ','); |
---|
[5192] | 593 | return 'CONCAT('. $string.')'; |
---|
[4833] | 594 | } |
---|
| 595 | |
---|
[4781] | 596 | function pwg_db_concat_ws($array, $separator) |
---|
[4367] | 597 | { |
---|
[4781] | 598 | $string = implode($array, ','); |
---|
| 599 | return 'CONCAT_WS(\''.$separator.'\','. $string.')'; |
---|
[4367] | 600 | } |
---|
| 601 | |
---|
[4387] | 602 | function pwg_db_cast_to_text($string) |
---|
[4367] | 603 | { |
---|
[12010] | 604 | return $string; |
---|
[4367] | 605 | } |
---|
| 606 | |
---|
| 607 | /** |
---|
| 608 | * returns an array containing the possible values of an enum field |
---|
| 609 | * |
---|
| 610 | * @param string tablename |
---|
| 611 | * @param string fieldname |
---|
| 612 | */ |
---|
| 613 | function get_enums($table, $field) |
---|
| 614 | { |
---|
| 615 | // retrieving the properties of the table. Each line represents a field : |
---|
| 616 | // columns are 'Field', 'Type' |
---|
| 617 | $result = pwg_query('desc '.$table); |
---|
| 618 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 619 | { |
---|
| 620 | // we are only interested in the the field given in parameter for the |
---|
| 621 | // function |
---|
| 622 | if ($row['Field'] == $field) |
---|
| 623 | { |
---|
| 624 | // retrieving possible values of the enum field |
---|
| 625 | // enum('blue','green','black') |
---|
| 626 | $options = explode(',', substr($row['Type'], 5, -1)); |
---|
| 627 | foreach ($options as $i => $option) |
---|
| 628 | { |
---|
| 629 | $options[$i] = str_replace("'", '',$option); |
---|
| 630 | } |
---|
| 631 | } |
---|
| 632 | } |
---|
| 633 | pwg_db_free_result($result); |
---|
| 634 | return $options; |
---|
| 635 | } |
---|
| 636 | |
---|
[12752] | 637 | /** |
---|
| 638 | * Smartly checks if a variable is equivalent to true or false |
---|
| 639 | * |
---|
| 640 | * @param mixed input |
---|
| 641 | * @return bool |
---|
| 642 | */ |
---|
| 643 | function get_boolean($input) |
---|
[4367] | 644 | { |
---|
[12752] | 645 | if ('false' === strtolower($input)) |
---|
[4367] | 646 | { |
---|
[12752] | 647 | return false; |
---|
[4367] | 648 | } |
---|
[12752] | 649 | |
---|
| 650 | return (bool)$input; |
---|
[4367] | 651 | } |
---|
| 652 | |
---|
| 653 | /** |
---|
| 654 | * returns boolean string 'true' or 'false' if the given var is boolean |
---|
| 655 | * |
---|
| 656 | * @param mixed $var |
---|
| 657 | * @return mixed |
---|
| 658 | */ |
---|
| 659 | function boolean_to_string($var) |
---|
| 660 | { |
---|
| 661 | if (is_bool($var)) |
---|
| 662 | { |
---|
| 663 | return $var ? 'true' : 'false'; |
---|
| 664 | } |
---|
| 665 | else |
---|
| 666 | { |
---|
| 667 | return $var; |
---|
| 668 | } |
---|
| 669 | } |
---|
| 670 | |
---|
[4385] | 671 | /** |
---|
| 672 | * |
---|
| 673 | * interval and date functions |
---|
| 674 | * |
---|
| 675 | */ |
---|
| 676 | |
---|
| 677 | function pwg_db_get_recent_period_expression($period, $date='CURRENT_DATE') |
---|
| 678 | { |
---|
| 679 | if ($date!='CURRENT_DATE') |
---|
| 680 | { |
---|
| 681 | $date = '\''.$date.'\''; |
---|
| 682 | } |
---|
| 683 | |
---|
| 684 | return 'SUBDATE('.$date.',INTERVAL '.$period.' DAY)'; |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | function pwg_db_get_recent_period($period, $date='CURRENT_DATE') |
---|
| 688 | { |
---|
| 689 | $query = ' |
---|
| 690 | SELECT '.pwg_db_get_recent_period_expression($period); |
---|
| 691 | list($d) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 692 | |
---|
| 693 | return $d; |
---|
| 694 | } |
---|
| 695 | |
---|
[6604] | 696 | function pwg_db_get_flood_period_expression($seconds) |
---|
| 697 | { |
---|
| 698 | return 'SUBDATE(now(), INTERVAL '.$seconds.' SECOND)'; |
---|
| 699 | } |
---|
| 700 | |
---|
[6578] | 701 | function pwg_db_get_hour($date) |
---|
| 702 | { |
---|
| 703 | return 'hour('.$date.')'; |
---|
| 704 | } |
---|
| 705 | |
---|
[4385] | 706 | function pwg_db_get_date_YYYYMM($date) |
---|
| 707 | { |
---|
| 708 | return 'DATE_FORMAT('.$date.', \'%Y%m\')'; |
---|
| 709 | } |
---|
| 710 | |
---|
| 711 | function pwg_db_get_date_MMDD($date) |
---|
| 712 | { |
---|
| 713 | return 'DATE_FORMAT('.$date.', \'%m%d\')'; |
---|
| 714 | } |
---|
| 715 | |
---|
| 716 | function pwg_db_get_year($date) |
---|
| 717 | { |
---|
| 718 | return 'YEAR('.$date.')'; |
---|
| 719 | } |
---|
| 720 | |
---|
| 721 | function pwg_db_get_month($date) |
---|
| 722 | { |
---|
| 723 | return 'MONTH('.$date.')'; |
---|
| 724 | } |
---|
| 725 | |
---|
| 726 | function pwg_db_get_week($date, $mode=null) |
---|
| 727 | { |
---|
| 728 | if ($mode) |
---|
| 729 | { |
---|
| 730 | return 'WEEK('.$date.', '.$mode.')'; |
---|
| 731 | } |
---|
| 732 | else |
---|
| 733 | { |
---|
| 734 | return 'WEEK('.$date.')'; |
---|
| 735 | } |
---|
| 736 | } |
---|
| 737 | |
---|
| 738 | function pwg_db_get_dayofmonth($date) |
---|
| 739 | { |
---|
| 740 | return 'DAYOFMONTH('.$date.')'; |
---|
| 741 | } |
---|
| 742 | |
---|
| 743 | function pwg_db_get_dayofweek($date) |
---|
| 744 | { |
---|
| 745 | return 'DAYOFWEEK('.$date.')'; |
---|
| 746 | } |
---|
| 747 | |
---|
| 748 | function pwg_db_get_weekday($date) |
---|
| 749 | { |
---|
| 750 | return 'WEEKDAY('.$date.')'; |
---|
| 751 | } |
---|
| 752 | |
---|
[6666] | 753 | function pwg_db_date_to_ts($date) |
---|
| 754 | { |
---|
| 755 | return 'UNIX_TIMESTAMP('.$date.')'; |
---|
| 756 | } |
---|
| 757 | |
---|
[4325] | 758 | // my_error returns (or send to standard output) the message concerning the |
---|
| 759 | // error occured for the last mysql query. |
---|
| 760 | function my_error($header, $die) |
---|
| 761 | { |
---|
| 762 | $error = "[mysql error ".mysql_errno().'] '.mysql_error()."\n"; |
---|
| 763 | $error .= $header; |
---|
| 764 | |
---|
| 765 | if ($die) |
---|
| 766 | { |
---|
| 767 | fatal_error($error); |
---|
| 768 | } |
---|
| 769 | echo("<pre>"); |
---|
| 770 | trigger_error($error, E_USER_WARNING); |
---|
| 771 | echo("</pre>"); |
---|
| 772 | } |
---|
| 773 | |
---|
| 774 | ?> |
---|