Find migrate.php even for disabled apps. Improve ui for user and admin migrations

This commit is contained in:
Tom Needham 2012-03-13 16:21:17 +00:00
parent d108bdc7c7
commit 4d5646a59f
6 changed files with 100 additions and 79 deletions

View File

@ -74,15 +74,16 @@ if (isset($_POST['admin_export'])) {
}
if (isset($_POST['user_files'])) {
// needs to handle data outside of the default data dir.
// adding user files
$zip->addFile($root . '/data/.htaccess', "data/.htaccess");
$zip->addFile($root . '/data/index.html', "data/index.html");
foreach (OC_User::getUsers() as $i) {
OC_Log::write('admin_export',"Adding owncloud user files of $i to export",OC_Log::INFO);
zipAddDir($root . "data/" . $i, $zip, true, "/data/");
}
// needs to handle data outside of the default data dir.
// adding user files
$zip->addFile($root . '/data/.htaccess', "data/.htaccess");
$zip->addFile($root . '/data/index.html', "data/index.html");
foreach (OC_User::getUsers() as $i) {
OC_Log::write('admin_export',"Adding owncloud user files of $i to export",OC_Log::INFO);
zipAddDir($root . "data/" . $i, $zip, true, "/data/");
}
}
$zip->close();
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=" . basename($filename));

View File

