migrate.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. class OC_Migration_Provider_Contacts extends OC_Migration_Provider{
  3. // Create the xml for the user supplied
  4. function export( ){
  5. $options = array(
  6. 'table'=>'contacts_addressbooks',
  7. 'matchcol'=>'userid',
  8. 'matchval'=>$this->uid,
  9. 'idcol'=>'id'
  10. );
  11. $ids = $this->content->copyRows( $options );
  12. $options = array(
  13. 'table'=>'contacts_cards',
  14. 'matchcol'=>'addressbookid',
  15. 'matchval'=>$ids
  16. );
  17. // Export tags
  18. $ids2 = $this->content->copyRows( $options );
  19. // If both returned some ids then they worked
  20. if( is_array( $ids ) && is_array( $ids2 ) )
  21. {
  22. return true;
  23. } else {
  24. return false;
  25. }
  26. }
  27. // Import function for bookmarks
  28. function import( ){
  29. switch( $this->appinfo->version ){
  30. default:
  31. // All versions of the app have had the same db structure, so all can use the same import function
  32. $query = $this->content->prepare( "SELECT * FROM contacts_addressbooks WHERE userid LIKE ?" );
  33. $results = $query->execute( array( $this->olduid ) );
  34. $idmap = array();
  35. while( $row = $results->fetchRow() ){
  36. // Import each bookmark, saving its id into the map
  37. $query = OCP\DB::prepare( "INSERT INTO `*PREFIX*contacts_addressbooks` (`userid`, `displayname`, `uri`, `description`, `ctag`) VALUES (?, ?, ?, ?, ?)" );
  38. $query->execute( array( $this->uid, $row['displayname'], $row['uri'], $row['description'], $row['ctag'] ) );
  39. // Map the id
  40. $idmap[$row['id']] = OCP\DB::insertid();
  41. }
  42. // Now tags
  43. foreach($idmap as $oldid => $newid){
  44. $query = $this->content->prepare( "SELECT * FROM contacts_cards WHERE addressbookid LIKE ?" );
  45. $results = $query->execute( array( $oldid ) );
  46. while( $row = $results->fetchRow() ){
  47. // Import the tags for this bookmark, using the new bookmark id
  48. $query = OCP\DB::prepare( "INSERT INTO `*PREFIX*contacts_cards` (`addressbookid`, `fullname`, `carddata`, `uri`, `lastmodified`) VALUES (?, ?, ?, ?, ?)" );
  49. $query->execute( array( $newid, $row['fullname'], $row['carddata'], $row['uri'], $row['lastmodified'] ) );
  50. }
  51. }
  52. // All done!
  53. break;
  54. }
  55. return true;
  56. }
  57. }
  58. // Load the provider
  59. new OC_Migration_Provider_Contacts( 'contacts' );