1
0

Manager.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UpdateNotification;
  8. use OCP\App\IAppManager;
  9. use OCP\IUser;
  10. use OCP\IUserSession;
  11. use OCP\L10N\IFactory;
  12. use Psr\Log\LoggerInterface;
  13. class Manager {
  14. private ?IUser $currentUser;
  15. public function __construct(
  16. IUserSession $currentSession,
  17. private IAppManager $appManager,
  18. private IFactory $l10NFactory,
  19. private LoggerInterface $logger,
  20. ) {
  21. $this->currentUser = $currentSession->getUser();
  22. }
  23. /**
  24. * Get the changelog entry for the given appId
  25. * @param string $appId The app for which to query the entry
  26. * @param string $version The version for which to query the changelog entry
  27. * @param ?string $languageCode The language in which to query the changelog (defaults to current user language and fallsback to English)
  28. * @return string|null Either the changelog entry or null if no changelog is found
  29. */
  30. public function getChangelog(string $appId, string $version, ?string $languageCode = null): ?string {
  31. if ($languageCode === null) {
  32. $languageCode = $this->l10NFactory->getUserLanguage($this->currentUser);
  33. }
  34. $path = $this->getChangelogFile($appId, $languageCode);
  35. if ($path === null) {
  36. $this->logger->debug('No changelog file found for app ' . $appId . ' and language code ' . $languageCode);
  37. return null;
  38. }
  39. $changes = $this->retrieveChangelogEntry($path, $version);
  40. return $changes;
  41. }
  42. /**
  43. * Get the changelog file in the requested language or fallback to English
  44. * @param string $appId The app to load the changelog for
  45. * @param string $languageCode The language code to search
  46. * @return string|null Either the file path or null if not found
  47. */
  48. public function getChangelogFile(string $appId, string $languageCode): ?string {
  49. try {
  50. $appPath = $this->appManager->getAppPath($appId);
  51. $files = ["CHANGELOG.$languageCode.md", 'CHANGELOG.en.md'];
  52. foreach ($files as $file) {
  53. $path = $appPath . '/' . $file;
  54. if (is_file($path)) {
  55. return $path;
  56. }
  57. }
  58. } catch (\Throwable $e) {
  59. // ignore and return null below
  60. }
  61. return null;
  62. }
  63. /**
  64. * Retrieve a log entry from the changelog
  65. * @param string $path The path to the changlog file
  66. * @param string $version The version to query (make sure to only pass in "{major}.{minor}(.{patch}" format)
  67. */
  68. protected function retrieveChangelogEntry(string $path, string $version): ?string {
  69. $matches = [];
  70. $content = file_get_contents($path);
  71. if ($content === false) {
  72. $this->logger->debug('Could not open changelog file', ['file-path' => $path]);
  73. return null;
  74. }
  75. $result = preg_match_all('/^## (?:\[)?(?:v)?(\d+\.\d+(\.\d+)?)/m', $content, $matches, PREG_OFFSET_CAPTURE);
  76. if ($result === false || $result === 0) {
  77. $this->logger->debug('No entries in changelog found', ['file_path' => $path]);
  78. return null;
  79. }
  80. // Get the key of the match that equals the requested version
  81. $index = array_key_first(
  82. // Get the array containing the match that equals the requested version, keys are preserved so: [1 => '1.2.4']
  83. array_filter(
  84. // This is the array of the versions found, like ['1.2.3', '1.2.4']
  85. $matches[1],
  86. // Callback to filter only version that matches the requested version
  87. fn (array $match) => version_compare($match[0], $version, '=='),
  88. )
  89. );
  90. if ($index === null) {
  91. $this->logger->debug('No changelog entry for version ' . $version . ' found', ['file_path' => $path]);
  92. return null;
  93. }
  94. $offsetChangelogEntry = $matches[0][$index][1];
  95. // Length of the changelog entry (offset of next match - own offset) or null if the whole rest should be considered
  96. $lengthChangelogEntry = $index < ($result - 1) ? ($matches[0][$index + 1][1] - $offsetChangelogEntry) : null;
  97. return substr($content, $offsetChangelogEntry, $lengthChangelogEntry);
  98. }
  99. }