View source
<?php
module_load_include('inc', 'coder_upgrade', 'conversions/list');
function coder_upgrade_conversions_form($form, &$form_state) {
$checks_passed = coder_upgrade_library_checks();
list($upgrades, $extensions, $directories, $modules) = coder_upgrade_conversions_form_defaults($form_state);
$form['tabs'] = array(
'#type' => 'vertical_tabs',
'#default_tab' => 'edit-directories',
);
$form['tabs']['upgrades'] = coder_upgrade_upgrades_build($upgrades);
$form['tabs']['extensions'] = coder_upgrade_extensions_build($extensions);
$form['tabs']['directories'] = coder_upgrade_directories_build($directories);
$form['tabs']['modules'] = coder_upgrade_modules_build($modules);
$form['convert'] = array(
'#type' => 'submit',
'#value' => t('Convert files'),
'#disabled' => !gplib_version_check(),
);
return $form;
}
function coder_upgrade_upgrades_build(&$upgrades) {
$upgrades_all = _coder_upgrade_upgrades();
foreach ($upgrades_all as $name => $upgrade) {
$upgrade_options[$name] = isset($upgrade['link']) ? l($upgrade['title'], $upgrade['link']) : $upgrade['title'];
if (isset($upgrade['description'])) {
$upgrade_options[$name] .= ' (' . $upgrade['description'] . ')';
}
$upgrades_sort[$name] = $upgrade['title'];
}
asort($upgrades_sort);
foreach ($upgrades_sort as $name => $upgrade) {
$upgrades_sort[$name] = $upgrade_options[$name];
}
$header = array(
'category' => array(
'data' => t('Category'),
'field' => 'category',
),
);
$i = 0;
$rows = array();
foreach ($upgrades_sort as $name => $upgrade) {
$row = array();
$row['category'] = $upgrades_sort[$name];
$row['description'] = 'Missing';
$row['#weight'] = ++$i;
$rows[$name] = $row;
}
$upgrade_fs = array(
'#type' => 'fieldset',
'#title' => t('Upgrades'),
'#description' => t('Apply the selected conversion routines ...'),
'#tree' => TRUE,
);
$upgrade_fs['list'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#default_value' => isset($upgrades) ? $upgrades : array(),
'#empty' => t('No routines available'),
);
return $upgrade_fs;
}
function coder_upgrade_extensions_build(&$extensions) {
$types = array(
'inc' => 'PHP code files',
'info' => 'Info files used with module installation',
'install' => 'PHP code files used with module installation, update and uninstallation',
'module' => 'PHP code files',
'php' => 'PHP code files',
'profile' => 'PHP code files used with site installation',
'test' => 'SimpleTest files',
'theme' => 'PHP code files used with theming',
);
$header = array(
'extension' => array(
'data' => t('Extension'),
'field' => 'extension',
),
'description' => array(
'data' => t('Description'),
'field' => 'description',
),
);
$i = 0;
$rows = array();
foreach ($types as $key => $description) {
$row = array();
$row['extension'] = $key;
$row['description'] = $description;
$row['#weight'] = ++$i;
$rows[$key] = $row;
}
$extension_fs = array(
'#type' => 'fieldset',
'#title' => t('Extensions'),
'#description' => t('... to files with the selected file extensions ...'),
'#tree' => TRUE,
);
$extension_fs['list'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#default_value' => isset($extensions) ? $extensions : array(),
'#empty' => t('No extensions available'),
);
return $extension_fs;
}
function coder_upgrade_directories_build(&$directories) {
$deadwood_dir = variable_get('coder_upgrade_dir_old', DEADWOOD_OLD);
$dirs = coder_upgrade_directory_list();
$header = array(
'name' => array(
'data' => t('Name'),
'field' => 'name',
),
'path' => array(
'data' => t('Location'),
'field' => 'path',
),
);
$i = 0;
$rows = array();
foreach ($dirs as $dir) {
$row = array();
$row['name'] = isset($directories[$dir]) ? l($dir, coder_upgrade_patch_link($dir), array(
'attributes' => array(
'target' => '_blank',
),
)) : $dir;
$row['path'] = $deadwood_dir . '/' . $dir;
$row['#weight'] = ++$i;
$rows[$dir] = $row;
}
$directory_fs = array(
'#type' => 'fieldset',
'#title' => t('Directories'),
'#description' => t('... residing in the selected directories (beneath the files directory), or ...'),
'#tree' => TRUE,
);
$directory_fs['list'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#default_value' => isset($directories) ? $directories : array(),
'#empty' => t('No directories available'),
);
return $directory_fs;
}
function coder_upgrade_modules_build(&$modules) {
$header = array(
'name' => array(
'data' => t('Name'),
'field' => 'name',
),
'path' => array(
'data' => t('Location'),
'field' => 'path',
),
);
$i = 0;
$rows = coder_upgrade_module_list();
foreach ($rows as $key => $row) {
$rows[$key]['name'] = isset($modules[$key]) ? l($row['name'], coder_upgrade_patch_link($key)) : $row['name'];
$rows[$key]['#weight'] = ++$i;
}
$module_fs = array(
'#type' => 'fieldset',
'#title' => t('Modules'),
'#description' => t('... residing in the selected modules (beneath the drupal directory).'),
'#tree' => TRUE,
);
$module_fs['list'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $rows,
'#default_value' => isset($modules) ? $modules : array(),
'#empty' => t('No modules available'),
);
return $module_fs;
}
function coder_upgrade_conversions_form_validate($form, &$form_state) {
$keys = coder_upgrade_selection_types($form_state);
$count = 0;
$values = $form_state['values'];
foreach ($values as $key => $list) {
if (!in_array($key, $keys)) {
continue;
}
$selections = coder_upgrade_selections_extract($list['list']);
if (in_array($key, array(
'upgrades',
'extensions',
)) && !count($selections)) {
form_set_error($key, t('Please select at least one item in the %item tab.', array(
'%item' => ucfirst($key),
)));
}
elseif (in_array($key, array(
'directories',
'modules',
))) {
$count += count($selections);
}
}
$test = array_diff(array(
'directories',
'modules',
), $keys);
if (!$count && empty($test)) {
form_set_error($key, t('Please select at least one item in the %item1 or %item2 tabs.', array(
'%item1' => ucfirst('directories'),
'%item2' => ucfirst('modules'),
)));
}
}
function coder_upgrade_conversions_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
$success = coder_upgrade_conversions_apply($form_state);
if ($success) {
drupal_set_message(t('Module conversion routines were applied.'));
drupal_set_message(t('Click to view the !log.', array(
'!log' => l(t('conversion log file'), coder_upgrade_path('log'), array(
'attributes' => array(
'target' => '_blank',
),
)),
)));
drupal_set_message(t('To view a <strong>patch file</strong>, click on a <strong>Name link</strong> in the Directories and Modules tabs below.'));
}
else {
drupal_set_message(t('Module conversion routines failed to complete.'), 'error');
}
}
function coder_upgrade_conversions_apply($form_state) {
list($upgrades, $extensions, $items) = coder_upgrade_conversions_prepare($form_state);
if (variable_get('coder_upgrade_use_separate_process', FALSE)) {
drupal_set_message(t('Module conversion routines will run in a separate process.'));
$path = coder_upgrade_parameters_save($upgrades, $extensions, $items);
$script = drupal_get_path('module', 'coder_upgrade') . '/scripts/coder_upgrade.run.php';
$output = coder_upgrade_directory_path('base') . 'coder_upgrade.run.txt';
$command = "php {$script} -- file={$path} > {$output}";
exec($command, $errors, $success);
$success = $success === 0;
if ($success && !empty($errors)) {
drupal_set_message(trim(implode("\n", $errors)), 'error');
}
}
else {
module_load_include('inc', 'coder_upgrade', 'includes/main');
drupal_set_message(t('Module conversion routines will run in the same process.'));
$success = coder_upgrade_start($upgrades, $extensions, $items);
}
return $success;
}
function coder_upgrade_conversions_prepare($form_state) {
list($upgrades, $extensions, $directories, $modules) = coder_upgrade_selections($form_state);
$upgrades_all = _coder_upgrade_upgrades();
foreach ($upgrades as $name => $upgrade) {
$upgrades[$name] = array(
'module' => isset($upgrades_all[$name]['module']) ? $upgrades_all[$name]['module'] : '',
'files' => isset($upgrades_all[$name]['files']) ? $upgrades_all[$name]['files'] : array(),
);
}
$old_dir = DRUPAL_ROOT . '/' . coder_upgrade_directory_path('old');
$new_dir = DRUPAL_ROOT . '/' . coder_upgrade_directory_path('new');
$items = array();
foreach ($directories as $key => $directory) {
$items[] = array(
'name' => $key,
'old_dir' => $old_dir . $key,
'new_dir' => $new_dir . $key,
);
}
$last = 'xx_XX';
$rows = coder_upgrade_module_list();
foreach ($modules as $key => $module) {
if (isset($rows[$key])) {
$row = $rows[$key];
if (strpos($row['dir'] . '/', $last . '/') === 0) {
continue;
}
$last = $row['dir'];
$items[] = array(
'name' => $key,
'old_dir' => $row['dir'],
'new_dir' => $new_dir . $key,
);
}
}
return array(
$upgrades,
$extensions,
$items,
);
}
function coder_upgrade_parameters_save($upgrades, $extensions, $items) {
foreach ($upgrades as $name => &$upgrade) {
$upgrade['path'] = drupal_get_path('module', $upgrade['module']);
}
$paths = array(
'files_base' => coder_upgrade_directory_path('', FALSE),
'libraries_base' => str_replace('/grammar_parser', '', libraries_get_path('grammar_parser')),
'modules_base' => str_replace('/coder', '', drupal_get_path('module', 'coder')),
);
$variables = array(
'coder_upgrade_dir' => variable_get('coder_upgrade_dir', DEADWOOD_DIR),
'coder_upgrade_dir_patch' => variable_get('coder_upgrade_dir_patch', DEADWOOD_PATCH),
'coder_upgrade_replace_files' => variable_get('coder_upgrade_replace_files', FALSE),
'coder_upgrade_preserve_array_format' => variable_get('coder_upgrade_preserve_array_format', FALSE),
'coder_upgrade_enable_debug_output' => variable_get('coder_upgrade_enable_debug_output', FALSE),
'coder_upgrade_enable_parser_debug_output' => variable_get('coder_upgrade_enable_parser_debug_output', FALSE),
'coder_upgrade_use_separate_process' => variable_get('coder_upgrade_use_separate_process', FALSE),
);
$parameters['paths'] = $paths;
$parameters['theme_cache'] = coder_upgrade_path('theme_cache');
$parameters['variables'] = $variables;
$parameters['upgrades'] = $upgrades;
$parameters['extensions'] = $extensions;
$parameters['items'] = $items;
$path = coder_upgrade_path('runtime');
file_put_contents($path, serialize($parameters));
return $path;
}
function coder_upgrade_conversions_form_defaults($form_state) {
$upgrades = array(
'coder_upgrade' => 1,
);
$extensions = array(
'inc' => TRUE,
'info' => TRUE,
'install' => TRUE,
'module' => TRUE,
);
$directories = array();
$modules = array();
if (!isset($form_state['values'])) {
return array(
$upgrades,
$extensions,
$directories,
$modules,
);
}
return coder_upgrade_selections($form_state);
}
function coder_upgrade_selections($form_state) {
$upgrades = $extensions = $directories = $modules = array();
$keys = coder_upgrade_selection_types($form_state);
$values = $form_state['values'];
foreach ($keys as $key) {
if (isset($values[$key])) {
${$key} = coder_upgrade_selections_extract($values[$key]['list']);
}
}
return array(
$upgrades,
$extensions,
$directories,
$modules,
);
}
function coder_upgrade_selections_extract($values) {
$selections = array();
foreach ($values as $key => $value) {
if ($value) {
$selections[$key] = $key;
}
}
return $selections;
}
function coder_upgrade_selection_types($form_state) {
if (isset($form_state['defaults']) && is_array($form_state['defaults']) && $form_state['defaults']) {
return $form_state['defaults'];
}
return array(
'upgrades',
'extensions',
'directories',
'modules',
);
}
function coder_upgrade_directory_list() {
$dirs = array();
$path = coder_upgrade_directory_path('old', FALSE);
if (!is_dir($path)) {
drupal_set_message(t('Module input directory does not exist at @path.', array(
'@path' => $path,
)), 'error');
return $dirs;
}
$path = realpath($path);
if (!$path) {
drupal_set_message(t('Module input directory does not exist at @path.', array(
'@path' => $path,
)), 'error');
return $dirs;
}
$dirs = coder_upgrade_scan_directory($path);
if (!$dirs) {
drupal_set_message(t('Please place modules to be converted in @path.', array(
'@path' => $path,
)), 'error');
}
return $dirs;
}
function coder_upgrade_module_list($core = NULL, $status = -1) {
$test = is_null($core) ? variable_get('coder_upgrade_upgrade_core', FALSE) : $core;
$like = $test ? 'LIKE' : 'NOT LIKE';
$where = $status == -1 ? '' : 'AND status = :status';
$sql = "SELECT name, filename, type, status, info,\n REPLACE(filename, CONCAT('/', name, '.', type), '') AS directory\n FROM {system}\n WHERE type = 'module'\n AND filename {$like} 'modules/%'\n {$where}\n ORDER BY directory, name";
$default_value = 0;
$results = db_query($sql, array(
':status' => $status,
));
$rows = array();
foreach ($results as $module) {
$info = unserialize($module->info);
$row = array();
$row['name'] = $info['name'];
$row['path'] = dirname($module->filename);
$row['dir'] = $module->directory;
$rows[$module->name] = $row;
}
return $rows;
}
function coder_upgrade_patch_link($name) {
return 'files/coder_upgrade/patch/' . $name . '.patch';
}
function coder_upgrade_patch_display($filename) {
echo '<html><head><body><pre>' . check_plain(file_get_contents(coder_upgrade_patch_path($filename))) . '</pre></body></head></html>';
}
function coder_upgrade_patch_path($filename) {
static $dirname = '';
if (!$dirname) {
$dirname = coder_upgrade_directory_path('patch');
}
return $dirname . "{$filename}";
}