| 1 |
<?php
|
| 2 |
// $Id:$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_menu().
|
| 6 |
*/
|
| 7 |
function mtk_menu($may_cache) {
|
| 8 |
$items = array();
|
| 9 |
|
| 10 |
if ($may_cache) {
|
| 11 |
$admin_access = user_access('administer site settings');
|
| 12 |
$items[] = array(
|
| 13 |
'path' => 'admin/mtk',
|
| 14 |
'title' => t('Content migration'),
|
| 15 |
'description' => t('Migration Toolkit Content migration.'),
|
| 16 |
'callback' => 'mtk_admin_overview',
|
| 17 |
'access' => $admin_access,
|
| 18 |
);
|
| 19 |
$items[] = array(
|
| 20 |
'path' => 'admin/mtk/dashboard',
|
| 21 |
'title' => t('Migration toolkit dashboard'),
|
| 22 |
'description' => t('Execute migration increments and roll back data.'),
|
| 23 |
'callback' => 'drupal_get_form',
|
| 24 |
'callback arguments' => array('mtk_dashboard'),
|
| 25 |
'access' => $admin_access,
|
| 26 |
);
|
| 27 |
$items[] = array(
|
| 28 |
'path' => 'admin/mtk/rollback',
|
| 29 |
'title' => t('Rollback'),
|
| 30 |
'description' => t('Content migration.'),
|
| 31 |
'callback' => 'mtk_rollback_page',
|
| 32 |
'access' => $admin_access,
|
| 33 |
'type' => MENU_CALLBACK
|
| 34 |
);
|
| 35 |
}
|
| 36 |
|
| 37 |
return $items;
|
| 38 |
}
|
| 39 |
|
| 40 |
/**
|
| 41 |
* Menu page callback.
|
| 42 |
*/
|
| 43 |
function mtk_admin_overview() {
|
| 44 |
$menu = menu_get_item(NULL, 'admin/mtk');
|
| 45 |
$content = system_admin_menu_block($menu);
|
| 46 |
$output = theme('admin_block_content', $content);
|
| 47 |
return $output;
|
| 48 |
}
|
| 49 |
|
| 50 |
/**
|
| 51 |
* Migration dashboard. Central point of action for user.
|
| 52 |
*/
|
| 53 |
function mtk_dashboard() {
|
| 54 |
// Get past migrations
|
| 55 |
$migration_history = variable_get('mtk_migration_history', array());
|
| 56 |
$done = array();
|
| 57 |
foreach ($migration_history as $time => $migrations) {
|
| 58 |
$items = array();
|
| 59 |
foreach ($migrations as $callback) {
|
| 60 |
$done[] = $callback;
|
| 61 |
$items[] = mtk_get_migration_name($callback);
|
| 62 |
}
|
| 63 |
$rows[] = array(
|
| 64 |
format_interval(time() - $time) .' '. t('ago'),
|
| 65 |
implode(', ', $items),
|
| 66 |
l(t('Roll back'), 'admin/mtk/rollback/'. $time, array(), 'destination='. $_GET['q']),
|
| 67 |
);
|
| 68 |
}
|
| 69 |
if ($rows) {
|
| 70 |
$form['migrated'] = array(
|
| 71 |
'#type' => 'fieldset',
|
| 72 |
'#title' => t('Executed migrations'),
|
| 73 |
'#description' => theme('table', $header, $rows),
|
| 74 |
);
|
| 75 |
}
|
| 76 |
|
| 77 |
// Build available migration options.
|
| 78 |
// @todo: Respect weights, add dependency definitions.
|
| 79 |
$migrations = mtk_get_migrations_list();
|
| 80 |
if (is_array($migrations)) {
|
| 81 |
foreach ($migrations as $callback => $name) {
|
| 82 |
$count++;
|
| 83 |
// Check if migrated.
|
| 84 |
if (in_array($callback, $done)) {
|
| 85 |
$migrated[$callback] = $count .') '. $name;
|
| 86 |
}
|
| 87 |
else {
|
| 88 |
$options[$callback] = $count .') '. $name;
|
| 89 |
}
|
| 90 |
}
|
| 91 |
$form['migrations'] = array(
|
| 92 |
'#type' => 'fieldset',
|
| 93 |
'#title' => t('Available migrations'),
|
| 94 |
'#tree' => FALSE,
|
| 95 |
);
|
| 96 |
$form['migrations']['migrations'] = array(
|
| 97 |
'#type' => 'checkboxes',
|
| 98 |
'#options' => $options,
|
| 99 |
);
|
| 100 |
}
|
| 101 |
else {
|
| 102 |
drupal_set_message(t('No migrations defined.'));
|
| 103 |
}
|
| 104 |
|
| 105 |
// Build watchdog link.
|
| 106 |
// @todo: Watchdog (2 notices, 3 errors since first migration)
|
| 107 |
$rows = array(
|
| 108 |
array(
|
| 109 |
l(t('Content'), 'admin/content/node'),
|
| 110 |
l(t('Users'), 'admin/user/user'),
|
| 111 |
l(t('Watchdog'), 'admin/logs/watchdog'),
|
| 112 |
),
|
| 113 |
);
|
| 114 |
$form['watchdog'] = array(
|
| 115 |
'#type' => 'fieldset',
|
| 116 |
'#title' => t('Links'),
|
| 117 |
'#description' => theme('table', NULL, $rows) .'<br />',
|
| 118 |
);
|
| 119 |
|
| 120 |
$form['submit'] = array(
|
| 121 |
'#type' => 'submit',
|
| 122 |
'#value' => t('Migrate'),
|
| 123 |
);
|
| 124 |
return $form;
|
| 125 |
}
|
| 126 |
|
| 127 |
/**
|
| 128 |
* Submit function for dashboard.
|
| 129 |
*/
|
| 130 |
function mtk_dashboard_submit($form_id, $form_values) {
|
| 131 |
if ($form_values['migrations'] && count($form_values['migrations'] > 1)) {
|
| 132 |
foreach ($form_values['migrations'] as $callback => $selected) {
|
| 133 |
if ($selected) {
|
| 134 |
$callbacks[] = $callback;
|
| 135 |
}
|
| 136 |
}
|
| 137 |
if ($callbacks) {
|
| 138 |
mtk_migrate($callbacks);
|
| 139 |
}
|
| 140 |
}
|
| 141 |
}
|
| 142 |
|
| 143 |
/**
|
| 144 |
* Rollback confirmation form.
|
| 145 |
*/
|
| 146 |
function mtk_rollback_page($time) {
|
| 147 |
mtk_rollback($time);
|
| 148 |
drupal_goto($_GET['destination']);
|
| 149 |
}
|
| 150 |
|
| 151 |
/**
|
| 152 |
* Do migration.
|
| 153 |
*/
|
| 154 |
function mtk_migrate($callbacks) {
|
| 155 |
// Include migration tools.
|
| 156 |
mtk_include();
|
| 157 |
$backup_time = mtk_snapshot();
|
| 158 |
// Do migrations.
|
| 159 |
foreach ($callbacks as $callback) {
|
| 160 |
call_user_func($callback);
|
| 161 |
// @todo: seperate migration backups from migration calls so that we
|
| 162 |
// can do incremental steps on a single migration callback and no backups for
|
| 163 |
// dry run migrations.
|
| 164 |
$migrations[$callback] = $callback;
|
| 165 |
}
|
| 166 |
if ($migrations) {
|
| 167 |
// Store in history variable.
|
| 168 |
$migration_history = variable_get('mtk_migration_history', array());
|
| 169 |
$migration_history[$backup_time] = $migrations;
|
| 170 |
variable_set('mtk_migration_history', $migration_history);
|
| 171 |
}
|
| 172 |
}
|
| 173 |
|
| 174 |
/**
|
| 175 |
* Rolls back to latest snapshot.
|
| 176 |
*
|
| 177 |
* @param unknown_type $backup_time
|
| 178 |
*/
|
| 179 |
function mtk_rollback($backup_time) {
|
| 180 |
// @todo: roll back.
|
| 181 |
if (demo_reset(_mtk_create_filename($backup_time))) {
|
| 182 |
$migration_history = variable_get('mtk_migration_history', array());
|
| 183 |
foreach ($migration_history as $time => $migrations) {
|
| 184 |
if ($time >= $backup_time) {
|
| 185 |
unset($migration_history[$time]);
|
| 186 |
}
|
| 187 |
}
|
| 188 |
variable_set('mtk_migration_history', $migration_history);
|
| 189 |
drupal_set_message(t('Roll back to before !ago ago.', array('!ago' => format_interval(time() - $backup_time))));
|
| 190 |
}
|
| 191 |
}
|
| 192 |
|
| 193 |
/**
|
| 194 |
* Take a snapshot of current DB.
|
| 195 |
* @return: Unix time of snapshot, serves as identifier.
|
| 196 |
*/
|
| 197 |
function mtk_snapshot() {
|
| 198 |
$time = time();
|
| 199 |
mtk_demo_dump(_mtk_create_filename($time));
|
| 200 |
return $time;
|
| 201 |
}
|
| 202 |
|
| 203 |
/**
|
| 204 |
* This one should actually live in the demo module.
|
| 205 |
* Code mostly ripped from demp_dump_submit().
|
| 206 |
*
|
| 207 |
* @param string $filename
|
| 208 |
*/
|
| 209 |
function mtk_demo_dump($filename) {
|
| 210 |
global $db_type;
|
| 211 |
|
| 212 |
// Write .info file
|
| 213 |
$values['filename'] = $filename;
|
| 214 |
$values['description'] = t('MTK Backup');
|
| 215 |
|
| 216 |
$info = demo_set_info($values);
|
| 217 |
if (!$info) {
|
| 218 |
return false;
|
| 219 |
}
|
| 220 |
|
| 221 |
// Include database specific functions
|
| 222 |
switch ($db_type) {
|
| 223 |
case 'mysqli':
|
| 224 |
$engine = 'mysql';
|
| 225 |
break;
|
| 226 |
default:
|
| 227 |
$engine = $db_type;
|
| 228 |
break;
|
| 229 |
}
|
| 230 |
require_once drupal_get_path('module', 'demo') ."/database_{$engine}_dump.inc";
|
| 231 |
|
| 232 |
// Increase PHP's max_execution_time for large dumps.
|
| 233 |
@set_time_limit(600);
|
| 234 |
|
| 235 |
// Perform dump
|
| 236 |
$fileconfig = demo_get_fileconfig($info['filename']);
|
| 237 |
$exclude = array('{cache}', '{cache_content}', '{cache_filter}', '{cache_menu}', '{cache_page}', '{cache_views}', '{panels_object_cache}', '{watchdog}');
|
| 238 |
$exclude = array_map('db_prefix_tables', $exclude);
|
| 239 |
demo_dump_db($fileconfig['sqlfile'], $exclude);
|
| 240 |
}
|
| 241 |
|
| 242 |
/**
|
| 243 |
* Retrieve a list of available migrations
|
| 244 |
*
|
| 245 |
* @return unknown
|
| 246 |
*/
|
| 247 |
function mtk_get_migrations_list() {
|
| 248 |
$migrations = module_invoke_all('mtk');
|
| 249 |
foreach ($migrations as $migration) {
|
| 250 |
$migration_list[$migration['callback']] = $migration['name'];
|
| 251 |
}
|
| 252 |
return $migration_list;
|
| 253 |
}
|
| 254 |
|
| 255 |
/**
|
| 256 |
* Include libraries.
|
| 257 |
*/
|
| 258 |
function mtk_include() {
|
| 259 |
static $loaded = FALSE;
|
| 260 |
if (!$loaded) {
|
| 261 |
$module_path = drupal_get_path('module', 'mtk') .'/';
|
| 262 |
foreach (array('crud', 'helper') as $sub_dir) {
|
| 263 |
$path = $module_path . $sub_dir;
|
| 264 |
$files = drupal_system_listing('.*\.inc$', $path, 'name', 0);
|
| 265 |
foreach($files as $file) {
|
| 266 |
require_once("./$file->filename");
|
| 267 |
}
|
| 268 |
}
|
| 269 |
}
|
| 270 |
$loaded = TRUE;
|
| 271 |
}
|
| 272 |
|
| 273 |
/**
|
| 274 |
* Retrieve the human readable name for a given callback.
|
| 275 |
*/
|
| 276 |
function mtk_get_migration_name($callback) {
|
| 277 |
if ($migration_list = mtk_get_migrations_list()) {
|
| 278 |
return $migration_list[$callback];
|
| 279 |
}
|
| 280 |
}
|
| 281 |
|
| 282 |
/**
|
| 283 |
* Creates a file db dump file name.
|
| 284 |
*/
|
| 285 |
function _mtk_create_filename($time) {
|
| 286 |
return 'backup-'. gmdate('Y-m-d\TH-i-s', $time);
|
| 287 |
}
|