@ -18,8 +18,4 @@ OC_App::addNavigationEntry( array( 'id' => 'bookmarks_index', 'order' => 70, 'hr
OC_App::registerPersonal('bookmarks', 'settings');
OC_Util::addScript('bookmarks','bookmarksearch');
// Include the migration provider
require_once('apps/bookmarks/lib/migrate.php');
OC_Search::registerProvider('OC_Search_Provider_Bookmarks');

View File

@ -54,4 +54,5 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{
}
// Load the provider
new OC_Migrate_Provider_Bookmarks( 'bookmarks' );

View File

@ -3,6 +3,8 @@
/**
* ownCloud - user_migrate
*
* @author Thomas Schmidt
* @copyright 2011 Thomas Schmidt tom@opensuse.org
* @author Tom Needham
* @copyright 2012 Tom Needham tom@owncloud.com
*
@ -22,52 +24,67 @@
*/
OC_Util::checkAppEnabled('user_migrate');
define('DS', '/');
if (isset($_POST['user_migrate'])) {
// Looks like they want to migrate
$root = OC::$SERVERROOT . "/";
$user = OC_User::getUser();
if (isset($_POST['user_export'])) {
// Setup the export
$zip = new ZipArchive();
$tempdir = get_temp_dir();
$filename = $tempdir . "/" . $user . "_export_" . date("y-m-d_H-i-s") . ".zip";
OC_Log::write('user_migrate',"Creating user export file at: " . $filename,OC_Log::INFO);
$tmp = get_temp_dir();
$user = OC_User::getUser();
// Create owncoud dir
if( !file_exists( $tmp . '/owncloud' ) ){
if( !mkdir( $tmp . '/owncloud' ) ){
die('Failed to create the owncloud tmp directory');
}
}
// Create the export dir
$exportdir = $tmp . '/owncloud' . '/export_' . $user . '_' . date("y-m-d_H-i-s");
if( !file_exists( $exportdir ) ){
if( !mkdir( $exportdir ) ){
die('Failed to create the owncloud export directory');
}
}
$filename = $exportdir . '/owncloud_export_' . $user . '_' . date("y-m-d_H-i-s") . ".zip";
OC_Log::write('user_migrate',"Creating export file at: " . $filename,OC_Log::INFO);
if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
exit("Cannot open <$filename>\n");
}
// Migrate the app info
$info = OC_Migrate::export( $user );
$infofile = $exportdir . '/exportinfo.json';
if( !file_put_contents( $infofile, $info ) ){
die('Failed to save the export info');
}
$zip->addFile( $infofile, "exportinfo.json");
$zip->addFile(OC::$SERVERROOT . '/data/' . $user . '/migration.db', "migration.db");
// Does the user want to include their files?
if (isset($_POST['user_files'])) {
// needs to handle data outside of the default data dir.
// adding user files
OC_Log::write('user_migrate',"Adding owncloud user files of $user to export",OC_Log::INFO);
zipAddDir($root . "data/" . $user, $zip, true, "files/");
}
// Does the user want their app data?
if (isset($_POST['user_appdata'])) {
// adding owncloud system files
OC_Log::write('user_migrate',"Adding app data to user export file",OC_Log::INFO);
// Call to OC_Migrate for the xml file.
// Create migration.db
OC_Migrate::export(OC_User::getUser());
// Add export db to zip
$zip->addFile($root.'data/'.$user.'/migration.db', "migration.db");
}
// Add the data dir
zipAddDir(OC::$SERVERROOT . "/data/" . $user, $zip, true, "files/");
// Save the zip
$zip->close();
//header("Content-Type: application/zip");
//header("Content-Disposition: attachment; filename=" . basename($filename));
//header("Content-Length: " . filesize($filename));
//readfile($filename);
//unlink($filename);
} else {
// fill template
// Send the zip
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=" . basename($filename));
header("Content-Length: " . filesize($filename));
@ob_end_clean();
readfile($filename);
// Cleanup
unlink($filename);
unlink($infofile);
rmdir($exportdir);
} if( isset( $_POST['user_import'] ) ){
// TODO
}else {
// fill template
$tmpl = new OC_Template('user_migrate', 'settings');
return $tmpl->fetchPage();
}
function zipAddDir($dir, $zip, $recursive=true, $internalDir='') {
@ -89,6 +106,6 @@ function zipAddDir($dir, $zip, $recursive=true, $internalDir='') {
}
closedir($dirhandle);
} else {
OC_Log::write('admin_export',"Was not able to open directory: " . $dir,OC_Log::ERROR);
OC_Log::write('user_migrate',"Was not able to open directory: " . $dir,OC_Log::ERROR);
}
}

View File

@ -1,12 +1,17 @@
<form id="export" action="#" method="post">
<fieldset class="personalblock">
<legend><strong><?php echo $l->t('Export your user account');?></strong></legend>
<p><?php echo $l->t('This will create a compressed file that contains the data of owncloud account.
Please choose which components should be included:');?>
<p><?php echo $l->t('This will create a compressed file that contains your ownCloud account.');?>
</p>
<p><input type="checkbox" id="user_files" name="user_files" value="true"><label for="user_files"><?php echo $l->t('Files');?></label><br/>
<input type="checkbox" id="user_appdata" name="user_appdata" value="true"><label for="owncloud_system"><?php echo $l->t('User app data');?></label><br/>
</p>
<input type="submit" name="user_migrate" value="Export" />
<input type="submit" name="user_export" value="Export" />
</fieldset>
</form>
<form id="import" action="#" method="post" enctype="multipart/form-data">
<fieldset class="personalblock">
<legend><strong><?php echo $l->t('Import user account');?></strong></legend>
</p>
<p><input type="file" id="owncloud_import" name="owncloud_import"><label for="owncloud_import"><?php echo $l->t('ownCloud User Zip');?></label>
</p>
<input type="submit" name="user_import" value="<?php echo $l->t('Import'); ?>" />
</fieldset>
</form>

View File

@ -26,11 +26,16 @@
*/
class OC_Migrate{
// Holds the db object
static private $MDB2=false;
// Array of OC_Migration_Provider objects
static private $providers=array();
// Schema db object
static private $schema=false;
// User id of the user to import/export
static private $uid=false;
static private $database=false;
// Path to the sqlite db
static private $dbpath=false;
/**
* register a new migration provider
@ -64,9 +69,18 @@ class OC_Migrate{
$ok = true;
$return = array();
// Find the providers
$apps = OC_App::getAllApps();
foreach($apps as $app){
$path = OC::$SERVERROOT . '/apps/' . $app . '/lib/migrate.php';
if( file_exists( $path ) ){
include( $path );
}
}
// Foreach provider
foreach( self::$providers as $provider ){
$failed = false;
// Does this app use the database?
@ -110,7 +124,7 @@ class OC_Migrate{
/**
* @breif imports a new user
* @param $db string path to migration.db
* @param $migrateinfo string path to the migration info json file
* @param $migrateinfo array of migration ino
* @param $uid optional uid to use
* @return bool if the import succedded
*/
@ -135,27 +149,14 @@ class OC_Migrate{
exit();
}
// Load the json info
if( file_exists( $migrateinfo ) ){
} else {
OC_Log::write( 'migration', 'Migration information file not found at: '.$migrateinfo, OC_Log::FATAL );
if( !is_array( $migrateinfo ) ){
OC_Log::write('migration','$migrateinfo is not an array', OC_Log::FATAL);
return false;
exit();
}
// Process migration info
$info = file_get_contents( $migrateinfo );
$info = json_decode( $info );
// Set the user id
self::$uid = !$uid : $info['migrateinfo']['uid'] ? $uid;
// Create the user
if(!self::createUser($uid, $hash)){
return false;
exit();
}
self::$uid = $info['migrateinfo']['uid'];
$apps = $info['apps'];
@ -177,7 +178,7 @@ class OC_Migrate{
// @breif connects to migration.db, or creates if not found
// @param $db optional path to migration.db, defaults to user data dir
// @return bool whether the operation was successful
private static function connectDB( $db=null ){
private static function connectDB( $dbpath=null ){
OC_Log::write('migration','connecting to migration.db for user: '.self::$uid,OC_Log::INFO);
// Fail if no user is set
if(!self::$uid){
@ -188,10 +189,10 @@ class OC_Migrate{
if(!self::$MDB2){
require_once('MDB2.php');
self::$database = !is_null( $db ) ? $db : $datadir.'/'.self::$uid.'/migration.db';
$datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" );
self::$dbpath = $datadir.'/'.self::$uid.'/migration.db';//!is_null( $dbpath ) ? $dbpath : $datadir.'/'.self::$uid.'/migration.db';
// Prepare options array
$options = array(
'portability' => MDB2_PORTABILITY_ALL & (!MDB2_PORTABILITY_FIX_CASE),
@ -202,10 +203,10 @@ class OC_Migrate{
);
$dsn = array(
'phptype' => 'sqlite3',
'database' => self::$database,
'database' => self::$dbpath,
'mode' => '0644'
);
// Try to establish connection
self::$MDB2 = MDB2::factory( $dsn, $options );
// Die if we could not connect