Skip to content

Commit 77d0cfd

Browse files
committed
Ability to define connection and table creation charset and collation
1 parent bc1a8bc commit 77d0cfd

5 files changed

Lines changed: 43 additions & 4 deletions

File tree

qa-config-example.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,17 @@
4949
5. Place all the Question2Answer files on your server.
5050
6. Open the appropriate URL, and follow the instructions.
5151
52-
More detailed installation instructions here: http://www.question2answer.org/
52+
More detailed installation instructions here: https://www.question2answer.org
5353
*/
5454

55+
56+
/*
57+
The QA_MYSQL_CHARSET and QA_MYSQL_COLLATION define the charset and collation used by Q2A to
58+
connect to the server, and to create tables and fields.
59+
*/
60+
61+
define('QA_MYSQL_CHARSET', 'utf8mb4');
62+
define('QA_MYSQL_COLLATION', 'utf8mb4_unicode_ci');
5563
/*
5664
======================================================================
5765
OPTIONAL CONSTANT DEFINITIONS, INCLUDING SUPPORT FOR SINGLE SIGN-ON

qa-include/db/install.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ function qa_db_create_table_sql($rawname, $definition)
714714
if (isset($coldef))
715715
$querycols .= (strlen($querycols) ? ', ' : '') . (is_int($colname) ? $coldef : ($colname . ' ' . $coldef));
716716

717-
return 'CREATE TABLE ^' . $rawname . ' (' . $querycols . ') ENGINE=InnoDB CHARSET=utf8';
717+
return 'CREATE TABLE ^' . $rawname . ' (' . $querycols . ') ENGINE=InnoDB ' . qa_get_table_charset_collation();
718718
}
719719

720720

qa-include/qa-base.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ function qa_undo_wordpress_quoting($param, $isget)
313313
define('QA_FINAL_MYSQL_PORT', QA_MYSQL_PORT);
314314
}
315315

316+
if (!defined('QA_MYSQL_CHARSET')) {
317+
define('QA_MYSQL_CHARSET', 'utf8'); // Use hardcoded default until v1.8.8
318+
}
319+
if (!defined('QA_MYSQL_COLLATION')) {
320+
define('QA_MYSQL_COLLATION', null); // Use hardcoded default until v1.8.8
321+
}
322+
316323
// Possible URL schemes for Q2A and the string used for url scheme testing
317324

318325
define('QA_URL_FORMAT_INDEX', 0); // http://...../index.php/123/why-is-the-sky-blue

qa-include/qa-db.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,15 @@ function qa_db_connect($failhandler = null)
9494
// From Q2A 1.5, we explicitly set the character encoding of the MySQL connection, instead of using lots of "SELECT BINARY col"-style queries.
9595
// Testing showed that overhead is minimal, so this seems worth trading off against the benefit of more straightforward queries, especially
9696
// for plugin developers.
97-
if (!$db->set_charset('utf8'))
97+
if ($db->set_charset(QA_MYSQL_CHARSET)) {
98+
$sql = 'SET NAMES ' . QA_MYSQL_CHARSET;
99+
if (QA_MYSQL_COLLATION !== null) {
100+
$sql .= ' COLLATE ' . QA_MYSQL_COLLATION;
101+
}
102+
$db->query($sql);
103+
} else {
98104
qa_db_fail_error('set_charset', $db->errno, $db->error);
105+
}
99106

100107
qa_report_process_stage('db_connected');
101108

@@ -174,6 +181,23 @@ function qa_db_disconnect()
174181
}
175182
}
176183

184+
/**
185+
* Return the default charset and collation string to use in CREATE TABLE statements. For fields that
186+
* have not been set an implicit charset or collation, these values will be assigned at creation time.
187+
* Collation might be null to provide backwards compatibility with Q2A 1.8.6 and before.
188+
* @param string $charset
189+
* @param string $collation
190+
* @return string
191+
*/
192+
function qa_get_table_charset_collation($charset = QA_MYSQL_CHARSET, $collation = QA_MYSQL_COLLATION)
193+
{
194+
$sql = 'DEFAULT CHARACTER SET ' . $charset;
195+
if ($collation !== null) {
196+
$sql .= ' COLLATE ' . $collation;
197+
}
198+
199+
return $sql;
200+
}
177201

178202
/**
179203
* Run the raw $query, call the global failure handler if necessary, otherwise return the result resource.

qa-plugin/event-logger/qa-event-logger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function init_queries($table_list)
4444
'KEY ipaddress (ipaddress),' .
4545
'KEY userid (userid),' .
4646
'KEY event (event)' .
47-
') ENGINE=MyISAM DEFAULT CHARSET=utf8';
47+
') ENGINE=MyISAM ' . qa_get_table_charset_collation();
4848
} else {
4949
// table exists: check it has the correct schema
5050
$column = qa_db_read_one_assoc(qa_db_query_sub('SHOW COLUMNS FROM ^eventlog WHERE Field="ipaddress"'));

0 commit comments

Comments
 (0)