|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 define('CLI_SCRIPT', true); 00028 00029 require(dirname(dirname(dirname(__FILE__))).'/config.php'); 00030 require_once($CFG->libdir.'/clilib.php'); // cli only functions 00031 00032 if ($DB->get_dbfamily() !== 'mysql') { 00033 cli_error('This function is designed for MySQL databases only!'); 00034 } 00035 00036 // now get cli options 00037 list($options, $unrecognized) = cli_get_params(array('help'=>false, 'list'=>false, 'engine'=>false), 00038 array('h'=>'help', 'l'=>'list')); 00039 00040 if ($unrecognized) { 00041 $unrecognized = implode("\n ", $unrecognized); 00042 cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); 00043 } 00044 00045 $help = 00046 "MySQL engine conversions script. 00047 00048 It is recommended to stop the web server before the conversion. 00049 Do not use MyISAM if possible, because it is not ACID compliant 00050 and does not support transactions. 00051 00052 Options: 00053 --engine=ENGINE Convert MySQL tables to different engine 00054 -l, --list Show table information 00055 -h, --help Print out this help 00056 00057 Example: 00058 \$sudo -u www-data /usr/bin/php admin/cli/mysql_engine.php --engine=InnoDB 00059 "; 00060 00061 if (!empty($options['engine'])) { 00062 $engine = clean_param($options['engine'], PARAM_ALPHA); 00063 00064 echo "Converting tables to '$engine' for $CFG->wwwroot:\n"; 00065 $prefix = $DB->get_prefix(); 00066 $prefix = str_replace('_', '\\_', $prefix); 00067 $sql = "SHOW TABLE STATUS WHERE Name LIKE BINARY '$prefix%'"; 00068 $rs = $DB->get_recordset_sql($sql); 00069 $converted = 0; 00070 $skipped = 0; 00071 foreach ($rs as $table) { 00072 if ($table->engine === $engine) { 00073 echo str_pad($table->name, 40). " - NO CONVERSION NEEDED\n"; 00074 $skipped++; 00075 continue; 00076 } 00077 echo str_pad($table->name, 40). " - "; 00078 00079 $DB->change_database_structure("ALTER TABLE {$table->name} ENGINE = $engine"); 00080 echo "DONE\n"; 00081 $converted++; 00082 } 00083 $rs->close(); 00084 echo "Converted: $converted, skipped: $skipped\n"; 00085 exit(0); // success 00086 00087 } else if (!empty($options['list'])) { 00088 echo "List of tables for $CFG->wwwroot:\n"; 00089 $prefix = $DB->get_prefix(); 00090 $prefix = str_replace('_', '\\_', $prefix); 00091 $sql = "SHOW TABLE STATUS WHERE Name LIKE BINARY '$prefix%'"; 00092 $rs = $DB->get_recordset_sql($sql); 00093 $counts = array(); 00094 foreach ($rs as $table) { 00095 if (isset($counts[$table->engine])) { 00096 $counts[$table->engine]++; 00097 } else { 00098 $counts[$table->engine] = 1; 00099 } 00100 echo str_pad($table->engine, 10); 00101 echo $table->name . "\n"; 00102 } 00103 $rs->close(); 00104 00105 echo "\n"; 00106 echo "Table engines summary for $CFG->wwwroot:\n"; 00107 foreach ($counts as $engine => $count) { 00108 echo "$engine: $count\n"; 00109 } 00110 exit(0); // success 00111 00112 } else { 00113 echo $help; 00114 die; 00115 }