Commit eef6e452 authored by Tim Steiner's avatar Tim Steiner
Browse files

Merge pull request #515 from erasmussen2/issue-469

Pull request: Closes #469 Move migration code into its own module
parents 245d3969 c69d4815
......@@ -30,5 +30,6 @@ dependencies[] = imce_mkdir
dependencies[] = imce_rename
dependencies[] = imce_wysiwyg
dependencies[] = unl
dependencies[] = unl_migration
dependencies[] = unl_wysiwyg
dependencies[] = unl_varnish
......@@ -232,11 +232,6 @@ function unl_node_presave($node) {
*/
function unl_permission() {
return array(
'unl migration' => array(
'title' => t('Migration'),
'description' => t('Migrate UNL Template based sites to drupal'),
),
'unl grant all permissions' => array(
'title' => t('Grant All Permissions'),
'description' => t('If this is not checked, a user can only grant permissions that they themselves have. Requires the "Administer permissions" permission.'),
......@@ -315,17 +310,6 @@ function unl_menu() {
'file' => 'technical_feedback.php',
);
// Adds UNL Migration Tool to the Content menu for admin
$items['admin/content/unl/migration'] = array(
'title' => 'UNL Migration Tool',
'description' => 'Migrate a static UNL template page into Drupal.',
'access arguments' => array('unl migration'),
'page callback' => 'drupal_get_form',
'page arguments' => array('unl_migration'),
'type' => MENU_LOCAL_TASK,
'file' => 'unl_migration.php',
);
$items['admin/content/unl/reset'] = array(
'title' => 'Reset Site',
'description' => 'Remove all nodes, menu items, etc from this site.',
......@@ -815,34 +799,6 @@ function unl_user_admin_role_validate($form, &$form_state) {
}
}
/**
* Implementation of hook_cron().
*/
function unl_cron() {
_unl_cron_migration_step();
}
/**
* If a site is being migrated via cron jobs, do some work towards that migration.
*/
function _unl_cron_migration_step() {
// We don't want this running as system user, only the web user.
if (PHP_SAPI == 'cli') {
return;
}
$queue = DrupalQueue::get('unl_migration');
if ($queue->numberOfItems() > 0) {
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'unl_migration.php';
$item = $queue->claimItem(120);
$queue->deleteItem($item);
if (unl_migration_queue_step($item->data)) {
module_load_include('module', 'unl_multisite');
_unl_multisite_send_site_created_email();
}
}
}
/**
* Custom function to sanitize user created URLs
*/
......
name = UNL Migration
description = Migrate websites into UNLcms.
package = UNL
dependencies[] = unl
core = 7.x
version = "7.x-1.x"
files[] = unl_migration.module
files[] = unl_migration.php
<?php
/**
* Implementation of hook_menu().
*/
function unl_migration_menu() {
$items = array();
// Adds UNL Migration Tool to the Content menu for admin
$items['admin/content/unl/migration'] = array(
'title' => 'UNL Migration Tool',
'description' => 'Migrate a static UNL template page into Drupal.',
'access arguments' => array('unl migration'),
'page callback' => 'drupal_get_form',
'page arguments' => array('unl_migration'),
'type' => MENU_LOCAL_TASK,
'file' => 'unl_migration.php',
);
return $items;
}
/**
* Implementation of hook_permission().
*/
function unl_migration_permission() {
return array(
'unl migration' => array(
'title' => t('Migration'),
'description' => t('Migrate UNL Template based sites to drupal'),
),
);
}
/**
* Implementation of hook_cron().
*/
function unl_migration_cron() {
_unl_migration_cron_migration_step();
}
/**
* If a site is being migrated via cron jobs, do some work towards that migration.
*/
function _unl_migration_cron_migration_step() {
// We don't want this running as system user, only the web user.
if (PHP_SAPI == 'cli') {
return;
}
$queue = DrupalQueue::get('unl_migration');
if ($queue->numberOfItems() > 0) {
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'unl_migration.php';
$item = $queue->claimItem(120);
$queue->deleteItem($item);
if (unl_migration_queue_step($item->data)) {
module_load_include('module', 'unl');
unl_send_site_created_email();
}
}
}
......@@ -12,21 +12,21 @@ function unl_migration($form, &$form_state)
'#value' => 'Continue',
);
return $form;
}
}
$form['root'] = array(
'#type' => 'fieldset',
'#title' => 'Migration Tool',
);
$form['root']['site_url'] = array(
'#type' => 'textfield',
'#title' => t('Site URL'),
'#description' => t('Full URL to the existing site you wish to migrate'),
'#required' => TRUE
);
$form['root']['frontier_path'] = array(
'#type' => 'textfield',
'#title' => t('Frontier FTP Path'),
......@@ -54,17 +54,17 @@ function unl_migration($form, &$form_state)
'#description' => t("Normally, this won't interfere with non-liferay sites. If you have a /web directory, you should turn this off."),
'#default_value' => 1,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Migrate'
);
return $form;
}
function unl_migration_submit($form, &$form_state) {
$migration = new Unl_Migration_Tool(
$form_state['values']['site_url'],
$form_state['values']['frontier_path'],
......@@ -73,7 +73,7 @@ function unl_migration_submit($form, &$form_state) {
$form_state['values']['ignore_duplicates'],
$form_state['values']['use_liferay_code']
);
$operations = array(
array(
'unl_migration_step',
......@@ -82,7 +82,7 @@ function unl_migration_submit($form, &$form_state) {
)
)
);
$batch = array(
'operations' => $operations,
'file' => substr(__FILE__, strlen(DRUPAL_ROOT) + 1),
......@@ -97,12 +97,12 @@ function unl_migration_step($migration, &$context)
$migration = Unl_Migration_Tool::load_from_disk($context['sandbox']['file']);
$finished = $context['sandbox']['finished'];
}
if ($migration->migrate()) {
$context['finished'] = 1;
return;
}
$finished += 0.01;
if ($finished > 0.99) {
$finished = 0.99;
......@@ -165,7 +165,7 @@ class Unl_Migration_Tool
private $_frontierFilesScanned = array();
private $_ignoreDuplicates = FALSE;
private $_useLiferayCode = TRUE;
/**
* Keep track of the state of the migration progress so that we can resume later
* @var int
......@@ -176,9 +176,9 @@ class Unl_Migration_Tool
const STATE_PROCESSING_PAGES = 3;
const STATE_CREATING_NODES = 4;
const STATE_DONE = 5;
private $_start_time;
public function __construct($baseUrl, $frontierPath, $frontierUser, $frontierPass, $ignoreDuplicates, $useLiferayCode = FALSE)
{
header('Content-type: text/plain');
......@@ -189,13 +189,13 @@ class Unl_Migration_Tool
if ($remoteHostname == 'frontier.unl.edu') {
$this->_isFrontier = TRUE;
}
// Add trailing slash if necessary
$baseUrl = trim($baseUrl);
if (substr($baseUrl, -1) != '/') {
$baseUrl .= '/';
}
$frontierPath = trim ($frontierPath);
if ($frontierPath && substr($frontierPath, -1) != '/') {
$frontierPath .= '/';
......@@ -204,34 +204,34 @@ class Unl_Migration_Tool
$this->_frontierPath = $frontierPath;
$this->_frontierUser = $frontierUser;
$this->_frontierPass = $frontierPass;
$this->_ignoreDuplicates = (bool) $ignoreDuplicates;
$this->_useLiferayCode = (bool) $useLiferayCode;
$this->_baseUrl = $baseUrl;
$this->_addSitePath('');
}
public function migrate($time_limit = 5)
{
if (!$this->_sanity_check()) {
return TRUE;
}
$this->_start_time = time();
ini_set('memory_limit', -1);
if ($this->_state == self::STATE_NONE) {
if (!$this->_frontierScan('', $time_limit)) {
return FALSE;
}
$this->_state = self::STATE_PROCESSING_BLOCKS;
if (time() - $this->_start_time > $time_limit) {
return FALSE;
}
}
if ($this->_state == self::STATE_PROCESSING_BLOCKS) {
// Parse the menu
$this->_processMenu();
......@@ -239,12 +239,12 @@ class Unl_Migration_Tool
$this->_process_breadcrumbs();
$this->_state = self::STATE_PROCESSING_PAGES;
}
if ($this->_state == self::STATE_PROCESSING_PAGES) {
// Process all of the pages on the site (Takes a while)
do {
set_time_limit(max(30, $time_limit));
$pagesToProcess = $this->_getPagesToProcess();
foreach ($pagesToProcess as $pageToProcess) {
if (time() - $this->_start_time > $time_limit) {
......@@ -253,8 +253,8 @@ class Unl_Migration_Tool
$this->_processPage($pageToProcess);
}
} while (count($pagesToProcess) > 0);
// Fix any links to files that got moved to sites/<site>/files
foreach ($this->_hrefTransform as $path => &$transforms) {
if (array_key_exists('', $transforms)) {
......@@ -269,11 +269,11 @@ class Unl_Migration_Tool
}
}
}
$this->_state = self::STATE_CREATING_NODES;
}
if ($this->_state == self::STATE_CREATING_NODES) {
// Update links and then create new page nodes. (Takes a while)
foreach ($this->_content as $path => $content) {
......@@ -284,24 +284,24 @@ class Unl_Migration_Tool
return FALSE;
}
set_time_limit(max(30, $time_limit));
$hrefTransforms = isset($this->_hrefTransform[$path]) ? $this->_hrefTransform[$path] : array();
foreach ($hrefTransforms as $hrefTransformFrom => $hrefTransformTo) {
$content = str_replace(htmlspecialchars($hrefTransformFrom), htmlspecialchars($hrefTransformTo), $content);
}
$pageTitle = $this->_pageTitles[$path];
$this->_createPage($pageTitle, $content, $path, '' == $path);
$this->_createdContent[] = $path;
}
$this->_createMenu();
$this->_create_blocks();
$this->_create_breadcrumbs();
$this->_state = self::STATE_DONE;
}
return TRUE;
}
......@@ -312,7 +312,7 @@ class Unl_Migration_Tool
}
return TRUE;
}
private function _addSitePath($path)
{
if (($fragmentStart = strrpos($path, '#')) !== FALSE) {
......@@ -324,7 +324,7 @@ class Unl_Migration_Tool
}
$this->_siteMap[hash('SHA256', $path)] = $path;
}
private function _getPagesToProcess()
{
$pagesToProcess = array();
......@@ -336,36 +336,36 @@ class Unl_Migration_Tool
}
return $pagesToProcess;
}
private function _addProcessedPage($path)
{
$this->_processedPages[hash('SHA256', $path)] = $path;
}
private function _processMenu()
{
$content = $this->_getUrl($this->_baseUrl);
$html = $content['content'];
$dom = new DOMDocument();
$dom->loadHTML($html);
$navlinksNode = $dom->getElementById('navigation');
if (!$navlinksNode) {
return;
}
// Check to see if there's a base tag on this page.
$base_tags = $dom->getElementsByTagName('base');
$page_base = NULL;
if ($base_tags->length > 0) {
$page_base = $base_tags->item(0)->getAttribute('href');
}
$linkNodes = $navlinksNode->getElementsByTagName('a');
foreach ($linkNodes as $linkNode) {
$this->_processLinks($linkNode->getAttribute('href'), '', $page_base, '<menu>');
}
$navlinksUlNode = $navlinksNode->getElementsByTagName('ul')->item(0);
foreach ($navlinksUlNode->childNodes as $primaryLinkLiNode) {
if (strtolower($primaryLinkLiNode->nodeName) != 'li') {
......@@ -374,7 +374,7 @@ class Unl_Migration_Tool
$primaryLinkNode = $primaryLinkLiNode->getElementsByTagName('a')->item(0);
$menuItem = array('text' => trim($primaryLinkNode->textContent),
'href' => $this->_makeLinkAbsolute($primaryLinkNode->getAttribute('href'), ''));
$childLinksUlNode = $primaryLinkLiNode->getElementsByTagName('ul')->item(0);
if (!$childLinksUlNode) {
$this->_menu[] = $menuItem;
......@@ -396,7 +396,7 @@ class Unl_Migration_Tool
$menuItem['children'] = $childMenu;
$this->_menu[] = $menuItem;
}
if (count($this->_menu) == 0) {
$this->_log('Could not find the navigation menu for your site!', WATCHDOG_ERROR);
}
......@@ -413,7 +413,7 @@ class Unl_Migration_Tool
menu_link_delete($menu_link['mlid']);
}
}
// Now recursively create each menu.
$primaryWeights = 1;
foreach ($this->_menu as $primaryMenu) {
......@@ -431,7 +431,7 @@ class Unl_Migration_Tool
$path = '';
}
$path = trim($path, '/');
if ($fragmentPos = strrpos($path, '#') !== FALSE) {
$item['options']['fragment'] = substr($path, $fragmentPos + 1);
$path = substr($path, 0, $fragmentPos);
......@@ -442,11 +442,11 @@ class Unl_Migration_Tool
$nodeId = array_search(strtolower($path), array_map('strtolower', $this->_nodeMap), TRUE);
if ($nodeId) {
$item['link_path'] = 'node/' . $nodeId;
}
}
} else {
$item['link_path'] = $href;
}
if ($item['link_path']) {
menu_link_save($item);
$this->_log('Created menu item "' . $item['link_title'] . '" linked to ' . $item['link_path'] . '.');
......@@ -454,11 +454,11 @@ class Unl_Migration_Tool
$this->_log('Could not find a node to link to the ' . $item['link_title'] . ' menu item.', WATCHDOG_ERROR);
continue;
}
if (!array_key_exists('children', $primaryMenu)) {
continue;
}
$plid = $item['mlid'];
$parentTitle = $item['link_title'];
$childWeights = 1;
......@@ -477,7 +477,7 @@ class Unl_Migration_Tool
$path = '';
}
$path = trim($path, '/');
if (($fragmentPos = strrpos($path, '#')) !== FALSE) {
$item['options']['fragment'] = substr($path, $fragmentPos + 1);
$path = substr($path, 0, $fragmentPos);
......@@ -492,7 +492,7 @@ class Unl_Migration_Tool
} else {
$item['link_path'] = $href;
}
if ($item['link_path']) {
menu_link_save($item);
$this->_log('Created menu item "' . $parentTitle . ' / ' . $item['link_title'] . '" linked to ' . $item['link_path'] . '.');
......@@ -501,28 +501,28 @@ class Unl_Migration_Tool
}
}
}
// Now set up the site hierarchy
$pageParentLinks = $this->_pageParentLinks;
foreach ($this->_pageParentLinks as $path => $parentLink) {
$this->_createParentLink($path, $parentLink);
}
}
private function _createParentLink($childPath, $parentPath) {
// If the child is the site root, just return the root mlid.
if (!$childPath) {
return 0;
}
// If the child link already exists, just return its mlid.
$childLink = menu_link_get_preferred(drupal_get_normal_path(rtrim($childPath, '/')));
if ($childLink && $childLink['link_path'] != 'node/%') {
return $childLink['mlid'];
}
// Find the parent link, if it doesn't exist, recursively create it.
$parentNodePath = drupal_get_normal_path(rtrim($parentPath, '/'));
$parentLink = menu_link_get_preferred($parentNodePath);
......@@ -535,7 +535,7 @@ class Unl_Migration_Tool
} else {
$parentLinkId = $this->_createParentLink($parentPath, $this->_pageParentLinks[$parentPath]);
}
// Create the menu item.
$item = array(
'menu_name' => 'main-menu',
......@@ -546,26 +546,26 @@ class Unl_Migration_Tool
'hidden' => 1,
);
menu_link_save($item);
// Return its mlid.
return $item['mlid'];
}
private function _process_blocks() {
$content = $this->_getUrl($this->_baseUrl);
$html = $content['content'];
$this->_blocks['related_links'] = $this->_get_instance_editable_content($html, 'leftcollinks');
$this->_blocks['contact_info'] = $this->_get_instance_editable_content($html, 'contactinfo');
$this->_blocks['optional_footer'] = $this->_get_instance_editable_content($html, 'optionalfooter');
$this->_blocks['footer_content'] = $this->_get_instance_editable_content($html, 'footercontent');
// Filter out the existing headers.
$this->_blocks['related_links'] = preg_replace('/\s*<h3>\s*Related Links\s*<\/h3>\s*/', '', $this->_blocks['related_links']);
$this->_blocks['contact_info'] = preg_replace('/\s*<h3>\sContacting Us*\s*<\/h3>\s*/', '', $this->_blocks['contact_info']);
$this->_blocks['contact_info'] = preg_replace('/\s*<h3>\s*Contact Us\s*<\/h3>\s*/', '', $this->_blocks['contact_info']);
}
private function _create_blocks() {
db_update('block_custom')
->fields(array(
......@@ -592,25 +592,25 @@ class Unl_Migration_Tool
->condition('bid', 104)
->execute();
}
private function _process_breadcrumbs() {
$content = $this->_getUrl($this->_baseUrl);
$html = $content['content'];
$dom = new DOMDocument();
$dom->loadHTML($html);
$breadcrumbs_node = $dom->getElementById('breadcrumbs');
if (!$breadcrumbs_node) {
return;
}
$link_nodes = $breadcrumbs_node->getElementsByTagName('a');
$list_nodes = $breadcrumbs_node->getElementsByTagName('li');
$unlinked_node = FALSE;
if ($list_nodes->length > $link_nodes->length) {
$unlinked_node = TRUE;
}
// Scan each of the breadcrumb links, skipping the first and the last (but only if there's an un-linked "true" last breadcrumb)
for ($i = 1; $i < $link_nodes->length - ($unlinked_node ? 1 : 0); $i++) {
$link_node = $link_nodes->item($i);
......@@ -620,26 +620,26 @@ class Unl_Migration_Tool
);
}
}
private function _create_breadcrumbs() {
$current_settings = variable_get