Result.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 string $authorName;
  49. /**
  50. * @deprecated 20.0.0
  51. */
  52. public $path;
  53. /**
  54. * @deprecated 20.0.0
  55. */
  56. public $fileName;
  57. /**
  58. * @throws NotFoundException
  59. * @deprecated 20.0.0
  60. */
  61. public function __construct(
  62. string $search,
  63. IComment $comment,
  64. string $authorName,
  65. string $path,
  66. ) {
  67. parent::__construct(
  68. $comment->getId(),
  69. $comment->getMessage()
  70. /* @todo , [link to file] */
  71. );
  72. $this->comment = $this->getRelevantMessagePart($comment->getMessage(), $search);
  73. $this->authorId = $comment->getActorId();
  74. $this->authorName = $authorName;
  75. $this->fileName = basename($path);
  76. $this->path = $this->getVisiblePath($path);
  77. }
  78. /**
  79. * @throws NotFoundException
  80. */
  81. protected function getVisiblePath(string $path): string {
  82. $segments = explode('/', trim($path, '/'), 3);
  83. if (!isset($segments[2])) {
  84. throw new NotFoundException('Path not inside visible section');
  85. }
  86. return $segments[2];
  87. }
  88. /**
  89. * @throws NotFoundException
  90. */
  91. protected function getRelevantMessagePart(string $message, string $search): string {
  92. $start = mb_stripos($message, $search);
  93. if ($start === false) {
  94. throw new NotFoundException('Comment section not found');
  95. }
  96. $end = $start + mb_strlen($search);
  97. if ($start <= 25) {
  98. $start = 0;
  99. $prefix = '';
  100. } else {
  101. $start -= 25;
  102. $prefix = '…';
  103. }
  104. if ((mb_strlen($message) - $end) <= 25) {
  105. $end = mb_strlen($message);
  106. $suffix = '';
  107. } else {
  108. $end += 25;
  109. $suffix = '…';
  110. }
  111. return $prefix . mb_substr($message, $start, $end - $start) . $suffix;
  112. }
  113. }