Return JSON for import and export methods of OC_Migrate

This commit is contained in:
Tom Needham 2012-04-07 21:55:16 +00:00
parent 9edf45a324
commit c0869887cf
4 changed files with 42 additions and 42 deletions

View File

@ -3,7 +3,6 @@ class OC_Migration_Provider_Bookmarks extends OC_Migration_Provider{
// Create the xml for the user supplied // Create the xml for the user supplied
function export( ){ function export( ){
OC_Log::write('migration','starting export for bookmarks',OC_Log::INFO);
$options = array( $options = array(
'table'=>'bookmarks', 'table'=>'bookmarks',
'matchcol'=>'user_id', 'matchcol'=>'user_id',

View File

@ -42,15 +42,15 @@ if (isset($_POST['user_import'])) {
$tmpl->assign('error',$error); $tmpl->assign('error',$error);
return $tmpl->fetchPage(); return $tmpl->fetchPage();
} }
$response = json_decode( OC_Migrate::import( $to, 'user' ) );
if( !$appsstatus = OC_Migrate::import( $to, 'user' ) ){ if( !$response->success ){
$error = array('error'=>'There was an error while importing the user!','hint'=>'Please check the logs for a more detailed explaination'); $error = array('error'=>'There was an error while importing the user!','hint'=>'Please check the logs for a more detailed explaination');
$tmpl = new OC_Template('user_migrate', 'admin'); $tmpl = new OC_Template('user_migrate', 'admin');
$tmpl->assign('error',$error); $tmpl->assign('error',$error);
return $tmpl->fetchPage(); return $tmpl->fetchPage();
} else { } else {
// Check import status // Check import status
foreach( $appsstatus as $app => $status ){ foreach( $response->data as $app => $status ){
if( $status != 'true' ){ if( $status != 'true' ){
// It failed for some reason // It failed for some reason
if( $status == 'notsupported' ){ if( $status == 'notsupported' ){

View File

@ -28,28 +28,29 @@ OC_JSON::checkLoggedIn();
OC_Util::checkAppEnabled('user_migrate'); OC_Util::checkAppEnabled('user_migrate');
// Which operation // Which operation
if( $_GET['operation']=='create' ){ if( $_GET['operation']=='create' ){
$uid = !empty( $_POST['uid'] ) ? $_POST['uid'] : OC_User::getUser(); $uid = !empty( $_POST['uid'] ) ? $_POST['uid'] : OC_User::getUser();
if( $uid != OC_User::getUser() ){ if( $uid != OC_User::getUser() ){
// Needs to be admin to export someone elses account // Needs to be admin to export someone elses account
OC_JSON::error(); OC_JSON::error();
die();
}
// Create the export zip
$response = json_decode( OC_Migrate::export( $uid ) );
if( !$response->success ){
// Error
OC_JSON::error();
die();
} else {
// Save path in session
$_SESSION['ocuserexportpath'] = $response->data;
}
OC_JSON::success();
die(); die();
}
// Create the export zip
if( !$path = OC_Migrate::export( $uid ) ){
// Error
OC_JSON::error();
die();
} else {
// Save path in session
$_SESSION['ocuserexportpath'] = $path;
}
OC_JSON::success();
die();
} else if( $_GET['operation']=='download' ){ } else if( $_GET['operation']=='download' ){
// Download the export // Download the export
$path = isset( $_SESSION['ocuserexportpath'] ) ? $_SESSION['ocuserexportpath'] : false; $path = isset( $_SESSION['ocuserexportpath'] ) ? $_SESSION['ocuserexportpath'] : false;
if( !$path ){ if( !$path ){
die(); OC_JSON::error();
} }
header("Content-Type: application/zip"); header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=" . basename($path)); header("Content-Disposition: attachment; filename=" . basename($path));

View File

@ -84,7 +84,7 @@ class OC_Migrate{
$types = array( 'user', 'instance', 'system', 'userfiles' ); $types = array( 'user', 'instance', 'system', 'userfiles' );
if( !in_array( $type, $types ) ){ if( !in_array( $type, $types ) ){
OC_Log::write( 'migration', 'Invalid export type', OC_Log::ERROR ); OC_Log::write( 'migration', 'Invalid export type', OC_Log::ERROR );
return false; return json_encode( array( array( 'success' => false ) ) );
} }
self::$exporttype = $type; self::$exporttype = $type;
// Userid? // Userid?
@ -93,7 +93,7 @@ class OC_Migrate{
if( !is_null($uid) ){ if( !is_null($uid) ){
if( !OC_User_Database::userExists( $uid ) ){ if( !OC_User_Database::userExists( $uid ) ){
OC_Log::write('migration', 'User: '.$uid.' is not in the database and so cannot be exported.', OC_Log::ERROR); OC_Log::write('migration', 'User: '.$uid.' is not in the database and so cannot be exported.', OC_Log::ERROR);
return false; return json_encode( array( 'success' => false ) );
} }
self::$uid = $uid; self::$uid = $uid;
} else { } else {
@ -114,7 +114,7 @@ class OC_Migrate{
// Validate custom path // Validate custom path
if( !file_exists( $path ) || !is_writeable( $path ) ){ if( !file_exists( $path ) || !is_writeable( $path ) ){
OC_Log::write( 'migration', 'Path supplied is invalid.', OC_Log::ERROR ); OC_Log::write( 'migration', 'Path supplied is invalid.', OC_Log::ERROR );
return false; return json_encode( array( 'success' => false ) );
} }
self::$zippath = $path . $zipname; self::$zippath = $path . $zipname;
} else { } else {
@ -124,7 +124,7 @@ class OC_Migrate{
} }
// Create the zip object // Create the zip object
if( !self::createZip() ){ if( !self::createZip() ){
return false; return json_encode( array( 'success' => false ) );
} }
// Do the export // Do the export
self::findProviders(); self::findProviders();
@ -134,7 +134,7 @@ class OC_Migrate{
// Connect to the db // Connect to the db
self::$dbpath = $datadir . '/' . self::$uid . '/migration.db'; self::$dbpath = $datadir . '/' . self::$uid . '/migration.db';
if( !self::connectDB() ){ if( !self::connectDB() ){
return false; return json_encode( array( 'success' => false ) );
} }
self::$content = new OC_Migration_Content( self::$zip, self::$MDB2 ); self::$content = new OC_Migration_Content( self::$zip, self::$MDB2 );
// Export the app info // Export the app info
@ -178,14 +178,14 @@ class OC_Migrate{
break; break;
} }
if( !$info = self::getExportInfo( $exportdata ) ){ if( !$info = self::getExportInfo( $exportdata ) ){
return false; return json_encode( array( 'success' => false ) );
} }
// Add the export info json to the export zip // Add the export info json to the export zip
self::$content->addFromString( $info, 'export_info.json' ); self::$content->addFromString( $info, 'export_info.json' );
if( !self::$content->finish() ){ if( !self::$content->finish() ){
return false; return json_encode( array( 'success' => false ) );
} }
return self::$zippath; return json_encode( array( 'success' => true, 'data' => self::$zippath ) );
} }
/** /**
@ -199,19 +199,19 @@ class OC_Migrate{
$datadir = OC_Config::getValue( 'datadirectory' ); $datadir = OC_Config::getValue( 'datadirectory' );
// Extract the zip // Extract the zip
if( !$extractpath = self::extractZip( $path ) ){ if( !$extractpath = self::extractZip( $path ) ){
return false; return json_encode( array( 'success' => false ) );
} }
// Get export_info.json // Get export_info.json
$scan = scandir( $extractpath ); $scan = scandir( $extractpath );
// Check for export_info.json // Check for export_info.json
if( !in_array( 'export_info.json', $scan ) ){ if( !in_array( 'export_info.json', $scan ) ){
OC_Log::write( 'migration', 'Invalid import file, export_info.json note found', OC_Log::ERROR ); OC_Log::write( 'migration', 'Invalid import file, export_info.json note found', OC_Log::ERROR );
return false; return json_encode( array( 'success' => false ) );
} }
$json = json_decode( file_get_contents( $extractpath . 'export_info.json' ) ); $json = json_decode( file_get_contents( $extractpath . 'export_info.json' ) );
if( $json->exporttype != $type ){ if( $json->exporttype != $type ){
OC_Log::write( 'migration', 'Invalid import file', OC_Log::ERROR ); OC_Log::write( 'migration', 'Invalid import file', OC_Log::ERROR );
return false; return json_encode( array( 'success' => false ) );
} }
self::$exporttype = $type; self::$exporttype = $type;
@ -230,31 +230,31 @@ class OC_Migrate{
// Check user availability // Check user availability
if( OC_User::userExists( self::$uid ) ){ if( OC_User::userExists( self::$uid ) ){
OC_Log::write( 'migration', 'User already exists', OC_Log::ERROR ); OC_Log::write( 'migration', 'User already exists', OC_Log::ERROR );
return false; return json_encode( array( 'success' => false ) );
} }
// Create the user // Create the user
if( !self::createUser( self::$uid, $json->hash ) ){ if( !self::createUser( self::$uid, $json->hash ) ){
return false; return json_encode( array( 'success' => false ) );
} }
// Make the new users data dir // Make the new users data dir
$path = $datadir . '/' . self::$uid . '/files/'; $path = $datadir . '/' . self::$uid . '/files/';
if( !mkdir( $path, 0755, true ) ){ if( !mkdir( $path, 0755, true ) ){
OC_Log::write( 'migration', 'Failed to create users data dir: '.$path, OC_Log::ERROR ); OC_Log::write( 'migration', 'Failed to create users data dir: '.$path, OC_Log::ERROR );
return false; return json_encode( array( 'success' => false ) );
} }
// Copy data // Copy data
if( !self::copy_r( $extractpath . $json->exporteduser . '/files', $datadir . '/' . self::$uid . '/files' ) ){ if( !self::copy_r( $extractpath . $json->exporteduser . '/files', $datadir . '/' . self::$uid . '/files' ) ){
return false; return json_encode( array( 'success' => false ) );
} }
// Import user app data // Import user app data
if( !$appsimported = self::importAppData( $extractpath . $json->exporteduser . '/migration.db', $json, self::$uid ) ){ if( !$appsimported = self::importAppData( $extractpath . $json->exporteduser . '/migration.db', $json, self::$uid ) ){
return false; return json_encode( array( 'success' => false ) );
} }
// All done! // All done!
if( !self::unlink_r( $extractpath ) ){ if( !self::unlink_r( $extractpath ) ){
OC_Log::write( 'migration', 'Failed to delete the extracted zip', OC_Log::ERROR ); OC_Log::write( 'migration', 'Failed to delete the extracted zip', OC_Log::ERROR );
} }
return $appsimported; return json_encode( array( 'success' => true, 'data' => $appsimported ) );
break; break;
case 'instance': case 'instance':
/* /*
@ -266,21 +266,21 @@ class OC_Migrate{
OC_Log::write( 'migration', "Deleting current data dir", OC_Log::INFO ); OC_Log::write( 'migration', "Deleting current data dir", OC_Log::INFO );
if( !self::unlink_r( $datadir, false ) ){ if( !self::unlink_r( $datadir, false ) ){
OC_Log::write( 'migration', 'Failed to delete the current data dir', OC_Log::ERROR ); OC_Log::write( 'migration', 'Failed to delete the current data dir', OC_Log::ERROR );
return false; return json_encode( array( 'success' => false ) );
} }
// Copy over data // Copy over data
if( !self::copy_r( $extractpath . 'userdata', $datadir ) ){ if( !self::copy_r( $extractpath . 'userdata', $datadir ) ){
OC_Log::write( 'migration', 'Failed to copy over data directory', OC_Log::ERROR ); OC_Log::write( 'migration', 'Failed to copy over data directory', OC_Log::ERROR );
return false; return json_encode( array( 'success' => false ) );
} }
// Import the db // Import the db
if( !OC_DB::replaceDB( $extractpath . 'dbexport.xml' ) ){ if( !OC_DB::replaceDB( $extractpath . 'dbexport.xml' ) ){
return false; return json_encode( array( 'success' => false ) );
} }
// Done // Done
return true; return json_encode( 'success' => true );
*/ */
break; break;
} }