UpdateChecker.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\UpdateNotification;
  8. use OC\Updater\ChangesCheck;
  9. use OC\Updater\VersionCheck;
  10. use OCP\AppFramework\Services\IInitialState;
  11. class UpdateChecker {
  12. public function __construct(
  13. private VersionCheck $updater,
  14. private ChangesCheck $changesCheck,
  15. private IInitialState $initialState,
  16. ) {
  17. }
  18. /**
  19. * @return array
  20. */
  21. public function getUpdateState(): array {
  22. $data = $this->updater->check();
  23. $result = [];
  24. if (isset($data['version']) && $data['version'] !== '' && $data['version'] !== []) {
  25. $result['updateAvailable'] = true;
  26. $result['updateVersion'] = $data['version'];
  27. $result['updateVersionString'] = $data['versionstring'];
  28. $result['updaterEnabled'] = $data['autoupdater'] === '1';
  29. $result['versionIsEol'] = $data['eol'] === '1';
  30. if (strpos($data['web'], 'https://') === 0) {
  31. $result['updateLink'] = $data['web'];
  32. }
  33. if (strpos($data['url'], 'https://') === 0) {
  34. $result['downloadLink'] = $data['url'];
  35. }
  36. if (strpos($data['changes'], 'https://') === 0) {
  37. try {
  38. $result['changes'] = $this->changesCheck->check($data['changes'], $data['version']);
  39. } catch (\Exception $e) {
  40. // no info, not a problem
  41. }
  42. }
  43. return $result;
  44. }
  45. return [];
  46. }
  47. /**
  48. * Provide update information as initial state
  49. */
  50. public function setInitialState(): void {
  51. $updateState = $this->getUpdateState();
  52. if (empty($updateState)) {
  53. return;
  54. }
  55. $this->initialState->provideInitialState('updateState', [
  56. 'updateVersion' => $updateState['updateVersionString'],
  57. 'updateLink' => $updateState['updateLink'] ?? '',
  58. ]);
  59. }
  60. }