Completed
Push — 16.1 ( 6f911f...5d651a )
by Ralf
58:52 queued 41:07
created
api/src/Vfs/Sqlfs/Utils.php 2 patches
Spacing   +44 added lines, -45 removed lines patch added patch discarded remove patch
@@ -25,7 +25,7 @@  discard block
 block discarded – undo
25 25
 	 *
26 26
 	 * @param boolean $debug true to echo a message for each copied file
27 27
 	 */
28
-	static function migrate_db2fs($debug=true)
28
+	static function migrate_db2fs($debug = true)
29 29
 	{
30 30
 		if (!is_object(self::$pdo))
31 31
 		{
@@ -38,16 +38,16 @@  discard block
 block discarded – undo
38 38
 		$fs_id = $fs_name = $fs_size = $fs_content = null;
39 39
 		$n = 0;
40 40
 		$stmt = self::$pdo->prepare($query);
41
-		$stmt->bindColumn(1,$fs_id);
42
-		$stmt->bindColumn(2,$fs_name);
43
-		$stmt->bindColumn(3,$fs_size);
44
-		$stmt->bindColumn(4,$fs_content,\PDO::PARAM_LOB);
41
+		$stmt->bindColumn(1, $fs_id);
42
+		$stmt->bindColumn(2, $fs_name);
43
+		$stmt->bindColumn(3, $fs_size);
44
+		$stmt->bindColumn(4, $fs_content, \PDO::PARAM_LOB);
45 45
 		$stmt->bindValue(':offset', $n, \PDO::PARAM_INT);
46 46
 
47 47
 		while ($stmt->execute())	// && $stmt->rowCount() does not work for all dbs :(
48 48
 		{
49 49
 			$start = $n;
50
-			foreach($stmt as $row)
50
+			foreach ($stmt as $row)
51 51
 			{
52 52
 				// hack to work around a current php bug (http://bugs.php.net/bug.php?id=40913)
53 53
 				// PDOStatement::bindColumn(,,\PDO::PARAM_LOB) is not working for MySQL, content is returned as string :-(
@@ -56,7 +56,7 @@  discard block
 block discarded – undo
56 56
 					$content = fopen('php://temp', 'wb');
57 57
 					fwrite($content, $fs_content);
58 58
 					fseek($content, 0, SEEK_SET);
59
-					$fs_content = '';	// can NOT unset it, at least in PHP 7 it looses bind to column!
59
+					$fs_content = ''; // can NOT unset it, at least in PHP 7 it looses bind to column!
60 60
 				}
61 61
 				else
62 62
 				{
@@ -67,15 +67,15 @@  discard block
 block discarded – undo
67 67
 					throw new Api\Exception\AssertionFailed(__METHOD__."(): fs_id=$fs_id ($fs_name, $fs_size bytes) content is NO resource! ".array2string($content));
68 68
 				}
69 69
 				$filename = self::_fs_path($fs_id);
70
-				if (!file_exists($fs_dir=Vfs::dirname($filename)))
70
+				if (!file_exists($fs_dir = Vfs::dirname($filename)))
71 71
 				{
72
-					self::mkdir_recursive($fs_dir,0700,true);
72
+					self::mkdir_recursive($fs_dir, 0700, true);
73 73
 				}
74
-				if (!($dest = fopen($filename,'w')))
74
+				if (!($dest = fopen($filename, 'w')))
75 75
 				{
76 76
 					throw new Api\Exception\AssertionFailed(__METHOD__."(): fopen($filename,'w') failed!");
77 77
 				}
78
-				if (($bytes = stream_copy_to_stream($content,$dest)) != $fs_size)
78
+				if (($bytes = stream_copy_to_stream($content, $dest)) != $fs_size)
79 79
 				{
80 80
 					throw new Api\Exception\AssertionFailed(__METHOD__."(): fs_id=$fs_id ($fs_name) $bytes bytes copied != size of $fs_size bytes!");
81 81
 				}
@@ -85,11 +85,11 @@  discard block
 block discarded – undo
85 85
 
86 86
 				++$n;
87 87
 			}
88
-			if (!$n || $n == $start) break;	// just in case nothing is found, statement will execute just fine
88
+			if (!$n || $n == $start) break; // just in case nothing is found, statement will execute just fine
89 89
 
90 90
 			$stmt->bindValue(':offset', $n, \PDO::PARAM_INT);
91 91
 		}
92
-		unset($row);	// not used, as we access bound variables
92
+		unset($row); // not used, as we access bound variables
93 93
 		unset($stmt);
94 94
 
95 95
 		if ($n)	// delete all content in DB, if there was some AND no error (exception thrown!)
@@ -107,14 +107,14 @@  discard block
 block discarded – undo
107 107
 	 * @param boolean $check_only =true
108 108
 	 * @return array with messages / found problems
109 109
 	 */
110
-	public static function fsck($check_only=true)
110
+	public static function fsck($check_only = true)
111 111
 	{
112 112
 		if (!is_object(self::$pdo))
113 113
 		{
114 114
 			self::_pdo();
115 115
 		}
116 116
 		$msgs = array();
117
-		foreach(array(
117
+		foreach (array(
118 118
 			self::fsck_fix_required_nodes($check_only),
119 119
 			self::fsck_fix_multiple_active($check_only),
120 120
 			self::fsck_fix_unconnected($check_only),
@@ -140,7 +140,7 @@  discard block
 block discarded – undo
140 140
 	 * @param boolean $check_only =true
141 141
 	 * @return array with messages / found problems
142 142
 	 */
143
-	private static function fsck_fix_required_nodes($check_only=true)
143
+	private static function fsck_fix_required_nodes($check_only = true)
144 144
 	{
145 145
 		static $dirs = array(
146 146
 			'/' => 1,
@@ -150,7 +150,7 @@  discard block
 block discarded – undo
150 150
 		$stmt = $delete_stmt = null;
151 151
 		$msgs = array();
152 152
 		$sqlfs = new StreamWrapper();
153
-		foreach($dirs as $path => $id)
153
+		foreach ($dirs as $path => $id)
154 154
 		{
155 155
 			if (!($stat = $sqlfs->url_stat($path, STREAM_URL_STAT_LINK)) || $stat['ino'] != $id)
156 156
 			{
@@ -168,7 +168,7 @@  discard block
 block discarded – undo
168 168
 					try {
169 169
 						$ok = $stmt->execute($data = array(
170 170
 							'fs_id' => $id,
171
-							'fs_name' => substr($path,1),
171
+							'fs_name' => substr($path, 1),
172 172
 							'fs_dir'  => $path == '/' ? 0 : $dirs['/'],
173 173
 							'fs_mode' => 05,
174 174
 							'fs_uid' => 0,
@@ -183,7 +183,7 @@  discard block
 block discarded – undo
183 183
 					catch (\PDOException $e)
184 184
 					{
185 185
 						$ok = false;
186
-						unset($e);	// ignore exception
186
+						unset($e); // ignore exception
187 187
 					}
188 188
 					if (!$ok)	// can not insert it, try deleting it first
189 189
 					{
@@ -196,15 +196,14 @@  discard block
 block discarded – undo
196 196
 						}
197 197
 						catch (\PDOException $e)
198 198
 						{
199
-							unset($e);	// ignore exception
199
+							unset($e); // ignore exception
200 200
 						}
201 201
 					}
202
-					$msgs[] = $ok ? lang('Required directory "%1" created.', $path) :
203
-						lang('Failed to create required directory "%1"!', $path);
202
+					$msgs[] = $ok ? lang('Required directory "%1" created.', $path) : lang('Failed to create required directory "%1"!', $path);
204 203
 				}
205 204
 			}
206 205
 			// check if directory is at least world readable and executable (r-x), we allow more but not less
207
-			elseif (($stat['mode'] & 05) != 05)
206
+			elseif (($stat['mode']&05) != 05)
208 207
 			{
209 208
 				if ($check_only)
210 209
 				{
@@ -244,20 +243,20 @@  discard block
 block discarded – undo
244 243
 	 * @param boolean $check_only =true
245 244
 	 * @return array with messages / found problems
246 245
 	 */
247
-	private static function fsck_fix_no_content($check_only=true)
246
+	private static function fsck_fix_no_content($check_only = true)
248 247
 	{
249 248
 		$stmt = null;
250 249
 		$msgs = array();
251
-		foreach(self::$pdo->query('SELECT fs_id FROM '.self::TABLE.
250
+		foreach (self::$pdo->query('SELECT fs_id FROM '.self::TABLE.
252 251
 			" WHERE fs_mime!='httpd/unix-directory' AND fs_content IS NULL AND fs_link IS NULL") as $row)
253 252
 		{
254
-			if (!file_exists($phy_path=self::_fs_path($row['fs_id'])))
253
+			if (!file_exists($phy_path = self::_fs_path($row['fs_id'])))
255 254
 			{
256 255
 				$path = self::id2path($row['fs_id']);
257 256
 				if ($check_only)
258 257
 				{
259 258
 					$msgs[] = lang('File %1 has no content in physical filesystem %2!',
260
-						$path.' (#'.$row['fs_id'].')',$phy_path);
259
+						$path.' (#'.$row['fs_id'].')', $phy_path);
261 260
 				}
262 261
 				else
263 262
 				{
@@ -269,12 +268,12 @@  discard block
 block discarded – undo
269 268
 					if ($stmt->execute(array('fs_id' => $row['fs_id'])) &&
270 269
 						$stmt_props->execute(array('fs_id' => $row['fs_id'])))
271 270
 					{
272
-						$msgs[] = lang('File %1 has no content in physical filesystem %2 --> file removed!',$path,$phy_path);
271
+						$msgs[] = lang('File %1 has no content in physical filesystem %2 --> file removed!', $path, $phy_path);
273 272
 					}
274 273
 					else
275 274
 					{
276 275
 						$msgs[] = lang('File %1 has no content in physical filesystem %2 --> failed to remove file!',
277
-							$path.' (#'.$row['fs_id'].')',$phy_path);
276
+							$path.' (#'.$row['fs_id'].')', $phy_path);
278 277
 					}
279 278
 				}
280 279
 			}
@@ -304,12 +303,12 @@  discard block
 block discarded – undo
304 303
 	 * @param boolean $check_only =true
305 304
 	 * @return array with messages / found problems
306 305
 	 */
307
-	private static function fsck_fix_unconnected($check_only=true)
306
+	private static function fsck_fix_unconnected($check_only = true)
308 307
 	{
309 308
 		$lostnfound = null;
310 309
 		$msgs = array();
311 310
 		$sqlfs = new StreamWrapper();
312
-		foreach(self::$pdo->query('SELECT fs.* FROM '.self::TABLE.' fs'.
311
+		foreach (self::$pdo->query('SELECT fs.* FROM '.self::TABLE.' fs'.
313 312
 			' LEFT JOIN '.self::TABLE.' dir ON dir.fs_id=fs.fs_dir'.
314 313
 			' WHERE fs.fs_id > 1 AND dir.fs_id IS NULL') as $row)
315 314
 		{
@@ -328,14 +327,14 @@  discard block
 block discarded – undo
328 327
 					Vfs::$is_root = true;
329 328
 					if (!$sqlfs->mkdir(self::LOST_N_FOUND, self::LOST_N_FOUND_MOD, 0) ||
330 329
 						!(!($admins = $GLOBALS['egw']->accounts->name2id(self::LOST_N_FOUND_GRP)) ||
331
-						   self::chgrp(self::LOST_N_FOUND, $admins) && self::chmod(self::LOST_N_FOUND,self::LOST_N_FOUND_MOD)) ||
330
+						   self::chgrp(self::LOST_N_FOUND, $admins) && self::chmod(self::LOST_N_FOUND, self::LOST_N_FOUND_MOD)) ||
332 331
 						!($lostnfound = $sqlfs->url_stat(self::LOST_N_FOUND, STREAM_URL_STAT_QUIET)))
333 332
 					{
334
-						$msgs[] = lang("Can't create directory %1 to connect found unconnected nodes to it!",self::LOST_N_FOUND);
333
+						$msgs[] = lang("Can't create directory %1 to connect found unconnected nodes to it!", self::LOST_N_FOUND);
335 334
 					}
336 335
 					else
337 336
 					{
338
-						$msgs[] = lang('Successful created new directory %1 for unconnected nods.',self::LOST_N_FOUND);
337
+						$msgs[] = lang('Successful created new directory %1 for unconnected nods.', self::LOST_N_FOUND);
339 338
 					}
340 339
 					Vfs::$is_root = false;
341 340
 					if (!$lostnfound) break;
@@ -360,7 +359,7 @@  discard block
 block discarded – undo
360 359
 		}
361 360
 		if ($check_only && $msgs)
362 361
 		{
363
-			$msgs[] = lang('Unconnected nodes will be moved to %1.',self::LOST_N_FOUND);
362
+			$msgs[] = lang('Unconnected nodes will be moved to %1.', self::LOST_N_FOUND);
364 363
 		}
365 364
 		return $msgs;
366 365
 	}
@@ -373,20 +372,20 @@  discard block
 block discarded – undo
373 372
 	 * @param boolean $check_only =true
374 373
 	 * @return array with messages / found problems
375 374
 	 */
376
-	private static function fsck_fix_multiple_active($check_only=true)
375
+	private static function fsck_fix_multiple_active($check_only = true)
377 376
 	{
378 377
 		$stmt = $inactivate_msg_added = null;
379 378
 		$msgs = array();
380
-		foreach(self::$pdo->query('SELECT fs_dir,fs_name,COUNT(*) FROM '.self::TABLE.
379
+		foreach (self::$pdo->query('SELECT fs_dir,fs_name,COUNT(*) FROM '.self::TABLE.
381 380
 			' WHERE fs_active='.self::_pdo_boolean(true)." OR fs_mime='httpd/unix-directory'".
382
-			' GROUP BY fs_dir,'.(self::$pdo_type == 'mysql' ? 'BINARY ' : '').'fs_name'.	// fs_name is casesensitive!
381
+			' GROUP BY fs_dir,'.(self::$pdo_type == 'mysql' ? 'BINARY ' : '').'fs_name'.// fs_name is casesensitive!
383 382
 			' HAVING COUNT(*) > 1') as $row)
384 383
 		{
385 384
 			if (!isset($stmt))
386 385
 			{
387 386
 				$stmt = self::$pdo->prepare('SELECT *,(SELECT COUNT(*) FROM '.self::TABLE.' sub WHERE sub.fs_dir=fs.fs_id) AS children'.
388 387
 					' FROM '.self::TABLE.' fs'.
389
-					' WHERE fs.fs_dir=:fs_dir AND (fs.fs_active='.self::_pdo_boolean(true)." OR fs_mime='httpd/unix-directory')" .
388
+					' WHERE fs.fs_dir=:fs_dir AND (fs.fs_active='.self::_pdo_boolean(true)." OR fs_mime='httpd/unix-directory')".
390 389
 					' AND fs.fs_name'.self::$case_sensitive_equal.':fs_name'.
391 390
 					" ORDER BY fs.fs_mime='httpd/unix-directory' DESC,fs.fs_active DESC,children DESC,fs.fs_modified DESC");
392 391
 				$inactivate_stmt = self::$pdo->prepare('UPDATE '.self::TABLE.
@@ -400,14 +399,14 @@  discard block
 block discarded – undo
400 399
 				'fs_dir'  => $row['fs_dir'],
401 400
 				'fs_name' => $row['fs_name'],
402 401
 			));
403
-			foreach($stmt as $n => $entry)
402
+			foreach ($stmt as $n => $entry)
404 403
 			{
405 404
 				if ($entry['fs_mime'] == 'httpd/unix-directory')
406 405
 				{
407 406
 					// by sorting active directores first (fs.fs_active DESC), we make sure active one is kept
408 407
 					if (!$n)
409 408
 					{
410
-						$dir = $entry;	// directory to keep
409
+						$dir = $entry; // directory to keep
411 410
 						$msgs[] = lang('%1 directories %2 found!', $row[2], self::id2path($entry['fs_id']));
412 411
 						if ($check_only) break;
413 412
 					}
@@ -423,7 +422,7 @@  discard block
 block discarded – undo
423 422
 							$dir['children'] += $children;
424 423
 						}
425 424
 						self::$pdo->query('DELETE FROM '.self::TABLE.' WHERE fs_id='.(int)$entry['fs_id']);
426
-						$msgs[] = lang('Removed (now) empty directory fs_id=%1',$entry['fs_id']);
425
+						$msgs[] = lang('Removed (now) empty directory fs_id=%1', $entry['fs_id']);
427 426
 					}
428 427
 				}
429 428
 				elseif (isset($dir))	// file and directory with same name exist!
@@ -439,9 +438,9 @@  discard block
 block discarded – undo
439 438
 					}
440 439
 					else
441 440
 					{
442
-						$cnt = ucfirst(lang('none of %1', $row[2]-1));
441
+						$cnt = ucfirst(lang('none of %1', $row[2] - 1));
443 442
 					}
444
-					$msgs[] = lang('%1 active file(s) with same name as directory inactivated!',$cnt);
443
+					$msgs[] = lang('%1 active file(s) with same name as directory inactivated!', $cnt);
445 444
 					break;
446 445
 				}
447 446
 				else	// newest file --> set for all other fs_active=false
@@ -457,7 +456,7 @@  discard block
 block discarded – undo
457 456
 					}
458 457
 					else
459 458
 					{
460
-						$cnt = lang('none of %1', $row[2]-1);
459
+						$cnt = lang('none of %1', $row[2] - 1);
461 460
 					}
462 461
 					$msgs[] = lang('More then one active file %1 found, inactivating %2 older revisions!',
463 462
 						self::id2path($entry['fs_id']), $cnt);
Please login to merge, or discard this patch.
Braces   +45 added lines, -11 removed lines patch added patch discarded remove patch
@@ -44,9 +44,12 @@  discard block
 block discarded – undo
44 44
 		$stmt->bindColumn(4,$fs_content,\PDO::PARAM_LOB);
45 45
 		$stmt->bindValue(':offset', $n, \PDO::PARAM_INT);
46 46
 
47
-		while ($stmt->execute())	// && $stmt->rowCount() does not work for all dbs :(
47
+		while ($stmt->execute())
48
+		{
49
+			// && $stmt->rowCount() does not work for all dbs :(
48 50
 		{
49 51
 			$start = $n;
52
+		}
50 53
 			foreach($stmt as $row)
51 54
 			{
52 55
 				// hack to work around a current php bug (http://bugs.php.net/bug.php?id=40913)
@@ -79,22 +82,32 @@  discard block
 block discarded – undo
79 82
 				{
80 83
 					throw new Api\Exception\AssertionFailed(__METHOD__."(): fs_id=$fs_id ($fs_name) $bytes bytes copied != size of $fs_size bytes!");
81 84
 				}
82
-				if ($debug) error_log("$fs_id: $fs_name: $bytes bytes copied to fs");
85
+				if ($debug)
86
+				{
87
+					error_log("$fs_id: $fs_name: $bytes bytes copied to fs");
88
+				}
83 89
 				fclose($dest);
84 90
 				fclose($content); unset($content);
85 91
 
86 92
 				++$n;
87 93
 			}
88
-			if (!$n || $n == $start) break;	// just in case nothing is found, statement will execute just fine
94
+			if (!$n || $n == $start)
95
+			{
96
+				break;
97
+			}
98
+			// just in case nothing is found, statement will execute just fine
89 99
 
90 100
 			$stmt->bindValue(':offset', $n, \PDO::PARAM_INT);
91 101
 		}
92 102
 		unset($row);	// not used, as we access bound variables
93 103
 		unset($stmt);
94 104
 
95
-		if ($n)	// delete all content in DB, if there was some AND no error (exception thrown!)
105
+		if ($n)
106
+		{
107
+			// delete all content in DB, if there was some AND no error (exception thrown!)
96 108
 		{
97 109
 			$query = 'UPDATE '.self::TABLE.' SET fs_content=NULL';
110
+		}
98 111
 			$stmt = self::$pdo->prepare($query);
99 112
 			$stmt->execute();
100 113
 		}
@@ -121,7 +134,10 @@  discard block
 block discarded – undo
121 134
 			self::fsck_fix_no_content($check_only),
122 135
 		) as $check_msgs)
123 136
 		{
124
-			if ($check_msgs) $msgs = array_merge($msgs, $check_msgs);
137
+			if ($check_msgs)
138
+			{
139
+				$msgs = array_merge($msgs, $check_msgs);
140
+			}
125 141
 		}
126 142
 
127 143
 		foreach (Api\Hooks::process(array(
@@ -129,7 +145,10 @@  discard block
 block discarded – undo
129 145
 			'check_only' => $check_only)
130 146
 		) as $app_msgs)
131 147
 		{
132
-			if ($app_msgs) $msgs = array_merge($msgs, $app_msgs);
148
+			if ($app_msgs)
149
+			{
150
+				$msgs = array_merge($msgs, $app_msgs);
151
+			}
133 152
 		}
134 153
 		return $msgs;
135 154
 	}
@@ -185,11 +204,14 @@  discard block
 block discarded – undo
185 204
 						$ok = false;
186 205
 						unset($e);	// ignore exception
187 206
 					}
188
-					if (!$ok)	// can not insert it, try deleting it first
207
+					if (!$ok)
208
+					{
209
+						// can not insert it, try deleting it first
189 210
 					{
190 211
 						if (!isset($delete_stmt))
191 212
 						{
192 213
 							$delete_stmt = self::$pdo->prepare('DELETE FROM '.self::TABLE.' WHERE fs_id=:fs_id');
214
+					}
193 215
 						}
194 216
 						try {
195 217
 							$ok = $delete_stmt->execute(array('fs_id' => $id)) && $stmt->execute($data);
@@ -231,7 +253,10 @@  discard block
 block discarded – undo
231 253
 		if (!$check_only && $msgs)
232 254
 		{
233 255
 			global $oProc;
234
-			if (!isset($oProc)) $oProc = new Api\Db\Schema();
256
+			if (!isset($oProc))
257
+			{
258
+				$oProc = new Api\Db\Schema();
259
+			}
235 260
 			// PostgreSQL seems to require to update the sequenz, after manually inserting id's
236 261
 			$oProc->UpdateSequence('egw_sqlfs', 'fs_id');
237 262
 		}
@@ -338,7 +363,10 @@  discard block
 block discarded – undo
338 363
 						$msgs[] = lang('Successful created new directory %1 for unconnected nods.',self::LOST_N_FOUND);
339 364
 					}
340 365
 					Vfs::$is_root = false;
341
-					if (!$lostnfound) break;
366
+					if (!$lostnfound)
367
+					{
368
+						break;
369
+					}
342 370
 				}
343 371
 				$stmt = self::$pdo->prepare('UPDATE '.self::TABLE.' SET fs_dir=:fs_dir WHERE fs_id=:fs_id');
344 372
 			}
@@ -409,7 +437,10 @@  discard block
 block discarded – undo
409 437
 					{
410 438
 						$dir = $entry;	// directory to keep
411 439
 						$msgs[] = lang('%1 directories %2 found!', $row[2], self::id2path($entry['fs_id']));
412
-						if ($check_only) break;
440
+						if ($check_only)
441
+						{
442
+							break;
443
+						}
413 444
 					}
414 445
 					else
415 446
 					{
@@ -426,7 +457,9 @@  discard block
 block discarded – undo
426 457
 						$msgs[] = lang('Removed (now) empty directory fs_id=%1',$entry['fs_id']);
427 458
 					}
428 459
 				}
429
-				elseif (isset($dir))	// file and directory with same name exist!
460
+				elseif (isset($dir))
461
+				{
462
+					// file and directory with same name exist!
430 463
 				{
431 464
 					if (!$check_only)
432 465
 					{
@@ -435,6 +468,7 @@  discard block
 block discarded – undo
435 468
 							'fs_name' => $row['fs_name'],
436 469
 							'fs_id'   => $dir['fs_id'],
437 470
 						));
471
+				}
438 472
 						$cnt = $inactivate_stmt->rowCount();
439 473
 					}
440 474
 					else
Please login to merge, or discard this patch.