Plugin index
<?php
if (txpinterface == 'admin' and gps('event') == 'rvm_utf8_to_utf8mb4')
{
add_privs('rvm_utf8_to_utf8mb4', '1');
register_tab('extensions', 'rvm_utf8_to_utf8mb4', 'utf8_to_utf8mb4');
register_callback('rvm_utf8_to_utf8mb4', 'rvm_utf8_to_utf8mb4');
}
function rvm_utf8_to_utf8mb4()
{
global $txpcfg, $DB, $dbversion;
$debug = FALSE;
$error = FALSE;
require_privs('rvm_utf8_to_utf8mb4');
pagetop('');
echo '<div style="margin-left: 50px">';
if (mysqli_get_server_version($DB->link) < 50503) {
$error = 'MySQL version is lower than 5.5.3';
} else {
if (false !== strpos(mysqli_get_client_info($DB->link), 'mysqlnd')) {
// mysqlnd 5.0.9+ required
if (mysqli_get_client_version($mylink) < 50009) {
$error = 'mysqlnd client version is below 5.0.9';
}
} else {
// libmysqlclient 5.5.3+ required
if (mysqli_get_client_version($DB->link) < 50503) {
$error = 'libmysqlclient version is below 5.5.3';
}
}
}
if (version_compare($dbversion, '4.6.0', '<')) {
$error = 'Textpattern database version is below 4.6.0. Upgrade Textpattern first';
}
if ($txpcfg['dbcharset'] == 'latin1') {
$error = 'dbcharset is latin1, so you have to use the rvm_latin1_to_utf8 plugin first';
}
if ($error)
{
echo graf('The rvm_utf8_to_utf8mb4 plugin has been removed, because your '.$error);
safe_delete('txp_plugin', "name = 'rvm_utf8_to_utf8mb4'");
rvm_utf8_to_utf8mb4_end();
}
# standard TXP tables
$txptables = array(
'textpattern',
'txp_category',
'txp_css',
'txp_discuss',
'txp_discuss_ipban',
'txp_discuss_nonce',
'txp_file',
'txp_form',
'txp_image',
'txp_lang',
'txp_link',
'txp_log',
'txp_page',
'txp_plugin',
'txp_prefs',
'txp_section',
'txp_token',
'txp_users'
);
# find tables that match the TXP table prefix, mark standard and selected ones.
$mytables = ps('mytables', array()) ? ps('mytables') : array();
$unknown = FALSE;
if ($rs = safe_query("SHOW TABLES LIKE '".addcslashes(doSlash(PFX), '%_')."%'"))
{
while ($row = mysqli_fetch_row($rs))
{
$table = substr($row[0], strlen(PFX));
if (in_array($table, $txptables) or in_array($table, $mytables))
{
$tables[] = $table;
}
else
{
$unknown = TRUE;
}
$inputs[] =
'<label>'.
'<input type="checkbox" name="mytables[]" value="'.htmlspecialchars($table).'" '.
(in_array($table, $txptables) ? 'disabled="disabled" checked="checked"' : '').' />'.
htmlspecialchars(PFX.$table).
'</label>';
}
}
else
{
echo graf('Hmmm... strange, I cannot find any tables that match your TXP table prefix');
}
# ask user what to do with tables we're not sure about.
if ($unknown and !ps('continue'))
{
echo
graf('Below, a list of tables is shown that may be part of your Textpattern install.').
graf('Some of these tables may have been created by plugins you have installed.<br />
In that case, check the tables that these plugins have added.<br />
Be careful not to check tables that are not related to this Textpattern install!').
graf('The standard Textpattern tables are already checked.').
form(
join('<br />', $inputs).
graf(
eInput('rvm_utf8_to_utf8mb4').'<br />'.
fInput('submit', 'continue', 'Continue', 'publish')
)
);
rvm_utf8_to_utf8mb4_end();
}
echo graf('Updating Textpattern tables...');
# loop through tables
foreach ($tables as $table)
{
$utf8mb4 = array();
# prepare alter statements for columns
$columns = getRows('SHOW COLUMNS FROM '.safe_pfx($table));
if ($columns) foreach ($columns as $column)
{
extract($column);
if (preg_match('/^(char|varchar|tinytext|text|mediumtext|longtext|enum|set)\b/', $Type))
{
$utf8mb4[] = 'MODIFY `'.doSlash($Field).'` '.$Type.'
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci'.
($Null == 'YES' ? ' NULL' : ' NOT NULL').
($Default == 'NULL'
? ((preg_match('/text/', $Type) || $Null == 'NO') ? '' : 'DEFAULT NULL')
: " DEFAULT '".doSlash($Default)."'"
);
}
}
$success = TRUE;
# alter the table
$success *= safe_alter($table, join(', ', $utf8mb4).', DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci', $debug);
}
# while we're here, might as well optimize the tables
safe_query("OPTIMIZE TABLE ".join(', ', array_map('safe_pfx', $tables)), $debug);
if ($success)
{
echo graf('Successfully completed.');
$file = txpath.'/config.php';
$config = file_get_contents($file);
$config = str_replace('$txpcfg[\'dbcharset\'] = \'utf8\''.n, '$txpcfg[\'dbcharset\'] = \'utf8\';'.n, $config); # fix missing ; on line added by in rvm_latin1_to_utf8
$config = str_replace('?>', n.'# rvm_utf8_to_utf8mb4 plugin added the following line to ensure a correct dbcharset'.n.'$txpcfg[\'dbcharset\'] = \'utf8mb4\';'.n.'?>', $config);
if ($txpcfg['dbcharset'] == 'utf8mb4'
or is_writable($file)
and $handle = fopen($file, 'w')
and fwrite($handle, $config) !== FALSE
and fclose($handle))
{
echo graf('The rvm_utf8_to_utf8mb4 plugin has been automatically de-installed.');
safe_delete('txp_plugin', "name = 'rvm_utf8_to_utf8mb4'");
}
else
{
echo graf('<b>WARNING: textpattern/config.php could not be updated automatically. Please update your textpattern/config.php file to contain <code>$txpcfg[\'dbcharset\'] = \'utf8mb4\';</code></b> and deinstall the rvm_utf8_to_utf8mb4 plugin manually');
}
}
else
{
echo graf('Due to one or more errors the tables could not be updated correctly. You may have to restore a backup.');
}
rvm_utf8_to_utf8mb4_end();
}
function rvm_utf8_to_utf8mb4_end()
{
echo '</div>';
end_page();
exit();
}
?>