Skip to content

Instantly share code, notes, and snippets.

@sirkitree
Created August 14, 2012 17:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sirkitree/82e48a3749d55d1dcaa6 to your computer and use it in GitHub Desktop.
Save sirkitree/82e48a3749d55d1dcaa6 to your computer and use it in GitHub Desktop.
lagra_migrate.drush.inc
<?php
/**
* @file
* lagra-migrate to import contents from the latingrammy db
*
*
* To add a new migration function:
* 1. Create an inc file in the lagra_migration folder, provide a function in the form of lagra_migrate_[type]() in the inc file
* 2. Add a case to the switch to identify the content type to import in #ADD below, include the inc file and call the function lagra_migrate_[type]() defined in your inc file
*
*
* To test:
* use drush lagra-migrate [type] or drush lmi [type], i.e. drush lmi event
*
*/
include_once 'lagra_migrate/settings.inc';
/**
* Implements hook_drush_command().
*/
function lagra_migrate_drush_command() {
$items = array();
$items['lagra-migrate'] = array(
'description' => "Import contents from the latingrammy db",
'arguments' => array(
'content-type' => 'The destination content type in drupal',
'arg1' => 'Any argument',
),
'examples' => array(
'drush lagra-migrate',
),
'aliases' => array('lmi'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
'drush dependencies' => array('sql', 'core'),
);
return $items;
}
/**
* Actual function run by the lagra-migrate command
*/
function drush_lagra_migrate($content_type = NULL, $arg1 = NULL) {
if(!empty($content_type)){
switch ($content_type){
// #ADD the case for the content type
case 'event':
include_once 'lagra_migrate/event.inc';
lagra_migrate_event();
break;
case 'genre':
include_once 'lagra_migrate/genre.inc';
lagra_migrate_genre();
break;
case 'award':
include_once 'lagra_migrate/award.inc';
lagra_migrate_award();
break;
case 'tag':
include_once 'lagra_migrate/tag.inc';
lagra_migrate_tag();
break;
case 'nominee':
include_once 'lagra_migrate/nominee.inc';
lagra_migrate_nominee();
break;
case 'photo':
include_once 'lagra_migrate/photo.inc';
lagra_migrate_photo();
break;
case 'podcast':
include_once 'lagra_migrate/podcast.inc';
lagra_migrate_podcast();
break;
case 'press_release':
include_once 'lagra_migrate/press_release.inc';
lagra_migrate_press_release();
break;
case 'page':
include_once 'lagra_migrate/page.inc';
lagra_migrate_page();
break;
case 'sponsor':
include_once 'lagra_migrate/sponsor.inc';
lagra_migrate_sponsor();
case 'video':
include_once 'lagra_migrate/video.inc';
switch($arg1) {
// Create Video nodes.
case 'import':
lagra_migrate_video();
break;
// Create ooyala_videos table and load video details from Ooyala API
// into it, for use when creating video nodes.
case 'generate-table':
lagra_migrate_video_generate_table();
break;
// Check which videos from the old site are NOT in Ooyala.
case 'check-files':
lagra_migrate_video_check_files();
break;
// Print a list of remote video URIs for use with wget.
case 'file-list':
lagra_migrate_video_generate_file_list();
break;
// Use the Ooyala API to push up locally-stored meta data to videos.
case 'update-ooyala':
lagra_migrate_video_update_ooyala();
break;
}
break;
case 'test':
drush_log(dt('Import command can run.'), 'success');
break;
}
}
drush_log(dt('Done.'), 'success');
}
/**
* Add the file_uri_to_object() function from media.module
*/
if (!function_exists('file_uri_to_object')) {
// @todo: get this committed http://drupal.org/node/685818
/**
* Returns a file object which can be passed to file_save().
*
* @param $uri
* A string containing the URI, path, or filename.
* @param $use_existing
* (Optional) If TRUE and there's an existing file in the {file_managed}
* table with the passed in URI, then that file object is returned.
* Otherwise, a new file object is returned.
* @return
* A file object, or FALSE on error.
*/
function file_uri_to_object($uri, $use_existing = FALSE) {
if ($use_existing) {
$query = db_select('file_managed', 'f')
->fields('f', array('fid'))
->condition('uri', $uri)
->execute()
->fetchCol();
if (!empty($query)) {
$file = file_load(array_shift($query));
}
}
if (!isset($file)) {
global $user;
$uri = file_stream_wrapper_uri_normalize($uri);
$wrapper = file_stream_wrapper_get_instance_by_uri($uri);
$file = new StdClass;
$file->uid = $user->uid;
$file->filename = basename($uri);
$file->uri = $uri;
$file->filemime = file_get_mimetype($uri);
// This is gagged because some uris will not support it.
$file->filesize = @filesize($uri);
$file->timestamp = REQUEST_TIME;
$file->status = FILE_STATUS_PERMANENT;
$file->is_new = TRUE;
}
return $file;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment