Skip to content

Instantly share code, notes, and snippets.

@OnkelTem
Last active August 29, 2015 14:05
Show Gist options
  • Save OnkelTem/3f63e3295252cc81382a to your computer and use it in GitHub Desktop.
Save OnkelTem/3f63e3295252cc81382a to your computer and use it in GitHub Desktop.
Migrate Site
<?php
function migrate_site_migrate_api() {
$api = array(
'api' => 2,
'groups' => array(
'site' => array(
'title' => t('Site'),
)
),
'migrations' => array(
'SiteFiles' => array(
'class_name' => 'SiteFilesMigration',
'group_name' => 'site'
),
'SitePages' => array(
'class_name' => 'SitePagesMigration',
'group_name' => 'site',
//'dependencies' => array(
// 'SiteFiles',
//)
),
),
);
return $api;
}
<?php
// Include SourceParser.
require_once drupal_get_path('module', 'migrate_site') . '/source_parser.inc';
abstract class SiteMigration extends Migration {
public function __construct($arguments) {
parent::__construct($arguments);
$this->team = array(
new MigrateTeamMember('Artiom Neganov', 'aneganov@gmail.com', t('Implementor'))
);
$this->issuePattern = 'http://drupal.org/node/:id:';
}
}
class SitePagesMigration extends SiteMigration {
public $base_dir;
public $base_url;
public function __construct($arguments) {
parent::__construct($arguments);
$this->description = t('Migrate site page into Drupal node');
// A map of source HTML filename -> destination node id.
$this->map = new MigrateSQLMap($this->machineName,
array(
'sourceid' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
)
),
MigrateDestinationNode::getKeySchema()
);
$directories = array(
'/var/www/sites/brassens',
);
$this->base_dir = '/var/www/sites/brassens/www/123/subdir';
$this->base_url = 'http://brassens.ru/123/subdir';
$regex = '/(.*\.htm$|.*\.html$)/i';
$list = new MigrateListFiles(array($this->base_dir), $this->base_dir, $regex);
$item = new MigrateItemFile($this->base_dir);
$fields = array(
'title' => t('Title'),
'body' => t('Body'),
'links' => t('Links'),
'files' => t('Files'),
'format' => t('Text Format'),
);
$this->source = new MigrateSourceList($list, $item, $fields);
$this->destination =
new MigrateDestinationNode('migrate_site_page');
// Map the fields, pretty straightforward in this case.
$this->addFieldMapping('title', 'title');
$this->addFieldMapping('body', 'body');
$this->addFieldMapping('body:format', 'format');
$this->addFieldMapping('field_migrate_site_page_refs', 'links');
$this->addFieldMapping('field_migrate_site_page_files', 'files');
$this->addFieldMapping('field_migrate_site_page_files:file_class')
->defaultValue('MigrateFileFid');
}
protected function createStub($migration, array $source_id) {
$node = new stdClass();
$node->title = t('Stub for @id', array('@id' => $source_id[0]));
$node->language = LANGUAGE_NONE;
$node->body[LANGUAGE_NONE][0]['value'] = t('Stub body');
$node->type = $this->destination->getBundle();
$node->uid = 1;
$node->status = 0;
node_save($node);
if (isset($node->nid)) {
return array($node->nid);
}
else {
return FALSE;
}
}
/**
* Prepare a row.
*/
public function prepareRow($row) {
// Create a new SourceParser to handle HTML content.
$source_parser = new SourceParser(substr($row->sourceid, 1), $row->filedata, $this->base_url, drupal_dirname($row->sourceid));
$source_ids = $source_parser->getLinks();
if (count($source_ids)) {
$row->links = $this->handleSourceMigration('SitePages', $source_ids);
if (!is_array($row->links)) {
$row->links = array($row->links);
}
}
$files = $source_parser->getFiles();
$row->files = $this->handleSourceMigration('SiteFiles', $files);
if (!is_array($row->files)) {
$row->files = array($row->files);
}
$row->body = $source_parser->getBody();
$row->format = 'raw';
// Replacing links
if (count($row->links)) {
foreach($row->links as $id => $nid) {
$row->body = str_replace('[#%' .$id . ']', '[#' .$nid . ']', $row->body);
}
}
// Replacing files
if (count($row->files)) {
foreach($row->files as $id => $fid) {
$row->body = str_replace('{#%' .$id . '}', '{#' .$fid . '}', $row->body);
}
}
// The title is the filename.
$row->title = $row->sourceid;
}
}
class SiteFilesMigration extends SiteMigration {
public $base_dir;
public $base_url;
public function __construct($arguments) {
parent::__construct($arguments);
$this->description = t('Migrate sites files into Drupal.');
// A map of source HTML filename -> destination node id.
$this->map = new MigrateSQLMap($this->machineName,
array(
'sourceid' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
)
),
MigrateDestinationNode::getKeySchema()
);
$directories = array(
'/var/www/sites/brassens',
);
$this->base_dir = '/var/www/sites/brassens/www/123/subdir';
$this->base_url = 'http://brassens.ru/123/subdir';
$regex = '/asdasd/';
$list = new MigrateListFiles(array($this->base_dir), $this->base_dir, $regex);
$item = new MigrateItemFile($this->base_dir);
$fields = array(
'sourceid' => t('File name with path'),
'file_url' => t('Path to the file'),
);
$this->source = new MigrateSourceList($list, $item, $fields);
$this->destination = new MigrateDestinationFile('file', 'MigrateFileLink');
$this->addFieldMapping('value', 'file_url');
}
public function prepareRow($row) {
if (parent::prepareRow($row) === FALSE) {
return FALSE;
}
$row->file_url = $this->base_url . $row->sourceid;
// Remove the leading forward slash.
//$row->destination_file = substr($row->sourceid, 1);
}
protected function createStub($migration, array $source_id) {
$file = FALSE;
$url = $this->base_url . $source_id[0];
$scheme = file_uri_scheme($url);
$class = file_stream_wrapper_get_class($scheme);
if (!class_exists($class)) {
return FALSE;
}
$file = remote_stream_wrapper_file_create_by_uri($url);
if (!$file) {
return FALSE;
}
$file = file_save($file);
if (is_object($file) && isset($file->fid)) {
return array($file->fid);
}
else {
return FALSE;
}
}
}
class MigrateFileLink implements MigrateFileInterface {
/**
* We have no custom fields
*/
static public function fields() {
return array();
}
/**
* Implementation of MigrateFileInterface::processFiles().
*
* @param $value
* The URI or local filespec of a file to be imported.
* @param $owner
* User ID (uid) to be the owner of the file.
* @return object
* The file entity being created or referenced.
*/
public function processFile($url, $owner) {
$file = FALSE;
$scheme = file_uri_scheme($url);
$class = file_stream_wrapper_get_class($scheme);
if (!class_exists($class)) {
return FALSE;
}
$file = remote_stream_wrapper_file_create_by_uri($url);
if (!$file) {
return FALSE;
}
$file = file_save($file);
if (is_object($file)) {
return $file;
}
else {
return FALSE;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment