From a6345fd8b38825470384a39dad04275cc2186431 Mon Sep 17 00:00:00 2001 From: kjmtsh Date: Wed, 11 Sep 2013 19:39:01 +0000 Subject: [PATCH] Added the database maintenance utility. Fixed and Changed the procedure for ALTER TABLE and SHOW INDEX query. Fixed some bugs. git-svn-id: https://plugins.svn.wordpress.org/sqlite-integration/trunk@770386 b8457f37-d9ea-0310-8a92-e5e31aec5664 --- languages/sqlite-integration-ja.mo | Bin 44139 -> 47935 bytes languages/sqlite-integration-ja.po | 110 +++++++ pdoengine.class.php | 8 + query_alter.class.php | 282 ++++++++++------- readme-ja.txt | 13 +- readme.txt | 13 +- sqlite-integration.php | 14 +- utilities/database_maintenance.php | 484 +++++++++++++++++++++++++++++ utilities/documentation.php | 1 + utilities/patch.php | 1 + utilities/utility.php | 6 + 11 files changed, 810 insertions(+), 122 deletions(-) create mode 100644 utilities/database_maintenance.php diff --git a/languages/sqlite-integration-ja.mo b/languages/sqlite-integration-ja.mo index f7a9b8a2fd69415a20fbdeda4c6da0391e055952..372e7f2f190196dfffbea972fc2068a23c3e5693 100644 GIT binary patch delta 7681 zcmbuC33yaRw#O?0LI@zTghepkARuDXd6WYCcSEsZ^=`wJ)&UxcafdzcB6GnBdoc7V6TfskOS zeAoq+!N#nw_S0bvbsQS-3?z!`JJ^7Q8#9Z9vK3!S=rUa10VO3p;cajr6bEKOvY~?T zZdeYPO1%Ol#lMH*$agT@kxoP0y%pXK?|>7acpiaSa2u3zI0a>avo33)OmrE_f_2(! z2U|n2JGkrxZ^Rw}{{v2gl9JK468||#NIs!2c5gdY)3rp3&pW1uo*0a;-Ceo zBUKH%!Qa6g=1E~U6R}4V?^)P;;0)NJvr-orFXiQJ*wcys1nlIl#Qz|j$z7G20~=%$ z=47fIipLhS&xAXm?7R_`CFAL^6YL0^!jVvFzytZG=I~JuZikYxJuZ(xdG2-BJ{_hb zIXnwble>J{84{`QX@4`Qj>I#{yq~n$Ljo>i-TU^!0jZ$J#?#up8uLsZlOx!w0Yz!xr!j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bdU~ZWH8y+b-CZn$GADfK7U<=xT*`H@N80@U^{48dcVidQiiqqgyL3+JJ3g@rut@(jZ;m9%kx zhs>iANk^&eC&?a5FBPg>}3Uy4S4eJKRG18R~>K?=vZHyx%-dlTcT_ z8g&7?P!Ho5_y%6bWDGyRlZ0B>cKM8$q7BN&jPELJyPUV{N`YOUXB&lCcD)Lj)32os zHFn2U+VQHZ9c`MfKfR7n4<_^+9VCVkUl97K>4@V+SD|Gcp)*b)_z-htgAXnFEukqR zY{YniS4|256u*5x*i~JyKe*bv+JK@2H{0;uw^|lgzzUC5n2WtBm!F!LtSn1UrgmR(U=Hxb(-Jq zf1rMx$Rozt{nKrCpq>eR)H)Km#LH4cC7xJKL=x=@Ej@@F;sZjTEG->$|63E=h>3)j z3WMu)0k6yKb{E@kZ1s?(6ZORFcHc(Z!^nAceX35tY~m;}jo3rHEIPg{Csu03E{E^} z@p~fC?wf&5Vh!;Mkx48fz9h5n9gb{$HcglNhBv zl#|3NVu>~=mkh2yHteeF>$;oW;OTOG&uep_-KT6KE)(uVD6y8%a>n5L>*#HL{yWjp zkVvxMD7W>*#e`+I6L2o^DKXRT^TSC*3Gohbl^9EC89{tSY$l?KmxUo+e+Q@qdH4Uc1CO=< diff --git a/languages/sqlite-integration-ja.po b/languages/sqlite-integration-ja.po index 1467a58..5e96c70 100644 --- a/languages/sqlite-integration-ja.po +++ b/languages/sqlite-integration-ja.po @@ -1227,3 +1227,113 @@ msgid "" "continue." msgstr "" "この内容を保存してもよろしいですか?\\n\\nやめるなら[キャンセル]を、続けるなら[OK]をクリック。" + +#: database_maintenance.php:314 +msgid "Can't create backup file." +msgstr "バックアップファイルを作れません。" + +#: database_maintenance.php:334 +msgid "Failed: " +msgstr "失敗: " + +#: database_maintenance.php:395 +msgid "Maintenance" +msgstr "メンテナンス" + +#: database_maintenance.php:399 +msgid "Database Maintenace" +msgstr "データベース・メンテナンス" + +#: database_maintenance.php:400 +msgid "Important Notice" +msgstr "重要なお知らせ" + +#: database_maintenance.php:402 +msgid "" +"When you installed WordPress 3.5.x with SQLite Integration and upgraded to " +"3.6, your database might not function as expected." +msgstr "" +"WordPress 3.5.x を SQLite Integrationを使ってインストールし、3.6 にアップグレードした場合、" +"データベースが意図したとおりに動作しないかもしれません。" + +#: database_maintenance.php:403 +msgid "" +"This page provide you the database sanity check utility and the restore " +"utility." +msgstr "" +"このページでは、データベースが正常かどうかをチェックするユーティリティと、回復ユーティリティを提供します。" + +#: database_maintenance.php:406 +msgid "" +"Click \"Sanity Check\" button first, and see if you need to fix database or " +"not. If needed, click \"Fix Database\" button. Afterward you may go to " +"Miscellaneous page and optimize database (this is not required)." +msgstr "" +"「正常チェック」ボタンを最初にクリックして、データベースが修復を必要とするかどうかを見てください。必要なら、" +"「修復」ボタンをクリックしてください。「いろいろ」ページに移動して、最適化をすることもできます(必須ではありません)。" + +#: database_maintenance.php:409 +msgid "" +"Fix Database procedure will create a database backup file each time the " +"button clicked. The backup file is named with \"maintenance-backup\", so you " +"can remove it if you don't need it. Please go to Miscellaneous page and " +"check if there is one." +msgstr "" +"データベースの修復は、ボタンがクリックされるたびにバックアップファイルを作成します。ファイル名には、" +"\"maintenance-backup\"とついていますので、必要なければ削除してかまいません。" +"「いろいろ」ページに移動して、バックアップファイルが作成されているかどうかご覧ください。" + +#: database_maintenance.php:412 +msgid "" +"If you installed WordPress 3.6 (not upgraded), you don't have to restore the " +"database." +msgstr "" +"アップグレードではなくて、WordPress 3.6 を最初からインストールした場合は、データベースの修復は" +"必要ありません。" + +#: database_maintenance.php:421 +msgid "Sanity Check" +msgstr "正常チェック" + +#: database_maintenance.php:421 +msgid "" +"Are you sure to check the database? This will take some time.\\n\\nClick " +"[Cancel] to stop, [OK] to continue." +msgstr "" +"データベースのチェックをします。この操作は少し時間がかかるかもしれません。\\n\\nやめるなら[キャンセル]を、" +"続けるなら[OK]をクリックしてください。" + +#: database_maintenance.php:422 +msgid "Fix database" +msgstr "修復" + +#: database_maintenance.php:422 +msgid "" +"Are you sure to do fix the database? This will take some time.\\n\\nClick " +"[Cancel] to stop, [OK] to continue." +msgstr "" +"データベースの修復をします。この操作は少し時間がかかるかもしれません。\\n\\nやめるなら[キャンセル]を、" +"続けるなら[OK]をクリックしてください。" + +#: database_maintenance.php:436 database_maintenance.php:446 +msgid "Results" +msgstr "結果" + +#: database_maintenance.php:447 +msgid "Your database restoration is successfully finished!" +msgstr "データベースの修復が成功しました。" + +#: database_maintenance.php:463 database_maintenance.php:474 +msgid "Checked Results" +msgstr "チェックの結果" + +#: database_maintenance.php:468 +msgid " needs restoring." +msgstr "修復が必要です。" + +#: database_maintenance.php:475 +msgid "Your database is OK. You don't have to restore it." +msgstr "あなたのデータベースは正常です。修復の必要はありません。" + +msgid "You can check your database and fix it if needed." +msgstr "データベースのチェックと修復をすることができます。" diff --git a/pdoengine.class.php b/pdoengine.class.php index a794ab0..a7ef23c 100644 --- a/pdoengine.class.php +++ b/pdoengine.class.php @@ -648,7 +648,12 @@ class PDOEngine extends PDO { $engine = $this->prepare_engine($this->query_type); $reason = 0; $message = ''; + $re_query = ''; $rewritten_query = $engine->rewrite_query($query, $this->query_type); + if (is_array($rewritten_query) && array_key_exists('recursion', $rewritten_query)) { + $re_query = $rewritten_query['recursion']; + unset($rewritten_query['recursion']); + } try { $this->beginTransaction(); if (is_array($rewritten_query)) { @@ -674,6 +679,9 @@ class PDOEngine extends PDO { $this->rollBack(); } } + if ($re_query != '') { + $this->query($re_query); + } if ($reason > 0) { $err_message = sprintf(__("Problem in executing alter query. Error was: %s", 'sqlite-integration'), $message); $this->set_error(__LINE__, __FUNCTION__, $err_message); diff --git a/query_alter.class.php b/query_alter.class.php index 1d52765..38d5d44 100644 --- a/query_alter.class.php +++ b/query_alter.class.php @@ -9,49 +9,65 @@ class AlterQuery { public $_query = null; public function rewrite_query($query, $query_type) { - $tokens = array(); if (stripos($query, $query_type) === false) { return false; } $query = str_replace('`', '', $query); if (preg_match('/^\\s*(ALTER\\s*TABLE)\\s*(\\w+)?\\s*/ims', $query, $match)) { + $tmp_query = array(); + $tokens = array(); + $re_command = ''; $command = str_ireplace($match[0], '', $query); $tmp_tokens['query_type'] = trim($match[1]); $tmp_tokens['table_name'] = trim($match[2]); - $command_array = $this->split_multiple($command); - foreach ($command_array as $single_command) { - $command_tokens = $this->command_tokenizer($single_command); - if (!empty($command_tokens)) { - $tokens[] = array_merge($tmp_tokens, $command_tokens); - } else { - $this->_query = 'SELECT 1=1'; - } +// $command_array = $this->split_multiple($command); + $command_array = explode(',', $command); + + $single_command = array_shift($command_array); + if (!empty($command_array)) { + $re_command = 'ALTER TABLE ' . $tmp_tokens['table_name'] . ' '; + $re_command .= implode(',', $command_array); + } + $command_tokens = $this->command_tokenizer($single_command); + if (!empty($command_tokens)) { + $tokens = array_merge($tmp_tokens, $command_tokens); + } else { + $this->_query = 'SELECT 1=1'; + return $this->_query; } - foreach ($tokens as $token) { - $command_name = $token['command']; - switch ($command_name) { - case 'add column': case 'rename to': case 'add index': case 'drop index': - $this->_query = $this->handle_single_command($token); - break; - case 'add primary key': - $this->_query = $this->handle_add_primary_key($token); - break; - case 'drop primary key': - $this->_query = $this->handle_drop_primary_key($token); - break; - case 'modify column': - $this->_query = $this->handle_modify_command($token); - break; - case 'change column': - $this->_query = $this->handle_change_command($token); - break; - case 'alter column': - $this->_query = $this->handle_alter_command($token); - break; - default: - break; +// foreach ($tokens as $token) { + $command_name = strtolower($tokens['command']); + switch ($command_name) { + case 'add column': case 'rename to': case 'add index': case 'drop index': + $tmp_query = $this->handle_single_command($tokens); + break; + case 'add primary key': + $tmp_query = $this->handle_add_primary_key($tokens); + break; + case 'drop primary key': + $tmp_query = $this->handle_drop_primary_key($tokens); + break; + case 'modify column': + $tmp_query = $this->handle_modify_command($tokens); + break; + case 'change column': + $tmp_query = $this->handle_change_command($tokens); + break; + case 'alter column': + $tmp_query = $this->handle_alter_command($tokens); + break; + default: + break; } - } +// } + if (!is_array($tmp_query)) { + $this->_query[] = $tmp_query; + } else { + $this->_query = $tmp_query; + } + if ($re_command != '') { + $this->_query = array_merge($this->_query, array('recursion' => $re_command)); + } } else { $this->_query = 'SELECT 1=1'; } @@ -60,29 +76,29 @@ class AlterQuery { private function command_tokenizer($command) { $tokens = array(); - if (preg_match('/^(ADD|DROP|RENAME|MODIFY|CHANGE|ALTER)\\s*(\\w+)?\\s*(\\w+)?\\s*/ims', $command, $match)) { + if (preg_match('/^(ADD|DROP|RENAME|MODIFY|CHANGE|ALTER)\\s*(\\w+)?\\s*(\\w+(\(.+\)|))?\\s*/ims', $command, $match)) { $the_rest = str_ireplace($match[0], '', $command); - $match_1 = strtolower(trim($match[1])); - $match_2 = strtolower(trim($match[2])); - $match_3 = isset($match[3]) ? strtolower(trim($match[3])) : ''; - switch ($match_1) { + $match_1 = trim($match[1]); + $match_2 = trim($match[2]); + $match_3 = isset($match[3]) ? trim($match[3]) : ''; + switch (strtolower($match_1)) { case 'add': - if (in_array($match_2, array('fulltext', 'constraint', 'foreign'))) { + if (in_array(strtolower($match_2), array('fulltext', 'constraint', 'foreign'))) { break; - } elseif ($match_2 == 'column') { + } elseif (stripos('column', $match_2) !== false) { $tokens['command'] = $match_1.' '.$match_2; $tokens['column_name'] = $match_3; $tokens['column_def'] = trim($the_rest); - } elseif ($match_2 == 'primary') { + } elseif (stripos('primary', $match_2) !== false) { $tokens['command'] = $match_1.' '.$match_2.' '.$match_3; $tokens['column_name'] = $the_rest; - } elseif ($match_2 == 'unique') { + } elseif (stripos('unique', $match_2) !== false) { list($index_name, $col_name) = preg_split('/[\(\)]/s', trim($the_rest), -1, PREG_SPLIT_DELIM_CAPTURE); $tokens['unique'] = true; $tokens['command'] = $match_1.' '.$match_3; $tokens['index_name'] = trim($index_name); $tokens['column_name'] = '('.trim($col_name).')'; - } elseif (in_array($match_2, array('index', 'key'))) { + } elseif (in_array(strtolower($match_2), array('index', 'key'))) { $tokens['command'] = $match_1.' '.$match_2; if ($match_3 == '') { $tokens['index_name'] = str_replace(array('(', ')'), '', $the_rest); @@ -91,82 +107,94 @@ class AlterQuery { } $tokens['column_name'] = trim($the_rest); } else { - $tokens['command'] = $match_1.' column'; + $tokens['command'] = $match_1.' COLUMN'; $tokens['column_name'] = $match_2; $tokens['column_def'] = $match_3.' '.$the_rest; } break; case 'drop': - if ($match_2 == 'column') { + if (stripos('column', $match_2) !== false) { $tokens['command'] = $match_1.' '.$match_2; $tokens['column_name'] = trim($match_3); - } elseif ($match_2 == 'primary') { + } elseif (stripos('primary', $match_2) !== false) { $tokens['command'] = $match_1.' '.$match_2.' '.$match_3; - } elseif (in_array($match_2, array('index', 'key'))) { + } elseif (in_array(strtolower($match_2), array('index', 'key'))) { $tokens['command'] = $match_1.' '.$match_2; $tokens['index_name'] = $match_3; - } elseif ($match_2 == 'primary') { + } elseif (stripos('primary', $match_2) !== false) { $tokens['command'] = $match_1.' '.$match_2.' '.$match_3; } else { - $tokens['command'] = $match_1.' column'; + $tokens['command'] = $match_1.' COLUMN'; $tokens['column_name'] = $match_2; } break; case 'rename': - if ($match_2 == 'to') { + if (stripos('to', $match_2) !== false) { $tokens['command'] = $match_1.' '.$match_2; $tokens['column_name'] = $match_3; } else { - $tokens['command'] = $match_1.' to'; + $tokens['command'] = $match_1.' TO'; $tokens['column_name'] = $match_2; } break; case 'modify': - if ($match_2 == 'column') { + if (stripos('column', $match_2) !== false) { $tokens['command'] = $match_1.' '.$match_2; $tokens['column_name'] = $match_3; $tokens['column_def'] = trim($the_rest); } else { - $tokens['command'] = $match_1.' column'; + $tokens['command'] = $match_1.' COLUMN'; $tokens['column_name'] = $match_2; $tokens['column_def'] = $match_3.' '.trim($the_rest); } break; case 'change': - if ($match_2 == 'column') { + $the_rest = trim($the_rest); + if (stripos('column', $match_2) !== false) { $tokens['command'] = $match_1.' '.$match_2; $tokens['old_column'] = $match_3; - list($new_col, $col_def) = explode(' ', trim($the_rest), 2); - $tokens['new_column'] = $new_col; - $tokens['column_def'] = trim($col_def); - } else { - $tokens['command'] = $match_1.' column'; - $tokens['old_column'] = $match_2; - $tokens['new_column'] = $match_3; - $tokens['column_def'] = trim($the_rest); - } - break; - case 'alter': - if ($match_2 == 'column') { - $tokens['command'] = $match_1.' '.$match_2; - $tokens['column_name'] = $match_3; - list($set_or_drop) = explode(' ', $the_rest); - if (strtolower($set_or_drop) == 'set') { - $tokens['default_command'] = 'set default'; - $default_value = str_ireplace('set default', '', $the_rest); - $tokens['default_value'] = trim($default_value); + list($new_col) = explode(' ', $the_rest); + $tmp_col = preg_replace('/\(.+?\)/im', '', $new_col); + if (array_key_exists(strtolower($tmp_col), $this->array_types)) { + $tokens['column_def'] = $the_rest; } else { - $tokens['default_command'] = 'drop default'; + $tokens['new_column'] = $new_col; + $col_def = str_replace($new_col, '', $the_rest); + $tokens['column_def'] = trim($col_def); } } else { $tokens['command'] = $match_1.' column'; + $tokens['old_column'] = $match_2; + $tmp_col = preg_replace('/\(.+?\)/im', '', $match_3); + if (array_key_exists(strtolower($tmp_col), $this->array_types)) { + $tokens['column_def'] = $match_3 . ' ' . $the_rest; + } else { + $tokens['new_column'] = $match_3; + $tokens['column_def'] = $the_rest; + } + } + break; + case 'alter': + if (stripos('column', $match_2) !== false) { + $tokens['command'] = $match_1.' '.$match_2; + $tokens['column_name'] = $match_3; + list($set_or_drop) = explode(' ', $the_rest); + if (stripos('set', $set_or_drop) !== false) { + $tokens['default_command'] = 'SET DEFAULT'; + $default_value = str_ireplace('set default', '', $the_rest); + $tokens['default_value'] = trim($default_value); + } else { + $tokens['default_command'] = 'DROP DEFAULT'; + } + } else { + $tokens['command'] = $match_1.' COLUMN'; $tokens['column_name'] = $match_2; - if (strtolower($match_3) == 'set') { - $tokens['default_command'] = 'set default'; + if (stripos('set', $match_3) !== false) { + $tokens['default_command'] = 'SET DEFAULT'; $default_value = str_ireplace('default', '', $the_rest); $tokens['default_value'] = trim($default_value); } else { - $tokens['default_command'] = 'drop default'; + $tokens['default_command'] = 'DROP DEFAULT'; } } break; @@ -301,14 +329,14 @@ class AlterQuery { $create_query = array_shift($index_queries); if (stripos($create_query, $tokenized_query['column_name']) === false) { return 'SELECT 1=1'; - } elseif (preg_match("/{$tokenized_query['column_name']}\\s*{$tokenized_query['column_def']}\\s*[,)]/i", $create_query)) { + } elseif (preg_match("/{$tokenized_query['column_name']}\\s*{$column_def}\\s*[,)]/i", $create_query)) { return 'SELECT 1=1'; } $create_query = preg_replace("/{$tokenized_query['table_name']}/i", $temp_table, $create_query); if (preg_match("/\\b{$tokenized_query['column_name']}\\s*.*(?=,)/ims", $create_query)) { - $create_query = preg_replace("/\\b{$tokenized_query['column_name']}\\s*.*(?=,)/ims", "{$tokenized_query['column_name']} {$tokenized_query['column_def']}", $create_query); + $create_query = preg_replace("/\\b{$tokenized_query['column_name']}\\s*.*(?=,)/ims", "{$tokenized_query['column_name']} {$column_def}", $create_query); } elseif (preg_match("/\\b{$tokenized_query['column_name']}\\s*.*(?=\))/ims", $create_query)) { - $create_query = preg_replace("/\\b{$tokenized_query['column_name']}\\s*.*(?=\))/ims", "{$tokenized_query['column_name']} {$tokenized_query['column_def']}", $create_query); + $create_query = preg_replace("/\\b{$tokenized_query['column_name']}\\s*.*(?=\))/ims", "{$tokenized_query['column_name']} {$column_def}", $create_query); } $query[] = $create_query; $query[] = "INSERT INTO $temp_table SELECT * FROM {$tokenized_query['table_name']}"; @@ -326,11 +354,16 @@ class AlterQuery { $new_fields = ''; $tokenized_query = $queries; $temp_table = 'temp_'.$tokenized_query['table_name']; - $column_def = $this->convert_field_types($tokenized_query['new_column'], $tokenized_query['column_def']); + if (isset($tokenized_query['new_column'])) { + $column_name = $tokenized_query['new_column']; + } else { + $column_name = $tokenized_query['old_column']; + } + $column_def = $this->convert_field_types($column_name, $tokenized_query['column_def']); $_wpdb = new PDODB(); $col_obj = $_wpdb->get_results("SHOW COLUMNS FROM {$tokenized_query['table_name']}"); foreach ($col_obj as $col) { - if ($col->Field == $tokenized_query['old_column']) $col_check = true; + if (stripos($col->Field, $tokenized_query['old_column']) !== false) $col_check = true; $old_fields .= $col->Field . ','; } if ($col_check == false) { @@ -338,7 +371,7 @@ class AlterQuery { return 'SELECT 1=1'; } $old_fields = rtrim($old_fields, ','); - $new_fields = str_replace($tokenized_query['old_column'], $tokenized_query['new_column'], $old_fields); + $new_fields = str_ireplace($tokenized_query['old_column'], $column_name, $old_fields); $query_obj = $_wpdb->get_results("SELECT sql FROM sqlite_master WHERE tbl_name='{$tokenized_query['table_name']}'"); $_wpdb = null; for ($i = 0; $i < count($query_obj); $i++) { @@ -347,16 +380,16 @@ class AlterQuery { $create_query = array_shift($index_queries); $create_query = preg_replace("/{$tokenized_query['table_name']}/i", $temp_table, $create_query); if (preg_match("/\\b{$tokenized_query['old_column']}\\s*(.+?)(?=,)/ims", $create_query, $match)) { - if ($column_def == trim($match[1])) { + if (stripos(trim($match[1]), $column_def) !== false) { return 'SELECT 1=1'; } else { - $create_query = preg_replace("/\\b{$tokenized_query['old_column']}\\s*.*?(?=,)/ims", "{$tokenized_query['new_column']} {$column_def}", $create_query); + $create_query = preg_replace("/\\b{$tokenized_query['old_column']}\\s*.+?(?=,)/ims", "{$column_name} {$column_def}", $create_query, 1); } } elseif (preg_match("/\\b{$tokenized_query['old_column']}\\s*(.+?)(?=\))/ims", $create_query, $match)) { - if ($column_def == trim($match[1])) { + if (stripos(trim($match[1]), $column_def) !== false) { return 'SELECT 1=1'; } else { - $create_query = preg_replace("/\\b{$tokenized_query['old_column']}\\s*.*?(?=\))/ims", "{$tokenized_query['new_column']} {$column_def}", $create_query); + $create_query = preg_replace("/\\b{$tokenized_query['old_column']}\\s*.*(?=\))/ims", "{$column_name} {$column_def}", $create_query, 1); } } $query[] = $create_query; @@ -372,11 +405,11 @@ class AlterQuery { private function handle_alter_command($queries) { $tokenized_query = $queries; $temp_table = 'temp_'.$tokenized_query['table_name']; - if (stripos($tokenized_query['default_command'], 'set') !== false) { + if (isset($tokenized_query['default_value'])) { $def_value = $this->convert_field_types($tokenized_query['column_name'], $tokenized_query['default_value']); $def_value = 'DEFAULT '.$def_value; } else { - $def_value = ''; + $def_value = null; } $_wpdb = new PDODB(); $query_obj = $_wpdb->get_results("SELECT sql FROM sqlite_master WHERE tbl_name='{$tokenized_query['table_name']}'"); @@ -388,12 +421,34 @@ class AlterQuery { if (stripos($create_query, $tokenized_query['column_name']) === false) { return 'SELECT 1=1'; } - if (preg_match("/\\s*({$tokenized_query['column_name']}\\s*.*?)\\s*(DEFAULT\\s*.*|)[,)]/im", $create_query, $match)) { - $col_def = trim($match[1]); - $old_default = trim($match[2]); - $create_query = preg_replace("/($col_def)\\s*$old_default/im", "\\1 $def_value", $create_query); + if (preg_match("/\\s*({$tokenized_query['column_name']})\\s*(.*)?(DEFAULT\\s*.*)[,)]/im", $create_query, $match)) { + $col_name = trim($match[1]); + $col_def = trim($match[2]); + $col_def_esc = str_replace(array('(', ')'), array('\(', '\)'), $col_def); + $checked_col_def = $this->convert_field_types($col_name, $col_def); + $old_default = trim($match[3]); + $pattern = "/$col_name\\s*$col_def_esc\\s*$old_default/im"; + if (is_null($def_value)) { + $replacement = $col_name . ' ' . $checked_col_def; + } else { + $replacement = $col_name . ' ' . $checked_col_def . ' ' . $def_value; + } + $create_query = preg_replace($pattern, $replacement, $create_query); $create_query = str_ireplace($tokenized_query['table_name'], $temp_table, $create_query); - } else { + } elseif (preg_match("/\\s*({$tokenized_query['column_name']})\\s*(.*)?[,)]/im", $create_query, $match)) { + $col_name = trim($match[1]); + $col_def = trim($match[2]); + $col_def_esc = str_replace(array('(', ')'), array('\(', '\)'), $col_def); + $checked_col_def = $this->convert_field_types($col_name, $col_def); + $pattern = "/$col_name\\s*$col_def_esc/im"; + if (is_null($def_value)) { + $replacement = $col_name . ' ' . $checked_col_def; + } else { + $replacement = $col_name . ' ' . $checked_col_def . ' ' . $def_value; + } + $create_query = preg_replace($pattern, $replacement, $create_query); + $create_query = str_ireplace($tokenized_query['table_name'], $temp_table, $create_query); + } else { return 'SELECT 1=1'; } $query[] = $create_query; @@ -412,28 +467,10 @@ class AlterQuery { * @return string */ private function convert_field_types($col_name, $col_def){ - $array_types = array( - 'bit' => 'INTEGER', 'bool' => 'INTEGER', - 'boolean' => 'INTEGER', 'tinyint' => 'INTEGER', - 'smallint' => 'INTEGER', 'mediumint' => 'INTEGER', - 'bigint' => 'INTEGER', 'integer' => 'INTEGER', - 'int' => 'INTEGER', 'float' => 'REAL', - 'double' => 'REAL', 'decimal' => 'REAL', - 'dec' => 'REAL', 'numeric' => 'REAL', - 'fixed' => 'REAL', 'datetime' => 'TEXT', - 'date' => 'TEXT', 'timestamp' => 'TEXT', - 'time' => 'TEXT', 'year' => 'TEXT', - 'varchar' => 'TEXT', 'char' => 'TEXT', - 'varbinary' => 'BLOB', 'binary' => 'BLOB', - 'tinyblob' => 'BLOB', 'mediumblob' => 'BLOB', - 'longblob' => 'BLOB', 'blob' => 'BLOB', - 'tinytext' => 'TEXT', 'mediumtext' => 'TEXT', - 'longtext' => 'TEXT', 'text' => 'TEXT' - ); $array_curtime = array('current_timestamp', 'current_time', 'current_date'); $array_reptime = array("'0000-00-00 00:00:00'", "'0000-00-00 00:00:00'", "'0000-00-00'"); $def_string = str_replace('`', '', $col_def); - foreach ($array_types as $o=>$r){ + foreach ($this->array_types as $o=>$r){ $pattern = "/\\b$o\\s*(\([^\)]*\)*)?\\s*/ims"; if (preg_match($pattern, $def_string)) { $def_string = preg_replace($pattern, "$r ", $def_string); @@ -451,5 +488,24 @@ class AlterQuery { } return $def_string; } + + private $array_types = array( + 'bit' => 'INTEGER', 'bool' => 'INTEGER', + 'boolean' => 'INTEGER', 'tinyint' => 'INTEGER', + 'smallint' => 'INTEGER', 'mediumint' => 'INTEGER', + 'bigint' => 'INTEGER', 'integer' => 'INTEGER', + 'int' => 'INTEGER', 'float' => 'REAL', + 'double' => 'REAL', 'decimal' => 'REAL', + 'dec' => 'REAL', 'numeric' => 'REAL', + 'fixed' => 'REAL', 'datetime' => 'TEXT', + 'date' => 'TEXT', 'timestamp' => 'TEXT', + 'time' => 'TEXT', 'year' => 'TEXT', + 'varchar' => 'TEXT', 'char' => 'TEXT', + 'varbinary' => 'BLOB', 'binary' => 'BLOB', + 'tinyblob' => 'BLOB', 'mediumblob' => 'BLOB', + 'longblob' => 'BLOB', 'blob' => 'BLOB', + 'tinytext' => 'TEXT', 'mediumtext' => 'TEXT', + 'longtext' => 'TEXT', 'text' => 'TEXT' + ); } ?> \ No newline at end of file diff --git a/readme-ja.txt b/readme-ja.txt index 51bc8ba..ba2bdf4 100644 --- a/readme-ja.txt +++ b/readme-ja.txt @@ -7,7 +7,7 @@ Author: Kojima Toshiyasu Author URI: http://dogwood.skr.jp/ Requires at least: 3.3 Tested up to: 3.6 -Stable tag: 1.3 +Stable tag: 1.4 License: GPLv2 License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -30,6 +30,12 @@ WordPressはMySQLと話していると思っていて、背後で何が起こっ SQLite Integrationは[PDO for WordPress](http://wordpress.org/extend/plugins/pdo-for-wordpress)プラグインの後継です。後者は残念なことに、もうメンテナンスされていないようです。SQLite IntegrationはPDO for WordPressの基本的なアイディアと構造を借りて、より多くの機能とユーティリティを追加しました。 += 重要なお知らせ = + +このプラグインを使ってWordPress 3.5.x をインストールし、3.6にアップグレードした場合、データベースが意図したとおりに動作していないかもしれません。これに該当する場合は、プラグインを最新版にアップグレードして、管理画面のメンテナンスページに移動し、修復が必要かどうかをチェックしてください。必要なら、そのページで提供しているユーティリティを使って修復をすることができます。 + +WordPress 3.6 をインストールした場合や、3.5.x を使っている場合は、これに該当しませんので、修復は必要ありません。 + = Features = SQLite Integrationは普通の「プラグイン」ではありません。WordPressをインストールするときに使います。そのため、少し準備が必要です。インストールのセクションを参照してください。[SQLite Integration Page](http://dogwood.skr.jp/wordpress/sqlite-integration/)をご覧になると、もっと詳しい説明を読むことができます。 @@ -167,9 +173,10 @@ SQLite Integrationのアップグレードに失敗するようなら、FTPを == Changelog == -= 1.3.1 () = += 1.4 (2013-09-12) = +* アップグレードしたWordPressで期待通り動作しないのを修正するために、データベース管理ユーティリティを追加しました。 * SHOW INDEXクエリにWHERE句がある場合の処理を変更しました。 -* ALTER TABLE ALTER COLUMNクエリのバグを修正しました。 +* ALTER TABLEクエリのバグを修正しました。 = 1.3 (2013-09-04) = * データベースファイルのスナップショットをzipアーカイブとしてバックアップするユーティリティを追加しました。 diff --git a/readme.txt b/readme.txt index da1bb2b..3ec24c9 100644 --- a/readme.txt +++ b/readme.txt @@ -7,7 +7,7 @@ Author: Kojima Toshiyasu Author URI: http://dogwood.skr.jp/ Requires at least: 3.3 Tested up to: 3.6 -Stable tag: 1.3 +Stable tag: 1.4 License: GPLv2 License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -30,6 +30,12 @@ WordPress thinks she talks with MySQL and doesn't know what has happened in the SQLite Integration is a successor to [PDO for WordPress](http://wordpress.org/extend/plugins/pdo-for-wordpress) plugin, which unfortunately enough, doesn't seem to be maintained any more. SQLite Integration uses the basic idea and structures of that plugin and adds some more features or some utilities. += Important Notice = + +When your installed WordPress 3.5.x with this plugin and upgraded to 3.6, your database might not work as expected. If this is your case, please upgrade this plugin to the newest version (1.4) and visit the maintenance page in the admin dashboard, where you can check if you need to fix your database, and you can do fixing job with the utility. + +When you installed WordPress 3.6 with this plugin or your WordPress is 3.5.x, you don't have to fix your database. + = Features = SQLite Integration is not an ordinary 'plugin'. It is used to install WordPress itself. You need to do some preparations. Please read the install section. And see more detailed instruction in the [SQLite Integration Page](http://dogwood.skr.jp/wordpress/sqlite-integration). @@ -158,9 +164,10 @@ When auto upgrading of SQLite Integration fails, please try manual upgrade via F == Changelog == -= 1.3.1 () = += 1.4 (2013-09-12) = +* Added the database maintenance utility for fixing the database mulfunction of the upgraded WordPress installation. * Changed the manipulation of SHOW INDEX query with WHERE clause. -* Fixed the bug of the manipulation of ALTER TABLE ALTER COLUMN query. +* Fixed the bug of the manipulation of ALTER TABLE query. = 1.3 (2013-09-04) = * Added the backup utility that creates the zipped archive of the current snapshot of the database file. diff --git a/sqlite-integration.php b/sqlite-integration.php index 80d0914..050f881 100644 --- a/sqlite-integration.php +++ b/sqlite-integration.php @@ -4,7 +4,7 @@ Plugin Name: SQLite Integration Plugin URI: http://dogwood.skr.jp/wordpress/sqlite-integration/ Description: SQLite Integration is the plugin that enables WordPress to use SQLite. If you don't have MySQL and want to build a WordPress website, it's for you. Author: Kojima Toshiyasu -Version: 1.3 +Version: 1.4 Author URI: http://dogwood.skr.jp Text Domain: sqlite-integration Domain Path: /languages @@ -59,6 +59,10 @@ if (!class_exists('PatchUtils')) { require_once SQLiteFilePath . '/utilities/patch.php'; $patch_utils = new PatchUtils(); } +if (!class_exists('DatabaseMaintenance')) { + require_once SQLiteFilePath . '/utilities/database_maintenance.php'; + $maintenance = new DatabaseMaintenance(); +} /** * This class is for WordPress Administration Panel @@ -153,13 +157,14 @@ class SQLiteIntegration { * users. */ function add_pages() { - global $utils, $doc, $patch_utils; + global $utils, $doc, $patch_utils, $maintenance; if (function_exists('add_options_page')) { $welcome_page = add_options_page(__('SQLite Integration'), __('SQLite Integration'), 'manage_options', 'sqlite-integration', array($utils, 'welcome')); $util_page = add_submenu_page(null, 'System Info', 'System Info', 'manage_options', 'sys-info', array($utils, 'show_utils')); $edit_db = add_submenu_page(null, 'Setting File', 'Setting File', 'manage_options', 'setting-file', array($utils, 'edit_db_file')); $doc_page = add_submenu_page(null, 'Documentation', 'Documentation', 'manage_options', 'doc', array($doc, 'show_doc')); $patch_page = add_submenu_page(null, 'Patch Utility', 'Patch Utility', 'manage_options', 'patch', array($patch_utils, 'show_patch_page')); + $maintenance_page = add_submenu_page(null, 'DB Maintenance', 'DB Maintenance', 'manage_options', 'maintenance', array($maintenance, 'show_maintenance_page')); add_action('admin_print_styles-'.$welcome_page, array($this, 'add_style_sheet')); add_action('admin_print_styles-'.$util_page, array($this, 'add_style_sheet')); add_action('admin_print_styles-'.$edit_db, array($this, 'add_style_sheet')); @@ -169,6 +174,7 @@ class SQLiteIntegration { add_action('admin_print_scripts-'.$doc_page, array($this, 'add_sqlite_script')); add_action('admin_print_scripts-'.$patch_page, array($this, 'add_sqlite_script')); add_action('admin_print_scripts-'.$edit_db, array($this, 'add_sqlite_script')); + add_action('admin_print_styles-'.$maintenance_page, array($this, 'add_style_sheet')); } } @@ -177,13 +183,14 @@ class SQLiteIntegration { * So, capability is set to manage_network_options. */ function add_network_pages() { - global $utils, $doc, $patch_utils; + global $utils, $doc, $patch_utils, $maintenance; if (function_exists('add_options_page')) { $welcome_page = add_submenu_page('settings.php', __('SQLite Integration'), __('SQLite Integration'), 'manage_network_options', 'sqlite-integration', array($utils, 'welcome')); $util_page = add_submenu_page(null, 'System Info', 'System Info', 'manage_network_options', 'sys-info', array($utils, 'show_utils')); $edit_db = add_submenu_page(null, 'Setting File', 'Setting File', 'manage_network_options', 'setting-file', array($utils, 'edit_db_file')); $doc_page = add_submenu_page(null, 'Documentation', 'Documentation', 'manage_network_options', 'doc', array($doc, 'show_doc')); $patch_page = add_submenu_page(null, 'Patch Utility', 'Patch Utility', 'manage_network_options', 'patch', array($patch_utils, 'show_patch_page')); + $maintenance_page = add_submenu_page(null, 'DB Maintenance', 'DB Maintenance', 'manage_network_options', 'maintenance', array($maintenance, 'show_maintenance_page')); add_action('admin_print_styles-'.$welcome_page, array($this, 'add_style_sheet')); add_action('admin_print_styles-'.$util_page, array($this, 'add_style_sheet')); add_action('admin_print_styles-'.$edit_db, array($this, 'add_style_sheet')); @@ -193,6 +200,7 @@ class SQLiteIntegration { add_action('admin_print_scripts-'.$doc_page, array($this, 'add_sqlite_script')); add_action('admin_print_scripts-'.$patch_page, array($this, 'add_sqlite_script')); add_action('admin_print_scripts-'.$edit_db, array($this, 'add_sqlite_script')); + add_action('admin_print_styles-'.$maintenance_page, array($this, 'add_style_sheet')); } } diff --git a/utilities/database_maintenance.php b/utilities/database_maintenance.php new file mode 100644 index 0000000..e86c34c --- /dev/null +++ b/utilities/database_maintenance.php @@ -0,0 +1,484 @@ +prefix.'commentmeta' => array( + 'comment_id' => '\'0\'', + 'meta_key' => 'NULL' + ), + $wpdb->prefix.'comments' => array( + 'comment_post_ID' => '\'0\'', + 'comment_author_email' => '\'\'', + 'comment_author_url' => '\'\'', + 'comment_author_IP' => '\'\'', + 'comment_date' => '\'0000-00-00 00:00:00\'', + 'comment_date_gmt' => '\'0000-00-00 00:00:00\'', + 'comment_karma' => '\'0\'', + 'comment_approved' => '\'1\'', + 'comment_agent' => '\'\'', + 'comment_type' => '\'\'', + 'comment_type' => '\'\'', + 'comment_parent' => '\'0\'', + 'user_id' => '\'0\'' + ), + $wpdb->prefix.'links' => array( + 'link_url' => '\'\'', + 'link_name' => '\'\'', + 'link_image' => '\'\'', + 'link_target' => '\'\'', + 'link_description' => '\'\'', + 'link_visible' => '\'Y\'', + 'link_owner' => '\'1\'', + 'link_rating' => '\'0\'', + 'link_updated' => '\'0000-00-00 00:00:00\'', + 'link_rel' => '\'\'', + 'link_rss' => '\'\'' + ), + $wpdb->prefix.'options' => array( + 'option_name' => '\'\'', + 'autoload' => '\'yes\'' + ), + $wpdb->prefix.'postmeta' => array( + 'post_id' => '\'0\'', + 'meta_key' => 'NULL' + ), + $wpdb->prefix.'posts' => array( + 'post_author' => '\'0\'', + 'post_date' => '\'0000-00-00 00:00:00\'', + 'post_date_gmt' => '\'0000-00-00 00:00:00\'', + 'post_status' => '\'publish\'', + 'comment_status' => '\'open\'', + 'ping_status' => '\'open\'', + 'post_password' => '\'\'', + 'post_name' => '\'\'', + 'post_modified' => '\'0000-00-00 00:00:00\'', + 'post_modified_gmt' => '\'0000-00-00 00:00:00\'', + 'post_parent' => '\'0\'', + 'guid' => '\'\'', + 'menu_order' => '\'0\'', + 'post_type' => '\'post\'', + 'post_mime_type' => '\'\'', + 'comment_count' => '\'0\'' + ), + $wpdb->prefix.'term_relationships' => array( + 'term_order' => '0' + ), + $wpdb->prefix.'term_taxonomy' => array( + 'taxonomy' => '\'\'', + 'parent' => '0', + 'count' => '0' + ), + $wpdb->prefix.'terms' => array( + 'name' => '\'\'', + 'slug' => '\'\'', + 'term_group' => '0' + ), + $wpdb->prefix.'users' => array( + 'user_login' => '\'\'', + 'user_pass' => '\'\'', + 'user_nicename' => '\'\'', + 'user_email' => '\'\'', + 'user_url' => '\'\'', + 'user_registered' => '\'0000-00-00 00:00:00\'', + 'user_activation_key' => '\'\'', + 'user_status' => '\'0\'', + 'display_name' => '\'\'', + // for network install + 'user_login' => '\'\'', + 'spam' => '\'0\'', + 'deleted' => '\'0\'' + ), + $wpdb->prefix.'usermeta' => array( + 'user_id' => '\'0\'', + 'meta_key' => 'NULL', + ), + // for network install + $wpdb->prefix.'blog_versions' => array( + 'blog_id' => '\'0\'', + 'db_version' => '\'\'', + 'last_updated' => '\'0000-00-00 00:00:00\'' + ), + $wpdb->prefix.'blogs' => array( + 'site_id' => '\'0\'', + 'domain' => '\'\'', + 'path' => '\'\'', + 'registered' => '\'0000-00-00 00:00:00\'', + 'last_updated' => '\'0000-00-00 00:00:00\'', + 'public' => '\'1\'', + 'mature' => '\'0\'', + 'spam' => '\'0\'', + 'deleted' => '\'0\'', + 'lang_id' => '\'0\'' + ), + $wpdb->prefix.'registration_log' => array( + 'email' => '\'\'', + 'IP' => '\'\'', + 'blog_id' => '\'0\'', + 'date_registered' => '\'0000-00-00 00:00:00\'' + ), + $wpdb->prefix.'signups' => array( + 'domain' => '\'\'', + 'path' => '\'\'', + 'user_login' => '\'\'', + 'user_email' => '\'\'', + 'registered' => '\'0000-00-00 00:00:00\'', + 'activated' => '\'0000-00-00 00:00:00\'', + 'active' => '\'0\'', + 'activation_key' => '\'\'', + ), + $wpdb->prefix.'site' => array( + 'domain' => '\'\'', + 'path' => '\'\'' + ), + $wpdb->prefix.'sitemeta' => array( + 'site_id' => '\'0\'', + 'meta_key' => 'NULL', + ) + ); + $tables = $wpdb->tables('all'); + foreach ($tables as $table) { + $col_infos = $wpdb->get_results("SHOW COLUMNS FROM $table"); + foreach ($col_infos as $col) { + if (array_key_exists($col->Field, $columns_to_check[$table]) && $col->Default != $columns_to_check[$table][$col->Field]) { + $results_table[$table] = 'damaged'; + break; + } + } + } + if (empty($results_table)) { + return true; + } else { + return $results_table; + } + } + private function do_fix_database() { + global $wpdb, $wp_version, $utils; + $global_schema_to_change = array( + $wpdb->prefix.'commentmeta' => array( + "comment_id bigint(20) unsigned NOT NULL default '0'", + "meta_key varchar(255) default NULL", + "meta_value longtext" + ), + $wpdb->prefix.'comments' => array( + "comment_post_ID bigint(20) unsigned NOT NULL default '0'", + "comment_author_email varchar(100) NOT NULL default ''", + "comment_author_url varchar(200) NOT NULL default ''", + "comment_author_IP varchar(100) NOT NULL default ''", + "comment_date datetime NOT NULL default '0000-00-00 00:00:00'", + "comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00'", + "comment_karma int(11) NOT NULL default '0'", + "comment_approved varchar(20) NOT NULL default '1'", + "comment_agent varchar(255) NOT NULL default ''", + "comment_type varchar(20) NOT NULL default ''", + "comment_parent bigint(20) unsigned NOT NULL default '0'", + "user_id bigint(20) unsigned NOT NULL default '0'" + ), + $wpdb->prefix.'links' => array( + "link_url varchar(255) NOT NULL default ''", + "link_name varchar(255) NOT NULL default ''", + "link_image varchar(255) NOT NULL default ''", + "link_target varchar(25) NOT NULL default ''", + "link_description varchar(255) NOT NULL default ''", + "link_visible varchar(20) NOT NULL default 'Y'", + "link_owner bigint(20) unsigned NOT NULL default '1'", + "link_rating int(11) NOT NULL default '0'", + "link_updated datetime NOT NULL default '0000-00-00 00:00:00'", + "link_rel varchar(255) NOT NULL default ''", + "link_notes mediumtext NOT NULL", + "link_rss varchar(255) NOT NULL default ''" + ), + $wpdb->prefix.'options' => array( + "option_name varchar(64) NOT NULL default ''", + "option_value longtext NOT NULL", + "autoload varchar(20) NOT NULL default 'yes'" + ), + $wpdb->prefix.'postmeta' => array( + "post_id bigint(20) unsigned NOT NULL default '0'", + "meta_key varchar(255) default NULL", + "meta_value longtext" + ), + $wpdb->prefix.'posts' => array( + "post_author bigint(20) unsigned NOT NULL default '0'", + "post_date datetime NOT NULL default '0000-00-00 00:00:00'", + "post_date_gmt datetime NOT NULL default '0000-00-00 00:00:00'", + "post_status varchar(20) NOT NULL default 'publish'", + "comment_status varchar(20) NOT NULL default 'open'", + "ping_status varchar(20) NOT NULL default 'open'", + "post_password varchar(20) NOT NULL default ''", + "post_name varchar(200) NOT NULL default ''", + "post_modified datetime NOT NULL default '0000-00-00 00:00:00'", + "post_modified_gmt datetime NOT NULL default '0000-00-00 00:00:00'", + "post_content_filtered longtext NOT NULL", + "post_parent bigint(20) unsigned NOT NULL default '0'", + "guid varchar(255) NOT NULL default ''", + "menu_order int(11) NOT NULL default '0'", + "post_type varchar(20) NOT NULL default 'post'", + "post_mime_type varchar(100) NOT NULL default ''", + "comment_count bigint(20) NOT NULL default '0'" + ), + $wpdb->prefix.'term_relationships' => array( + "term_order int(11) NOT NULL default 0" + ), + $wpdb->prefix.'term_taxonomy' => array( + "taxonomy varchar(32) NOT NULL default ''", + "description longtext NOT NULL", + "parent bigint(20) unsigned NOT NULL default 0", + "count bigint(20) NOT NULL default 0" + ), + $wpdb->prefix.'terms' => array( + "name varchar(200) NOT NULL default ''", + "slug varchar(200) NOT NULL default ''", + "term_group bigint(10) NOT NULL default 0" + ), + $wpdb->prefix.'users' => array( + "user_login varchar(60) NOT NULL default ''", + "user_pass varchar(64) NOT NULL default ''", + "user_nicename varchar(50) NOT NULL default ''", + "user_email varchar(100) NOT NULL default ''", + "user_url varchar(100) NOT NULL default ''", + "user_registered datetime NOT NULL default '0000-00-00 00:00:00'", + "user_activation_key varchar(60) NOT NULL default ''", + "user_status int(11) NOT NULL default '0'", + "display_name varchar(250) NOT NULL default ''" + ), + $wpdb->prefix.'usermeta' => array( + "user_id bigint(20) unsigned NOT NULL default '0'", + "meta_key varchar(255) default NULL", + "meta_value longtext" + ) + ); + + $network_schema_to_change = array( + $wpdb->prefix.'blog_versions' => array( + "blog_id bigint(20) NOT NULL default '0'", + "db_version varchar(20) NOT NULL default ''", + "last_updated datetime NOT NULL default '0000-00-00 00:00:00'" + ), + $wpdb->prefix.'blogs' => array( + "site_id bigint(20) NOT NULL default '0'", + "domain varchar(200) NOT NULL default ''", + "path varchar(100) NOT NULL default ''", + "registered datetime NOT NULL default '0000-00-00 00:00:00'", + "last_updated datetime NOT NULL default '0000-00-00 00:00:00'", + "public tinyint(2) NOT NULL default '1'", + "mature tinyint(2) NOT NULL default '0'", + "spam tinyint(2) NOT NULL default '0'", + "deleted tinyint(2) NOT NULL default '0'", + "lang_id int(11) NOT NULL default '0'" + ), + $wpdb->prefix.'registration_log' => array( + "email varchar(255) NOT NULL default ''", + "IP varchar(30) NOT NULL default ''", + "blog_id bigint(20) NOT NULL default '0'", + "date_registered datetime NOT NULL default '0000-00-00 00:00:00'" + ), + $wpdb->prefix.'signups' => array( + "domain varchar(200) NOT NULL default ''", + "path varchar(100) NOT NULL default ''", + "title longtext NOT NULL", + "user_login varchar(60) NOT NULL default ''", + "user_email varchar(100) NOT NULL default ''", + "registered datetime NOT NULL default '0000-00-00 00:00:00'", + "activated datetime NOT NULL default '0000-00-00 00:00:00'", + "active tinyint(1) NOT NULL default '0'", + "activation_key varchar(50) NOT NULL default ''", + "meta longtext" + ), + $wpdb->prefix.'site' => array( + "domain varchar(200) NOT NULL default ''", + "path varchar(100) NOT NULL default ''" + ), + $wpdb->prefix.'sitemeta' => array( + "site_id bigint(20) NOT NULL default '0'", + "meta_key varchar(255) default NULL", + "meta_value longtext" + ), + $wpdb->prefix.'users' => array( + "user_login varchar(60) NOT NULL default ''", + "spam tinyint(2) NOT NULL default '0'", + "deleted tinyint(2) NOT NULL default '0'" + ) + ); + if (version_compare($wp_version, '3.6', '<')) return false; + $results = $this->sanity_check(); + $return_val = array(); + $queries = array(); + if ($results !== true) { + if (!$this->maintenance_backup()) { + $message = __('Can\'t create backup file.', $domain); + echo $message; + return false; + } + $tables = array_keys($results); + foreach ($tables as $table) { + if (key_exists($table, $global_schema_to_change)) { + $queries = $global_schema_to_change[$table]; + } + if (key_exists($table, $network_schema_to_change)) { + if (!empty($queries)) { + $queries = array_merge($queries, $network_schema_to_change[$table]); + } else { + $queries = $network_schema_to_change[$table]; + } + } + foreach ($queries as $query) { + $sql = 'ALTER TABLE' . ' ' . $table . ' ' . 'CHANGE COLUMN' . ' ' . $query; + $res = $wpdb->query($sql); + if ($res === false) { + $return_val[] = __('Failed: ', $domain) . $query; + } + } + } + } else { + return true; + } + if (empty($return_val)) { + return true; + } else { + return $return_val; + } + } + + private function maintenance_backup() { + $result = array(); + $database_file = FQDB; + $db_name = basename(FQDB); + if (!file_exists($database_file)) { + return false; + } + $today = date("Ymd-His"); + if (!extension_loaded('zip')) { + $backup_file = $database_file . '.' . $today . '.maintenance-backup'; + if (copy($database_file, $backup_file)) { + $result = true; + } else { + $result = false; + } + } else { + $backup_file = $database_file . '.' . $today . '.maintenance-backup.zip'; + $zip = new ZipArchive(); + $res = $zip->open($backup_file, ZipArchive::CREATE | ZipArchive::OVERWRITE); + if ($res === true) { + $zip->addFile($database_file, $db_name); + $result = true; + } else { + $result = false; + } + $zip->close(); + } + return $result; + } + + function show_maintenance_page() { + global $utils; + $domain = $utils->text_domain; + if (is_multisite() && !current_user_can('manage_network_options')) { + die(__('You are not allowed to access this page!', $domain)); + } elseif (!current_user_can('manage_options')) { + die(__('You are not allowed to access this page!', $domain)); + } + + + if (isset($_GET['page']) && $_GET['page'] == 'maintenance') : ?> + +
+

