diff options
author | Tom Needham <needham.thomas@gmail.com> | 2012-03-10 15:52:38 +0000 |
---|---|---|
committer | Tom Needham <needham.thomas@gmail.com> | 2012-03-10 15:52:38 +0000 |
commit | 3ca76d24a9b27101661c454be84c7126315315d6 (patch) | |
tree | b9c3b505694a5a73bc89357b95e00eb6515691be | |
parent | c3dfcc5b21620476b6e5bf356b42aee9f0da5874 (diff) | |
download | nextcloud-server-3ca76d24a9b27101661c454be84c7126315315d6.tar.gz nextcloud-server-3ca76d24a9b27101661c454be84c7126315315d6.zip |
Add OC_Migrate::copyRows() method
-rw-r--r-- | apps/bookmarks/lib/migrate.php | 89 | ||||
-rw-r--r-- | lib/migrate.php | 59 |
2 files changed, 90 insertions, 58 deletions
diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index f50a8c46334..7d8ad8bfc53 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -2,73 +2,48 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ // Create the xml for the user supplied - function export($uid){ + function export( $uid ){ - $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()) { - - // Get the tags - $query = OC_DB::prepare("SELECT * FROM *PREFIX*bookmarks_tags WHERE *PREFIX*bookmarks_tags.bookmark_id = ?"); - $tagsdata =& $query->execute(array($row['id'])); + $options = array( + 'table'=>'bookmarks', + 'matchcol'=>'user_id', + 'matchval'=>$uid, + 'idcol'=>'id' + ); + $ids = OC_Migrate::copyRows( $options ); - $tags = array(); - // Foreach tag - while ($row = $tagsdata->fetchRow()) { - $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 - ); - - } + $options = array( + 'table'=>'bookmarks_tags', + 'matchcol'=>'id', + 'matchval'=>$ids + ); - return array('bookmarks' => $bookmarks); + // Export tags + OC_Migrate::copyRows( $options ); } // Import function for bookmarks - function import($data,$uid){ + function import( $data, $uid ){ + + // new id mapping + $newids = array(); + + // Import bookmarks + foreach($data['bookmarks'] as $bookmark){ + $bookmark['user_id'] = $uid; + // import to the db now + $newids[$bookmark['id']] = OC_DB::insertid(); + } - // 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; + // Import tags + foreach($data['bookmarks_tags'] as $tag){ + // Map the new ids + $tag['id'] = $newids[$tag['id']]; + // Import to the db now using OC_DB } - // Finished import } } -new OC_Migrate_Provider_Bookmarks('bookmarks');
\ No newline at end of file +new OC_Migrate_Provider_Bookmarks( 'bookmarks' );
\ No newline at end of file diff --git a/lib/migrate.php b/lib/migrate.php index da7a5ea34ff..b09626d11be 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -134,7 +134,7 @@ class OC_Migrate{ // @breif prepares the db // @param $query the sql query to prepare - public static function prepareDB( $query ){ + public static function prepare( $query ){ // Optimize the query $query = self::processQuery( $query ); @@ -174,12 +174,69 @@ class OC_Migrate{ } + // @brief copys rows to migration.db from the main database + // @param $options array of options. + // @return bool + public static function copyRows( $options ){ + if( !array_key_exists( 'table', $options ) ){ + return false; + } + + // Need to include 'where' in the query? + if( array_key_exists( 'matchval', $options ) && array_key_exists( 'matchcol', $options ) ){ + foreach( $options['matchval'] as $matchval ){ + // Run the query for this match value (where x = y value) + $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] . " WHERE " . $options['matchcol'] . " LIKE ?" ); + $results = $query->execute( array( $matchval ) ); + self::insertData( $results, $options ); + + } + + } else { + // Just get everything + $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] ); + $results = $query->execute(); + self::insertData( $results, $options ); + + } + + return true; + + } + + // @breif saves a sql data set into migration.db + // @param $data a sql data set returned from self::prepare()->query() + // @param $options array of copyRows options + // @return void + private static function insertData( $data, $options ){ + while( $data = $result->fetchRow() ){ + // Now save all this to the migration.db + foreach($row as $field=>$value){ + $fields[] = $field; + $values[] = $value; + } + + // Generate some sql + $sql = "INSERT INTO `*PREFIX*" . $options['table'] . '` ( `'; + $fieldssql = implode( '`, `', $fields ); + $sql .= $fieldssql . "` ) VALUES( "; + $valuessql = substr( str_repeat( '?, ', count( $fields ) ),0,-1 ); + $sql .= $valuessql . " )"; + // Make the query + $query = self::prepare( $sql ); + $query->execute( $values ); + } + } + // @breif creates the tables in migration.db from an apps database.xml // @param $appid string id of the app // @return bool whether the operation was successful private static function createAppTables( $appid ){ $file = OC::$SERVERROOT.'/apps/'.$appid.'appinfo/database.xml'; if(file_exists( $file )){ + + self::connectScheme(); + // There is a database.xml file $content = file_get_contents( $file ); |