@@ -30,128 +30,128 @@ |
||
| 30 | 30 | use OCP\ILogger; |
| 31 | 31 | |
| 32 | 32 | class ChangesCheck { |
| 33 | - /** @var IClientService */ |
|
| 34 | - protected $clientService; |
|
| 35 | - /** @var ChangesMapper */ |
|
| 36 | - private $mapper; |
|
| 37 | - /** @var ILogger */ |
|
| 38 | - private $logger; |
|
| 33 | + /** @var IClientService */ |
|
| 34 | + protected $clientService; |
|
| 35 | + /** @var ChangesMapper */ |
|
| 36 | + private $mapper; |
|
| 37 | + /** @var ILogger */ |
|
| 38 | + private $logger; |
|
| 39 | 39 | |
| 40 | - const RESPONSE_NO_CONTENT = 0; |
|
| 41 | - const RESPONSE_USE_CACHE = 1; |
|
| 42 | - const RESPONSE_HAS_CONTENT = 2; |
|
| 40 | + const RESPONSE_NO_CONTENT = 0; |
|
| 41 | + const RESPONSE_USE_CACHE = 1; |
|
| 42 | + const RESPONSE_HAS_CONTENT = 2; |
|
| 43 | 43 | |
| 44 | - public function __construct(IClientService $clientService, ChangesMapper $mapper, ILogger $logger) { |
|
| 45 | - $this->clientService = $clientService; |
|
| 46 | - $this->mapper = $mapper; |
|
| 47 | - $this->logger = $logger; |
|
| 48 | - } |
|
| 44 | + public function __construct(IClientService $clientService, ChangesMapper $mapper, ILogger $logger) { |
|
| 45 | + $this->clientService = $clientService; |
|
| 46 | + $this->mapper = $mapper; |
|
| 47 | + $this->logger = $logger; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @throws \Exception |
|
| 52 | - */ |
|
| 53 | - public function check(string $uri, string $version): array { |
|
| 54 | - try { |
|
| 55 | - $version = $this->normalizeVersion($version); |
|
| 56 | - $changesInfo = $this->mapper->getChanges($version); |
|
| 57 | - if($changesInfo->getLastCheck() + 1800 > time()) { |
|
| 58 | - return json_decode($changesInfo->getData(), true); |
|
| 59 | - } |
|
| 60 | - } catch (DoesNotExistException $e) { |
|
| 61 | - $changesInfo = new ChangesResult(); |
|
| 62 | - } |
|
| 50 | + /** |
|
| 51 | + * @throws \Exception |
|
| 52 | + */ |
|
| 53 | + public function check(string $uri, string $version): array { |
|
| 54 | + try { |
|
| 55 | + $version = $this->normalizeVersion($version); |
|
| 56 | + $changesInfo = $this->mapper->getChanges($version); |
|
| 57 | + if($changesInfo->getLastCheck() + 1800 > time()) { |
|
| 58 | + return json_decode($changesInfo->getData(), true); |
|
| 59 | + } |
|
| 60 | + } catch (DoesNotExistException $e) { |
|
| 61 | + $changesInfo = new ChangesResult(); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - $response = $this->queryChangesServer($uri, $changesInfo); |
|
| 64 | + $response = $this->queryChangesServer($uri, $changesInfo); |
|
| 65 | 65 | |
| 66 | - switch($this->evaluateResponse($response)) { |
|
| 67 | - case self::RESPONSE_NO_CONTENT: |
|
| 68 | - return []; |
|
| 69 | - case self::RESPONSE_USE_CACHE: |
|
| 70 | - return json_decode($changesInfo->getData(), true); |
|
| 71 | - case self::RESPONSE_HAS_CONTENT: |
|
| 72 | - default: |
|
| 73 | - $data = $this->extractData($response->getBody()); |
|
| 74 | - $changesInfo->setData(json_encode($data)); |
|
| 75 | - $changesInfo->setEtag($response->getHeader('Etag')); |
|
| 76 | - $this->cacheResult($changesInfo, $version); |
|
| 66 | + switch($this->evaluateResponse($response)) { |
|
| 67 | + case self::RESPONSE_NO_CONTENT: |
|
| 68 | + return []; |
|
| 69 | + case self::RESPONSE_USE_CACHE: |
|
| 70 | + return json_decode($changesInfo->getData(), true); |
|
| 71 | + case self::RESPONSE_HAS_CONTENT: |
|
| 72 | + default: |
|
| 73 | + $data = $this->extractData($response->getBody()); |
|
| 74 | + $changesInfo->setData(json_encode($data)); |
|
| 75 | + $changesInfo->setEtag($response->getHeader('Etag')); |
|
| 76 | + $this->cacheResult($changesInfo, $version); |
|
| 77 | 77 | |
| 78 | - return $data; |
|
| 79 | - } |
|
| 80 | - } |
|
| 78 | + return $data; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - protected function evaluateResponse(IResponse $response): int { |
|
| 83 | - if($response->getStatusCode() === 304) { |
|
| 84 | - return self::RESPONSE_USE_CACHE; |
|
| 85 | - } else if($response->getStatusCode() === 404) { |
|
| 86 | - return self::RESPONSE_NO_CONTENT; |
|
| 87 | - } else if($response->getStatusCode() === 200) { |
|
| 88 | - return self::RESPONSE_HAS_CONTENT; |
|
| 89 | - } |
|
| 90 | - $this->logger->debug('Unexpected return code {code} from changelog server', [ |
|
| 91 | - 'app' => 'core', |
|
| 92 | - 'code' => $response->getStatusCode(), |
|
| 93 | - ]); |
|
| 94 | - return self::RESPONSE_NO_CONTENT; |
|
| 95 | - } |
|
| 82 | + protected function evaluateResponse(IResponse $response): int { |
|
| 83 | + if($response->getStatusCode() === 304) { |
|
| 84 | + return self::RESPONSE_USE_CACHE; |
|
| 85 | + } else if($response->getStatusCode() === 404) { |
|
| 86 | + return self::RESPONSE_NO_CONTENT; |
|
| 87 | + } else if($response->getStatusCode() === 200) { |
|
| 88 | + return self::RESPONSE_HAS_CONTENT; |
|
| 89 | + } |
|
| 90 | + $this->logger->debug('Unexpected return code {code} from changelog server', [ |
|
| 91 | + 'app' => 'core', |
|
| 92 | + 'code' => $response->getStatusCode(), |
|
| 93 | + ]); |
|
| 94 | + return self::RESPONSE_NO_CONTENT; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - protected function cacheResult(ChangesResult $entry, string $version) { |
|
| 98 | - if($entry->getVersion() === $version) { |
|
| 99 | - $this->mapper->update($entry); |
|
| 100 | - } else { |
|
| 101 | - $entry->setVersion($version); |
|
| 102 | - $this->mapper->insert($entry); |
|
| 103 | - } |
|
| 104 | - } |
|
| 97 | + protected function cacheResult(ChangesResult $entry, string $version) { |
|
| 98 | + if($entry->getVersion() === $version) { |
|
| 99 | + $this->mapper->update($entry); |
|
| 100 | + } else { |
|
| 101 | + $entry->setVersion($version); |
|
| 102 | + $this->mapper->insert($entry); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * @throws \Exception |
|
| 108 | - */ |
|
| 109 | - protected function queryChangesServer(string $uri, ChangesResult $entry): IResponse { |
|
| 110 | - $headers = []; |
|
| 111 | - if($entry->getEtag() !== '') { |
|
| 112 | - $headers['If-None-Match'] = [$entry->getEtag()]; |
|
| 113 | - } |
|
| 106 | + /** |
|
| 107 | + * @throws \Exception |
|
| 108 | + */ |
|
| 109 | + protected function queryChangesServer(string $uri, ChangesResult $entry): IResponse { |
|
| 110 | + $headers = []; |
|
| 111 | + if($entry->getEtag() !== '') { |
|
| 112 | + $headers['If-None-Match'] = [$entry->getEtag()]; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - $entry->setLastCheck(time()); |
|
| 116 | - $client = $this->clientService->newClient(); |
|
| 117 | - return $client->get($uri, [ |
|
| 118 | - 'headers' => $headers, |
|
| 119 | - ]); |
|
| 120 | - } |
|
| 115 | + $entry->setLastCheck(time()); |
|
| 116 | + $client = $this->clientService->newClient(); |
|
| 117 | + return $client->get($uri, [ |
|
| 118 | + 'headers' => $headers, |
|
| 119 | + ]); |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - protected function extractData($body):array { |
|
| 123 | - $data = []; |
|
| 124 | - if ($body) { |
|
| 125 | - $loadEntities = libxml_disable_entity_loader(true); |
|
| 126 | - $xml = @simplexml_load_string($body); |
|
| 127 | - libxml_disable_entity_loader($loadEntities); |
|
| 128 | - if ($xml !== false) { |
|
| 129 | - $data['changelogURL'] = (string)$xml->changelog['href']; |
|
| 130 | - $data['whatsNew'] = []; |
|
| 131 | - foreach($xml->whatsNew as $infoSet) { |
|
| 132 | - $data['whatsNew'][(string)$infoSet['lang']] = [ |
|
| 133 | - 'regular' => (array)$infoSet->regular->item, |
|
| 134 | - 'admin' => (array)$infoSet->admin->item, |
|
| 135 | - ]; |
|
| 136 | - } |
|
| 137 | - } else { |
|
| 138 | - libxml_clear_errors(); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - return $data; |
|
| 142 | - } |
|
| 122 | + protected function extractData($body):array { |
|
| 123 | + $data = []; |
|
| 124 | + if ($body) { |
|
| 125 | + $loadEntities = libxml_disable_entity_loader(true); |
|
| 126 | + $xml = @simplexml_load_string($body); |
|
| 127 | + libxml_disable_entity_loader($loadEntities); |
|
| 128 | + if ($xml !== false) { |
|
| 129 | + $data['changelogURL'] = (string)$xml->changelog['href']; |
|
| 130 | + $data['whatsNew'] = []; |
|
| 131 | + foreach($xml->whatsNew as $infoSet) { |
|
| 132 | + $data['whatsNew'][(string)$infoSet['lang']] = [ |
|
| 133 | + 'regular' => (array)$infoSet->regular->item, |
|
| 134 | + 'admin' => (array)$infoSet->admin->item, |
|
| 135 | + ]; |
|
| 136 | + } |
|
| 137 | + } else { |
|
| 138 | + libxml_clear_errors(); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + return $data; |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * returns a x.y.z form of the provided version. Extra numbers will be |
|
| 146 | - * omitted, missing ones added as zeros. |
|
| 147 | - */ |
|
| 148 | - protected function normalizeVersion(string $version): string { |
|
| 149 | - $versionNumbers = array_slice(explode('.', $version), 0, 3); |
|
| 150 | - $versionNumbers[0] = $versionNumbers[0] ?: '0'; // deal with empty input |
|
| 151 | - while(count($versionNumbers) < 3) { |
|
| 152 | - // changelog server expects x.y.z, pad 0 if it is too short |
|
| 153 | - $versionNumbers[] = 0; |
|
| 154 | - } |
|
| 155 | - return implode('.', $versionNumbers); |
|
| 156 | - } |
|
| 144 | + /** |
|
| 145 | + * returns a x.y.z form of the provided version. Extra numbers will be |
|
| 146 | + * omitted, missing ones added as zeros. |
|
| 147 | + */ |
|
| 148 | + protected function normalizeVersion(string $version): string { |
|
| 149 | + $versionNumbers = array_slice(explode('.', $version), 0, 3); |
|
| 150 | + $versionNumbers[0] = $versionNumbers[0] ?: '0'; // deal with empty input |
|
| 151 | + while(count($versionNumbers) < 3) { |
|
| 152 | + // changelog server expects x.y.z, pad 0 if it is too short |
|
| 153 | + $versionNumbers[] = 0; |
|
| 154 | + } |
|
| 155 | + return implode('.', $versionNumbers); |
|
| 156 | + } |
|
| 157 | 157 | } |
@@ -30,28 +30,28 @@ |
||
| 30 | 30 | use OCP\IDBConnection; |
| 31 | 31 | |
| 32 | 32 | class ChangesMapper extends QBMapper { |
| 33 | - const TABLE_NAME = 'whats_new'; |
|
| 33 | + const TABLE_NAME = 'whats_new'; |
|
| 34 | 34 | |
| 35 | - public function __construct(IDBConnection $db) { |
|
| 36 | - parent::__construct($db, self::TABLE_NAME); |
|
| 37 | - } |
|
| 35 | + public function __construct(IDBConnection $db) { |
|
| 36 | + parent::__construct($db, self::TABLE_NAME); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @throws DoesNotExistException |
|
| 41 | - */ |
|
| 42 | - public function getChanges(string $version): ChangesResult { |
|
| 43 | - /* @var $qb IQueryBuilder */ |
|
| 44 | - $qb = $this->db->getQueryBuilder(); |
|
| 45 | - $result = $qb->select('*') |
|
| 46 | - ->from(self::TABLE_NAME) |
|
| 47 | - ->where($qb->expr()->eq('version', $qb->createNamedParameter($version))) |
|
| 48 | - ->execute(); |
|
| 39 | + /** |
|
| 40 | + * @throws DoesNotExistException |
|
| 41 | + */ |
|
| 42 | + public function getChanges(string $version): ChangesResult { |
|
| 43 | + /* @var $qb IQueryBuilder */ |
|
| 44 | + $qb = $this->db->getQueryBuilder(); |
|
| 45 | + $result = $qb->select('*') |
|
| 46 | + ->from(self::TABLE_NAME) |
|
| 47 | + ->where($qb->expr()->eq('version', $qb->createNamedParameter($version))) |
|
| 48 | + ->execute(); |
|
| 49 | 49 | |
| 50 | - $data = $result->fetch(); |
|
| 51 | - $result->closeCursor(); |
|
| 52 | - if ($data === false) { |
|
| 53 | - throw new DoesNotExistException('Changes info is not present'); |
|
| 54 | - } |
|
| 55 | - return ChangesResult::fromRow($data); |
|
| 56 | - } |
|
| 50 | + $data = $result->fetch(); |
|
| 51 | + $result->closeCursor(); |
|
| 52 | + if ($data === false) { |
|
| 53 | + throw new DoesNotExistException('Changes info is not present'); |
|
| 54 | + } |
|
| 55 | + return ChangesResult::fromRow($data); |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -31,95 +31,95 @@ |
||
| 31 | 31 | |
| 32 | 32 | class VersionCheck { |
| 33 | 33 | |
| 34 | - /** @var IClientService */ |
|
| 35 | - private $clientService; |
|
| 34 | + /** @var IClientService */ |
|
| 35 | + private $clientService; |
|
| 36 | 36 | |
| 37 | - /** @var IConfig */ |
|
| 38 | - private $config; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @param IClientService $clientService |
|
| 42 | - * @param IConfig $config |
|
| 43 | - */ |
|
| 44 | - public function __construct(IClientService $clientService, |
|
| 45 | - IConfig $config) { |
|
| 46 | - $this->clientService = $clientService; |
|
| 47 | - $this->config = $config; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Check if a new version is available |
|
| 53 | - * |
|
| 54 | - * @return array|bool |
|
| 55 | - */ |
|
| 56 | - public function check() { |
|
| 57 | - // Look up the cache - it is invalidated all 30 minutes |
|
| 58 | - if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) { |
|
| 59 | - return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - $updaterUrl = $this->config->getSystemValue('updater.server.url', 'https://updates.nextcloud.com/updater_server/'); |
|
| 63 | - |
|
| 64 | - $this->config->setAppValue('core', 'lastupdatedat', time()); |
|
| 65 | - |
|
| 66 | - if ($this->config->getAppValue('core', 'installedat', '') === '') { |
|
| 67 | - $this->config->setAppValue('core', 'installedat', microtime(true)); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $version = Util::getVersion(); |
|
| 71 | - $version['installed'] = $this->config->getAppValue('core', 'installedat'); |
|
| 72 | - $version['updated'] = $this->config->getAppValue('core', 'lastupdatedat'); |
|
| 73 | - $version['updatechannel'] = \OC_Util::getChannel(); |
|
| 74 | - $version['edition'] = ''; |
|
| 75 | - $version['build'] = \OC_Util::getBuild(); |
|
| 76 | - $version['php_major'] = PHP_MAJOR_VERSION; |
|
| 77 | - $version['php_minor'] = PHP_MINOR_VERSION; |
|
| 78 | - $version['php_release'] = PHP_RELEASE_VERSION; |
|
| 79 | - $versionString = implode('x', $version); |
|
| 80 | - |
|
| 81 | - //fetch xml data from updater |
|
| 82 | - $url = $updaterUrl . '?version=' . $versionString; |
|
| 83 | - |
|
| 84 | - $tmp = []; |
|
| 85 | - try { |
|
| 86 | - $xml = $this->getUrlContent($url); |
|
| 87 | - } catch (\Exception $e) { |
|
| 88 | - return false; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - if ($xml) { |
|
| 92 | - $loadEntities = libxml_disable_entity_loader(true); |
|
| 93 | - $data = @simplexml_load_string($xml); |
|
| 94 | - libxml_disable_entity_loader($loadEntities); |
|
| 95 | - if ($data !== false) { |
|
| 96 | - $tmp['version'] = (string)$data->version; |
|
| 97 | - $tmp['versionstring'] = (string)$data->versionstring; |
|
| 98 | - $tmp['url'] = (string)$data->url; |
|
| 99 | - $tmp['web'] = (string)$data->web; |
|
| 100 | - $tmp['changes'] = isset($data->changes) ? (string)$data->changes : ''; |
|
| 101 | - $tmp['autoupdater'] = (string)$data->autoupdater; |
|
| 102 | - $tmp['eol'] = isset($data->eol) ? (string)$data->eol : '0'; |
|
| 103 | - } else { |
|
| 104 | - libxml_clear_errors(); |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Cache the result |
|
| 109 | - $this->config->setAppValue('core', 'lastupdateResult', json_encode($tmp)); |
|
| 110 | - return $tmp; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @codeCoverageIgnore |
|
| 115 | - * @param string $url |
|
| 116 | - * @return resource|string |
|
| 117 | - * @throws \Exception |
|
| 118 | - */ |
|
| 119 | - protected function getUrlContent($url) { |
|
| 120 | - $client = $this->clientService->newClient(); |
|
| 121 | - $response = $client->get($url); |
|
| 122 | - return $response->getBody(); |
|
| 123 | - } |
|
| 37 | + /** @var IConfig */ |
|
| 38 | + private $config; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @param IClientService $clientService |
|
| 42 | + * @param IConfig $config |
|
| 43 | + */ |
|
| 44 | + public function __construct(IClientService $clientService, |
|
| 45 | + IConfig $config) { |
|
| 46 | + $this->clientService = $clientService; |
|
| 47 | + $this->config = $config; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Check if a new version is available |
|
| 53 | + * |
|
| 54 | + * @return array|bool |
|
| 55 | + */ |
|
| 56 | + public function check() { |
|
| 57 | + // Look up the cache - it is invalidated all 30 minutes |
|
| 58 | + if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) { |
|
| 59 | + return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + $updaterUrl = $this->config->getSystemValue('updater.server.url', 'https://updates.nextcloud.com/updater_server/'); |
|
| 63 | + |
|
| 64 | + $this->config->setAppValue('core', 'lastupdatedat', time()); |
|
| 65 | + |
|
| 66 | + if ($this->config->getAppValue('core', 'installedat', '') === '') { |
|
| 67 | + $this->config->setAppValue('core', 'installedat', microtime(true)); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $version = Util::getVersion(); |
|
| 71 | + $version['installed'] = $this->config->getAppValue('core', 'installedat'); |
|
| 72 | + $version['updated'] = $this->config->getAppValue('core', 'lastupdatedat'); |
|
| 73 | + $version['updatechannel'] = \OC_Util::getChannel(); |
|
| 74 | + $version['edition'] = ''; |
|
| 75 | + $version['build'] = \OC_Util::getBuild(); |
|
| 76 | + $version['php_major'] = PHP_MAJOR_VERSION; |
|
| 77 | + $version['php_minor'] = PHP_MINOR_VERSION; |
|
| 78 | + $version['php_release'] = PHP_RELEASE_VERSION; |
|
| 79 | + $versionString = implode('x', $version); |
|
| 80 | + |
|
| 81 | + //fetch xml data from updater |
|
| 82 | + $url = $updaterUrl . '?version=' . $versionString; |
|
| 83 | + |
|
| 84 | + $tmp = []; |
|
| 85 | + try { |
|
| 86 | + $xml = $this->getUrlContent($url); |
|
| 87 | + } catch (\Exception $e) { |
|
| 88 | + return false; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + if ($xml) { |
|
| 92 | + $loadEntities = libxml_disable_entity_loader(true); |
|
| 93 | + $data = @simplexml_load_string($xml); |
|
| 94 | + libxml_disable_entity_loader($loadEntities); |
|
| 95 | + if ($data !== false) { |
|
| 96 | + $tmp['version'] = (string)$data->version; |
|
| 97 | + $tmp['versionstring'] = (string)$data->versionstring; |
|
| 98 | + $tmp['url'] = (string)$data->url; |
|
| 99 | + $tmp['web'] = (string)$data->web; |
|
| 100 | + $tmp['changes'] = isset($data->changes) ? (string)$data->changes : ''; |
|
| 101 | + $tmp['autoupdater'] = (string)$data->autoupdater; |
|
| 102 | + $tmp['eol'] = isset($data->eol) ? (string)$data->eol : '0'; |
|
| 103 | + } else { |
|
| 104 | + libxml_clear_errors(); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Cache the result |
|
| 109 | + $this->config->setAppValue('core', 'lastupdateResult', json_encode($tmp)); |
|
| 110 | + return $tmp; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @codeCoverageIgnore |
|
| 115 | + * @param string $url |
|
| 116 | + * @return resource|string |
|
| 117 | + * @throws \Exception |
|
| 118 | + */ |
|
| 119 | + protected function getUrlContent($url) { |
|
| 120 | + $client = $this->clientService->newClient(); |
|
| 121 | + $response = $client->get($url); |
|
| 122 | + return $response->getBody(); |
|
| 123 | + } |
|
| 124 | 124 | } |
| 125 | 125 | |
@@ -40,22 +40,22 @@ |
||
| 40 | 40 | * @method void setData(string $data) |
| 41 | 41 | */ |
| 42 | 42 | class ChangesResult extends Entity { |
| 43 | - /** @var string */ |
|
| 44 | - protected $version = ''; |
|
| 43 | + /** @var string */ |
|
| 44 | + protected $version = ''; |
|
| 45 | 45 | |
| 46 | - /** @var string */ |
|
| 47 | - protected $etag = ''; |
|
| 46 | + /** @var string */ |
|
| 47 | + protected $etag = ''; |
|
| 48 | 48 | |
| 49 | - /** @var int */ |
|
| 50 | - protected $lastCheck = 0; |
|
| 49 | + /** @var int */ |
|
| 50 | + protected $lastCheck = 0; |
|
| 51 | 51 | |
| 52 | - /** @var string */ |
|
| 53 | - protected $data = ''; |
|
| 52 | + /** @var string */ |
|
| 53 | + protected $data = ''; |
|
| 54 | 54 | |
| 55 | - public function __construct() { |
|
| 56 | - $this->addType('version', 'string'); |
|
| 57 | - $this->addType('etag', 'string'); |
|
| 58 | - $this->addType('lastCheck', 'int'); |
|
| 59 | - $this->addType('data', 'string'); |
|
| 60 | - } |
|
| 55 | + public function __construct() { |
|
| 56 | + $this->addType('version', 'string'); |
|
| 57 | + $this->addType('etag', 'string'); |
|
| 58 | + $this->addType('lastCheck', 'int'); |
|
| 59 | + $this->addType('data', 'string'); |
|
| 60 | + } |
|
| 61 | 61 | } |
@@ -29,59 +29,59 @@ |
||
| 29 | 29 | use OC\Updater\VersionCheck; |
| 30 | 30 | |
| 31 | 31 | class UpdateChecker { |
| 32 | - /** @var VersionCheck */ |
|
| 33 | - private $updater; |
|
| 34 | - /** @var ChangesCheck */ |
|
| 35 | - private $changesCheck; |
|
| 32 | + /** @var VersionCheck */ |
|
| 33 | + private $updater; |
|
| 34 | + /** @var ChangesCheck */ |
|
| 35 | + private $changesCheck; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @param VersionCheck $updater |
|
| 39 | - */ |
|
| 40 | - public function __construct(VersionCheck $updater, ChangesCheck $changesCheck) { |
|
| 41 | - $this->updater = $updater; |
|
| 42 | - $this->changesCheck = $changesCheck; |
|
| 43 | - } |
|
| 37 | + /** |
|
| 38 | + * @param VersionCheck $updater |
|
| 39 | + */ |
|
| 40 | + public function __construct(VersionCheck $updater, ChangesCheck $changesCheck) { |
|
| 41 | + $this->updater = $updater; |
|
| 42 | + $this->changesCheck = $changesCheck; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @return array |
|
| 47 | - */ |
|
| 48 | - public function getUpdateState(): array { |
|
| 49 | - $data = $this->updater->check(); |
|
| 50 | - $result = []; |
|
| 45 | + /** |
|
| 46 | + * @return array |
|
| 47 | + */ |
|
| 48 | + public function getUpdateState(): array { |
|
| 49 | + $data = $this->updater->check(); |
|
| 50 | + $result = []; |
|
| 51 | 51 | |
| 52 | - if (isset($data['version']) && $data['version'] !== '' && $data['version'] !== []) { |
|
| 53 | - $result['updateAvailable'] = true; |
|
| 54 | - $result['updateVersion'] = $data['versionstring']; |
|
| 55 | - $result['updaterEnabled'] = $data['autoupdater'] === '1'; |
|
| 56 | - $result['versionIsEol'] = $data['eol'] === '1'; |
|
| 57 | - if (strpos($data['web'], 'https://') === 0) { |
|
| 58 | - $result['updateLink'] = $data['web']; |
|
| 59 | - } |
|
| 60 | - if (strpos($data['url'], 'https://') === 0) { |
|
| 61 | - $result['downloadLink'] = $data['url']; |
|
| 62 | - } |
|
| 63 | - if (strpos($data['changes'], 'https://') === 0) { |
|
| 64 | - try { |
|
| 65 | - $result['changes'] = $this->changesCheck->check($data['changes'], $data['version']); |
|
| 66 | - } catch (\Exception $e) { |
|
| 67 | - // no info, not a problem |
|
| 68 | - } |
|
| 69 | - } |
|
| 52 | + if (isset($data['version']) && $data['version'] !== '' && $data['version'] !== []) { |
|
| 53 | + $result['updateAvailable'] = true; |
|
| 54 | + $result['updateVersion'] = $data['versionstring']; |
|
| 55 | + $result['updaterEnabled'] = $data['autoupdater'] === '1'; |
|
| 56 | + $result['versionIsEol'] = $data['eol'] === '1'; |
|
| 57 | + if (strpos($data['web'], 'https://') === 0) { |
|
| 58 | + $result['updateLink'] = $data['web']; |
|
| 59 | + } |
|
| 60 | + if (strpos($data['url'], 'https://') === 0) { |
|
| 61 | + $result['downloadLink'] = $data['url']; |
|
| 62 | + } |
|
| 63 | + if (strpos($data['changes'], 'https://') === 0) { |
|
| 64 | + try { |
|
| 65 | + $result['changes'] = $this->changesCheck->check($data['changes'], $data['version']); |
|
| 66 | + } catch (\Exception $e) { |
|
| 67 | + // no info, not a problem |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - return $result; |
|
| 72 | - } |
|
| 71 | + return $result; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - return []; |
|
| 75 | - } |
|
| 74 | + return []; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * @param array $data |
|
| 79 | - */ |
|
| 80 | - public function populateJavaScriptVariables(array $data) { |
|
| 81 | - $data['array']['oc_updateState'] = json_encode([ |
|
| 82 | - 'updateAvailable' => true, |
|
| 83 | - 'updateVersion' => $this->getUpdateState()['updateVersion'], |
|
| 84 | - 'updateLink' => $this->getUpdateState()['updateLink'] ?? '', |
|
| 85 | - ]); |
|
| 86 | - } |
|
| 77 | + /** |
|
| 78 | + * @param array $data |
|
| 79 | + */ |
|
| 80 | + public function populateJavaScriptVariables(array $data) { |
|
| 81 | + $data['array']['oc_updateState'] = json_encode([ |
|
| 82 | + 'updateAvailable' => true, |
|
| 83 | + 'updateVersion' => $this->getUpdateState()['updateVersion'], |
|
| 84 | + 'updateLink' => $this->getUpdateState()['updateLink'] ?? '', |
|
| 85 | + ]); |
|
| 86 | + } |
|
| 87 | 87 | } |
@@ -37,159 +37,159 @@ |
||
| 37 | 37 | use OCP\Util; |
| 38 | 38 | |
| 39 | 39 | class Admin implements ISettings { |
| 40 | - /** @var IConfig */ |
|
| 41 | - private $config; |
|
| 42 | - /** @var UpdateChecker */ |
|
| 43 | - private $updateChecker; |
|
| 44 | - /** @var IGroupManager */ |
|
| 45 | - private $groupManager; |
|
| 46 | - /** @var IDateTimeFormatter */ |
|
| 47 | - private $dateTimeFormatter; |
|
| 48 | - /** @var IUserSession */ |
|
| 49 | - private $session; |
|
| 50 | - /** @var IFactory */ |
|
| 51 | - private $l10nFactory; |
|
| 52 | - |
|
| 53 | - public function __construct( |
|
| 54 | - IConfig $config, |
|
| 55 | - UpdateChecker $updateChecker, |
|
| 56 | - IGroupManager $groupManager, |
|
| 57 | - IDateTimeFormatter $dateTimeFormatter, |
|
| 58 | - IUserSession $session, |
|
| 59 | - IFactory $l10nFactory |
|
| 60 | - ) { |
|
| 61 | - $this->config = $config; |
|
| 62 | - $this->updateChecker = $updateChecker; |
|
| 63 | - $this->groupManager = $groupManager; |
|
| 64 | - $this->dateTimeFormatter = $dateTimeFormatter; |
|
| 65 | - $this->session = $session; |
|
| 66 | - $this->l10nFactory = $l10nFactory; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @return TemplateResponse |
|
| 71 | - */ |
|
| 72 | - public function getForm(): TemplateResponse { |
|
| 73 | - $lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat'); |
|
| 74 | - $lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp); |
|
| 75 | - |
|
| 76 | - $channels = [ |
|
| 77 | - 'daily', |
|
| 78 | - 'beta', |
|
| 79 | - 'stable', |
|
| 80 | - 'production', |
|
| 81 | - ]; |
|
| 82 | - $currentChannel = Util::getChannel(); |
|
| 83 | - if ($currentChannel === 'git') { |
|
| 84 | - $channels[] = 'git'; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $updateState = $this->updateChecker->getUpdateState(); |
|
| 88 | - |
|
| 89 | - $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true); |
|
| 90 | - |
|
| 91 | - $defaultUpdateServerURL = 'https://updates.nextcloud.com/updater_server/'; |
|
| 92 | - $updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL); |
|
| 93 | - |
|
| 94 | - $params = [ |
|
| 95 | - 'isNewVersionAvailable' => !empty($updateState['updateAvailable']), |
|
| 96 | - 'isUpdateChecked' => $lastUpdateCheckTimestamp > 0, |
|
| 97 | - 'lastChecked' => $lastUpdateCheck, |
|
| 98 | - 'currentChannel' => $currentChannel, |
|
| 99 | - 'channels' => $channels, |
|
| 100 | - 'newVersionString' => empty($updateState['updateVersion']) ? '' : $updateState['updateVersion'], |
|
| 101 | - 'downloadLink' => empty($updateState['downloadLink']) ? '' : $updateState['downloadLink'], |
|
| 102 | - 'changes' => $this->filterChanges($updateState['changes'] ?? []), |
|
| 103 | - 'updaterEnabled' => empty($updateState['updaterEnabled']) ? false : $updateState['updaterEnabled'], |
|
| 104 | - 'versionIsEol' => empty($updateState['versionIsEol']) ? false : $updateState['versionIsEol'], |
|
| 105 | - 'isDefaultUpdateServerURL' => $updateServerURL === $defaultUpdateServerURL, |
|
| 106 | - 'updateServerURL' => $updateServerURL, |
|
| 107 | - 'notifyGroups' => $this->getSelectedGroups($notifyGroups), |
|
| 108 | - ]; |
|
| 109 | - |
|
| 110 | - $params = [ |
|
| 111 | - 'json' => json_encode($params), |
|
| 112 | - ]; |
|
| 113 | - |
|
| 114 | - return new TemplateResponse('updatenotification', 'admin', $params, ''); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - protected function filterChanges(array $changes) { |
|
| 118 | - $filtered = []; |
|
| 119 | - if(isset($changes['changelogURL'])) { |
|
| 120 | - $filtered['changelogURL'] = $changes['changelogURL']; |
|
| 121 | - } |
|
| 122 | - if(!isset($changes['whatsNew'])) { |
|
| 123 | - return $filtered; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - $isFirstCall = true; |
|
| 127 | - do { |
|
| 128 | - $lang = $this->l10nFactory->iterateLanguage($isFirstCall); |
|
| 129 | - if($this->findWhatsNewTranslation($lang, $filtered, $changes['whatsNew'])) { |
|
| 130 | - return $filtered; |
|
| 131 | - } |
|
| 132 | - $isFirstCall = false; |
|
| 133 | - } while($lang !== 'en'); |
|
| 134 | - |
|
| 135 | - return $filtered; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - protected function getLangTrunk(string $lang):string { |
|
| 139 | - $pos = strpos($lang, '_'); |
|
| 140 | - if($pos !== false) { |
|
| 141 | - $lang = substr($lang, 0, $pos); |
|
| 142 | - } |
|
| 143 | - return $lang; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - protected function findWhatsNewTranslation(string $lang, array &$result, array $whatsNew): bool { |
|
| 147 | - if(isset($whatsNew[$lang])) { |
|
| 148 | - $result['whatsNew'] = $whatsNew[$lang]; |
|
| 149 | - return true; |
|
| 150 | - } |
|
| 151 | - $trunkedLang = $this->getLangTrunk($lang); |
|
| 152 | - if($trunkedLang !== $lang && isset($whatsNew[$trunkedLang])) { |
|
| 153 | - $result['whatsNew'] = $whatsNew[$trunkedLang]; |
|
| 154 | - return true; |
|
| 155 | - } |
|
| 156 | - return false; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @param array $groupIds |
|
| 161 | - * @return array |
|
| 162 | - */ |
|
| 163 | - protected function getSelectedGroups(array $groupIds): array { |
|
| 164 | - $result = []; |
|
| 165 | - foreach ($groupIds as $groupId) { |
|
| 166 | - $group = $this->groupManager->get($groupId); |
|
| 167 | - |
|
| 168 | - if ($group === null) { |
|
| 169 | - continue; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - $result[] = ['value' => $group->getGID(), 'label' => $group->getDisplayName()]; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - return $result; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @return string the section ID, e.g. 'sharing' |
|
| 180 | - */ |
|
| 181 | - public function getSection(): string { |
|
| 182 | - return 'overview'; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @return int whether the form should be rather on the top or bottom of |
|
| 187 | - * the admin section. The forms are arranged in ascending order of the |
|
| 188 | - * priority values. It is required to return a value between 0 and 100. |
|
| 189 | - * |
|
| 190 | - * E.g.: 70 |
|
| 191 | - */ |
|
| 192 | - public function getPriority(): int { |
|
| 193 | - return 11; |
|
| 194 | - } |
|
| 40 | + /** @var IConfig */ |
|
| 41 | + private $config; |
|
| 42 | + /** @var UpdateChecker */ |
|
| 43 | + private $updateChecker; |
|
| 44 | + /** @var IGroupManager */ |
|
| 45 | + private $groupManager; |
|
| 46 | + /** @var IDateTimeFormatter */ |
|
| 47 | + private $dateTimeFormatter; |
|
| 48 | + /** @var IUserSession */ |
|
| 49 | + private $session; |
|
| 50 | + /** @var IFactory */ |
|
| 51 | + private $l10nFactory; |
|
| 52 | + |
|
| 53 | + public function __construct( |
|
| 54 | + IConfig $config, |
|
| 55 | + UpdateChecker $updateChecker, |
|
| 56 | + IGroupManager $groupManager, |
|
| 57 | + IDateTimeFormatter $dateTimeFormatter, |
|
| 58 | + IUserSession $session, |
|
| 59 | + IFactory $l10nFactory |
|
| 60 | + ) { |
|
| 61 | + $this->config = $config; |
|
| 62 | + $this->updateChecker = $updateChecker; |
|
| 63 | + $this->groupManager = $groupManager; |
|
| 64 | + $this->dateTimeFormatter = $dateTimeFormatter; |
|
| 65 | + $this->session = $session; |
|
| 66 | + $this->l10nFactory = $l10nFactory; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @return TemplateResponse |
|
| 71 | + */ |
|
| 72 | + public function getForm(): TemplateResponse { |
|
| 73 | + $lastUpdateCheckTimestamp = $this->config->getAppValue('core', 'lastupdatedat'); |
|
| 74 | + $lastUpdateCheck = $this->dateTimeFormatter->formatDateTime($lastUpdateCheckTimestamp); |
|
| 75 | + |
|
| 76 | + $channels = [ |
|
| 77 | + 'daily', |
|
| 78 | + 'beta', |
|
| 79 | + 'stable', |
|
| 80 | + 'production', |
|
| 81 | + ]; |
|
| 82 | + $currentChannel = Util::getChannel(); |
|
| 83 | + if ($currentChannel === 'git') { |
|
| 84 | + $channels[] = 'git'; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $updateState = $this->updateChecker->getUpdateState(); |
|
| 88 | + |
|
| 89 | + $notifyGroups = json_decode($this->config->getAppValue('updatenotification', 'notify_groups', '["admin"]'), true); |
|
| 90 | + |
|
| 91 | + $defaultUpdateServerURL = 'https://updates.nextcloud.com/updater_server/'; |
|
| 92 | + $updateServerURL = $this->config->getSystemValue('updater.server.url', $defaultUpdateServerURL); |
|
| 93 | + |
|
| 94 | + $params = [ |
|
| 95 | + 'isNewVersionAvailable' => !empty($updateState['updateAvailable']), |
|
| 96 | + 'isUpdateChecked' => $lastUpdateCheckTimestamp > 0, |
|
| 97 | + 'lastChecked' => $lastUpdateCheck, |
|
| 98 | + 'currentChannel' => $currentChannel, |
|
| 99 | + 'channels' => $channels, |
|
| 100 | + 'newVersionString' => empty($updateState['updateVersion']) ? '' : $updateState['updateVersion'], |
|
| 101 | + 'downloadLink' => empty($updateState['downloadLink']) ? '' : $updateState['downloadLink'], |
|
| 102 | + 'changes' => $this->filterChanges($updateState['changes'] ?? []), |
|
| 103 | + 'updaterEnabled' => empty($updateState['updaterEnabled']) ? false : $updateState['updaterEnabled'], |
|
| 104 | + 'versionIsEol' => empty($updateState['versionIsEol']) ? false : $updateState['versionIsEol'], |
|
| 105 | + 'isDefaultUpdateServerURL' => $updateServerURL === $defaultUpdateServerURL, |
|
| 106 | + 'updateServerURL' => $updateServerURL, |
|
| 107 | + 'notifyGroups' => $this->getSelectedGroups($notifyGroups), |
|
| 108 | + ]; |
|
| 109 | + |
|
| 110 | + $params = [ |
|
| 111 | + 'json' => json_encode($params), |
|
| 112 | + ]; |
|
| 113 | + |
|
| 114 | + return new TemplateResponse('updatenotification', 'admin', $params, ''); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + protected function filterChanges(array $changes) { |
|
| 118 | + $filtered = []; |
|
| 119 | + if(isset($changes['changelogURL'])) { |
|
| 120 | + $filtered['changelogURL'] = $changes['changelogURL']; |
|
| 121 | + } |
|
| 122 | + if(!isset($changes['whatsNew'])) { |
|
| 123 | + return $filtered; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + $isFirstCall = true; |
|
| 127 | + do { |
|
| 128 | + $lang = $this->l10nFactory->iterateLanguage($isFirstCall); |
|
| 129 | + if($this->findWhatsNewTranslation($lang, $filtered, $changes['whatsNew'])) { |
|
| 130 | + return $filtered; |
|
| 131 | + } |
|
| 132 | + $isFirstCall = false; |
|
| 133 | + } while($lang !== 'en'); |
|
| 134 | + |
|
| 135 | + return $filtered; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + protected function getLangTrunk(string $lang):string { |
|
| 139 | + $pos = strpos($lang, '_'); |
|
| 140 | + if($pos !== false) { |
|
| 141 | + $lang = substr($lang, 0, $pos); |
|
| 142 | + } |
|
| 143 | + return $lang; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + protected function findWhatsNewTranslation(string $lang, array &$result, array $whatsNew): bool { |
|
| 147 | + if(isset($whatsNew[$lang])) { |
|
| 148 | + $result['whatsNew'] = $whatsNew[$lang]; |
|
| 149 | + return true; |
|
| 150 | + } |
|
| 151 | + $trunkedLang = $this->getLangTrunk($lang); |
|
| 152 | + if($trunkedLang !== $lang && isset($whatsNew[$trunkedLang])) { |
|
| 153 | + $result['whatsNew'] = $whatsNew[$trunkedLang]; |
|
| 154 | + return true; |
|
| 155 | + } |
|
| 156 | + return false; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @param array $groupIds |
|
| 161 | + * @return array |
|
| 162 | + */ |
|
| 163 | + protected function getSelectedGroups(array $groupIds): array { |
|
| 164 | + $result = []; |
|
| 165 | + foreach ($groupIds as $groupId) { |
|
| 166 | + $group = $this->groupManager->get($groupId); |
|
| 167 | + |
|
| 168 | + if ($group === null) { |
|
| 169 | + continue; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + $result[] = ['value' => $group->getGID(), 'label' => $group->getDisplayName()]; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + return $result; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @return string the section ID, e.g. 'sharing' |
|
| 180 | + */ |
|
| 181 | + public function getSection(): string { |
|
| 182 | + return 'overview'; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @return int whether the form should be rather on the top or bottom of |
|
| 187 | + * the admin section. The forms are arranged in ascending order of the |
|
| 188 | + * priority values. It is required to return a value between 0 and 100. |
|
| 189 | + * |
|
| 190 | + * E.g.: 70 |
|
| 191 | + */ |
|
| 192 | + public function getPriority(): int { |
|
| 193 | + return 11; |
|
| 194 | + } |
|
| 195 | 195 | } |
@@ -28,42 +28,42 @@ |
||
| 28 | 28 | use OCP\Migration\SimpleMigrationStep; |
| 29 | 29 | |
| 30 | 30 | class Version14000Date20180626223656 extends SimpleMigrationStep { |
| 31 | - public function changeSchema(\OCP\Migration\IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 32 | - /** @var ISchemaWrapper $schema */ |
|
| 33 | - $schema = $schemaClosure(); |
|
| 34 | - if(!$schema->hasTable('whats_new')) { |
|
| 35 | - $table = $schema->createTable('whats_new'); |
|
| 36 | - $table->addColumn('id', 'integer', [ |
|
| 37 | - 'autoincrement' => true, |
|
| 38 | - 'notnull' => true, |
|
| 39 | - 'length' => 4, |
|
| 40 | - 'unsigned' => true, |
|
| 41 | - ]); |
|
| 42 | - $table->addColumn('version', 'string', [ |
|
| 43 | - 'notnull' => true, |
|
| 44 | - 'length' => 64, |
|
| 45 | - 'default' => '11', |
|
| 46 | - ]); |
|
| 47 | - $table->addColumn('etag', 'string', [ |
|
| 48 | - 'notnull' => true, |
|
| 49 | - 'length' => 64, |
|
| 50 | - 'default' => '', |
|
| 51 | - ]); |
|
| 52 | - $table->addColumn('last_check', 'integer', [ |
|
| 53 | - 'notnull' => true, |
|
| 54 | - 'length' => 4, |
|
| 55 | - 'unsigned' => true, |
|
| 56 | - 'default' => 0, |
|
| 57 | - ]); |
|
| 58 | - $table->addColumn('data', 'text', [ |
|
| 59 | - 'notnull' => true, |
|
| 60 | - 'default' => '', |
|
| 61 | - ]); |
|
| 62 | - $table->setPrimaryKey(['id']); |
|
| 63 | - $table->addUniqueIndex(['version']); |
|
| 64 | - $table->addIndex(['version', 'etag'], 'version_etag_idx'); |
|
| 65 | - } |
|
| 31 | + public function changeSchema(\OCP\Migration\IOutput $output, \Closure $schemaClosure, array $options) { |
|
| 32 | + /** @var ISchemaWrapper $schema */ |
|
| 33 | + $schema = $schemaClosure(); |
|
| 34 | + if(!$schema->hasTable('whats_new')) { |
|
| 35 | + $table = $schema->createTable('whats_new'); |
|
| 36 | + $table->addColumn('id', 'integer', [ |
|
| 37 | + 'autoincrement' => true, |
|
| 38 | + 'notnull' => true, |
|
| 39 | + 'length' => 4, |
|
| 40 | + 'unsigned' => true, |
|
| 41 | + ]); |
|
| 42 | + $table->addColumn('version', 'string', [ |
|
| 43 | + 'notnull' => true, |
|
| 44 | + 'length' => 64, |
|
| 45 | + 'default' => '11', |
|
| 46 | + ]); |
|
| 47 | + $table->addColumn('etag', 'string', [ |
|
| 48 | + 'notnull' => true, |
|
| 49 | + 'length' => 64, |
|
| 50 | + 'default' => '', |
|
| 51 | + ]); |
|
| 52 | + $table->addColumn('last_check', 'integer', [ |
|
| 53 | + 'notnull' => true, |
|
| 54 | + 'length' => 4, |
|
| 55 | + 'unsigned' => true, |
|
| 56 | + 'default' => 0, |
|
| 57 | + ]); |
|
| 58 | + $table->addColumn('data', 'text', [ |
|
| 59 | + 'notnull' => true, |
|
| 60 | + 'default' => '', |
|
| 61 | + ]); |
|
| 62 | + $table->setPrimaryKey(['id']); |
|
| 63 | + $table->addUniqueIndex(['version']); |
|
| 64 | + $table->addIndex(['version', 'etag'], 'version_etag_idx'); |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - return $schema; |
|
| 68 | - } |
|
| 67 | + return $schema; |
|
| 68 | + } |
|
| 69 | 69 | } |
@@ -25,78 +25,78 @@ |
||
| 25 | 25 | * @since 8.2.0 |
| 26 | 26 | */ |
| 27 | 27 | interface IFactory { |
| 28 | - /** |
|
| 29 | - * Get a language instance |
|
| 30 | - * |
|
| 31 | - * @param string $app |
|
| 32 | - * @param string|null $lang |
|
| 33 | - * @return \OCP\IL10N |
|
| 34 | - * @since 8.2.0 |
|
| 35 | - */ |
|
| 36 | - public function get($app, $lang = null); |
|
| 28 | + /** |
|
| 29 | + * Get a language instance |
|
| 30 | + * |
|
| 31 | + * @param string $app |
|
| 32 | + * @param string|null $lang |
|
| 33 | + * @return \OCP\IL10N |
|
| 34 | + * @since 8.2.0 |
|
| 35 | + */ |
|
| 36 | + public function get($app, $lang = null); |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Find the best language |
|
| 40 | - * |
|
| 41 | - * @param string|null $app App id or null for core |
|
| 42 | - * @return string language If nothing works it returns 'en' |
|
| 43 | - * @since 9.0.0 |
|
| 44 | - */ |
|
| 45 | - public function findLanguage($app = null); |
|
| 38 | + /** |
|
| 39 | + * Find the best language |
|
| 40 | + * |
|
| 41 | + * @param string|null $app App id or null for core |
|
| 42 | + * @return string language If nothing works it returns 'en' |
|
| 43 | + * @since 9.0.0 |
|
| 44 | + */ |
|
| 45 | + public function findLanguage($app = null); |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * @param string|null $lang user language as default locale |
|
| 49 | - * @return string locale If nothing works it returns 'en_US' |
|
| 50 | - * @since 14.0.0 |
|
| 51 | - */ |
|
| 52 | - public function findLocale($lang = null); |
|
| 47 | + /** |
|
| 48 | + * @param string|null $lang user language as default locale |
|
| 49 | + * @return string locale If nothing works it returns 'en_US' |
|
| 50 | + * @since 14.0.0 |
|
| 51 | + */ |
|
| 52 | + public function findLocale($lang = null); |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Find all available languages for an app |
|
| 56 | - * |
|
| 57 | - * @param string|null $app App id or null for core |
|
| 58 | - * @return string[] an array of available languages |
|
| 59 | - * @since 9.0.0 |
|
| 60 | - */ |
|
| 61 | - public function findAvailableLanguages($app = null); |
|
| 54 | + /** |
|
| 55 | + * Find all available languages for an app |
|
| 56 | + * |
|
| 57 | + * @param string|null $app App id or null for core |
|
| 58 | + * @return string[] an array of available languages |
|
| 59 | + * @since 9.0.0 |
|
| 60 | + */ |
|
| 61 | + public function findAvailableLanguages($app = null); |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * @return array an array of available |
|
| 65 | - * @since 14.0.0 |
|
| 66 | - */ |
|
| 67 | - public function findAvailableLocales(); |
|
| 63 | + /** |
|
| 64 | + * @return array an array of available |
|
| 65 | + * @since 14.0.0 |
|
| 66 | + */ |
|
| 67 | + public function findAvailableLocales(); |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @param string|null $app App id or null for core |
|
| 71 | - * @param string $lang |
|
| 72 | - * @return bool |
|
| 73 | - * @since 9.0.0 |
|
| 74 | - */ |
|
| 75 | - public function languageExists($app, $lang); |
|
| 69 | + /** |
|
| 70 | + * @param string|null $app App id or null for core |
|
| 71 | + * @param string $lang |
|
| 72 | + * @return bool |
|
| 73 | + * @since 9.0.0 |
|
| 74 | + */ |
|
| 75 | + public function languageExists($app, $lang); |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * @param string $locale |
|
| 79 | - * @return bool |
|
| 80 | - * @since 14.0.0 |
|
| 81 | - */ |
|
| 82 | - public function localeExists($locale); |
|
| 77 | + /** |
|
| 78 | + * @param string $locale |
|
| 79 | + * @return bool |
|
| 80 | + * @since 14.0.0 |
|
| 81 | + */ |
|
| 82 | + public function localeExists($locale); |
|
| 83 | 83 | |
| 84 | - /** |
|
| 85 | - * Creates a function from the plural string |
|
| 86 | - * |
|
| 87 | - * @param string $string |
|
| 88 | - * @return string Unique function name |
|
| 89 | - * @since 14.0.0 |
|
| 90 | - */ |
|
| 91 | - public function createPluralFunction($string); |
|
| 84 | + /** |
|
| 85 | + * Creates a function from the plural string |
|
| 86 | + * |
|
| 87 | + * @param string $string |
|
| 88 | + * @return string Unique function name |
|
| 89 | + * @since 14.0.0 |
|
| 90 | + */ |
|
| 91 | + public function createPluralFunction($string); |
|
| 92 | 92 | |
| 93 | - /** |
|
| 94 | - * iterate through language settings (if provided) in this order: |
|
| 95 | - * 1. returns the forced language or: |
|
| 96 | - * 2. returns the user language or: |
|
| 97 | - * 3. returns the system default language or: |
|
| 98 | - * 4+∞. returns 'en' |
|
| 99 | - * @since 14.0.0 |
|
| 100 | - */ |
|
| 101 | - public function iterateLanguage(bool $reset = false): string; |
|
| 93 | + /** |
|
| 94 | + * iterate through language settings (if provided) in this order: |
|
| 95 | + * 1. returns the forced language or: |
|
| 96 | + * 2. returns the user language or: |
|
| 97 | + * 3. returns the system default language or: |
|
| 98 | + * 4+∞. returns 'en' |
|
| 99 | + * @since 14.0.0 |
|
| 100 | + */ |
|
| 101 | + public function iterateLanguage(bool $reset = false): string; |
|
| 102 | 102 | } |
@@ -41,604 +41,604 @@ |
||
| 41 | 41 | */ |
| 42 | 42 | class Factory implements IFactory { |
| 43 | 43 | |
| 44 | - /** @var string */ |
|
| 45 | - protected $requestLanguage = ''; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * cached instances |
|
| 49 | - * @var array Structure: Lang => App => \OCP\IL10N |
|
| 50 | - */ |
|
| 51 | - protected $instances = []; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var array Structure: App => string[] |
|
| 55 | - */ |
|
| 56 | - protected $availableLanguages = []; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @var array |
|
| 60 | - */ |
|
| 61 | - protected $availableLocales = []; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @var array Structure: string => callable |
|
| 65 | - */ |
|
| 66 | - protected $pluralFunctions = []; |
|
| 67 | - |
|
| 68 | - const COMMON_LANGUAGE_CODES = [ |
|
| 69 | - 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', |
|
| 70 | - 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' |
|
| 71 | - ]; |
|
| 72 | - |
|
| 73 | - /** @var IConfig */ |
|
| 74 | - protected $config; |
|
| 75 | - |
|
| 76 | - /** @var IRequest */ |
|
| 77 | - protected $request; |
|
| 78 | - |
|
| 79 | - /** @var IUserSession */ |
|
| 80 | - protected $userSession; |
|
| 81 | - |
|
| 82 | - /** @var string */ |
|
| 83 | - protected $serverRoot; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param IConfig $config |
|
| 87 | - * @param IRequest $request |
|
| 88 | - * @param IUserSession $userSession |
|
| 89 | - * @param string $serverRoot |
|
| 90 | - */ |
|
| 91 | - public function __construct(IConfig $config, |
|
| 92 | - IRequest $request, |
|
| 93 | - IUserSession $userSession, |
|
| 94 | - $serverRoot) { |
|
| 95 | - $this->config = $config; |
|
| 96 | - $this->request = $request; |
|
| 97 | - $this->userSession = $userSession; |
|
| 98 | - $this->serverRoot = $serverRoot; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Get a language instance |
|
| 103 | - * |
|
| 104 | - * @param string $app |
|
| 105 | - * @param string|null $lang |
|
| 106 | - * @param string|null $locale |
|
| 107 | - * @return \OCP\IL10N |
|
| 108 | - */ |
|
| 109 | - public function get($app, $lang = null, $locale = null) { |
|
| 110 | - $app = \OC_App::cleanAppId($app); |
|
| 111 | - if ($lang !== null) { |
|
| 112 | - $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $forceLang = $this->config->getSystemValue('force_language', false); |
|
| 116 | - if (is_string($forceLang)) { |
|
| 117 | - $lang = $forceLang; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $forceLocale = $this->config->getSystemValue('force_locale', false); |
|
| 121 | - if (is_string($forceLocale)) { |
|
| 122 | - $locale = $forceLocale; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - if ($lang === null || !$this->languageExists($app, $lang)) { |
|
| 126 | - $lang = $this->findLanguage($app); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - if ($locale === null || !$this->localeExists($locale)) { |
|
| 130 | - $locale = $this->findLocale($lang); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - if (!isset($this->instances[$lang][$app])) { |
|
| 134 | - $this->instances[$lang][$app] = new L10N( |
|
| 135 | - $this, $app, $lang, $locale, |
|
| 136 | - $this->getL10nFilesForApp($app, $lang) |
|
| 137 | - ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return $this->instances[$lang][$app]; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Find the best language |
|
| 145 | - * |
|
| 146 | - * @param string|null $app App id or null for core |
|
| 147 | - * @return string language If nothing works it returns 'en' |
|
| 148 | - */ |
|
| 149 | - public function findLanguage($app = null) { |
|
| 150 | - $forceLang = $this->config->getSystemValue('force_language', false); |
|
| 151 | - if (is_string($forceLang)) { |
|
| 152 | - $this->requestLanguage = $forceLang; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
|
| 156 | - return $this->requestLanguage; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * At this point Nextcloud might not yet be installed and thus the lookup |
|
| 161 | - * in the preferences table might fail. For this reason we need to check |
|
| 162 | - * whether the instance has already been installed |
|
| 163 | - * |
|
| 164 | - * @link https://github.com/owncloud/core/issues/21955 |
|
| 165 | - */ |
|
| 166 | - if ($this->config->getSystemValue('installed', false)) { |
|
| 167 | - $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 168 | - if (!is_null($userId)) { |
|
| 169 | - $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
|
| 170 | - } else { |
|
| 171 | - $userLang = null; |
|
| 172 | - } |
|
| 173 | - } else { |
|
| 174 | - $userId = null; |
|
| 175 | - $userLang = null; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if ($userLang) { |
|
| 179 | - $this->requestLanguage = $userLang; |
|
| 180 | - if ($this->languageExists($app, $userLang)) { |
|
| 181 | - return $userLang; |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - try { |
|
| 186 | - // Try to get the language from the Request |
|
| 187 | - $lang = $this->getLanguageFromRequest($app); |
|
| 188 | - if ($userId !== null && $app === null && !$userLang) { |
|
| 189 | - $this->config->setUserValue($userId, 'core', 'lang', $lang); |
|
| 190 | - } |
|
| 191 | - return $lang; |
|
| 192 | - } catch (LanguageNotFoundException $e) { |
|
| 193 | - // Finding language from request failed fall back to default language |
|
| 194 | - $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 195 | - if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
|
| 196 | - return $defaultLanguage; |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - // We could not find any language so fall back to english |
|
| 201 | - return 'en'; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * find the best locale |
|
| 206 | - * |
|
| 207 | - * @param string $lang |
|
| 208 | - * @return null|string |
|
| 209 | - */ |
|
| 210 | - public function findLocale($lang = null) { |
|
| 211 | - $forceLocale = $this->config->getSystemValue('force_locale', false); |
|
| 212 | - if (is_string($forceLocale) && $this->localeExists($forceLocale)) { |
|
| 213 | - return $forceLocale; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - if ($this->config->getSystemValue('installed', false)) { |
|
| 217 | - $userId = null !== $this->userSession->getUser() ? $this->userSession->getUser()->getUID() : null; |
|
| 218 | - $userLocale = null; |
|
| 219 | - if (null !== $userId) { |
|
| 220 | - $userLocale = $this->config->getUserValue($userId, 'core', 'locale', null); |
|
| 221 | - } |
|
| 222 | - } else { |
|
| 223 | - $userId = null; |
|
| 224 | - $userLocale = null; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - if ($userLocale && $this->localeExists($userLocale)) { |
|
| 228 | - return $userLocale; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - // Default : use system default locale |
|
| 232 | - $defaultLocale = $this->config->getSystemValue('default_locale', false); |
|
| 233 | - if ($defaultLocale !== false && $this->localeExists($defaultLocale)) { |
|
| 234 | - return $defaultLocale; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - // If no user locale set, use lang as locale |
|
| 238 | - if (null !== $lang && $this->localeExists($lang)) { |
|
| 239 | - return $lang; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - // At last, return USA |
|
| 243 | - return 'en_US'; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Find all available languages for an app |
|
| 248 | - * |
|
| 249 | - * @param string|null $app App id or null for core |
|
| 250 | - * @return array an array of available languages |
|
| 251 | - */ |
|
| 252 | - public function findAvailableLanguages($app = null) { |
|
| 253 | - $key = $app; |
|
| 254 | - if ($key === null) { |
|
| 255 | - $key = 'null'; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - // also works with null as key |
|
| 259 | - if (!empty($this->availableLanguages[$key])) { |
|
| 260 | - return $this->availableLanguages[$key]; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - $available = ['en']; //english is always available |
|
| 264 | - $dir = $this->findL10nDir($app); |
|
| 265 | - if (is_dir($dir)) { |
|
| 266 | - $files = scandir($dir); |
|
| 267 | - if ($files !== false) { |
|
| 268 | - foreach ($files as $file) { |
|
| 269 | - if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 270 | - $available[] = substr($file, 0, -5); |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - // merge with translations from theme |
|
| 277 | - $theme = $this->config->getSystemValue('theme'); |
|
| 278 | - if (!empty($theme)) { |
|
| 279 | - $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 280 | - |
|
| 281 | - if (is_dir($themeDir)) { |
|
| 282 | - $files = scandir($themeDir); |
|
| 283 | - if ($files !== false) { |
|
| 284 | - foreach ($files as $file) { |
|
| 285 | - if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 286 | - $available[] = substr($file, 0, -5); |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - $this->availableLanguages[$key] = $available; |
|
| 294 | - return $available; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** |
|
| 298 | - * @return array|mixed |
|
| 299 | - */ |
|
| 300 | - public function findAvailableLocales() { |
|
| 301 | - if (!empty($this->availableLocales)) { |
|
| 302 | - return $this->availableLocales; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - $localeData = file_get_contents(\OC::$SERVERROOT . '/resources/locales.json'); |
|
| 306 | - $this->availableLocales = \json_decode($localeData, true); |
|
| 307 | - |
|
| 308 | - return $this->availableLocales; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * @param string|null $app App id or null for core |
|
| 313 | - * @param string $lang |
|
| 314 | - * @return bool |
|
| 315 | - */ |
|
| 316 | - public function languageExists($app, $lang) { |
|
| 317 | - if ($lang === 'en') {//english is always available |
|
| 318 | - return true; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - $languages = $this->findAvailableLanguages($app); |
|
| 322 | - return array_search($lang, $languages) !== false; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - public function iterateLanguage(bool $reset = false): string { |
|
| 326 | - static $i = 0; |
|
| 327 | - if($reset) { |
|
| 328 | - $i = 0; |
|
| 329 | - } |
|
| 330 | - switch($i) { |
|
| 331 | - /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 332 | - case 0: |
|
| 333 | - $i++; |
|
| 334 | - $forcedLang = $this->config->getSystemValue('force_language', false); |
|
| 335 | - if(is_string($forcedLang)) { |
|
| 336 | - return $forcedLang; |
|
| 337 | - } |
|
| 338 | - /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 339 | - case 1: |
|
| 340 | - $i++; |
|
| 341 | - $user = $this->userSession->getUser(); |
|
| 342 | - if($user instanceof IUser) { |
|
| 343 | - $userLang = $this->config->getUserValue($user->getUID(), 'core', 'lang', null); |
|
| 344 | - if(is_string($userLang)) { |
|
| 345 | - return $userLang; |
|
| 346 | - } |
|
| 347 | - } |
|
| 348 | - case 2: |
|
| 349 | - $i++; |
|
| 350 | - return $this->config->getSystemValue('default_language', 'en'); |
|
| 351 | - default: |
|
| 352 | - return 'en'; |
|
| 353 | - } |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * @param string $locale |
|
| 358 | - * @return bool |
|
| 359 | - */ |
|
| 360 | - public function localeExists($locale) { |
|
| 361 | - if ($locale === 'en') { //english is always available |
|
| 362 | - return true; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - $locales = $this->findAvailableLocales(); |
|
| 366 | - $userLocale = array_filter($locales, function($value) use ($locale) { |
|
| 367 | - return $locale === $value['code']; |
|
| 368 | - }); |
|
| 369 | - |
|
| 370 | - return !empty($userLocale); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * @param string|null $app |
|
| 375 | - * @return string |
|
| 376 | - * @throws LanguageNotFoundException |
|
| 377 | - */ |
|
| 378 | - private function getLanguageFromRequest($app) { |
|
| 379 | - $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
|
| 380 | - if ($header !== '') { |
|
| 381 | - $available = $this->findAvailableLanguages($app); |
|
| 382 | - |
|
| 383 | - // E.g. make sure that 'de' is before 'de_DE'. |
|
| 384 | - sort($available); |
|
| 385 | - |
|
| 386 | - $preferences = preg_split('/,\s*/', strtolower($header)); |
|
| 387 | - foreach ($preferences as $preference) { |
|
| 388 | - list($preferred_language) = explode(';', $preference); |
|
| 389 | - $preferred_language = str_replace('-', '_', $preferred_language); |
|
| 390 | - |
|
| 391 | - foreach ($available as $available_language) { |
|
| 392 | - if ($preferred_language === strtolower($available_language)) { |
|
| 393 | - return $this->respectDefaultLanguage($app, $available_language); |
|
| 394 | - } |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - // Fallback from de_De to de |
|
| 398 | - foreach ($available as $available_language) { |
|
| 399 | - if (substr($preferred_language, 0, 2) === $available_language) { |
|
| 400 | - return $available_language; |
|
| 401 | - } |
|
| 402 | - } |
|
| 403 | - } |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - throw new LanguageNotFoundException(); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - /** |
|
| 410 | - * if default language is set to de_DE (formal German) this should be |
|
| 411 | - * preferred to 'de' (non-formal German) if possible |
|
| 412 | - * |
|
| 413 | - * @param string|null $app |
|
| 414 | - * @param string $lang |
|
| 415 | - * @return string |
|
| 416 | - */ |
|
| 417 | - protected function respectDefaultLanguage($app, $lang) { |
|
| 418 | - $result = $lang; |
|
| 419 | - $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 420 | - |
|
| 421 | - // use formal version of german ("Sie" instead of "Du") if the default |
|
| 422 | - // language is set to 'de_DE' if possible |
|
| 423 | - if (is_string($defaultLanguage) && |
|
| 424 | - strtolower($lang) === 'de' && |
|
| 425 | - strtolower($defaultLanguage) === 'de_de' && |
|
| 426 | - $this->languageExists($app, 'de_DE') |
|
| 427 | - ) { |
|
| 428 | - $result = 'de_DE'; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - return $result; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * Checks if $sub is a subdirectory of $parent |
|
| 436 | - * |
|
| 437 | - * @param string $sub |
|
| 438 | - * @param string $parent |
|
| 439 | - * @return bool |
|
| 440 | - */ |
|
| 441 | - private function isSubDirectory($sub, $parent) { |
|
| 442 | - // Check whether $sub contains no ".." |
|
| 443 | - if (strpos($sub, '..') !== false) { |
|
| 444 | - return false; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - // Check whether $sub is a subdirectory of $parent |
|
| 448 | - if (strpos($sub, $parent) === 0) { |
|
| 449 | - return true; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - return false; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Get a list of language files that should be loaded |
|
| 457 | - * |
|
| 458 | - * @param string $app |
|
| 459 | - * @param string $lang |
|
| 460 | - * @return string[] |
|
| 461 | - */ |
|
| 462 | - // FIXME This method is only public, until OC_L10N does not need it anymore, |
|
| 463 | - // FIXME This is also the reason, why it is not in the public interface |
|
| 464 | - public function getL10nFilesForApp($app, $lang) { |
|
| 465 | - $languageFiles = []; |
|
| 466 | - |
|
| 467 | - $i18nDir = $this->findL10nDir($app); |
|
| 468 | - $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 469 | - |
|
| 470 | - if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 471 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 472 | - || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 473 | - || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 474 | - ) |
|
| 475 | - && file_exists($transFile)) { |
|
| 476 | - // load the translations file |
|
| 477 | - $languageFiles[] = $transFile; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - // merge with translations from theme |
|
| 481 | - $theme = $this->config->getSystemValue('theme'); |
|
| 482 | - if (!empty($theme)) { |
|
| 483 | - $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 484 | - if (file_exists($transFile)) { |
|
| 485 | - $languageFiles[] = $transFile; |
|
| 486 | - } |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - return $languageFiles; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * find the l10n directory |
|
| 494 | - * |
|
| 495 | - * @param string $app App id or empty string for core |
|
| 496 | - * @return string directory |
|
| 497 | - */ |
|
| 498 | - protected function findL10nDir($app = null) { |
|
| 499 | - if (in_array($app, ['core', 'lib', 'settings'])) { |
|
| 500 | - if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 501 | - return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 502 | - } |
|
| 503 | - } else if ($app && \OC_App::getAppPath($app) !== false) { |
|
| 504 | - // Check if the app is in the app folder |
|
| 505 | - return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 506 | - } |
|
| 507 | - return $this->serverRoot . '/core/l10n/'; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * Creates a function from the plural string |
|
| 513 | - * |
|
| 514 | - * Parts of the code is copied from Habari: |
|
| 515 | - * https://github.com/habari/system/blob/master/classes/locale.php |
|
| 516 | - * @param string $string |
|
| 517 | - * @return string |
|
| 518 | - */ |
|
| 519 | - public function createPluralFunction($string) { |
|
| 520 | - if (isset($this->pluralFunctions[$string])) { |
|
| 521 | - return $this->pluralFunctions[$string]; |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 525 | - // sanitize |
|
| 526 | - $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 527 | - $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 528 | - |
|
| 529 | - $body = str_replace( |
|
| 530 | - array( 'plural', 'n', '$n$plurals', ), |
|
| 531 | - array( '$plural', '$n', '$nplurals', ), |
|
| 532 | - 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 533 | - ); |
|
| 534 | - |
|
| 535 | - // add parents |
|
| 536 | - // important since PHP's ternary evaluates from left to right |
|
| 537 | - $body .= ';'; |
|
| 538 | - $res = ''; |
|
| 539 | - $p = 0; |
|
| 540 | - $length = strlen($body); |
|
| 541 | - for($i = 0; $i < $length; $i++) { |
|
| 542 | - $ch = $body[$i]; |
|
| 543 | - switch ( $ch ) { |
|
| 544 | - case '?': |
|
| 545 | - $res .= ' ? ('; |
|
| 546 | - $p++; |
|
| 547 | - break; |
|
| 548 | - case ':': |
|
| 549 | - $res .= ') : ('; |
|
| 550 | - break; |
|
| 551 | - case ';': |
|
| 552 | - $res .= str_repeat( ')', $p ) . ';'; |
|
| 553 | - $p = 0; |
|
| 554 | - break; |
|
| 555 | - default: |
|
| 556 | - $res .= $ch; |
|
| 557 | - } |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 561 | - $function = create_function('$n', $body); |
|
| 562 | - $this->pluralFunctions[$string] = $function; |
|
| 563 | - return $function; |
|
| 564 | - } else { |
|
| 565 | - // default: one plural form for all cases but n==1 (english) |
|
| 566 | - $function = create_function( |
|
| 567 | - '$n', |
|
| 568 | - '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
| 569 | - ); |
|
| 570 | - $this->pluralFunctions[$string] = $function; |
|
| 571 | - return $function; |
|
| 572 | - } |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - /** |
|
| 576 | - * returns the common language and other languages in an |
|
| 577 | - * associative array |
|
| 578 | - * |
|
| 579 | - * @return array |
|
| 580 | - */ |
|
| 581 | - public function getLanguages() { |
|
| 582 | - $forceLanguage = $this->config->getSystemValue('force_language', false); |
|
| 583 | - if ($forceLanguage !== false) { |
|
| 584 | - return []; |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - $languageCodes = $this->findAvailableLanguages(); |
|
| 588 | - |
|
| 589 | - $commonLanguages = []; |
|
| 590 | - $languages = []; |
|
| 591 | - |
|
| 592 | - foreach($languageCodes as $lang) { |
|
| 593 | - $l = $this->get('lib', $lang); |
|
| 594 | - // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version |
|
| 595 | - $potentialName = (string) $l->t('__language_name__'); |
|
| 596 | - if ($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file |
|
| 597 | - $ln = array( |
|
| 598 | - 'code' => $lang, |
|
| 599 | - 'name' => $potentialName |
|
| 600 | - ); |
|
| 601 | - } else if ($lang === 'en') { |
|
| 602 | - $ln = array( |
|
| 603 | - 'code' => $lang, |
|
| 604 | - 'name' => 'English (US)' |
|
| 605 | - ); |
|
| 606 | - } else {//fallback to language code |
|
| 607 | - $ln = array( |
|
| 608 | - 'code' => $lang, |
|
| 609 | - 'name' => $lang |
|
| 610 | - ); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - // put appropriate languages into appropriate arrays, to print them sorted |
|
| 614 | - // common languages -> divider -> other languages |
|
| 615 | - if (in_array($lang, self::COMMON_LANGUAGE_CODES)) { |
|
| 616 | - $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)] = $ln; |
|
| 617 | - } else { |
|
| 618 | - $languages[] = $ln; |
|
| 619 | - } |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - ksort($commonLanguages); |
|
| 623 | - |
|
| 624 | - // sort now by displayed language not the iso-code |
|
| 625 | - usort( $languages, function ($a, $b) { |
|
| 626 | - if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { |
|
| 627 | - // If a doesn't have a name, but b does, list b before a |
|
| 628 | - return 1; |
|
| 629 | - } |
|
| 630 | - if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) { |
|
| 631 | - // If a does have a name, but b doesn't, list a before b |
|
| 632 | - return -1; |
|
| 633 | - } |
|
| 634 | - // Otherwise compare the names |
|
| 635 | - return strcmp($a['name'], $b['name']); |
|
| 636 | - }); |
|
| 637 | - |
|
| 638 | - return [ |
|
| 639 | - // reset indexes |
|
| 640 | - 'commonlanguages' => array_values($commonLanguages), |
|
| 641 | - 'languages' => $languages |
|
| 642 | - ]; |
|
| 643 | - } |
|
| 44 | + /** @var string */ |
|
| 45 | + protected $requestLanguage = ''; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * cached instances |
|
| 49 | + * @var array Structure: Lang => App => \OCP\IL10N |
|
| 50 | + */ |
|
| 51 | + protected $instances = []; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var array Structure: App => string[] |
|
| 55 | + */ |
|
| 56 | + protected $availableLanguages = []; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @var array |
|
| 60 | + */ |
|
| 61 | + protected $availableLocales = []; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @var array Structure: string => callable |
|
| 65 | + */ |
|
| 66 | + protected $pluralFunctions = []; |
|
| 67 | + |
|
| 68 | + const COMMON_LANGUAGE_CODES = [ |
|
| 69 | + 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', |
|
| 70 | + 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' |
|
| 71 | + ]; |
|
| 72 | + |
|
| 73 | + /** @var IConfig */ |
|
| 74 | + protected $config; |
|
| 75 | + |
|
| 76 | + /** @var IRequest */ |
|
| 77 | + protected $request; |
|
| 78 | + |
|
| 79 | + /** @var IUserSession */ |
|
| 80 | + protected $userSession; |
|
| 81 | + |
|
| 82 | + /** @var string */ |
|
| 83 | + protected $serverRoot; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param IConfig $config |
|
| 87 | + * @param IRequest $request |
|
| 88 | + * @param IUserSession $userSession |
|
| 89 | + * @param string $serverRoot |
|
| 90 | + */ |
|
| 91 | + public function __construct(IConfig $config, |
|
| 92 | + IRequest $request, |
|
| 93 | + IUserSession $userSession, |
|
| 94 | + $serverRoot) { |
|
| 95 | + $this->config = $config; |
|
| 96 | + $this->request = $request; |
|
| 97 | + $this->userSession = $userSession; |
|
| 98 | + $this->serverRoot = $serverRoot; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Get a language instance |
|
| 103 | + * |
|
| 104 | + * @param string $app |
|
| 105 | + * @param string|null $lang |
|
| 106 | + * @param string|null $locale |
|
| 107 | + * @return \OCP\IL10N |
|
| 108 | + */ |
|
| 109 | + public function get($app, $lang = null, $locale = null) { |
|
| 110 | + $app = \OC_App::cleanAppId($app); |
|
| 111 | + if ($lang !== null) { |
|
| 112 | + $lang = str_replace(array('\0', '/', '\\', '..'), '', (string) $lang); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $forceLang = $this->config->getSystemValue('force_language', false); |
|
| 116 | + if (is_string($forceLang)) { |
|
| 117 | + $lang = $forceLang; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $forceLocale = $this->config->getSystemValue('force_locale', false); |
|
| 121 | + if (is_string($forceLocale)) { |
|
| 122 | + $locale = $forceLocale; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + if ($lang === null || !$this->languageExists($app, $lang)) { |
|
| 126 | + $lang = $this->findLanguage($app); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + if ($locale === null || !$this->localeExists($locale)) { |
|
| 130 | + $locale = $this->findLocale($lang); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + if (!isset($this->instances[$lang][$app])) { |
|
| 134 | + $this->instances[$lang][$app] = new L10N( |
|
| 135 | + $this, $app, $lang, $locale, |
|
| 136 | + $this->getL10nFilesForApp($app, $lang) |
|
| 137 | + ); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return $this->instances[$lang][$app]; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Find the best language |
|
| 145 | + * |
|
| 146 | + * @param string|null $app App id or null for core |
|
| 147 | + * @return string language If nothing works it returns 'en' |
|
| 148 | + */ |
|
| 149 | + public function findLanguage($app = null) { |
|
| 150 | + $forceLang = $this->config->getSystemValue('force_language', false); |
|
| 151 | + if (is_string($forceLang)) { |
|
| 152 | + $this->requestLanguage = $forceLang; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + if ($this->requestLanguage !== '' && $this->languageExists($app, $this->requestLanguage)) { |
|
| 156 | + return $this->requestLanguage; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * At this point Nextcloud might not yet be installed and thus the lookup |
|
| 161 | + * in the preferences table might fail. For this reason we need to check |
|
| 162 | + * whether the instance has already been installed |
|
| 163 | + * |
|
| 164 | + * @link https://github.com/owncloud/core/issues/21955 |
|
| 165 | + */ |
|
| 166 | + if ($this->config->getSystemValue('installed', false)) { |
|
| 167 | + $userId = !is_null($this->userSession->getUser()) ? $this->userSession->getUser()->getUID() : null; |
|
| 168 | + if (!is_null($userId)) { |
|
| 169 | + $userLang = $this->config->getUserValue($userId, 'core', 'lang', null); |
|
| 170 | + } else { |
|
| 171 | + $userLang = null; |
|
| 172 | + } |
|
| 173 | + } else { |
|
| 174 | + $userId = null; |
|
| 175 | + $userLang = null; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if ($userLang) { |
|
| 179 | + $this->requestLanguage = $userLang; |
|
| 180 | + if ($this->languageExists($app, $userLang)) { |
|
| 181 | + return $userLang; |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + try { |
|
| 186 | + // Try to get the language from the Request |
|
| 187 | + $lang = $this->getLanguageFromRequest($app); |
|
| 188 | + if ($userId !== null && $app === null && !$userLang) { |
|
| 189 | + $this->config->setUserValue($userId, 'core', 'lang', $lang); |
|
| 190 | + } |
|
| 191 | + return $lang; |
|
| 192 | + } catch (LanguageNotFoundException $e) { |
|
| 193 | + // Finding language from request failed fall back to default language |
|
| 194 | + $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 195 | + if ($defaultLanguage !== false && $this->languageExists($app, $defaultLanguage)) { |
|
| 196 | + return $defaultLanguage; |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + // We could not find any language so fall back to english |
|
| 201 | + return 'en'; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * find the best locale |
|
| 206 | + * |
|
| 207 | + * @param string $lang |
|
| 208 | + * @return null|string |
|
| 209 | + */ |
|
| 210 | + public function findLocale($lang = null) { |
|
| 211 | + $forceLocale = $this->config->getSystemValue('force_locale', false); |
|
| 212 | + if (is_string($forceLocale) && $this->localeExists($forceLocale)) { |
|
| 213 | + return $forceLocale; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + if ($this->config->getSystemValue('installed', false)) { |
|
| 217 | + $userId = null !== $this->userSession->getUser() ? $this->userSession->getUser()->getUID() : null; |
|
| 218 | + $userLocale = null; |
|
| 219 | + if (null !== $userId) { |
|
| 220 | + $userLocale = $this->config->getUserValue($userId, 'core', 'locale', null); |
|
| 221 | + } |
|
| 222 | + } else { |
|
| 223 | + $userId = null; |
|
| 224 | + $userLocale = null; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + if ($userLocale && $this->localeExists($userLocale)) { |
|
| 228 | + return $userLocale; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + // Default : use system default locale |
|
| 232 | + $defaultLocale = $this->config->getSystemValue('default_locale', false); |
|
| 233 | + if ($defaultLocale !== false && $this->localeExists($defaultLocale)) { |
|
| 234 | + return $defaultLocale; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + // If no user locale set, use lang as locale |
|
| 238 | + if (null !== $lang && $this->localeExists($lang)) { |
|
| 239 | + return $lang; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + // At last, return USA |
|
| 243 | + return 'en_US'; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Find all available languages for an app |
|
| 248 | + * |
|
| 249 | + * @param string|null $app App id or null for core |
|
| 250 | + * @return array an array of available languages |
|
| 251 | + */ |
|
| 252 | + public function findAvailableLanguages($app = null) { |
|
| 253 | + $key = $app; |
|
| 254 | + if ($key === null) { |
|
| 255 | + $key = 'null'; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + // also works with null as key |
|
| 259 | + if (!empty($this->availableLanguages[$key])) { |
|
| 260 | + return $this->availableLanguages[$key]; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + $available = ['en']; //english is always available |
|
| 264 | + $dir = $this->findL10nDir($app); |
|
| 265 | + if (is_dir($dir)) { |
|
| 266 | + $files = scandir($dir); |
|
| 267 | + if ($files !== false) { |
|
| 268 | + foreach ($files as $file) { |
|
| 269 | + if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 270 | + $available[] = substr($file, 0, -5); |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + // merge with translations from theme |
|
| 277 | + $theme = $this->config->getSystemValue('theme'); |
|
| 278 | + if (!empty($theme)) { |
|
| 279 | + $themeDir = $this->serverRoot . '/themes/' . $theme . substr($dir, strlen($this->serverRoot)); |
|
| 280 | + |
|
| 281 | + if (is_dir($themeDir)) { |
|
| 282 | + $files = scandir($themeDir); |
|
| 283 | + if ($files !== false) { |
|
| 284 | + foreach ($files as $file) { |
|
| 285 | + if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') { |
|
| 286 | + $available[] = substr($file, 0, -5); |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + $this->availableLanguages[$key] = $available; |
|
| 294 | + return $available; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * @return array|mixed |
|
| 299 | + */ |
|
| 300 | + public function findAvailableLocales() { |
|
| 301 | + if (!empty($this->availableLocales)) { |
|
| 302 | + return $this->availableLocales; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + $localeData = file_get_contents(\OC::$SERVERROOT . '/resources/locales.json'); |
|
| 306 | + $this->availableLocales = \json_decode($localeData, true); |
|
| 307 | + |
|
| 308 | + return $this->availableLocales; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * @param string|null $app App id or null for core |
|
| 313 | + * @param string $lang |
|
| 314 | + * @return bool |
|
| 315 | + */ |
|
| 316 | + public function languageExists($app, $lang) { |
|
| 317 | + if ($lang === 'en') {//english is always available |
|
| 318 | + return true; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + $languages = $this->findAvailableLanguages($app); |
|
| 322 | + return array_search($lang, $languages) !== false; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + public function iterateLanguage(bool $reset = false): string { |
|
| 326 | + static $i = 0; |
|
| 327 | + if($reset) { |
|
| 328 | + $i = 0; |
|
| 329 | + } |
|
| 330 | + switch($i) { |
|
| 331 | + /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 332 | + case 0: |
|
| 333 | + $i++; |
|
| 334 | + $forcedLang = $this->config->getSystemValue('force_language', false); |
|
| 335 | + if(is_string($forcedLang)) { |
|
| 336 | + return $forcedLang; |
|
| 337 | + } |
|
| 338 | + /** @noinspection PhpMissingBreakStatementInspection */ |
|
| 339 | + case 1: |
|
| 340 | + $i++; |
|
| 341 | + $user = $this->userSession->getUser(); |
|
| 342 | + if($user instanceof IUser) { |
|
| 343 | + $userLang = $this->config->getUserValue($user->getUID(), 'core', 'lang', null); |
|
| 344 | + if(is_string($userLang)) { |
|
| 345 | + return $userLang; |
|
| 346 | + } |
|
| 347 | + } |
|
| 348 | + case 2: |
|
| 349 | + $i++; |
|
| 350 | + return $this->config->getSystemValue('default_language', 'en'); |
|
| 351 | + default: |
|
| 352 | + return 'en'; |
|
| 353 | + } |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * @param string $locale |
|
| 358 | + * @return bool |
|
| 359 | + */ |
|
| 360 | + public function localeExists($locale) { |
|
| 361 | + if ($locale === 'en') { //english is always available |
|
| 362 | + return true; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + $locales = $this->findAvailableLocales(); |
|
| 366 | + $userLocale = array_filter($locales, function($value) use ($locale) { |
|
| 367 | + return $locale === $value['code']; |
|
| 368 | + }); |
|
| 369 | + |
|
| 370 | + return !empty($userLocale); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * @param string|null $app |
|
| 375 | + * @return string |
|
| 376 | + * @throws LanguageNotFoundException |
|
| 377 | + */ |
|
| 378 | + private function getLanguageFromRequest($app) { |
|
| 379 | + $header = $this->request->getHeader('ACCEPT_LANGUAGE'); |
|
| 380 | + if ($header !== '') { |
|
| 381 | + $available = $this->findAvailableLanguages($app); |
|
| 382 | + |
|
| 383 | + // E.g. make sure that 'de' is before 'de_DE'. |
|
| 384 | + sort($available); |
|
| 385 | + |
|
| 386 | + $preferences = preg_split('/,\s*/', strtolower($header)); |
|
| 387 | + foreach ($preferences as $preference) { |
|
| 388 | + list($preferred_language) = explode(';', $preference); |
|
| 389 | + $preferred_language = str_replace('-', '_', $preferred_language); |
|
| 390 | + |
|
| 391 | + foreach ($available as $available_language) { |
|
| 392 | + if ($preferred_language === strtolower($available_language)) { |
|
| 393 | + return $this->respectDefaultLanguage($app, $available_language); |
|
| 394 | + } |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + // Fallback from de_De to de |
|
| 398 | + foreach ($available as $available_language) { |
|
| 399 | + if (substr($preferred_language, 0, 2) === $available_language) { |
|
| 400 | + return $available_language; |
|
| 401 | + } |
|
| 402 | + } |
|
| 403 | + } |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + throw new LanguageNotFoundException(); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + /** |
|
| 410 | + * if default language is set to de_DE (formal German) this should be |
|
| 411 | + * preferred to 'de' (non-formal German) if possible |
|
| 412 | + * |
|
| 413 | + * @param string|null $app |
|
| 414 | + * @param string $lang |
|
| 415 | + * @return string |
|
| 416 | + */ |
|
| 417 | + protected function respectDefaultLanguage($app, $lang) { |
|
| 418 | + $result = $lang; |
|
| 419 | + $defaultLanguage = $this->config->getSystemValue('default_language', false); |
|
| 420 | + |
|
| 421 | + // use formal version of german ("Sie" instead of "Du") if the default |
|
| 422 | + // language is set to 'de_DE' if possible |
|
| 423 | + if (is_string($defaultLanguage) && |
|
| 424 | + strtolower($lang) === 'de' && |
|
| 425 | + strtolower($defaultLanguage) === 'de_de' && |
|
| 426 | + $this->languageExists($app, 'de_DE') |
|
| 427 | + ) { |
|
| 428 | + $result = 'de_DE'; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + return $result; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * Checks if $sub is a subdirectory of $parent |
|
| 436 | + * |
|
| 437 | + * @param string $sub |
|
| 438 | + * @param string $parent |
|
| 439 | + * @return bool |
|
| 440 | + */ |
|
| 441 | + private function isSubDirectory($sub, $parent) { |
|
| 442 | + // Check whether $sub contains no ".." |
|
| 443 | + if (strpos($sub, '..') !== false) { |
|
| 444 | + return false; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + // Check whether $sub is a subdirectory of $parent |
|
| 448 | + if (strpos($sub, $parent) === 0) { |
|
| 449 | + return true; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + return false; |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Get a list of language files that should be loaded |
|
| 457 | + * |
|
| 458 | + * @param string $app |
|
| 459 | + * @param string $lang |
|
| 460 | + * @return string[] |
|
| 461 | + */ |
|
| 462 | + // FIXME This method is only public, until OC_L10N does not need it anymore, |
|
| 463 | + // FIXME This is also the reason, why it is not in the public interface |
|
| 464 | + public function getL10nFilesForApp($app, $lang) { |
|
| 465 | + $languageFiles = []; |
|
| 466 | + |
|
| 467 | + $i18nDir = $this->findL10nDir($app); |
|
| 468 | + $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json'; |
|
| 469 | + |
|
| 470 | + if (($this->isSubDirectory($transFile, $this->serverRoot . '/core/l10n/') |
|
| 471 | + || $this->isSubDirectory($transFile, $this->serverRoot . '/lib/l10n/') |
|
| 472 | + || $this->isSubDirectory($transFile, $this->serverRoot . '/settings/l10n/') |
|
| 473 | + || $this->isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/') |
|
| 474 | + ) |
|
| 475 | + && file_exists($transFile)) { |
|
| 476 | + // load the translations file |
|
| 477 | + $languageFiles[] = $transFile; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + // merge with translations from theme |
|
| 481 | + $theme = $this->config->getSystemValue('theme'); |
|
| 482 | + if (!empty($theme)) { |
|
| 483 | + $transFile = $this->serverRoot . '/themes/' . $theme . substr($transFile, strlen($this->serverRoot)); |
|
| 484 | + if (file_exists($transFile)) { |
|
| 485 | + $languageFiles[] = $transFile; |
|
| 486 | + } |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + return $languageFiles; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * find the l10n directory |
|
| 494 | + * |
|
| 495 | + * @param string $app App id or empty string for core |
|
| 496 | + * @return string directory |
|
| 497 | + */ |
|
| 498 | + protected function findL10nDir($app = null) { |
|
| 499 | + if (in_array($app, ['core', 'lib', 'settings'])) { |
|
| 500 | + if (file_exists($this->serverRoot . '/' . $app . '/l10n/')) { |
|
| 501 | + return $this->serverRoot . '/' . $app . '/l10n/'; |
|
| 502 | + } |
|
| 503 | + } else if ($app && \OC_App::getAppPath($app) !== false) { |
|
| 504 | + // Check if the app is in the app folder |
|
| 505 | + return \OC_App::getAppPath($app) . '/l10n/'; |
|
| 506 | + } |
|
| 507 | + return $this->serverRoot . '/core/l10n/'; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * Creates a function from the plural string |
|
| 513 | + * |
|
| 514 | + * Parts of the code is copied from Habari: |
|
| 515 | + * https://github.com/habari/system/blob/master/classes/locale.php |
|
| 516 | + * @param string $string |
|
| 517 | + * @return string |
|
| 518 | + */ |
|
| 519 | + public function createPluralFunction($string) { |
|
| 520 | + if (isset($this->pluralFunctions[$string])) { |
|
| 521 | + return $this->pluralFunctions[$string]; |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) { |
|
| 525 | + // sanitize |
|
| 526 | + $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] ); |
|
| 527 | + $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] ); |
|
| 528 | + |
|
| 529 | + $body = str_replace( |
|
| 530 | + array( 'plural', 'n', '$n$plurals', ), |
|
| 531 | + array( '$plural', '$n', '$nplurals', ), |
|
| 532 | + 'nplurals='. $nplurals . '; plural=' . $plural |
|
| 533 | + ); |
|
| 534 | + |
|
| 535 | + // add parents |
|
| 536 | + // important since PHP's ternary evaluates from left to right |
|
| 537 | + $body .= ';'; |
|
| 538 | + $res = ''; |
|
| 539 | + $p = 0; |
|
| 540 | + $length = strlen($body); |
|
| 541 | + for($i = 0; $i < $length; $i++) { |
|
| 542 | + $ch = $body[$i]; |
|
| 543 | + switch ( $ch ) { |
|
| 544 | + case '?': |
|
| 545 | + $res .= ' ? ('; |
|
| 546 | + $p++; |
|
| 547 | + break; |
|
| 548 | + case ':': |
|
| 549 | + $res .= ') : ('; |
|
| 550 | + break; |
|
| 551 | + case ';': |
|
| 552 | + $res .= str_repeat( ')', $p ) . ';'; |
|
| 553 | + $p = 0; |
|
| 554 | + break; |
|
| 555 | + default: |
|
| 556 | + $res .= $ch; |
|
| 557 | + } |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);'; |
|
| 561 | + $function = create_function('$n', $body); |
|
| 562 | + $this->pluralFunctions[$string] = $function; |
|
| 563 | + return $function; |
|
| 564 | + } else { |
|
| 565 | + // default: one plural form for all cases but n==1 (english) |
|
| 566 | + $function = create_function( |
|
| 567 | + '$n', |
|
| 568 | + '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);' |
|
| 569 | + ); |
|
| 570 | + $this->pluralFunctions[$string] = $function; |
|
| 571 | + return $function; |
|
| 572 | + } |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + /** |
|
| 576 | + * returns the common language and other languages in an |
|
| 577 | + * associative array |
|
| 578 | + * |
|
| 579 | + * @return array |
|
| 580 | + */ |
|
| 581 | + public function getLanguages() { |
|
| 582 | + $forceLanguage = $this->config->getSystemValue('force_language', false); |
|
| 583 | + if ($forceLanguage !== false) { |
|
| 584 | + return []; |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + $languageCodes = $this->findAvailableLanguages(); |
|
| 588 | + |
|
| 589 | + $commonLanguages = []; |
|
| 590 | + $languages = []; |
|
| 591 | + |
|
| 592 | + foreach($languageCodes as $lang) { |
|
| 593 | + $l = $this->get('lib', $lang); |
|
| 594 | + // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version |
|
| 595 | + $potentialName = (string) $l->t('__language_name__'); |
|
| 596 | + if ($l->getLanguageCode() === $lang && $potentialName[0] !== '_') {//first check if the language name is in the translation file |
|
| 597 | + $ln = array( |
|
| 598 | + 'code' => $lang, |
|
| 599 | + 'name' => $potentialName |
|
| 600 | + ); |
|
| 601 | + } else if ($lang === 'en') { |
|
| 602 | + $ln = array( |
|
| 603 | + 'code' => $lang, |
|
| 604 | + 'name' => 'English (US)' |
|
| 605 | + ); |
|
| 606 | + } else {//fallback to language code |
|
| 607 | + $ln = array( |
|
| 608 | + 'code' => $lang, |
|
| 609 | + 'name' => $lang |
|
| 610 | + ); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + // put appropriate languages into appropriate arrays, to print them sorted |
|
| 614 | + // common languages -> divider -> other languages |
|
| 615 | + if (in_array($lang, self::COMMON_LANGUAGE_CODES)) { |
|
| 616 | + $commonLanguages[array_search($lang, self::COMMON_LANGUAGE_CODES)] = $ln; |
|
| 617 | + } else { |
|
| 618 | + $languages[] = $ln; |
|
| 619 | + } |
|
| 620 | + } |
|
| 621 | + |
|
| 622 | + ksort($commonLanguages); |
|
| 623 | + |
|
| 624 | + // sort now by displayed language not the iso-code |
|
| 625 | + usort( $languages, function ($a, $b) { |
|
| 626 | + if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { |
|
| 627 | + // If a doesn't have a name, but b does, list b before a |
|
| 628 | + return 1; |
|
| 629 | + } |
|
| 630 | + if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) { |
|
| 631 | + // If a does have a name, but b doesn't, list a before b |
|
| 632 | + return -1; |
|
| 633 | + } |
|
| 634 | + // Otherwise compare the names |
|
| 635 | + return strcmp($a['name'], $b['name']); |
|
| 636 | + }); |
|
| 637 | + |
|
| 638 | + return [ |
|
| 639 | + // reset indexes |
|
| 640 | + 'commonlanguages' => array_values($commonLanguages), |
|
| 641 | + 'languages' => $languages |
|
| 642 | + ]; |
|
| 643 | + } |
|
| 644 | 644 | } |