Completed
Pull Request — master (#3218)
by Vars
46:46 queued 34:29
created
lib/private/Mail/Message.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -162,7 +162,7 @@
 block discarded – undo
162 162
 	/**
163 163
 	 * Set the BCC recipients of this message.
164 164
 	 *
165
-	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
165
+	 * @param string[] $recipients Example: array('[email protected]', '[email protected]' => 'A name')
166 166
 	 * @return $this
167 167
 	 */
168 168
 	public function setBcc(array $recipients) {
Please login to merge, or discard this patch.
Indentation   +219 added lines, -219 removed lines patch added patch discarded remove patch
@@ -31,223 +31,223 @@
 block discarded – undo
31 31
  * @package OC\Mail
32 32
  */
33 33
 class Message {
34
-	/** @var Swift_Message */
35
-	private $swiftMessage;
36
-
37
-	/**
38
-	 * @param Swift_Message $swiftMessage
39
-	 */
40
-	function __construct(Swift_Message $swiftMessage) {
41
-		$this->swiftMessage = $swiftMessage;
42
-	}
43
-
44
-	/**
45
-	 * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains
46
-	 * FIXME: Remove this once SwiftMailer supports IDN
47
-	 *
48
-	 * @param array $addresses Array of mail addresses, key will get converted
49
-	 * @return array Converted addresses if `idn_to_ascii` exists
50
-	 */
51
-	protected function convertAddresses($addresses) {
52
-		if (!function_exists('idn_to_ascii')) {
53
-			return $addresses;
54
-		}
55
-
56
-		$convertedAddresses = array();
57
-
58
-		foreach($addresses as $email => $readableName) {
59
-			if(!is_numeric($email)) {
60
-				list($name, $domain) = explode('@', $email, 2);
61
-				$domain = idn_to_ascii($domain);
62
-				$convertedAddresses[$name.'@'.$domain] = $readableName;
63
-			} else {
64
-				list($name, $domain) = explode('@', $readableName, 2);
65
-				$domain = idn_to_ascii($domain);
66
-				$convertedAddresses[$email] = $name.'@'.$domain;
67
-			}
68
-		}
69
-
70
-		return $convertedAddresses;
71
-	}
72
-
73
-	/**
74
-	 * Set the from address of this message.
75
-	 *
76
-	 * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
77
-	 *
78
-	 * @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name')
79
-	 * @return $this
80
-	 */
81
-	public function setFrom(array $addresses) {
82
-		$addresses = $this->convertAddresses($addresses);
83
-
84
-		$this->swiftMessage->setFrom($addresses);
85
-		return $this;
86
-	}
87
-
88
-	/**
89
-	 * Get the from address of this message.
90
-	 *
91
-	 * @return array
92
-	 */
93
-	public function getFrom() {
94
-		return $this->swiftMessage->getFrom();
95
-	}
96
-
97
-	/**
98
-	 * Set the Reply-To address of this message
99
-	 *
100
-	 * @param array $addresses
101
-	 * @return $this
102
-	 */
103
-	public function setReplyTo(array $addresses) {
104
-		$addresses = $this->convertAddresses($addresses);
105
-
106
-		$this->swiftMessage->setReplyTo($addresses);
107
-		return $this;
108
-	}
109
-
110
-	/**
111
-	 * Returns the Reply-To address of this message
112
-	 *
113
-	 * @return array
114
-	 */
115
-	public function getReplyTo() {
116
-		return $this->swiftMessage->getReplyTo();
117
-	}
118
-
119
-	/**
120
-	 * Set the to addresses of this message.
121
-	 *
122
-	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
123
-	 * @return $this
124
-	 */
125
-	public function setTo(array $recipients) {
126
-		$recipients = $this->convertAddresses($recipients);
127
-
128
-		$this->swiftMessage->setTo($recipients);
129
-		return $this;
130
-	}
131
-
132
-	/**
133
-	 * Get the to address of this message.
134
-	 *
135
-	 * @return array
136
-	 */
137
-	public function getTo() {
138
-		return $this->swiftMessage->getTo();
139
-	}
140
-
141
-	/**
142
-	 * Set the CC recipients of this message.
143
-	 *
144
-	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
145
-	 * @return $this
146
-	 */
147
-	public function setCc(array $recipients) {
148
-		$recipients = $this->convertAddresses($recipients);
149
-
150
-		$this->swiftMessage->setCc($recipients);
151
-		return $this;
152
-	}
153
-
154
-	/**
155
-	 * Get the cc address of this message.
156
-	 *
157
-	 * @return array
158
-	 */
159
-	public function getCc() {
160
-		return $this->swiftMessage->getCc();
161
-	}
162
-
163
-	/**
164
-	 * Set the BCC recipients of this message.
165
-	 *
166
-	 * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
167
-	 * @return $this
168
-	 */
169
-	public function setBcc(array $recipients) {
170
-		$recipients = $this->convertAddresses($recipients);
171
-
172
-		$this->swiftMessage->setBcc($recipients);
173
-		return $this;
174
-	}
175
-
176
-	/**
177
-	 * Get the Bcc address of this message.
178
-	 *
179
-	 * @return array
180
-	 */
181
-	public function getBcc() {
182
-		return $this->swiftMessage->getBcc();
183
-	}
184
-
185
-	/**
186
-	 * Set the subject of this message.
187
-	 *
188
-	 * @param $subject
189
-	 * @return $this
190
-	 */
191
-	public function setSubject($subject) {
192
-		$this->swiftMessage->setSubject($subject);
193
-		return $this;
194
-	}
195
-
196
-	/**
197
-	 * Get the from subject of this message.
198
-	 *
199
-	 * @return string
200
-	 */
201
-	public function getSubject() {
202
-		return $this->swiftMessage->getSubject();
203
-	}
204
-
205
-	/**
206
-	 * Set the plain-text body of this message.
207
-	 *
208
-	 * @param string $body
209
-	 * @return $this
210
-	 */
211
-	public function setPlainBody($body) {
212
-		$this->swiftMessage->setBody($body);
213
-		return $this;
214
-	}
215
-
216
-	/**
217
-	 * Get the plain body of this message.
218
-	 *
219
-	 * @return string
220
-	 */
221
-	public function getPlainBody() {
222
-		return $this->swiftMessage->getBody();
223
-	}
224
-
225
-	/**
226
-	 * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one.
227
-	 *
228
-	 * @param string $body
229
-	 * @return $this
230
-	 */
231
-	public function setHtmlBody($body) {
232
-		$this->swiftMessage->addPart($body, 'text/html');
233
-		return $this;
234
-	}
235
-
236
-	/**
237
-	 * Get's the underlying SwiftMessage
238
-	 * @return Swift_Message
239
-	 */
240
-	public function getSwiftMessage() {
241
-		return $this->swiftMessage;
242
-	}
243
-
244
-	/**
245
-	 * @param string $body
246
-	 * @param string $contentType
247
-	 * @return $this
248
-	 */
249
-	public function setBody($body, $contentType) {
250
-		$this->swiftMessage->setBody($body, $contentType);
251
-		return $this;
252
-	}
34
+    /** @var Swift_Message */
35
+    private $swiftMessage;
36
+
37
+    /**
38
+     * @param Swift_Message $swiftMessage
39
+     */
40
+    function __construct(Swift_Message $swiftMessage) {
41
+        $this->swiftMessage = $swiftMessage;
42
+    }
43
+
44
+    /**
45
+     * SwiftMailer does currently not work with IDN domains, this function therefore converts the domains
46
+     * FIXME: Remove this once SwiftMailer supports IDN
47
+     *
48
+     * @param array $addresses Array of mail addresses, key will get converted
49
+     * @return array Converted addresses if `idn_to_ascii` exists
50
+     */
51
+    protected function convertAddresses($addresses) {
52
+        if (!function_exists('idn_to_ascii')) {
53
+            return $addresses;
54
+        }
55
+
56
+        $convertedAddresses = array();
57
+
58
+        foreach($addresses as $email => $readableName) {
59
+            if(!is_numeric($email)) {
60
+                list($name, $domain) = explode('@', $email, 2);
61
+                $domain = idn_to_ascii($domain);
62
+                $convertedAddresses[$name.'@'.$domain] = $readableName;
63
+            } else {
64
+                list($name, $domain) = explode('@', $readableName, 2);
65
+                $domain = idn_to_ascii($domain);
66
+                $convertedAddresses[$email] = $name.'@'.$domain;
67
+            }
68
+        }
69
+
70
+        return $convertedAddresses;
71
+    }
72
+
73
+    /**
74
+     * Set the from address of this message.
75
+     *
76
+     * If no "From" address is used \OC\Mail\Mailer will use mail_from_address and mail_domain from config.php
77
+     *
78
+     * @param array $addresses Example: array('[email protected]', '[email protected]' => 'A name')
79
+     * @return $this
80
+     */
81
+    public function setFrom(array $addresses) {
82
+        $addresses = $this->convertAddresses($addresses);
83
+
84
+        $this->swiftMessage->setFrom($addresses);
85
+        return $this;
86
+    }
87
+
88
+    /**
89
+     * Get the from address of this message.
90
+     *
91
+     * @return array
92
+     */
93
+    public function getFrom() {
94
+        return $this->swiftMessage->getFrom();
95
+    }
96
+
97
+    /**
98
+     * Set the Reply-To address of this message
99
+     *
100
+     * @param array $addresses
101
+     * @return $this
102
+     */
103
+    public function setReplyTo(array $addresses) {
104
+        $addresses = $this->convertAddresses($addresses);
105
+
106
+        $this->swiftMessage->setReplyTo($addresses);
107
+        return $this;
108
+    }
109
+
110
+    /**
111
+     * Returns the Reply-To address of this message
112
+     *
113
+     * @return array
114
+     */
115
+    public function getReplyTo() {
116
+        return $this->swiftMessage->getReplyTo();
117
+    }
118
+
119
+    /**
120
+     * Set the to addresses of this message.
121
+     *
122
+     * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
123
+     * @return $this
124
+     */
125
+    public function setTo(array $recipients) {
126
+        $recipients = $this->convertAddresses($recipients);
127
+
128
+        $this->swiftMessage->setTo($recipients);
129
+        return $this;
130
+    }
131
+
132
+    /**
133
+     * Get the to address of this message.
134
+     *
135
+     * @return array
136
+     */
137
+    public function getTo() {
138
+        return $this->swiftMessage->getTo();
139
+    }
140
+
141
+    /**
142
+     * Set the CC recipients of this message.
143
+     *
144
+     * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
145
+     * @return $this
146
+     */
147
+    public function setCc(array $recipients) {
148
+        $recipients = $this->convertAddresses($recipients);
149
+
150
+        $this->swiftMessage->setCc($recipients);
151
+        return $this;
152
+    }
153
+
154
+    /**
155
+     * Get the cc address of this message.
156
+     *
157
+     * @return array
158
+     */
159
+    public function getCc() {
160
+        return $this->swiftMessage->getCc();
161
+    }
162
+
163
+    /**
164
+     * Set the BCC recipients of this message.
165
+     *
166
+     * @param array $recipients Example: array('[email protected]', '[email protected]' => 'A name')
167
+     * @return $this
168
+     */
169
+    public function setBcc(array $recipients) {
170
+        $recipients = $this->convertAddresses($recipients);
171
+
172
+        $this->swiftMessage->setBcc($recipients);
173
+        return $this;
174
+    }
175
+
176
+    /**
177
+     * Get the Bcc address of this message.
178
+     *
179
+     * @return array
180
+     */
181
+    public function getBcc() {
182
+        return $this->swiftMessage->getBcc();
183
+    }
184
+
185
+    /**
186
+     * Set the subject of this message.
187
+     *
188
+     * @param $subject
189
+     * @return $this
190
+     */
191
+    public function setSubject($subject) {
192
+        $this->swiftMessage->setSubject($subject);
193
+        return $this;
194
+    }
195
+
196
+    /**
197
+     * Get the from subject of this message.
198
+     *
199
+     * @return string
200
+     */
201
+    public function getSubject() {
202
+        return $this->swiftMessage->getSubject();
203
+    }
204
+
205
+    /**
206
+     * Set the plain-text body of this message.
207
+     *
208
+     * @param string $body
209
+     * @return $this
210
+     */
211
+    public function setPlainBody($body) {
212
+        $this->swiftMessage->setBody($body);
213
+        return $this;
214
+    }
215
+
216
+    /**
217
+     * Get the plain body of this message.
218
+     *
219
+     * @return string
220
+     */
221
+    public function getPlainBody() {
222
+        return $this->swiftMessage->getBody();
223
+    }
224
+
225
+    /**
226
+     * Set the HTML body of this message. Consider also sending a plain-text body instead of only an HTML one.
227
+     *
228
+     * @param string $body
229
+     * @return $this
230
+     */
231
+    public function setHtmlBody($body) {
232
+        $this->swiftMessage->addPart($body, 'text/html');
233
+        return $this;
234
+    }
235
+
236
+    /**
237
+     * Get's the underlying SwiftMessage
238
+     * @return Swift_Message
239
+     */
240
+    public function getSwiftMessage() {
241
+        return $this->swiftMessage;
242
+    }
243
+
244
+    /**
245
+     * @param string $body
246
+     * @param string $contentType
247
+     * @return $this
248
+     */
249
+    public function setBody($body, $contentType) {
250
+        $this->swiftMessage->setBody($body, $contentType);
251
+        return $this;
252
+    }
253 253
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -55,8 +55,8 @@
 block discarded – undo
55 55
 
56 56
 		$convertedAddresses = array();
57 57
 
58
-		foreach($addresses as $email => $readableName) {
59
-			if(!is_numeric($email)) {
58
+		foreach ($addresses as $email => $readableName) {
59
+			if (!is_numeric($email)) {
60 60
 				list($name, $domain) = explode('@', $email, 2);
61 61
 				$domain = idn_to_ascii($domain);
62 62
 				$convertedAddresses[$name.'@'.$domain] = $readableName;
Please login to merge, or discard this patch.
lib/private/Memcache/Memcached.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -155,7 +155,7 @@
 block discarded – undo
155 155
 	 * Set a value in the cache if it's not already stored
156 156
 	 *
157 157
 	 * @param string $key
158
-	 * @param mixed $value
158
+	 * @param integer $value
159 159
 	 * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
160 160
 	 * @return bool
161 161
 	 * @throws \Exception
Please login to merge, or discard this patch.
Indentation   +189 added lines, -189 removed lines patch added patch discarded remove patch
@@ -33,193 +33,193 @@
 block discarded – undo
33 33
 use OCP\IMemcache;
34 34
 
35 35
 class Memcached extends Cache implements IMemcache {
36
-	use CASTrait;
37
-
38
-	/**
39
-	 * @var \Memcached $cache
40
-	 */
41
-	private static $cache = null;
42
-
43
-	use CADTrait;
44
-
45
-	public function __construct($prefix = '') {
46
-		parent::__construct($prefix);
47
-		if (is_null(self::$cache)) {
48
-			self::$cache = new \Memcached();
49
-
50
-			$defaultOptions = [
51
-				\Memcached::OPT_CONNECT_TIMEOUT => 50,
52
-				\Memcached::OPT_RETRY_TIMEOUT =>   50,
53
-				\Memcached::OPT_SEND_TIMEOUT =>    50,
54
-				\Memcached::OPT_RECV_TIMEOUT =>    50,
55
-				\Memcached::OPT_POLL_TIMEOUT =>    50,
56
-
57
-				// Enable compression
58
-				\Memcached::OPT_COMPRESSION =>          true,
59
-
60
-				// Turn on consistent hashing
61
-				\Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
62
-
63
-				// Enable Binary Protocol
64
-				//\Memcached::OPT_BINARY_PROTOCOL =>      true,
65
-			];
66
-			// by default enable igbinary serializer if available
67
-			if (\Memcached::HAVE_IGBINARY) {
68
-				$defaultOptions[\Memcached::OPT_SERIALIZER] =
69
-					\Memcached::SERIALIZER_IGBINARY;
70
-			}
71
-			$options = \OC::$server->getConfig()->getSystemValue('memcached_options', []);
72
-			if (is_array($options)) {
73
-				$options = $options + $defaultOptions;
74
-				self::$cache->setOptions($options);
75
-			} else {
76
-				throw new HintException("Expected 'memcached_options' config to be an array, got $options");
77
-			}
78
-
79
-			$servers = \OC::$server->getSystemConfig()->getValue('memcached_servers');
80
-			if (!$servers) {
81
-				$server = \OC::$server->getSystemConfig()->getValue('memcached_server');
82
-				if ($server) {
83
-					$servers = [$server];
84
-				} else {
85
-					$servers = [['localhost', 11211]];
86
-				}
87
-			}
88
-			self::$cache->addServers($servers);
89
-		}
90
-	}
91
-
92
-	/**
93
-	 * entries in XCache gets namespaced to prevent collisions between owncloud instances and users
94
-	 */
95
-	protected function getNameSpace() {
96
-		return $this->prefix;
97
-	}
98
-
99
-	public function get($key) {
100
-		$result = self::$cache->get($this->getNamespace() . $key);
101
-		if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) {
102
-			return null;
103
-		} else {
104
-			return $result;
105
-		}
106
-	}
107
-
108
-	public function set($key, $value, $ttl = 0) {
109
-		if ($ttl > 0) {
110
-			$result =  self::$cache->set($this->getNamespace() . $key, $value, $ttl);
111
-		} else {
112
-			$result = self::$cache->set($this->getNamespace() . $key, $value);
113
-		}
114
-		if ($result !== true) {
115
-			$this->verifyReturnCode();
116
-		}
117
-		return $result;
118
-	}
119
-
120
-	public function hasKey($key) {
121
-		self::$cache->get($this->getNamespace() . $key);
122
-		return self::$cache->getResultCode() === \Memcached::RES_SUCCESS;
123
-	}
124
-
125
-	public function remove($key) {
126
-		$result= self::$cache->delete($this->getNamespace() . $key);
127
-		if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) {
128
-			$this->verifyReturnCode();
129
-		}
130
-		return $result;
131
-	}
132
-
133
-	public function clear($prefix = '') {
134
-		$prefix = $this->getNamespace() . $prefix;
135
-		$allKeys = self::$cache->getAllKeys();
136
-		if ($allKeys === false) {
137
-			// newer Memcached doesn't like getAllKeys(), flush everything
138
-			self::$cache->flush();
139
-			return true;
140
-		}
141
-		$keys = array();
142
-		$prefixLength = strlen($prefix);
143
-		foreach ($allKeys as $key) {
144
-			if (substr($key, 0, $prefixLength) === $prefix) {
145
-				$keys[] = $key;
146
-			}
147
-		}
148
-		if (method_exists(self::$cache, 'deleteMulti')) {
149
-			self::$cache->deleteMulti($keys);
150
-		} else {
151
-			foreach ($keys as $key) {
152
-				self::$cache->delete($key);
153
-			}
154
-		}
155
-		return true;
156
-	}
157
-
158
-	/**
159
-	 * Set a value in the cache if it's not already stored
160
-	 *
161
-	 * @param string $key
162
-	 * @param mixed $value
163
-	 * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
164
-	 * @return bool
165
-	 * @throws \Exception
166
-	 */
167
-	public function add($key, $value, $ttl = 0) {
168
-		$result = self::$cache->add($this->getPrefix() . $key, $value, $ttl);
169
-		if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) {
170
-			$this->verifyReturnCode();
171
-		}
172
-		return $result;
173
-	}
174
-
175
-	/**
176
-	 * Increase a stored number
177
-	 *
178
-	 * @param string $key
179
-	 * @param int $step
180
-	 * @return int | bool
181
-	 */
182
-	public function inc($key, $step = 1) {
183
-		$this->add($key, 0);
184
-		$result = self::$cache->increment($this->getPrefix() . $key, $step);
185
-
186
-		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
187
-			return false;
188
-		}
189
-
190
-		return $result;
191
-	}
192
-
193
-	/**
194
-	 * Decrease a stored number
195
-	 *
196
-	 * @param string $key
197
-	 * @param int $step
198
-	 * @return int | bool
199
-	 */
200
-	public function dec($key, $step = 1) {
201
-		$result = self::$cache->decrement($this->getPrefix() . $key, $step);
202
-
203
-		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
204
-			return false;
205
-		}
206
-
207
-		return $result;
208
-	}
209
-
210
-	static public function isAvailable() {
211
-		return extension_loaded('memcached');
212
-	}
213
-
214
-	/**
215
-	 * @throws \Exception
216
-	 */
217
-	private function verifyReturnCode() {
218
-		$code = self::$cache->getResultCode();
219
-		if ($code === \Memcached::RES_SUCCESS) {
220
-			return;
221
-		}
222
-		$message = self::$cache->getResultMessage();
223
-		throw new \Exception("Error $code interacting with memcached : $message");
224
-	}
36
+    use CASTrait;
37
+
38
+    /**
39
+     * @var \Memcached $cache
40
+     */
41
+    private static $cache = null;
42
+
43
+    use CADTrait;
44
+
45
+    public function __construct($prefix = '') {
46
+        parent::__construct($prefix);
47
+        if (is_null(self::$cache)) {
48
+            self::$cache = new \Memcached();
49
+
50
+            $defaultOptions = [
51
+                \Memcached::OPT_CONNECT_TIMEOUT => 50,
52
+                \Memcached::OPT_RETRY_TIMEOUT =>   50,
53
+                \Memcached::OPT_SEND_TIMEOUT =>    50,
54
+                \Memcached::OPT_RECV_TIMEOUT =>    50,
55
+                \Memcached::OPT_POLL_TIMEOUT =>    50,
56
+
57
+                // Enable compression
58
+                \Memcached::OPT_COMPRESSION =>          true,
59
+
60
+                // Turn on consistent hashing
61
+                \Memcached::OPT_LIBKETAMA_COMPATIBLE => true,
62
+
63
+                // Enable Binary Protocol
64
+                //\Memcached::OPT_BINARY_PROTOCOL =>      true,
65
+            ];
66
+            // by default enable igbinary serializer if available
67
+            if (\Memcached::HAVE_IGBINARY) {
68
+                $defaultOptions[\Memcached::OPT_SERIALIZER] =
69
+                    \Memcached::SERIALIZER_IGBINARY;
70
+            }
71
+            $options = \OC::$server->getConfig()->getSystemValue('memcached_options', []);
72
+            if (is_array($options)) {
73
+                $options = $options + $defaultOptions;
74
+                self::$cache->setOptions($options);
75
+            } else {
76
+                throw new HintException("Expected 'memcached_options' config to be an array, got $options");
77
+            }
78
+
79
+            $servers = \OC::$server->getSystemConfig()->getValue('memcached_servers');
80
+            if (!$servers) {
81
+                $server = \OC::$server->getSystemConfig()->getValue('memcached_server');
82
+                if ($server) {
83
+                    $servers = [$server];
84
+                } else {
85
+                    $servers = [['localhost', 11211]];
86
+                }
87
+            }
88
+            self::$cache->addServers($servers);
89
+        }
90
+    }
91
+
92
+    /**
93
+     * entries in XCache gets namespaced to prevent collisions between owncloud instances and users
94
+     */
95
+    protected function getNameSpace() {
96
+        return $this->prefix;
97
+    }
98
+
99
+    public function get($key) {
100
+        $result = self::$cache->get($this->getNamespace() . $key);
101
+        if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) {
102
+            return null;
103
+        } else {
104
+            return $result;
105
+        }
106
+    }
107
+
108
+    public function set($key, $value, $ttl = 0) {
109
+        if ($ttl > 0) {
110
+            $result =  self::$cache->set($this->getNamespace() . $key, $value, $ttl);
111
+        } else {
112
+            $result = self::$cache->set($this->getNamespace() . $key, $value);
113
+        }
114
+        if ($result !== true) {
115
+            $this->verifyReturnCode();
116
+        }
117
+        return $result;
118
+    }
119
+
120
+    public function hasKey($key) {
121
+        self::$cache->get($this->getNamespace() . $key);
122
+        return self::$cache->getResultCode() === \Memcached::RES_SUCCESS;
123
+    }
124
+
125
+    public function remove($key) {
126
+        $result= self::$cache->delete($this->getNamespace() . $key);
127
+        if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) {
128
+            $this->verifyReturnCode();
129
+        }
130
+        return $result;
131
+    }
132
+
133
+    public function clear($prefix = '') {
134
+        $prefix = $this->getNamespace() . $prefix;
135
+        $allKeys = self::$cache->getAllKeys();
136
+        if ($allKeys === false) {
137
+            // newer Memcached doesn't like getAllKeys(), flush everything
138
+            self::$cache->flush();
139
+            return true;
140
+        }
141
+        $keys = array();
142
+        $prefixLength = strlen($prefix);
143
+        foreach ($allKeys as $key) {
144
+            if (substr($key, 0, $prefixLength) === $prefix) {
145
+                $keys[] = $key;
146
+            }
147
+        }
148
+        if (method_exists(self::$cache, 'deleteMulti')) {
149
+            self::$cache->deleteMulti($keys);
150
+        } else {
151
+            foreach ($keys as $key) {
152
+                self::$cache->delete($key);
153
+            }
154
+        }
155
+        return true;
156
+    }
157
+
158
+    /**
159
+     * Set a value in the cache if it's not already stored
160
+     *
161
+     * @param string $key
162
+     * @param mixed $value
163
+     * @param int $ttl Time To Live in seconds. Defaults to 60*60*24
164
+     * @return bool
165
+     * @throws \Exception
166
+     */
167
+    public function add($key, $value, $ttl = 0) {
168
+        $result = self::$cache->add($this->getPrefix() . $key, $value, $ttl);
169
+        if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) {
170
+            $this->verifyReturnCode();
171
+        }
172
+        return $result;
173
+    }
174
+
175
+    /**
176
+     * Increase a stored number
177
+     *
178
+     * @param string $key
179
+     * @param int $step
180
+     * @return int | bool
181
+     */
182
+    public function inc($key, $step = 1) {
183
+        $this->add($key, 0);
184
+        $result = self::$cache->increment($this->getPrefix() . $key, $step);
185
+
186
+        if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
187
+            return false;
188
+        }
189
+
190
+        return $result;
191
+    }
192
+
193
+    /**
194
+     * Decrease a stored number
195
+     *
196
+     * @param string $key
197
+     * @param int $step
198
+     * @return int | bool
199
+     */
200
+    public function dec($key, $step = 1) {
201
+        $result = self::$cache->decrement($this->getPrefix() . $key, $step);
202
+
203
+        if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
204
+            return false;
205
+        }
206
+
207
+        return $result;
208
+    }
209
+
210
+    static public function isAvailable() {
211
+        return extension_loaded('memcached');
212
+    }
213
+
214
+    /**
215
+     * @throws \Exception
216
+     */
217
+    private function verifyReturnCode() {
218
+        $code = self::$cache->getResultCode();
219
+        if ($code === \Memcached::RES_SUCCESS) {
220
+            return;
221
+        }
222
+        $message = self::$cache->getResultMessage();
223
+        throw new \Exception("Error $code interacting with memcached : $message");
224
+    }
225 225
 }
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
 	}
