Completed
Push — master ( f8e4d0...306d4e )
by Björn
38:32 queued 22:55
created

Hooks::pre_renameOrCopy_hook()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
nc 3
nop 1
dl 0
loc 18
rs 9.2
c 1
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bart Visscher <[email protected]>
6
 * @author Björn Schießle <[email protected]>
7
 * @author Jörn Friedrich Dreyer <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Robin McCorkell <[email protected]>
11
 * @author Sam Tuke <[email protected]>
12
 * @author Vincent Petry <[email protected]>
13
 *
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
30
/**
31
 * This class contains all hooks.
32
 */
33
34
namespace OCA\Files_Versions;
35
36
class Hooks {
37
38
	public static function connectHooks() {
39
		// Listen to write signals
40
		\OCP\Util::connectHook('OC_Filesystem', 'write', 'OCA\Files_Versions\Hooks', 'write_hook');
41
		// Listen to delete and rename signals
42
		\OCP\Util::connectHook('OC_Filesystem', 'post_delete', 'OCA\Files_Versions\Hooks', 'remove_hook');
43
		\OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Versions\Hooks', 'pre_remove_hook');
44
		\OCP\Util::connectHook('OC_Filesystem', 'post_rename', 'OCA\Files_Versions\Hooks', 'rename_hook');
45
		\OCP\Util::connectHook('OC_Filesystem', 'post_copy', 'OCA\Files_Versions\Hooks', 'copy_hook');
46
		\OCP\Util::connectHook('OC_Filesystem', 'rename', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook');
47
		\OCP\Util::connectHook('OC_Filesystem', 'copy', 'OCA\Files_Versions\Hooks', 'pre_renameOrCopy_hook');
48
49
		$eventDispatcher = \OC::$server->getEventDispatcher();
50
		$eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', ['OCA\Files_Versions\Hooks', 'onLoadFilesAppScripts']);
51
	}
52
53
	/**
54
	 * listen to write event.
55
	 */
56
	public static function write_hook( $params ) {
57
		$path = $params[\OC\Files\Filesystem::signal_param_path];
58
		if($path !== '') {
59
			Storage::store($path);
60
		}
61
	}
62
63
64
	/**
65
	 * Erase versions of deleted file
66
	 * @param array $params
67
	 *
68
	 * This function is connected to the delete signal of OC_Filesystem
69
	 * cleanup the versions directory if the actual file gets deleted
70
	 */
71
	public static function remove_hook($params) {
72
		$path = $params[\OC\Files\Filesystem::signal_param_path];
73
		if($path !== '') {
74
			Storage::delete($path);
75
		}
76
	}
77
78
	/**
79
	 * mark file as "deleted" so that we can clean up the versions if the file is gone
80
	 * @param array $params
81
	 */
82
	public static function pre_remove_hook($params) {
83
		$path = $params[\OC\Files\Filesystem::signal_param_path];
84
			if($path !== '') {
85
				Storage::markDeletedFile($path);
86
			}
87
	}
88
89
	/**
90
	 * rename/move versions of renamed/moved files
91
	 * @param array $params array with oldpath and newpath
92
	 *
93
	 * This function is connected to the rename signal of OC_Filesystem and adjust the name and location
94
	 * of the stored versions along the actual file
95
	 */
96 View Code Duplication
	public static function rename_hook($params) {
97
		$oldpath = $params['oldpath'];
98
		$newpath = $params['newpath'];
99
		if($oldpath !== '' && $newpath !== '') {
100
			Storage::renameOrCopy($oldpath, $newpath, 'rename');
101
		}
102
	}
103
104
	/**
105
	 * copy versions of copied files
106
	 * @param array $params array with oldpath and newpath
107
	 *
108
	 * This function is connected to the copy signal of OC_Filesystem and copies the
109
	 * the stored versions to the new location
110
	 */
111 View Code Duplication
	public static function copy_hook($params) {
112
		$oldpath = $params['oldpath'];
113
		$newpath = $params['newpath'];
114
		if($oldpath !== '' && $newpath !== '') {
115
			Storage::renameOrCopy($oldpath, $newpath, 'copy');
116
		}
117
	}
118
119
	/**
120
	 * Remember owner and the owner path of the source file.
121
	 * If the file already exists, then it was a upload of a existing file
122
	 * over the web interface and we call Storage::store() directly
123
	 *
124
	 * @param array $params array with oldpath and newpath
125
	 *
126
	 */
127
	public static function pre_renameOrCopy_hook($params) {
128
		// if we rename a movable mount point, then the versions don't have
129
		// to be renamed
130
		$absOldPath = \OC\Files\Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files' . $params['oldpath']);
131
		$manager = \OC\Files\Filesystem::getMountManager();
132
		$mount = $manager->find($absOldPath);
133
		$internalPath = $mount->getInternalPath($absOldPath);
134
		if ($internalPath === '' and $mount instanceof \OC\Files\Mount\MoveableMount) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
135
			return;
136
		}
137
138
		$view = new \OC\Files\View(\OCP\User::getUser() . '/files');
139
		if ($view->file_exists($params['newpath'])) {
140
			Storage::store($params['newpath']);
141
		} else {
142
			Storage::setSourcePathAndUser($params['oldpath']);
143
		}
144
	}
145
146
	/**
147
	 * Load additional scripts when the files app is visible
148
	 */
149
	public static function onLoadFilesAppScripts() {
150
		\OCP\Util::addScript('files_versions', 'merged');
151
	}
152
}
153