Blame view

sources/apps/contacts/appinfo/migrate.php 2.93 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  <?php
  class OC_Migration_Provider_Contacts extends OC_Migration_Provider{
  
  	// Create the xml for the user supplied
  	function export( ) {
  		$options = array(
  			'table'=>'contacts_addressbooks',
  			'matchcol'=>'userid',
  			'matchval'=>$this->uid,
  			'idcol'=>'id'
  		);
  		$ids = $this->content->copyRows( $options );
  
  		$options = array(
  			'table'=>'contacts_cards',
  			'matchcol'=>'addressbookid',
  			'matchval'=>$ids
  		);
  
  		// Export tags
  		$ids2 = $this->content->copyRows( $options );
  
  		// If both returned some ids then they worked
  		if(is_array($ids) && is_array($ids2)) {
  			return true;
  		} else {
  			return false;
  		}
  
  	}
  
  	// Import function for contacts
  	function import( ) {
  		$existingURIs = array();
  		$existingNames = array();
  		$query = $this->content->prepare('SELECT `displayname`, `uri` FROM *PREFIX*contacts_addressbooks WHERE userid = ?');
  		$results = $query->execute( array( $this->uid ) );
  		while($row = $results->fetchRow()) {
  			\OCP\Util::writeLog('contacts', __METHOD__.', row: ' . print_r($row, true), \OCP\Util::DEBUG);
  			$existingURIs[] = $row['uri'];
  			$existingNames[] = $row['displayname'];
  		}
  		switch( $this->appinfo->version ) {
  			default:
  				// All versions of the app have had the same db structure, so all can use the same import function
  				$query = $this->content->prepare( 'SELECT * FROM contacts_addressbooks WHERE userid = ?' );
  				$results = $query->execute( array( $this->olduid ) );
  				$idmap = array();
  				while( $row = $results->fetchRow() ) {
  					// Import each addressbook
  					$addressbookquery = OCP\DB::prepare( 'INSERT INTO `*PREFIX*contacts_addressbooks` (`userid`, `displayname`, `uri`, `description`, `ctag`) VALUES (?, ?, ?, ?, ?)' );
  					$uriSuffix = '';
  					$nameSuffix = '';
  					while (in_array($row['uri'].$uriSuffix, $existingURIs)) {
  						$uriSuffix++;
  					}
  					while (in_array($row['displayname'].$nameSuffix, $existingNames)) {
  						$nameSuffix++;
  					}
  					$addressbookquery->execute( array( $this->uid, $row['displayname'].$nameSuffix, $row['uri'].$uriSuffix, $row['description'], $row['ctag'] ) );
  					// Map the id
  					$idmap[$row['id']] = OCP\DB::insertid('*PREFIX*contacts_addressbooks');
  					// Make the addressbook active
  					OCA\Contacts\Addressbook::setActive($idmap[$row['id']], true);
  				}
  				// Now tags
  				foreach($idmap as $oldid => $newid) {
  
  					$query = $this->content->prepare( 'SELECT * FROM contacts_cards WHERE addressbookid = ?' );
  					$results = $query->execute( array( $oldid ) );
  					while( $row = $results->fetchRow() ) {
  						// Import the contacts
  						$contactquery = OCP\DB::prepare( 'INSERT INTO `*PREFIX*contacts_cards` (`addressbookid`, `fullname`, `carddata`, `uri`, `lastmodified`) VALUES (?, ?, ?, ?, ?)' );
  						$contactquery->execute( array( $newid, $row['fullname'], $row['carddata'], $row['uri'], $row['lastmodified'] ) );
  					}
  				}
  				// All done!
  			break;
  		}
  
  		return true;
  	}
  
  }
  
  // Load the provider
  new OC_Migration_Provider_Contacts( 'contacts' );