98 98
 
99 99
 	public function get($key) {
100
-		$result = self::$cache->get($this->getNamespace() . $key);
100
+		$result = self::$cache->get($this->getNamespace().$key);
101 101
 		if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) {
102 102
 			return null;
103 103
 		} else {
@@ -107,9 +107,9 @@  discard block
 block discarded – undo
107 107
 
108 108
 	public function set($key, $value, $ttl = 0) {
109 109
 		if ($ttl > 0) {
110
-			$result =  self::$cache->set($this->getNamespace() . $key, $value, $ttl);
110
+			$result = self::$cache->set($this->getNamespace().$key, $value, $ttl);
111 111
 		} else {
112
-			$result = self::$cache->set($this->getNamespace() . $key, $value);
112
+			$result = self::$cache->set($this->getNamespace().$key, $value);
113 113
 		}
114 114
 		if ($result !== true) {
115 115
 			$this->verifyReturnCode();
@@ -118,12 +118,12 @@  discard block
 block discarded – undo
118 118
 	}
119 119
 
120 120
 	public function hasKey($key) {
121
-		self::$cache->get($this->getNamespace() . $key);
121
+		self::$cache->get($this->getNamespace().$key);
122 122
 		return self::$cache->getResultCode() === \Memcached::RES_SUCCESS;
123 123
 	}
124 124
 
125 125
 	public function remove($key) {
126
-		$result= self::$cache->delete($this->getNamespace() . $key);
126
+		$result = self::$cache->delete($this->getNamespace().$key);
127 127
 		if (self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND) {
128 128
 			$this->verifyReturnCode();
129 129
 		}
@@ -131,7 +131,7 @@  discard block
 block discarded – undo
131 131
 	}
132 132
 
133 133
 	public function clear($prefix = '') {
134
-		$prefix = $this->getNamespace() . $prefix;
134
+		$prefix = $this->getNamespace().$prefix;
135 135
 		$allKeys = self::$cache->getAllKeys();
136 136
 		if ($allKeys === false) {
137 137
 			// newer Memcached doesn't like getAllKeys(), flush everything
@@ -165,7 +165,7 @@  discard block
 block discarded – undo
165 165
 	 * @throws \Exception
166 166
 	 */
167 167
 	public function add($key, $value, $ttl = 0) {
168
-		$result = self::$cache->add($this->getPrefix() . $key, $value, $ttl);
168
+		$result = self::$cache->add($this->getPrefix().$key, $value, $ttl);
169 169
 		if (self::$cache->getResultCode() !== \Memcached::RES_NOTSTORED) {
170 170
 			$this->verifyReturnCode();
171 171
 		}
@@ -181,7 +181,7 @@  discard block
 block discarded – undo
181 181
 	 */
182 182
 	public function inc($key, $step = 1) {
183 183
 		$this->add($key, 0);
184
-		$result = self::$cache->increment($this->getPrefix() . $key, $step);
184
+		$result = self::$cache->increment($this->getPrefix().$key, $step);
185 185
 
186 186
 		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
187 187
 			return false;
@@ -198,7 +198,7 @@  discard block
 block discarded – undo
198 198
 	 * @return int | bool
199 199
 	 */
200 200
 	public function dec($key, $step = 1) {
201
-		$result = self::$cache->decrement($this->getPrefix() . $key, $step);
201
+		$result = self::$cache->decrement($this->getPrefix().$key, $step);
202 202
 
203 203
 		if (self::$cache->getResultCode() !== \Memcached::RES_SUCCESS) {
204 204
 			return false;
Please login to merge, or discard this patch.
lib/private/Repair.php 3 patches
Doc Comments   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -139,7 +139,7 @@  discard block
 block discarded – undo
139 139
 	 * Returns expensive repair steps to be run on the
140 140
 	 * command line with a special option.
141 141
 	 *
142
-	 * @return IRepairStep[]
142
+	 * @return OldGroupMembershipShares[]
143 143
 	 */
144 144
 	public static function getExpensiveRepairSteps() {
145 145
 		return [
@@ -216,7 +216,6 @@  discard block
 block discarded – undo
216 216
 	}
217 217
 
218 218
 	/**
219
-	 * @param int $max
220 219
 	 */
221 220
 	public function finishProgress() {
222 221
 		// for now just emit as we did in the past
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -58,7 +58,7 @@
 block discarded – undo
58 58
 use Symfony\Component\EventDispatcher\EventDispatcher;
59 59
 use Symfony\Component\EventDispatcher\GenericEvent;
60 60
 
61
-class Repair implements IOutput{
61
+class Repair implements IOutput {
62 62
 	/* @var IRepairStep[] */
63 63
 	private $repairSteps;
64 64
 	/** @var EventDispatcher */
Please login to merge, or discard this patch.
Indentation   +167 added lines, -167 removed lines patch added patch discarded remove patch
@@ -49,171 +49,171 @@
 block discarded – undo
49 49
 use Symfony\Component\EventDispatcher\GenericEvent;
50 50
 
51 51
 class Repair implements IOutput{
52
-	/* @var IRepairStep[] */
53
-	private $repairSteps;
54
-	/** @var EventDispatcher */
55
-	private $dispatcher;
56
-	/** @var string */
57
-	private $currentStep;
58
-
59
-	/**
60
-	 * Creates a new repair step runner
61
-	 *
62
-	 * @param IRepairStep[] $repairSteps array of RepairStep instances
63
-	 * @param EventDispatcher $dispatcher
64
-	 */
65
-	public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
66
-		$this->repairSteps = $repairSteps;
67
-		$this->dispatcher = $dispatcher;
68
-	}
69
-
70
-	/**
71
-	 * Run a series of repair steps for common problems
72
-	 */
73
-	public function run() {
74
-		if (count($this->repairSteps) === 0) {
75
-			$this->emit('\OC\Repair', 'info', array('No repair steps available'));
76
-			return;
77
-		}
78
-		// run each repair step
79
-		foreach ($this->repairSteps as $step) {
80
-			$this->currentStep = $step->getName();
81
-			$this->emit('\OC\Repair', 'step', [$this->currentStep]);
82
-			$step->run($this);
83
-		}
84
-	}
85
-
86
-	/**
87
-	 * Add repair step
88
-	 *
89
-	 * @param IRepairStep|string $repairStep repair step
90
-	 * @throws \Exception
91
-	 */
92
-	public function addStep($repairStep) {
93
-		if (is_string($repairStep)) {
94
-			try {
95
-				$s = \OC::$server->query($repairStep);
96
-			} catch (QueryException $e) {
97
-				if (class_exists($repairStep)) {
98
-					$s = new $repairStep();
99
-				} else {
100
-					throw new \Exception("Repair step '$repairStep' is unknown");
101
-				}
102
-			}
103
-
104
-			if ($s instanceof IRepairStep) {
105
-				$this->repairSteps[] = $s;
106
-			} else {
107
-				throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
108
-			}
109
-		} else {
110
-			$this->repairSteps[] = $repairStep;
111
-		}
112
-	}
113
-
114
-	/**
115
-	 * Returns the default repair steps to be run on the
116
-	 * command line or after an upgrade.
117
-	 *
118
-	 * @return IRepairStep[]
119
-	 */
120
-	public static function getRepairSteps() {
121
-		return [
122
-			new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
123
-			new RepairMimeTypes(\OC::$server->getConfig()),
124
-			new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
125
-			new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
126
-			new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
127
-			new MoveUpdaterStepFile(\OC::$server->getConfig()),
128
-			new MoveAvatars(
129
-				\OC::$server->getJobList(),
130
-				\OC::$server->getConfig()
131
-			),
132
-			new CleanPreviews(
133
-				\OC::$server->getJobList(),
134
-				\OC::$server->getUserManager(),
135
-				\OC::$server->getConfig()
136
-			),
137
-			new FixMountStorages(\OC::$server->getDatabaseConnection()),
138
-			new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
139
-		];
140
-	}
141
-
142
-	/**
143
-	 * Returns expensive repair steps to be run on the
144
-	 * command line with a special option.
145
-	 *
146
-	 * @return IRepairStep[]
147
-	 */
148
-	public static function getExpensiveRepairSteps() {
149
-		return [
150
-			new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()),
151
-		];
152
-	}
153
-
154
-	/**
155
-	 * Returns the repair steps to be run before an
156
-	 * upgrade.
157
-	 *
158
-	 * @return IRepairStep[]
159
-	 */
160
-	public static function getBeforeUpgradeRepairSteps() {
161
-		$connection = \OC::$server->getDatabaseConnection();
162
-		$steps = [
163
-			new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
164
-			new SqliteAutoincrement($connection),
165
-		];
166
-
167
-		return $steps;
168
-	}
169
-
170
-	/**
171
-	 * @param string $scope
172
-	 * @param string $method
173
-	 * @param array $arguments
174
-	 */
175
-	public function emit($scope, $method, array $arguments = []) {
176
-		if (!is_null($this->dispatcher)) {
177
-			$this->dispatcher->dispatch("$scope::$method",
178
-				new GenericEvent("$scope::$method", $arguments));
179
-		}
180
-	}
181
-
182
-	public function info($string) {
183
-		// for now just emit as we did in the past
184
-		$this->emit('\OC\Repair', 'info', array($string));
185
-	}
186
-
187
-	/**
188
-	 * @param string $message
189
-	 */
190
-	public function warning($message) {
191
-		// for now just emit as we did in the past
192
-		$this->emit('\OC\Repair', 'warning', [$message]);
193
-	}
194
-
195
-	/**
196
-	 * @param int $max
197
-	 */
198
-	public function startProgress($max = 0) {
199
-		// for now just emit as we did in the past
200
-		$this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
201
-	}
202
-
203
-	/**
204
-	 * @param int $step
205
-	 * @param string $description
206
-	 */
207
-	public function advance($step = 1, $description = '') {
208
-		// for now just emit as we did in the past
209
-		$this->emit('\OC\Repair', 'advance', [$step, $description]);
210
-	}
211
-
212
-	/**
213
-	 * @param int $max
214
-	 */
215
-	public function finishProgress() {
216
-		// for now just emit as we did in the past
217
-		$this->emit('\OC\Repair', 'finishProgress', []);
218
-	}
52
+    /* @var IRepairStep[] */
53
+    private $repairSteps;
54
+    /** @var EventDispatcher */
55
+    private $dispatcher;
56
+    /** @var string */
57
+    private $currentStep;
58
+
59
+    /**
60
+     * Creates a new repair step runner
61
+     *
62
+     * @param IRepairStep[] $repairSteps array of RepairStep instances
63
+     * @param EventDispatcher $dispatcher
64
+     */
65
+    public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
66
+        $this->repairSteps = $repairSteps;
67
+        $this->dispatcher = $dispatcher;
68
+    }
69
+
70
+    /**
71
+     * Run a series of repair steps for common problems
72
+     */
73
+    public function run() {
74
+        if (count($this->repairSteps) === 0) {
75
+            $this->emit('\OC\Repair', 'info', array('No repair steps available'));
76
+            return;
77
+        }
78
+        // run each repair step
79
+        foreach ($this->repairSteps as $step) {
80
+            $this->currentStep = $step->getName();
81
+            $this->emit('\OC\Repair', 'step', [$this->currentStep]);
82
+            $step->run($this);
83
+        }
84
+    }
85
+
86
+    /**
87
+     * Add repair step
88
+     *
89
+     * @param IRepairStep|string $repairStep repair step
90
+     * @throws \Exception
91
+     */
92
+    public function addStep($repairStep) {
93
+        if (is_string($repairStep)) {
94
+            try {
95
+                $s = \OC::$server->query($repairStep);
96
+            } catch (QueryException $e) {
97
+                if (class_exists($repairStep)) {
98
+                    $s = new $repairStep();
99
+                } else {
100
+                    throw new \Exception("Repair step '$repairStep' is unknown");
101
+                }
102
+            }
103
+
104
+            if ($s instanceof IRepairStep) {
105
+                $this->repairSteps[] = $s;
106
+            } else {
107
+                throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
108
+            }
109
+        } else {
110
+            $this->repairSteps[] = $repairStep;
111
+        }
112
+    }
113
+
114
+    /**
115
+     * Returns the default repair steps to be run on the
116
+     * command line or after an upgrade.
117
+     *
118
+     * @return IRepairStep[]
119
+     */
120
+    public static function getRepairSteps() {
121
+        return [
122
+            new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
123
+            new RepairMimeTypes(\OC::$server->getConfig()),
124
+            new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
125
+            new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
126
+            new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
127
+            new MoveUpdaterStepFile(\OC::$server->getConfig()),
128
+            new MoveAvatars(
129
+                \OC::$server->getJobList(),
130
+                \OC::$server->getConfig()
131
+            ),
132
+            new CleanPreviews(
133
+                \OC::$server->getJobList(),
134
+                \OC::$server->getUserManager(),
135
+                \OC::$server->getConfig()
136
+            ),
137
+            new FixMountStorages(\OC::$server->getDatabaseConnection()),
138
+            new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
139
+        ];
140
+    }
141
+
142
+    /**
143
+     * Returns expensive repair steps to be run on the
144
+     * command line with a special option.
145
+     *
146
+     * @return IRepairStep[]
147
+     */
148
+    public static function getExpensiveRepairSteps() {
149
+        return [
150
+            new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()),
151
+        ];
152
+    }
153
+
154
+    /**
155
+     * Returns the repair steps to be run before an
156
+     * upgrade.
157
+     *
158
+     * @return IRepairStep[]
159
+     */
160
+    public static function getBeforeUpgradeRepairSteps() {
161
+        $connection = \OC::$server->getDatabaseConnection();
162
+        $steps = [
163
+            new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
164
+            new SqliteAutoincrement($connection),
165
+        ];
166
+
167
+        return $steps;
168
+    }
169
+
170
+    /**
171
+     * @param string $scope
172
+     * @param string $method
173
+     * @param array $arguments
174
+     */
175
+    public function emit($scope, $method, array $arguments = []) {
176
+        if (!is_null($this->dispatcher)) {
177
+            $this->dispatcher->dispatch("$scope::$method",
178
+                new GenericEvent("$scope::$method", $arguments));
179
+        }
180
+    }
181
+
182
+    public function info($string) {
183
+        // for now just emit as we did in the past
184
+        $this->emit('\OC\Repair', 'info', array($string));
185
+    }
186
+
187
+    /**
188
+     * @param string $message
189
+     */
190
+    public function warning($message) {
191
+        // for now just emit as we did in the past
192
+        $this->emit('\OC\Repair', 'warning', [$message]);
193
+    }
194
+
195
+    /**
196
+     * @param int $max
197
+     */
198
+    public function startProgress($max = 0) {
199
+        // for now just emit as we did in the past
200
+        $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
201
+    }
202
+
203
+    /**
204
+     * @param int $step
205
+     * @param string $description
206
+     */
207
+    public function advance($step = 1, $description = '') {
208
+        // for now just emit as we did in the past
209
+        $this->emit('\OC\Repair', 'advance', [$step, $description]);
210
+    }
211
+
212
+    /**
213
+     * @param int $max
214
+     */
215
+    public function finishProgress() {
216
+        // for now just emit as we did in the past
217
+        $this->emit('\OC\Repair', 'finishProgress', []);
218
+    }
219 219
 }
Please login to merge, or discard this patch.
lib/private/Security/CredentialsManager.php 2 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -24,7 +24,6 @@
 block discarded – undo
24 24
 use OCP\Security\ICrypto;
25 25
 use OCP\IDBConnection;
26 26
 use OCP\Security\ICredentialsManager;
27
-use OCP\IConfig;
28 27
 
29 28
 /**
30 29
  * Store and retrieve credentials for external services
Please login to merge, or discard this patch.
Indentation   +78 added lines, -78 removed lines patch added patch discarded remove patch
@@ -34,93 +34,93 @@
 block discarded – undo
34 34
  */
35 35
 class CredentialsManager implements ICredentialsManager {
36 36
 
37
-	const DB_TABLE = 'credentials';
37
+    const DB_TABLE = 'credentials';
38 38
 
39
-	/** @var ICrypto */
40
-	protected $crypto;
39
+    /** @var ICrypto */
40
+    protected $crypto;
41 41
 
42
-	/** @var IDBConnection */
43
-	protected $dbConnection;
42
+    /** @var IDBConnection */
43
+    protected $dbConnection;
44 44
 
45
-	/**
46
-	 * @param ICrypto $crypto
47
-	 * @param IDBConnection $dbConnection
48
-	 */
49
-	public function __construct(ICrypto $crypto, IDBConnection $dbConnection) {
50
-		$this->crypto = $crypto;
51
-		$this->dbConnection = $dbConnection;
52
-	}
45
+    /**
46
+     * @param ICrypto $crypto
47
+     * @param IDBConnection $dbConnection
48
+     */
49
+    public function __construct(ICrypto $crypto, IDBConnection $dbConnection) {
50
+        $this->crypto = $crypto;
51
+        $this->dbConnection = $dbConnection;
52
+    }
53 53
 
54
-	/**
55
-	 * Store a set of credentials
56
-	 *
57
-	 * @param string|null $userId Null for system-wide credentials
58
-	 * @param string $identifier
59
-	 * @param mixed $credentials
60
-	 */
61
-	public function store($userId, $identifier, $credentials) {
62
-		$value = $this->crypto->encrypt(json_encode($credentials));
54
+    /**
55
+     * Store a set of credentials
56
+     *
57
+     * @param string|null $userId Null for system-wide credentials
58
+     * @param string $identifier
59
+     * @param mixed $credentials
60
+     */
61
+    public function store($userId, $identifier, $credentials) {
62
+        $value = $this->crypto->encrypt(json_encode($credentials));
63 63
 
64
-		$this->dbConnection->setValues(self::DB_TABLE, [
65
-			'user' => $userId,
66
-			'identifier' => $identifier,
67
-		], [
68
-			'credentials' => $value,
69
-		]);
70
-	}
64
+        $this->dbConnection->setValues(self::DB_TABLE, [
65
+            'user' => $userId,
66
+            'identifier' => $identifier,
67
+        ], [
68
+            'credentials' => $value,
69
+        ]);
70
+    }
71 71
 
72
-	/**
73
-	 * Retrieve a set of credentials
74
-	 *
75
-	 * @param string|null $userId Null for system-wide credentials
76
-	 * @param string $identifier
77
-	 * @return mixed
78
-	 */
79
-	public function retrieve($userId, $identifier) {
80
-		$qb = $this->dbConnection->getQueryBuilder();
81
-		$qb->select('credentials')
82
-			->from(self::DB_TABLE)
83
-			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
84
-			->andWhere($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier)))
85
-		;
86
-		$result = $qb->execute()->fetch();
72
+    /**
73
+     * Retrieve a set of credentials
74
+     *
75
+     * @param string|null $userId Null for system-wide credentials
76
+     * @param string $identifier
77
+     * @return mixed
78
+     */
79
+    public function retrieve($userId, $identifier) {
80
+        $qb = $this->dbConnection->getQueryBuilder();
81
+        $qb->select('credentials')
82
+            ->from(self::DB_TABLE)
83
+            ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
84
+            ->andWhere($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier)))
85
+        ;
86
+        $result = $qb->execute()->fetch();
87 87
 
88
-		if (!$result) {
89
-			return null;
90
-		}
91
-		$value = $result['credentials'];
88
+        if (!$result) {
89
+            return null;
90
+        }
91
+        $value = $result['credentials'];
92 92
 
93
-		return json_decode($this->crypto->decrypt($value), true);
94
-	}
93
+        return json_decode($this->crypto->decrypt($value), true);
94
+    }
95 95
 
96
-	/**
97
-	 * Delete a set of credentials
98
-	 *
99
-	 * @param string|null $userId Null for system-wide credentials
100
-	 * @param string $identifier
101
-	 * @return int rows removed
102
-	 */
103
-	public function delete($userId, $identifier) {
104
-		$qb = $this->dbConnection->getQueryBuilder();
105
-		$qb->delete(self::DB_TABLE)
106
-			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
107
-			->andWhere($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier)))
108
-		;
109
-		return $qb->execute();
110
-	}
96
+    /**
97
+     * Delete a set of credentials
98
+     *
99
+     * @param string|null $userId Null for system-wide credentials
100
+     * @param string $identifier
101
+     * @return int rows removed
102
+     */
103
+    public function delete($userId, $identifier) {
104
+        $qb = $this->dbConnection->getQueryBuilder();
105
+        $qb->delete(self::DB_TABLE)
106
+            ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
107
+            ->andWhere($qb->expr()->eq('identifier', $qb->createNamedParameter($identifier)))
108
+        ;
109
+        return $qb->execute();
110
+    }
111 111
 
112
-	/**
113
-	 * Erase all credentials stored for a user
114
-	 *
115
-	 * @param string $userId
116
-	 * @return int rows removed
117
-	 */
118
-	public function erase($userId) {
119
-		$qb = $this->dbConnection->getQueryBuilder();
120
-		$qb->delete(self::DB_TABLE)
121
-			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
122
-		;
123
-		return $qb->execute();
124
-	}
112
+    /**
113
+     * Erase all credentials stored for a user
114
+     *
115
+     * @param string $userId
116
+     * @return int rows removed
117
+     */
118
+    public function erase($userId) {
119
+        $qb = $this->dbConnection->getQueryBuilder();
120
+        $qb->delete(self::DB_TABLE)
121
+            ->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
122
+        ;
123
+        return $qb->execute();
124
+    }
125 125
 
126 126
 }
Please login to merge, or discard this patch.
lib/private/Session/CryptoSessionData.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -176,7 +176,7 @@
 block discarded – undo
176 176
 
177 177
 	/**
178 178
 	 * @param mixed $offset
179
-	 * @return mixed
179
+	 * @return string|null
180 180
 	 */
181 181
 	public function offsetGet($offset) {
182 182
 		return $this->get($offset);
Please login to merge, or discard this patch.
Indentation   +163 added lines, -163 removed lines patch added patch discarded remove patch
@@ -34,167 +34,167 @@
 block discarded – undo
34 34
  * @package OC\Session
35 35
  */
36 36
 class CryptoSessionData implements \ArrayAccess, ISession {
37
-	/** @var ISession */
38
-	protected $session;
39
-	/** @var \OCP\Security\ICrypto */
40
-	protected $crypto;
41
-	/** @var string */
42
-	protected $passphrase;
43
-	/** @var array */
44
-	protected $sessionValues;
45
-	/** @var bool */
46
-	protected $isModified = false;
47
-	CONST encryptedSessionName = 'encrypted_session_data';
48
-
49
-	/**
50
-	 * @param ISession $session
51
-	 * @param ICrypto $crypto
52
-	 * @param string $passphrase
53
-	 */
54
-	public function __construct(ISession $session,
55
-								ICrypto $crypto,
56
-								$passphrase) {
57
-		$this->crypto = $crypto;
58
-		$this->session = $session;
59
-		$this->passphrase = $passphrase;
60
-		$this->initializeSession();
61
-	}
62
-
63
-	/**
64
-	 * Close session if class gets destructed
65
-	 */
66
-	public function __destruct() {
67
-		$this->close();
68
-	}
69
-
70
-	protected function initializeSession() {
71
-		$encryptedSessionData = $this->session->get(self::encryptedSessionName);
72
-		try {
73
-			$this->sessionValues = json_decode(
74
-				$this->crypto->decrypt($encryptedSessionData, $this->passphrase),
75
-				true
76
-			);
77
-		} catch (\Exception $e) {
78
-			$this->sessionValues = [];
79
-		}
80
-	}
81
-
82
-	/**
83
-	 * Set a value in the session
84
-	 *
85
-	 * @param string $key
86
-	 * @param mixed $value
87
-	 */
88
-	public function set($key, $value) {
89
-		$this->sessionValues[$key] = $value;
90
-		$this->isModified = true;
91
-	}
92
-
93
-	/**
94
-	 * Get a value from the session
95
-	 *
96
-	 * @param string $key
97
-	 * @return string|null Either the value or null
98
-	 */
99
-	public function get($key) {
100
-		if(isset($this->sessionValues[$key])) {
101
-			return $this->sessionValues[$key];
102
-		}
103
-
104
-		return null;
105
-	}
106
-
107
-	/**
108
-	 * Check if a named key exists in the session
109
-	 *
110
-	 * @param string $key
111
-	 * @return bool
112
-	 */
113
-	public function exists($key) {
114
-		return isset($this->sessionValues[$key]);
115
-	}
116
-
117
-	/**
118
-	 * Remove a $key/$value pair from the session
119
-	 *
120
-	 * @param string $key
121
-	 */
122
-	public function remove($key) {
123
-		$this->isModified = true;
124
-		unset($this->sessionValues[$key]);
125
-		$this->session->remove(self::encryptedSessionName);
126
-	}
127
-
128
-	/**
129
-	 * Reset and recreate the session
130
-	 */
131
-	public function clear() {
132
-		$this->sessionValues = [];
133
-		$this->isModified = true;
134
-		$this->session->clear();
135
-	}
136
-
137
-	/**
138
-	 * Wrapper around session_regenerate_id
139
-	 *
140
-	 * @param bool $deleteOldSession Whether to delete the old associated session file or not.
141
-	 * @return void
142
-	 */
143
-	public function regenerateId($deleteOldSession = true) {
144
-		$this->session->regenerateId($deleteOldSession);
145
-	}
146
-
147
-	/**
148
-	 * Wrapper around session_id
149
-	 *
150
-	 * @return string
151
-	 * @throws SessionNotAvailableException
152
-	 * @since 9.1.0
153
-	 */
154
-	public function getId() {
155
-		return $this->session->getId();
156
-	}
157
-
158
-	/**
159
-	 * Close the session and release the lock, also writes all changed data in batch
160
-	 */
161
-	public function close() {
162
-		if($this->isModified) {
163
-			$encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
164
-			$this->session->set(self::encryptedSessionName, $encryptedValue);
165
-			$this->isModified = false;
166
-		}
167
-		$this->session->close();
168
-	}
169
-
170
-	/**
171
-	 * @param mixed $offset
172
-	 * @return bool
173
-	 */
174
-	public function offsetExists($offset) {
175
-		return $this->exists($offset);
176
-	}
177
-
178
-	/**
179
-	 * @param mixed $offset
180
-	 * @return mixed
181
-	 */
182
-	public function offsetGet($offset) {
183
-		return $this->get($offset);
184
-	}
185
-
186
-	/**
187
-	 * @param mixed $offset
188
-	 * @param mixed $value
189
-	 */
190
-	public function offsetSet($offset, $value) {
191
-		$this->set($offset, $value);
192
-	}
193
-
194
-	/**
195
-	 * @param mixed $offset
196
-	 */
197
-	public function offsetUnset($offset) {
198
-		$this->remove($offset);
199
-	}
37
+    /** @var ISession */
38
+    protected $session;
39
+    /** @var \OCP\Security\ICrypto */
40
+    protected $crypto;
41
+    /** @var string */
42
+    protected $passphrase;
43
+    /** @var array */
44
+    protected $sessionValues;
45
+    /** @var bool */
46
+    protected $isModified = false;
47
+    CONST encryptedSessionName = 'encrypted_session_data';
48
+
49
+    /**
50
+     * @param ISession $session
51
+     * @param ICrypto $crypto
52
+     * @param string $passphrase
53
+     */
54
+    public function __construct(ISession $session,
55
+                                ICrypto $crypto,
56
+                                $passphrase) {
57
+        $this->crypto = $crypto;
58
+        $this->session = $session;
59
+        $this->passphrase = $passphrase;
60
+        $this->initializeSession();
61
+    }
62
+
63
+    /**
64
+     * Close session if class gets destructed
65
+     */
66
+    public function __destruct() {
67
+        $this->close();
68
+    }
69
+
70
+    protected function initializeSession() {
71
+        $encryptedSessionData = $this->session->get(self::encryptedSessionName);
72
+        try {
73
+            $this->sessionValues = json_decode(
74
+                $this->crypto->decrypt($encryptedSessionData, $this->passphrase),
75
+                true
76
+            );
77
+        } catch (\Exception $e) {
78
+            $this->sessionValues = [];
79
+        }
80
+    }
81
+
82
+    /**
83
+     * Set a value in the session
84
+     *
85
+     * @param string $key
86
+     * @param mixed $value
87
+     */
88
+    public function set($key, $value) {
89
+        $this->sessionValues[$key] = $value;
90
+        $this->isModified = true;
91
+    }
92
+
93
+    /**
94
+     * Get a value from the session
95
+     *
96
+     * @param string $key
97
+     * @return string|null Either the value or null
98
+     */
99
+    public function get($key) {
100
+        if(isset($this->sessionValues[$key])) {
101
+            return $this->sessionValues[$key];
102
+        }
103
+
104
+        return null;
105
+    }
106
+
107
+    /**
108
+     * Check if a named key exists in the session
109
+     *
110
+     * @param string $key
111
+     * @return bool
112
+     */
113
+    public function exists($key) {
114
+        return isset($this->sessionValues[$key]);
115
+    }
116
+
117
+    /**
118
+     * Remove a $key/$value pair from the session
119
+     *
120
+     * @param string $key
121
+     */
122
+    public function remove($key) {
123
+        $this->isModified = true;
124
+        unset($this->sessionValues[$key]);
125
+        $this->session->remove(self::encryptedSessionName);
126
+    }
127
+
128
+    /**
129
+     * Reset and recreate the session
130
+     */
131
+    public function clear() {
132
+        $this->sessionValues = [];
133
+        $this->isModified = true;
134
+        $this->session->clear();
135
+    }
136
+
137
+    /**
138
+     * Wrapper around session_regenerate_id
139
+     *
140
+     * @param bool $deleteOldSession Whether to delete the old associated session file or not.
141
+     * @return void
142
+     */
143
+    public function regenerateId($deleteOldSession = true) {
144
+        $this->session->regenerateId($deleteOldSession);
145
+    }
146
+
147
+    /**
148
+     * Wrapper around session_id
149
+     *
150
+     * @return string
151
+     * @throws SessionNotAvailableException
152
+     * @since 9.1.0
153
+     */
154
+    public function getId() {
155
+        return $this->session->getId();
156
+    }
157
+
158
+    /**
159
+     * Close the session and release the lock, also writes all changed data in batch
160
+     */
161
+    public function close() {
162
+        if($this->isModified) {
163
+            $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
164
+            $this->session->set(self::encryptedSessionName, $encryptedValue);
165
+            $this->isModified = false;
166
+        }
167
+        $this->session->close();
168
+    }
169
+
170
+    /**
171
+     * @param mixed $offset
172
+     * @return bool
173
+     */
174
+    public function offsetExists($offset) {
175
+        return $this->exists($offset);
176
+    }
177
+
178
+    /**
179
+     * @param mixed $offset
180
+     * @return mixed
181
+     */
182
+    public function offsetGet($offset) {
183
+        return $this->get($offset);
184
+    }
185
+
186
+    /**
187
+     * @param mixed $offset
188
+     * @param mixed $value
189
+     */
190
+    public function offsetSet($offset, $value) {
191
+        $this->set($offset, $value);
192
+    }
193
+
194
+    /**
195
+     * @param mixed $offset
196
+     */
197
+    public function offsetUnset($offset) {
198
+        $this->remove($offset);
199
+    }
200 200
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -97,7 +97,7 @@  discard block
 block discarded – undo
97 97
 	 * @return string|null Either the value or null
98 98
 	 */
99 99
 	public function get($key) {
100
-		if(isset($this->sessionValues[$key])) {
100
+		if (isset($this->sessionValues[$key])) {
101 101
 			return $this->sessionValues[$key];
102 102
 		}
103 103
 
@@ -159,7 +159,7 @@  discard block
 block discarded – undo
159 159
 	 * Close the session and release the lock, also writes all changed data in batch
160 160
 	 */
161 161
 	public function close() {
162
-		if($this->isModified) {
162
+		if ($this->isModified) {
163 163
 			$encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase);
164 164
 			$this->session->set(self::encryptedSessionName, $encryptedValue);
165 165
 			$this->isModified = false;
Please login to merge, or discard this patch.
lib/private/SystemTag/SystemTagManager.php 3 patches
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -30,7 +30,6 @@
 block discarded – undo
30 30
 use OCP\SystemTag\TagAlreadyExistsException;
31 31
 use OCP\SystemTag\TagNotFoundException;
32 32
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
33
-use OCP\IUserManager;
34 33
 use OCP\IGroupManager;
35 34
 use OCP\SystemTag\ISystemTag;
36 35
 use OCP\IUser;
Please login to merge, or discard this patch.
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -136,14 +136,14 @@  discard block
 block discarded – undo
136 136
 			->from(self::TAG_TABLE);
137 137
 
138 138
 		if (!is_null($visibilityFilter)) {
139
-			$query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int)$visibilityFilter)));
139
+			$query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int) $visibilityFilter)));
140 140
 		}
