1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @copyright Copyright (c) 2018 Joas Schilling <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @author Joas Schilling <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @license GNU AGPL version 3 or any later version |
11
|
|
|
* |
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
15
|
|
|
* License, or (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU Affero General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace OCA\Comments\Search; |
28
|
|
|
|
29
|
|
|
use OCP\Comments\IComment; |
30
|
|
|
use OCP\Files\Folder; |
31
|
|
|
use OCP\Files\Node; |
32
|
|
|
use OCP\Files\NotFoundException; |
33
|
|
|
use OCP\IUser; |
34
|
|
|
use OCP\Search\Provider; |
|
|
|
|
35
|
|
|
use function count; |
36
|
|
|
|
37
|
|
|
class LegacyProvider extends Provider { |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Search for $query |
41
|
|
|
* |
42
|
|
|
* @param string $query |
43
|
|
|
* @return array An array of OCP\Search\Result's |
44
|
|
|
* @since 7.0.0 |
45
|
|
|
*/ |
46
|
|
|
public function search($query): array { |
47
|
|
|
$cm = \OC::$server->getCommentsManager(); |
48
|
|
|
$us = \OC::$server->getUserSession(); |
49
|
|
|
|
50
|
|
|
$user = $us->getUser(); |
51
|
|
|
if (!$user instanceof IUser) { |
|
|
|
|
52
|
|
|
return []; |
53
|
|
|
} |
54
|
|
|
$uf = \OC::$server->getUserFolder($user->getUID()); |
55
|
|
|
|
56
|
|
|
if ($uf === null) { |
57
|
|
|
return []; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$result = []; |
61
|
|
|
$numComments = 50; |
62
|
|
|
$offset = 0; |
63
|
|
|
|
64
|
|
|
while (count($result) < $numComments) { |
65
|
|
|
/** @var IComment[] $comments */ |
66
|
|
|
$comments = $cm->search($query, 'files', '', 'comment', $offset, $numComments); |
67
|
|
|
|
68
|
|
|
foreach ($comments as $comment) { |
69
|
|
|
if ($comment->getActorType() !== 'users') { |
70
|
|
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$displayName = $cm->resolveDisplayName('user', $comment->getActorId()); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
$file = $this->getFileForComment($uf, $comment); |
77
|
|
|
$result[] = new Result($query, |
|
|
|
|
78
|
|
|
$comment, |
79
|
|
|
$displayName, |
80
|
|
|
$file->getPath() |
81
|
|
|
); |
82
|
|
|
} catch (NotFoundException $e) { |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (count($comments) < $numComments) { |
88
|
|
|
// Didn't find more comments when we tried to get, so there are no more comments. |
89
|
|
|
return $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$offset += $numComments; |
93
|
|
|
$numComments = 50 - count($result); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $result; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Folder $userFolder |
101
|
|
|
* @param IComment $comment |
102
|
|
|
* @return Node |
103
|
|
|
* @throws NotFoundException |
104
|
|
|
*/ |
105
|
|
|
protected function getFileForComment(Folder $userFolder, IComment $comment): Node { |
106
|
|
|
$nodes = $userFolder->getById((int) $comment->getObjectId()); |
107
|
|
|
if (empty($nodes)) { |
108
|
|
|
throw new NotFoundException('File not found'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return array_shift($nodes); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: