@@ -155,7 +155,7 @@ |
||
| 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 |
@@ -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 = 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 | } |
@@ -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; |
@@ -139,7 +139,7 @@ discard block |
||
| 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 |
||
| 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 |
@@ -58,7 +58,7 @@ |
||
| 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 */ |
@@ -53,179 +53,179 @@ |
||
| 53 | 53 | use Symfony\Component\EventDispatcher\GenericEvent; |
| 54 | 54 | |
| 55 | 55 | class Repair implements IOutput{ |
| 56 | - /* @var IRepairStep[] */ |
|
| 57 | - private $repairSteps; |
|
| 58 | - /** @var EventDispatcher */ |
|
| 59 | - private $dispatcher; |
|
| 60 | - /** @var string */ |
|
| 61 | - private $currentStep; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Creates a new repair step runner |
|
| 65 | - * |
|
| 66 | - * @param IRepairStep[] $repairSteps array of RepairStep instances |
|
| 67 | - * @param EventDispatcher $dispatcher |
|
| 68 | - */ |
|
| 69 | - public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) { |
|
| 70 | - $this->repairSteps = $repairSteps; |
|
| 71 | - $this->dispatcher = $dispatcher; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Run a series of repair steps for common problems |
|
| 76 | - */ |
|
| 77 | - public function run() { |
|
| 78 | - if (count($this->repairSteps) === 0) { |
|
| 79 | - $this->emit('\OC\Repair', 'info', array('No repair steps available')); |
|
| 80 | - return; |
|
| 81 | - } |
|
| 82 | - // run each repair step |
|
| 83 | - foreach ($this->repairSteps as $step) { |
|
| 84 | - $this->currentStep = $step->getName(); |
|
| 85 | - $this->emit('\OC\Repair', 'step', [$this->currentStep]); |
|
| 86 | - $step->run($this); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Add repair step |
|
| 92 | - * |
|
| 93 | - * @param IRepairStep|string $repairStep repair step |
|
| 94 | - * @throws \Exception |
|
| 95 | - */ |
|
| 96 | - public function addStep($repairStep) { |
|
| 97 | - if (is_string($repairStep)) { |
|
| 98 | - try { |
|
| 99 | - $s = \OC::$server->query($repairStep); |
|
| 100 | - } catch (QueryException $e) { |
|
| 101 | - if (class_exists($repairStep)) { |
|
| 102 | - $s = new $repairStep(); |
|
| 103 | - } else { |
|
| 104 | - throw new \Exception("Repair step '$repairStep' is unknown"); |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - if ($s instanceof IRepairStep) { |
|
| 109 | - $this->repairSteps[] = $s; |
|
| 110 | - } else { |
|
| 111 | - throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep"); |
|
| 112 | - } |
|
| 113 | - } else { |
|
| 114 | - $this->repairSteps[] = $repairStep; |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Returns the default repair steps to be run on the |
|
| 120 | - * command line or after an upgrade. |
|
| 121 | - * |
|
| 122 | - * @return IRepairStep[] |
|
| 123 | - */ |
|
| 124 | - public static function getRepairSteps() { |
|
| 125 | - return [ |
|
| 126 | - new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false), |
|
| 127 | - new RepairMimeTypes(\OC::$server->getConfig()), |
|
| 128 | - new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()), |
|
| 129 | - new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()), |
|
| 130 | - new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()), |
|
| 131 | - new MoveUpdaterStepFile(\OC::$server->getConfig()), |
|
| 132 | - new MoveAvatars( |
|
| 133 | - \OC::$server->getJobList(), |
|
| 134 | - \OC::$server->getConfig() |
|
| 135 | - ), |
|
| 136 | - new CleanPreviews( |
|
| 137 | - \OC::$server->getJobList(), |
|
| 138 | - \OC::$server->getUserManager(), |
|
| 139 | - \OC::$server->getConfig() |
|
| 140 | - ), |
|
| 141 | - new FixMountStorages(\OC::$server->getDatabaseConnection()), |
|
| 142 | - new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()), |
|
| 143 | - new InstallCoreBundle( |
|
| 144 | - \OC::$server->query(BundleFetcher::class), |
|
| 145 | - \OC::$server->getConfig(), |
|
| 146 | - \OC::$server->query(Installer::class) |
|
| 147 | - ), |
|
| 148 | - new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()) |
|
| 149 | - ]; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Returns expensive repair steps to be run on the |
|
| 154 | - * command line with a special option. |
|
| 155 | - * |
|
| 156 | - * @return IRepairStep[] |
|
| 157 | - */ |
|
| 158 | - public static function getExpensiveRepairSteps() { |
|
| 159 | - return [ |
|
| 160 | - new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()) |
|
| 161 | - ]; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Returns the repair steps to be run before an |
|
| 166 | - * upgrade. |
|
| 167 | - * |
|
| 168 | - * @return IRepairStep[] |
|
| 169 | - */ |
|
| 170 | - public static function getBeforeUpgradeRepairSteps() { |
|
| 171 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 172 | - $config = \OC::$server->getConfig(); |
|
| 173 | - $steps = [ |
|
| 174 | - new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true), |
|
| 175 | - new SqliteAutoincrement($connection), |
|
| 176 | - new SaveAccountsTableData($connection, $config), |
|
| 177 | - ]; |
|
| 178 | - |
|
| 179 | - return $steps; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @param string $scope |
|
| 184 | - * @param string $method |
|
| 185 | - * @param array $arguments |
|
| 186 | - */ |
|
| 187 | - public function emit($scope, $method, array $arguments = []) { |
|
| 188 | - if (!is_null($this->dispatcher)) { |
|
| 189 | - $this->dispatcher->dispatch("$scope::$method", |
|
| 190 | - new GenericEvent("$scope::$method", $arguments)); |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - public function info($string) { |
|
| 195 | - // for now just emit as we did in the past |
|
| 196 | - $this->emit('\OC\Repair', 'info', array($string)); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * @param string $message |
|
| 201 | - */ |
|
| 202 | - public function warning($message) { |
|
| 203 | - // for now just emit as we did in the past |
|
| 204 | - $this->emit('\OC\Repair', 'warning', [$message]); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * @param int $max |
|
| 209 | - */ |
|
| 210 | - public function startProgress($max = 0) { |
|
| 211 | - // for now just emit as we did in the past |
|
| 212 | - $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * @param int $step |
|
| 217 | - * @param string $description |
|
| 218 | - */ |
|
| 219 | - public function advance($step = 1, $description = '') { |
|
| 220 | - // for now just emit as we did in the past |
|
| 221 | - $this->emit('\OC\Repair', 'advance', [$step, $description]); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * @param int $max |
|
| 226 | - */ |
|
| 227 | - public function finishProgress() { |
|
| 228 | - // for now just emit as we did in the past |
|
| 229 | - $this->emit('\OC\Repair', 'finishProgress', []); |
|
| 230 | - } |
|
| 56 | + /* @var IRepairStep[] */ |
|
| 57 | + private $repairSteps; |
|
| 58 | + /** @var EventDispatcher */ |
|
| 59 | + private $dispatcher; |
|
| 60 | + /** @var string */ |
|
| 61 | + private $currentStep; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Creates a new repair step runner |
|
| 65 | + * |
|
| 66 | + * @param IRepairStep[] $repairSteps array of RepairStep instances |
|
| 67 | + * @param EventDispatcher $dispatcher |
|
| 68 | + */ |
|
| 69 | + public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) { |
|
| 70 | + $this->repairSteps = $repairSteps; |
|
| 71 | + $this->dispatcher = $dispatcher; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Run a series of repair steps for common problems |
|
| 76 | + */ |
|
| 77 | + public function run() { |
|
| 78 | + if (count($this->repairSteps) === 0) { |
|
| 79 | + $this->emit('\OC\Repair', 'info', array('No repair steps available')); |
|
| 80 | + return; |
|
| 81 | + } |
|
| 82 | + // run each repair step |
|
| 83 | + foreach ($this->repairSteps as $step) { |
|
| 84 | + $this->currentStep = $step->getName(); |
|
| 85 | + $this->emit('\OC\Repair', 'step', [$this->currentStep]); |
|
| 86 | + $step->run($this); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Add repair step |
|
| 92 | + * |
|
| 93 | + * @param IRepairStep|string $repairStep repair step |
|
| 94 | + * @throws \Exception |
|
| 95 | + */ |
|
| 96 | + public function addStep($repairStep) { |
|
| 97 | + if (is_string($repairStep)) { |
|
| 98 | + try { |
|
| 99 | + $s = \OC::$server->query($repairStep); |
|
| 100 | + } catch (QueryException $e) { |
|
| 101 | + if (class_exists($repairStep)) { |
|
| 102 | + $s = new $repairStep(); |
|
| 103 | + } else { |
|
| 104 | + throw new \Exception("Repair step '$repairStep' is unknown"); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + if ($s instanceof IRepairStep) { |
|
| 109 | + $this->repairSteps[] = $s; |
|
| 110 | + } else { |
|
| 111 | + throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep"); |
|
| 112 | + } |
|
| 113 | + } else { |
|
| 114 | + $this->repairSteps[] = $repairStep; |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Returns the default repair steps to be run on the |
|
| 120 | + * command line or after an upgrade. |
|
| 121 | + * |
|
| 122 | + * @return IRepairStep[] |
|
| 123 | + */ |
|
| 124 | + public static function getRepairSteps() { |
|
| 125 | + return [ |
|
| 126 | + new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false), |
|
| 127 | + new RepairMimeTypes(\OC::$server->getConfig()), |
|
| 128 | + new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()), |
|
| 129 | + new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()), |
|
| 130 | + new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()), |
|
| 131 | + new MoveUpdaterStepFile(\OC::$server->getConfig()), |
|
| 132 | + new MoveAvatars( |
|
| 133 | + \OC::$server->getJobList(), |
|
| 134 | + \OC::$server->getConfig() |
|
| 135 | + ), |
|
| 136 | + new CleanPreviews( |
|
| 137 | + \OC::$server->getJobList(), |
|
| 138 | + \OC::$server->getUserManager(), |
|
| 139 | + \OC::$server->getConfig() |
|
| 140 | + ), |
|
| 141 | + new FixMountStorages(\OC::$server->getDatabaseConnection()), |
|
| 142 | + new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()), |
|
| 143 | + new InstallCoreBundle( |
|
| 144 | + \OC::$server->query(BundleFetcher::class), |
|
| 145 | + \OC::$server->getConfig(), |
|
| 146 | + \OC::$server->query(Installer::class) |
|
| 147 | + ), |
|
| 148 | + new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()) |
|
| 149 | + ]; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Returns expensive repair steps to be run on the |
|
| 154 | + * command line with a special option. |
|
| 155 | + * |
|
| 156 | + * @return IRepairStep[] |
|
| 157 | + */ |
|
| 158 | + public static function getExpensiveRepairSteps() { |
|
| 159 | + return [ |
|
| 160 | + new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()) |
|
| 161 | + ]; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Returns the repair steps to be run before an |
|
| 166 | + * upgrade. |
|
| 167 | + * |
|
| 168 | + * @return IRepairStep[] |
|
| 169 | + */ |
|
| 170 | + public static function getBeforeUpgradeRepairSteps() { |
|
| 171 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 172 | + $config = \OC::$server->getConfig(); |
|
| 173 | + $steps = [ |
|
| 174 | + new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true), |
|
| 175 | + new SqliteAutoincrement($connection), |
|
| 176 | + new SaveAccountsTableData($connection, $config), |
|
| 177 | + ]; |
|
| 178 | + |
|
| 179 | + return $steps; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @param string $scope |
|
| 184 | + * @param string $method |
|
| 185 | + * @param array $arguments |
|
| 186 | + */ |
|
| 187 | + public function emit($scope, $method, array $arguments = []) { |
|
| 188 | + if (!is_null($this->dispatcher)) { |
|
| 189 | + $this->dispatcher->dispatch("$scope::$method", |
|
| 190 | + new GenericEvent("$scope::$method", $arguments)); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + public function info($string) { |
|
| 195 | + // for now just emit as we did in the past |
|
| 196 | + $this->emit('\OC\Repair', 'info', array($string)); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * @param string $message |
|
| 201 | + */ |
|
| 202 | + public function warning($message) { |
|
| 203 | + // for now just emit as we did in the past |
|
| 204 | + $this->emit('\OC\Repair', 'warning', [$message]); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * @param int $max |
|
| 209 | + */ |
|
| 210 | + public function startProgress($max = 0) { |
|
| 211 | + // for now just emit as we did in the past |
|
| 212 | + $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * @param int $step |
|
| 217 | + * @param string $description |
|
| 218 | + */ |
|
| 219 | + public function advance($step = 1, $description = '') { |
|
| 220 | + // for now just emit as we did in the past |
|
| 221 | + $this->emit('\OC\Repair', 'advance', [$step, $description]); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * @param int $max |
|
| 226 | + */ |
|
| 227 | + public function finishProgress() { |
|
| 228 | + // for now just emit as we did in the past |
|
| 229 | + $this->emit('\OC\Repair', 'finishProgress', []); |
|
| 230 | + } |
|
| 231 | 231 | } |
@@ -176,7 +176,7 @@ |
||
| 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); |
@@ -34,176 +34,176 @@ |
||
| 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 | - try { |
|
| 68 | - $this->close(); |
|
| 69 | - } catch (SessionNotAvailableException $e){ |
|
| 70 | - // This exception can occur if session is already closed |
|
| 71 | - // So it is safe to ignore it and let the garbage collector to proceed |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - protected function initializeSession() { |
|
| 76 | - $encryptedSessionData = $this->session->get(self::encryptedSessionName); |
|
| 77 | - try { |
|
| 78 | - $this->sessionValues = json_decode( |
|
| 79 | - $this->crypto->decrypt($encryptedSessionData, $this->passphrase), |
|
| 80 | - true |
|
| 81 | - ); |
|
| 82 | - } catch (\Exception $e) { |
|
| 83 | - $this->sessionValues = []; |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Set a value in the session |
|
| 89 | - * |
|
| 90 | - * @param string $key |
|
| 91 | - * @param mixed $value |
|
| 92 | - */ |
|
| 93 | - public function set($key, $value) { |
|
| 94 | - $this->sessionValues[$key] = $value; |
|
| 95 | - $this->isModified = true; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get a value from the session |
|
| 100 | - * |
|
| 101 | - * @param string $key |
|
| 102 | - * @return string|null Either the value or null |
|
| 103 | - */ |
|
| 104 | - public function get($key) { |
|
| 105 | - if(isset($this->sessionValues[$key])) { |
|
| 106 | - return $this->sessionValues[$key]; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return null; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Check if a named key exists in the session |
|
| 114 | - * |
|
| 115 | - * @param string $key |
|
| 116 | - * @return bool |
|
| 117 | - */ |
|
| 118 | - public function exists($key) { |
|
| 119 | - return isset($this->sessionValues[$key]); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Remove a $key/$value pair from the session |
|
| 124 | - * |
|
| 125 | - * @param string $key |
|
| 126 | - */ |
|
| 127 | - public function remove($key) { |
|
| 128 | - $this->isModified = true; |
|
| 129 | - unset($this->sessionValues[$key]); |
|
| 130 | - $this->session->remove(self::encryptedSessionName); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Reset and recreate the session |
|
| 135 | - */ |
|
| 136 | - public function clear() { |
|
| 137 | - $requesttoken = $this->get('requesttoken'); |
|
| 138 | - $this->sessionValues = []; |
|
| 139 | - if ($requesttoken !== null) { |
|
| 140 | - $this->set('requesttoken', $requesttoken); |
|
| 141 | - } |
|
| 142 | - $this->isModified = true; |
|
| 143 | - $this->session->clear(); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Wrapper around session_regenerate_id |
|
| 148 | - * |
|
| 149 | - * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
|
| 150 | - * @return void |
|
| 151 | - */ |
|
| 152 | - public function regenerateId($deleteOldSession = true) { |
|
| 153 | - $this->session->regenerateId($deleteOldSession); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Wrapper around session_id |
|
| 158 | - * |
|
| 159 | - * @return string |
|
| 160 | - * @throws SessionNotAvailableException |
|
| 161 | - * @since 9.1.0 |
|
| 162 | - */ |
|
| 163 | - public function getId() { |
|
| 164 | - return $this->session->getId(); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Close the session and release the lock, also writes all changed data in batch |
|
| 169 | - */ |
|
| 170 | - public function close() { |
|
| 171 | - if($this->isModified) { |
|
| 172 | - $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
|
| 173 | - $this->session->set(self::encryptedSessionName, $encryptedValue); |
|
| 174 | - $this->isModified = false; |
|
| 175 | - } |
|
| 176 | - $this->session->close(); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * @param mixed $offset |
|
| 181 | - * @return bool |
|
| 182 | - */ |
|
| 183 | - public function offsetExists($offset) { |
|
| 184 | - return $this->exists($offset); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * @param mixed $offset |
|
| 189 | - * @return mixed |
|
| 190 | - */ |
|
| 191 | - public function offsetGet($offset) { |
|
| 192 | - return $this->get($offset); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * @param mixed $offset |
|
| 197 | - * @param mixed $value |
|
| 198 | - */ |
|
| 199 | - public function offsetSet($offset, $value) { |
|
| 200 | - $this->set($offset, $value); |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * @param mixed $offset |
|
| 205 | - */ |
|
| 206 | - public function offsetUnset($offset) { |
|
| 207 | - $this->remove($offset); |
|
| 208 | - } |
|
| 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 | + try { |
|
| 68 | + $this->close(); |
|
| 69 | + } catch (SessionNotAvailableException $e){ |
|
| 70 | + // This exception can occur if session is already closed |
|
| 71 | + // So it is safe to ignore it and let the garbage collector to proceed |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + protected function initializeSession() { |
|
| 76 | + $encryptedSessionData = $this->session->get(self::encryptedSessionName); |
|
| 77 | + try { |
|
| 78 | + $this->sessionValues = json_decode( |
|
| 79 | + $this->crypto->decrypt($encryptedSessionData, $this->passphrase), |
|
| 80 | + true |
|
| 81 | + ); |
|
| 82 | + } catch (\Exception $e) { |
|
| 83 | + $this->sessionValues = []; |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Set a value in the session |
|
| 89 | + * |
|
| 90 | + * @param string $key |
|
| 91 | + * @param mixed $value |
|
| 92 | + */ |
|
| 93 | + public function set($key, $value) { |
|
| 94 | + $this->sessionValues[$key] = $value; |
|
| 95 | + $this->isModified = true; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get a value from the session |
|
| 100 | + * |
|
| 101 | + * @param string $key |
|
| 102 | + * @return string|null Either the value or null |
|
| 103 | + */ |
|
| 104 | + public function get($key) { |
|
| 105 | + if(isset($this->sessionValues[$key])) { |
|
| 106 | + return $this->sessionValues[$key]; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return null; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Check if a named key exists in the session |
|
| 114 | + * |
|
| 115 | + * @param string $key |
|
| 116 | + * @return bool |
|
| 117 | + */ |
|
| 118 | + public function exists($key) { |
|
| 119 | + return isset($this->sessionValues[$key]); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Remove a $key/$value pair from the session |
|
| 124 | + * |
|
| 125 | + * @param string $key |
|
| 126 | + */ |
|
| 127 | + public function remove($key) { |
|
| 128 | + $this->isModified = true; |
|
| 129 | + unset($this->sessionValues[$key]); |
|
| 130 | + $this->session->remove(self::encryptedSessionName); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Reset and recreate the session |
|
| 135 | + */ |
|
| 136 | + public function clear() { |
|
| 137 | + $requesttoken = $this->get('requesttoken'); |
|
| 138 | + $this->sessionValues = []; |
|
| 139 | + if ($requesttoken !== null) { |
|
| 140 | + $this->set('requesttoken', $requesttoken); |
|
| 141 | + } |
|
| 142 | + $this->isModified = true; |
|
| 143 | + $this->session->clear(); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Wrapper around session_regenerate_id |
|
| 148 | + * |
|
| 149 | + * @param bool $deleteOldSession Whether to delete the old associated session file or not. |
|
| 150 | + * @return void |
|
| 151 | + */ |
|
| 152 | + public function regenerateId($deleteOldSession = true) { |
|
| 153 | + $this->session->regenerateId($deleteOldSession); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Wrapper around session_id |
|
| 158 | + * |
|
| 159 | + * @return string |
|
| 160 | + * @throws SessionNotAvailableException |
|
| 161 | + * @since 9.1.0 |
|
| 162 | + */ |
|
| 163 | + public function getId() { |
|
| 164 | + return $this->session->getId(); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Close the session and release the lock, also writes all changed data in batch |
|
| 169 | + */ |
|
| 170 | + public function close() { |
|
| 171 | + if($this->isModified) { |
|
| 172 | + $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
|
| 173 | + $this->session->set(self::encryptedSessionName, $encryptedValue); |
|
| 174 | + $this->isModified = false; |
|
| 175 | + } |
|
| 176 | + $this->session->close(); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * @param mixed $offset |
|
| 181 | + * @return bool |
|
| 182 | + */ |
|
| 183 | + public function offsetExists($offset) { |
|
| 184 | + return $this->exists($offset); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * @param mixed $offset |
|
| 189 | + * @return mixed |
|
| 190 | + */ |
|
| 191 | + public function offsetGet($offset) { |
|
| 192 | + return $this->get($offset); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * @param mixed $offset |
|
| 197 | + * @param mixed $value |
|
| 198 | + */ |
|
| 199 | + public function offsetSet($offset, $value) { |
|
| 200 | + $this->set($offset, $value); |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * @param mixed $offset |
|
| 205 | + */ |
|
| 206 | + public function offsetUnset($offset) { |
|
| 207 | + $this->remove($offset); |
|
| 208 | + } |
|
| 209 | 209 | } |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | public function __destruct() { |
| 67 | 67 | try { |
| 68 | 68 | $this->close(); |
| 69 | - } catch (SessionNotAvailableException $e){ |
|
| 69 | + } catch (SessionNotAvailableException $e) { |
|
| 70 | 70 | // This exception can occur if session is already closed |
| 71 | 71 | // So it is safe to ignore it and let the garbage collector to proceed |
| 72 | 72 | } |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | * @return string|null Either the value or null |
| 103 | 103 | */ |
| 104 | 104 | public function get($key) { |
| 105 | - if(isset($this->sessionValues[$key])) { |
|
| 105 | + if (isset($this->sessionValues[$key])) { |
|
| 106 | 106 | return $this->sessionValues[$key]; |
| 107 | 107 | } |
| 108 | 108 | |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | * Close the session and release the lock, also writes all changed data in batch |
| 169 | 169 | */ |
| 170 | 170 | public function close() { |
| 171 | - if($this->isModified) { |
|
| 171 | + if ($this->isModified) { |
|
| 172 | 172 | $encryptedValue = $this->crypto->encrypt(json_encode($this->sessionValues), $this->passphrase); |
| 173 | 173 | $this->session->set(self::encryptedSessionName, $encryptedValue); |
| 174 | 174 | $this->isModified = false; |
@@ -742,11 +742,19 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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) { |
@@ -48,761 +48,761 @@ |
||
| 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 | } |
@@ -132,7 +132,7 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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( |
@@ -103,7 +103,7 @@ |
||
| 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 |
@@ -114,8 +114,7 @@ discard block |
||
| 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 |
||
| 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 | } |
@@ -31,158 +31,158 @@ |
||
| 31 | 31 | use OCP\Defaults; |
| 32 | 32 | |
| 33 | 33 | class Base { |
| 34 | - private $template; // The template |
|
| 35 | - private $vars; // Vars |
|
| 36 | - |
|
| 37 | - /** @var \OCP\IL10N */ |
|
| 38 | - private $l10n; |
|
| 39 | - |
|
| 40 | - /** @var Defaults */ |
|
| 41 | - private $theme; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @param string $template |
|
| 45 | - * @param string $requestToken |
|
| 46 | - * @param \OCP\IL10N $l10n |
|
| 47 | - * @param Defaults $theme |
|
| 48 | - */ |
|
| 49 | - public function __construct($template, $requestToken, $l10n, $theme ) { |
|
| 50 | - $this->vars = array(); |
|
| 51 | - $this->vars['requesttoken'] = $requestToken; |
|
| 52 | - $this->l10n = $l10n; |
|
| 53 | - $this->template = $template; |
|
| 54 | - $this->theme = $theme; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * @param string $serverRoot |
|
| 59 | - * @param string|false $app_dir |
|
| 60 | - * @param string $theme |
|
| 61 | - * @param string $app |
|
| 62 | - * @return string[] |
|
| 63 | - */ |
|
| 64 | - protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
|
| 65 | - // Check if the app is in the app folder or in the root |
|
| 66 | - if( file_exists($app_dir.'/templates/' )) { |
|
| 67 | - return [ |
|
| 68 | - $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
|
| 69 | - $app_dir.'/templates/', |
|
| 70 | - ]; |
|
| 71 | - } |
|
| 72 | - return [ |
|
| 73 | - $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/', |
|
| 74 | - $serverRoot.'/'.$app.'/templates/', |
|
| 75 | - ]; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @param string $serverRoot |
|
| 80 | - * @param string $theme |
|
| 81 | - * @return string[] |
|
| 82 | - */ |
|
| 83 | - protected function getCoreTemplateDirs($theme, $serverRoot) { |
|
| 84 | - return [ |
|
| 85 | - $serverRoot.'/themes/'.$theme.'/core/templates/', |
|
| 86 | - $serverRoot.'/core/templates/', |
|
| 87 | - ]; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Assign variables |
|
| 92 | - * @param string $key key |
|
| 93 | - * @param array|bool|integer|string $value value |
|
| 94 | - * @return bool |
|
| 95 | - * |
|
| 96 | - * This function assigns a variable. It can be accessed via $_[$key] in |
|
| 97 | - * the template. |
|
| 98 | - * |
|
| 99 | - * If the key existed before, it will be overwritten |
|
| 100 | - */ |
|
| 101 | - public function assign( $key, $value) { |
|
| 102 | - $this->vars[$key] = $value; |
|
| 103 | - return true; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Appends a variable |
|
| 108 | - * @param string $key key |
|
| 109 | - * @param mixed $value value |
|
| 110 | - * @return boolean|null |
|
| 111 | - * |
|
| 112 | - * This function assigns a variable in an array context. If the key already |
|
| 113 | - * exists, the value will be appended. It can be accessed via |
|
| 114 | - * $_[$key][$position] in the template. |
|
| 115 | - */ |
|
| 116 | - public function append( $key, $value ) { |
|
| 117 | - if( array_key_exists( $key, $this->vars )) { |
|
| 118 | - $this->vars[$key][] = $value; |
|
| 119 | - } |
|
| 120 | - else{ |
|
| 121 | - $this->vars[$key] = array( $value ); |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Prints the proceeded template |
|
| 127 | - * @return bool |
|
| 128 | - * |
|
| 129 | - * This function proceeds the template and prints its output. |
|
| 130 | - */ |
|
| 131 | - public function printPage() { |
|
| 132 | - $data = $this->fetchPage(); |
|
| 133 | - if( $data === false ) { |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - else{ |
|
| 137 | - print $data; |
|
| 138 | - return true; |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Process the template |
|
| 144 | - * |
|
| 145 | - * @param array|null $additionalParams |
|
| 146 | - * @return string This function processes the template. |
|
| 147 | - * |
|
| 148 | - * This function processes the template. |
|
| 149 | - */ |
|
| 150 | - public function fetchPage($additionalParams = null) { |
|
| 151 | - return $this->load($this->template, $additionalParams); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * doing the actual work |
|
| 156 | - * |
|
| 157 | - * @param string $file |
|
| 158 | - * @param array|null $additionalParams |
|
| 159 | - * @return string content |
|
| 160 | - * |
|
| 161 | - * Includes the template file, fetches its output |
|
| 162 | - */ |
|
| 163 | - protected function load($file, $additionalParams = null) { |
|
| 164 | - // Register the variables |
|
| 165 | - $_ = $this->vars; |
|
| 166 | - $l = $this->l10n; |
|
| 167 | - $theme = $this->theme; |
|
| 168 | - |
|
| 169 | - if( !is_null($additionalParams)) { |
|
| 170 | - $_ = array_merge( $additionalParams, $this->vars ); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - // Include |
|
| 174 | - ob_start(); |
|
| 175 | - try { |
|
| 176 | - include $file; |
|
| 177 | - $data = ob_get_contents(); |
|
| 178 | - } catch (\Exception $e) { |
|
| 179 | - @ob_end_clean(); |
|
| 180 | - throw $e; |
|
| 181 | - } |
|
| 182 | - @ob_end_clean(); |
|
| 183 | - |
|
| 184 | - // Return data |
|
| 185 | - return $data; |
|
| 186 | - } |
|
| 34 | + private $template; // The template |
|
| 35 | + private $vars; // Vars |
|
| 36 | + |
|
| 37 | + /** @var \OCP\IL10N */ |
|
| 38 | + private $l10n; |
|
| 39 | + |
|
| 40 | + /** @var Defaults */ |
|
| 41 | + private $theme; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @param string $template |
|
| 45 | + * @param string $requestToken |
|
| 46 | + * @param \OCP\IL10N $l10n |
|
| 47 | + * @param Defaults $theme |
|
| 48 | + */ |
|
| 49 | + public function __construct($template, $requestToken, $l10n, $theme ) { |
|
| 50 | + $this->vars = array(); |
|
| 51 | + $this->vars['requesttoken'] = $requestToken; |
|
| 52 | + $this->l10n = $l10n; |
|
| 53 | + $this->template = $template; |
|
| 54 | + $this->theme = $theme; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * @param string $serverRoot |
|
| 59 | + * @param string|false $app_dir |
|
| 60 | + * @param string $theme |
|
| 61 | + * @param string $app |
|
| 62 | + * @return string[] |
|
| 63 | + */ |
|
| 64 | + protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
|
| 65 | + // Check if the app is in the app folder or in the root |
|
| 66 | + if( file_exists($app_dir.'/templates/' )) { |
|
| 67 | + return [ |
|
| 68 | + $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
|
| 69 | + $app_dir.'/templates/', |
|
| 70 | + ]; |
|
| 71 | + } |
|
| 72 | + return [ |
|
| 73 | + $serverRoot.'/themes/'.$theme.'/'.$app.'/templates/', |
|
| 74 | + $serverRoot.'/'.$app.'/templates/', |
|
| 75 | + ]; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @param string $serverRoot |
|
| 80 | + * @param string $theme |
|
| 81 | + * @return string[] |
|
| 82 | + */ |
|
| 83 | + protected function getCoreTemplateDirs($theme, $serverRoot) { |
|
| 84 | + return [ |
|
| 85 | + $serverRoot.'/themes/'.$theme.'/core/templates/', |
|
| 86 | + $serverRoot.'/core/templates/', |
|
| 87 | + ]; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Assign variables |
|
| 92 | + * @param string $key key |
|
| 93 | + * @param array|bool|integer|string $value value |
|
| 94 | + * @return bool |
|
| 95 | + * |
|
| 96 | + * This function assigns a variable. It can be accessed via $_[$key] in |
|
| 97 | + * the template. |
|
| 98 | + * |
|
| 99 | + * If the key existed before, it will be overwritten |
|
| 100 | + */ |
|
| 101 | + public function assign( $key, $value) { |
|
| 102 | + $this->vars[$key] = $value; |
|
| 103 | + return true; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Appends a variable |
|
| 108 | + * @param string $key key |
|
| 109 | + * @param mixed $value value |
|
| 110 | + * @return boolean|null |
|
| 111 | + * |
|
| 112 | + * This function assigns a variable in an array context. If the key already |
|
| 113 | + * exists, the value will be appended. It can be accessed via |
|
| 114 | + * $_[$key][$position] in the template. |
|
| 115 | + */ |
|
| 116 | + public function append( $key, $value ) { |
|
| 117 | + if( array_key_exists( $key, $this->vars )) { |
|
| 118 | + $this->vars[$key][] = $value; |
|
| 119 | + } |
|
| 120 | + else{ |
|
| 121 | + $this->vars[$key] = array( $value ); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Prints the proceeded template |
|
| 127 | + * @return bool |
|
| 128 | + * |
|
| 129 | + * This function proceeds the template and prints its output. |
|
| 130 | + */ |
|
| 131 | + public function printPage() { |
|
| 132 | + $data = $this->fetchPage(); |
|
| 133 | + if( $data === false ) { |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + else{ |
|
| 137 | + print $data; |
|
| 138 | + return true; |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Process the template |
|
| 144 | + * |
|
| 145 | + * @param array|null $additionalParams |
|
| 146 | + * @return string This function processes the template. |
|
| 147 | + * |
|
| 148 | + * This function processes the template. |
|
| 149 | + */ |
|
| 150 | + public function fetchPage($additionalParams = null) { |
|
| 151 | + return $this->load($this->template, $additionalParams); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * doing the actual work |
|
| 156 | + * |
|
| 157 | + * @param string $file |
|
| 158 | + * @param array|null $additionalParams |
|
| 159 | + * @return string content |
|
| 160 | + * |
|
| 161 | + * Includes the template file, fetches its output |
|
| 162 | + */ |
|
| 163 | + protected function load($file, $additionalParams = null) { |
|
| 164 | + // Register the variables |
|
| 165 | + $_ = $this->vars; |
|
| 166 | + $l = $this->l10n; |
|
| 167 | + $theme = $this->theme; |
|
| 168 | + |
|
| 169 | + if( !is_null($additionalParams)) { |
|
| 170 | + $_ = array_merge( $additionalParams, $this->vars ); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + // Include |
|
| 174 | + ob_start(); |
|
| 175 | + try { |
|
| 176 | + include $file; |
|
| 177 | + $data = ob_get_contents(); |
|
| 178 | + } catch (\Exception $e) { |
|
| 179 | + @ob_end_clean(); |
|
| 180 | + throw $e; |
|
| 181 | + } |
|
| 182 | + @ob_end_clean(); |
|
| 183 | + |
|
| 184 | + // Return data |
|
| 185 | + return $data; |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | 188 | } |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * @param \OCP\IL10N $l10n |
| 47 | 47 | * @param Defaults $theme |
| 48 | 48 | */ |
| 49 | - public function __construct($template, $requestToken, $l10n, $theme ) { |
|
| 49 | + public function __construct($template, $requestToken, $l10n, $theme) { |
|
| 50 | 50 | $this->vars = array(); |
| 51 | 51 | $this->vars['requesttoken'] = $requestToken; |
| 52 | 52 | $this->l10n = $l10n; |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | */ |
| 64 | 64 | protected function getAppTemplateDirs($theme, $app, $serverRoot, $app_dir) { |
| 65 | 65 | // Check if the app is in the app folder or in the root |
| 66 | - if( file_exists($app_dir.'/templates/' )) { |
|
| 66 | + if (file_exists($app_dir.'/templates/')) { |
|
| 67 | 67 | return [ |
| 68 | 68 | $serverRoot.'/themes/'.$theme.'/apps/'.$app.'/templates/', |
| 69 | 69 | $app_dir.'/templates/', |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * If the key existed before, it will be overwritten |
| 100 | 100 | */ |
| 101 | - public function assign( $key, $value) { |
|
| 101 | + public function assign($key, $value) { |
|
| 102 | 102 | $this->vars[$key] = $value; |
| 103 | 103 | return true; |
| 104 | 104 | } |
@@ -113,12 +113,12 @@ discard block |
||
| 113 | 113 | * exists, the value will be appended. It can be accessed via |
| 114 | 114 | * $_[$key][$position] in the template. |
| 115 | 115 | */ |
| 116 | - public function append( $key, $value ) { |
|
| 117 | - if( array_key_exists( $key, $this->vars )) { |
|
| 116 | + public function append($key, $value) { |
|
| 117 | + if (array_key_exists($key, $this->vars)) { |
|
| 118 | 118 | $this->vars[$key][] = $value; |
| 119 | 119 | } |
| 120 | - else{ |
|
| 121 | - $this->vars[$key] = array( $value ); |
|
| 120 | + else { |
|
| 121 | + $this->vars[$key] = array($value); |
|
| 122 | 122 | } |
| 123 | 123 | } |
| 124 | 124 | |
@@ -130,10 +130,10 @@ discard block |
||
| 130 | 130 | */ |
| 131 | 131 | public function printPage() { |
| 132 | 132 | $data = $this->fetchPage(); |
| 133 | - if( $data === false ) { |
|
| 133 | + if ($data === false) { |
|
| 134 | 134 | return false; |
| 135 | 135 | } |
| 136 | - else{ |
|
| 136 | + else { |
|
| 137 | 137 | print $data; |
| 138 | 138 | return true; |
| 139 | 139 | } |
@@ -166,8 +166,8 @@ discard block |
||
| 166 | 166 | $l = $this->l10n; |
| 167 | 167 | $theme = $this->theme; |
| 168 | 168 | |
| 169 | - if( !is_null($additionalParams)) { |
|
| 170 | - $_ = array_merge( $additionalParams, $this->vars ); |
|
| 169 | + if (!is_null($additionalParams)) { |
|
| 170 | + $_ = array_merge($additionalParams, $this->vars); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | // Include |
@@ -46,7 +46,7 @@ |
||
| 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 | */ |
@@ -32,61 +32,61 @@ |
||
| 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 | } |
@@ -84,7 +84,7 @@ |
||
| 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); |
@@ -308,6 +308,7 @@ discard block |
||
| 308 | 308 | * @param array $params the parameters of the sql query |
| 309 | 309 | * @param int $limit the maximum number of rows |
| 310 | 310 | * @param int $offset from which row we want to start |
| 311 | + * @param string $msg |
|
| 311 | 312 | * @return string formatted error message string |
| 312 | 313 | * @since 9.1.0 |
| 313 | 314 | */ |
@@ -360,7 +361,7 @@ discard block |
||
| 360 | 361 | * Returns an db result and throws exceptions when there are more or less |
| 361 | 362 | * results |
| 362 | 363 | * @param string $sql the sql query |
| 363 | - * @param array $params the parameters of the sql query |
|
| 364 | + * @param string[] $params the parameters of the sql query |
|
| 364 | 365 | * @param int $limit the maximum number of rows |
| 365 | 366 | * @param int $offset from which row we want to start |
| 366 | 367 | * @throws DoesNotExistException if the item does not exist |
@@ -37,327 +37,327 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | abstract class Mapper { |
| 39 | 39 | |
| 40 | - protected $tableName; |
|
| 41 | - protected $entityClass; |
|
| 42 | - protected $db; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @param IDBConnection $db Instance of the Db abstraction layer |
|
| 46 | - * @param string $tableName the name of the table. set this to allow entity |
|
| 47 | - * @param string $entityClass the name of the entity that the sql should be |
|
| 48 | - * mapped to queries without using sql |
|
| 49 | - * @since 7.0.0 |
|
| 50 | - */ |
|
| 51 | - public function __construct(IDBConnection $db, $tableName, $entityClass=null){ |
|
| 52 | - $this->db = $db; |
|
| 53 | - $this->tableName = '*PREFIX*' . $tableName; |
|
| 54 | - |
|
| 55 | - // if not given set the entity name to the class without the mapper part |
|
| 56 | - // cache it here for later use since reflection is slow |
|
| 57 | - if($entityClass === null) { |
|
| 58 | - $this->entityClass = str_replace('Mapper', '', get_class($this)); |
|
| 59 | - } else { |
|
| 60 | - $this->entityClass = $entityClass; |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @return string the table name |
|
| 67 | - * @since 7.0.0 |
|
| 68 | - */ |
|
| 69 | - public function getTableName(){ |
|
| 70 | - return $this->tableName; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Deletes an entity from the table |
|
| 76 | - * @param Entity $entity the entity that should be deleted |
|
| 77 | - * @return Entity the deleted entity |
|
| 78 | - * @since 7.0.0 - return value added in 8.1.0 |
|
| 79 | - */ |
|
| 80 | - public function delete(Entity $entity){ |
|
| 81 | - $sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?'; |
|
| 82 | - $stmt = $this->execute($sql, [$entity->getId()]); |
|
| 83 | - $stmt->closeCursor(); |
|
| 84 | - return $entity; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Creates a new entry in the db from an entity |
|
| 90 | - * @param Entity $entity the entity that should be created |
|
| 91 | - * @return Entity the saved entity with the set id |
|
| 92 | - * @since 7.0.0 |
|
| 93 | - */ |
|
| 94 | - public function insert(Entity $entity){ |
|
| 95 | - // get updated fields to save, fields have to be set using a setter to |
|
| 96 | - // be saved |
|
| 97 | - $properties = $entity->getUpdatedFields(); |
|
| 98 | - $values = ''; |
|
| 99 | - $columns = ''; |
|
| 100 | - $params = []; |
|
| 101 | - |
|
| 102 | - // build the fields |
|
| 103 | - $i = 0; |
|
| 104 | - foreach($properties as $property => $updated) { |
|
| 105 | - $column = $entity->propertyToColumn($property); |
|
| 106 | - $getter = 'get' . ucfirst($property); |
|
| 107 | - |
|
| 108 | - $columns .= '`' . $column . '`'; |
|
| 109 | - $values .= '?'; |
|
| 110 | - |
|
| 111 | - // only append colon if there are more entries |
|
| 112 | - if($i < count($properties)-1){ |
|
| 113 | - $columns .= ','; |
|
| 114 | - $values .= ','; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $params[] = $entity->$getter(); |
|
| 118 | - $i++; |
|
| 119 | - |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - $sql = 'INSERT INTO `' . $this->tableName . '`(' . |
|
| 123 | - $columns . ') VALUES(' . $values . ')'; |
|
| 124 | - |
|
| 125 | - $stmt = $this->execute($sql, $params); |
|
| 126 | - |
|
| 127 | - $entity->setId((int) $this->db->lastInsertId($this->tableName)); |
|
| 128 | - |
|
| 129 | - $stmt->closeCursor(); |
|
| 130 | - |
|
| 131 | - return $entity; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Updates an entry in the db from an entity |
|
| 138 | - * @throws \InvalidArgumentException if entity has no id |
|
| 139 | - * @param Entity $entity the entity that should be created |
|
| 140 | - * @return Entity the saved entity with the set id |
|
| 141 | - * @since 7.0.0 - return value was added in 8.0.0 |
|
| 142 | - */ |
|
| 143 | - public function update(Entity $entity){ |
|
| 144 | - // if entity wasn't changed it makes no sense to run a db query |
|
| 145 | - $properties = $entity->getUpdatedFields(); |
|
| 146 | - if(count($properties) === 0) { |
|
| 147 | - return $entity; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - // entity needs an id |
|
| 151 | - $id = $entity->getId(); |
|
| 152 | - if($id === null){ |
|
| 153 | - throw new \InvalidArgumentException( |
|
| 154 | - 'Entity which should be updated has no id'); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - // get updated fields to save, fields have to be set using a setter to |
|
| 158 | - // be saved |
|
| 159 | - // do not update the id field |
|
| 160 | - unset($properties['id']); |
|
| 161 | - |
|
| 162 | - $columns = ''; |
|
| 163 | - $params = []; |
|
| 164 | - |
|
| 165 | - // build the fields |
|
| 166 | - $i = 0; |
|
| 167 | - foreach($properties as $property => $updated) { |
|
| 168 | - |
|
| 169 | - $column = $entity->propertyToColumn($property); |
|
| 170 | - $getter = 'get' . ucfirst($property); |
|
| 171 | - |
|
| 172 | - $columns .= '`' . $column . '` = ?'; |
|
| 173 | - |
|
| 174 | - // only append colon if there are more entries |
|
| 175 | - if($i < count($properties)-1){ |
|
| 176 | - $columns .= ','; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - $params[] = $entity->$getter(); |
|
| 180 | - $i++; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - $sql = 'UPDATE `' . $this->tableName . '` SET ' . |
|
| 184 | - $columns . ' WHERE `id` = ?'; |
|
| 185 | - $params[] = $id; |
|
| 186 | - |
|
| 187 | - $stmt = $this->execute($sql, $params); |
|
| 188 | - $stmt->closeCursor(); |
|
| 189 | - |
|
| 190 | - return $entity; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Checks if an array is associative |
|
| 195 | - * @param array $array |
|
| 196 | - * @return bool true if associative |
|
| 197 | - * @since 8.1.0 |
|
| 198 | - */ |
|
| 199 | - private function isAssocArray(array $array) { |
|
| 200 | - return array_values($array) !== $array; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - /** |
|
| 204 | - * Returns the correct PDO constant based on the value type |
|
| 205 | - * @param $value |
|
| 206 | - * @return int PDO constant |
|
| 207 | - * @since 8.1.0 |
|
| 208 | - */ |
|
| 209 | - private function getPDOType($value) { |
|
| 210 | - switch (gettype($value)) { |
|
| 211 | - case 'integer': |
|
| 212 | - return \PDO::PARAM_INT; |
|
| 213 | - case 'boolean': |
|
| 214 | - return \PDO::PARAM_BOOL; |
|
| 215 | - default: |
|
| 216 | - return \PDO::PARAM_STR; |
|
| 217 | - } |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Runs an sql query |
|
| 223 | - * @param string $sql the prepare string |
|
| 224 | - * @param array $params the params which should replace the ? in the sql query |
|
| 225 | - * @param int $limit the maximum number of rows |
|
| 226 | - * @param int $offset from which row we want to start |
|
| 227 | - * @return \PDOStatement the database query result |
|
| 228 | - * @since 7.0.0 |
|
| 229 | - */ |
|
| 230 | - protected function execute($sql, array $params=[], $limit=null, $offset=null){ |
|
| 231 | - $query = $this->db->prepare($sql, $limit, $offset); |
|
| 232 | - |
|
| 233 | - if ($this->isAssocArray($params)) { |
|
| 234 | - foreach ($params as $key => $param) { |
|
| 235 | - $pdoConstant = $this->getPDOType($param); |
|
| 236 | - $query->bindValue($key, $param, $pdoConstant); |
|
| 237 | - } |
|
| 238 | - } else { |
|
| 239 | - $index = 1; // bindParam is 1 indexed |
|
| 240 | - foreach ($params as $param) { |
|
| 241 | - $pdoConstant = $this->getPDOType($param); |
|
| 242 | - $query->bindValue($index, $param, $pdoConstant); |
|
| 243 | - $index++; |
|
| 244 | - } |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - $result = $query->execute(); |
|
| 248 | - |
|
| 249 | - return $query; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Returns an db result and throws exceptions when there are more or less |
|
| 255 | - * results |
|
| 256 | - * @see findEntity |
|
| 257 | - * @param string $sql the sql query |
|
| 258 | - * @param array $params the parameters of the sql query |
|
| 259 | - * @param int $limit the maximum number of rows |
|
| 260 | - * @param int $offset from which row we want to start |
|
| 261 | - * @throws DoesNotExistException if the item does not exist |
|
| 262 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
| 263 | - * @return array the result as row |
|
| 264 | - * @since 7.0.0 |
|
| 265 | - */ |
|
| 266 | - protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null){ |
|
| 267 | - $stmt = $this->execute($sql, $params, $limit, $offset); |
|
| 268 | - $row = $stmt->fetch(); |
|
| 269 | - |
|
| 270 | - if($row === false || $row === null){ |
|
| 271 | - $stmt->closeCursor(); |
|
| 272 | - $msg = $this->buildDebugMessage( |
|
| 273 | - 'Did expect one result but found none when executing', $sql, $params, $limit, $offset |
|
| 274 | - ); |
|
| 275 | - throw new DoesNotExistException($msg); |
|
| 276 | - } |
|
| 277 | - $row2 = $stmt->fetch(); |
|
| 278 | - $stmt->closeCursor(); |
|
| 279 | - //MDB2 returns null, PDO and doctrine false when no row is available |
|
| 280 | - if( ! ($row2 === false || $row2 === null )) { |
|
| 281 | - $msg = $this->buildDebugMessage( |
|
| 282 | - 'Did not expect more than one result when executing', $sql, $params, $limit, $offset |
|
| 283 | - ); |
|
| 284 | - throw new MultipleObjectsReturnedException($msg); |
|
| 285 | - } else { |
|
| 286 | - return $row; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Builds an error message by prepending the $msg to an error message which |
|
| 292 | - * has the parameters |
|
| 293 | - * @see findEntity |
|
| 294 | - * @param string $sql the sql query |
|
| 295 | - * @param array $params the parameters of the sql query |
|
| 296 | - * @param int $limit the maximum number of rows |
|
| 297 | - * @param int $offset from which row we want to start |
|
| 298 | - * @return string formatted error message string |
|
| 299 | - * @since 9.1.0 |
|
| 300 | - */ |
|
| 301 | - private function buildDebugMessage($msg, $sql, array $params=[], $limit=null, $offset=null) { |
|
| 302 | - return $msg . |
|
| 303 | - ': query "' . $sql . '"; ' . |
|
| 304 | - 'parameters ' . print_r($params, true) . '; ' . |
|
| 305 | - 'limit "' . $limit . '"; '. |
|
| 306 | - 'offset "' . $offset . '"'; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Creates an entity from a row. Automatically determines the entity class |
|
| 312 | - * from the current mapper name (MyEntityMapper -> MyEntity) |
|
| 313 | - * @param array $row the row which should be converted to an entity |
|
| 314 | - * @return Entity the entity |
|
| 315 | - * @since 7.0.0 |
|
| 316 | - */ |
|
| 317 | - protected function mapRowToEntity($row) { |
|
| 318 | - return call_user_func($this->entityClass .'::fromRow', $row); |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Runs a sql query and returns an array of entities |
|
| 324 | - * @param string $sql the prepare string |
|
| 325 | - * @param array $params the params which should replace the ? in the sql query |
|
| 326 | - * @param int $limit the maximum number of rows |
|
| 327 | - * @param int $offset from which row we want to start |
|
| 328 | - * @return array all fetched entities |
|
| 329 | - * @since 7.0.0 |
|
| 330 | - */ |
|
| 331 | - protected function findEntities($sql, array $params=[], $limit=null, $offset=null) { |
|
| 332 | - $stmt = $this->execute($sql, $params, $limit, $offset); |
|
| 333 | - |
|
| 334 | - $entities = []; |
|
| 335 | - |
|
| 336 | - while($row = $stmt->fetch()){ |
|
| 337 | - $entities[] = $this->mapRowToEntity($row); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - $stmt->closeCursor(); |
|
| 341 | - |
|
| 342 | - return $entities; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Returns an db result and throws exceptions when there are more or less |
|
| 348 | - * results |
|
| 349 | - * @param string $sql the sql query |
|
| 350 | - * @param array $params the parameters of the sql query |
|
| 351 | - * @param int $limit the maximum number of rows |
|
| 352 | - * @param int $offset from which row we want to start |
|
| 353 | - * @throws DoesNotExistException if the item does not exist |
|
| 354 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
| 355 | - * @return Entity the entity |
|
| 356 | - * @since 7.0.0 |
|
| 357 | - */ |
|
| 358 | - protected function findEntity($sql, array $params=[], $limit=null, $offset=null){ |
|
| 359 | - return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); |
|
| 360 | - } |
|
| 40 | + protected $tableName; |
|
| 41 | + protected $entityClass; |
|
| 42 | + protected $db; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @param IDBConnection $db Instance of the Db abstraction layer |
|
| 46 | + * @param string $tableName the name of the table. set this to allow entity |
|
| 47 | + * @param string $entityClass the name of the entity that the sql should be |
|
| 48 | + * mapped to queries without using sql |
|
| 49 | + * @since 7.0.0 |
|
| 50 | + */ |
|
| 51 | + public function __construct(IDBConnection $db, $tableName, $entityClass=null){ |
|
| 52 | + $this->db = $db; |
|
| 53 | + $this->tableName = '*PREFIX*' . $tableName; |
|
| 54 | + |
|
| 55 | + // if not given set the entity name to the class without the mapper part |
|
| 56 | + // cache it here for later use since reflection is slow |
|
| 57 | + if($entityClass === null) { |
|
| 58 | + $this->entityClass = str_replace('Mapper', '', get_class($this)); |
|
| 59 | + } else { |
|
| 60 | + $this->entityClass = $entityClass; |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @return string the table name |
|
| 67 | + * @since 7.0.0 |
|
| 68 | + */ |
|
| 69 | + public function getTableName(){ |
|
| 70 | + return $this->tableName; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Deletes an entity from the table |
|
| 76 | + * @param Entity $entity the entity that should be deleted |
|
| 77 | + * @return Entity the deleted entity |
|
| 78 | + * @since 7.0.0 - return value added in 8.1.0 |
|
| 79 | + */ |
|
| 80 | + public function delete(Entity $entity){ |
|
| 81 | + $sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?'; |
|
| 82 | + $stmt = $this->execute($sql, [$entity->getId()]); |
|
| 83 | + $stmt->closeCursor(); |
|
| 84 | + return $entity; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Creates a new entry in the db from an entity |
|
| 90 | + * @param Entity $entity the entity that should be created |
|
| 91 | + * @return Entity the saved entity with the set id |
|
| 92 | + * @since 7.0.0 |
|
| 93 | + */ |
|
| 94 | + public function insert(Entity $entity){ |
|
| 95 | + // get updated fields to save, fields have to be set using a setter to |
|
| 96 | + // be saved |
|
| 97 | + $properties = $entity->getUpdatedFields(); |
|
| 98 | + $values = ''; |
|
| 99 | + $columns = ''; |
|
| 100 | + $params = []; |
|
| 101 | + |
|
| 102 | + // build the fields |
|
| 103 | + $i = 0; |
|
| 104 | + foreach($properties as $property => $updated) { |
|
| 105 | + $column = $entity->propertyToColumn($property); |
|
| 106 | + $getter = 'get' . ucfirst($property); |
|
| 107 | + |
|
| 108 | + $columns .= '`' . $column . '`'; |
|
| 109 | + $values .= '?'; |
|
| 110 | + |
|
| 111 | + // only append colon if there are more entries |
|
| 112 | + if($i < count($properties)-1){ |
|
| 113 | + $columns .= ','; |
|
| 114 | + $values .= ','; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $params[] = $entity->$getter(); |
|
| 118 | + $i++; |
|
| 119 | + |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + $sql = 'INSERT INTO `' . $this->tableName . '`(' . |
|
| 123 | + $columns . ') VALUES(' . $values . ')'; |
|
| 124 | + |
|
| 125 | + $stmt = $this->execute($sql, $params); |
|
| 126 | + |
|
| 127 | + $entity->setId((int) $this->db->lastInsertId($this->tableName)); |
|
| 128 | + |
|
| 129 | + $stmt->closeCursor(); |
|
| 130 | + |
|
| 131 | + return $entity; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Updates an entry in the db from an entity |
|
| 138 | + * @throws \InvalidArgumentException if entity has no id |
|
| 139 | + * @param Entity $entity the entity that should be created |
|
| 140 | + * @return Entity the saved entity with the set id |
|
| 141 | + * @since 7.0.0 - return value was added in 8.0.0 |
|
| 142 | + */ |
|
| 143 | + public function update(Entity $entity){ |
|
| 144 | + // if entity wasn't changed it makes no sense to run a db query |
|
| 145 | + $properties = $entity->getUpdatedFields(); |
|
| 146 | + if(count($properties) === 0) { |
|
| 147 | + return $entity; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + // entity needs an id |
|
| 151 | + $id = $entity->getId(); |
|
| 152 | + if($id === null){ |
|
| 153 | + throw new \InvalidArgumentException( |
|
| 154 | + 'Entity which should be updated has no id'); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + // get updated fields to save, fields have to be set using a setter to |
|
| 158 | + // be saved |
|
| 159 | + // do not update the id field |
|
| 160 | + unset($properties['id']); |
|
| 161 | + |
|
| 162 | + $columns = ''; |
|
| 163 | + $params = []; |
|
| 164 | + |
|
| 165 | + // build the fields |
|
| 166 | + $i = 0; |
|
| 167 | + foreach($properties as $property => $updated) { |
|
| 168 | + |
|
| 169 | + $column = $entity->propertyToColumn($property); |
|
| 170 | + $getter = 'get' . ucfirst($property); |
|
| 171 | + |
|
| 172 | + $columns .= '`' . $column . '` = ?'; |
|
| 173 | + |
|
| 174 | + // only append colon if there are more entries |
|
| 175 | + if($i < count($properties)-1){ |
|
| 176 | + $columns .= ','; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + $params[] = $entity->$getter(); |
|
| 180 | + $i++; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + $sql = 'UPDATE `' . $this->tableName . '` SET ' . |
|
| 184 | + $columns . ' WHERE `id` = ?'; |
|
| 185 | + $params[] = $id; |
|
| 186 | + |
|
| 187 | + $stmt = $this->execute($sql, $params); |
|
| 188 | + $stmt->closeCursor(); |
|
| 189 | + |
|
| 190 | + return $entity; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Checks if an array is associative |
|
| 195 | + * @param array $array |
|
| 196 | + * @return bool true if associative |
|
| 197 | + * @since 8.1.0 |
|
| 198 | + */ |
|
| 199 | + private function isAssocArray(array $array) { |
|
| 200 | + return array_values($array) !== $array; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + /** |
|
| 204 | + * Returns the correct PDO constant based on the value type |
|
| 205 | + * @param $value |
|
| 206 | + * @return int PDO constant |
|
| 207 | + * @since 8.1.0 |
|
| 208 | + */ |
|
| 209 | + private function getPDOType($value) { |
|
| 210 | + switch (gettype($value)) { |
|
| 211 | + case 'integer': |
|
| 212 | + return \PDO::PARAM_INT; |
|
| 213 | + case 'boolean': |
|
| 214 | + return \PDO::PARAM_BOOL; |
|
| 215 | + default: |
|
| 216 | + return \PDO::PARAM_STR; |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Runs an sql query |
|
| 223 | + * @param string $sql the prepare string |
|
| 224 | + * @param array $params the params which should replace the ? in the sql query |
|
| 225 | + * @param int $limit the maximum number of rows |
|
| 226 | + * @param int $offset from which row we want to start |
|
| 227 | + * @return \PDOStatement the database query result |
|
| 228 | + * @since 7.0.0 |
|
| 229 | + */ |
|
| 230 | + protected function execute($sql, array $params=[], $limit=null, $offset=null){ |
|
| 231 | + $query = $this->db->prepare($sql, $limit, $offset); |
|
| 232 | + |
|
| 233 | + if ($this->isAssocArray($params)) { |
|
| 234 | + foreach ($params as $key => $param) { |
|
| 235 | + $pdoConstant = $this->getPDOType($param); |
|
| 236 | + $query->bindValue($key, $param, $pdoConstant); |
|
| 237 | + } |
|
| 238 | + } else { |
|
| 239 | + $index = 1; // bindParam is 1 indexed |
|
| 240 | + foreach ($params as $param) { |
|
| 241 | + $pdoConstant = $this->getPDOType($param); |
|
| 242 | + $query->bindValue($index, $param, $pdoConstant); |
|
| 243 | + $index++; |
|
| 244 | + } |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + $result = $query->execute(); |
|
| 248 | + |
|
| 249 | + return $query; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Returns an db result and throws exceptions when there are more or less |
|
| 255 | + * results |
|
| 256 | + * @see findEntity |
|
| 257 | + * @param string $sql the sql query |
|
| 258 | + * @param array $params the parameters of the sql query |
|
| 259 | + * @param int $limit the maximum number of rows |
|
| 260 | + * @param int $offset from which row we want to start |
|
| 261 | + * @throws DoesNotExistException if the item does not exist |
|
| 262 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
| 263 | + * @return array the result as row |
|
| 264 | + * @since 7.0.0 |
|
| 265 | + */ |
|
| 266 | + protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null){ |
|
| 267 | + $stmt = $this->execute($sql, $params, $limit, $offset); |
|
| 268 | + $row = $stmt->fetch(); |
|
| 269 | + |
|
| 270 | + if($row === false || $row === null){ |
|
| 271 | + $stmt->closeCursor(); |
|
| 272 | + $msg = $this->buildDebugMessage( |
|
| 273 | + 'Did expect one result but found none when executing', $sql, $params, $limit, $offset |
|
| 274 | + ); |
|
| 275 | + throw new DoesNotExistException($msg); |
|
| 276 | + } |
|
| 277 | + $row2 = $stmt->fetch(); |
|
| 278 | + $stmt->closeCursor(); |
|
| 279 | + //MDB2 returns null, PDO and doctrine false when no row is available |
|
| 280 | + if( ! ($row2 === false || $row2 === null )) { |
|
| 281 | + $msg = $this->buildDebugMessage( |
|
| 282 | + 'Did not expect more than one result when executing', $sql, $params, $limit, $offset |
|
| 283 | + ); |
|
| 284 | + throw new MultipleObjectsReturnedException($msg); |
|
| 285 | + } else { |
|
| 286 | + return $row; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Builds an error message by prepending the $msg to an error message which |
|
| 292 | + * has the parameters |
|
| 293 | + * @see findEntity |
|
| 294 | + * @param string $sql the sql query |
|
| 295 | + * @param array $params the parameters of the sql query |
|
| 296 | + * @param int $limit the maximum number of rows |
|
| 297 | + * @param int $offset from which row we want to start |
|
| 298 | + * @return string formatted error message string |
|
| 299 | + * @since 9.1.0 |
|
| 300 | + */ |
|
| 301 | + private function buildDebugMessage($msg, $sql, array $params=[], $limit=null, $offset=null) { |
|
| 302 | + return $msg . |
|
| 303 | + ': query "' . $sql . '"; ' . |
|
| 304 | + 'parameters ' . print_r($params, true) . '; ' . |
|
| 305 | + 'limit "' . $limit . '"; '. |
|
| 306 | + 'offset "' . $offset . '"'; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Creates an entity from a row. Automatically determines the entity class |
|
| 312 | + * from the current mapper name (MyEntityMapper -> MyEntity) |
|
| 313 | + * @param array $row the row which should be converted to an entity |
|
| 314 | + * @return Entity the entity |
|
| 315 | + * @since 7.0.0 |
|
| 316 | + */ |
|
| 317 | + protected function mapRowToEntity($row) { |
|
| 318 | + return call_user_func($this->entityClass .'::fromRow', $row); |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Runs a sql query and returns an array of entities |
|
| 324 | + * @param string $sql the prepare string |
|
| 325 | + * @param array $params the params which should replace the ? in the sql query |
|
| 326 | + * @param int $limit the maximum number of rows |
|
| 327 | + * @param int $offset from which row we want to start |
|
| 328 | + * @return array all fetched entities |
|
| 329 | + * @since 7.0.0 |
|
| 330 | + */ |
|
| 331 | + protected function findEntities($sql, array $params=[], $limit=null, $offset=null) { |
|
| 332 | + $stmt = $this->execute($sql, $params, $limit, $offset); |
|
| 333 | + |
|
| 334 | + $entities = []; |
|
| 335 | + |
|
| 336 | + while($row = $stmt->fetch()){ |
|
| 337 | + $entities[] = $this->mapRowToEntity($row); |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + $stmt->closeCursor(); |
|
| 341 | + |
|
| 342 | + return $entities; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Returns an db result and throws exceptions when there are more or less |
|
| 348 | + * results |
|
| 349 | + * @param string $sql the sql query |
|
| 350 | + * @param array $params the parameters of the sql query |
|
| 351 | + * @param int $limit the maximum number of rows |
|
| 352 | + * @param int $offset from which row we want to start |
|
| 353 | + * @throws DoesNotExistException if the item does not exist |
|
| 354 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
| 355 | + * @return Entity the entity |
|
| 356 | + * @since 7.0.0 |
|
| 357 | + */ |
|
| 358 | + protected function findEntity($sql, array $params=[], $limit=null, $offset=null){ |
|
| 359 | + return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); |
|
| 360 | + } |
|
| 361 | 361 | |
| 362 | 362 | |
| 363 | 363 | } |
@@ -48,13 +48,13 @@ discard block |
||
| 48 | 48 | * mapped to queries without using sql |
| 49 | 49 | * @since 7.0.0 |
| 50 | 50 | */ |
| 51 | - public function __construct(IDBConnection $db, $tableName, $entityClass=null){ |
|
| 51 | + public function __construct(IDBConnection $db, $tableName, $entityClass = null) { |
|
| 52 | 52 | $this->db = $db; |
| 53 | - $this->tableName = '*PREFIX*' . $tableName; |
|
| 53 | + $this->tableName = '*PREFIX*'.$tableName; |
|
| 54 | 54 | |
| 55 | 55 | // if not given set the entity name to the class without the mapper part |
| 56 | 56 | // cache it here for later use since reflection is slow |
| 57 | - if($entityClass === null) { |
|
| 57 | + if ($entityClass === null) { |
|
| 58 | 58 | $this->entityClass = str_replace('Mapper', '', get_class($this)); |
| 59 | 59 | } else { |
| 60 | 60 | $this->entityClass = $entityClass; |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | * @return string the table name |
| 67 | 67 | * @since 7.0.0 |
| 68 | 68 | */ |
| 69 | - public function getTableName(){ |
|
| 69 | + public function getTableName() { |
|
| 70 | 70 | return $this->tableName; |
| 71 | 71 | } |
| 72 | 72 | |
@@ -77,8 +77,8 @@ discard block |
||
| 77 | 77 | * @return Entity the deleted entity |
| 78 | 78 | * @since 7.0.0 - return value added in 8.1.0 |
| 79 | 79 | */ |
| 80 | - public function delete(Entity $entity){ |
|
| 81 | - $sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?'; |
|
| 80 | + public function delete(Entity $entity) { |
|
| 81 | + $sql = 'DELETE FROM `'.$this->tableName.'` WHERE `id` = ?'; |
|
| 82 | 82 | $stmt = $this->execute($sql, [$entity->getId()]); |
| 83 | 83 | $stmt->closeCursor(); |
| 84 | 84 | return $entity; |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * @return Entity the saved entity with the set id |
| 92 | 92 | * @since 7.0.0 |
| 93 | 93 | */ |
| 94 | - public function insert(Entity $entity){ |
|
| 94 | + public function insert(Entity $entity) { |
|
| 95 | 95 | // get updated fields to save, fields have to be set using a setter to |
| 96 | 96 | // be saved |
| 97 | 97 | $properties = $entity->getUpdatedFields(); |
@@ -101,15 +101,15 @@ discard block |
||
| 101 | 101 | |
| 102 | 102 | // build the fields |
| 103 | 103 | $i = 0; |
| 104 | - foreach($properties as $property => $updated) { |
|
| 104 | + foreach ($properties as $property => $updated) { |
|
| 105 | 105 | $column = $entity->propertyToColumn($property); |
| 106 | - $getter = 'get' . ucfirst($property); |
|
| 106 | + $getter = 'get'.ucfirst($property); |
|
| 107 | 107 | |
| 108 | - $columns .= '`' . $column . '`'; |
|
| 108 | + $columns .= '`'.$column.'`'; |
|
| 109 | 109 | $values .= '?'; |
| 110 | 110 | |
| 111 | 111 | // only append colon if there are more entries |
| 112 | - if($i < count($properties)-1){ |
|
| 112 | + if ($i < count($properties) - 1) { |
|
| 113 | 113 | $columns .= ','; |
| 114 | 114 | $values .= ','; |
| 115 | 115 | } |
@@ -119,8 +119,8 @@ discard block |
||
| 119 | 119 | |
| 120 | 120 | } |
| 121 | 121 | |
| 122 | - $sql = 'INSERT INTO `' . $this->tableName . '`(' . |
|
| 123 | - $columns . ') VALUES(' . $values . ')'; |
|
| 122 | + $sql = 'INSERT INTO `'.$this->tableName.'`('. |
|
| 123 | + $columns.') VALUES('.$values.')'; |
|
| 124 | 124 | |
| 125 | 125 | $stmt = $this->execute($sql, $params); |
| 126 | 126 | |
@@ -140,16 +140,16 @@ discard block |
||
| 140 | 140 | * @return Entity the saved entity with the set id |
| 141 | 141 | * @since 7.0.0 - return value was added in 8.0.0 |
| 142 | 142 | */ |
| 143 | - public function update(Entity $entity){ |
|
| 143 | + public function update(Entity $entity) { |
|
| 144 | 144 | // if entity wasn't changed it makes no sense to run a db query |
| 145 | 145 | $properties = $entity->getUpdatedFields(); |
| 146 | - if(count($properties) === 0) { |
|
| 146 | + if (count($properties) === 0) { |
|
| 147 | 147 | return $entity; |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | // entity needs an id |
| 151 | 151 | $id = $entity->getId(); |
| 152 | - if($id === null){ |
|
| 152 | + if ($id === null) { |
|
| 153 | 153 | throw new \InvalidArgumentException( |
| 154 | 154 | 'Entity which should be updated has no id'); |
| 155 | 155 | } |
@@ -164,15 +164,15 @@ discard block |
||
| 164 | 164 | |
| 165 | 165 | // build the fields |
| 166 | 166 | $i = 0; |
| 167 | - foreach($properties as $property => $updated) { |
|
| 167 | + foreach ($properties as $property => $updated) { |
|
| 168 | 168 | |
| 169 | 169 | $column = $entity->propertyToColumn($property); |
| 170 | - $getter = 'get' . ucfirst($property); |
|
| 170 | + $getter = 'get'.ucfirst($property); |
|
| 171 | 171 | |
| 172 | - $columns .= '`' . $column . '` = ?'; |
|
| 172 | + $columns .= '`'.$column.'` = ?'; |
|
| 173 | 173 | |
| 174 | 174 | // only append colon if there are more entries |
| 175 | - if($i < count($properties)-1){ |
|
| 175 | + if ($i < count($properties) - 1) { |
|
| 176 | 176 | $columns .= ','; |
| 177 | 177 | } |
| 178 | 178 | |
@@ -180,8 +180,8 @@ discard block |
||
| 180 | 180 | $i++; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - $sql = 'UPDATE `' . $this->tableName . '` SET ' . |
|
| 184 | - $columns . ' WHERE `id` = ?'; |
|
| 183 | + $sql = 'UPDATE `'.$this->tableName.'` SET '. |
|
| 184 | + $columns.' WHERE `id` = ?'; |
|
| 185 | 185 | $params[] = $id; |
| 186 | 186 | |
| 187 | 187 | $stmt = $this->execute($sql, $params); |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | * @return \PDOStatement the database query result |
| 228 | 228 | * @since 7.0.0 |
| 229 | 229 | */ |
| 230 | - protected function execute($sql, array $params=[], $limit=null, $offset=null){ |
|
| 230 | + protected function execute($sql, array $params = [], $limit = null, $offset = null) { |
|
| 231 | 231 | $query = $this->db->prepare($sql, $limit, $offset); |
| 232 | 232 | |
| 233 | 233 | if ($this->isAssocArray($params)) { |
@@ -236,7 +236,7 @@ discard block |
||
| 236 | 236 | $query->bindValue($key, $param, $pdoConstant); |
| 237 | 237 | } |
| 238 | 238 | } else { |
| 239 | - $index = 1; // bindParam is 1 indexed |
|
| 239 | + $index = 1; // bindParam is 1 indexed |
|
| 240 | 240 | foreach ($params as $param) { |
| 241 | 241 | $pdoConstant = $this->getPDOType($param); |
| 242 | 242 | $query->bindValue($index, $param, $pdoConstant); |
@@ -263,11 +263,11 @@ discard block |
||
| 263 | 263 | * @return array the result as row |
| 264 | 264 | * @since 7.0.0 |
| 265 | 265 | */ |
| 266 | - protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null){ |
|
| 266 | + protected function findOneQuery($sql, array $params = [], $limit = null, $offset = null) { |
|
| 267 | 267 | $stmt = $this->execute($sql, $params, $limit, $offset); |
| 268 | 268 | $row = $stmt->fetch(); |
| 269 | 269 | |
| 270 | - if($row === false || $row === null){ |
|
| 270 | + if ($row === false || $row === null) { |
|
| 271 | 271 | $stmt->closeCursor(); |
| 272 | 272 | $msg = $this->buildDebugMessage( |
| 273 | 273 | 'Did expect one result but found none when executing', $sql, $params, $limit, $offset |
@@ -277,7 +277,7 @@ discard block |
||
| 277 | 277 | $row2 = $stmt->fetch(); |
| 278 | 278 | $stmt->closeCursor(); |
| 279 | 279 | //MDB2 returns null, PDO and doctrine false when no row is available |
| 280 | - if( ! ($row2 === false || $row2 === null )) { |
|
| 280 | + if (!($row2 === false || $row2 === null)) { |
|
| 281 | 281 | $msg = $this->buildDebugMessage( |
| 282 | 282 | 'Did not expect more than one result when executing', $sql, $params, $limit, $offset |
| 283 | 283 | ); |
@@ -298,12 +298,12 @@ discard block |
||
| 298 | 298 | * @return string formatted error message string |
| 299 | 299 | * @since 9.1.0 |
| 300 | 300 | */ |
| 301 | - private function buildDebugMessage($msg, $sql, array $params=[], $limit=null, $offset=null) { |
|
| 302 | - return $msg . |
|
| 303 | - ': query "' . $sql . '"; ' . |
|
| 304 | - 'parameters ' . print_r($params, true) . '; ' . |
|
| 305 | - 'limit "' . $limit . '"; '. |
|
| 306 | - 'offset "' . $offset . '"'; |
|
| 301 | + private function buildDebugMessage($msg, $sql, array $params = [], $limit = null, $offset = null) { |
|
| 302 | + return $msg. |
|
| 303 | + ': query "'.$sql.'"; '. |
|
| 304 | + 'parameters '.print_r($params, true).'; '. |
|
| 305 | + 'limit "'.$limit.'"; '. |
|
| 306 | + 'offset "'.$offset.'"'; |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | |
@@ -315,7 +315,7 @@ discard block |
||
| 315 | 315 | * @since 7.0.0 |
| 316 | 316 | */ |
| 317 | 317 | protected function mapRowToEntity($row) { |
| 318 | - return call_user_func($this->entityClass .'::fromRow', $row); |
|
| 318 | + return call_user_func($this->entityClass.'::fromRow', $row); |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | 321 | |
@@ -328,12 +328,12 @@ discard block |
||
| 328 | 328 | * @return array all fetched entities |
| 329 | 329 | * @since 7.0.0 |
| 330 | 330 | */ |
| 331 | - protected function findEntities($sql, array $params=[], $limit=null, $offset=null) { |
|
| 331 | + protected function findEntities($sql, array $params = [], $limit = null, $offset = null) { |
|
| 332 | 332 | $stmt = $this->execute($sql, $params, $limit, $offset); |
| 333 | 333 | |
| 334 | 334 | $entities = []; |
| 335 | 335 | |
| 336 | - while($row = $stmt->fetch()){ |
|
| 336 | + while ($row = $stmt->fetch()) { |
|
| 337 | 337 | $entities[] = $this->mapRowToEntity($row); |
| 338 | 338 | } |
| 339 | 339 | |
@@ -355,7 +355,7 @@ discard block |
||
| 355 | 355 | * @return Entity the entity |
| 356 | 356 | * @since 7.0.0 |
| 357 | 357 | */ |
| 358 | - protected function findEntity($sql, array $params=[], $limit=null, $offset=null){ |
|
| 358 | + protected function findEntity($sql, array $params = [], $limit = null, $offset = null) { |
|
| 359 | 359 | return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); |
| 360 | 360 | } |
| 361 | 361 | |
@@ -30,7 +30,6 @@ |
||
| 30 | 30 | * StorageAuthException constructor. |
| 31 | 31 | * |
| 32 | 32 | * @param string $message |
| 33 | - * @param int $code |
|
| 34 | 33 | * @param \Exception $previous |
| 35 | 34 | * @since 9.0.0 |
| 36 | 35 | */ |
@@ -27,16 +27,16 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class StorageAuthException extends StorageNotAvailableException { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * StorageAuthException constructor. |
|
| 32 | - * |
|
| 33 | - * @param string $message |
|
| 34 | - * @param int $code |
|
| 35 | - * @param \Exception $previous |
|
| 36 | - * @since 9.0.0 |
|
| 37 | - */ |
|
| 38 | - public function __construct($message = '', \Exception $previous = null) { |
|
| 39 | - $l = \OC::$server->getL10N('core'); |
|
| 40 | - parent::__construct($l->t('Storage unauthorized. %s', $message), self::STATUS_UNAUTHORIZED, $previous); |
|
| 41 | - } |
|
| 30 | + /** |
|
| 31 | + * StorageAuthException constructor. |
|
| 32 | + * |
|
| 33 | + * @param string $message |
|
| 34 | + * @param int $code |
|
| 35 | + * @param \Exception $previous |
|
| 36 | + * @since 9.0.0 |
|
| 37 | + */ |
|
| 38 | + public function __construct($message = '', \Exception $previous = null) { |
|
| 39 | + $l = \OC::$server->getL10N('core'); |
|
| 40 | + parent::__construct($l->t('Storage unauthorized. %s', $message), self::STATUS_UNAUTHORIZED, $previous); |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -30,7 +30,6 @@ |
||
| 30 | 30 | * ExtStorageBadConfigException constructor. |
| 31 | 31 | * |
| 32 | 32 | * @param string $message |
| 33 | - * @param int $code |
|
| 34 | 33 | * @param \Exception $previous |
| 35 | 34 | * @since 9.0.0 |
| 36 | 35 | */ |
@@ -27,17 +27,17 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | class StorageBadConfigException extends StorageNotAvailableException { |
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * ExtStorageBadConfigException constructor. |
|
| 32 | - * |
|
| 33 | - * @param string $message |
|
| 34 | - * @param int $code |
|
| 35 | - * @param \Exception $previous |
|
| 36 | - * @since 9.0.0 |
|
| 37 | - */ |
|
| 38 | - public function __construct($message = '', \Exception $previous = null) { |
|
| 39 | - $l = \OC::$server->getL10N('core'); |
|
| 40 | - parent::__construct($l->t('Storage incomplete configuration. %s', $message), self::STATUS_INCOMPLETE_CONF, $previous); |
|
| 41 | - } |
|
| 30 | + /** |
|
| 31 | + * ExtStorageBadConfigException constructor. |
|
| 32 | + * |
|
| 33 | + * @param string $message |
|
| 34 | + * @param int $code |
|
| 35 | + * @param \Exception $previous |
|
| 36 | + * @since 9.0.0 |
|
| 37 | + */ |
|
| 38 | + public function __construct($message = '', \Exception $previous = null) { |
|
| 39 | + $l = \OC::$server->getL10N('core'); |
|
| 40 | + parent::__construct($l->t('Storage incomplete configuration. %s', $message), self::STATUS_INCOMPLETE_CONF, $previous); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | 43 | } |