141 141
 
142 142
 		if (!empty($nameSearchPattern)) {
143 143
 			$query->andWhere(
144 144
 				$query->expr()->like(
145 145
 					'name',
146
-					$query->createNamedParameter('%' . $this->connection->escapeLikeParameter($nameSearchPattern). '%')
146
+					$query->createNamedParameter('%'.$this->connection->escapeLikeParameter($nameSearchPattern).'%')
147 147
 				)
148 148
 			);
149 149
 		}
@@ -167,8 +167,8 @@  discard block
 block discarded – undo
167 167
 	 * {@inheritdoc}
168 168
 	 */
169 169
 	public function getTag($tagName, $userVisible, $userAssignable) {
170
-		$userVisible = (int)$userVisible;
171
-		$userAssignable = (int)$userAssignable;
170
+		$userVisible = (int) $userVisible;
171
+		$userAssignable = (int) $userAssignable;
172 172
 
173 173
 		$result = $this->selectTagQuery
174 174
 			->setParameter('name', $tagName)
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
 		$result->closeCursor();
181 181
 		if (!$row) {
182 182
 			throw new TagNotFoundException(
183
-				'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') does not exist'
183
+				'Tag ("'.$tagName.'", '.$userVisible.', '.$userAssignable.') does not exist'
184 184
 			);
185 185
 		}
186 186
 
@@ -191,8 +191,8 @@  discard block
 block discarded – undo
191 191
 	 * {@inheritdoc}
192 192
 	 */
