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.7KB

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