1
0

Result.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Comments\Search;
  7. use OCP\Comments\IComment;
  8. use OCP\Files\NotFoundException;
  9. use OCP\Search\Result as BaseResult;
  10. /**
  11. * @deprecated 20.0.0
  12. */
  13. class Result extends BaseResult {
  14. /**
  15. * @deprecated 20.0.0
  16. */
  17. public $type = 'comment';
  18. /**
  19. * @deprecated 20.0.0
  20. */
  21. public $comment;
  22. /**
  23. * @deprecated 20.0.0
  24. */
  25. public $authorId;
  26. /**
  27. * @deprecated 20.0.0
  28. */
  29. public string $authorName;
  30. /**
  31. * @deprecated 20.0.0
  32. */
  33. public $path;
  34. /**
  35. * @deprecated 20.0.0
  36. */
  37. public $fileName;
  38. /**
  39. * @throws NotFoundException
  40. * @deprecated 20.0.0
  41. */
  42. public function __construct(
  43. string $search,
  44. IComment $comment,
  45. string $authorName,
  46. string $path,
  47. ) {
  48. parent::__construct(
  49. $comment->getId(),
  50. $comment->getMessage()
  51. /* @todo , [link to file] */
  52. );
  53. $this->comment = $this->getRelevantMessagePart($comment->getMessage(), $search);
  54. $this->authorId = $comment->getActorId();
  55. $this->authorName = $authorName;
  56. $this->fileName = basename($path);
  57. $this->path = $this->getVisiblePath($path);
  58. }
  59. /**
  60. * @throws NotFoundException
  61. */
  62. protected function getVisiblePath(string $path): string {
  63. $segments = explode('/', trim($path, '/'), 3);
  64. if (!isset($segments[2])) {
  65. throw new NotFoundException('Path not inside visible section');
  66. }
  67. return $segments[2];
  68. }
  69. /**
  70. * @throws NotFoundException
  71. */
  72. protected function getRelevantMessagePart(string $message, string $search): string {
  73. $start = mb_stripos($message, $search);
  74. if ($start === false) {
  75. throw new NotFoundException('Comment section not found');
  76. }
  77. $end = $start + mb_strlen($search);
  78. if ($start <= 25) {
  79. $start = 0;
  80. $prefix = '';
  81. } else {
  82. $start -= 25;
  83. $prefix = '…';
  84. }
  85. if ((mb_strlen($message) - $end) <= 25) {
  86. $end = mb_strlen($message);
  87. $suffix = '';
  88. } else {
  89. $end += 25;
  90. $suffix = '…';
  91. }
  92. return $prefix . mb_substr($message, $start, $end - $start) . $suffix;
  93. }
  94. }