193 193
 	public function createTag($tagName, $userVisible, $userAssignable) {
194
-		$userVisible = (int)$userVisible;
195
-		$userAssignable = (int)$userAssignable;
194
+		$userVisible = (int) $userVisible;
195
+		$userAssignable = (int) $userAssignable;
196 196
 
197 197
 		$query = $this->connection->getQueryBuilder();
198 198
 		$query->insert(self::TAG_TABLE)
@@ -206,7 +206,7 @@  discard block
 block discarded – undo
206 206
 			$query->execute();
207 207
 		} catch (UniqueConstraintViolationException $e) {
208 208
 			throw new TagAlreadyExistsException(
209
-				'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists',
209
+				'Tag ("'.$tagName.'", '.$userVisible.', '.$userAssignable.') already exists',
210 210
 				0,
211 211
 				$e
212 212
 			);
@@ -215,10 +215,10 @@  discard block
 block discarded – undo
215 215
 		$tagId = $query->getLastInsertId();
216 216
 
217 217
 		$tag = new SystemTag(
218
-			(int)$tagId,
218
+			(int) $tagId,
219 219
 			$tagName,
220
-			(bool)$userVisible,
221
-			(bool)$userAssignable
220
+			(bool) $userVisible,
221
+			(bool) $userAssignable
222 222
 		);
223 223
 
224 224
 		$this->dispatcher->dispatch(ManagerEvent::EVENT_CREATE, new ManagerEvent(
@@ -232,8 +232,8 @@  discard block
 block discarded – undo
232 232
 	 * {@inheritdoc}
233 233
 	 */
234 234
 	public function updateTag($tagId, $tagName, $userVisible, $userAssignable) {
235
-		$userVisible = (int)$userVisible;
236
-		$userAssignable = (int)$userAssignable;
235
+		$userVisible = (int) $userVisible;
236
+		$userAssignable = (int) $userAssignable;
237 237
 
238 238
 		try {
239 239
 			$tags = $this->getTagsByIds($tagId);
@@ -270,7 +270,7 @@  discard block
 block discarded – undo
270 270
 			}
271 271
 		} catch (UniqueConstraintViolationException $e) {
272 272
 			throw new TagAlreadyExistsException(
273
-				'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists',
273
+				'Tag ("'.$tagName.'", '.$userVisible.', '.$userAssignable.') already exists',
274 274
 				0,
275 275
 				$e
276 276
 			);
@@ -377,7 +377,7 @@  discard block
 block discarded – undo
377 377
 	}
378 378
 
379 379
 	private function createSystemTagFromRow($row) {
380
-		return new SystemTag((int)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable']);
380
+		return new SystemTag((int) $row['id'], $row['name'], (bool) $row['visibility'], (bool) $row['editable']);
381 381
 	}
382 382
 
383 383
 	/**
Please login to merge, or discard this patch.
Indentation   +393 added lines, -393 removed lines patch added patch discarded remove patch
@@ -41,397 +41,397 @@
 block discarded – undo
41 41
  */
42 42
 class SystemTagManager implements ISystemTagManager {
43 43
 
44
-	const TAG_TABLE = 'systemtag';
45
-	const TAG_GROUP_TABLE = 'systemtag_group';
46
-
47
-	/** @var IDBConnection */
48
-	protected $connection;
49
-
50
-	/** @var EventDispatcherInterface */
51
-	protected $dispatcher;
52
-
53
-	/** @var IGroupManager */
54
-	protected $groupManager;
55
-
56
-	/**
57
-	 * Prepared query for selecting tags directly
58
-	 *
59
-	 * @var \OCP\DB\QueryBuilder\IQueryBuilder
60
-	 */
61
-	private $selectTagQuery;
62
-
63
-	/**
64
-	 * Constructor.
65
-	 *
66
-	 * @param IDBConnection $connection database connection
67
-	 * @param EventDispatcherInterface $dispatcher
68
-	 */
69
-	public function __construct(
70
-		IDBConnection $connection,
71
-		IGroupManager $groupManager,
72
-		EventDispatcherInterface $dispatcher
73
-	) {
74
-		$this->connection = $connection;
75
-		$this->groupManager = $groupManager;
76
-		$this->dispatcher = $dispatcher;
77
-
78
-		$query = $this->connection->getQueryBuilder();
79
-		$this->selectTagQuery = $query->select('*')
80
-			->from(self::TAG_TABLE)
81
-			->where($query->expr()->eq('name', $query->createParameter('name')))
82
-			->andWhere($query->expr()->eq('visibility', $query->createParameter('visibility')))
83
-			->andWhere($query->expr()->eq('editable', $query->createParameter('editable')));
84
-	}
85
-
86
-	/**
87
-	 * {@inheritdoc}
88
-	 */
89
-	public function getTagsByIds($tagIds) {
90
-		if (!is_array($tagIds)) {
91
-			$tagIds = [$tagIds];
92
-		}
93
-
94
-		$tags = [];
95
-
96
-		// note: not all databases will fail if it's a string or starts with a number
97
-		foreach ($tagIds as $tagId) {
98
-			if (!is_numeric($tagId)) {
99
-				throw new \InvalidArgumentException('Tag id must be integer');
100
-			}
101
-		}
102
-
103
-		$query = $this->connection->getQueryBuilder();
104
-		$query->select('*')
105
-			->from(self::TAG_TABLE)
106
-			->where($query->expr()->in('id', $query->createParameter('tagids')))
107
-			->addOrderBy('name', 'ASC')
108
-			->addOrderBy('visibility', 'ASC')
109
-			->addOrderBy('editable', 'ASC')
110
-			->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY);
111
-
112
-		$result = $query->execute();
113
-		while ($row = $result->fetch()) {
114
-			$tags[$row['id']] = $this->createSystemTagFromRow($row);
115
-		}
116
-
117
-		$result->closeCursor();
118
-
119
-		if (count($tags) !== count($tagIds)) {
120
-			throw new TagNotFoundException(
121
-				'Tag id(s) not found', 0, null, array_diff($tagIds, array_keys($tags))
122
-			);
123
-		}
124
-
125
-		return $tags;
126
-	}
127
-
128
-	/**
129
-	 * {@inheritdoc}
130
-	 */
131
-	public function getAllTags($visibilityFilter = null, $nameSearchPattern = null) {
132
-		$tags = [];
133
-
134
-		$query = $this->connection->getQueryBuilder();
135
-		$query->select('*')
136
-			->from(self::TAG_TABLE);
137
-
138
-		if (!is_null($visibilityFilter)) {
139
-			$query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int)$visibilityFilter)));
140
-		}
141
-
142
-		if (!empty($nameSearchPattern)) {
143
-			$query->andWhere(
144
-				$query->expr()->like(
145
-					'name',
146
-					$query->createNamedParameter('%' . $this->connection->escapeLikeParameter($nameSearchPattern). '%')
147
-				)
148
-			);
149
-		}
150
-
151
-		$query
152
-			->addOrderBy('name', 'ASC')
153
-			->addOrderBy('visibility', 'ASC')
154
-			->addOrderBy('editable', 'ASC');
155
-
156
-		$result = $query->execute();
157
-		while ($row = $result->fetch()) {
158
-			$tags[$row['id']] = $this->createSystemTagFromRow($row);
159
-		}
160
-
161
-		$result->closeCursor();
162
-
163
-		return $tags;
164
-	}
165
-
166
-	/**
167
-	 * {@inheritdoc}
168
-	 */
169
-	public function getTag($tagName, $userVisible, $userAssignable) {
170
-		$userVisible = (int)$userVisible;
171
-		$userAssignable = (int)$userAssignable;
172
-
173
-		$result = $this->selectTagQuery
174
-			->setParameter('name', $tagName)
175
-			->setParameter('visibility', $userVisible)
176
-			->setParameter('editable', $userAssignable)
177
-			->execute();
178
-
179
-		$row = $result->fetch();
180
-		$result->closeCursor();
181
-		if (!$row) {
182
-			throw new TagNotFoundException(
183
-				'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') does not exist'
184
-			);
185
-		}
186
-
187
-		return $this->createSystemTagFromRow($row);
188
-	}
189
-
190
-	/**
191
-	 * {@inheritdoc}
192
-	 */
193
-	public function createTag($tagName, $userVisible, $userAssignable) {
194
-		$userVisible = (int)$userVisible;
195
-		$userAssignable = (int)$userAssignable;
196
-
197
-		$query = $this->connection->getQueryBuilder();
198
-		$query->insert(self::TAG_TABLE)
199
-			->values([
200
-				'name' => $query->createNamedParameter($tagName),
201
-				'visibility' => $query->createNamedParameter($userVisible),
202
-				'editable' => $query->createNamedParameter($userAssignable),
203
-			]);
204
-
205
-		try {
206
-			$query->execute();
207
-		} catch (UniqueConstraintViolationException $e) {
208
-			throw new TagAlreadyExistsException(
209
-				'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists',
210
-				0,
211
-				$e
212
-			);
213
-		}
214
-
215
-		$tagId = $query->getLastInsertId();
216
-
217
-		$tag = new SystemTag(
218
-			(int)$tagId,
219
-			$tagName,
220
-			(bool)$userVisible,
221
-			(bool)$userAssignable
222
-		);
223
-
224
-		$this->dispatcher->dispatch(ManagerEvent::EVENT_CREATE, new ManagerEvent(
225
-			ManagerEvent::EVENT_CREATE, $tag
226
-		));
227
-
228
-		return $tag;
229
-	}
230
-
231
-	/**
232
-	 * {@inheritdoc}
233
-	 */
234
-	public function updateTag($tagId, $tagName, $userVisible, $userAssignable) {
235
-		$userVisible = (int)$userVisible;
236
-		$userAssignable = (int)$userAssignable;
237
-
238
-		try {
239
-			$tags = $this->getTagsByIds($tagId);
240
-		} catch (TagNotFoundException $e) {
241
-			throw new TagNotFoundException(
242
-				'Tag does not exist', 0, null, [$tagId]
243
-			);
244
-		}
245
-
246
-		$beforeUpdate = array_shift($tags);
247
-		$afterUpdate = new SystemTag(
248
-			(int) $tagId,
249
-			$tagName,
250
-			(bool) $userVisible,
251
-			(bool) $userAssignable
252
-		);
253
-
254
-		$query = $this->connection->getQueryBuilder();
255
-		$query->update(self::TAG_TABLE)
256
-			->set('name', $query->createParameter('name'))
257
-			->set('visibility', $query->createParameter('visibility'))
258
-			->set('editable', $query->createParameter('editable'))
259
-			->where($query->expr()->eq('id', $query->createParameter('tagid')))
260
-			->setParameter('name', $tagName)
261
-			->setParameter('visibility', $userVisible)
262
-			->setParameter('editable', $userAssignable)
263
-			->setParameter('tagid', $tagId);
264
-
265
-		try {
266
-			if ($query->execute() === 0) {
267
-				throw new TagNotFoundException(
268
-					'Tag does not exist', 0, null, [$tagId]
269
-				);
270
-			}
271
-		} catch (UniqueConstraintViolationException $e) {
272
-			throw new TagAlreadyExistsException(
273
-				'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists',
274
-				0,
275
-				$e
276
-			);
277
-		}
278
-
279
-		$this->dispatcher->dispatch(ManagerEvent::EVENT_UPDATE, new ManagerEvent(
280
-			ManagerEvent::EVENT_UPDATE, $afterUpdate, $beforeUpdate
281
-		));
282
-	}
283
-
284
-	/**
285
-	 * {@inheritdoc}
286
-	 */
287
-	public function deleteTags($tagIds) {
288
-		if (!is_array($tagIds)) {
289
-			$tagIds = [$tagIds];
290
-		}
291
-
292
-		$tagNotFoundException = null;
293
-		$tags = [];
294
-		try {
295
-			$tags = $this->getTagsByIds($tagIds);
296
-		} catch (TagNotFoundException $e) {
297
-			$tagNotFoundException = $e;
298
-
299
-			// Get existing tag objects for the hooks later
300
-			$existingTags = array_diff($tagIds, $tagNotFoundException->getMissingTags());
301
-			if (!empty($existingTags)) {
302
-				try {
303
-					$tags = $this->getTagsByIds($existingTags);
304
-				} catch (TagNotFoundException $e) {
305
-					// Ignore further errors...
306
-				}
307
-			}
308
-		}
309
-
310
-		// delete relationships first
311
-		$query = $this->connection->getQueryBuilder();
312
-		$query->delete(SystemTagObjectMapper::RELATION_TABLE)
313
-			->where($query->expr()->in('systemtagid', $query->createParameter('tagids')))
314
-			->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY)
315
-			->execute();
316
-
317
-		$query = $this->connection->getQueryBuilder();
318
-		$query->delete(self::TAG_TABLE)
319
-			->where($query->expr()->in('id', $query->createParameter('tagids')))
320
-			->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY)
321
-			->execute();
322
-
323
-		foreach ($tags as $tag) {
324
-			$this->dispatcher->dispatch(ManagerEvent::EVENT_DELETE, new ManagerEvent(
325
-				ManagerEvent::EVENT_DELETE, $tag
326
-			));
327
-		}
328
-
329
-		if ($tagNotFoundException !== null) {
330
-			throw new TagNotFoundException(
331
-				'Tag id(s) not found', 0, $tagNotFoundException, $tagNotFoundException->getMissingTags()
332
-			);
333
-		}
334
-	}
335
-
336
-	/**
337
-	 * {@inheritdoc}
338
-	 */
339
-	public function canUserAssignTag(ISystemTag $tag, IUser $user) {
340
-		// early check to avoid unneeded group lookups
341
-		if ($tag->isUserAssignable() && $tag->isUserVisible()) {
342
-			return true;
343
-		}
344
-
345
-		if ($this->groupManager->isAdmin($user->getUID())) {
346
-			return true;
347
-		}
348
-
349
-		if (!$tag->isUserVisible()) {
350
-			return false;
351
-		}
352
-
353
-		$groupIds = $this->groupManager->getUserGroupIds($user);
354
-		if (!empty($groupIds)) {
355
-			$matchingGroups = array_intersect($groupIds, $this->getTagGroups($tag));
356
-			if (!empty($matchingGroups)) {
357
-				return true;
358
-			}
359
-		}
360
-
361
-		return false;
362
-	}
363
-
364
-	/**
365
-	 * {@inheritdoc}
366
-	 */
367
-	public function canUserSeeTag(ISystemTag $tag, IUser $user) {
368
-		if ($tag->isUserVisible()) {
369
-			return true;
370
-		}
371
-
372
-		if ($this->groupManager->isAdmin($user->getUID())) {
373
-			return true;
374
-		}
375
-
376
-		return false;
377
-	}
378
-
379
-	private function createSystemTagFromRow($row) {
380
-		return new SystemTag((int)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable']);
381
-	}
382
-
383
-	/**
384
-	 * {@inheritdoc}
385
-	 */
386
-	public function setTagGroups(ISystemTag $tag, $groupIds) {
387
-		// delete relationships first
388
-		$this->connection->beginTransaction();
389
-		try {
390
-			$query = $this->connection->getQueryBuilder();
391
-			$query->delete(self::TAG_GROUP_TABLE)
392
-				->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId())))
393
-				->execute();
394
-
395
-			// add each group id
396
-			$query = $this->connection->getQueryBuilder();
397
-			$query->insert(self::TAG_GROUP_TABLE)
398
-				->values([
399
-					'systemtagid' => $query->createNamedParameter($tag->getId()),
400
-					'gid' => $query->createParameter('gid'),
401
-				]);
402
-			foreach ($groupIds as $groupId) {
403
-				if ($groupId === '') {
404
-					continue;
405
-				}
406
-				$query->setParameter('gid', $groupId);
407
-				$query->execute();
408
-			}
409
-
410
-			$this->connection->commit();
411
-		} catch (\Exception $e) {
412
-			$this->connection->rollback();
413
-			throw $e;
414
-		}
415
-	}
416
-
417
-	/**
418
-	 * {@inheritdoc}
419
-	 */
420
-	public function getTagGroups(ISystemTag $tag) {
421
-		$groupIds = [];
422
-		$query = $this->connection->getQueryBuilder();
423
-		$query->select('gid')
424
-			->from(self::TAG_GROUP_TABLE)
425
-			->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId())))
426
-			->orderBy('gid');
427
-
428
-		$result = $query->execute();
429
-		while ($row = $result->fetch()) {
430
-			$groupIds[] = $row['gid'];
431
-		}
432
-
433
-		$result->closeCursor();
434
-
435
-		return $groupIds;
436
-	}
44
+    const TAG_TABLE = 'systemtag';
45
+    const TAG_GROUP_TABLE = 'systemtag_group';
46
+
47
+    /** @var IDBConnection */
48
+    protected $connection;
49
+
50
+    /** @var EventDispatcherInterface */
51
+    protected $dispatcher;
52
+
53
+    /** @var IGroupManager */
54
+    protected $groupManager;
55
+
56
+    /**
57
+     * Prepared query for selecting tags directly
58
+     *
59
+     * @var \OCP\DB\QueryBuilder\IQueryBuilder
60
+     */
61
+    private $selectTagQuery;
62
+
63
+    /**
64
+     * Constructor.
65
+     *
66
+     * @param IDBConnection $connection database connection
67
+     * @param EventDispatcherInterface $dispatcher
68
+     */
69
+    public function __construct(
70
+        IDBConnection $connection,
71
+        IGroupManager $groupManager,
72
+        EventDispatcherInterface $dispatcher
73
+    ) {
74
+        $this->connection = $connection;
75
+        $this->groupManager = $groupManager;
76
+        $this->dispatcher = $dispatcher;
77
+
78
+        $query = $this->connection->getQueryBuilder();
79
+        $this->selectTagQuery = $query->select('*')
80
+            ->from(self::TAG_TABLE)
81
+            ->where($query->expr()->eq('name', $query->createParameter('name')))
82
+            ->andWhere($query->expr()->eq('visibility', $query->createParameter('visibility')))
83
+            ->andWhere($query->expr()->eq('editable', $query->createParameter('editable')));
84
+    }
85
+
86
+    /**
87
+     * {@inheritdoc}
88
+     */
89
+    public function getTagsByIds($tagIds) {
90
+        if (!is_array($tagIds)) {
91
+            $tagIds = [$tagIds];
92
+        }
93
+
94
+        $tags = [];
95
+
96
+        // note: not all databases will fail if it's a string or starts with a number
97
+        foreach ($tagIds as $tagId) {
98
+            if (!is_numeric($tagId)) {
99
+                throw new \InvalidArgumentException('Tag id must be integer');
100
+            }
101
+        }
102
+
103
+        $query = $this->connection->getQueryBuilder();
104
+        $query->select('*')
105
+            ->from(self::TAG_TABLE)
106
+            ->where($query->expr()->in('id', $query->createParameter('tagids')))
107
+            ->addOrderBy('name', 'ASC')
108
+            ->addOrderBy('visibility', 'ASC')
109
+            ->addOrderBy('editable', 'ASC')
110
+            ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY);
111
+
112
+        $result = $query->execute();
113
+        while ($row = $result->fetch()) {
114
+            $tags[$row['id']] = $this->createSystemTagFromRow($row);
115
+        }
116
+
117
+        $result->closeCursor();
118
+
119
+        if (count($tags) !== count($tagIds)) {
120
+            throw new TagNotFoundException(
121
+                'Tag id(s) not found', 0, null, array_diff($tagIds, array_keys($tags))
122
+            );
123
+        }
124
+
125
+        return $tags;
126
+    }
127
+
128
+    /**
129
+     * {@inheritdoc}
130
+     */
131
+    public function getAllTags($visibilityFilter = null, $nameSearchPattern = null) {
132
+        $tags = [];
133
+
134
+        $query = $this->connection->getQueryBuilder();
135
+        $query->select('*')
136
+            ->from(self::TAG_TABLE);
137
+
138
+        if (!is_null($visibilityFilter)) {
139
+            $query->andWhere($query->expr()->eq('visibility', $query->createNamedParameter((int)$visibilityFilter)));
140
+        }
141
+
142
+        if (!empty($nameSearchPattern)) {
143
+            $query->andWhere(
144
+                $query->expr()->like(
145
+                    'name',
146
+                    $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($nameSearchPattern). '%')
147
+                )
148
+            );
149
+        }
150
+
151
+        $query
152
+            ->addOrderBy('name', 'ASC')
153
+            ->addOrderBy('visibility', 'ASC')
154
+            ->addOrderBy('editable', 'ASC');
155
+
156
+        $result = $query->execute();
157
+        while ($row = $result->fetch()) {
158
+            $tags[$row['id']] = $this->createSystemTagFromRow($row);
159
+        }
160
+
161
+        $result->closeCursor();
162
+
163
+        return $tags;
164
+    }
165
+
166
+    /**
167
+     * {@inheritdoc}
168
+     */
169
+    public function getTag($tagName, $userVisible, $userAssignable) {
170
+        $userVisible = (int)$userVisible;
171
+        $userAssignable = (int)$userAssignable;
172
+
173
+        $result = $this->selectTagQuery
174
+            ->setParameter('name', $tagName)
175
+            ->setParameter('visibility', $userVisible)
176
+            ->setParameter('editable', $userAssignable)
177
+            ->execute();
178
+
179
+        $row = $result->fetch();
180
+        $result->closeCursor();
181
+        if (!$row) {
182
+            throw new TagNotFoundException(
183
+                'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') does not exist'
184
+            );
185
+        }
186
+
187
+        return $this->createSystemTagFromRow($row);
188
+    }
189
+
190
+    /**
191
+     * {@inheritdoc}
192
+     */
193
+    public function createTag($tagName, $userVisible, $userAssignable) {
194
+        $userVisible = (int)$userVisible;
195
+        $userAssignable = (int)$userAssignable;
196
+
197
+        $query = $this->connection->getQueryBuilder();
198
+        $query->insert(self::TAG_TABLE)
199
+            ->values([
200
+                'name' => $query->createNamedParameter($tagName),
201
+                'visibility' => $query->createNamedParameter($userVisible),
202
+                'editable' => $query->createNamedParameter($userAssignable),
203
+            ]);
204
+
205
+        try {
206
+            $query->execute();
207
+        } catch (UniqueConstraintViolationException $e) {
208
+            throw new TagAlreadyExistsException(
209
+                'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists',
210
+                0,
211
+                $e
212
+            );
213
+        }
214
+
215
+        $tagId = $query->getLastInsertId();
216
+
217
+        $tag = new SystemTag(
218
+            (int)$tagId,
219
+            $tagName,
220
+            (bool)$userVisible,
221
+            (bool)$userAssignable
222
+        );
223
+
224
+        $this->dispatcher->dispatch(ManagerEvent::EVENT_CREATE, new ManagerEvent(
225
+            ManagerEvent::EVENT_CREATE, $tag
226
+        ));
227
+
228
+        return $tag;
229
+    }
230
+
231
+    /**
232
+     * {@inheritdoc}
233
+     */
234
+    public function updateTag($tagId, $tagName, $userVisible, $userAssignable) {
235
+        $userVisible = (int)$userVisible;
236
+        $userAssignable = (int)$userAssignable;
237
+
238
+        try {
239
+            $tags = $this->getTagsByIds($tagId);
240
+        } catch (TagNotFoundException $e) {
241
+            throw new TagNotFoundException(
242
+                'Tag does not exist', 0, null, [$tagId]
243
+            );
244
+        }
245
+
246
+        $beforeUpdate = array_shift($tags);
247
+        $afterUpdate = new SystemTag(
248
+            (int) $tagId,
249
+            $tagName,
250
+            (bool) $userVisible,
251
+            (bool) $userAssignable
252
+        );
253
+
254
+        $query = $this->connection->getQueryBuilder();
255
+        $query->update(self::TAG_TABLE)
256
+            ->set('name', $query->createParameter('name'))
257
+            ->set('visibility', $query->createParameter('visibility'))
258
+            ->set('editable', $query->createParameter('editable'))
259
+            ->where($query->expr()->eq('id', $query->createParameter('tagid')))
260
+            ->setParameter('name', $tagName)
261
+            ->setParameter('visibility', $userVisible)
262
+            ->setParameter('editable', $userAssignable)
263
+            ->setParameter('tagid', $tagId);
264
+
265
+        try {
266
+            if ($query->execute() === 0) {
267
+                throw new TagNotFoundException(
268
+                    'Tag does not exist', 0, null, [$tagId]
269
+                );
270
+            }
271
+        } catch (UniqueConstraintViolationException $e) {
272
+            throw new TagAlreadyExistsException(
273
+                'Tag ("' . $tagName . '", '. $userVisible . ', ' . $userAssignable . ') already exists',
274
+                0,
275
+                $e
276
+            );
277
+        }
278
+
279
+        $this->dispatcher->dispatch(ManagerEvent::EVENT_UPDATE, new ManagerEvent(
280
+            ManagerEvent::EVENT_UPDATE, $afterUpdate, $beforeUpdate
281
+        ));
282
+    }
283
+
284
+    /**
285
+     * {@inheritdoc}
286
+     */
287
+    public function deleteTags($tagIds) {
288
+        if (!is_array($tagIds)) {
289
+            $tagIds = [$tagIds];
290
+        }
291
+
292
+        $tagNotFoundException = null;
293
+        $tags = [];
294
+        try {
295
+            $tags = $this->getTagsByIds($tagIds);
296
+        } catch (TagNotFoundException $e) {
297
+            $tagNotFoundException = $e;
298
+
299
+            // Get existing tag objects for the hooks later
300
+            $existingTags = array_diff($tagIds, $tagNotFoundException->getMissingTags());
301
+            if (!empty($existingTags)) {
302
+                try {
303
+                    $tags = $this->getTagsByIds($existingTags);
304
+                } catch (TagNotFoundException $e) {
305
+                    // Ignore further errors...
306
+                }
307
+            }
308
+        }
309
+
310
+        // delete relationships first
311
+        $query = $this->connection->getQueryBuilder();
312
+        $query->delete(SystemTagObjectMapper::RELATION_TABLE)
313
+            ->where($query->expr()->in('systemtagid', $query->createParameter('tagids')))
314
+            ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY)
315
+            ->execute();
316
+
317
+        $query = $this->connection->getQueryBuilder();
318
+        $query->delete(self::TAG_TABLE)
319
+            ->where($query->expr()->in('id', $query->createParameter('tagids')))
320
+            ->setParameter('tagids', $tagIds, IQueryBuilder::PARAM_INT_ARRAY)
321
+            ->execute();
322
+
323
+        foreach ($tags as $tag) {
324
+            $this->dispatcher->dispatch(ManagerEvent::EVENT_DELETE, new ManagerEvent(
325
+                ManagerEvent::EVENT_DELETE, $tag
326
+            ));
327
+        }
328
+
329
+        if ($tagNotFoundException !== null) {
330
+            throw new TagNotFoundException(
331
+                'Tag id(s) not found', 0, $tagNotFoundException, $tagNotFoundException->getMissingTags()
332
+            );
333
+        }
334
+    }
335
+
336
+    /**
337
+     * {@inheritdoc}
338
+     */
339
+    public function canUserAssignTag(ISystemTag $tag, IUser $user) {
340
+        // early check to avoid unneeded group lookups
341
+        if ($tag->isUserAssignable() && $tag->isUserVisible()) {
342
+            return true;
343
+        }
344
+
345
+        if ($this->groupManager->isAdmin($user->getUID())) {
346
+            return true;
347
+        }
348
+
349
+        if (!$tag->isUserVisible()) {
350
+            return false;
351
+        }
352
+
353
+        $groupIds = $this->groupManager->getUserGroupIds($user);
354
+        if (!empty($groupIds)) {
355
+            $matchingGroups = array_intersect($groupIds, $this->getTagGroups($tag));
356
+            if (!empty($matchingGroups)) {
357
+                return true;
358
+            }
359
+        }
360
+
361
+        return false;
362
+    }
363
+
364
+    /**
365
+     * {@inheritdoc}
366
+     */
367
+    public function canUserSeeTag(ISystemTag $tag, IUser $user) {
368
+        if ($tag->isUserVisible()) {
369
+            return true;
370
+        }
371
+
372
+        if ($this->groupManager->isAdmin($user->getUID())) {
373
+            return true;
374
+        }
375
+
376
+        return false;
377
+    }
378
+
379
+    private function createSystemTagFromRow($row) {
380
+        return new SystemTag((int)$row['id'], $row['name'], (bool)$row['visibility'], (bool)$row['editable']);
381
+    }
382
+
383
+    /**
384
+     * {@inheritdoc}
385
+     */
386
+    public function setTagGroups(ISystemTag $tag, $groupIds) {
387
+        // delete relationships first
388
+        $this->connection->beginTransaction();
389
+        try {
390
+            $query = $this->connection->getQueryBuilder();
391
+            $query->delete(self::TAG_GROUP_TABLE)
392
+                ->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId())))
393
+                ->execute();
394
+
395
+            // add each group id
396
+            $query = $this->connection->getQueryBuilder();
397
+            $query->insert(self::TAG_GROUP_TABLE)
398
+                ->values([
399
+                    'systemtagid' => $query->createNamedParameter($tag->getId()),
400
+                    'gid' => $query->createParameter('gid'),
401
+                ]);
402
+            foreach ($groupIds as $groupId) {
403
+                if ($groupId === '') {
404
+                    continue;
405
+                }
406
+                $query->setParameter('gid', $groupId);
407
+                $query->execute();
408
+            }
409
+
410
+            $this->connection->commit();
411
+        } catch (\Exception $e) {
412
+            $this->connection->rollback();
413
+            throw $e;
414
+        }
415
+    }
416
+
417
+    /**
418
+     * {@inheritdoc}
419
+     */
420
+    public function getTagGroups(ISystemTag $tag) {
421
+        $groupIds = [];
422
+        $query = $this->connection->getQueryBuilder();
423
+        $query->select('gid')
424
+            ->from(self::TAG_GROUP_TABLE)
425
+            ->where($query->expr()->eq('systemtagid', $query->createNamedParameter($tag->getId())))
426
+            ->orderBy('gid');
427
+
428
+        $result = $query->execute();
429
+        while ($row = $result->fetch()) {
430
+            $groupIds[] = $row['gid'];
431
+        }
432
+
433
+        $result->closeCursor();
434
+
435
+        return $groupIds;
436
+    }
437 437
 }
