Blame view

sources/lib/private/migration/content.php 6.56 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
  <?php
  /**
   * ownCloud
   *
   * @author Tom Needham
   * @copyright 2012 Tom Needham tom@owncloud.com
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
   *
   * You should have received a copy of the GNU Affero General Public
   * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
   *
   */
  
  
  /**
   * provides methods to add and access data from the migration
   */
  class OC_Migration_Content{
  
  	private $zip=false;
6d9380f96   Cédric Dupont   Update sources OC...
30
  	// Holds the database object
03e52840d   Kload   Init
31
32
33
34
35
  	private $db=null;
  	// Holds an array of tmpfiles to delete after zip creation
  	private $tmpfiles=array();
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
36
37
38
39
  	* sets up the
  	* @param ZipArchive $zip ZipArchive object
  	* @param object $db a database object (required for exporttype user)
  	* @return bool|null
03e52840d   Kload   Init
40
41
42
43
44
45
46
  	*/
  	public function __construct( $zip, $db=null ) {
  
  		$this->zip = $zip;
  		$this->db = $db;
  
  	}
6d9380f96   Cédric Dupont   Update sources OC...
47
48
49
50
  	/**
  	 * prepares the db
  	 * @param string $query the sql query to prepare
  	 */
03e52840d   Kload   Init
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  	public function prepare( $query ) {
  
  		// Only add database to tmpfiles if actually used
  		if( !is_null( $this->db ) ) {
  			// Get db path
  			$db = $this->db->getDatabase();
  			if(!in_array($db, $this->tmpfiles)) {
  				$this->tmpfiles[] = $db;
  			}
  		}
  
  		// Optimize the query
  		$query = $this->processQuery( $query );
  
  		// Optimize the query
  		$query = $this->db->prepare( $query );
6d9380f96   Cédric Dupont   Update sources OC...
67
  		$query = new OC_DB_StatementWrapper($query, false);
03e52840d   Kload   Init
68

6d9380f96   Cédric Dupont   Update sources OC...
69
  		return $query;
03e52840d   Kload   Init
70
71
72
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
73
74
  	* processes the db query
  	* @param string $query the query to process
03e52840d   Kload   Init
75
76
77
78
79
80
81
82
83
84
85
86
  	* @return string of processed query
  	*/
  	private function processQuery( $query ) {
  		$query = str_replace( '`', '\'', $query );
  		$query = str_replace( 'NOW()', 'datetime(\'now\')', $query );
  		$query = str_replace( 'now()', 'datetime(\'now\')', $query );
  		// remove table prefixes
  		$query = str_replace( '*PREFIX*', '', $query );
  		return $query;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
87
88
  	* copys rows to migration.db from the main database
  	* @param array $options array of options.
03e52840d   Kload   Init
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  	* @return bool
  	*/
  	public function copyRows( $options ) {
  		if( !array_key_exists( 'table', $options ) ) {
  			return false;
  		}
  
  		$return = array();
  
  		// Need to include 'where' in the query?
  		if( array_key_exists( 'matchval', $options ) && array_key_exists( 'matchcol', $options ) ) {
  
  			// If only one matchval, create an array
  			if(!is_array($options['matchval'])) {
  				$options['matchval'] = array( $options['matchval'] );
  			}
  
  			foreach( $options['matchval'] as $matchval ) {
  				// Run the query for this match value (where x = y value)
  				$sql = 'SELECT * FROM `*PREFIX*' . $options['table'] . '` WHERE `' . $options['matchcol'] . '` = ?';
  				$query = OC_DB::prepare( $sql );
  				$results = $query->execute( array( $matchval ) );
  				$newreturns = $this->insertData( $results, $options );
  				$return = array_merge( $return, $newreturns );
  			}
  
  		} else {
  			// Just get everything
  			$sql = 'SELECT * FROM `*PREFIX*' . $options['table'] . '`';
  			$query = OC_DB::prepare( $sql );
  			$results = $query->execute();
  			$return = $this->insertData( $results, $options );
  
  		}
  
  		return $return;
  
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
129
130
131
  	* saves a sql data set into migration.db
  	* @param OC_DB_StatementWrapper $data a sql data set returned from self::prepare()->query()
  	* @param array $options array of copyRows options
03e52840d   Kload   Init
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  	* @return void
  	*/
  	private function insertData( $data, $options ) {
  		$return = array();
  		// Foreach row of data to insert
  		while( $row = $data->fetchRow() ) {
  			// Now save all this to the migration.db
  			foreach($row as $field=>$value) {
  				$fields[] = $field;
  				$values[] = $value;
  			}
  
  			// Generate some sql
  			$sql = "INSERT INTO `" . $options['table'] . '` ( `';
  			$fieldssql = implode( '`, `', $fields );
  			$sql .= $fieldssql . "` ) VALUES( ";
  			$valuessql = substr( str_repeat( '?, ', count( $fields ) ), 0, -2 );
  			$sql .= $valuessql . " )";
  			// Make the query
  			$query = $this->prepare( $sql );
6d9380f96   Cédric Dupont   Update sources OC...
152
153
154
155
156
  			$query->execute( $values );
  			// Do we need to return some values?
  			if( array_key_exists( 'idcol', $options ) ) {
  				// Yes we do
  				$return[] = $row[$options['idcol']];
03e52840d   Kload   Init
157
  			} else {
6d9380f96   Cédric Dupont   Update sources OC...
158
159
  				// Take a guess and return the first field :)
  				$return[] = reset($row);
03e52840d   Kload   Init
160
161
162
163
164
165
166
167
  			}
  			$fields = '';
  			$values = '';
  		}
  		return $return;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
168
169
170
171
  	* adds a directory to the zip object
  	* @param boolean|string $dir string path of the directory to add
  	* @param bool $recursive
  	* @param string $internaldir path of folder to add dir to in zip
03e52840d   Kload   Init
172
173
174
175
176
177
178
179
180
  	* @return bool
  	*/
  	public function addDir( $dir, $recursive=true, $internaldir='' ) {
  		$dirname = basename($dir);
  		$this->zip->addEmptyDir($internaldir . $dirname);
  		$internaldir.=$dirname.='/';
  		if( !file_exists( $dir ) ) {
  			return false;
  		}
31b7f2792   Kload   Upgrade to ownclo...
181
182
  		$dirhandle = opendir($dir);
  		if(is_resource($dirhandle)) {
03e52840d   Kload   Init
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  			while (false !== ( $file = readdir($dirhandle))) {
  
  				if (( $file != '.' ) && ( $file != '..' )) {
  
  					if (is_dir($dir . '/' . $file) && $recursive) {
  						$this->addDir($dir . '/' . $file, $recursive, $internaldir);
  					} elseif (is_file($dir . '/' . $file)) {
  						$this->zip->addFile($dir . '/' . $file, $internaldir . $file);
  					}
  				}
  			}
  			closedir($dirhandle);
  		} else {
  			OC_Log::write('admin_export', "Was not able to open directory: " . $dir, OC_Log::ERROR);
  			return false;
  		}
  		return true;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
203
204
205
  	* adds a file to the zip from a given string
  	* @param string $data string of data to add
  	* @param string $path the relative path inside of the zip to save the file to
03e52840d   Kload   Init
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  	* @return bool
  	*/
  	public function addFromString( $data, $path ) {
  		// Create a temp file
  		$file = tempnam( get_temp_dir(). '/', 'oc_export_tmp_' );
  		$this->tmpfiles[] = $file;
  		if( !file_put_contents( $file, $data ) ) {
  			OC_Log::write( 'migation', 'Failed to save data to a temporary file', OC_Log::ERROR );
  			return false;
  		}
  		// Add file to the zip
  		$this->zip->addFile( $file, $path );
  		return true;
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
222
  	* closes the zip, removes temp files
03e52840d   Kload   Init
223
224
225
226
227
228
229
230
231
232
233
234
235
236
  	* @return bool
  	*/
  	public function finish() {
  		if( !$this->zip->close() ) {
  			OC_Log::write( 'migration',
  				'Failed to write the zip file with error: '.$this->zip->getStatusString(),
  				OC_Log::ERROR );
  			return false;
  		}
  		$this->cleanup();
  		return true;
  	}
  
  		/**
6d9380f96   Cédric Dupont   Update sources OC...
237
  	* cleans up after the zip
03e52840d   Kload   Init
238
239
240
241
242
243
244
245
  	*/
  	private function cleanup() {
  		// Delete tmp files
  		foreach($this->tmpfiles as $i) {
  			unlink( $i );
  		}
  	}
  }