1
0

Manager.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Ferdinand Thiessen <opensource@fthiessen.de>
  5. *
  6. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\UpdateNotification;
  25. use OCP\App\IAppManager;
  26. use OCP\IUser;
  27. use OCP\IUserSession;
  28. use OCP\L10N\IFactory;
  29. use Psr\Log\LoggerInterface;
  30. class Manager {
  31. private ?IUser $currentUser;
  32. public function __construct(
  33. IUserSession $currentSession,
  34. private IAppManager $appManager,
  35. private IFactory $l10NFactory,
  36. private LoggerInterface $logger,
  37. ) {
  38. $this->currentUser = $currentSession->getUser();
  39. }
  40. /**
  41. * Get the changelog entry for the given appId
  42. * @param string $appId The app for which to query the entry
  43. * @param string $version The version for which to query the changelog entry
  44. * @param ?string $languageCode The language in which to query the changelog (defaults to current user language and fallsback to English)
  45. * @return string|null Either the changelog entry or null if no changelog is found
  46. */
  47. public function getChangelog(string $appId, string $version, ?string $languageCode = null): string|null {
  48. if ($languageCode === null) {
  49. $languageCode = $this->l10NFactory->getUserLanguage($this->currentUser);
  50. }
  51. $path = $this->getChangelogFile($appId, $languageCode);
  52. if ($path === null) {
  53. $this->logger->debug('No changelog file found for app ' . $appId . ' and language code ' . $languageCode);
  54. return null;
  55. }
  56. $changes = $this->retrieveChangelogEntry($path, $version);
  57. return $changes;
  58. }
  59. /**
  60. * Get the changelog file in the requested language or fallback to English
  61. * @param string $appId The app to load the changelog for
  62. * @param string $languageCode The language code to search
  63. * @return string|null Either the file path or null if not found
  64. */
  65. public function getChangelogFile(string $appId, string $languageCode): string|null {
  66. try {
  67. $appPath = $this->appManager->getAppPath($appId);
  68. $files = ["CHANGELOG.$languageCode.md", 'CHANGELOG.en.md'];
  69. foreach ($files as $file) {
  70. $path = $appPath . '/' . $file;
  71. if (is_file($path)) {
  72. return $path;
  73. }
  74. }
  75. } catch (\Throwable $e) {
  76. // ignore and return null below
  77. }
  78. return null;
  79. }
  80. /**
  81. * Retrieve a log entry from the changelog
  82. * @param string $path The path to the changlog file
  83. * @param string $version The version to query (make sure to only pass in "{major}.{minor}(.{patch}" format)
  84. */
  85. protected function retrieveChangelogEntry(string $path, string $version): string|null {
  86. $matches = [];
  87. $content = file_get_contents($path);
  88. if ($content === false) {
  89. $this->logger->debug('Could not open changelog file', ['file-path' => $path]);
  90. return null;
  91. }
  92. $result = preg_match_all('/^## (?:\[)?(?:v)?(\d+\.\d+(\.\d+)?)/m', $content, $matches, PREG_OFFSET_CAPTURE);
  93. if ($result === false || $result === 0) {
  94. $this->logger->debug('No entries in changelog found', ['file_path' => $path]);
  95. return null;
  96. }
  97. // Get the key of the match that equals the requested version
  98. $index = array_key_first(
  99. // Get the array containing the match that equals the requested version, keys are preserved so: [1 => '1.2.4']
  100. array_filter(
  101. // This is the array of the versions found, like ['1.2.3', '1.2.4']
  102. $matches[1],
  103. // Callback to filter only version that matches the requested version
  104. fn (array $match) => version_compare($match[0], $version, '=='),
  105. )
  106. );
  107. if ($index === null) {
  108. $this->logger->debug('No changelog entry for version ' . $version . ' found', ['file_path' => $path]);
  109. return null;
  110. }
  111. $offsetChangelogEntry = $matches[0][$index][1];
  112. // Length of the changelog entry (offset of next match - own offset) or null if the whole rest should be considered
  113. $lengthChangelogEntry = $index < ($result - 1) ? ($matches[0][$index + 1][1] - $offsetChangelogEntry) : null;
  114. return substr($content, $offsetChangelogEntry, $lengthChangelogEntry);
  115. }
  116. }