Please login to merge, or discard this patch.
lib/private/Tags.php 3 patches
Doc Comments   +10 added lines, -2 removed lines patch added patch discarded remove patch
@@ -742,11 +742,19 @@  discard block
 block discarded – undo
742 742
 	}
743 743
 
744 744
 	// case-insensitive array_search
745
+
746
+	/**
747
+	 * @param string $needle
748
+	 */
745 749
 	protected function array_searchi($needle, $haystack, $mem='getName') {
746 750
 		if(!is_array($haystack)) {
747 751
 			return false;
748 752
 		}
749 753
 		return array_search(strtolower($needle), array_map(
754
+
755
+			/**
756
+			 * @param string $tag
757
+			 */
750 758
 			function($tag) use($mem) {
751 759
 				return strtolower(call_user_func(array($tag, $mem)));
752 760
 			}, $haystack)
@@ -771,7 +779,7 @@  discard block
 block discarded – undo
771 779
 	* Get a tag by its name.
772 780
 	*
773 781
 	* @param string $name The tag name.
774
-	* @return integer|bool The tag object's offset within the $this->tags
782
+	* @return \OCP\AppFramework\Db\Entity The tag object's offset within the $this->tags
775 783
 	*                      array or false if it doesn't exist.
776 784
 	*/
777 785
 	private function getTagByName($name) {
@@ -782,7 +790,7 @@  discard block
 block discarded – undo
782 790
 	* Get a tag by its ID.
783 791
 	*
784 792
 	* @param string $id The tag ID to look for.
785
-	* @return integer|bool The tag object's offset within the $this->tags
793
+	* @return \OCP\AppFramework\Db\Entity The tag object's offset within the $this->tags
786 794
 	*                      array or false if it doesn't exist.
787 795
 	*/
788 796
 	private function getTagById($id) {
Please login to merge, or discard this patch.
Indentation   +757 added lines, -757 removed lines patch added patch discarded remove patch
@@ -48,761 +48,761 @@
 block discarded – undo
48 48
 
49 49
 class Tags implements \OCP\ITags {
50 50
 
51
-	/**
52
-	 * Tags
53
-	 *
54
-	 * @var array
55
-	 */
56
-	private $tags = array();
57
-
58
-	/**
59
-	 * Used for storing objectid/categoryname pairs while rescanning.
60
-	 *
61
-	 * @var array
62
-	 */
63
-	private static $relations = array();
64
-
65
-	/**
66
-	 * Type
67
-	 *
68
-	 * @var string
69
-	 */
70
-	private $type;
71
-
72
-	/**
73
-	 * User
74
-	 *
75
-	 * @var string
76
-	 */
77
-	private $user;
78
-
79
-	/**
80
-	 * Are we including tags for shared items?
81
-	 *
82
-	 * @var bool
83
-	 */
84
-	private $includeShared = false;
85
-
86
-	/**
87
-	 * The current user, plus any owners of the items shared with the current
88
-	 * user, if $this->includeShared === true.
89
-	 *
90
-	 * @var array
91
-	 */
92
-	private $owners = array();
93
-
94
-	/**
95
-	 * The Mapper we're using to communicate our Tag objects to the database.
96
-	 *
97
-	 * @var TagMapper
98
-	 */
99
-	private $mapper;
100
-
101
-	/**
102
-	 * The sharing backend for objects of $this->type. Required if
103
-	 * $this->includeShared === true to determine ownership of items.
104
-	 *
105
-	 * @var \OCP\Share_Backend
106
-	 */
107
-	private $backend;
108
-
109
-	const TAG_TABLE = '*PREFIX*vcategory';
110
-	const RELATION_TABLE = '*PREFIX*vcategory_to_object';
111
-
112
-	const TAG_FAVORITE = '_$!<Favorite>!$_';
113
-
114
-	/**
115
-	* Constructor.
116
-	*
117
-	* @param TagMapper $mapper Instance of the TagMapper abstraction layer.
118
-	* @param string $user The user whose data the object will operate on.
119
-	* @param string $type The type of items for which tags will be loaded.
120
-	* @param array $defaultTags Tags that should be created at construction.
121
-	* @param boolean $includeShared Whether to include tags for items shared with this user by others.
122
-	*/
123
-	public function __construct(TagMapper $mapper, $user, $type, $defaultTags = array(), $includeShared = false) {
124
-		$this->mapper = $mapper;
125
-		$this->user = $user;
126
-		$this->type = $type;
127
-		$this->includeShared = $includeShared;
128
-		$this->owners = array($this->user);
129
-		if ($this->includeShared) {
130
-			$this->owners = array_merge($this->owners, \OC\Share\Share::getSharedItemsOwners($this->user, $this->type, true));
131
-			$this->backend = \OC\Share\Share::getBackend($this->type);
132
-		}
133
-		$this->tags = $this->mapper->loadTags($this->owners, $this->type);
134
-
135
-		if(count($defaultTags) > 0 && count($this->tags) === 0) {
136
-			$this->addMultiple($defaultTags, true);
137
-		}
138
-	}
139
-
140
-	/**
141
-	* Check if any tags are saved for this type and user.
142
-	*
143
-	* @return boolean.
144
-	*/
145
-	public function isEmpty() {
146
-		return count($this->tags) === 0;
147
-	}
148
-
149
-	/**
150
-	* Returns an array mapping a given tag's properties to its values:
151
-	* ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype']
152
-	*
153
-	* @param string $id The ID of the tag that is going to be mapped
154
-	* @return array|false
155
-	*/
156
-	public function getTag($id) {
157
-		$key = $this->getTagById($id);
158
-		if ($key !== false) {
159
-			return $this->tagMap($this->tags[$key]);
160
-		}
161
-		return false;
162
-	}
163
-
164
-	/**
165
-	* Get the tags for a specific user.
166
-	*
167
-	* This returns an array with maps containing each tag's properties:
168
-	* [
169
-	* 	['id' => 0, 'name' = 'First tag', 'owner' = 'User', 'type' => 'tagtype'],
170
-	* 	['id' => 1, 'name' = 'Shared tag', 'owner' = 'Other user', 'type' => 'tagtype'],
171
-	* ]
172
-	*
173
-	* @return array
174
-	*/
175
-	public function getTags() {
176
-		if(!count($this->tags)) {
177
-			return array();
178
-		}
179
-
180
-		usort($this->tags, function($a, $b) {
181
-			return strnatcasecmp($a->getName(), $b->getName());
182
-		});
183
-		$tagMap = array();
184
-
185
-		foreach($this->tags as $tag) {
186
-			if($tag->getName() !== self::TAG_FAVORITE) {
187
-				$tagMap[] = $this->tagMap($tag);
188
-			}
189
-		}
190
-		return $tagMap;
191
-
192
-	}
193
-
194
-	/**
195
-	* Return only the tags owned by the given user, omitting any tags shared
196
-	* by other users.
197
-	*
198
-	* @param string $user The user whose tags are to be checked.
199
-	* @return array An array of Tag objects.
200
-	*/
201
-	public function getTagsForUser($user) {
202
-		return array_filter($this->tags,
203
-			function($tag) use($user) {
204
-				return $tag->getOwner() === $user;
205
-			}
206
-		);
207
-	}
208
-
209
-	/**
210
-	 * Get the list of tags for the given ids.
211
-	 *
212
-	 * @param array $objIds array of object ids
213
-	 * @return array|boolean of tags id as key to array of tag names
214
-	 * or false if an error occurred
215
-	 */
216
-	public function getTagsForObjects(array $objIds) {
217
-		$entries = array();
218
-
219
-		try {
220
-			$conn = \OC::$server->getDatabaseConnection();
221
-			$chunks = array_chunk($objIds, 900, false);
222
-			foreach ($chunks as $chunk) {
223
-				$result = $conn->executeQuery(
224
-					'SELECT `category`, `categoryid`, `objid` ' .
225
-					'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' .
226
-					'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)',
227
-					array($this->user, $this->type, $chunk),
228
-					array(null, null, IQueryBuilder::PARAM_INT_ARRAY)
229
-				);
230
-				while ($row = $result->fetch()) {
231
-					$objId = (int)$row['objid'];
232
-					if (!isset($entries[$objId])) {
233
-						$entries[$objId] = array();
234
-					}
235
-					$entries[$objId][] = $row['category'];
236
-				}
237
-				if (\OCP\DB::isError($result)) {
238
-					\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
239
-					return false;
240
-				}
241
-			}
242
-		} catch(\Exception $e) {
243
-			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
244
-				\OCP\Util::ERROR);
245
-			return false;
246
-		}
247
-
248
-		return $entries;
249
-	}
250
-
251
-	/**
252
-	* Get the a list if items tagged with $tag.
253
-	*
254
-	* Throws an exception if the tag could not be found.
255
-	*
256
-	* @param string $tag Tag id or name.
257
-	* @return array|false An array of object ids or false on error.
258
-	* @throws \Exception
259
-	*/
260
-	public function getIdsForTag($tag) {
261
-		$result = null;
262
-		$tagId = false;
263
-		if(is_numeric($tag)) {
264
-			$tagId = $tag;
265
-		} elseif(is_string($tag)) {
266
-			$tag = trim($tag);
267
-			if($tag === '') {
268
-				\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
269
-				return false;
270
-			}
271
-			$tagId = $this->getTagId($tag);
272
-		}
273
-
274
-		if($tagId === false) {
275
-			$l10n = \OC::$server->getL10N('core');
276
-			throw new \Exception(
277
-				$l10n->t('Could not find category "%s"', $tag)
278
-			);
279
-		}
280
-
281
-		$ids = array();
282
-		$sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE
283
-			. '` WHERE `categoryid` = ?';
284
-
285
-		try {
286
-			$stmt = \OCP\DB::prepare($sql);
287
-			$result = $stmt->execute(array($tagId));
288
-			if (\OCP\DB::isError($result)) {
289
-				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
290
-				return false;
291
-			}
292
-		} catch(\Exception $e) {
293
-			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
294
-				\OCP\Util::ERROR);
295
-			return false;
296
-		}
297
-
298
-		if(!is_null($result)) {
299
-			while( $row = $result->fetchRow()) {
300
-				$id = (int)$row['objid'];
301
-
302
-				if ($this->includeShared) {
303
-					// We have to check if we are really allowed to access the
304
-					// items that are tagged with $tag. To that end, we ask the
305
-					// corresponding sharing backend if the item identified by $id
306
-					// is owned by any of $this->owners.
307
-					foreach ($this->owners as $owner) {
308
-						if ($this->backend->isValidSource($id, $owner)) {
309
-							$ids[] = $id;
310
-							break;
311
-						}
312
-					}
313
-				} else {
314
-					$ids[] = $id;
315
-				}
316
-			}
317
-		}
318
-
319
-		return $ids;
320
-	}
321
-
322
-	/**
323
-	* Checks whether a tag is saved for the given user,
324
-	* disregarding the ones shared with him or her.
325
-	*
326
-	* @param string $name The tag name to check for.
327
-	* @param string $user The user whose tags are to be checked.
328
-	* @return bool
329
-	*/
330
-	public function userHasTag($name, $user) {
331
-		$key = $this->array_searchi($name, $this->getTagsForUser($user));
332
-		return ($key !== false) ? $this->tags[$key]->getId() : false;
333
-	}
334
-
335
-	/**
336
-	* Checks whether a tag is saved for or shared with the current user.
337
-	*
338
-	* @param string $name The tag name to check for.
339
-	* @return bool
340
-	*/
341
-	public function hasTag($name) {
342
-		return $this->getTagId($name) !== false;
343
-	}
344
-
345
-	/**
346
-	* Add a new tag.
347
-	*
348
-	* @param string $name A string with a name of the tag
349
-	* @return false|int the id of the added tag or false on error.
350
-	*/
351
-	public function add($name) {
352
-		$name = trim($name);
353
-
354
-		if($name === '') {
355
-			\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
356
-			return false;
357
-		}
358
-		if($this->userHasTag($name, $this->user)) {
359
-			\OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', \OCP\Util::DEBUG);
360
-			return false;
361
-		}
362
-		try {
363
-			$tag = new Tag($this->user, $this->type, $name);
364
-			$tag = $this->mapper->insert($tag);
365
-			$this->tags[] = $tag;
366
-		} catch(\Exception $e) {
367
-			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
368
-				\OCP\Util::ERROR);
369
-			return false;
370
-		}
371
-		\OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), \OCP\Util::DEBUG);
372
-		return $tag->getId();
373
-	}
374
-
375
-	/**
376
-	* Rename tag.
377
-	*
378
-	* @param string|integer $from The name or ID of the existing tag
379
-	* @param string $to The new name of the tag.
380
-	* @return bool
381
-	*/
382
-	public function rename($from, $to) {
383
-		$from = trim($from);
384
-		$to = trim($to);
385
-
386
-		if($to === '' || $from === '') {
387
-			\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
388
-			return false;
389
-		}
390
-
391
-		if (is_numeric($from)) {
392
-			$key = $this->getTagById($from);
393
-		} else {
394
-			$key = $this->getTagByName($from);
395
-		}
396
-		if($key === false) {
397
-			\OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', \OCP\Util::DEBUG);
398
-			return false;
399
-		}
400
-		$tag = $this->tags[$key];
401
-
402
-		if($this->userHasTag($to, $tag->getOwner())) {
403
-			\OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', \OCP\Util::DEBUG);
404
-			return false;
405
-		}
406
-
407
-		try {
408
-			$tag->setName($to);
409
-			$this->tags[$key] = $this->mapper->update($tag);
410
-		} catch(\Exception $e) {
411
-			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
412
-				\OCP\Util::ERROR);
413
-			return false;
414
-		}
415
-		return true;
416
-	}
417
-
418
-	/**
419
-	* Add a list of new tags.
420
-	*
421
-	* @param string[] $names A string with a name or an array of strings containing
422
-	* the name(s) of the tag(s) to add.
423
-	* @param bool $sync When true, save the tags
424
-	* @param int|null $id int Optional object id to add to this|these tag(s)
425
-	* @return bool Returns false on error.
426
-	*/
427
-	public function addMultiple($names, $sync=false, $id = null) {
428
-		if(!is_array($names)) {
429
-			$names = array($names);
430
-		}
431
-		$names = array_map('trim', $names);
432
-		array_filter($names);
433
-
434
-		$newones = array();
435
-		foreach($names as $name) {
436
-			if(!$this->hasTag($name) && $name !== '') {
437
-				$newones[] = new Tag($this->user, $this->type, $name);
438
-			}
439
-			if(!is_null($id) ) {
440
-				// Insert $objectid, $categoryid  pairs if not exist.
441
-				self::$relations[] = array('objid' => $id, 'tag' => $name);
442
-			}
443
-		}
444
-		$this->tags = array_merge($this->tags, $newones);
445
-		if($sync === true) {
446
-			$this->save();
447
-		}
448
-
449
-		return true;
450
-	}
451
-
452
-	/**
453
-	 * Save the list of tags and their object relations
454
-	 */
455
-	protected function save() {
456
-		if(is_array($this->tags)) {
457
-			foreach($this->tags as $tag) {
458
-				try {
459
-					if (!$this->mapper->tagExists($tag)) {
460
-						$this->mapper->insert($tag);
461
-					}
462
-				} catch(\Exception $e) {
463
-					\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
464
-						\OCP\Util::ERROR);
465
-				}
466
-			}
467
-
468
-			// reload tags to get the proper ids.
469
-			$this->tags = $this->mapper->loadTags($this->owners, $this->type);
470
-			\OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true),
471
-				\OCP\Util::DEBUG);
472
-			// Loop through temporarily cached objectid/tagname pairs
473
-			// and save relations.
474
-			$tags = $this->tags;
475
-			// For some reason this is needed or array_search(i) will return 0..?
476
-			ksort($tags);
477
-			foreach(self::$relations as $relation) {
478
-				$tagId = $this->getTagId($relation['tag']);
479
-				\OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, \OCP\Util::DEBUG);
480
-				if($tagId) {
481
-					try {
482
-						\OCP\DB::insertIfNotExist(self::RELATION_TABLE,
483
-							array(
484
-								'objid' => $relation['objid'],
485
-								'categoryid' => $tagId,
486
-								'type' => $this->type,
487
-								));
488
-					} catch(\Exception $e) {
489
-						\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
490
-							\OCP\Util::ERROR);
491
-					}
492
-				}
493
-			}
494
-			self::$relations = array(); // reset
495
-		} else {
496
-			\OCP\Util::writeLog('core', __METHOD__.', $this->tags is not an array! '
497
-				. print_r($this->tags, true), \OCP\Util::ERROR);
498
-		}
499
-	}
500
-
501
-	/**
502
-	* Delete tags and tag/object relations for a user.
503
-	*
504
-	* For hooking up on post_deleteUser
505
-	*
506
-	* @param array $arguments
507
-	*/
508
-	public static function post_deleteUser($arguments) {
509
-		// Find all objectid/tagId pairs.
510
-		$result = null;
511
-		try {
512
-			$stmt = \OCP\DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` '
513
-				. 'WHERE `uid` = ?');
514
-			$result = $stmt->execute(array($arguments['uid']));
515
-			if (\OCP\DB::isError($result)) {
516
-				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
517
-			}
518
-		} catch(\Exception $e) {
519
-			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
520
-				\OCP\Util::ERROR);
521
-		}
522
-
523
-		if(!is_null($result)) {
524
-			try {
525
-				$stmt = \OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
526
-					. 'WHERE `categoryid` = ?');
527
-				while( $row = $result->fetchRow()) {
528
-					try {
529
-						$stmt->execute(array($row['id']));
530
-					} catch(\Exception $e) {
531
-						\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
532
-							\OCP\Util::ERROR);
533
-					}
534
-				}
535
-			} catch(\Exception $e) {
536
-				\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
537
-					\OCP\Util::ERROR);
538
-			}
539
-		}
540
-		try {
541
-			$stmt = \OCP\DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` '
542
-				. 'WHERE `uid` = ?');
543
-			$result = $stmt->execute(array($arguments['uid']));
544
-			if (\OCP\DB::isError($result)) {
545
-				\OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
546
-			}
547
-		} catch(\Exception $e) {
548
-			\OCP\Util::writeLog('core', __METHOD__ . ', exception: '
549
-				. $e->getMessage(), \OCP\Util::ERROR);
550
-		}
551
-	}
552
-
553
-	/**
554
-	* Delete tag/object relations from the db
555
-	*
556
-	* @param array $ids The ids of the objects
557
-	* @return boolean Returns false on error.
558
-	*/
559
-	public function purgeObjects(array $ids) {
560
-		if(count($ids) === 0) {
561
-			// job done ;)
562
-			return true;
563
-		}
564
-		$updates = $ids;
565
-		try {
566
-			$query = 'DELETE FROM `' . self::RELATION_TABLE . '` ';
567
-			$query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) ';
568
-			$query .= 'AND `type`= ?';
569
-			$updates[] = $this->type;
570
-			$stmt = \OCP\DB::prepare($query);
571
-			$result = $stmt->execute($updates);
572
-			if (\OCP\DB::isError($result)) {
573
-				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
574
-				return false;
575
-			}
576
-		} catch(\Exception $e) {
577
-			\OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(),
578
-				\OCP\Util::ERROR);
579
-			return false;
580
-		}
581
-		return true;
582
-	}
583
-
584
-	/**
585
-	* Get favorites for an object type
586
-	*
587
-	* @return array|false An array of object ids.
588
-	*/
589
-	public function getFavorites() {
590
-		try {
591
-			return $this->getIdsForTag(self::TAG_FAVORITE);
592
-		} catch(\Exception $e) {
593
-			\OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(),
594
-				\OCP\Util::DEBUG);
595
-			return array();
596
-		}
597
-	}
598
-
599
-	/**
600
-	* Add an object to favorites
601
-	*
602
-	* @param int $objid The id of the object
603
-	* @return boolean
604
-	*/
605
-	public function addToFavorites($objid) {
606
-		if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) {
607
-			$this->add(self::TAG_FAVORITE);
608
-		}
609
-		return $this->tagAs($objid, self::TAG_FAVORITE);
610
-	}
611
-
612
-	/**
613
-	* Remove an object from favorites
614
-	*
615
-	* @param int $objid The id of the object
616
-	* @return boolean
617
-	*/
618
-	public function removeFromFavorites($objid) {
619
-		return $this->unTag($objid, self::TAG_FAVORITE);
620
-	}
621
-
622
-	/**
623
-	* Creates a tag/object relation.
624
-	*
625
-	* @param int $objid The id of the object
626
-	* @param string $tag The id or name of the tag
627
-	* @return boolean Returns false on error.
628
-	*/
629
-	public function tagAs($objid, $tag) {
630
-		if(is_string($tag) && !is_numeric($tag)) {
631
-			$tag = trim($tag);
632
-			if($tag === '') {
633
-				\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
634
-				return false;
635
-			}
636
-			if(!$this->hasTag($tag)) {
637
-				$this->add($tag);
638
-			}
639
-			$tagId =  $this->getTagId($tag);
640
-		} else {
641
-			$tagId = $tag;
642
-		}
643
-		try {
644
-			\OCP\DB::insertIfNotExist(self::RELATION_TABLE,
645
-				array(
646
-					'objid' => $objid,
647
-					'categoryid' => $tagId,
648
-					'type' => $this->type,
649
-				));
650
-		} catch(\Exception $e) {
651
-			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
652
-				\OCP\Util::ERROR);
653
-			return false;
654
-		}
655
-		return true;
656
-	}
657
-
658
-	/**
659
-	* Delete single tag/object relation from the db
660
-	*
661
-	* @param int $objid The id of the object
662
-	* @param string $tag The id or name of the tag
663
-	* @return boolean
664
-	*/
665
-	public function unTag($objid, $tag) {
666
-		if(is_string($tag) && !is_numeric($tag)) {
667
-			$tag = trim($tag);
668
-			if($tag === '') {
669
-				\OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', \OCP\Util::DEBUG);
670
-				return false;
671
-			}
672
-			$tagId =  $this->getTagId($tag);
673
-		} else {
674
-			$tagId = $tag;
675
-		}
676
-
677
-		try {
678
-			$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
679
-					. 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?';
680
-			$stmt = \OCP\DB::prepare($sql);
681
-			$stmt->execute(array($objid, $tagId, $this->type));
682
-		} catch(\Exception $e) {
683
-			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
684
-				\OCP\Util::ERROR);
685
-			return false;
686
-		}
687
-		return true;
688
-	}
689
-
690
-	/**
691
-	* Delete tags from the database.
692
-	*
693
-	* @param string[]|integer[] $names An array of tags (names or IDs) to delete
694
-	* @return bool Returns false on error
695
-	*/
696
-	public function delete($names) {
697
-		if(!is_array($names)) {
698
-			$names = array($names);
699
-		}
700
-
701
-		$names = array_map('trim', $names);
702
-		array_filter($names);
703
-
704
-		\OCP\Util::writeLog('core', __METHOD__ . ', before: '
705
-			. print_r($this->tags, true), \OCP\Util::DEBUG);
706
-		foreach($names as $name) {
707
-			$id = null;
708
-
709
-			if (is_numeric($name)) {
710
-				$key = $this->getTagById($name);
711
-			} else {
712
-				$key = $this->getTagByName($name);
713
-			}
714
-			if ($key !== false) {
715
-				$tag = $this->tags[$key];
716
-				$id = $tag->getId();
717
-				unset($this->tags[$key]);
718
-				$this->mapper->delete($tag);
719
-			} else {
720
-				\OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name
721
-					. ': not found.', \OCP\Util::ERROR);
722
-			}
723
-			if(!is_null($id) && $id !== false) {
724
-				try {
725
-					$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
726
-							. 'WHERE `categoryid` = ?';
727
-					$stmt = \OCP\DB::prepare($sql);
728
-					$result = $stmt->execute(array($id));
729
-					if (\OCP\DB::isError($result)) {
730
-						\OCP\Util::writeLog('core',
731
-							__METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(),
732
-							\OCP\Util::ERROR);
733
-						return false;
734
-					}
735
-				} catch(\Exception $e) {
736
-					\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
737
-						\OCP\Util::ERROR);
738
-					return false;
739
-				}
740
-			}
741
-		}
742
-		return true;
743
-	}
744
-
745
-	// case-insensitive array_search
746
-	protected function array_searchi($needle, $haystack, $mem='getName') {
747
-		if(!is_array($haystack)) {
748
-			return false;
749
-		}
750
-		return array_search(strtolower($needle), array_map(
751
-			function($tag) use($mem) {
752
-				return strtolower(call_user_func(array($tag, $mem)));
753
-			}, $haystack)
754
-		);
755
-	}
756
-
757
-	/**
758
-	* Get a tag's ID.
759
-	*
760
-	* @param string $name The tag name to look for.
761
-	* @return string|bool The tag's id or false if no matching tag is found.
762
-	*/
763
-	private function getTagId($name) {
764
-		$key = $this->array_searchi($name, $this->tags);
765
-		if ($key !== false) {
766
-			return $this->tags[$key]->getId();
767
-		}
768
-		return false;
769
-	}
770
-
771
-	/**
772
-	* Get a tag by its name.
773
-	*
774
-	* @param string $name The tag name.
775
-	* @return integer|bool The tag object's offset within the $this->tags
776
-	*                      array or false if it doesn't exist.
777
-	*/
778
-	private function getTagByName($name) {
779
-		return $this->array_searchi($name, $this->tags, 'getName');
780
-	}
781
-
782
-	/**
783
-	* Get a tag by its ID.
784
-	*
785
-	* @param string $id The tag ID to look for.
786
-	* @return integer|bool The tag object's offset within the $this->tags
787
-	*                      array or false if it doesn't exist.
788
-	*/
789
-	private function getTagById($id) {
790
-		return $this->array_searchi($id, $this->tags, 'getId');
791
-	}
792
-
793
-	/**
794
-	* Returns an array mapping a given tag's properties to its values:
795
-	* ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype']
796
-	*
797
-	* @param Tag $tag The tag that is going to be mapped
798
-	* @return array
799
-	*/
800
-	private function tagMap(Tag $tag) {
801
-		return array(
802
-			'id'    => $tag->getId(),
803
-			'name'  => $tag->getName(),
804
-			'owner' => $tag->getOwner(),
805
-			'type'  => $tag->getType()
806
-		);
807
-	}
51
+    /**
52
+     * Tags
53
+     *
54
+     * @var array
55
+     */
56
+    private $tags = array();
57
+
58
+    /**
59
+     * Used for storing objectid/categoryname pairs while rescanning.
60
+     *
61
+     * @var array
62
+     */
63
+    private static $relations = array();
64
+
65
+    /**
66
+     * Type
67
+     *
68
+     * @var string
69
+     */
70
+    private $type;
71
+
72
+    /**
73
+     * User
74
+     *
75
+     * @var string
76
+     */
77
+    private $user;
78
+
79
+    /**
80
+     * Are we including tags for shared items?
81
+     *
82
+     * @var bool
83
+     */
84
+    private $includeShared = false;
85
+
86
+    /**
87
+     * The current user, plus any owners of the items shared with the current
88
+     * user, if $this->includeShared === true.
89
+     *
90
+     * @var array
91
+     */
92
+    private $owners = array();
93
+
94
+    /**
95
+     * The Mapper we're using to communicate our Tag objects to the database.
96
+     *
97
+     * @var TagMapper
98
+     */
99
+    private $mapper;
100
+
101
+    /**
102
+     * The sharing backend for objects of $this->type. Required if
103
+     * $this->includeShared === true to determine ownership of items.
104
+     *
105
+     * @var \OCP\Share_Backend
106
+     */
107
+    private $backend;
108
+
109
+    const TAG_TABLE = '*PREFIX*vcategory';
110
+    const RELATION_TABLE = '*PREFIX*vcategory_to_object';
111
+
112
+    const TAG_FAVORITE = '_$!<Favorite>!$_';
113
+
114
+    /**
115
+     * Constructor.
116
+     *
117
+     * @param TagMapper $mapper Instance of the TagMapper abstraction layer.
118
+     * @param string $user The user whose data the object will operate on.
119
+     * @param string $type The type of items for which tags will be loaded.
120
+     * @param array $defaultTags Tags that should be created at construction.
121
+     * @param boolean $includeShared Whether to include tags for items shared with this user by others.
122
+     */
123
+    public function __construct(TagMapper $mapper, $user, $type, $defaultTags = array(), $includeShared = false) {
124
+        $this->mapper = $mapper;
125
+        $this->user = $user;
126
+        $this->type = $type;
127
+        $this->includeShared = $includeShared;
128
+        $this->owners = array($this->user);
129
+        if ($this->includeShared) {
130
+            $this->owners = array_merge($this->owners, \OC\Share\Share::getSharedItemsOwners($this->user, $this->type, true));
131
+            $this->backend = \OC\Share\Share::getBackend($this->type);
132
+        }
133
+        $this->tags = $this->mapper->loadTags($this->owners, $this->type);
134
+
135
+        if(count($defaultTags) > 0 && count($this->tags) === 0) {
136
+            $this->addMultiple($defaultTags, true);
137
+        }
138
+    }
139
+
140
+    /**
141
+     * Check if any tags are saved for this type and user.
142
+     *
143
+     * @return boolean.
144
+     */
145
+    public function isEmpty() {
146
+        return count($this->tags) === 0;
147
+    }
148
+
149
+    /**
150
+     * Returns an array mapping a given tag's properties to its values:
151
+     * ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype']
152
+     *
153
+     * @param string $id The ID of the tag that is going to be mapped
154
+     * @return array|false
155
+     */
156
+    public function getTag($id) {
157
+        $key = $this->getTagById($id);
158
+        if ($key !== false) {
159
+            return $this->tagMap($this->tags[$key]);
160
+        }
161
+        return false;
162
+    }
163
+
164
+    /**
165
+     * Get the tags for a specific user.
166
+     *
167
+     * This returns an array with maps containing each tag's properties:
168
+     * [
169
+     * 	['id' => 0, 'name' = 'First tag', 'owner' = 'User', 'type' => 'tagtype'],
170
+     * 	['id' => 1, 'name' = 'Shared tag', 'owner' = 'Other user', 'type' => 'tagtype'],
171
+     * ]
172
+     *
173
+     * @return array
174
+     */
175
+    public function getTags() {
176
+        if(!count($this->tags)) {
177
+            return array();
178
+        }
179
+
180
+        usort($this->tags, function($a, $b) {
181
+            return strnatcasecmp($a->getName(), $b->getName());
182
+        });
183
+        $tagMap = array();
184
+
185
+        foreach($this->tags as $tag) {
186
+            if($tag->getName() !== self::TAG_FAVORITE) {
187
+                $tagMap[] = $this->tagMap($tag);
188
+            }
189
+        }
190
+        return $tagMap;
191
+
192
+    }
193
+
194
+    /**
195
+     * Return only the tags owned by the given user, omitting any tags shared
196
+     * by other users.
197
+     *
198
+     * @param string $user The user whose tags are to be checked.
199
+     * @return array An array of Tag objects.
200
+     */
201
+    public function getTagsForUser($user) {
202
+        return array_filter($this->tags,
203
+            function($tag) use($user) {
204
+                return $tag->getOwner() === $user;
205
+            }
206
+        );
207
+    }
208
+
209
+    /**
210
+     * Get the list of tags for the given ids.
211
+     *
212
+     * @param array $objIds array of object ids
213
+     * @return array|boolean of tags id as key to array of tag names
214
+     * or false if an error occurred
215
+     */
216
+    public function getTagsForObjects(array $objIds) {
217
+        $entries = array();
218
+
219
+        try {
220
+            $conn = \OC::$server->getDatabaseConnection();
221
+            $chunks = array_chunk($objIds, 900, false);
222
+            foreach ($chunks as $chunk) {
223
+                $result = $conn->executeQuery(
224
+                    'SELECT `category`, `categoryid`, `objid` ' .
225
+                    'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' .
226
+                    'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)',
227
+                    array($this->user, $this->type, $chunk),
228
+                    array(null, null, IQueryBuilder::PARAM_INT_ARRAY)
229
+                );
230
+                while ($row = $result->fetch()) {
231
+                    $objId = (int)$row['objid'];
232
+                    if (!isset($entries[$objId])) {
233
+                        $entries[$objId] = array();
234
+                    }
235
+                    $entries[$objId][] = $row['category'];
236
+                }
237
+                if (\OCP\DB::isError($result)) {
238
+                    \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
239
+                    return false;
240
+                }
241
+            }
242
+        } catch(\Exception $e) {
243
+            \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
244
+                \OCP\Util::ERROR);
245
+            return false;
246
+        }
247
+
248
+        return $entries;
249
+    }
250
+
251
+    /**
252
+     * Get the a list if items tagged with $tag.
253
+     *
254
+     * Throws an exception if the tag could not be found.
255
+     *
256
+     * @param string $tag Tag id or name.
257
+     * @return array|false An array of object ids or false on error.
258
+     * @throws \Exception
259
+     */
260
+    public function getIdsForTag($tag) {
261
+        $result = null;
262
+        $tagId = false;
263
+        if(is_numeric($tag)) {
264
+            $tagId = $tag;
265
+        } elseif(is_string($tag)) {
266
+            $tag = trim($tag);
267
+            if($tag === '') {
268
+                \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
269
+                return false;
270
+            }
271
+            $tagId = $this->getTagId($tag);
272
+        }
273
+
274
+        if($tagId === false) {
275
+            $l10n = \OC::$server->getL10N('core');
276
+            throw new \Exception(
277
+                $l10n->t('Could not find category "%s"', $tag)
278
+            );
279
+        }
280
+
281
+        $ids = array();
282
+        $sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE
283
+            . '` WHERE `categoryid` = ?';
284
+
285
+        try {
286
+            $stmt = \OCP\DB::prepare($sql);
287
+            $result = $stmt->execute(array($tagId));
288
+            if (\OCP\DB::isError($result)) {
289
+                \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
290
+                return false;
291
+            }
292
+        } catch(\Exception $e) {
293
+            \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
294
+                \OCP\Util::ERROR);
295
+            return false;
296
+        }
297
+
298
+        if(!is_null($result)) {
299
+            while( $row = $result->fetchRow()) {
300
+                $id = (int)$row['objid'];
301
+
302
+                if ($this->includeShared) {
303
+                    // We have to check if we are really allowed to access the
304
+                    // items that are tagged with $tag. To that end, we ask the
305
+                    // corresponding sharing backend if the item identified by $id
306
+                    // is owned by any of $this->owners.
307
+                    foreach ($this->owners as $owner) {
308
+                        if ($this->backend->isValidSource($id, $owner)) {
309
+                            $ids[] = $id;
310
+                            break;
311
+                        }
312
+                    }
313
+                } else {
314
+                    $ids[] = $id;
315
+                }
316
+            }
317
+        }
318
+
319
+        return $ids;
320
+    }
321
+
322
+    /**
323
+     * Checks whether a tag is saved for the given user,
324
+     * disregarding the ones shared with him or her.
325
+     *
326
+     * @param string $name The tag name to check for.
327
+     * @param string $user The user whose tags are to be checked.
328
+     * @return bool
329
+     */
330
+    public function userHasTag($name, $user) {
331
+        $key = $this->array_searchi($name, $this->getTagsForUser($user));
332
+        return ($key !== false) ? $this->tags[$key]->getId() : false;
333
+    }
334
+
335
+    /**
336
+     * Checks whether a tag is saved for or shared with the current user.
337
+     *
338
+     * @param string $name The tag name to check for.
339
+     * @return bool
340
+     */
341
+    public function hasTag($name) {
342
+        return $this->getTagId($name) !== false;
343
+    }
344
+
345
+    /**
346
+     * Add a new tag.
347
+     *
348
+     * @param string $name A string with a name of the tag
349
+     * @return false|int the id of the added tag or false on error.
350
+     */
351
+    public function add($name) {
352
+        $name = trim($name);
353
+
354
+        if($name === '') {
355
+            \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
356
+            return false;
357
+        }
358
+        if($this->userHasTag($name, $this->user)) {
359
+            \OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', \OCP\Util::DEBUG);
360
+            return false;
361
+        }
362
+        try {
363
+            $tag = new Tag($this->user, $this->type, $name);
364
+            $tag = $this->mapper->insert($tag);
365
+            $this->tags[] = $tag;
366
+        } catch(\Exception $e) {
367
+            \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
368
+                \OCP\Util::ERROR);
369
+            return false;
370
+        }
371
+        \OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), \OCP\Util::DEBUG);
372
+        return $tag->getId();
373
+    }
374
+
375
+    /**
376
+     * Rename tag.
377
+     *
378
+     * @param string|integer $from The name or ID of the existing tag
379
+     * @param string $to The new name of the tag.
380
+     * @return bool
381
+     */
382
+    public function rename($from, $to) {
383
+        $from = trim($from);
384
+        $to = trim($to);
385
+
386
+        if($to === '' || $from === '') {
387
+            \OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
388
+            return false;
389
+        }
390
+
391
+        if (is_numeric($from)) {
392
+            $key = $this->getTagById($from);
393
+        } else {
394
+            $key = $this->getTagByName($from);
395
+        }
396
+        if($key === false) {
397
+            \OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', \OCP\Util::DEBUG);
398
+            return false;
399
+        }
400
+        $tag = $this->tags[$key];
401
+
402
+        if($this->userHasTag($to, $tag->getOwner())) {
403
+            \OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', \OCP\Util::DEBUG);
404
+            return false;
405
+        }
406
+
407
+        try {
408
+            $tag->setName($to);
409
+            $this->tags[$key] = $this->mapper->update($tag);
410
+        } catch(\Exception $e) {
411
+            \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
412
+                \OCP\Util::ERROR);
413
+            return false;
414
+        }
415
+        return true;
416
+    }
417
+
418
+    /**
419
+     * Add a list of new tags.
420
+     *
421
+     * @param string[] $names A string with a name or an array of strings containing
422
+     * the name(s) of the tag(s) to add.
423
+     * @param bool $sync When true, save the tags
424
+     * @param int|null $id int Optional object id to add to this|these tag(s)
425
+     * @return bool Returns false on error.
426
+     */
427
+    public function addMultiple($names, $sync=false, $id = null) {
428
+        if(!is_array($names)) {
429
+            $names = array($names);
430
+        }
431
+        $names = array_map('trim', $names);
432
+        array_filter($names);
433
+
434
+        $newones = array();
435
+        foreach($names as $name) {
436
+            if(!$this->hasTag($name) && $name !== '') {
437
+                $newones[] = new Tag($this->user, $this->type, $name);
438
+            }
439
+            if(!is_null($id) ) {
440
+                // Insert $objectid, $categoryid  pairs if not exist.
441
+                self::$relations[] = array('objid' => $id, 'tag' => $name);
442
+            }
443
+        }
444
+        $this->tags = array_merge($this->tags, $newones);
445
+        if($sync === true) {
446
+            $this->save();
447
+        }
448
+
449
+        return true;
450
+    }
451
+
452
+    /**
453
+     * Save the list of tags and their object relations
454
+     */
455
+    protected function save() {
456
+        if(is_array($this->tags)) {
457
+            foreach($this->tags as $tag) {
458
+                try {
459
+                    if (!$this->mapper->tagExists($tag)) {
460
+                        $this->mapper->insert($tag);
461
+                    }
462
+                } catch(\Exception $e) {
463
+                    \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
464
+                        \OCP\Util::ERROR);
465
+                }
466
+            }
467
+
468
+            // reload tags to get the proper ids.
469
+            $this->tags = $this->mapper->loadTags($this->owners, $this->type);
470
+            \OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true),
471
+                \OCP\Util::DEBUG);
472
+            // Loop through temporarily cached objectid/tagname pairs
473
+            // and save relations.
474
+            $tags = $this->tags;
475
+            // For some reason this is needed or array_search(i) will return 0..?
476
+            ksort($tags);
477
+            foreach(self::$relations as $relation) {
478
+                $tagId = $this->getTagId($relation['tag']);
479
+                \OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, \OCP\Util::DEBUG);
480
+                if($tagId) {
481
+                    try {
482
+                        \OCP\DB::insertIfNotExist(self::RELATION_TABLE,
483
+                            array(
484
+                                'objid' => $relation['objid'],
485
+                                'categoryid' => $tagId,
486
+                                'type' => $this->type,
487
+                                ));
488
+                    } catch(\Exception $e) {
489
+                        \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
490
+                            \OCP\Util::ERROR);
491
+                    }
492
+                }
493
+            }
494
+            self::$relations = array(); // reset
495
+        } else {
496
+            \OCP\Util::writeLog('core', __METHOD__.', $this->tags is not an array! '
497
+                . print_r($this->tags, true), \OCP\Util::ERROR);
498
+        }
499
+    }
500
+
501
+    /**
502
+     * Delete tags and tag/object relations for a user.
503
+     *
504
+     * For hooking up on post_deleteUser
505
+     *
506
+     * @param array $arguments
507
+     */
508
+    public static function post_deleteUser($arguments) {
509
+        // Find all objectid/tagId pairs.
510
+        $result = null;
511
+        try {
512
+            $stmt = \OCP\DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` '
513
+                . 'WHERE `uid` = ?');
514
+            $result = $stmt->execute(array($arguments['uid']));
515
+            if (\OCP\DB::isError($result)) {
516
+                \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
517
+            }
518
+        } catch(\Exception $e) {
519
+            \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
520
+                \OCP\Util::ERROR);
521
+        }
522
+
523
+        if(!is_null($result)) {
524
+            try {
525
+                $stmt = \OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
526
+                    . 'WHERE `categoryid` = ?');
527
+                while( $row = $result->fetchRow()) {
528
+                    try {
529
+                        $stmt->execute(array($row['id']));
530
+                    } catch(\Exception $e) {
531
+                        \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
532
+                            \OCP\Util::ERROR);
533
+                    }
534
+                }
535
+            } catch(\Exception $e) {
536
+                \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
537
+                    \OCP\Util::ERROR);
538
+            }
539
+        }
540
+        try {
541
+            $stmt = \OCP\DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` '
542
+                . 'WHERE `uid` = ?');
543
+            $result = $stmt->execute(array($arguments['uid']));
544
+            if (\OCP\DB::isError($result)) {
545
+                \OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
546
+            }
547
+        } catch(\Exception $e) {
548
+            \OCP\Util::writeLog('core', __METHOD__ . ', exception: '
549
+                . $e->getMessage(), \OCP\Util::ERROR);
550
+        }
551
+    }
552
+
553
+    /**
554
+     * Delete tag/object relations from the db
555
+     *
556
+     * @param array $ids The ids of the objects
557
+     * @return boolean Returns false on error.
558
+     */
559
+    public function purgeObjects(array $ids) {
560
+        if(count($ids) === 0) {
561
+            // job done ;)
562
+            return true;
563
+        }
564
+        $updates = $ids;
565
+        try {
566
+            $query = 'DELETE FROM `' . self::RELATION_TABLE . '` ';
567
+            $query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) ';
568
+            $query .= 'AND `type`= ?';
569
+            $updates[] = $this->type;
570
+            $stmt = \OCP\DB::prepare($query);
571
+            $result = $stmt->execute($updates);
572
+            if (\OCP\DB::isError($result)) {
573
+                \OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
574
+                return false;
575
+            }
576
+        } catch(\Exception $e) {
577
+            \OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(),
578
+                \OCP\Util::ERROR);
579
+            return false;
580
+        }
581
+        return true;
582
+    }
583
+
584
+    /**
585
+     * Get favorites for an object type
586
+     *
587
+     * @return array|false An array of object ids.
588
+     */
589
+    public function getFavorites() {
590
+        try {
591
+            return $this->getIdsForTag(self::TAG_FAVORITE);
592
+        } catch(\Exception $e) {
593
+            \OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(),
594
+                \OCP\Util::DEBUG);
595
+            return array();
596
+        }
597
+    }
598
+
599
+    /**
600
+     * Add an object to favorites
601
+     *
602
+     * @param int $objid The id of the object
603
+     * @return boolean
604
+     */
605
+    public function addToFavorites($objid) {
606
+        if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) {
607
+            $this->add(self::TAG_FAVORITE);
608
+        }
609
+        return $this->tagAs($objid, self::TAG_FAVORITE);
610
+    }
611
+
612
+    /**
613
+     * Remove an object from favorites
614
+     *
615
+     * @param int $objid The id of the object
616
+     * @return boolean
617
+     */
618
+    public function removeFromFavorites($objid) {
619
+        return $this->unTag($objid, self::TAG_FAVORITE);
620
+    }
621
+
622
+    /**
623
+     * Creates a tag/object relation.
624
+     *
625
+     * @param int $objid The id of the object
626
+     * @param string $tag The id or name of the tag
627
+     * @return boolean Returns false on error.
628
+     */
629
+    public function tagAs($objid, $tag) {
630
+        if(is_string($tag) && !is_numeric($tag)) {
631
+            $tag = trim($tag);
632
+            if($tag === '') {
633
+                \OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
634
+                return false;
635
+            }
636
+            if(!$this->hasTag($tag)) {
637
+                $this->add($tag);
638
+            }
639
+            $tagId =  $this->getTagId($tag);
640
+        } else {
641
+            $tagId = $tag;
642
+        }
643
+        try {
644
+            \OCP\DB::insertIfNotExist(self::RELATION_TABLE,
645
+                array(
646
+                    'objid' => $objid,
647
+                    'categoryid' => $tagId,
648
+                    'type' => $this->type,
649
+                ));
650
+        } catch(\Exception $e) {
651
+            \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
652
+                \OCP\Util::ERROR);
653
+            return false;
654
+        }
655
+        return true;
656
+    }
657
+
658
+    /**
659
+     * Delete single tag/object relation from the db
660
+     *
661
+     * @param int $objid The id of the object
662
+     * @param string $tag The id or name of the tag
663
+     * @return boolean
664
+     */
665
+    public function unTag($objid, $tag) {
666
+        if(is_string($tag) && !is_numeric($tag)) {
667
+            $tag = trim($tag);
668
+            if($tag === '') {
669
+                \OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', \OCP\Util::DEBUG);
670
+                return false;
671
+            }
672
+            $tagId =  $this->getTagId($tag);
673
+        } else {
674
+            $tagId = $tag;
675
+        }
676
+
677
+        try {
678
+            $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
679
+                    . 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?';
680
+            $stmt = \OCP\DB::prepare($sql);
681
+            $stmt->execute(array($objid, $tagId, $this->type));
682
+        } catch(\Exception $e) {
683
+            \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
684
+                \OCP\Util::ERROR);
685
+            return false;
686
+        }
687
+        return true;
688
+    }
689
+
690
+    /**
691
+     * Delete tags from the database.
692
+     *
693
+     * @param string[]|integer[] $names An array of tags (names or IDs) to delete
694
+     * @return bool Returns false on error
695
+     */
696
+    public function delete($names) {
697
+        if(!is_array($names)) {
698
+            $names = array($names);
699
+        }
700
+
701
+        $names = array_map('trim', $names);
702
+        array_filter($names);
703
+
704
+        \OCP\Util::writeLog('core', __METHOD__ . ', before: '
705
+            . print_r($this->tags, true), \OCP\Util::DEBUG);
706
+        foreach($names as $name) {
707
+            $id = null;
708
+
709
+            if (is_numeric($name)) {
710
+                $key = $this->getTagById($name);
711
+            } else {
712
+                $key = $this->getTagByName($name);
713
+            }
714
+            if ($key !== false) {
715
+                $tag = $this->tags[$key];
716
+                $id = $tag->getId();
717
+                unset($this->tags[$key]);
718
+                $this->mapper->delete($tag);
719
+            } else {
720
+                \OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name
721
+                    . ': not found.', \OCP\Util::ERROR);
722
+            }
723
+            if(!is_null($id) && $id !== false) {
724
+                try {
725
+                    $sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
726
+                            . 'WHERE `categoryid` = ?';
727
+                    $stmt = \OCP\DB::prepare($sql);
728
+                    $result = $stmt->execute(array($id));
729
+                    if (\OCP\DB::isError($result)) {
730
+                        \OCP\Util::writeLog('core',
731
+                            __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(),
732
+                            \OCP\Util::ERROR);
733
+                        return false;
734
+                    }
735
+                } catch(\Exception $e) {
736
+                    \OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
737
+                        \OCP\Util::ERROR);
738
+                    return false;
739
+                }
740
+            }
741
+        }
742
+        return true;
743
+    }
744
+
745
+    // case-insensitive array_search
746
+    protected function array_searchi($needle, $haystack, $mem='getName') {
747
+        if(!is_array($haystack)) {
748
+            return false;
749
+        }
750
+        return array_search(strtolower($needle), array_map(
751
+            function($tag) use($mem) {
752
+                return strtolower(call_user_func(array($tag, $mem)));
753
+            }, $haystack)
754
+        );
755
+    }
756
+
757
+    /**
758
+     * Get a tag's ID.
759
+     *
760
+     * @param string $name The tag name to look for.
761
+     * @return string|bool The tag's id or false if no matching tag is found.
762
+     */
763
+    private function getTagId($name) {
764
+        $key = $this->array_searchi($name, $this->tags);
765
+        if ($key !== false) {
766
+            return $this->tags[$key]->getId();
767
+        }
768
+        return false;
769
+    }
770
+
771
+    /**
772
+     * Get a tag by its name.
773
+     *
774
+     * @param string $name The tag name.
775
+     * @return integer|bool The tag object's offset within the $this->tags
776
+     *                      array or false if it doesn't exist.
777
+     */
778
+    private function getTagByName($name) {
779
+        return $this->array_searchi($name, $this->tags, 'getName');
780
+    }
781
+
782
+    /**
783
+     * Get a tag by its ID.
784
+     *
785
+     * @param string $id The tag ID to look for.
786
+     * @return integer|bool The tag object's offset within the $this->tags
787
+     *                      array or false if it doesn't exist.
788
+     */
789
+    private function getTagById($id) {
790
+        return $this->array_searchi($id, $this->tags, 'getId');
791
+    }
792
+
793
+    /**
794
+     * Returns an array mapping a given tag's properties to its values:
795
+     * ['id' => 0, 'name' = 'Tag', 'owner' = 'User', 'type' => 'tagtype']
796
+     *
797
+     * @param Tag $tag The tag that is going to be mapped
798
+     * @return array
799
+     */
800
+    private function tagMap(Tag $tag) {
801
+        return array(
802
+            'id'    => $tag->getId(),
803
+            'name'  => $tag->getName(),
804
+            'owner' => $tag->getOwner(),
805
+            'type'  => $tag->getType()
806
+        );
807
+    }
808 808
 }
