Result.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\Comments\Search;
  22. use OCP\Comments\IComment;
  23. use OCP\Files\NotFoundException;
  24. use OCP\Search\Result as BaseResult;
  25. class Result extends BaseResult {
  26. public $type = 'comment';
  27. public $comment;
  28. public $authorId;
  29. public $authorName;
  30. public $path;
  31. public $fileName;
  32. /**
  33. * @param string $search
  34. * @param IComment $comment
  35. * @param string $authorName
  36. * @param string $path
  37. * @throws NotFoundException
  38. */
  39. public function __construct(string $search,
  40. IComment $comment,
  41. string $authorName,
  42. string $path) {
  43. parent::__construct(
  44. (int) $comment->getId(),
  45. $comment->getMessage()
  46. /* @todo , [link to file] */
  47. );
  48. $this->comment = $this->getRelevantMessagePart($comment->getMessage(), $search);
  49. $this->authorId = $comment->getActorId();
  50. $this->authorName = $authorName;
  51. $this->fileName = basename($path);
  52. $this->path = $this->getVisiblePath($path);
  53. }
  54. /**
  55. * @param string $path
  56. * @return string
  57. * @throws NotFoundException
  58. */
  59. protected function getVisiblePath(string $path): string {
  60. $segments = explode('/', trim($path, '/'), 3);
  61. if (!isset($segments[2])) {
  62. throw new NotFoundException('Path not inside visible section');
  63. }
  64. return $segments[2];
  65. }
  66. /**
  67. * @param string $message
  68. * @param string $search
  69. * @return string
  70. * @throws NotFoundException
  71. */
  72. protected function getRelevantMessagePart(string $message, string $search): string {
  73. $start = stripos($message, $search);
  74. if ($start === false) {
  75. throw new NotFoundException('Comment section not found');
  76. }
  77. $end = $start + strlen($search);
  78. if ($start <= 25) {
  79. $start = 0;
  80. $prefix = '';
  81. } else {
  82. $start -= 25;
  83. $prefix = '…';
  84. }
  85. if ((strlen($message) - $end) <= 25) {
  86. $end = strlen($message);
  87. $suffix = '';
  88. } else {
  89. $end += 25;
  90. $suffix = '…';
  91. }
  92. return $prefix . substr($message, $start, $end - $start) . $suffix;
  93. }
  94. }