diff options
-rw-r--r-- | apps/bookmarks/lib/migrate.php | 98 | ||||
-rw-r--r-- | apps/user_migrate/settings.php | 4 | ||||
-rw-r--r-- | lib/migrate.php | 157 | ||||
-rw-r--r-- | lib/migrate/provider.php | 9 |
4 files changed, 178 insertions, 90 deletions
diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index c655154d41b..f50a8c46334 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -4,69 +4,71 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ // Create the xml for the user supplied function export($uid){ - $doc = new DOMDocument(); - $doc->formatOutput = true; - $bookmarks = $doc->createElement('bookmarks'); - $bookmarks = $doc->appendChild($bookmarks); - + $bookmarks = array(); + $query = OC_DB::prepare("SELECT * FROM *PREFIX*bookmarks WHERE *PREFIX*bookmarks.user_id = ?"); $bookmarksdata =& $query->execute(array($uid)); - - // Foreach bookmark while ($row = $bookmarksdata->fetchRow()) { - $bookmark = $doc->createElement('bookmark'); - $bookmark = $bookmarks->appendChild($bookmark); - - $attr = $doc->createElement('title'); - $attr = $bookmark->appendChild($attr); - $value = $doc->createTextNode($row['title']); - $attr->appendChild($value); - - $attr = $doc->createElement('url'); - $attr = $bookmark->appendChild($attr); - $value = $doc->createTextNode($row['url']); - $attr->appendChild($value); - - $attr = $doc->createElement('added'); - $attr = $bookmark->appendChild($attr); - $value = $doc->createTextNode($row['added']); - $attr->appendChild($value); - - $attr = $doc->createElement('lastmodified'); - $attr = $bookmark->appendChild($attr); - $value = $doc->createTextNode($row['lastmodified']); - $attr->appendChild($value); - - $attr = $doc->createElement('public'); - $attr = $bookmark->appendChild($attr); - $value = $doc->createTextNode($row['public']); - $attr->appendChild($value); - - $attr = $doc->createElement('clickcount'); - $attr = $bookmark->appendChild($attr); - $value = $doc->createTextNode($row['clickcount']); - $attr->appendChild($value); - - $attr = $doc->createElement('tags'); - $tags = $bookmark->appendChild($attr); + // Get the tags $query = OC_DB::prepare("SELECT * FROM *PREFIX*bookmarks_tags WHERE *PREFIX*bookmarks_tags.bookmark_id = ?"); $tagsdata =& $query->execute(array($row['id'])); + $tags = array(); // Foreach tag while ($row = $tagsdata->fetchRow()) { - $attr = $doc->createElement('tag'); - $attr = $tags->appendChild($attr); - $value = $doc->createTextNode($row['tag']); - $attr->appendChild($value); - } + $tags[] = $row['tag']; + } + + $bookmarks[] = array( + 'url' => $row['url'], + 'title' => $row['title'], + 'public' => $row['public'], + 'added' => $row['added'], + 'lastmodified' => $row['lastmodified'], + 'clickcount' => $row['clickcount'], + 'tags' => $tags + ); + } - return $doc; + return array('bookmarks' => $bookmarks); } + // Import function for bookmarks + function import($data,$uid){ + + // Different import code for different versions of the app + switch($data['info']['version']){ + default: + // Foreach bookmark + foreach($data['data']['bookmarks'] as $bookmark){ + + $query = OC_DB::prepare( "INSERT INTO `*PREFIX*bookmarks` ( `url`, `title`, `user_id`, `public`, `added`, `lastmodified`, `clickcount` ) VALUES( ?, ?, ?, ?, ?, ?, ? )" ); + $result = $query->execute( array( + $bookmark['url'], + $bookmark['title'], + $uid, + $bookmark['public'], + $bookmark['added'], + $bookmark['lastmodified'], + $bookmark['clickcount'] + ) ); + // Now add the tags + $id = OC_DB::insertid(); + foreach($bookmark['tags'] as $tag){ + $query = OC_DB::prepare( "INSERT INTO `*PREFIX*bookmarks_tags` ( `id`, `tag` ) VALUES( ?, ? )" ); + $result = $query->execute( array( $id, $tag)); + } + + } + break; + } + // Finished import + } + } new OC_Migrate_Provider_Bookmarks('bookmarks');
\ No newline at end of file diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 7c00dace3c2..ed08abe79d7 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -49,11 +49,11 @@ if (isset($_POST['user_migrate'])) { // 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. - $appdatafile = $tempdir . "/appdata.xml"; + $appdatafile = $tempdir . "/userexport.json"; $appdata = OC_Migrate::export(OC_User::getUser()); file_put_contents($appdatafile, $appdata); - $zip->addFile($appdatafile, "appdata.xml"); + $zip->addFile($appdatafile, "userexport.json"); } diff --git a/lib/migrate.php b/lib/migrate.php index 5179e431996..f3dd3080d30 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -42,49 +42,134 @@ class OC_Migrate{ */ public static function export($uid){ - $doc = new DOMDocument(); - $doc->formatOutput = true; - - OC_Log::write('user_migrate','App data export started for user: '.$uid,OC_Log::INFO); - - foreach(self::$providers as $provider){ + // Only export database users, otherwise we get chaos + if(OC_User_Database::userExists($uid)){ + + $data = array(); + $data['userid'] = OC_User::getUser(); - OC_Log::write('user_migrate','Getting app data for app:'.$provider->appid,OC_Log::INFO); - $app = $doc->createElement('app'); - $app = $doc->appendChild($app); - $app->setAttribute('id',$provider->appid); - // Append app info - $appinfo = $doc->importNode( self::appInfoXML( $provider->appid )->documentElement, true ); - $app->appendChild( $appinfo ); + $query = OC_DB::prepare( "SELECT uid, password FROM *PREFIX*users WHERE uid LIKE ?" ); + $result = $query->execute( array( $uid)); + + $row = $result->fetchRow(); + if($row){ + $data['hash'] = $row['password']; + } else { + return false; + exit(); + } - $appdata = $doc->createElement('appdata'); - $appdata = $app->appendChild($appdata); - // Add the app data - $appdatanode = $doc->importNode( $provider->export($uid)->documentElement, true ); - $appdata->appendChild( $appdatanode ); - + foreach(self::$providers as $provider){ + + $data['apps'][$prodider->appid]['info'] = OC_App::getAppInfo($provider->appid); + $data['apps'][$provider->appid]['data'] = $provider->export($uid); + + } + + return self::indent(json_encode($data)); + + } else { + return false; } - - return $doc->saveXML(); + } /** - * generates the app info xml - * @param string appid - * @return string xml app info - */ - public static function appInfoXML($appid){ + * @breif imports a new user + * @param $data json data for the user + * @param $uid optional uid to use + * @return json reply + */ + public function import($data,$uid=null){ + + // Import the data + $data = json_decode($data); + if(is_null($data)){ + // TODO LOG + return false; + exit(); + } + + // Specified user or use original + $uid = !is_null($uid) ? $uid : $data['userid']; + + // Check if userid exists + if(OC_User::userExists($uid)){ + // TODO LOG + return false; + exit(); + } + + // Create the user + $query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" ); + $result = $query->execute( array( $uid, $data['hash'])); + if(!$result){ + // TODO LOG + return false; + exit(); + } + + foreach($data['app'] as $app){ + // Check if supports migration and is enabled + if(in_array($app, self::$providers)){ + if(OC_App::isEnabled($app)){ + $provider->import($data['app'][$app],$uid); + } + } + + } + + } + + private static function indent($json){ + + $result = ''; + $pos = 0; + $strLen = strlen($json); + $indentStr = ' '; + $newLine = "\n"; + $prevChar = ''; + $outOfQuotes = true; - $info = OC_App::getAppInfo($appid); + for ($i=0; $i<=$strLen; $i++) { - $doc = new DOMDocument(); - $appinfo = $doc->createElement('appinfo'); - $appinfo = $doc->appendChild($appinfo); - $version = $doc->createElement('version'); - $appinfo->appendChild($version); - $versionval = $doc->createTextNode($info['version']); - $version->appendChild($versionval); + // Grab the next character in the string. + $char = substr($json, $i, 1); - return $doc; - } + // Are we inside a quoted string? + if ($char == '"' && $prevChar != '\\') { + $outOfQuotes = !$outOfQuotes; + + // If this character is the end of an element, + // output a new line and indent the next line. + } else if(($char == '}' || $char == ']') && $outOfQuotes) { + $result .= $newLine; + $pos --; + for ($j=0; $j<$pos; $j++) { + $result .= $indentStr; + } + } + + // Add the character to the result string. + $result .= $char; + + // If the last character was the beginning of an element, + // output a new line and indent the next line. + if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { + $result .= $newLine; + if ($char == '{' || $char == '[') { + $pos ++; + } + + for ($j = 0; $j < $pos; $j++) { + $result .= $indentStr; + } + } + + $prevChar = $char; + } + + return $result; + } + } diff --git a/lib/migrate/provider.php b/lib/migrate/provider.php index 9dd1b2f3897..63b804b6202 100644 --- a/lib/migrate/provider.php +++ b/lib/migrate/provider.php @@ -13,14 +13,15 @@ abstract class OC_Migrate_Provider{ /** * exports data for apps * @param string $uid - * @return string xml data for that app + * @return array appdata to be exported */ abstract function export($uid); /** * imports data for the app - * @param string $query - * @return array An array of OC_Search_Result's + * @param $data array of data. eg: array('info'=> APPINFO, 'data'=>APPDATA ARRAY) + * @param $info array of info of the source install + * @return void */ - //abstract function import($data); + abstract function import($data,$uid); } |