Please login to merge, or discard this patch.
Spacing   +85 added lines, -85 removed lines patch added patch discarded remove patch
@@ -132,7 +132,7 @@  discard block
 block discarded – undo
132 132
 		}
133 133
 		$this->tags = $this->mapper->loadTags($this->owners, $this->type);
134 134
 
135
-		if(count($defaultTags) > 0 && count($this->tags) === 0) {
135
+		if (count($defaultTags) > 0 && count($this->tags) === 0) {
136 136
 			$this->addMultiple($defaultTags, true);
137 137
 		}
138 138
 	}
@@ -173,7 +173,7 @@  discard block
 block discarded – undo
173 173
 	* @return array
174 174
 	*/
175 175
 	public function getTags() {
176
-		if(!count($this->tags)) {
176
+		if (!count($this->tags)) {
177 177
 			return array();
178 178
 		}
179 179
 
@@ -182,8 +182,8 @@  discard block
 block discarded – undo
182 182
 		});
183 183
 		$tagMap = array();
184 184
 
185
-		foreach($this->tags as $tag) {
186
-			if($tag->getName() !== self::TAG_FAVORITE) {
185
+		foreach ($this->tags as $tag) {
186
+			if ($tag->getName() !== self::TAG_FAVORITE) {
187 187
 				$tagMap[] = $this->tagMap($tag);
188 188
 			}
189 189
 		}
@@ -221,25 +221,25 @@  discard block
 block discarded – undo
221 221
 			$chunks = array_chunk($objIds, 900, false);
222 222
 			foreach ($chunks as $chunk) {
223 223
 				$result = $conn->executeQuery(
224
-					'SELECT `category`, `categoryid`, `objid` ' .
225
-					'FROM `' . self::RELATION_TABLE . '` r, `' . self::TAG_TABLE . '` ' .
224
+					'SELECT `category`, `categoryid`, `objid` '.
225
+					'FROM `'.self::RELATION_TABLE.'` r, `'.self::TAG_TABLE.'` '.
226 226
 					'WHERE `categoryid` = `id` AND `uid` = ? AND r.`type` = ? AND `objid` IN (?)',
227 227
 					array($this->user, $this->type, $chunk),
228 228
 					array(null, null, IQueryBuilder::PARAM_INT_ARRAY)
229 229
 				);
230 230
 				while ($row = $result->fetch()) {
231
-					$objId = (int)$row['objid'];
231
+					$objId = (int) $row['objid'];
232 232
 					if (!isset($entries[$objId])) {
233 233
 						$entries[$objId] = array();
234 234
 					}
235 235
 					$entries[$objId][] = $row['category'];
236 236
 				}
237 237
 				if (\OCP\DB::isError($result)) {
238
-					\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
238
+					\OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
239 239
 					return false;
240 240
 				}
241 241
 			}
242
-		} catch(\Exception $e) {
242
+		} catch (\Exception $e) {
243 243
 			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
244 244
 				\OCP\Util::ERROR);
245 245
 			return false;
@@ -260,18 +260,18 @@  discard block
 block discarded – undo
260 260
 	public function getIdsForTag($tag) {
261 261
 		$result = null;
262 262
 		$tagId = false;
263
-		if(is_numeric($tag)) {
263
+		if (is_numeric($tag)) {
264 264
 			$tagId = $tag;
265
-		} elseif(is_string($tag)) {
265
+		} elseif (is_string($tag)) {
266 266
 			$tag = trim($tag);
267
-			if($tag === '') {
267
+			if ($tag === '') {
268 268
 				\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
269 269
 				return false;
270 270
 			}
271 271
 			$tagId = $this->getTagId($tag);
272 272
 		}
273 273
 
274
-		if($tagId === false) {
274
+		if ($tagId === false) {
275 275
 			$l10n = \OC::$server->getL10N('core');
276 276
 			throw new \Exception(
277 277
 				$l10n->t('Could not find category "%s"', $tag)
@@ -279,25 +279,25 @@  discard block
 block discarded – undo
279 279
 		}
280 280
 
281 281
 		$ids = array();
282
-		$sql = 'SELECT `objid` FROM `' . self::RELATION_TABLE
282
+		$sql = 'SELECT `objid` FROM `'.self::RELATION_TABLE
283 283
 			. '` WHERE `categoryid` = ?';
284 284
 
285 285
 		try {
286 286
 			$stmt = \OCP\DB::prepare($sql);
287 287
 			$result = $stmt->execute(array($tagId));
288 288
 			if (\OCP\DB::isError($result)) {
289
-				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
289
+				\OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
290 290
 				return false;
291 291
 			}
292
-		} catch(\Exception $e) {
292
+		} catch (\Exception $e) {
293 293
 			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
294 294
 				\OCP\Util::ERROR);
295 295
 			return false;
296 296
 		}
297 297
 
298
-		if(!is_null($result)) {
299
-			while( $row = $result->fetchRow()) {
300
-				$id = (int)$row['objid'];
298
+		if (!is_null($result)) {
299
+			while ($row = $result->fetchRow()) {
300
+				$id = (int) $row['objid'];
301 301
 
302 302
 				if ($this->includeShared) {
303 303
 					// We have to check if we are really allowed to access the
@@ -351,24 +351,24 @@  discard block
 block discarded – undo
351 351
 	public function add($name) {
352 352
 		$name = trim($name);
353 353
 
354
-		if($name === '') {
354
+		if ($name === '') {
355 355
 			\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
356 356
 			return false;
357 357
 		}
358
-		if($this->userHasTag($name, $this->user)) {
359
-			\OCP\Util::writeLog('core', __METHOD__.', name: ' . $name. ' exists already', \OCP\Util::DEBUG);
358
+		if ($this->userHasTag($name, $this->user)) {
359
+			\OCP\Util::writeLog('core', __METHOD__.', name: '.$name.' exists already', \OCP\Util::DEBUG);
360 360
 			return false;
361 361
 		}
362 362
 		try {
363 363
 			$tag = new Tag($this->user, $this->type, $name);
364 364
 			$tag = $this->mapper->insert($tag);
365 365
 			$this->tags[] = $tag;
366
-		} catch(\Exception $e) {
366
+		} catch (\Exception $e) {
367 367
 			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
368 368
 				\OCP\Util::ERROR);
369 369
 			return false;
370 370
 		}
371
-		\OCP\Util::writeLog('core', __METHOD__.', id: ' . $tag->getId(), \OCP\Util::DEBUG);
371
+		\OCP\Util::writeLog('core', __METHOD__.', id: '.$tag->getId(), \OCP\Util::DEBUG);
372 372
 		return $tag->getId();
373 373
 	}
374 374
 
@@ -383,7 +383,7 @@  discard block
 block discarded – undo
383 383
 		$from = trim($from);
384 384
 		$to = trim($to);
385 385
 
386
-		if($to === '' || $from === '') {
386
+		if ($to === '' || $from === '') {
387 387
 			\OCP\Util::writeLog('core', __METHOD__.', Cannot use empty tag names', \OCP\Util::DEBUG);
388 388
 			return false;
389 389
 		}
@@ -393,21 +393,21 @@  discard block
 block discarded – undo
393 393
 		} else {
394 394
 			$key = $this->getTagByName($from);
395 395
 		}
396
-		if($key === false) {
397
-			\OCP\Util::writeLog('core', __METHOD__.', tag: ' . $from. ' does not exist', \OCP\Util::DEBUG);
396
+		if ($key === false) {
397
+			\OCP\Util::writeLog('core', __METHOD__.', tag: '.$from.' does not exist', \OCP\Util::DEBUG);
398 398
 			return false;
399 399
 		}
400 400
 		$tag = $this->tags[$key];
401 401
 
402
-		if($this->userHasTag($to, $tag->getOwner())) {
403
-			\OCP\Util::writeLog('core', __METHOD__.', A tag named ' . $to. ' already exists for user ' . $tag->getOwner() . '.', \OCP\Util::DEBUG);
402
+		if ($this->userHasTag($to, $tag->getOwner())) {
403
+			\OCP\Util::writeLog('core', __METHOD__.', A tag named '.$to.' already exists for user '.$tag->getOwner().'.', \OCP\Util::DEBUG);
404 404
 			return false;
405 405
 		}
406 406
 
407 407
 		try {
408 408
 			$tag->setName($to);
409 409
 			$this->tags[$key] = $this->mapper->update($tag);
410
-		} catch(\Exception $e) {
410
+		} catch (\Exception $e) {
411 411
 			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
412 412
 				\OCP\Util::ERROR);
413 413
 			return false;
@@ -424,25 +424,25 @@  discard block
 block discarded – undo
424 424
 	* @param int|null $id int Optional object id to add to this|these tag(s)
425 425
 	* @return bool Returns false on error.
426 426
 	*/
427
-	public function addMultiple($names, $sync=false, $id = null) {
428
-		if(!is_array($names)) {
427
+	public function addMultiple($names, $sync = false, $id = null) {
428
+		if (!is_array($names)) {
429 429
 			$names = array($names);
430 430
 		}
431 431
 		$names = array_map('trim', $names);
432 432
 		array_filter($names);
433 433
 
434 434
 		$newones = array();
435
-		foreach($names as $name) {
436
-			if(!$this->hasTag($name) && $name !== '') {
435
+		foreach ($names as $name) {
436
+			if (!$this->hasTag($name) && $name !== '') {
437 437
 				$newones[] = new Tag($this->user, $this->type, $name);
438 438
 			}
439
-			if(!is_null($id) ) {
439
+			if (!is_null($id)) {
440 440
 				// Insert $objectid, $categoryid  pairs if not exist.
441 441
 				self::$relations[] = array('objid' => $id, 'tag' => $name);
442 442
 			}
443 443
 		}
444 444
 		$this->tags = array_merge($this->tags, $newones);
445
-		if($sync === true) {
445
+		if ($sync === true) {
446 446
 			$this->save();
447 447
 		}
448 448
 
@@ -453,13 +453,13 @@  discard block
 block discarded – undo
453 453
 	 * Save the list of tags and their object relations
454 454
 	 */
455 455
 	protected function save() {
456
-		if(is_array($this->tags)) {
457
-			foreach($this->tags as $tag) {
456
+		if (is_array($this->tags)) {
457
+			foreach ($this->tags as $tag) {
458 458
 				try {
459 459
 					if (!$this->mapper->tagExists($tag)) {
460 460
 						$this->mapper->insert($tag);
461 461
 					}
462
-				} catch(\Exception $e) {
462
+				} catch (\Exception $e) {
463 463
 					\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
464 464
 						\OCP\Util::ERROR);
465 465
 				}
@@ -467,17 +467,17 @@  discard block
 block discarded – undo
467 467
 
468 468
 			// reload tags to get the proper ids.
469 469
 			$this->tags = $this->mapper->loadTags($this->owners, $this->type);
470
-			\OCP\Util::writeLog('core', __METHOD__.', tags: ' . print_r($this->tags, true),
470
+			\OCP\Util::writeLog('core', __METHOD__.', tags: '.print_r($this->tags, true),
471 471
 				\OCP\Util::DEBUG);
472 472
 			// Loop through temporarily cached objectid/tagname pairs
473 473
 			// and save relations.
474 474
 			$tags = $this->tags;
475 475
 			// For some reason this is needed or array_search(i) will return 0..?
476 476
 			ksort($tags);
477
-			foreach(self::$relations as $relation) {
477
+			foreach (self::$relations as $relation) {
478 478
 				$tagId = $this->getTagId($relation['tag']);
479
-				\OCP\Util::writeLog('core', __METHOD__ . 'catid, ' . $relation['tag'] . ' ' . $tagId, \OCP\Util::DEBUG);
480
-				if($tagId) {
479
+				\OCP\Util::writeLog('core', __METHOD__.'catid, '.$relation['tag'].' '.$tagId, \OCP\Util::DEBUG);
480
+				if ($tagId) {
481 481
 					try {
482 482
 						\OCP\DB::insertIfNotExist(self::RELATION_TABLE,
483 483
 							array(
@@ -485,7 +485,7 @@  discard block
 block discarded – undo
485 485
 								'categoryid' => $tagId,
486 486
 								'type' => $this->type,
487 487
 								));
488
-					} catch(\Exception $e) {
488
+					} catch (\Exception $e) {
489 489
 						\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
490 490
 							\OCP\Util::ERROR);
491 491
 					}
@@ -509,43 +509,43 @@  discard block
 block discarded – undo
509 509
 		// Find all objectid/tagId pairs.
510 510
 		$result = null;
511 511
 		try {
512
-			$stmt = \OCP\DB::prepare('SELECT `id` FROM `' . self::TAG_TABLE . '` '
512
+			$stmt = \OCP\DB::prepare('SELECT `id` FROM `'.self::TAG_TABLE.'` '
513 513
 				. 'WHERE `uid` = ?');
514 514
 			$result = $stmt->execute(array($arguments['uid']));
515 515
 			if (\OCP\DB::isError($result)) {
516
-				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
516
+				\OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
517 517
 			}
518
-		} catch(\Exception $e) {
518
+		} catch (\Exception $e) {
519 519
 			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
520 520
 				\OCP\Util::ERROR);
521 521
 		}
522 522
 
523
-		if(!is_null($result)) {
523
+		if (!is_null($result)) {
524 524
 			try {
525
-				$stmt = \OCP\DB::prepare('DELETE FROM `' . self::RELATION_TABLE . '` '
525
+				$stmt = \OCP\DB::prepare('DELETE FROM `'.self::RELATION_TABLE.'` '
526 526
 					. 'WHERE `categoryid` = ?');
527
-				while( $row = $result->fetchRow()) {
527
+				while ($row = $result->fetchRow()) {
528 528
 					try {
529 529
 						$stmt->execute(array($row['id']));
530
-					} catch(\Exception $e) {
530
+					} catch (\Exception $e) {
531 531
 						\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
532 532
 							\OCP\Util::ERROR);
533 533
 					}
534 534
 				}
535
-			} catch(\Exception $e) {
535
+			} catch (\Exception $e) {
536 536
 				\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
537 537
 					\OCP\Util::ERROR);
538 538
 			}
539 539
 		}
540 540
 		try {
541
-			$stmt = \OCP\DB::prepare('DELETE FROM `' . self::TAG_TABLE . '` '
541
+			$stmt = \OCP\DB::prepare('DELETE FROM `'.self::TAG_TABLE.'` '
542 542
 				. 'WHERE `uid` = ?');
543 543
 			$result = $stmt->execute(array($arguments['uid']));
544 544
 			if (\OCP\DB::isError($result)) {
545
-				\OCP\Util::writeLog('core', __METHOD__. ', DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
545
+				\OCP\Util::writeLog('core', __METHOD__.', DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
546 546
 			}
547
-		} catch(\Exception $e) {
548
-			\OCP\Util::writeLog('core', __METHOD__ . ', exception: '
547
+		} catch (\Exception $e) {
548
+			\OCP\Util::writeLog('core', __METHOD__.', exception: '
549 549
 				. $e->getMessage(), \OCP\Util::ERROR);
550 550
 		}
551 551
 	}
@@ -557,24 +557,24 @@  discard block
 block discarded – undo
557 557
 	* @return boolean Returns false on error.
558 558
 	*/
559 559
 	public function purgeObjects(array $ids) {
560
-		if(count($ids) === 0) {
560
+		if (count($ids) === 0) {
561 561
 			// job done ;)
562 562
 			return true;
563 563
 		}
564 564
 		$updates = $ids;
565 565
 		try {
566
-			$query = 'DELETE FROM `' . self::RELATION_TABLE . '` ';
567
-			$query .= 'WHERE `objid` IN (' . str_repeat('?,', count($ids)-1) . '?) ';
566
+			$query = 'DELETE FROM `'.self::RELATION_TABLE.'` ';
567
+			$query .= 'WHERE `objid` IN ('.str_repeat('?,', count($ids) - 1).'?) ';
568 568
 			$query .= 'AND `type`= ?';
569 569
 			$updates[] = $this->type;
570 570
 			$stmt = \OCP\DB::prepare($query);
571 571
 			$result = $stmt->execute($updates);
572 572
 			if (\OCP\DB::isError($result)) {
573
-				\OCP\Util::writeLog('core', __METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
573
+				\OCP\Util::writeLog('core', __METHOD__.'DB error: '.\OCP\DB::getErrorMessage(), \OCP\Util::ERROR);
574 574
 				return false;
575 575
 			}
576
-		} catch(\Exception $e) {
577
-			\OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(),
576
+		} catch (\Exception $e) {
577
+			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
578 578
 				\OCP\Util::ERROR);
579 579
 			return false;
580 580
 		}
@@ -589,8 +589,8 @@  discard block
 block discarded – undo
589 589
 	public function getFavorites() {
590 590
 		try {
591 591
 			return $this->getIdsForTag(self::TAG_FAVORITE);
592
-		} catch(\Exception $e) {
593
-			\OCP\Util::writeLog('core', __METHOD__.', exception: ' . $e->getMessage(),
592
+		} catch (\Exception $e) {
593
+			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
594 594
 				\OCP\Util::DEBUG);
595 595
 			return array();
596 596
 		}
@@ -603,7 +603,7 @@  discard block
 block discarded – undo
603 603
 	* @return boolean
604 604
 	*/
605 605
 	public function addToFavorites($objid) {
606
-		if(!$this->userHasTag(self::TAG_FAVORITE, $this->user)) {
606
+		if (!$this->userHasTag(self::TAG_FAVORITE, $this->user)) {
607 607
 			$this->add(self::TAG_FAVORITE);
608 608
 		}
609 609
 		return $this->tagAs($objid, self::TAG_FAVORITE);
@@ -627,16 +627,16 @@  discard block
 block discarded – undo
627 627
 	* @return boolean Returns false on error.
628 628
 	*/
629 629
 	public function tagAs($objid, $tag) {
630
-		if(is_string($tag) && !is_numeric($tag)) {
630
+		if (is_string($tag) && !is_numeric($tag)) {
631 631
 			$tag = trim($tag);
632
-			if($tag === '') {
632
+			if ($tag === '') {
633 633
 				\OCP\Util::writeLog('core', __METHOD__.', Cannot add an empty tag', \OCP\Util::DEBUG);
634 634
 				return false;
635 635
 			}
636
-			if(!$this->hasTag($tag)) {
636
+			if (!$this->hasTag($tag)) {
637 637
 				$this->add($tag);
638 638
 			}
639
-			$tagId =  $this->getTagId($tag);
639
+			$tagId = $this->getTagId($tag);
640 640
 		} else {
641 641
 			$tagId = $tag;
642 642
 		}
@@ -647,7 +647,7 @@  discard block
 block discarded – undo
647 647
 					'categoryid' => $tagId,
648 648
 					'type' => $this->type,
649 649
 				));
650
-		} catch(\Exception $e) {
650
+		} catch (\Exception $e) {
651 651
 			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
652 652
 				\OCP\Util::ERROR);
653 653
 			return false;
@@ -663,23 +663,23 @@  discard block
 block discarded – undo
663 663
 	* @return boolean
664 664
 	*/
665 665
 	public function unTag($objid, $tag) {
666
-		if(is_string($tag) && !is_numeric($tag)) {
666
+		if (is_string($tag) && !is_numeric($tag)) {
667 667
 			$tag = trim($tag);
668
-			if($tag === '') {
668
+			if ($tag === '') {
669 669
 				\OCP\Util::writeLog('core', __METHOD__.', Tag name is empty', \OCP\Util::DEBUG);
670 670
 				return false;
671 671
 			}
672
-			$tagId =  $this->getTagId($tag);
672
+			$tagId = $this->getTagId($tag);
673 673
 		} else {
674 674
 			$tagId = $tag;
675 675
 		}
676 676
 
677 677
 		try {
678
-			$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
678
+			$sql = 'DELETE FROM `'.self::RELATION_TABLE.'` '
679 679
 					. 'WHERE `objid` = ? AND `categoryid` = ? AND `type` = ?';
680 680
 			$stmt = \OCP\DB::prepare($sql);
681 681
 			$stmt->execute(array($objid, $tagId, $this->type));
682
-		} catch(\Exception $e) {
682
+		} catch (\Exception $e) {
683 683
 			\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
684 684
 				\OCP\Util::ERROR);
685 685
 			return false;
@@ -694,16 +694,16 @@  discard block
 block discarded – undo
694 694
 	* @return bool Returns false on error
695 695
 	*/
696 696
 	public function delete($names) {
697
-		if(!is_array($names)) {
697
+		if (!is_array($names)) {
698 698
 			$names = array($names);
699 699
 		}
700 700
 
701 701
 		$names = array_map('trim', $names);
702 702
 		array_filter($names);
703 703
 
704
-		\OCP\Util::writeLog('core', __METHOD__ . ', before: '
704
+		\OCP\Util::writeLog('core', __METHOD__.', before: '
705 705
 			. print_r($this->tags, true), \OCP\Util::DEBUG);
706
-		foreach($names as $name) {
706
+		foreach ($names as $name) {
707 707
 			$id = null;
708 708
 
709 709
 			if (is_numeric($name)) {
@@ -717,22 +717,22 @@  discard block
 block discarded – undo
717 717
 				unset($this->tags[$key]);
718 718
 				$this->mapper->delete($tag);
719 719
 			} else {
720
-				\OCP\Util::writeLog('core', __METHOD__ . 'Cannot delete tag ' . $name
720
+				\OCP\Util::writeLog('core', __METHOD__.'Cannot delete tag '.$name
721 721
 					. ': not found.', \OCP\Util::ERROR);
722 722
 			}
723
-			if(!is_null($id) && $id !== false) {
723
+			if (!is_null($id) && $id !== false) {
724 724
 				try {
725
-					$sql = 'DELETE FROM `' . self::RELATION_TABLE . '` '
725
+					$sql = 'DELETE FROM `'.self::RELATION_TABLE.'` '
726 726
 							. 'WHERE `categoryid` = ?';
727 727
 					$stmt = \OCP\DB::prepare($sql);
728 728
 					$result = $stmt->execute(array($id));
729 729
 					if (\OCP\DB::isError($result)) {
730 730
 						\OCP\Util::writeLog('core',
731
-							__METHOD__. 'DB error: ' . \OCP\DB::getErrorMessage(),
731
+							__METHOD__.'DB error: '.\OCP\DB::getErrorMessage(),
732 732
 							\OCP\Util::ERROR);
733 733
 						return false;
734 734
 					}
735
-				} catch(\Exception $e) {
735
+				} catch (\Exception $e) {
736 736
 					\OCP\Util::writeLog('core', __METHOD__.', exception: '.$e->getMessage(),
737 737
 						\OCP\Util::ERROR);
738 738
 					return false;
@@ -743,8 +743,8 @@  discard block
 block discarded – undo
743 743
 	}
744 744
 
745 745
 	// case-insensitive array_search
746
-	protected function array_searchi($needle, $haystack, $mem='getName') {
747
-		if(!is_array($haystack)) {
746
+	protected function array_searchi($needle, $haystack, $mem = 'getName') {
747
+		if (!is_array($haystack)) {
748 748
 			return false;
749 749
 		}
750 750
 		return array_search(strtolower($needle), array_map(
Please login to merge, or discard this patch.
lib/private/Template/Base.php 4 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -103,7 +103,7 @@
 block discarded – undo
103 103
 	/**
104 104
 	 * Appends a variable
105 105
 	 * @param string $key key
106
-	 * @param mixed $value value
106
+	 * @param string $value value
107 107
 	 * @return boolean|null
108 108
 	 *
109 109
 	 * This function assigns a variable in an array context. If the key already
Please login to merge, or discard this patch.
Indentation   +153 added lines, -153 removed lines patch added patch discarded remove patch
@@ -29,158 +29,158 @@
 block discarded – undo
29 29
 namespace OC\Template;
30 30
 
31 31
 class Base {
32
-	private $template; // The template
33
-	private $vars; // Vars
34
-
35
-	/** @var \OCP\IL10N */
36
-	private $l10n;
37
-
38
-	/** @var \OC_Defaults */
39
-	private $theme;
40
-
41
-	/**
42
-	 * @param string $template
43
-	 * @param string $requestToken
44
-	 * @param \OCP\IL10N $l10n
45
-	 * @param \OC_Defaults $theme
46
-	 */
47
-	public function __construct($template, $requestToken, $l10n, $theme ) {
48
-		$this->vars = array();
49
-		$this->vars['requesttoken'] = $requestToken;
50
-		$this->l10n = $l10n;
51
-		$this->template = $template;
52
-		$this->theme = $theme;
53
-	}
54
-
55
-	/**
56
-	 * @param string $serverRoot
57
-	 * @param string|false $app_dir
58
-	 * @param string $theme
59
-	 * @param string $app
60
-	 * @return string[]
61
-	 */
62
-	protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
63
-		// Check if the app is in the app folder or in the root
64
-		if( file_exists($app_dir.'/templates/' )) {
65
-			return [
66
-				$serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
67
-				$app_dir.'/templates/',
68
-			];
69
-		}
70
-		return [
71
-			$serverRoot.'/themes/'.$theme.'/'.$app.'/templates/',
72
-			$serverRoot.'/'.$app.'/templates/',
73
-		];
74
-	}
75
-
76
-	/**
77
-	 * @param string $serverRoot
78
-	 * @param string $theme
79
-	 * @return string[]
80
-	 */
81
-	protected function getCoreTemplateDirs($theme, $serverRoot) {
82
-		return [
83
-			$serverRoot.'/themes/'.$theme.'/core/templates/',
84
-			$serverRoot.'/core/templates/',
85
-		];
86
-	}
87
-
88
-	/**
89
-	 * Assign variables
90
-	 * @param string $key key
91
-	 * @param array|bool|integer|string $value value
92
-	 * @return bool
93
-	 *
94
-	 * This function assigns a variable. It can be accessed via $_[$key] in
95
-	 * the template.
96
-	 *
97
-	 * If the key existed before, it will be overwritten
98
-	 */
99
-	public function assign( $key, $value) {
100
-		$this->vars[$key] = $value;
101
-		return true;
102
-	}
103
-
104
-	/**
105
-	 * Appends a variable
106
-	 * @param string $key key
107
-	 * @param mixed $value value
108
-	 * @return boolean|null
109
-	 *
110
-	 * This function assigns a variable in an array context. If the key already
111
-	 * exists, the value will be appended. It can be accessed via
112
-	 * $_[$key][$position] in the template.
113
-	 */
114
-	public function append( $key, $value ) {
115
-		if( array_key_exists( $key, $this->vars )) {
116
-			$this->vars[$key][] = $value;
117
-		}
118
-		else{
119
-			$this->vars[$key] = array( $value );
120
-		}
121
-	}
122
-
123
-	/**
124
-	 * Prints the proceeded template
125
-	 * @return bool
126
-	 *
127
-	 * This function proceeds the template and prints its output.
128
-	 */
129
-	public function printPage() {
130
-		$data = $this->fetchPage();
131
-		if( $data === false ) {
132
-			return false;
133
-		}
134
-		else{
135
-			print $data;
136
-			return true;
137
-		}
138
-	}
139
-
140
-	/**
141
-	 * Process the template
142
-	 *
143
-	 * @param array|null $additionalParams
144
-	 * @return string This function processes the template.
145
-	 *
146
-	 * This function processes the template.
147
-	 */
148
-	public function fetchPage($additionalParams = null) {
149
-		return $this->load($this->template, $additionalParams);
150
-	}
151
-
152
-	/**
153
-	 * doing the actual work
154
-	 *
155
-	 * @param string $file
156
-	 * @param array|null $additionalParams
157
-	 * @return string content
158
-	 *
159
-	 * Includes the template file, fetches its output
160
-	 */
161
-	protected function load($file, $additionalParams = null) {
162
-		// Register the variables
163
-		$_ = $this->vars;
164
-		$l = $this->l10n;
165
-		$theme = $this->theme;
166
-
167
-		if( !is_null($additionalParams)) {
168
-			$_ = array_merge( $additionalParams, $this->vars );
169
-		}
170
-
171
-		// Include
172
-		ob_start();
173
-		try {
174
-			include $file;
175
-			$data = ob_get_contents();
176
-		} catch (\Exception $e) {
177
-			@ob_end_clean();
178
-			throw $e;
179
-		}
180
-		@ob_end_clean();
181
-
182
-		// Return data
183
-		return $data;
184
-	}
32
+    private $template; // The template
33
+    private $vars; // Vars
34
+
35
+    /** @var \OCP\IL10N */
36
+    private $l10n;
37
+
38
+    /** @var \OC_Defaults */
39
+    private $theme;
40
+
41
+    /**
42
+     * @param string $template
43
+     * @param string $requestToken
44
+     * @param \OCP\IL10N $l10n
45
+     * @param \OC_Defaults $theme
46
+     */
47
+    public function __construct($template, $requestToken, $l10n, $theme ) {
48
+        $this->vars = array();
49
+        $this->vars['requesttoken'] = $requestToken;
50
+        $this->l10n = $l10n;
51
+        $this->template = $template;
52
+        $this->theme = $theme;
53
+    }
54
+
55
+    /**
56
+     * @param string $serverRoot
57
+     * @param string|false $app_dir
58
+     * @param string $theme
59
+     * @param string $app
60
+     * @return string[]
61
+     */
62
+    protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
63
+        // Check if the app is in the app folder or in the root
64
+        if( file_exists($app_dir.'/templates/' )) {
65
+            return [
66
+                $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
67
+                $app_dir.'/templates/',
68
+            ];
69
+        }
70
+        return [
71
+            $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/',
72
+            $serverRoot.'/'.$app.'/templates/',
73
+        ];
74
+    }
75
+
76
+    /**
77
+     * @param string $serverRoot
78
+     * @param string $theme
79
+     * @return string[]
80
+     */
81
+    protected function getCoreTemplateDirs($theme, $serverRoot) {
82
+        return [
83
+            $serverRoot.'/themes/'.$theme.'/core/templates/',
84
+            $serverRoot.'/core/templates/',
85
+        ];
86
+    }
87
+
88
+    /**
89
+     * Assign variables
90
+     * @param string $key key
91
+     * @param array|bool|integer|string $value value
92
+     * @return bool
93
+     *
94
+     * This function assigns a variable. It can be accessed via $_[$key] in
95
+     * the template.
96
+     *
97
+     * If the key existed before, it will be overwritten
98
+     */
99
+    public function assign( $key, $value) {
100
+        $this->vars[$key] = $value;
101
+        return true;
102
+    }
103
+
104
+    /**
105
+     * Appends a variable
106
+     * @param string $key key
107
+     * @param mixed $value value
108
+     * @return boolean|null
109
+     *
110
+     * This function assigns a variable in an array context. If the key already
111
+     * exists, the value will be appended. It can be accessed via
112
+     * $_[$key][$position] in the template.
113
+     */
114
+    public function append( $key, $value ) {
115
+        if( array_key_exists( $key, $this->vars )) {
116
+            $this->vars[$key][] = $value;
117
+        }
118
+        else{
119
+            $this->vars[$key] = array( $value );
120
+        }
121
+    }
122
+
123
+    /**
124
+     * Prints the proceeded template
125
+     * @return bool
126
+     *
127
+     * This function proceeds the template and prints its output.
128
+     */
129
+    public function printPage() {
130
+        $data = $this->fetchPage();
131
+        if( $data === false ) {
132
+            return false;
133
+        }
134
+        else{
135
+            print $data;
136
+            return true;
137
+        }
138
+    }
139
+
140
+    /**
141
+     * Process the template
142
+     *
143
+     * @param array|null $additionalParams
144
+     * @return string This function processes the template.
145
+     *
146
+     * This function processes the template.
147
+     */
148
+    public function fetchPage($additionalParams = null) {
149
+        return $this->load($this->template, $additionalParams);
150
+    }
151
+
152
+    /**
153
+     * doing the actual work
154
+     *
155
+     * @param string $file
156
+     * @param array|null $additionalParams
157
+     * @return string content
158
+     *
159
+     * Includes the template file, fetches its output
160
+     */
161
+    protected function load($file, $additionalParams = null) {
162
+        // Register the variables
163
+        $_ = $this->vars;
164
+        $l = $this->l10n;
165
+        $theme = $this->theme;
166
+
167
+        if( !is_null($additionalParams)) {
168
+            $_ = array_merge( $additionalParams, $this->vars );
169
+        }
170
+
171
+        // Include
172
+        ob_start();
173
+        try {
174
+            include $file;
175
+            $data = ob_get_contents();
176
+        } catch (\Exception $e) {
177
+            @ob_end_clean();
178
+            throw $e;
179
+        }
180
+        @ob_end_clean();
181
+
182
+        // Return data
183
+        return $data;
184
+    }
185 185
 
186 186
 }
Please login to merge, or discard this patch.
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -44,7 +44,7 @@  discard block
 block discarded – undo
44 44
 	 * @param \OCP\IL10N $l10n
45 45
 	 * @param \OC_Defaults $theme
46 46
 	 */
47
-	public function __construct($template, $requestToken, $l10n, $theme ) {
47
+	public function __construct($template, $requestToken, $l10n, $theme) {
48 48
 		$this->vars = array();
49 49
 		$this->vars['requesttoken'] = $requestToken;
50 50
 		$this->l10n = $l10n;
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
 	 */
62 62
 	protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) {
63 63
 		// Check if the app is in the app folder or in the root
64
-		if( file_exists($app_dir.'/templates/' )) {
64
+		if (file_exists($app_dir.'/templates/')) {
65 65
 			return [
66 66
 				$serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/',
67 67
 				$app_dir.'/templates/',
@@ -96,7 +96,7 @@  discard block
 block discarded – undo
96 96
 	 *
97 97
 	 * If the key existed before, it will be overwritten
98 98
 	 */
99
-	public function assign( $key, $value) {
99
+	public function assign($key, $value) {
100 100
 		$this->vars[$key] = $value;
101 101
 		return true;
102 102
 	}
@@ -111,12 +111,12 @@  discard block
 block discarded – undo
111 111
 	 * exists, the value will be appended. It can be accessed via
112 112
 	 * $_[$key][$position] in the template.
113 113
 	 */
114
-	public function append( $key, $value ) {
115
-		if( array_key_exists( $key, $this->vars )) {
114
+	public function append($key, $value) {
115
+		if (array_key_exists($key, $this->vars)) {
116 116
 			$this->vars[$key][] = $value;
117 117
 		}
118
-		else{
119
-			$this->vars[$key] = array( $value );
118
+		else {
119
+			$this->vars[$key] = array($value);
120 120
 		}
121 121
 	}
122 122
 
@@ -128,10 +128,10 @@  discard block
 block discarded – undo
128 128
 	 */
129 129
 	public function printPage() {
130 130
 		$data = $this->fetchPage();
131
-		if( $data === false ) {
131
+		if ($data === false) {
132 132
 			return false;
133 133
 		}
134
-		else{
134
+		else {
135 135
 			print $data;
136 136
 			return true;
137 137
 		}
@@ -164,8 +164,8 @@  discard block
 block discarded – undo
164 164
 		$l = $this->l10n;
165 165
 		$theme = $this->theme;
166 166
 
167
-		if( !is_null($additionalParams)) {
168
-			$_ = array_merge( $additionalParams, $this->vars );
167
+		if (!is_null($additionalParams)) {
168
+			$_ = array_merge($additionalParams, $this->vars);
169 169
 		}
170 170
 
171 171
 		// Include
Please login to merge, or discard this patch.
Braces   +2 added lines, -4 removed lines patch added patch discarded remove patch
@@ -114,8 +114,7 @@  discard block
 block discarded – undo
114 114
 	public function append( $key, $value ) {
115 115
 		if( array_key_exists( $key, $this->vars )) {
116 116
 			$this->vars[$key][] = $value;
117
-		}
118
-		else{
117
+		} else{
119 118
 			$this->vars[$key] = array( $value );
120 119
 		}
121 120
 	}
@@ -130,8 +129,7 @@  discard block
 block discarded – undo
130 129
 		$data = $this->fetchPage();
131 130
 		if( $data === false ) {
132 131
 			return false;
133
-		}
134
-		else{
132
+		} else{
135 133
 			print $data;
136 134
 			return true;
137 135
 		}
Please login to merge, or discard this patch.
lib/public/App/ManagerEvent.php 3 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -46,7 +46,7 @@
 block discarded – undo
46 46
 	 * DispatcherEvent constructor.
47 47
 	 *
48 48
 	 * @param string $event
49
-	 * @param $appID
49
+	 * @param string $appID
50 50
 	 * @param \OCP\IGroup[] $groups
51 51
 	 * @since 9.0.0
52 52
 	 */
Please login to merge, or discard this patch.
Indentation   +51 added lines, -51 removed lines patch added patch discarded remove patch
@@ -32,61 +32,61 @@
 block discarded – undo
32 32
  */
33 33
 class ManagerEvent extends Event {
34 34
 
35
-	const EVENT_APP_ENABLE = 'OCP\App\IAppManager::enableApp';
36
-	const EVENT_APP_ENABLE_FOR_GROUPS = 'OCP\App\IAppManager::enableAppForGroups';
37
-	const EVENT_APP_DISABLE = 'OCP\App\IAppManager::disableApp';
35
+    const EVENT_APP_ENABLE = 'OCP\App\IAppManager::enableApp';
36
+    const EVENT_APP_ENABLE_FOR_GROUPS = 'OCP\App\IAppManager::enableAppForGroups';
37
+    const EVENT_APP_DISABLE = 'OCP\App\IAppManager::disableApp';
38 38
 
39
-	/**
40
-	 * @since 9.1.0
41
-	 */
42
-	const EVENT_APP_UPDATE = 'OCP\App\IAppManager::updateApp';
39
+    /**
40
+     * @since 9.1.0
41
+     */
42
+    const EVENT_APP_UPDATE = 'OCP\App\IAppManager::updateApp';
43 43
 
44
-	/** @var string */
45
-	protected $event;
46
-	/** @var string */
47
-	protected $appID;
48
-	/** @var \OCP\IGroup[] */
49
-	protected $groups;
44
+    /** @var string */
45
+    protected $event;
46
+    /** @var string */
47
+    protected $appID;
48
+    /** @var \OCP\IGroup[] */
49
+    protected $groups;
50 50
 
51
-	/**
52
-	 * DispatcherEvent constructor.
53
-	 *
54
-	 * @param string $event
55
-	 * @param $appID
56
-	 * @param \OCP\IGroup[] $groups
57
-	 * @since 9.0.0
58
-	 */
59
-	public function __construct($event, $appID, array $groups = null) {
60
-		$this->event = $event;
61
-		$this->appID = $appID;
62
-		$this->groups = $groups;
63
-	}
51
+    /**
52
+     * DispatcherEvent constructor.
53
+     *
54
+     * @param string $event
55
+     * @param $appID
56
+     * @param \OCP\IGroup[] $groups
57
+     * @since 9.0.0
58
+     */
59
+    public function __construct($event, $appID, array $groups = null) {
60
+        $this->event = $event;
61
+        $this->appID = $appID;
62
+        $this->groups = $groups;
63
+    }
64 64
 
65
-	/**
66
-	 * @return string
67
-	 * @since 9.0.0
68
-	 */
69
-	public function getEvent() {
70
-		return $this->event;
71
-	}
65
+    /**
66
+     * @return string
67
+     * @since 9.0.0
68
+     */
69
+    public function getEvent() {
70
+        return $this->event;
71
+    }
72 72
 
73
-	/**
74
-	 * @return string
75
-	 * @since 9.0.0
76
-	 */
77
-	public function getAppID() {
78
-		return $this->appID;
79
-	}
73
+    /**
74
+     * @return string
75
+     * @since 9.0.0
76
+     */
77
+    public function getAppID() {
78
+        return $this->appID;
79
+    }
80 80
 
81
-	/**
82
-	 * returns the group Ids
83
-	 * @return string[]
84
-	 * @since 9.0.0
85
-	 */
86
-	public function getGroups() {
87
-		return array_map(function ($group) {
88
-			/** @var \OCP\IGroup $group */
89
-			return $group->getGID();
90
-		}, $this->groups);
91
-	}
81
+    /**
82
+     * returns the group Ids
83
+     * @return string[]
84
+     * @since 9.0.0
85
+     */
86
+    public function getGroups() {
87
+        return array_map(function ($group) {
88
+            /** @var \OCP\IGroup $group */
89
+            return $group->getGID();
90
+        }, $this->groups);
91
+    }
92 92
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -84,7 +84,7 @@
 block discarded – undo
84 84
 	 * @since 9.0.0
85 85
 	 */
86 86
 	public function getGroups() {
87
-		return array_map(function ($group) {
87
+		return array_map(function($group) {
88 88
 			/** @var \OCP\IGroup $group */
89 89
 			return $group->getGID();
90 90
 		}, $this->groups);
Please login to merge, or discard this patch.