Completed
Push — stable7 ( 35746e...825360 )
by
unknown
29:41
created

update.php ➔ removeSharedFolder()   C

Complexity

Conditions 17
Paths 35

Size

Total Lines 69
Code Lines 41

Duplication

Lines 14
Ratio 20.29 %
Metric Value
cc 17
eloc 41
nc 35
nop 2
dl 14
loc 69
rs 5.6439

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
$installedVersion = OCP\Config::getAppValue('files_sharing', 'installed_version');
4
5
if (version_compare($installedVersion, '0.5', '<')) {
6
	updateFilePermissions();
7
}
8
9
if (version_compare($installedVersion, '0.4', '<')) {
10
	removeSharedFolder();
11
}
12
13
// clean up oc_share table from files which are no longer exists
14
if (version_compare($installedVersion, '0.3.5.6', '<')) {
15
	\OC\Files\Cache\Shared_Updater::fixBrokenSharesOnAppUpdate();
16
}
17
18
19
/**
20
 * it is no longer possible to share single files with delete permissions. User
21
 * should only be able to unshare single files but never to delete them.
22
 */
23
function updateFilePermissions($chunkSize = 99) {
24
	$query = OCP\DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `item_type` = ?');
25
	$result = $query->execute(array('file'));
26
27
	$updatedRows = array();
28
29
	while ($row = $result->fetchRow()) {
30
		if ($row['permissions'] & \OCP\PERMISSION_DELETE) {
31
			$updatedRows[$row['id']] = (int)$row['permissions'] & ~\OCP\PERMISSION_DELETE;
32
		}
33
	}
34
35
	$connection = \OC_DB::getConnection();
36
	$chunkedPermissionList = array_chunk($updatedRows, $chunkSize, true);
37
38 View Code Duplication
	foreach ($chunkedPermissionList as $subList) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
		$statement = "UPDATE `*PREFIX*share` SET `permissions` = CASE `id` ";
40
		//update share table
41
		$ids = implode(',', array_keys($subList));
42
		foreach ($subList as $id => $permission) {
43
			$statement .= "WHEN " . $connection->quote($id, \PDO::PARAM_INT) . " THEN " . $permission . " ";
44
		}
45
		$statement .= ' END WHERE `id` IN (' . $ids . ')';
46
47
		$query = OCP\DB::prepare($statement);
48
		$query->execute();
49
	}
50
51
}
52
53
/**
54
 * update script for the removal of the logical "Shared" folder, we create physical "Shared" folder and
55
 * update the users file_target so that it doesn't make any difference for the user
56
 * @note parameters are just for testing, please ignore them
57
 */
58
function removeSharedFolder($mkdirs = true, $chunkSize = 99) {
59
	$query = OCP\DB::prepare('SELECT * FROM `*PREFIX*share`');
60
	$result = $query->execute();
61
	$view = new \OC\Files\View('/');
62
	$users = array();
63
	$shares = array();
64
	//we need to set up user backends
65
	OC_User::useBackend(new OC_User_Database());
66
	OC_Group::useBackend(new OC_Group_Database());
67
	OC_App::loadApps(array('authentication'));
68
	//we need to set up user backends, otherwise creating the shares will fail with "because user does not exist"
69
	while ($row = $result->fetchRow()) {
70
		//collect all user shares
71
		if ((int)$row['share_type'] === 0 && ($row['item_type'] === 'file' || $row['item_type'] === 'folder')) {
72
			$users[] = $row['share_with'];
73
			$shares[$row['id']] = $row['file_target'];
74
		} else if ((int)$row['share_type'] === 1 && ($row['item_type'] === 'file' || $row['item_type'] === 'folder')) {
75
			//collect all group shares
76
			$users = array_merge($users, \OC_group::usersInGroup($row['share_with']));
77
			$shares[$row['id']] = $row['file_target'];
78
		} else if ((int)$row['share_type'] === 2) {
79
			$shares[$row['id']] = $row['file_target'];
80
		}
81
	}
82
83
	$unique_users = array_unique($users);
84
85
	if (!empty($unique_users) && !empty($shares)) {
86
87
		// create folder Shared for each user
88
89
		if ($mkdirs) {
90
			$logger = \OC::$server->getLogger();
91
			foreach ($unique_users as $user) {
92
				try {
93
					\OC\Files\Filesystem::initMountPoints($user);
94
				} catch(\OC\User\NoUserException $e) {
95
					$logger->warning("Update: removeSharedFolder - user '$user' is not present anymore" , array('app' => 'files_sharing'));
96
					continue;
97
				}
98
				if (!$view->file_exists('/' . $user . '/files/Shared')) {
99
					$view->mkdir('/' . $user . '/files/Shared');
100
				}
101
			}
102
		}
103
104
		$chunkedShareList = array_chunk($shares, $chunkSize, true);
105
		$connection = \OC_DB::getConnection();
106
107 View Code Duplication
		foreach ($chunkedShareList as $subList) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
109
			$statement = "UPDATE `*PREFIX*share` SET `file_target` = CASE `id` ";
110
			//update share table
111
			$ids = implode(',', array_keys($subList));
112
			foreach ($subList as $id => $target) {
113
				$statement .= "WHEN " . $connection->quote($id, \PDO::PARAM_INT) . " THEN " . $connection->quote('/Shared' . $target, \PDO::PARAM_STR);
114
			}
115
			$statement .= ' END WHERE `id` IN (' . $ids . ')';
116
117
			$query = OCP\DB::prepare($statement);
118
119
			$query->execute(array());
120
		}
121
122
		// set config to keep the Shared folder as the default location for new shares
123
		\OCA\Files_Sharing\Helper::setShareFolder('/Shared');
124
125
	}
126
}
127