@@ -32,141 +32,141 @@ |
||
| 32 | 32 | use OCP\ILogger; |
| 33 | 33 | |
| 34 | 34 | class ChangesCheck { |
| 35 | - /** @var IClientService */ |
|
| 36 | - protected $clientService; |
|
| 37 | - /** @var ChangesMapper */ |
|
| 38 | - private $mapper; |
|
| 39 | - /** @var ILogger */ |
|
| 40 | - private $logger; |
|
| 41 | - |
|
| 42 | - const RESPONSE_NO_CONTENT = 0; |
|
| 43 | - const RESPONSE_USE_CACHE = 1; |
|
| 44 | - const RESPONSE_HAS_CONTENT = 2; |
|
| 45 | - |
|
| 46 | - public function __construct(IClientService $clientService, ChangesMapper $mapper, ILogger $logger) { |
|
| 47 | - $this->clientService = $clientService; |
|
| 48 | - $this->mapper = $mapper; |
|
| 49 | - $this->logger = $logger; |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @throws DoesNotExistException |
|
| 54 | - */ |
|
| 55 | - public function getChangesForVersion(string $version): array { |
|
| 56 | - $version = $this->normalizeVersion($version); |
|
| 57 | - $changesInfo = $this->mapper->getChanges($version); |
|
| 58 | - $changesData = json_decode($changesInfo->getData(), true); |
|
| 59 | - if(empty($changesData)) { |
|
| 60 | - throw new DoesNotExistException(); |
|
| 61 | - } |
|
| 62 | - return $changesData; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @throws \Exception |
|
| 67 | - */ |
|
| 68 | - public function check(string $uri, string $version): array { |
|
| 69 | - try { |
|
| 70 | - $version = $this->normalizeVersion($version); |
|
| 71 | - $changesInfo = $this->mapper->getChanges($version); |
|
| 72 | - if($changesInfo->getLastCheck() + 1800 > time()) { |
|
| 73 | - return json_decode($changesInfo->getData(), true); |
|
| 74 | - } |
|
| 75 | - } catch (DoesNotExistException $e) { |
|
| 76 | - $changesInfo = new ChangesResult(); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - $response = $this->queryChangesServer($uri, $changesInfo); |
|
| 80 | - |
|
| 81 | - switch($this->evaluateResponse($response)) { |
|
| 82 | - case self::RESPONSE_NO_CONTENT: |
|
| 83 | - return []; |
|
| 84 | - case self::RESPONSE_USE_CACHE: |
|
| 85 | - return json_decode($changesInfo->getData(), true); |
|
| 86 | - case self::RESPONSE_HAS_CONTENT: |
|
| 87 | - default: |
|
| 88 | - $data = $this->extractData($response->getBody()); |
|
| 89 | - $changesInfo->setData(json_encode($data)); |
|
| 90 | - $changesInfo->setEtag($response->getHeader('Etag')); |
|
| 91 | - $this->cacheResult($changesInfo, $version); |
|
| 92 | - |
|
| 93 | - return $data; |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - protected function evaluateResponse(IResponse $response): int { |
|
| 98 | - if($response->getStatusCode() === 304) { |
|
| 99 | - return self::RESPONSE_USE_CACHE; |
|
| 100 | - } else if($response->getStatusCode() === 404) { |
|
| 101 | - return self::RESPONSE_NO_CONTENT; |
|
| 102 | - } else if($response->getStatusCode() === 200) { |
|
| 103 | - return self::RESPONSE_HAS_CONTENT; |
|
| 104 | - } |
|
| 105 | - $this->logger->debug('Unexpected return code {code} from changelog server', [ |
|
| 106 | - 'app' => 'core', |
|
| 107 | - 'code' => $response->getStatusCode(), |
|
| 108 | - ]); |
|
| 109 | - return self::RESPONSE_NO_CONTENT; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - protected function cacheResult(ChangesResult $entry, string $version) { |
|
| 113 | - if($entry->getVersion() === $version) { |
|
| 114 | - $this->mapper->update($entry); |
|
| 115 | - } else { |
|
| 116 | - $entry->setVersion($version); |
|
| 117 | - $this->mapper->insert($entry); |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @throws \Exception |
|
| 123 | - */ |
|
| 124 | - protected function queryChangesServer(string $uri, ChangesResult $entry): IResponse { |
|
| 125 | - $headers = []; |
|
| 126 | - if($entry->getEtag() !== '') { |
|
| 127 | - $headers['If-None-Match'] = [$entry->getEtag()]; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - $entry->setLastCheck(time()); |
|
| 131 | - $client = $this->clientService->newClient(); |
|
| 132 | - return $client->get($uri, [ |
|
| 133 | - 'headers' => $headers, |
|
| 134 | - ]); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - protected function extractData($body):array { |
|
| 138 | - $data = []; |
|
| 139 | - if ($body) { |
|
| 140 | - $loadEntities = libxml_disable_entity_loader(true); |
|
| 141 | - $xml = @simplexml_load_string($body); |
|
| 142 | - libxml_disable_entity_loader($loadEntities); |
|
| 143 | - if ($xml !== false) { |
|
| 144 | - $data['changelogURL'] = (string)$xml->changelog['href']; |
|
| 145 | - $data['whatsNew'] = []; |
|
| 146 | - foreach($xml->whatsNew as $infoSet) { |
|
| 147 | - $data['whatsNew'][(string)$infoSet['lang']] = [ |
|
| 148 | - 'regular' => (array)$infoSet->regular->item, |
|
| 149 | - 'admin' => (array)$infoSet->admin->item, |
|
| 150 | - ]; |
|
| 151 | - } |
|
| 152 | - } else { |
|
| 153 | - libxml_clear_errors(); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - return $data; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * returns a x.y.z form of the provided version. Extra numbers will be |
|
| 161 | - * omitted, missing ones added as zeros. |
|
| 162 | - */ |
|
| 163 | - public function normalizeVersion(string $version): string { |
|
| 164 | - $versionNumbers = array_slice(explode('.', $version), 0, 3); |
|
| 165 | - $versionNumbers[0] = $versionNumbers[0] ?: '0'; // deal with empty input |
|
| 166 | - while(count($versionNumbers) < 3) { |
|
| 167 | - // changelog server expects x.y.z, pad 0 if it is too short |
|
| 168 | - $versionNumbers[] = 0; |
|
| 169 | - } |
|
| 170 | - return implode('.', $versionNumbers); |
|
| 171 | - } |
|
| 35 | + /** @var IClientService */ |
|
| 36 | + protected $clientService; |
|
| 37 | + /** @var ChangesMapper */ |
|
| 38 | + private $mapper; |
|
| 39 | + /** @var ILogger */ |
|
| 40 | + private $logger; |
|
| 41 | + |
|
| 42 | + const RESPONSE_NO_CONTENT = 0; |
|
| 43 | + const RESPONSE_USE_CACHE = 1; |
|
| 44 | + const RESPONSE_HAS_CONTENT = 2; |
|
| 45 | + |
|
| 46 | + public function __construct(IClientService $clientService, ChangesMapper $mapper, ILogger $logger) { |
|
| 47 | + $this->clientService = $clientService; |
|
| 48 | + $this->mapper = $mapper; |
|
| 49 | + $this->logger = $logger; |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @throws DoesNotExistException |
|
| 54 | + */ |
|
| 55 | + public function getChangesForVersion(string $version): array { |
|
| 56 | + $version = $this->normalizeVersion($version); |
|
| 57 | + $changesInfo = $this->mapper->getChanges($version); |
|
| 58 | + $changesData = json_decode($changesInfo->getData(), true); |
|
| 59 | + if(empty($changesData)) { |
|
| 60 | + throw new DoesNotExistException(); |
|
| 61 | + } |
|
| 62 | + return $changesData; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @throws \Exception |
|
| 67 | + */ |
|
| 68 | + public function check(string $uri, string $version): array { |
|
| 69 | + try { |
|
| 70 | + $version = $this->normalizeVersion($version); |
|
| 71 | + $changesInfo = $this->mapper->getChanges($version); |
|
| 72 | + if($changesInfo->getLastCheck() + 1800 > time()) { |
|
| 73 | + return json_decode($changesInfo->getData(), true); |
|
| 74 | + } |
|
| 75 | + } catch (DoesNotExistException $e) { |
|
| 76 | + $changesInfo = new ChangesResult(); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + $response = $this->queryChangesServer($uri, $changesInfo); |
|
| 80 | + |
|
| 81 | + switch($this->evaluateResponse($response)) { |
|
| 82 | + case self::RESPONSE_NO_CONTENT: |
|
| 83 | + return []; |
|
| 84 | + case self::RESPONSE_USE_CACHE: |
|
| 85 | + return json_decode($changesInfo->getData(), true); |
|
| 86 | + case self::RESPONSE_HAS_CONTENT: |
|
| 87 | + default: |
|
| 88 | + $data = $this->extractData($response->getBody()); |
|
| 89 | + $changesInfo->setData(json_encode($data)); |
|
| 90 | + $changesInfo->setEtag($response->getHeader('Etag')); |
|
| 91 | + $this->cacheResult($changesInfo, $version); |
|
| 92 | + |
|
| 93 | + return $data; |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + protected function evaluateResponse(IResponse $response): int { |
|
| 98 | + if($response->getStatusCode() === 304) { |
|
| 99 | + return self::RESPONSE_USE_CACHE; |
|
| 100 | + } else if($response->getStatusCode() === 404) { |
|
| 101 | + return self::RESPONSE_NO_CONTENT; |
|
| 102 | + } else if($response->getStatusCode() === 200) { |
|
| 103 | + return self::RESPONSE_HAS_CONTENT; |
|
| 104 | + } |
|
| 105 | + $this->logger->debug('Unexpected return code {code} from changelog server', [ |
|
| 106 | + 'app' => 'core', |
|
| 107 | + 'code' => $response->getStatusCode(), |
|
| 108 | + ]); |
|
| 109 | + return self::RESPONSE_NO_CONTENT; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + protected function cacheResult(ChangesResult $entry, string $version) { |
|
| 113 | + if($entry->getVersion() === $version) { |
|
| 114 | + $this->mapper->update($entry); |
|
| 115 | + } else { |
|
| 116 | + $entry->setVersion($version); |
|
| 117 | + $this->mapper->insert($entry); |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @throws \Exception |
|
| 123 | + */ |
|
| 124 | + protected function queryChangesServer(string $uri, ChangesResult $entry): IResponse { |
|
| 125 | + $headers = []; |
|
| 126 | + if($entry->getEtag() !== '') { |
|
| 127 | + $headers['If-None-Match'] = [$entry->getEtag()]; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + $entry->setLastCheck(time()); |
|
| 131 | + $client = $this->clientService->newClient(); |
|
| 132 | + return $client->get($uri, [ |
|
| 133 | + 'headers' => $headers, |
|
| 134 | + ]); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + protected function extractData($body):array { |
|
| 138 | + $data = []; |
|
| 139 | + if ($body) { |
|
| 140 | + $loadEntities = libxml_disable_entity_loader(true); |
|
| 141 | + $xml = @simplexml_load_string($body); |
|
| 142 | + libxml_disable_entity_loader($loadEntities); |
|
| 143 | + if ($xml !== false) { |
|
| 144 | + $data['changelogURL'] = (string)$xml->changelog['href']; |
|
| 145 | + $data['whatsNew'] = []; |
|
| 146 | + foreach($xml->whatsNew as $infoSet) { |
|
| 147 | + $data['whatsNew'][(string)$infoSet['lang']] = [ |
|
| 148 | + 'regular' => (array)$infoSet->regular->item, |
|
| 149 | + 'admin' => (array)$infoSet->admin->item, |
|
| 150 | + ]; |
|
| 151 | + } |
|
| 152 | + } else { |
|
| 153 | + libxml_clear_errors(); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + return $data; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * returns a x.y.z form of the provided version. Extra numbers will be |
|
| 161 | + * omitted, missing ones added as zeros. |
|
| 162 | + */ |
|
| 163 | + public function normalizeVersion(string $version): string { |
|
| 164 | + $versionNumbers = array_slice(explode('.', $version), 0, 3); |
|
| 165 | + $versionNumbers[0] = $versionNumbers[0] ?: '0'; // deal with empty input |
|
| 166 | + while(count($versionNumbers) < 3) { |
|
| 167 | + // changelog server expects x.y.z, pad 0 if it is too short |
|
| 168 | + $versionNumbers[] = 0; |
|
| 169 | + } |
|
| 170 | + return implode('.', $versionNumbers); |
|
| 171 | + } |
|
| 172 | 172 | } |
@@ -56,7 +56,7 @@ discard block |
||
| 56 | 56 | $version = $this->normalizeVersion($version); |
| 57 | 57 | $changesInfo = $this->mapper->getChanges($version); |
| 58 | 58 | $changesData = json_decode($changesInfo->getData(), true); |
| 59 | - if(empty($changesData)) { |
|
| 59 | + if (empty($changesData)) { |
|
| 60 | 60 | throw new DoesNotExistException(); |
| 61 | 61 | } |
| 62 | 62 | return $changesData; |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | try { |
| 70 | 70 | $version = $this->normalizeVersion($version); |
| 71 | 71 | $changesInfo = $this->mapper->getChanges($version); |
| 72 | - if($changesInfo->getLastCheck() + 1800 > time()) { |
|
| 72 | + if ($changesInfo->getLastCheck() + 1800 > time()) { |
|
| 73 | 73 | return json_decode($changesInfo->getData(), true); |
| 74 | 74 | } |
| 75 | 75 | } catch (DoesNotExistException $e) { |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | |
| 79 | 79 | $response = $this->queryChangesServer($uri, $changesInfo); |
| 80 | 80 | |
| 81 | - switch($this->evaluateResponse($response)) { |
|
| 81 | + switch ($this->evaluateResponse($response)) { |
|
| 82 | 82 | case self::RESPONSE_NO_CONTENT: |
| 83 | 83 | return []; |
| 84 | 84 | case self::RESPONSE_USE_CACHE: |
@@ -95,11 +95,11 @@ discard block |
||
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | protected function evaluateResponse(IResponse $response): int { |
| 98 | - if($response->getStatusCode() === 304) { |
|
| 98 | + if ($response->getStatusCode() === 304) { |
|
| 99 | 99 | return self::RESPONSE_USE_CACHE; |
| 100 | - } else if($response->getStatusCode() === 404) { |
|
| 100 | + } else if ($response->getStatusCode() === 404) { |
|
| 101 | 101 | return self::RESPONSE_NO_CONTENT; |
| 102 | - } else if($response->getStatusCode() === 200) { |
|
| 102 | + } else if ($response->getStatusCode() === 200) { |
|
| 103 | 103 | return self::RESPONSE_HAS_CONTENT; |
| 104 | 104 | } |
| 105 | 105 | $this->logger->debug('Unexpected return code {code} from changelog server', [ |
@@ -110,7 +110,7 @@ discard block |
||
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | protected function cacheResult(ChangesResult $entry, string $version) { |
| 113 | - if($entry->getVersion() === $version) { |
|
| 113 | + if ($entry->getVersion() === $version) { |
|
| 114 | 114 | $this->mapper->update($entry); |
| 115 | 115 | } else { |
| 116 | 116 | $entry->setVersion($version); |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | */ |
| 124 | 124 | protected function queryChangesServer(string $uri, ChangesResult $entry): IResponse { |
| 125 | 125 | $headers = []; |
| 126 | - if($entry->getEtag() !== '') { |
|
| 126 | + if ($entry->getEtag() !== '') { |
|
| 127 | 127 | $headers['If-None-Match'] = [$entry->getEtag()]; |
| 128 | 128 | } |
| 129 | 129 | |
@@ -141,12 +141,12 @@ discard block |
||
| 141 | 141 | $xml = @simplexml_load_string($body); |
| 142 | 142 | libxml_disable_entity_loader($loadEntities); |
| 143 | 143 | if ($xml !== false) { |
| 144 | - $data['changelogURL'] = (string)$xml->changelog['href']; |
|
| 144 | + $data['changelogURL'] = (string) $xml->changelog['href']; |
|
| 145 | 145 | $data['whatsNew'] = []; |
| 146 | - foreach($xml->whatsNew as $infoSet) { |
|
| 147 | - $data['whatsNew'][(string)$infoSet['lang']] = [ |
|
| 148 | - 'regular' => (array)$infoSet->regular->item, |
|
| 149 | - 'admin' => (array)$infoSet->admin->item, |
|
| 146 | + foreach ($xml->whatsNew as $infoSet) { |
|
| 147 | + $data['whatsNew'][(string) $infoSet['lang']] = [ |
|
| 148 | + 'regular' => (array) $infoSet->regular->item, |
|
| 149 | + 'admin' => (array) $infoSet->admin->item, |
|
| 150 | 150 | ]; |
| 151 | 151 | } |
| 152 | 152 | } else { |
@@ -163,7 +163,7 @@ discard block |
||
| 163 | 163 | public function normalizeVersion(string $version): string { |
| 164 | 164 | $versionNumbers = array_slice(explode('.', $version), 0, 3); |
| 165 | 165 | $versionNumbers[0] = $versionNumbers[0] ?: '0'; // deal with empty input |
| 166 | - while(count($versionNumbers) < 3) { |
|
| 166 | + while (count($versionNumbers) < 3) { |
|
| 167 | 167 | // changelog server expects x.y.z, pad 0 if it is too short |
| 168 | 168 | $versionNumbers[] = 0; |
| 169 | 169 | } |