You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Result.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Michał Węgrzynek <michal.wegrzynek@malloc.com.pl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  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\Comments\Search;
  25. use OCP\Comments\IComment;
  26. use OCP\Files\NotFoundException;
  27. use OCP\Search\Result as BaseResult;
  28. class Result extends BaseResult {
  29. public $type = 'comment';
  30. public $comment;
  31. public $authorId;
  32. public $authorName;
  33. public $path;
  34. public $fileName;
  35. /**
  36. * @param string $search
  37. * @param IComment $comment
  38. * @param string $authorName
  39. * @param string $path
  40. * @throws NotFoundException
  41. */
  42. public function __construct(string $search,
  43. IComment $comment,
  44. string $authorName,
  45. string $path) {
  46. parent::__construct(
  47. (int) $comment->getId(),
  48. $comment->getMessage()
  49. /* @todo , [link to file] */
  50. );
  51. $this->comment = $this->getRelevantMessagePart($comment->getMessage(), $search);
  52. $this->authorId = $comment->getActorId();
  53. $this->authorName = $authorName;
  54. $this->fileName = basename($path);
  55. $this->path = $this->getVisiblePath($path);
  56. }
  57. /**
  58. * @param string $path
  59. * @return string
  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. * @param string $message
  71. * @param string $search
  72. * @return string
  73. * @throws NotFoundException
  74. */
  75. protected function getRelevantMessagePart(string $message, string $search): string {
  76. $start = mb_stripos($message, $search);
  77. if ($start === false) {
  78. throw new NotFoundException('Comment section not found');
  79. }
  80. $end = $start + mb_strlen($search);
  81. if ($start <= 25) {
  82. $start = 0;
  83. $prefix = '';
  84. } else {
  85. $start -= 25;
  86. $prefix = '…';
  87. }
  88. if ((mb_strlen($message) - $end) <= 25) {
  89. $end = mb_strlen($message);
  90. $suffix = '';
  91. } else {
  92. $end += 25;
  93. $suffix = '…';
  94. }
  95. return $prefix . mb_substr($message, $start, $end - $start) . $suffix;
  96. }
  97. }