Result.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 $path;
  30. /**
  31. * @deprecated 20.0.0
  32. */
  33. public $fileName;
  34. /**
  35. * @throws NotFoundException
  36. * @deprecated 20.0.0
  37. */
  38. public function __construct(
  39. string $search,
  40. IComment $comment,
  41. /**
  42. * @deprecated 20.0.0
  43. */
  44. public string $authorName,
  45. string $path,
  46. ) {
  47. parent::__construct(
  48. $comment->getId(),
  49. $comment->getMessage()
  50. /* @todo , [link to file] */
  51. );
  52. $this->comment = $this->getRelevantMessagePart($comment->getMessage(), $search);
  53. $this->authorId = $comment->getActorId();
  54. $this->fileName = basename($path);
  55. $this->path = $this->getVisiblePath($path);
  56. }
  57. /**
  58. * @throws NotFoundException
  59. */
  60. protected function getVisiblePath(string $path): string {
  61. $segments = explode('/', trim($path, '/'), 3);
  62. if (!isset($segments[2])) {
  63. throw new NotFoundException('Path not inside visible section');
  64. }
  65. return $segments[2];
  66. }
  67. /**
  68. * @throws NotFoundException
  69. */
  70. protected function getRelevantMessagePart(string $message, string $search): string {
  71. $start = mb_stripos($message, $search);
  72. if ($start === false) {
  73. throw new NotFoundException('Comment section not found');
  74. }
  75. $end = $start + mb_strlen($search);
  76. if ($start <= 25) {
  77. $start = 0;
  78. $prefix = '';
  79. } else {
  80. $start -= 25;
  81. $prefix = '…';
  82. }
  83. if ((mb_strlen($message) - $end) <= 25) {
  84. $end = mb_strlen($message);
  85. $suffix = '';
  86. } else {
  87. $end += 25;
  88. $suffix = '…';
  89. }
  90. return $prefix . mb_substr($message, $start, $end - $start) . $suffix;
  91. }
  92. }