+

+

+ + +

+

+ +

+

+ +

+

+ +

+ +
+ + + +
+
+ do_fix_database(); + if ($fix_results !== true) { + $title = '

'. __('Results', $domain) . '

'; + echo '
'; + echo $title; + echo '
    '; + foreach ($fix_results as $result) { + echo '
  • ' . $result . '
  • '; + } + echo '
'; + echo '
'; + } else { + $title = '

'. __('Results', $domain) . '

'; + $message = __('Your database restoration is successfully finished!', $domain); + echo '
'; + echo $title; + echo '

'.$message.'

'; + echo '
'; + } + } + if (isset($_POST['sanity-check'])) { + check_admin_referer('sqliteintegration-database-manip-stats'); + if (is_multisite() && !current_user_can('manage_network_options')) { + die(__('You are not allowed to do this operation!', $domain)); + } elseif (!current_user_can('manage_options')) { + die(__('You are not allowed to do this operation!', $domain)); + } + $check_results = $this->sanity_check(); + if ($check_results !== true) { + $title = '

'. __('Checked Results', $domain) . '

'; + echo '
'; + echo $title; + echo '
    '; + foreach ($check_results as $table => $damaged) { + $message = __(' needs restoring.', $domain); + echo '
  • ' . $table . '' . $message . '
  • '; + } + echo '
'; + echo '
'; + } else { + $title = '

'. __('Checked Results', $domain) . '

'; + $message = __('Your database is OK. You don\'t have to restore it.', $domain); + echo '
'; + echo $title; + echo '

'.$message.'

'; + echo '
'; + } + } + } +} +?> \ No newline at end of file diff --git a/utilities/documentation.php b/utilities/documentation.php index 04188f4..44895be 100644 --- a/utilities/documentation.php +++ b/utilities/documentation.php @@ -24,6 +24,7 @@ class SQLiteIntegrationDocument { +
diff --git a/utilities/patch.php b/utilities/patch.php index ed1a7f2..4b8e2f7 100644 --- a/utilities/patch.php +++ b/utilities/patch.php @@ -222,6 +222,7 @@ class PatchUtils { +
diff --git a/utilities/utility.php b/utilities/utility.php index acd0deb..f2b8295 100644 --- a/utilities/utility.php +++ b/utilities/utility.php @@ -439,6 +439,10 @@ class SQLiteIntegrationUtils { + + + +
@@ -463,6 +467,7 @@ class SQLiteIntegrationUtils { +
@@ -717,6 +722,7 @@ class SQLiteIntegrationUtils { +