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

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