|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Andrew Brown <[email protected]> |
|
6
|
|
|
* @author Christoph Wurst <[email protected]> |
|
7
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
|
8
|
|
|
* @author Lukas Reschke <[email protected]> |
|
9
|
|
|
* @author Morris Jobke <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @license AGPL-3.0 |
|
12
|
|
|
* |
|
13
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
14
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
15
|
|
|
* as published by the Free Software Foundation. |
|
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, version 3, |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace OC\Search\Result; |
|
28
|
|
|
|
|
29
|
|
|
use OCP\Files\FileInfo; |
|
30
|
|
|
use OCP\Files\Folder; |
|
|
|
|
|
|
31
|
|
|
use OCP\IPreview; |
|
32
|
|
|
use OCP\IUserSession; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* A found file |
|
36
|
|
|
* @deprecated 20.0.0 |
|
37
|
|
|
*/ |
|
38
|
|
|
class File extends \OCP\Search\Result { |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Type name; translated in templates |
|
42
|
|
|
* @var string |
|
43
|
|
|
* @deprecated 20.0.0 |
|
44
|
|
|
*/ |
|
45
|
|
|
public $type = 'file'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Path to file |
|
49
|
|
|
* @var string |
|
50
|
|
|
* @deprecated 20.0.0 |
|
51
|
|
|
*/ |
|
52
|
|
|
public $path; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Size, in bytes |
|
56
|
|
|
* @var int |
|
57
|
|
|
* @deprecated 20.0.0 |
|
58
|
|
|
*/ |
|
59
|
|
|
public $size; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Date modified, in human readable form |
|
63
|
|
|
* @var string |
|
64
|
|
|
* @deprecated 20.0.0 |
|
65
|
|
|
*/ |
|
66
|
|
|
public $modified; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* File mime type |
|
70
|
|
|
* @var string |
|
71
|
|
|
* @deprecated 20.0.0 |
|
72
|
|
|
*/ |
|
73
|
|
|
public $mime_type; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* File permissions: |
|
77
|
|
|
* |
|
78
|
|
|
* @var string |
|
79
|
|
|
* @deprecated 20.0.0 |
|
80
|
|
|
*/ |
|
81
|
|
|
public $permissions; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Has a preview |
|
85
|
|
|
* |
|
86
|
|
|
* @var string |
|
87
|
|
|
* @deprecated 20.0.0 |
|
88
|
|
|
*/ |
|
89
|
|
|
public $has_preview; |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Create a new file search result |
|
93
|
|
|
* @param FileInfo $data file data given by provider |
|
94
|
|
|
* @deprecated 20.0.0 |
|
95
|
|
|
*/ |
|
96
|
|
|
public function __construct(FileInfo $data) { |
|
97
|
|
|
$path = $this->getRelativePath($data->getPath()); |
|
98
|
|
|
|
|
99
|
|
|
$info = pathinfo($path); |
|
100
|
|
|
$this->id = $data->getId(); |
|
|
|
|
|
|
101
|
|
|
$this->name = $info['basename']; |
|
|
|
|
|
|
102
|
|
|
$this->link = \OC::$server->getURLGenerator()->linkToRoute( |
|
|
|
|
|
|
103
|
|
|
'files.view.index', |
|
104
|
|
|
[ |
|
105
|
|
|
'dir' => $info['dirname'], |
|
106
|
|
|
'scrollto' => $info['basename'], |
|
107
|
|
|
] |
|
108
|
|
|
); |
|
109
|
|
|
$this->permissions = $data->getPermissions(); |
|
|
|
|
|
|
110
|
|
|
$this->path = $path; |
|
|
|
|
|
|
111
|
|
|
$this->size = $data->getSize(); |
|
|
|
|
|
|
112
|
|
|
$this->modified = $data->getMtime(); |
|
|
|
|
|
|
113
|
|
|
$this->mime_type = $data->getMimetype(); |
|
|
|
|
|
|
114
|
|
|
$this->has_preview = $this->hasPreview($data); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @var Folder $userFolderCache |
|
119
|
|
|
* @deprecated 20.0.0 |
|
120
|
|
|
*/ |
|
121
|
|
|
protected static $userFolderCache = null; |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* converts a path relative to the users files folder |
|
125
|
|
|
* eg /user/files/foo.txt -> /foo.txt |
|
126
|
|
|
* @param string $path |
|
127
|
|
|
* @return string relative path |
|
128
|
|
|
* @deprecated 20.0.0 |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function getRelativePath($path) { |
|
131
|
|
|
if (!isset(self::$userFolderCache)) { |
|
132
|
|
|
$userSession = \OC::$server->get(IUserSession::class); |
|
133
|
|
|
$userID = $userSession->getUser()->getUID(); |
|
|
|
|
|
|
134
|
|
|
self::$userFolderCache = \OC::$server->getUserFolder($userID); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
return self::$userFolderCache->getRelativePath($path); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Is the preview available |
|
141
|
|
|
* @param FileInfo $data |
|
142
|
|
|
* @return bool |
|
143
|
|
|
* @deprecated 20.0.0 |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function hasPreview($data) { |
|
146
|
|
|
$previewManager = \OC::$server->get(IPreview::class); |
|
147
|
|
|
return $previewManager->isAvailable($data); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: