diff --git a/admin/include/c13y_internal.class.php b/admin/include/c13y_internal.class.php index 313989bfa..27df84246 100644 --- a/admin/include/c13y_internal.class.php +++ b/admin/include/c13y_internal.class.php @@ -42,13 +42,17 @@ class c13y_internal $check_list = array(); - $check_list[] = array('type' => 'PHP', 'current' => phpversion(), 'required' => REQUIRED_PHP_VERSION); + $check_list[] = array( + 'type' => 'PHP', + 'current' => phpversion(), + 'required' => REQUIRED_PHP_VERSION, + ); - $db_version = pwg_get_db_version(); - $check_list[] = array('type' => $conf['dblayer'], - 'current' => $db_version, - 'required' => constant('REQUIRED_'.str_replace('-', '_', strtoupper($conf['dblayer'])).'_VERSION') - ); + $check_list[] = array( + 'type' => 'MySQL', + 'current' => pwg_get_db_version(), + 'required' => REQUIRED_MYSQL_VERSION, + ); foreach ($check_list as $elem) { diff --git a/admin/intro.php b/admin/intro.php index d49f6d48a..c454e50b4 100644 --- a/admin/intro.php +++ b/admin/intro.php @@ -197,7 +197,7 @@ $template->assign( 'PWG_VERSION' => PHPWG_VERSION, 'OS' => PHP_OS, 'PHP_VERSION' => phpversion(), - 'DB_ENGINE' => $conf['dblayer'], + 'DB_ENGINE' => 'MySQL', 'DB_VERSION' => $db_version, 'DB_ELEMENTS' => l10n_dec('%d photo', '%d photos', $nb_elements), 'DB_CATEGORIES' => diff --git a/i.php b/i.php index c4702c0d7..d0d70cc84 100644 --- a/i.php +++ b/i.php @@ -402,8 +402,8 @@ include_once( PHPWG_ROOT_PATH .'/include/derivative_std_params.inc.php'); try { - $pwg_db_link = pwg_db_connect($conf['db_host'], $conf['db_user'], - $conf['db_password'], $conf['db_base']); + pwg_db_connect($conf['db_host'], $conf['db_user'], + $conf['db_password'], $conf['db_base']); } catch (Exception $e) { @@ -511,7 +511,7 @@ else { $page['rotation_angle'] = 0; } -pwg_db_close($pwg_db_link); +pwg_db_close(); if (!try_switch_source($params, $src_mtime) && $params->type==IMG_CUSTOM) { diff --git a/include/dblayer/functions_mysql.inc.php b/include/dblayer/functions_mysql.inc.php index 0b2c35195..f8182bdf4 100644 --- a/include/dblayer/functions_mysql.inc.php +++ b/include/dblayer/functions_mysql.inc.php @@ -40,11 +40,7 @@ function pwg_db_connect($host, $user, $password, $database) { throw new Exception("Can't connect to server"); } - if (mysql_select_db($database, $link)) - { - return $link; - } - else + if (!mysql_select_db($database, $link)) { throw new Exception('Connection to server succeed, but it was impossible to connect to database'); } @@ -138,7 +134,7 @@ SELECT IF(MAX('.$column.')+1 IS NULL, 1, MAX('.$column.')+1) return $next; } -function pwg_db_changes($result) +function pwg_db_changes() { return mysql_affected_rows(); } @@ -173,14 +169,14 @@ function pwg_db_real_escape_string($s) return mysql_real_escape_string($s); } -function pwg_db_insert_id($table=null, $column='id') +function pwg_db_insert_id() { return mysql_insert_id(); } -function pwg_db_close($link=null) +function pwg_db_close() { - return mysql_close($link); + return mysql_close(); } /** diff --git a/include/dblayer/functions_mysqli.inc.php b/include/dblayer/functions_mysqli.inc.php new file mode 100644 index 000000000..af42b865b --- /dev/null +++ b/include/dblayer/functions_mysqli.inc.php @@ -0,0 +1,798 @@ +select_db($database)) + { + throw new Exception('Connection to server succeed, but it was impossible to connect to database'); + } +} + +function pwg_db_check_charset() +{ + $db_charset = 'utf8'; + if (defined('DB_CHARSET') and DB_CHARSET != '') + { + $db_charset = DB_CHARSET; + } + pwg_query('SET NAMES "'.$db_charset.'"'); +} + +function pwg_db_check_version() +{ + $current_mysql = pwg_get_db_version(); + if (version_compare($current_mysql, REQUIRED_MYSQL_VERSION, '<')) + { + fatal_error( + sprintf( + 'your MySQL version is too old, you have "%s" and you need at least "%s"', + $current_mysql, + REQUIRED_MYSQL_VERSION + ) + ); + } +} + +function pwg_get_db_version() +{ + global $mysqli; + + return $mysqli->server_info; +} + +function pwg_query($query) +{ + global $mysqli, $conf, $page, $debug, $t2; + + $start = microtime(true); + ($result = $mysqli->query($query)) or my_error($query, $conf['die_on_sql_error']); + + $time = microtime(true) - $start; + + if (!isset($page['count_queries'])) + { + $page['count_queries'] = 0; + $page['queries_time'] = 0; + } + + $page['count_queries']++; + $page['queries_time']+= $time; + + if ($conf['show_queries']) + { + $output = ''; + $output.= '
['.$page['count_queries'].'] ';
+ $output.= "\n".$query;
+ $output.= "\n".'(this query time : ';
+ $output.= ''.number_format($time, 3, '.', ' ').' s)';
+ $output.= "\n".'(total SQL time : ';
+ $output.= number_format($page['queries_time'], 3, '.', ' ').' s)';
+ $output.= "\n".'(total time : ';
+ $output.= number_format( ($time+$start-$t2), 3, '.', ' ').' s)';
+ if ( $result!=null and preg_match('/\s*SELECT\s+/i',$query) )
+ {
+ $output.= "\n".'(num rows : ';
+ $output.= pwg_db_num_rows($result).' )';
+ }
+ elseif ( $result!=null
+ and preg_match('/\s*INSERT|UPDATE|REPLACE|DELETE\s+/i',$query) )
+ {
+ $output.= "\n".'(affected rows : ';
+ $output.= pwg_db_changes().' )';
+ }
+ $output.= "\n";
+
+ $debug .= $output;
+ }
+
+ return $result;
+}
+
+function pwg_db_nextval($column, $table)
+{
+ $query = '
+SELECT IF(MAX('.$column.')+1 IS NULL, 1, MAX('.$column.')+1)
+ FROM '.$table;
+ list($next) = pwg_db_fetch_row(pwg_query($query));
+
+ return $next;
+}
+
+function pwg_db_changes()
+{
+ global $mysqli;
+
+ return $mysqli->affected_rows;
+}
+
+function pwg_db_num_rows($result)
+{
+ return $result->num_rows;
+}
+
+function pwg_db_fetch_assoc($result)
+{
+ return $result->fetch_assoc();
+}
+
+function pwg_db_fetch_row($result)
+{
+ return $result->fetch_row();
+}
+
+function pwg_db_fetch_object($result)
+{
+ return $result->fetch_object();
+}
+
+function pwg_db_free_result($result)
+{
+ return $result->free_result();
+}
+
+function pwg_db_real_escape_string($s)
+{
+ global $mysqli;
+
+ return $mysqli->real_escape_string($s);
+}
+
+function pwg_db_insert_id()
+{
+ global $mysqli;
+
+ return $mysqli->insert_id;
+}
+
+function pwg_db_close()
+{
+ global $mysqli;
+
+ return $mysqli->close();
+}
+
+/**
+ *
+ * complex functions
+ *
+ */
+
+/**
+ * creates an array based on a query, this function is a very common pattern
+ * used here
+ *
+ * @param string $query
+ * @param string $fieldname optional
+ * @return array
+ */
+function array_from_query($query, $fieldname=false)
+{
+ $array = array();
+
+ $result = pwg_query($query);
+ if (false === $fieldname)
+ {
+ while ($row = pwg_db_fetch_assoc($result))
+ {
+ $array[] = $row;
+ }
+ }
+ else
+ {
+ while ($row = pwg_db_fetch_assoc($result))
+ {
+ $array[] = $row[$fieldname];
+ }
+ }
+ return $array;
+}
+
+define('MASS_UPDATES_SKIP_EMPTY', 1);
+/**
+ * updates multiple lines in a table
+ *
+ * @param string table_name
+ * @param array dbfields
+ * @param array datas
+ * @param int flags - if MASS_UPDATES_SKIP_EMPTY - empty values do not overwrite existing ones
+ * @return void
+ */
+function mass_updates($tablename, $dbfields, $datas, $flags=0)
+{
+ if (count($datas) == 0)
+ return;
+
+ // depending on the MySQL version, we use the multi table update or N update queries
+ if (count($datas) < 10)
+ {
+ foreach ($datas as $data)
+ {
+ $query = '
+UPDATE '.$tablename.'
+ SET ';
+ $is_first = true;
+ foreach ($dbfields['update'] as $key)
+ {
+ $separator = $is_first ? '' : ",\n ";
+
+ if (isset($data[$key]) and $data[$key] != '')
+ {
+ $query.= $separator.$key.' = \''.$data[$key].'\'';
+ }
+ else
+ {
+ if ( $flags & MASS_UPDATES_SKIP_EMPTY )
+ continue; // next field
+ $query.= "$separator$key = NULL";
+ }
+ $is_first = false;
+ }
+ if (!$is_first)
+ {// only if one field at least updated
+ $query.= '
+ WHERE ';
+ $is_first = true;
+ foreach ($dbfields['primary'] as $key)
+ {
+ if (!$is_first)
+ {
+ $query.= ' AND ';
+ }
+ if ( isset($data[$key]) )
+ {
+ $query.= $key.' = \''.$data[$key].'\'';
+ }
+ else
+ {
+ $query.= $key.' IS NULL';
+ }
+ $is_first = false;
+ }
+ pwg_query($query);
+ }
+ } // foreach update
+ } // if mysqli_ver or count");
+ trigger_error($error, E_USER_WARNING);
+ echo("");
+}
+
+?>
\ No newline at end of file
diff --git a/install.php b/install.php
index 8d8704087..bc9bc2144 100644
--- a/install.php
+++ b/install.php
@@ -128,7 +128,7 @@ $dbhost = (!empty($_POST['dbhost'])) ? $_POST['dbhost'] : 'localhost';
$dbuser = (!empty($_POST['dbuser'])) ? $_POST['dbuser'] : '';
$dbpasswd = (!empty($_POST['dbpasswd'])) ? $_POST['dbpasswd'] : '';
$dbname = (!empty($_POST['dbname'])) ? $_POST['dbname'] : '';
-$dblayer = 'mysql';
+$dblayer = extension_loaded('mysqli') ? 'mysqli' : 'mysql';
$admin_name = (!empty($_POST['admin_name'])) ? $_POST['admin_name'] : '';
$admin_pass1 = (!empty($_POST['admin_pass1'])) ? $_POST['admin_pass1'] : '';
diff --git a/install/db/134-database.php b/install/db/134-database.php
new file mode 100644
index 000000000..235a72331
--- /dev/null
+++ b/install/db/134-database.php
@@ -0,0 +1,43 @@
+
\ No newline at end of file