2018-04-18 12:29:49 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com>
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-19 15:16:31 +03:00
|
|
|
* @author Michał Węgrzynek <michal.wegrzynek@malloc.com.pl>
|
2019-12-03 21:57:53 +03:00
|
|
|
*
|
2018-04-18 12:29:49 +03:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-04-18 12:29:49 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Comments\Search;
|
|
|
|
|
|
|
|
use OCP\Comments\IComment;
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Search\Result as BaseResult;
|
|
|
|
|
2020-06-22 13:19:34 +03:00
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0
|
|
|
|
*/
|
2018-04-18 12:29:49 +03:00
|
|
|
class Result extends BaseResult {
|
2020-06-22 13:19:34 +03:00
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0
|
|
|
|
*/
|
2018-04-18 12:29:49 +03:00
|
|
|
public $type = 'comment';
|
2020-06-22 13:19:34 +03:00
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0
|
|
|
|
*/
|
2018-04-18 12:29:49 +03:00
|
|
|
public $comment;
|
2020-06-22 13:19:34 +03:00
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0
|
|
|
|
*/
|
2018-04-18 12:29:49 +03:00
|
|
|
public $authorId;
|
2020-06-22 13:19:34 +03:00
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0
|
|
|
|
*/
|
2018-04-18 12:29:49 +03:00
|
|
|
public $authorName;
|
2020-06-22 13:19:34 +03:00
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0
|
|
|
|
*/
|
2018-04-18 12:29:49 +03:00
|
|
|
public $path;
|
2020-06-22 13:19:34 +03:00
|
|
|
/**
|
|
|
|
* @deprecated 20.0.0
|
|
|
|
*/
|
2018-04-18 12:29:49 +03:00
|
|
|
public $fileName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $search
|
|
|
|
* @param IComment $comment
|
|
|
|
* @param string $authorName
|
|
|
|
* @param string $path
|
|
|
|
* @throws NotFoundException
|
2020-06-22 13:19:34 +03:00
|
|
|
* @deprecated 20.0.0
|
2018-04-18 12:29:49 +03:00
|
|
|
*/
|
|
|
|
public function __construct(string $search,
|
|
|
|
IComment $comment,
|
|
|
|
string $authorName,
|
|
|
|
string $path) {
|
|
|
|
parent::__construct(
|
|
|
|
(int) $comment->getId(),
|
|
|
|
$comment->getMessage()
|
|
|
|
/* @todo , [link to file] */
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->comment = $this->getRelevantMessagePart($comment->getMessage(), $search);
|
|
|
|
$this->authorId = $comment->getActorId();
|
|
|
|
$this->authorName = $authorName;
|
|
|
|
$this->fileName = basename($path);
|
|
|
|
$this->path = $this->getVisiblePath($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @return string
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
protected function getVisiblePath(string $path): string {
|
|
|
|
$segments = explode('/', trim($path, '/'), 3);
|
|
|
|
|
|
|
|
if (!isset($segments[2])) {
|
|
|
|
throw new NotFoundException('Path not inside visible section');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $segments[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $message
|
|
|
|
* @param string $search
|
|
|
|
* @return string
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
protected function getRelevantMessagePart(string $message, string $search): string {
|
2019-12-09 19:22:49 +03:00
|
|
|
$start = mb_stripos($message, $search);
|
2018-04-18 12:29:49 +03:00
|
|
|
if ($start === false) {
|
|
|
|
throw new NotFoundException('Comment section not found');
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:22:49 +03:00
|
|
|
$end = $start + mb_strlen($search);
|
2018-04-18 12:29:49 +03:00
|
|
|
|
|
|
|
if ($start <= 25) {
|
|
|
|
$start = 0;
|
|
|
|
$prefix = '';
|
|
|
|
} else {
|
|
|
|
$start -= 25;
|
|
|
|
$prefix = '…';
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:22:49 +03:00
|
|
|
if ((mb_strlen($message) - $end) <= 25) {
|
|
|
|
$end = mb_strlen($message);
|
2018-04-18 12:29:49 +03:00
|
|
|
$suffix = '';
|
|
|
|
} else {
|
|
|
|
$end += 25;
|
|
|
|
$suffix = '…';
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:22:49 +03:00
|
|
|
return $prefix . mb_substr($message, $start, $end - $start) . $suffix;
|
2018-04-18 12:29:49 +03:00
|
|
|
}
|
|
|
|
}
|