@@ -33,193 +33,193 @@ |
||
| 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 = []; |
|
| 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 | - public static 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 = []; |
|
| 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 | + public static 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 | } |
@@ -97,7 +97,7 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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; |
@@ -27,47 +27,47 @@ |
||
| 27 | 27 | namespace OC\Memcache; |
| 28 | 28 | |
| 29 | 29 | class NullCache extends Cache implements \OCP\IMemcache { |
| 30 | - public function get($key) { |
|
| 31 | - return null; |
|
| 32 | - } |
|
| 30 | + public function get($key) { |
|
| 31 | + return null; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - public function set($key, $value, $ttl = 0) { |
|
| 35 | - return true; |
|
| 36 | - } |
|
| 34 | + public function set($key, $value, $ttl = 0) { |
|
| 35 | + return true; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - public function hasKey($key) { |
|
| 39 | - return false; |
|
| 40 | - } |
|
| 38 | + public function hasKey($key) { |
|
| 39 | + return false; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - public function remove($key) { |
|
| 43 | - return true; |
|
| 44 | - } |
|
| 42 | + public function remove($key) { |
|
| 43 | + return true; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - public function add($key, $value, $ttl = 0) { |
|
| 47 | - return true; |
|
| 48 | - } |
|
| 46 | + public function add($key, $value, $ttl = 0) { |
|
| 47 | + return true; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - public function inc($key, $step = 1) { |
|
| 51 | - return true; |
|
| 52 | - } |
|
| 50 | + public function inc($key, $step = 1) { |
|
| 51 | + return true; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - public function dec($key, $step = 1) { |
|
| 55 | - return true; |
|
| 56 | - } |
|
| 54 | + public function dec($key, $step = 1) { |
|
| 55 | + return true; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - public function cas($key, $old, $new) { |
|
| 59 | - return true; |
|
| 60 | - } |
|
| 58 | + public function cas($key, $old, $new) { |
|
| 59 | + return true; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - public function cad($key, $old) { |
|
| 63 | - return true; |
|
| 64 | - } |
|
| 62 | + public function cad($key, $old) { |
|
| 63 | + return true; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - public function clear($prefix = '') { |
|
| 67 | - return true; |
|
| 68 | - } |
|
| 66 | + public function clear($prefix = '') { |
|
| 67 | + return true; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - public static function isAvailable() { |
|
| 71 | - return true; |
|
| 72 | - } |
|
| 70 | + public static function isAvailable() { |
|
| 71 | + return true; |
|
| 72 | + } |
|
| 73 | 73 | } |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | use CADTrait; |
| 38 | 38 | |
| 39 | 39 | public function get($key) { |
| 40 | - $result = apcu_fetch($this->getPrefix() . $key, $success); |
|
| 40 | + $result = apcu_fetch($this->getPrefix().$key, $success); |
|
| 41 | 41 | if (!$success) { |
| 42 | 42 | return null; |
| 43 | 43 | } |
@@ -45,24 +45,24 @@ discard block |
||
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | public function set($key, $value, $ttl = 0) { |
| 48 | - return apcu_store($this->getPrefix() . $key, $value, $ttl); |
|
| 48 | + return apcu_store($this->getPrefix().$key, $value, $ttl); |
|
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | public function hasKey($key) { |
| 52 | - return apcu_exists($this->getPrefix() . $key); |
|
| 52 | + return apcu_exists($this->getPrefix().$key); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | public function remove($key) { |
| 56 | - return apcu_delete($this->getPrefix() . $key); |
|
| 56 | + return apcu_delete($this->getPrefix().$key); |
|
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | public function clear($prefix = '') { |
| 60 | - $ns = $this->getPrefix() . $prefix; |
|
| 60 | + $ns = $this->getPrefix().$prefix; |
|
| 61 | 61 | $ns = preg_quote($ns, '/'); |
| 62 | 62 | if (class_exists('\APCIterator')) { |
| 63 | - $iter = new \APCIterator('user', '/^' . $ns . '/', APC_ITER_KEY); |
|
| 63 | + $iter = new \APCIterator('user', '/^'.$ns.'/', APC_ITER_KEY); |
|
| 64 | 64 | } else { |
| 65 | - $iter = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY); |
|
| 65 | + $iter = new \APCUIterator('/^'.$ns.'/', APC_ITER_KEY); |
|
| 66 | 66 | } |
| 67 | 67 | return apcu_delete($iter); |
| 68 | 68 | } |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | * @return bool |
| 77 | 77 | */ |
| 78 | 78 | public function add($key, $value, $ttl = 0) { |
| 79 | - return apcu_add($this->getPrefix() . $key, $value, $ttl); |
|
| 79 | + return apcu_add($this->getPrefix().$key, $value, $ttl); |
|
| 80 | 80 | } |
| 81 | 81 | |
| 82 | 82 | /** |
@@ -100,8 +100,8 @@ discard block |
||
| 100 | 100 | * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
| 101 | 101 | * for details |
| 102 | 102 | */ |
| 103 | - return apcu_exists($this->getPrefix() . $key) |
|
| 104 | - ? apcu_inc($this->getPrefix() . $key, $step) |
|
| 103 | + return apcu_exists($this->getPrefix().$key) |
|
| 104 | + ? apcu_inc($this->getPrefix().$key, $step) |
|
| 105 | 105 | : false; |
| 106 | 106 | } |
| 107 | 107 | |
@@ -125,8 +125,8 @@ discard block |
||
| 125 | 125 | * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
| 126 | 126 | * for details |
| 127 | 127 | */ |
| 128 | - return apcu_exists($this->getPrefix() . $key) |
|
| 129 | - ? apcu_dec($this->getPrefix() . $key, $step) |
|
| 128 | + return apcu_exists($this->getPrefix().$key) |
|
| 129 | + ? apcu_dec($this->getPrefix().$key, $step) |
|
| 130 | 130 | : false; |
| 131 | 131 | } |
| 132 | 132 | |
@@ -141,7 +141,7 @@ discard block |
||
| 141 | 141 | public function cas($key, $old, $new) { |
| 142 | 142 | // apc only does cas for ints |
| 143 | 143 | if (is_int($old) and is_int($new)) { |
| 144 | - return apcu_cas($this->getPrefix() . $key, $old, $new); |
|
| 144 | + return apcu_cas($this->getPrefix().$key, $old, $new); |
|
| 145 | 145 | } else { |
| 146 | 146 | return $this->casEmulated($key, $old, $new); |
| 147 | 147 | } |
@@ -32,140 +32,140 @@ |
||
| 32 | 32 | use OCP\IMemcache; |
| 33 | 33 | |
| 34 | 34 | class APCu extends Cache implements IMemcache { |
| 35 | - use CASTrait { |
|
| 36 | - cas as casEmulated; |
|
| 37 | - } |
|
| 35 | + use CASTrait { |
|
| 36 | + cas as casEmulated; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - use CADTrait; |
|
| 39 | + use CADTrait; |
|
| 40 | 40 | |
| 41 | - public function get($key) { |
|
| 42 | - $result = apcu_fetch($this->getPrefix() . $key, $success); |
|
| 43 | - if (!$success) { |
|
| 44 | - return null; |
|
| 45 | - } |
|
| 46 | - return $result; |
|
| 47 | - } |
|
| 41 | + public function get($key) { |
|
| 42 | + $result = apcu_fetch($this->getPrefix() . $key, $success); |
|
| 43 | + if (!$success) { |
|
| 44 | + return null; |
|
| 45 | + } |
|
| 46 | + return $result; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - public function set($key, $value, $ttl = 0) { |
|
| 50 | - return apcu_store($this->getPrefix() . $key, $value, $ttl); |
|
| 51 | - } |
|
| 49 | + public function set($key, $value, $ttl = 0) { |
|
| 50 | + return apcu_store($this->getPrefix() . $key, $value, $ttl); |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - public function hasKey($key) { |
|
| 54 | - return apcu_exists($this->getPrefix() . $key); |
|
| 55 | - } |
|
| 53 | + public function hasKey($key) { |
|
| 54 | + return apcu_exists($this->getPrefix() . $key); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - public function remove($key) { |
|
| 58 | - return apcu_delete($this->getPrefix() . $key); |
|
| 59 | - } |
|
| 57 | + public function remove($key) { |
|
| 58 | + return apcu_delete($this->getPrefix() . $key); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - public function clear($prefix = '') { |
|
| 62 | - $ns = $this->getPrefix() . $prefix; |
|
| 63 | - $ns = preg_quote($ns, '/'); |
|
| 64 | - if (class_exists('\APCIterator')) { |
|
| 65 | - $iter = new \APCIterator('user', '/^' . $ns . '/', APC_ITER_KEY); |
|
| 66 | - } else { |
|
| 67 | - $iter = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY); |
|
| 68 | - } |
|
| 69 | - return apcu_delete($iter); |
|
| 70 | - } |
|
| 61 | + public function clear($prefix = '') { |
|
| 62 | + $ns = $this->getPrefix() . $prefix; |
|
| 63 | + $ns = preg_quote($ns, '/'); |
|
| 64 | + if (class_exists('\APCIterator')) { |
|
| 65 | + $iter = new \APCIterator('user', '/^' . $ns . '/', APC_ITER_KEY); |
|
| 66 | + } else { |
|
| 67 | + $iter = new \APCUIterator('/^' . $ns . '/', APC_ITER_KEY); |
|
| 68 | + } |
|
| 69 | + return apcu_delete($iter); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Set a value in the cache if it's not already stored |
|
| 74 | - * |
|
| 75 | - * @param string $key |
|
| 76 | - * @param mixed $value |
|
| 77 | - * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
| 78 | - * @return bool |
|
| 79 | - */ |
|
| 80 | - public function add($key, $value, $ttl = 0) { |
|
| 81 | - return apcu_add($this->getPrefix() . $key, $value, $ttl); |
|
| 82 | - } |
|
| 72 | + /** |
|
| 73 | + * Set a value in the cache if it's not already stored |
|
| 74 | + * |
|
| 75 | + * @param string $key |
|
| 76 | + * @param mixed $value |
|
| 77 | + * @param int $ttl Time To Live in seconds. Defaults to 60*60*24 |
|
| 78 | + * @return bool |
|
| 79 | + */ |
|
| 80 | + public function add($key, $value, $ttl = 0) { |
|
| 81 | + return apcu_add($this->getPrefix() . $key, $value, $ttl); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Increase a stored number |
|
| 86 | - * |
|
| 87 | - * @param string $key |
|
| 88 | - * @param int $step |
|
| 89 | - * @return int | bool |
|
| 90 | - */ |
|
| 91 | - public function inc($key, $step = 1) { |
|
| 92 | - $this->add($key, 0); |
|
| 93 | - /** |
|
| 94 | - * TODO - hack around a PHP 7 specific issue in APCu |
|
| 95 | - * |
|
| 96 | - * on PHP 7 the apcu_inc method on a non-existing object will increment |
|
| 97 | - * "0" and result in "1" as value - therefore we check for existence |
|
| 98 | - * first |
|
| 99 | - * |
|
| 100 | - * on PHP 5.6 this is not the case |
|
| 101 | - * |
|
| 102 | - * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
|
| 103 | - * for details |
|
| 104 | - */ |
|
| 105 | - return apcu_exists($this->getPrefix() . $key) |
|
| 106 | - ? apcu_inc($this->getPrefix() . $key, $step) |
|
| 107 | - : false; |
|
| 108 | - } |
|
| 84 | + /** |
|
| 85 | + * Increase a stored number |
|
| 86 | + * |
|
| 87 | + * @param string $key |
|
| 88 | + * @param int $step |
|
| 89 | + * @return int | bool |
|
| 90 | + */ |
|
| 91 | + public function inc($key, $step = 1) { |
|
| 92 | + $this->add($key, 0); |
|
| 93 | + /** |
|
| 94 | + * TODO - hack around a PHP 7 specific issue in APCu |
|
| 95 | + * |
|
| 96 | + * on PHP 7 the apcu_inc method on a non-existing object will increment |
|
| 97 | + * "0" and result in "1" as value - therefore we check for existence |
|
| 98 | + * first |
|
| 99 | + * |
|
| 100 | + * on PHP 5.6 this is not the case |
|
| 101 | + * |
|
| 102 | + * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
|
| 103 | + * for details |
|
| 104 | + */ |
|
| 105 | + return apcu_exists($this->getPrefix() . $key) |
|
| 106 | + ? apcu_inc($this->getPrefix() . $key, $step) |
|
| 107 | + : false; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Decrease a stored number |
|
| 112 | - * |
|
| 113 | - * @param string $key |
|
| 114 | - * @param int $step |
|
| 115 | - * @return int | bool |
|
| 116 | - */ |
|
| 117 | - public function dec($key, $step = 1) { |
|
| 118 | - /** |
|
| 119 | - * TODO - hack around a PHP 7 specific issue in APCu |
|
| 120 | - * |
|
| 121 | - * on PHP 7 the apcu_dec method on a non-existing object will decrement |
|
| 122 | - * "0" and result in "-1" as value - therefore we check for existence |
|
| 123 | - * first |
|
| 124 | - * |
|
| 125 | - * on PHP 5.6 this is not the case |
|
| 126 | - * |
|
| 127 | - * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
|
| 128 | - * for details |
|
| 129 | - */ |
|
| 130 | - return apcu_exists($this->getPrefix() . $key) |
|
| 131 | - ? apcu_dec($this->getPrefix() . $key, $step) |
|
| 132 | - : false; |
|
| 133 | - } |
|
| 110 | + /** |
|
| 111 | + * Decrease a stored number |
|
| 112 | + * |
|
| 113 | + * @param string $key |
|
| 114 | + * @param int $step |
|
| 115 | + * @return int | bool |
|
| 116 | + */ |
|
| 117 | + public function dec($key, $step = 1) { |
|
| 118 | + /** |
|
| 119 | + * TODO - hack around a PHP 7 specific issue in APCu |
|
| 120 | + * |
|
| 121 | + * on PHP 7 the apcu_dec method on a non-existing object will decrement |
|
| 122 | + * "0" and result in "-1" as value - therefore we check for existence |
|
| 123 | + * first |
|
| 124 | + * |
|
| 125 | + * on PHP 5.6 this is not the case |
|
| 126 | + * |
|
| 127 | + * see https://github.com/krakjoe/apcu/issues/183#issuecomment-244038221 |
|
| 128 | + * for details |
|
| 129 | + */ |
|
| 130 | + return apcu_exists($this->getPrefix() . $key) |
|
| 131 | + ? apcu_dec($this->getPrefix() . $key, $step) |
|
| 132 | + : false; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - /** |
|
| 136 | - * Compare and set |
|
| 137 | - * |
|
| 138 | - * @param string $key |
|
| 139 | - * @param mixed $old |
|
| 140 | - * @param mixed $new |
|
| 141 | - * @return bool |
|
| 142 | - */ |
|
| 143 | - public function cas($key, $old, $new) { |
|
| 144 | - // apc only does cas for ints |
|
| 145 | - if (is_int($old) and is_int($new)) { |
|
| 146 | - return apcu_cas($this->getPrefix() . $key, $old, $new); |
|
| 147 | - } else { |
|
| 148 | - return $this->casEmulated($key, $old, $new); |
|
| 149 | - } |
|
| 150 | - } |
|
| 135 | + /** |
|
| 136 | + * Compare and set |
|
| 137 | + * |
|
| 138 | + * @param string $key |
|
| 139 | + * @param mixed $old |
|
| 140 | + * @param mixed $new |
|
| 141 | + * @return bool |
|
| 142 | + */ |
|
| 143 | + public function cas($key, $old, $new) { |
|
| 144 | + // apc only does cas for ints |
|
| 145 | + if (is_int($old) and is_int($new)) { |
|
| 146 | + return apcu_cas($this->getPrefix() . $key, $old, $new); |
|
| 147 | + } else { |
|
| 148 | + return $this->casEmulated($key, $old, $new); |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * @return bool |
|
| 154 | - */ |
|
| 155 | - public static function isAvailable() { |
|
| 156 | - if (!extension_loaded('apcu')) { |
|
| 157 | - return false; |
|
| 158 | - } elseif (!\OC::$server->get(IniGetWrapper::class)->getBool('apc.enabled')) { |
|
| 159 | - return false; |
|
| 160 | - } elseif (!\OC::$server->get(IniGetWrapper::class)->getBool('apc.enable_cli') && \OC::$CLI) { |
|
| 161 | - return false; |
|
| 162 | - } elseif ( |
|
| 163 | - version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 && |
|
| 164 | - version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1 |
|
| 165 | - ) { |
|
| 166 | - return false; |
|
| 167 | - } else { |
|
| 168 | - return true; |
|
| 169 | - } |
|
| 170 | - } |
|
| 152 | + /** |
|
| 153 | + * @return bool |
|
| 154 | + */ |
|
| 155 | + public static function isAvailable() { |
|
| 156 | + if (!extension_loaded('apcu')) { |
|
| 157 | + return false; |
|
| 158 | + } elseif (!\OC::$server->get(IniGetWrapper::class)->getBool('apc.enabled')) { |
|
| 159 | + return false; |
|
| 160 | + } elseif (!\OC::$server->get(IniGetWrapper::class)->getBool('apc.enable_cli') && \OC::$CLI) { |
|
| 161 | + return false; |
|
| 162 | + } elseif ( |
|
| 163 | + version_compare(phpversion('apc') ?: '0.0.0', '4.0.6') === -1 && |
|
| 164 | + version_compare(phpversion('apcu') ?: '0.0.0', '5.1.0') === -1 |
|
| 165 | + ) { |
|
| 166 | + return false; |
|
| 167 | + } else { |
|
| 168 | + return true; |
|
| 169 | + } |
|
| 170 | + } |
|
| 171 | 171 | } |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | IUserSession $userSession |
| 61 | 61 | ) { |
| 62 | 62 | $this->user = $user; |
| 63 | - $this->isAdmin = (bool)$isAdmin; |
|
| 63 | + $this->isAdmin = (bool) $isAdmin; |
|
| 64 | 64 | $this->groupManager = $groupManager; |
| 65 | 65 | $this->userSession = $userSession; |
| 66 | 66 | } |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | * @return array |
| 77 | 77 | */ |
| 78 | 78 | public function get($groupSearch = '', $userSearch = '') { |
| 79 | - $key = $groupSearch . '::' . $userSearch; |
|
| 79 | + $key = $groupSearch.'::'.$userSearch; |
|
| 80 | 80 | if (isset($this->metaData[$key])) { |
| 81 | 81 | return $this->metaData[$key]; |
| 82 | 82 | } |
@@ -35,175 +35,175 @@ |
||
| 35 | 35 | use OCP\IUserSession; |
| 36 | 36 | |
| 37 | 37 | class MetaData { |
| 38 | - public const SORT_NONE = 0; |
|
| 39 | - public const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends |
|
| 40 | - public const SORT_GROUPNAME = 2; |
|
| 41 | - |
|
| 42 | - /** @var string */ |
|
| 43 | - protected $user; |
|
| 44 | - /** @var bool */ |
|
| 45 | - protected $isAdmin; |
|
| 46 | - /** @var array */ |
|
| 47 | - protected $metaData = []; |
|
| 48 | - /** @var GroupManager */ |
|
| 49 | - protected $groupManager; |
|
| 50 | - /** @var bool */ |
|
| 51 | - protected $sorting = false; |
|
| 52 | - /** @var IUserSession */ |
|
| 53 | - protected $userSession; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @param string $user the uid of the current user |
|
| 57 | - * @param bool $isAdmin whether the current users is an admin |
|
| 58 | - * @param IGroupManager $groupManager |
|
| 59 | - * @param IUserSession $userSession |
|
| 60 | - */ |
|
| 61 | - public function __construct( |
|
| 62 | - $user, |
|
| 63 | - $isAdmin, |
|
| 64 | - IGroupManager $groupManager, |
|
| 65 | - IUserSession $userSession |
|
| 66 | - ) { |
|
| 67 | - $this->user = $user; |
|
| 68 | - $this->isAdmin = (bool)$isAdmin; |
|
| 69 | - $this->groupManager = $groupManager; |
|
| 70 | - $this->userSession = $userSession; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * returns an array with meta data about all available groups |
|
| 75 | - * the array is structured as follows: |
|
| 76 | - * [0] array containing meta data about admin groups |
|
| 77 | - * [1] array containing meta data about unprivileged groups |
|
| 78 | - * @param string $groupSearch only effective when instance was created with |
|
| 79 | - * isAdmin being true |
|
| 80 | - * @param string $userSearch the pattern users are search for |
|
| 81 | - * @return array |
|
| 82 | - */ |
|
| 83 | - public function get($groupSearch = '', $userSearch = '') { |
|
| 84 | - $key = $groupSearch . '::' . $userSearch; |
|
| 85 | - if (isset($this->metaData[$key])) { |
|
| 86 | - return $this->metaData[$key]; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - $adminGroups = []; |
|
| 90 | - $groups = []; |
|
| 91 | - $sortGroupsIndex = 0; |
|
| 92 | - $sortGroupsKeys = []; |
|
| 93 | - $sortAdminGroupsIndex = 0; |
|
| 94 | - $sortAdminGroupsKeys = []; |
|
| 95 | - |
|
| 96 | - foreach ($this->getGroups($groupSearch) as $group) { |
|
| 97 | - $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
| 98 | - if (strtolower($group->getGID()) !== 'admin') { |
|
| 99 | - $this->addEntry( |
|
| 100 | - $groups, |
|
| 101 | - $sortGroupsKeys, |
|
| 102 | - $sortGroupsIndex, |
|
| 103 | - $groupMetaData); |
|
| 104 | - } else { |
|
| 105 | - //admin group is hard coded to 'admin' for now. In future, |
|
| 106 | - //backends may define admin groups too. Then the if statement |
|
| 107 | - //has to be adjusted accordingly. |
|
| 108 | - $this->addEntry( |
|
| 109 | - $adminGroups, |
|
| 110 | - $sortAdminGroupsKeys, |
|
| 111 | - $sortAdminGroupsIndex, |
|
| 112 | - $groupMetaData); |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - //whether sorting is necessary is will be checked in sort() |
|
| 117 | - $this->sort($groups, $sortGroupsKeys); |
|
| 118 | - $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
| 119 | - |
|
| 120 | - $this->metaData[$key] = [$adminGroups, $groups]; |
|
| 121 | - return $this->metaData[$key]; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * sets the sort mode, see SORT_* constants for supported modes |
|
| 126 | - * |
|
| 127 | - * @param int $sortMode |
|
| 128 | - */ |
|
| 129 | - public function setSorting($sortMode) { |
|
| 130 | - switch ($sortMode) { |
|
| 131 | - case self::SORT_USERCOUNT: |
|
| 132 | - case self::SORT_GROUPNAME: |
|
| 133 | - $this->sorting = $sortMode; |
|
| 134 | - break; |
|
| 135 | - |
|
| 136 | - default: |
|
| 137 | - $this->sorting = self::SORT_NONE; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * adds an group entry to the resulting array |
|
| 143 | - * @param array $entries the resulting array, by reference |
|
| 144 | - * @param array $sortKeys the sort key array, by reference |
|
| 145 | - * @param int $sortIndex the sort key index, by reference |
|
| 146 | - * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
| 147 | - */ |
|
| 148 | - private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { |
|
| 149 | - $entries[] = $data; |
|
| 150 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 151 | - $sortKeys[$sortIndex] = $data['usercount']; |
|
| 152 | - $sortIndex++; |
|
| 153 | - } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
| 154 | - $sortKeys[$sortIndex] = $data['name']; |
|
| 155 | - $sortIndex++; |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * creates an array containing the group meta data |
|
| 161 | - * @param \OCP\IGroup $group |
|
| 162 | - * @param string $userSearch |
|
| 163 | - * @return array with the keys 'id', 'name', 'usercount' and 'disabled' |
|
| 164 | - */ |
|
| 165 | - private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { |
|
| 166 | - return [ |
|
| 167 | - 'id' => $group->getGID(), |
|
| 168 | - 'name' => $group->getDisplayName(), |
|
| 169 | - 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
| 170 | - 'disabled' => $group->countDisabled(), |
|
| 171 | - 'canAdd' => $group->canAddUser(), |
|
| 172 | - 'canRemove' => $group->canRemoveUser(), |
|
| 173 | - ]; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * sorts the result array, if applicable |
|
| 178 | - * @param array $entries the result array, by reference |
|
| 179 | - * @param array $sortKeys the array containing the sort keys |
|
| 180 | - * @param return null |
|
| 181 | - */ |
|
| 182 | - private function sort(&$entries, $sortKeys) { |
|
| 183 | - if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 184 | - array_multisort($sortKeys, SORT_DESC, $entries); |
|
| 185 | - } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
| 186 | - array_multisort($sortKeys, SORT_ASC, $entries); |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * returns the available groups |
|
| 192 | - * @param string $search a search string |
|
| 193 | - * @return \OCP\IGroup[] |
|
| 194 | - */ |
|
| 195 | - public function getGroups($search = '') { |
|
| 196 | - if ($this->isAdmin) { |
|
| 197 | - return $this->groupManager->search($search); |
|
| 198 | - } else { |
|
| 199 | - $userObject = $this->userSession->getUser(); |
|
| 200 | - if ($userObject !== null) { |
|
| 201 | - $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
| 202 | - } else { |
|
| 203 | - $groups = []; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - return $groups; |
|
| 207 | - } |
|
| 208 | - } |
|
| 38 | + public const SORT_NONE = 0; |
|
| 39 | + public const SORT_USERCOUNT = 1; // May have performance issues on LDAP backends |
|
| 40 | + public const SORT_GROUPNAME = 2; |
|
| 41 | + |
|
| 42 | + /** @var string */ |
|
| 43 | + protected $user; |
|
| 44 | + /** @var bool */ |
|
| 45 | + protected $isAdmin; |
|
| 46 | + /** @var array */ |
|
| 47 | + protected $metaData = []; |
|
| 48 | + /** @var GroupManager */ |
|
| 49 | + protected $groupManager; |
|
| 50 | + /** @var bool */ |
|
| 51 | + protected $sorting = false; |
|
| 52 | + /** @var IUserSession */ |
|
| 53 | + protected $userSession; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @param string $user the uid of the current user |
|
| 57 | + * @param bool $isAdmin whether the current users is an admin |
|
| 58 | + * @param IGroupManager $groupManager |
|
| 59 | + * @param IUserSession $userSession |
|
| 60 | + */ |
|
| 61 | + public function __construct( |
|
| 62 | + $user, |
|
| 63 | + $isAdmin, |
|
| 64 | + IGroupManager $groupManager, |
|
| 65 | + IUserSession $userSession |
|
| 66 | + ) { |
|
| 67 | + $this->user = $user; |
|
| 68 | + $this->isAdmin = (bool)$isAdmin; |
|
| 69 | + $this->groupManager = $groupManager; |
|
| 70 | + $this->userSession = $userSession; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * returns an array with meta data about all available groups |
|
| 75 | + * the array is structured as follows: |
|
| 76 | + * [0] array containing meta data about admin groups |
|
| 77 | + * [1] array containing meta data about unprivileged groups |
|
| 78 | + * @param string $groupSearch only effective when instance was created with |
|
| 79 | + * isAdmin being true |
|
| 80 | + * @param string $userSearch the pattern users are search for |
|
| 81 | + * @return array |
|
| 82 | + */ |
|
| 83 | + public function get($groupSearch = '', $userSearch = '') { |
|
| 84 | + $key = $groupSearch . '::' . $userSearch; |
|
| 85 | + if (isset($this->metaData[$key])) { |
|
| 86 | + return $this->metaData[$key]; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + $adminGroups = []; |
|
| 90 | + $groups = []; |
|
| 91 | + $sortGroupsIndex = 0; |
|
| 92 | + $sortGroupsKeys = []; |
|
| 93 | + $sortAdminGroupsIndex = 0; |
|
| 94 | + $sortAdminGroupsKeys = []; |
|
| 95 | + |
|
| 96 | + foreach ($this->getGroups($groupSearch) as $group) { |
|
| 97 | + $groupMetaData = $this->generateGroupMetaData($group, $userSearch); |
|
| 98 | + if (strtolower($group->getGID()) !== 'admin') { |
|
| 99 | + $this->addEntry( |
|
| 100 | + $groups, |
|
| 101 | + $sortGroupsKeys, |
|
| 102 | + $sortGroupsIndex, |
|
| 103 | + $groupMetaData); |
|
| 104 | + } else { |
|
| 105 | + //admin group is hard coded to 'admin' for now. In future, |
|
| 106 | + //backends may define admin groups too. Then the if statement |
|
| 107 | + //has to be adjusted accordingly. |
|
| 108 | + $this->addEntry( |
|
| 109 | + $adminGroups, |
|
| 110 | + $sortAdminGroupsKeys, |
|
| 111 | + $sortAdminGroupsIndex, |
|
| 112 | + $groupMetaData); |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + //whether sorting is necessary is will be checked in sort() |
|
| 117 | + $this->sort($groups, $sortGroupsKeys); |
|
| 118 | + $this->sort($adminGroups, $sortAdminGroupsKeys); |
|
| 119 | + |
|
| 120 | + $this->metaData[$key] = [$adminGroups, $groups]; |
|
| 121 | + return $this->metaData[$key]; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * sets the sort mode, see SORT_* constants for supported modes |
|
| 126 | + * |
|
| 127 | + * @param int $sortMode |
|
| 128 | + */ |
|
| 129 | + public function setSorting($sortMode) { |
|
| 130 | + switch ($sortMode) { |
|
| 131 | + case self::SORT_USERCOUNT: |
|
| 132 | + case self::SORT_GROUPNAME: |
|
| 133 | + $this->sorting = $sortMode; |
|
| 134 | + break; |
|
| 135 | + |
|
| 136 | + default: |
|
| 137 | + $this->sorting = self::SORT_NONE; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * adds an group entry to the resulting array |
|
| 143 | + * @param array $entries the resulting array, by reference |
|
| 144 | + * @param array $sortKeys the sort key array, by reference |
|
| 145 | + * @param int $sortIndex the sort key index, by reference |
|
| 146 | + * @param array $data the group's meta data as returned by generateGroupMetaData() |
|
| 147 | + */ |
|
| 148 | + private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) { |
|
| 149 | + $entries[] = $data; |
|
| 150 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 151 | + $sortKeys[$sortIndex] = $data['usercount']; |
|
| 152 | + $sortIndex++; |
|
| 153 | + } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
| 154 | + $sortKeys[$sortIndex] = $data['name']; |
|
| 155 | + $sortIndex++; |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * creates an array containing the group meta data |
|
| 161 | + * @param \OCP\IGroup $group |
|
| 162 | + * @param string $userSearch |
|
| 163 | + * @return array with the keys 'id', 'name', 'usercount' and 'disabled' |
|
| 164 | + */ |
|
| 165 | + private function generateGroupMetaData(\OCP\IGroup $group, $userSearch) { |
|
| 166 | + return [ |
|
| 167 | + 'id' => $group->getGID(), |
|
| 168 | + 'name' => $group->getDisplayName(), |
|
| 169 | + 'usercount' => $this->sorting === self::SORT_USERCOUNT ? $group->count($userSearch) : 0, |
|
| 170 | + 'disabled' => $group->countDisabled(), |
|
| 171 | + 'canAdd' => $group->canAddUser(), |
|
| 172 | + 'canRemove' => $group->canRemoveUser(), |
|
| 173 | + ]; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * sorts the result array, if applicable |
|
| 178 | + * @param array $entries the result array, by reference |
|
| 179 | + * @param array $sortKeys the array containing the sort keys |
|
| 180 | + * @param return null |
|
| 181 | + */ |
|
| 182 | + private function sort(&$entries, $sortKeys) { |
|
| 183 | + if ($this->sorting === self::SORT_USERCOUNT) { |
|
| 184 | + array_multisort($sortKeys, SORT_DESC, $entries); |
|
| 185 | + } elseif ($this->sorting === self::SORT_GROUPNAME) { |
|
| 186 | + array_multisort($sortKeys, SORT_ASC, $entries); |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * returns the available groups |
|
| 192 | + * @param string $search a search string |
|
| 193 | + * @return \OCP\IGroup[] |
|
| 194 | + */ |
|
| 195 | + public function getGroups($search = '') { |
|
| 196 | + if ($this->isAdmin) { |
|
| 197 | + return $this->groupManager->search($search); |
|
| 198 | + } else { |
|
| 199 | + $userObject = $this->userSession->getUser(); |
|
| 200 | + if ($userObject !== null) { |
|
| 201 | + $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($userObject); |
|
| 202 | + } else { |
|
| 203 | + $groups = []; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + return $groups; |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | 209 | } |
@@ -67,7 +67,7 @@ |
||
| 67 | 67 | * compared with \OC\Group\Backend::CREATE_GROUP etc. |
| 68 | 68 | */ |
| 69 | 69 | public function implementsActions($actions) { |
| 70 | - return (bool)($this->getSupportedActions() & $actions); |
|
| 70 | + return (bool) ($this->getSupportedActions() & $actions); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -29,107 +29,107 @@ |
||
| 29 | 29 | * Abstract base class for user management |
| 30 | 30 | */ |
| 31 | 31 | abstract class Backend implements \OCP\GroupInterface { |
| 32 | - /** |
|
| 33 | - * error code for functions not provided by the group backend |
|
| 34 | - */ |
|
| 35 | - public const NOT_IMPLEMENTED = -501; |
|
| 32 | + /** |
|
| 33 | + * error code for functions not provided by the group backend |
|
| 34 | + */ |
|
| 35 | + public const NOT_IMPLEMENTED = -501; |
|
| 36 | 36 | |
| 37 | - protected $possibleActions = [ |
|
| 38 | - self::CREATE_GROUP => 'createGroup', |
|
| 39 | - self::DELETE_GROUP => 'deleteGroup', |
|
| 40 | - self::ADD_TO_GROUP => 'addToGroup', |
|
| 41 | - self::REMOVE_FROM_GOUP => 'removeFromGroup', |
|
| 42 | - self::COUNT_USERS => 'countUsersInGroup', |
|
| 43 | - self::GROUP_DETAILS => 'getGroupDetails', |
|
| 44 | - self::IS_ADMIN => 'isAdmin', |
|
| 45 | - ]; |
|
| 37 | + protected $possibleActions = [ |
|
| 38 | + self::CREATE_GROUP => 'createGroup', |
|
| 39 | + self::DELETE_GROUP => 'deleteGroup', |
|
| 40 | + self::ADD_TO_GROUP => 'addToGroup', |
|
| 41 | + self::REMOVE_FROM_GOUP => 'removeFromGroup', |
|
| 42 | + self::COUNT_USERS => 'countUsersInGroup', |
|
| 43 | + self::GROUP_DETAILS => 'getGroupDetails', |
|
| 44 | + self::IS_ADMIN => 'isAdmin', |
|
| 45 | + ]; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Get all supported actions |
|
| 49 | - * @return int bitwise-or'ed actions |
|
| 50 | - * |
|
| 51 | - * Returns the supported actions as int to be |
|
| 52 | - * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
| 53 | - */ |
|
| 54 | - public function getSupportedActions() { |
|
| 55 | - $actions = 0; |
|
| 56 | - foreach ($this->possibleActions as $action => $methodName) { |
|
| 57 | - if (method_exists($this, $methodName)) { |
|
| 58 | - $actions |= $action; |
|
| 59 | - } |
|
| 60 | - } |
|
| 47 | + /** |
|
| 48 | + * Get all supported actions |
|
| 49 | + * @return int bitwise-or'ed actions |
|
| 50 | + * |
|
| 51 | + * Returns the supported actions as int to be |
|
| 52 | + * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
| 53 | + */ |
|
| 54 | + public function getSupportedActions() { |
|
| 55 | + $actions = 0; |
|
| 56 | + foreach ($this->possibleActions as $action => $methodName) { |
|
| 57 | + if (method_exists($this, $methodName)) { |
|
| 58 | + $actions |= $action; |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - return $actions; |
|
| 63 | - } |
|
| 62 | + return $actions; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Check if backend implements actions |
|
| 67 | - * @param int $actions bitwise-or'ed actions |
|
| 68 | - * @return bool |
|
| 69 | - * |
|
| 70 | - * Returns the supported actions as int to be |
|
| 71 | - * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
| 72 | - */ |
|
| 73 | - public function implementsActions($actions) { |
|
| 74 | - return (bool)($this->getSupportedActions() & $actions); |
|
| 75 | - } |
|
| 65 | + /** |
|
| 66 | + * Check if backend implements actions |
|
| 67 | + * @param int $actions bitwise-or'ed actions |
|
| 68 | + * @return bool |
|
| 69 | + * |
|
| 70 | + * Returns the supported actions as int to be |
|
| 71 | + * compared with \OC\Group\Backend::CREATE_GROUP etc. |
|
| 72 | + */ |
|
| 73 | + public function implementsActions($actions) { |
|
| 74 | + return (bool)($this->getSupportedActions() & $actions); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * is user in group? |
|
| 79 | - * @param string $uid uid of the user |
|
| 80 | - * @param string $gid gid of the group |
|
| 81 | - * @return bool |
|
| 82 | - * |
|
| 83 | - * Checks whether the user is member of a group or not. |
|
| 84 | - */ |
|
| 85 | - public function inGroup($uid, $gid) { |
|
| 86 | - return in_array($gid, $this->getUserGroups($uid)); |
|
| 87 | - } |
|
| 77 | + /** |
|
| 78 | + * is user in group? |
|
| 79 | + * @param string $uid uid of the user |
|
| 80 | + * @param string $gid gid of the group |
|
| 81 | + * @return bool |
|
| 82 | + * |
|
| 83 | + * Checks whether the user is member of a group or not. |
|
| 84 | + */ |
|
| 85 | + public function inGroup($uid, $gid) { |
|
| 86 | + return in_array($gid, $this->getUserGroups($uid)); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Get all groups a user belongs to |
|
| 91 | - * @param string $uid Name of the user |
|
| 92 | - * @return array an array of group names |
|
| 93 | - * |
|
| 94 | - * This function fetches all groups a user belongs to. It does not check |
|
| 95 | - * if the user exists at all. |
|
| 96 | - */ |
|
| 97 | - public function getUserGroups($uid) { |
|
| 98 | - return []; |
|
| 99 | - } |
|
| 89 | + /** |
|
| 90 | + * Get all groups a user belongs to |
|
| 91 | + * @param string $uid Name of the user |
|
| 92 | + * @return array an array of group names |
|
| 93 | + * |
|
| 94 | + * This function fetches all groups a user belongs to. It does not check |
|
| 95 | + * if the user exists at all. |
|
| 96 | + */ |
|
| 97 | + public function getUserGroups($uid) { |
|
| 98 | + return []; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * get a list of all groups |
|
| 103 | - * @param string $search |
|
| 104 | - * @param int $limit |
|
| 105 | - * @param int $offset |
|
| 106 | - * @return array an array of group names |
|
| 107 | - * |
|
| 108 | - * Returns a list with all groups |
|
| 109 | - */ |
|
| 101 | + /** |
|
| 102 | + * get a list of all groups |
|
| 103 | + * @param string $search |
|
| 104 | + * @param int $limit |
|
| 105 | + * @param int $offset |
|
| 106 | + * @return array an array of group names |
|
| 107 | + * |
|
| 108 | + * Returns a list with all groups |
|
| 109 | + */ |
|
| 110 | 110 | |
| 111 | - public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 112 | - return []; |
|
| 113 | - } |
|
| 111 | + public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 112 | + return []; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * check if a group exists |
|
| 117 | - * @param string $gid |
|
| 118 | - * @return bool |
|
| 119 | - */ |
|
| 120 | - public function groupExists($gid) { |
|
| 121 | - return in_array($gid, $this->getGroups($gid, 1)); |
|
| 122 | - } |
|
| 115 | + /** |
|
| 116 | + * check if a group exists |
|
| 117 | + * @param string $gid |
|
| 118 | + * @return bool |
|
| 119 | + */ |
|
| 120 | + public function groupExists($gid) { |
|
| 121 | + return in_array($gid, $this->getGroups($gid, 1)); |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * get a list of all users in a group |
|
| 126 | - * @param string $gid |
|
| 127 | - * @param string $search |
|
| 128 | - * @param int $limit |
|
| 129 | - * @param int $offset |
|
| 130 | - * @return array an array of user ids |
|
| 131 | - */ |
|
| 132 | - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 133 | - return []; |
|
| 134 | - } |
|
| 124 | + /** |
|
| 125 | + * get a list of all users in a group |
|
| 126 | + * @param string $gid |
|
| 127 | + * @param string $search |
|
| 128 | + * @param int $limit |
|
| 129 | + * @param int $offset |
|
| 130 | + * @return array an array of user ids |
|
| 131 | + */ |
|
| 132 | + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 133 | + return []; |
|
| 134 | + } |
|
| 135 | 135 | } |
@@ -133,10 +133,10 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | // Check that all parameters from $event exist in $previousEvent |
| 135 | 135 | foreach ($params1 as $key => $parameter) { |
| 136 | - if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 136 | + if (preg_match('/^'.$mergeParameter.'(\d+)?$/', $key)) { |
|
| 137 | 137 | if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) { |
| 138 | 138 | $combined++; |
| 139 | - $params[$mergeParameter . $combined] = $parameter; |
|
| 139 | + $params[$mergeParameter.$combined] = $parameter; |
|
| 140 | 140 | } |
| 141 | 141 | continue; |
| 142 | 142 | } |
@@ -151,10 +151,10 @@ discard block |
||
| 151 | 151 | |
| 152 | 152 | // Check that all parameters from $previousEvent exist in $event |
| 153 | 153 | foreach ($params2 as $key => $parameter) { |
| 154 | - if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 154 | + if (preg_match('/^'.$mergeParameter.'(\d+)?$/', $key)) { |
|
| 155 | 155 | if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) { |
| 156 | 156 | $combined++; |
| 157 | - $params[$mergeParameter . $combined] = $parameter; |
|
| 157 | + $params[$mergeParameter.$combined] = $parameter; |
|
| 158 | 158 | } |
| 159 | 159 | continue; |
| 160 | 160 | } |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | */ |
| 179 | 179 | protected function checkParameterAlreadyExits($parameters, $mergeParameter, $parameter) { |
| 180 | 180 | foreach ($parameters as $key => $param) { |
| 181 | - if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 181 | + if (preg_match('/^'.$mergeParameter.'(\d+)?$/', $key)) { |
|
| 182 | 182 | if ($param === $parameter) { |
| 183 | 183 | return true; |
| 184 | 184 | } |
@@ -196,30 +196,30 @@ discard block |
||
| 196 | 196 | protected function getExtendedSubject($subject, $parameter, $counter) { |
| 197 | 197 | switch ($counter) { |
| 198 | 198 | case 1: |
| 199 | - $replacement = '{' . $parameter . '1}'; |
|
| 199 | + $replacement = '{'.$parameter.'1}'; |
|
| 200 | 200 | break; |
| 201 | 201 | case 2: |
| 202 | 202 | $replacement = $this->l10n->t( |
| 203 | 203 | '%1$s and %2$s', |
| 204 | - ['{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 204 | + ['{'.$parameter.'2}', '{'.$parameter.'1}'] |
|
| 205 | 205 | ); |
| 206 | 206 | break; |
| 207 | 207 | case 3: |
| 208 | 208 | $replacement = $this->l10n->t( |
| 209 | 209 | '%1$s, %2$s and %3$s', |
| 210 | - ['{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 210 | + ['{'.$parameter.'3}', '{'.$parameter.'2}', '{'.$parameter.'1}'] |
|
| 211 | 211 | ); |
| 212 | 212 | break; |
| 213 | 213 | case 4: |
| 214 | 214 | $replacement = $this->l10n->t( |
| 215 | 215 | '%1$s, %2$s, %3$s and %4$s', |
| 216 | - ['{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 216 | + ['{'.$parameter.'4}', '{'.$parameter.'3}', '{'.$parameter.'2}', '{'.$parameter.'1}'] |
|
| 217 | 217 | ); |
| 218 | 218 | break; |
| 219 | 219 | case 5: |
| 220 | 220 | $replacement = $this->l10n->t( |
| 221 | 221 | '%1$s, %2$s, %3$s, %4$s and %5$s', |
| 222 | - ['{' . $parameter . '5}', '{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 222 | + ['{'.$parameter.'5}', '{'.$parameter.'4}', '{'.$parameter.'3}', '{'.$parameter.'2}', '{'.$parameter.'1}'] |
|
| 223 | 223 | ); |
| 224 | 224 | break; |
| 225 | 225 | default: |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | return str_replace( |
| 230 | - '{' . $parameter . '}', |
|
| 230 | + '{'.$parameter.'}', |
|
| 231 | 231 | $replacement, |
| 232 | 232 | $subject |
| 233 | 233 | ); |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | protected function generateParsedSubject($subject, $parameters) { |
| 242 | 242 | $placeholders = $replacements = []; |
| 243 | 243 | foreach ($parameters as $placeholder => $parameter) { |
| 244 | - $placeholders[] = '{' . $placeholder . '}'; |
|
| 244 | + $placeholders[] = '{'.$placeholder.'}'; |
|
| 245 | 245 | if ($parameter['type'] === 'file') { |
| 246 | 246 | $replacements[] = trim($parameter['path'], '/'); |
| 247 | 247 | } elseif (isset($parameter['name'])) { |
@@ -31,231 +31,231 @@ |
||
| 31 | 31 | |
| 32 | 32 | class EventMerger implements IEventMerger { |
| 33 | 33 | |
| 34 | - /** @var IL10N */ |
|
| 35 | - protected $l10n; |
|
| 34 | + /** @var IL10N */ |
|
| 35 | + protected $l10n; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @param IL10N $l10n |
|
| 39 | - */ |
|
| 40 | - public function __construct(IL10N $l10n) { |
|
| 41 | - $this->l10n = $l10n; |
|
| 42 | - } |
|
| 37 | + /** |
|
| 38 | + * @param IL10N $l10n |
|
| 39 | + */ |
|
| 40 | + public function __construct(IL10N $l10n) { |
|
| 41 | + $this->l10n = $l10n; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Combines two events when possible to have grouping: |
|
| 46 | - * |
|
| 47 | - * Example1: Two events with subject '{user} created {file}' and |
|
| 48 | - * $mergeParameter file with different file and same user will be merged |
|
| 49 | - * to '{user} created {file1} and {file2}' and the childEvent on the return |
|
| 50 | - * will be set, if the events have been merged. |
|
| 51 | - * |
|
| 52 | - * Example2: Two events with subject '{user} created {file}' and |
|
| 53 | - * $mergeParameter file with same file and same user will be merged to |
|
| 54 | - * '{user} created {file1}' and the childEvent on the return will be set, if |
|
| 55 | - * the events have been merged. |
|
| 56 | - * |
|
| 57 | - * The following requirements have to be met, in order to be merged: |
|
| 58 | - * - Both events need to have the same `getApp()` |
|
| 59 | - * - Both events must not have a message `getMessage()` |
|
| 60 | - * - Both events need to have the same subject `getSubject()` |
|
| 61 | - * - Both events need to have the same object type `getObjectType()` |
|
| 62 | - * - The time difference between both events must not be bigger then 3 hours |
|
| 63 | - * - Only up to 5 events can be merged. |
|
| 64 | - * - All parameters apart from such starting with $mergeParameter must be |
|
| 65 | - * the same for both events. |
|
| 66 | - * |
|
| 67 | - * @param string $mergeParameter |
|
| 68 | - * @param IEvent $event |
|
| 69 | - * @param IEvent|null $previousEvent |
|
| 70 | - * @return IEvent |
|
| 71 | - */ |
|
| 72 | - public function mergeEvents($mergeParameter, IEvent $event, IEvent $previousEvent = null) { |
|
| 73 | - // No second event => can not combine |
|
| 74 | - if (!$previousEvent instanceof IEvent) { |
|
| 75 | - return $event; |
|
| 76 | - } |
|
| 44 | + /** |
|
| 45 | + * Combines two events when possible to have grouping: |
|
| 46 | + * |
|
| 47 | + * Example1: Two events with subject '{user} created {file}' and |
|
| 48 | + * $mergeParameter file with different file and same user will be merged |
|
| 49 | + * to '{user} created {file1} and {file2}' and the childEvent on the return |
|
| 50 | + * will be set, if the events have been merged. |
|
| 51 | + * |
|
| 52 | + * Example2: Two events with subject '{user} created {file}' and |
|
| 53 | + * $mergeParameter file with same file and same user will be merged to |
|
| 54 | + * '{user} created {file1}' and the childEvent on the return will be set, if |
|
| 55 | + * the events have been merged. |
|
| 56 | + * |
|
| 57 | + * The following requirements have to be met, in order to be merged: |
|
| 58 | + * - Both events need to have the same `getApp()` |
|
| 59 | + * - Both events must not have a message `getMessage()` |
|
| 60 | + * - Both events need to have the same subject `getSubject()` |
|
| 61 | + * - Both events need to have the same object type `getObjectType()` |
|
| 62 | + * - The time difference between both events must not be bigger then 3 hours |
|
| 63 | + * - Only up to 5 events can be merged. |
|
| 64 | + * - All parameters apart from such starting with $mergeParameter must be |
|
| 65 | + * the same for both events. |
|
| 66 | + * |
|
| 67 | + * @param string $mergeParameter |
|
| 68 | + * @param IEvent $event |
|
| 69 | + * @param IEvent|null $previousEvent |
|
| 70 | + * @return IEvent |
|
| 71 | + */ |
|
| 72 | + public function mergeEvents($mergeParameter, IEvent $event, IEvent $previousEvent = null) { |
|
| 73 | + // No second event => can not combine |
|
| 74 | + if (!$previousEvent instanceof IEvent) { |
|
| 75 | + return $event; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - // Different app => can not combine |
|
| 79 | - if ($event->getApp() !== $previousEvent->getApp()) { |
|
| 80 | - return $event; |
|
| 81 | - } |
|
| 78 | + // Different app => can not combine |
|
| 79 | + if ($event->getApp() !== $previousEvent->getApp()) { |
|
| 80 | + return $event; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - // Message is set => can not combine |
|
| 84 | - if ($event->getMessage() !== '' || $previousEvent->getMessage() !== '') { |
|
| 85 | - return $event; |
|
| 86 | - } |
|
| 83 | + // Message is set => can not combine |
|
| 84 | + if ($event->getMessage() !== '' || $previousEvent->getMessage() !== '') { |
|
| 85 | + return $event; |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - // Different subject => can not combine |
|
| 89 | - if ($event->getSubject() !== $previousEvent->getSubject()) { |
|
| 90 | - return $event; |
|
| 91 | - } |
|
| 88 | + // Different subject => can not combine |
|
| 89 | + if ($event->getSubject() !== $previousEvent->getSubject()) { |
|
| 90 | + return $event; |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | - // Different object type => can not combine |
|
| 94 | - if ($event->getObjectType() !== $previousEvent->getObjectType()) { |
|
| 95 | - return $event; |
|
| 96 | - } |
|
| 93 | + // Different object type => can not combine |
|
| 94 | + if ($event->getObjectType() !== $previousEvent->getObjectType()) { |
|
| 95 | + return $event; |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - // More than 3 hours difference => can not combine |
|
| 99 | - if (abs($event->getTimestamp() - $previousEvent->getTimestamp()) > 3 * 60 * 60) { |
|
| 100 | - return $event; |
|
| 101 | - } |
|
| 98 | + // More than 3 hours difference => can not combine |
|
| 99 | + if (abs($event->getTimestamp() - $previousEvent->getTimestamp()) > 3 * 60 * 60) { |
|
| 100 | + return $event; |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - // Other parameters are not the same => can not combine |
|
| 104 | - try { |
|
| 105 | - [$combined, $parameters] = $this->combineParameters($mergeParameter, $event, $previousEvent); |
|
| 106 | - } catch (\UnexpectedValueException $e) { |
|
| 107 | - return $event; |
|
| 108 | - } |
|
| 103 | + // Other parameters are not the same => can not combine |
|
| 104 | + try { |
|
| 105 | + [$combined, $parameters] = $this->combineParameters($mergeParameter, $event, $previousEvent); |
|
| 106 | + } catch (\UnexpectedValueException $e) { |
|
| 107 | + return $event; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - try { |
|
| 111 | - $newSubject = $this->getExtendedSubject($event->getRichSubject(), $mergeParameter, $combined); |
|
| 112 | - $parsedSubject = $this->generateParsedSubject($newSubject, $parameters); |
|
| 110 | + try { |
|
| 111 | + $newSubject = $this->getExtendedSubject($event->getRichSubject(), $mergeParameter, $combined); |
|
| 112 | + $parsedSubject = $this->generateParsedSubject($newSubject, $parameters); |
|
| 113 | 113 | |
| 114 | - $event->setRichSubject($newSubject, $parameters) |
|
| 115 | - ->setParsedSubject($parsedSubject) |
|
| 116 | - ->setChildEvent($previousEvent) |
|
| 117 | - ->setTimestamp(max($event->getTimestamp(), $previousEvent->getTimestamp())); |
|
| 118 | - } catch (\UnexpectedValueException $e) { |
|
| 119 | - return $event; |
|
| 120 | - } |
|
| 114 | + $event->setRichSubject($newSubject, $parameters) |
|
| 115 | + ->setParsedSubject($parsedSubject) |
|
| 116 | + ->setChildEvent($previousEvent) |
|
| 117 | + ->setTimestamp(max($event->getTimestamp(), $previousEvent->getTimestamp())); |
|
| 118 | + } catch (\UnexpectedValueException $e) { |
|
| 119 | + return $event; |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - return $event; |
|
| 123 | - } |
|
| 122 | + return $event; |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * @param string $mergeParameter |
|
| 127 | - * @param IEvent $event |
|
| 128 | - * @param IEvent $previousEvent |
|
| 129 | - * @return array |
|
| 130 | - * @throws \UnexpectedValueException |
|
| 131 | - */ |
|
| 132 | - protected function combineParameters($mergeParameter, IEvent $event, IEvent $previousEvent) { |
|
| 133 | - $params1 = $event->getRichSubjectParameters(); |
|
| 134 | - $params2 = $previousEvent->getRichSubjectParameters(); |
|
| 135 | - $params = []; |
|
| 125 | + /** |
|
| 126 | + * @param string $mergeParameter |
|
| 127 | + * @param IEvent $event |
|
| 128 | + * @param IEvent $previousEvent |
|
| 129 | + * @return array |
|
| 130 | + * @throws \UnexpectedValueException |
|
| 131 | + */ |
|
| 132 | + protected function combineParameters($mergeParameter, IEvent $event, IEvent $previousEvent) { |
|
| 133 | + $params1 = $event->getRichSubjectParameters(); |
|
| 134 | + $params2 = $previousEvent->getRichSubjectParameters(); |
|
| 135 | + $params = []; |
|
| 136 | 136 | |
| 137 | - $combined = 0; |
|
| 137 | + $combined = 0; |
|
| 138 | 138 | |
| 139 | - // Check that all parameters from $event exist in $previousEvent |
|
| 140 | - foreach ($params1 as $key => $parameter) { |
|
| 141 | - if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 142 | - if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) { |
|
| 143 | - $combined++; |
|
| 144 | - $params[$mergeParameter . $combined] = $parameter; |
|
| 145 | - } |
|
| 146 | - continue; |
|
| 147 | - } |
|
| 139 | + // Check that all parameters from $event exist in $previousEvent |
|
| 140 | + foreach ($params1 as $key => $parameter) { |
|
| 141 | + if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 142 | + if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) { |
|
| 143 | + $combined++; |
|
| 144 | + $params[$mergeParameter . $combined] = $parameter; |
|
| 145 | + } |
|
| 146 | + continue; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - if (!isset($params2[$key]) || $params2[$key] !== $parameter) { |
|
| 150 | - // Parameter missing on $previousEvent or different => can not combine |
|
| 151 | - throw new \UnexpectedValueException(); |
|
| 152 | - } |
|
| 149 | + if (!isset($params2[$key]) || $params2[$key] !== $parameter) { |
|
| 150 | + // Parameter missing on $previousEvent or different => can not combine |
|
| 151 | + throw new \UnexpectedValueException(); |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - $params[$key] = $parameter; |
|
| 155 | - } |
|
| 154 | + $params[$key] = $parameter; |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - // Check that all parameters from $previousEvent exist in $event |
|
| 158 | - foreach ($params2 as $key => $parameter) { |
|
| 159 | - if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 160 | - if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) { |
|
| 161 | - $combined++; |
|
| 162 | - $params[$mergeParameter . $combined] = $parameter; |
|
| 163 | - } |
|
| 164 | - continue; |
|
| 165 | - } |
|
| 157 | + // Check that all parameters from $previousEvent exist in $event |
|
| 158 | + foreach ($params2 as $key => $parameter) { |
|
| 159 | + if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 160 | + if (!$this->checkParameterAlreadyExits($params, $mergeParameter, $parameter)) { |
|
| 161 | + $combined++; |
|
| 162 | + $params[$mergeParameter . $combined] = $parameter; |
|
| 163 | + } |
|
| 164 | + continue; |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - if (!isset($params1[$key]) || $params1[$key] !== $parameter) { |
|
| 168 | - // Parameter missing on $event or different => can not combine |
|
| 169 | - throw new \UnexpectedValueException(); |
|
| 170 | - } |
|
| 167 | + if (!isset($params1[$key]) || $params1[$key] !== $parameter) { |
|
| 168 | + // Parameter missing on $event or different => can not combine |
|
| 169 | + throw new \UnexpectedValueException(); |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - $params[$key] = $parameter; |
|
| 173 | - } |
|
| 172 | + $params[$key] = $parameter; |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - return [$combined, $params]; |
|
| 176 | - } |
|
| 175 | + return [$combined, $params]; |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * @param array[] $parameters |
|
| 180 | - * @param string $mergeParameter |
|
| 181 | - * @param array $parameter |
|
| 182 | - * @return bool |
|
| 183 | - */ |
|
| 184 | - protected function checkParameterAlreadyExits($parameters, $mergeParameter, $parameter) { |
|
| 185 | - foreach ($parameters as $key => $param) { |
|
| 186 | - if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 187 | - if ($param === $parameter) { |
|
| 188 | - return true; |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - return false; |
|
| 193 | - } |
|
| 178 | + /** |
|
| 179 | + * @param array[] $parameters |
|
| 180 | + * @param string $mergeParameter |
|
| 181 | + * @param array $parameter |
|
| 182 | + * @return bool |
|
| 183 | + */ |
|
| 184 | + protected function checkParameterAlreadyExits($parameters, $mergeParameter, $parameter) { |
|
| 185 | + foreach ($parameters as $key => $param) { |
|
| 186 | + if (preg_match('/^' . $mergeParameter . '(\d+)?$/', $key)) { |
|
| 187 | + if ($param === $parameter) { |
|
| 188 | + return true; |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + return false; |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - /** |
|
| 196 | - * @param string $subject |
|
| 197 | - * @param string $parameter |
|
| 198 | - * @param int $counter |
|
| 199 | - * @return mixed |
|
| 200 | - */ |
|
| 201 | - protected function getExtendedSubject($subject, $parameter, $counter) { |
|
| 202 | - switch ($counter) { |
|
| 203 | - case 1: |
|
| 204 | - $replacement = '{' . $parameter . '1}'; |
|
| 205 | - break; |
|
| 206 | - case 2: |
|
| 207 | - $replacement = $this->l10n->t( |
|
| 208 | - '%1$s and %2$s', |
|
| 209 | - ['{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 210 | - ); |
|
| 211 | - break; |
|
| 212 | - case 3: |
|
| 213 | - $replacement = $this->l10n->t( |
|
| 214 | - '%1$s, %2$s and %3$s', |
|
| 215 | - ['{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 216 | - ); |
|
| 217 | - break; |
|
| 218 | - case 4: |
|
| 219 | - $replacement = $this->l10n->t( |
|
| 220 | - '%1$s, %2$s, %3$s and %4$s', |
|
| 221 | - ['{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 222 | - ); |
|
| 223 | - break; |
|
| 224 | - case 5: |
|
| 225 | - $replacement = $this->l10n->t( |
|
| 226 | - '%1$s, %2$s, %3$s, %4$s and %5$s', |
|
| 227 | - ['{' . $parameter . '5}', '{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 228 | - ); |
|
| 229 | - break; |
|
| 230 | - default: |
|
| 231 | - throw new \UnexpectedValueException(); |
|
| 232 | - } |
|
| 195 | + /** |
|
| 196 | + * @param string $subject |
|
| 197 | + * @param string $parameter |
|
| 198 | + * @param int $counter |
|
| 199 | + * @return mixed |
|
| 200 | + */ |
|
| 201 | + protected function getExtendedSubject($subject, $parameter, $counter) { |
|
| 202 | + switch ($counter) { |
|
| 203 | + case 1: |
|
| 204 | + $replacement = '{' . $parameter . '1}'; |
|
| 205 | + break; |
|
| 206 | + case 2: |
|
| 207 | + $replacement = $this->l10n->t( |
|
| 208 | + '%1$s and %2$s', |
|
| 209 | + ['{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 210 | + ); |
|
| 211 | + break; |
|
| 212 | + case 3: |
|
| 213 | + $replacement = $this->l10n->t( |
|
| 214 | + '%1$s, %2$s and %3$s', |
|
| 215 | + ['{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 216 | + ); |
|
| 217 | + break; |
|
| 218 | + case 4: |
|
| 219 | + $replacement = $this->l10n->t( |
|
| 220 | + '%1$s, %2$s, %3$s and %4$s', |
|
| 221 | + ['{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 222 | + ); |
|
| 223 | + break; |
|
| 224 | + case 5: |
|
| 225 | + $replacement = $this->l10n->t( |
|
| 226 | + '%1$s, %2$s, %3$s, %4$s and %5$s', |
|
| 227 | + ['{' . $parameter . '5}', '{' . $parameter . '4}', '{' . $parameter . '3}', '{' . $parameter . '2}', '{' . $parameter . '1}'] |
|
| 228 | + ); |
|
| 229 | + break; |
|
| 230 | + default: |
|
| 231 | + throw new \UnexpectedValueException(); |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - return str_replace( |
|
| 235 | - '{' . $parameter . '}', |
|
| 236 | - $replacement, |
|
| 237 | - $subject |
|
| 238 | - ); |
|
| 239 | - } |
|
| 234 | + return str_replace( |
|
| 235 | + '{' . $parameter . '}', |
|
| 236 | + $replacement, |
|
| 237 | + $subject |
|
| 238 | + ); |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - /** |
|
| 242 | - * @param string $subject |
|
| 243 | - * @param array[] $parameters |
|
| 244 | - * @return string |
|
| 245 | - */ |
|
| 246 | - protected function generateParsedSubject($subject, $parameters) { |
|
| 247 | - $placeholders = $replacements = []; |
|
| 248 | - foreach ($parameters as $placeholder => $parameter) { |
|
| 249 | - $placeholders[] = '{' . $placeholder . '}'; |
|
| 250 | - if ($parameter['type'] === 'file') { |
|
| 251 | - $replacements[] = trim($parameter['path'], '/'); |
|
| 252 | - } elseif (isset($parameter['name'])) { |
|
| 253 | - $replacements[] = $parameter['name']; |
|
| 254 | - } else { |
|
| 255 | - $replacements[] = $parameter['id']; |
|
| 256 | - } |
|
| 257 | - } |
|
| 241 | + /** |
|
| 242 | + * @param string $subject |
|
| 243 | + * @param array[] $parameters |
|
| 244 | + * @return string |
|
| 245 | + */ |
|
| 246 | + protected function generateParsedSubject($subject, $parameters) { |
|
| 247 | + $placeholders = $replacements = []; |
|
| 248 | + foreach ($parameters as $placeholder => $parameter) { |
|
| 249 | + $placeholders[] = '{' . $placeholder . '}'; |
|
| 250 | + if ($parameter['type'] === 'file') { |
|
| 251 | + $replacements[] = trim($parameter['path'], '/'); |
|
| 252 | + } elseif (isset($parameter['name'])) { |
|
| 253 | + $replacements[] = $parameter['name']; |
|
| 254 | + } else { |
|
| 255 | + $replacements[] = $parameter['id']; |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | 258 | |
| 259 | - return str_replace($placeholders, $replacements, $subject); |
|
| 260 | - } |
|
| 259 | + return str_replace($placeholders, $replacements, $subject); |
|
| 260 | + } |
|
| 261 | 261 | } |
@@ -89,7 +89,7 @@ |
||
| 89 | 89 | * @return IEntry[] |
| 90 | 90 | */ |
| 91 | 91 | private function sortEntries(array $entries) { |
| 92 | - usort($entries, function (IEntry $entryA, IEntry $entryB) { |
|
| 92 | + usort($entries, function(IEntry $entryA, IEntry $entryB) { |
|
| 93 | 93 | return strcasecmp($entryA->getFullName(), $entryB->getFullName()); |
| 94 | 94 | }); |
| 95 | 95 | return $entries; |
@@ -34,90 +34,90 @@ |
||
| 34 | 34 | |
| 35 | 35 | class Manager { |
| 36 | 36 | |
| 37 | - /** @var ContactsStore */ |
|
| 38 | - private $store; |
|
| 37 | + /** @var ContactsStore */ |
|
| 38 | + private $store; |
|
| 39 | 39 | |
| 40 | - /** @var ActionProviderStore */ |
|
| 41 | - private $actionProviderStore; |
|
| 40 | + /** @var ActionProviderStore */ |
|
| 41 | + private $actionProviderStore; |
|
| 42 | 42 | |
| 43 | - /** @var IAppManager */ |
|
| 44 | - private $appManager; |
|
| 43 | + /** @var IAppManager */ |
|
| 44 | + private $appManager; |
|
| 45 | 45 | |
| 46 | - /** @var IConfig */ |
|
| 47 | - private $config; |
|
| 46 | + /** @var IConfig */ |
|
| 47 | + private $config; |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @param ContactsStore $store |
|
| 51 | - * @param ActionProviderStore $actionProviderStore |
|
| 52 | - * @param IAppManager $appManager |
|
| 53 | - */ |
|
| 54 | - public function __construct(ContactsStore $store, ActionProviderStore $actionProviderStore, IAppManager $appManager, IConfig $config) { |
|
| 55 | - $this->store = $store; |
|
| 56 | - $this->actionProviderStore = $actionProviderStore; |
|
| 57 | - $this->appManager = $appManager; |
|
| 58 | - $this->config = $config; |
|
| 59 | - } |
|
| 49 | + /** |
|
| 50 | + * @param ContactsStore $store |
|
| 51 | + * @param ActionProviderStore $actionProviderStore |
|
| 52 | + * @param IAppManager $appManager |
|
| 53 | + */ |
|
| 54 | + public function __construct(ContactsStore $store, ActionProviderStore $actionProviderStore, IAppManager $appManager, IConfig $config) { |
|
| 55 | + $this->store = $store; |
|
| 56 | + $this->actionProviderStore = $actionProviderStore; |
|
| 57 | + $this->appManager = $appManager; |
|
| 58 | + $this->config = $config; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * @param IUser $user |
|
| 63 | - * @param string $filter |
|
| 64 | - * @return array |
|
| 65 | - */ |
|
| 66 | - public function getEntries(IUser $user, $filter) { |
|
| 67 | - $maxAutocompleteResults = max(0, $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT)); |
|
| 68 | - $minSearchStringLength = $this->config->getSystemValueInt('sharing.minSearchStringLength', 0); |
|
| 69 | - $topEntries = []; |
|
| 70 | - if (strlen($filter) >= $minSearchStringLength) { |
|
| 71 | - $entries = $this->store->getContacts($user, $filter, $maxAutocompleteResults); |
|
| 61 | + /** |
|
| 62 | + * @param IUser $user |
|
| 63 | + * @param string $filter |
|
| 64 | + * @return array |
|
| 65 | + */ |
|
| 66 | + public function getEntries(IUser $user, $filter) { |
|
| 67 | + $maxAutocompleteResults = max(0, $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT)); |
|
| 68 | + $minSearchStringLength = $this->config->getSystemValueInt('sharing.minSearchStringLength', 0); |
|
| 69 | + $topEntries = []; |
|
| 70 | + if (strlen($filter) >= $minSearchStringLength) { |
|
| 71 | + $entries = $this->store->getContacts($user, $filter, $maxAutocompleteResults); |
|
| 72 | 72 | |
| 73 | - $sortedEntries = $this->sortEntries($entries); |
|
| 74 | - $topEntries = array_slice($sortedEntries, 0, $maxAutocompleteResults); |
|
| 75 | - $this->processEntries($topEntries, $user); |
|
| 76 | - } |
|
| 73 | + $sortedEntries = $this->sortEntries($entries); |
|
| 74 | + $topEntries = array_slice($sortedEntries, 0, $maxAutocompleteResults); |
|
| 75 | + $this->processEntries($topEntries, $user); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - $contactsEnabled = $this->appManager->isEnabledForUser('contacts', $user); |
|
| 79 | - return [ |
|
| 80 | - 'contacts' => $topEntries, |
|
| 81 | - 'contactsAppEnabled' => $contactsEnabled, |
|
| 82 | - ]; |
|
| 83 | - } |
|
| 78 | + $contactsEnabled = $this->appManager->isEnabledForUser('contacts', $user); |
|
| 79 | + return [ |
|
| 80 | + 'contacts' => $topEntries, |
|
| 81 | + 'contactsAppEnabled' => $contactsEnabled, |
|
| 82 | + ]; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * @param IUser $user |
|
| 87 | - * @param integer $shareType |
|
| 88 | - * @param string $shareWith |
|
| 89 | - * @return IEntry |
|
| 90 | - */ |
|
| 91 | - public function findOne(IUser $user, $shareType, $shareWith) { |
|
| 92 | - $entry = $this->store->findOne($user, $shareType, $shareWith); |
|
| 93 | - if ($entry) { |
|
| 94 | - $this->processEntries([$entry], $user); |
|
| 95 | - } |
|
| 85 | + /** |
|
| 86 | + * @param IUser $user |
|
| 87 | + * @param integer $shareType |
|
| 88 | + * @param string $shareWith |
|
| 89 | + * @return IEntry |
|
| 90 | + */ |
|
| 91 | + public function findOne(IUser $user, $shareType, $shareWith) { |
|
| 92 | + $entry = $this->store->findOne($user, $shareType, $shareWith); |
|
| 93 | + if ($entry) { |
|
| 94 | + $this->processEntries([$entry], $user); |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - return $entry; |
|
| 98 | - } |
|
| 97 | + return $entry; |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * @param IEntry[] $entries |
|
| 102 | - * @return IEntry[] |
|
| 103 | - */ |
|
| 104 | - private function sortEntries(array $entries) { |
|
| 105 | - usort($entries, function (IEntry $entryA, IEntry $entryB) { |
|
| 106 | - return strcasecmp($entryA->getFullName(), $entryB->getFullName()); |
|
| 107 | - }); |
|
| 108 | - return $entries; |
|
| 109 | - } |
|
| 100 | + /** |
|
| 101 | + * @param IEntry[] $entries |
|
| 102 | + * @return IEntry[] |
|
| 103 | + */ |
|
| 104 | + private function sortEntries(array $entries) { |
|
| 105 | + usort($entries, function (IEntry $entryA, IEntry $entryB) { |
|
| 106 | + return strcasecmp($entryA->getFullName(), $entryB->getFullName()); |
|
| 107 | + }); |
|
| 108 | + return $entries; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * @param IEntry[] $entries |
|
| 113 | - * @param IUser $user |
|
| 114 | - */ |
|
| 115 | - private function processEntries(array $entries, IUser $user) { |
|
| 116 | - $providers = $this->actionProviderStore->getProviders($user); |
|
| 117 | - foreach ($entries as $entry) { |
|
| 118 | - foreach ($providers as $provider) { |
|
| 119 | - $provider->process($entry); |
|
| 120 | - } |
|
| 121 | - } |
|
| 122 | - } |
|
| 111 | + /** |
|
| 112 | + * @param IEntry[] $entries |
|
| 113 | + * @param IUser $user |
|
| 114 | + */ |
|
| 115 | + private function processEntries(array $entries, IUser $user) { |
|
| 116 | + $providers = $this->actionProviderStore->getProviders($user); |
|
| 117 | + foreach ($entries as $entry) { |
|
| 118 | + foreach ($providers as $provider) { |
|
| 119 | + $provider->process($entry); |
|
| 120 | + } |
|
| 121 | + } |
|
| 122 | + } |
|
| 123 | 123 | } |
@@ -24,12 +24,12 @@ |
||
| 24 | 24 | namespace OC\BackgroundJob\Legacy; |
| 25 | 25 | |
| 26 | 26 | class QueuedJob extends \OC\BackgroundJob\QueuedJob { |
| 27 | - public function run($argument) { |
|
| 28 | - $class = $argument['klass']; |
|
| 29 | - $method = $argument['method']; |
|
| 30 | - $parameters = $argument['parameters']; |
|
| 31 | - if (is_callable([$class, $method])) { |
|
| 32 | - call_user_func([$class, $method], $parameters); |
|
| 33 | - } |
|
| 34 | - } |
|
| 27 | + public function run($argument) { |
|
| 28 | + $class = $argument['klass']; |
|
| 29 | + $method = $argument['method']; |
|
| 30 | + $parameters = $argument['parameters']; |
|
| 31 | + if (is_callable([$class, $method])) { |
|
| 32 | + call_user_func([$class, $method], $parameters); |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -26,37 +26,37 @@ |
||
| 26 | 26 | namespace OC\Template; |
| 27 | 27 | |
| 28 | 28 | class TemplateFileLocator { |
| 29 | - protected $dirs; |
|
| 30 | - private $path; |
|
| 29 | + protected $dirs; |
|
| 30 | + private $path; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @param string[] $dirs |
|
| 34 | - */ |
|
| 35 | - public function __construct($dirs) { |
|
| 36 | - $this->dirs = $dirs; |
|
| 37 | - } |
|
| 32 | + /** |
|
| 33 | + * @param string[] $dirs |
|
| 34 | + */ |
|
| 35 | + public function __construct($dirs) { |
|
| 36 | + $this->dirs = $dirs; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param string $template |
|
| 41 | - * @return string |
|
| 42 | - * @throws \Exception |
|
| 43 | - */ |
|
| 44 | - public function find($template) { |
|
| 45 | - if ($template === '') { |
|
| 46 | - throw new \InvalidArgumentException('Empty template name'); |
|
| 47 | - } |
|
| 39 | + /** |
|
| 40 | + * @param string $template |
|
| 41 | + * @return string |
|
| 42 | + * @throws \Exception |
|
| 43 | + */ |
|
| 44 | + public function find($template) { |
|
| 45 | + if ($template === '') { |
|
| 46 | + throw new \InvalidArgumentException('Empty template name'); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - foreach ($this->dirs as $dir) { |
|
| 50 | - $file = $dir.$template.'.php'; |
|
| 51 | - if (is_file($file)) { |
|
| 52 | - $this->path = $dir; |
|
| 53 | - return $file; |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - throw new \Exception('template file not found: template:'.$template); |
|
| 57 | - } |
|
| 49 | + foreach ($this->dirs as $dir) { |
|
| 50 | + $file = $dir.$template.'.php'; |
|
| 51 | + if (is_file($file)) { |
|
| 52 | + $this->path = $dir; |
|
| 53 | + return $file; |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + throw new \Exception('template file not found: template:'.$template); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - public function getPath() { |
|
| 60 | - return $this->path; |
|
| 61 | - } |
|
| 59 | + public function getPath() { |
|
| 60 | + return $this->path; |
|
| 61 | + } |
|
| 62 | 62 | } |