@@ -45,247 +45,247 @@ |
||
45 | 45 | final class SearchOption implements ISearchOption, JsonSerializable { |
46 | 46 | |
47 | 47 | |
48 | - /** @var string */ |
|
49 | - private $name = ''; |
|
50 | - |
|
51 | - /** @var string */ |
|
52 | - private $title = ''; |
|
53 | - |
|
54 | - /** @var string */ |
|
55 | - private $type = ''; |
|
56 | - |
|
57 | - /** @var string */ |
|
58 | - private $size = ''; |
|
59 | - |
|
60 | - /** @var string */ |
|
61 | - private $placeholder = ''; |
|
62 | - |
|
63 | - |
|
64 | - /** |
|
65 | - * * |
|
66 | - * |
|
67 | - * The array can be empty in case no search options are available. |
|
68 | - * The format of the array must be like this: |
|
69 | - * |
|
70 | - * [ |
|
71 | - * 'panel' => [ |
|
72 | - * 'options' => [ |
|
73 | - * OPTION1, |
|
74 | - * OPTION2, |
|
75 | - * OPTION3 |
|
76 | - * ] |
|
77 | - * ], |
|
78 | - * 'navigation' => [ |
|
79 | - * 'icon' => 'css-class-of-the-icon', |
|
80 | - * 'options' => [ |
|
81 | - * OPTION1, |
|
82 | - * OPTION2, |
|
83 | - * OPTION3 |
|
84 | - * ] |
|
85 | - * ] |
|
86 | - * ] |
|
87 | - * |
|
88 | - * - PANEL contains entries that will be displayed in the app itself, when |
|
89 | - * a search is initiated. |
|
90 | - * - NAVIGATION contains entries that will be available when using the |
|
91 | - * FullTextSearch navigation page |
|
92 | - * - OPTION is an element that define each option available to the user. |
|
93 | - * |
|
94 | - * The format for the options must be like this: |
|
95 | - * |
|
96 | - * [ |
|
97 | - * 'name' => 'name_of_the_option', |
|
98 | - * 'title' => 'Name displayed in the panel', |
|
99 | - * 'type' => '', |
|
100 | - * 'size' => '' (optional), |
|
101 | - * 'placeholder' => '' (optional) |
|
102 | - * ] |
|
103 | - * |
|
104 | - * - NAME is the variable name that is sent to the IFullTextSearchProvider |
|
105 | - * when a ISearchRequest is requested. (keys in the array returned by the |
|
106 | - * ISearchRequest->getOptions()) |
|
107 | - * - TYPE can be 'input' or 'checkbox' |
|
108 | - * - SIZE is only used in case TYPE='input', default is 'large' but can be |
|
109 | - * 'small' |
|
110 | - * - PLACEHOLDER is only used in case TYPE='input', default is empty. |
|
111 | - */ |
|
112 | - |
|
113 | - /** |
|
114 | - * ISearchOption constructor. |
|
115 | - * |
|
116 | - * Some value can be setduring the creation of the object. |
|
117 | - * |
|
118 | - * @since 15.0.0 |
|
119 | - * |
|
120 | - * @param string $name |
|
121 | - * @param string $title |
|
122 | - * @param string $type |
|
123 | - * @param string $size |
|
124 | - * @param string $placeholder |
|
125 | - */ |
|
126 | - public function __construct(string $name = '', string $title = '', string $type = '', string $size = '', string $placeholder = '') { |
|
127 | - $this->name = $name; |
|
128 | - $this->title = $title; |
|
129 | - $this->type = $type; |
|
130 | - $this->size = $size; |
|
131 | - $this->placeholder = $placeholder; |
|
132 | - } |
|
133 | - |
|
134 | - |
|
135 | - /** |
|
136 | - * Set the name/key of the option. |
|
137 | - * The string should only contains alphanumerical chars and underscore. |
|
138 | - * The key can be retrieve when using ISearchRequest::getOption |
|
139 | - * |
|
140 | - * @see ISearchRequest::getOption |
|
141 | - * |
|
142 | - * @since 15.0.0 |
|
143 | - * |
|
144 | - * @param string $name |
|
145 | - * |
|
146 | - * @return ISearchOption |
|
147 | - */ |
|
148 | - public function setName(string $name): ISearchOption { |
|
149 | - $this->name = $name; |
|
150 | - |
|
151 | - return $this; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Get the name/key of the option. |
|
156 | - * |
|
157 | - * @since 15.0.0 |
|
158 | - * |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - public function getName(): string { |
|
162 | - return $this->name; |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * Set the title/display name of the option. |
|
168 | - * |
|
169 | - * @since 15.0.0 |
|
170 | - * |
|
171 | - * @param string $title |
|
172 | - * |
|
173 | - * @return ISearchOption |
|
174 | - */ |
|
175 | - public function setTitle(string $title): ISearchOption { |
|
176 | - $this->title = $title; |
|
177 | - |
|
178 | - return $this; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Get the title of the option. |
|
183 | - * |
|
184 | - * @since 15.0.0 |
|
185 | - * |
|
186 | - * @return string |
|
187 | - */ |
|
188 | - public function getTitle(): string { |
|
189 | - return $this->title; |
|
190 | - } |
|
191 | - |
|
192 | - |
|
193 | - /** |
|
194 | - * Set the type of the option. |
|
195 | - * $type can be ISearchOption::CHECKBOX or ISearchOption::INPUT |
|
196 | - * |
|
197 | - * @since 15.0.0 |
|
198 | - * |
|
199 | - * @param string $type |
|
200 | - * |
|
201 | - * @return ISearchOption |
|
202 | - */ |
|
203 | - public function setType(string $type): ISearchOption { |
|
204 | - $this->type = $type; |
|
205 | - |
|
206 | - return $this; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Get the type of the option. |
|
211 | - * |
|
212 | - * @since 15.0.0 |
|
213 | - * |
|
214 | - * @return string |
|
215 | - */ |
|
216 | - public function getType(): string { |
|
217 | - return $this->type; |
|
218 | - } |
|
219 | - |
|
220 | - |
|
221 | - /** |
|
222 | - * In case of Type is INPUT, set the size of the input field. |
|
223 | - * Value can be ISearchOption::INPUT_SMALL or not defined. |
|
224 | - * |
|
225 | - * @since 15.0.0 |
|
226 | - * |
|
227 | - * @param string $size |
|
228 | - * |
|
229 | - * @return ISearchOption |
|
230 | - */ |
|
231 | - public function setSize(string $size): ISearchOption { |
|
232 | - $this->size = $size; |
|
233 | - |
|
234 | - return $this; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Get the size of the INPUT. |
|
239 | - * |
|
240 | - * @since 15.0.0 |
|
241 | - * |
|
242 | - * @return string |
|
243 | - */ |
|
244 | - public function getSize(): string { |
|
245 | - return $this->size; |
|
246 | - } |
|
247 | - |
|
248 | - |
|
249 | - /** |
|
250 | - * In case of Type is , set the placeholder to be displayed in the input |
|
251 | - * field. |
|
252 | - * |
|
253 | - * @since 15.0.0 |
|
254 | - * |
|
255 | - * @param string $placeholder |
|
256 | - * |
|
257 | - * @return ISearchOption |
|
258 | - */ |
|
259 | - public function setPlaceholder(string $placeholder): ISearchOption { |
|
260 | - $this->placeholder = $placeholder; |
|
261 | - |
|
262 | - return $this; |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Get the placeholder. |
|
267 | - * |
|
268 | - * @since 15.0.0 |
|
269 | - * |
|
270 | - * @return string |
|
271 | - */ |
|
272 | - public function getPlaceholder(): string { |
|
273 | - return $this->placeholder; |
|
274 | - } |
|
275 | - |
|
276 | - |
|
277 | - /** |
|
278 | - * @since 15.0.0 |
|
279 | - * |
|
280 | - * @return array |
|
281 | - */ |
|
282 | - public function jsonSerialize(): array { |
|
283 | - return [ |
|
284 | - 'name' => $this->getName(), |
|
285 | - 'title' => $this->getTitle(), |
|
286 | - 'type' => $this->getType(), |
|
287 | - 'size' => $this->getSize(), |
|
288 | - 'placeholder' => $this->getPlaceholder() |
|
289 | - ]; |
|
290 | - } |
|
48 | + /** @var string */ |
|
49 | + private $name = ''; |
|
50 | + |
|
51 | + /** @var string */ |
|
52 | + private $title = ''; |
|
53 | + |
|
54 | + /** @var string */ |
|
55 | + private $type = ''; |
|
56 | + |
|
57 | + /** @var string */ |
|
58 | + private $size = ''; |
|
59 | + |
|
60 | + /** @var string */ |
|
61 | + private $placeholder = ''; |
|
62 | + |
|
63 | + |
|
64 | + /** |
|
65 | + * * |
|
66 | + * |
|
67 | + * The array can be empty in case no search options are available. |
|
68 | + * The format of the array must be like this: |
|
69 | + * |
|
70 | + * [ |
|
71 | + * 'panel' => [ |
|
72 | + * 'options' => [ |
|
73 | + * OPTION1, |
|
74 | + * OPTION2, |
|
75 | + * OPTION3 |
|
76 | + * ] |
|
77 | + * ], |
|
78 | + * 'navigation' => [ |
|
79 | + * 'icon' => 'css-class-of-the-icon', |
|
80 | + * 'options' => [ |
|
81 | + * OPTION1, |
|
82 | + * OPTION2, |
|
83 | + * OPTION3 |
|
84 | + * ] |
|
85 | + * ] |
|
86 | + * ] |
|
87 | + * |
|
88 | + * - PANEL contains entries that will be displayed in the app itself, when |
|
89 | + * a search is initiated. |
|
90 | + * - NAVIGATION contains entries that will be available when using the |
|
91 | + * FullTextSearch navigation page |
|
92 | + * - OPTION is an element that define each option available to the user. |
|
93 | + * |
|
94 | + * The format for the options must be like this: |
|
95 | + * |
|
96 | + * [ |
|
97 | + * 'name' => 'name_of_the_option', |
|
98 | + * 'title' => 'Name displayed in the panel', |
|
99 | + * 'type' => '', |
|
100 | + * 'size' => '' (optional), |
|
101 | + * 'placeholder' => '' (optional) |
|
102 | + * ] |
|
103 | + * |
|
104 | + * - NAME is the variable name that is sent to the IFullTextSearchProvider |
|
105 | + * when a ISearchRequest is requested. (keys in the array returned by the |
|
106 | + * ISearchRequest->getOptions()) |
|
107 | + * - TYPE can be 'input' or 'checkbox' |
|
108 | + * - SIZE is only used in case TYPE='input', default is 'large' but can be |
|
109 | + * 'small' |
|
110 | + * - PLACEHOLDER is only used in case TYPE='input', default is empty. |
|
111 | + */ |
|
112 | + |
|
113 | + /** |
|
114 | + * ISearchOption constructor. |
|
115 | + * |
|
116 | + * Some value can be setduring the creation of the object. |
|
117 | + * |
|
118 | + * @since 15.0.0 |
|
119 | + * |
|
120 | + * @param string $name |
|
121 | + * @param string $title |
|
122 | + * @param string $type |
|
123 | + * @param string $size |
|
124 | + * @param string $placeholder |
|
125 | + */ |
|
126 | + public function __construct(string $name = '', string $title = '', string $type = '', string $size = '', string $placeholder = '') { |
|
127 | + $this->name = $name; |
|
128 | + $this->title = $title; |
|
129 | + $this->type = $type; |
|
130 | + $this->size = $size; |
|
131 | + $this->placeholder = $placeholder; |
|
132 | + } |
|
133 | + |
|
134 | + |
|
135 | + /** |
|
136 | + * Set the name/key of the option. |
|
137 | + * The string should only contains alphanumerical chars and underscore. |
|
138 | + * The key can be retrieve when using ISearchRequest::getOption |
|
139 | + * |
|
140 | + * @see ISearchRequest::getOption |
|
141 | + * |
|
142 | + * @since 15.0.0 |
|
143 | + * |
|
144 | + * @param string $name |
|
145 | + * |
|
146 | + * @return ISearchOption |
|
147 | + */ |
|
148 | + public function setName(string $name): ISearchOption { |
|
149 | + $this->name = $name; |
|
150 | + |
|
151 | + return $this; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Get the name/key of the option. |
|
156 | + * |
|
157 | + * @since 15.0.0 |
|
158 | + * |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + public function getName(): string { |
|
162 | + return $this->name; |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * Set the title/display name of the option. |
|
168 | + * |
|
169 | + * @since 15.0.0 |
|
170 | + * |
|
171 | + * @param string $title |
|
172 | + * |
|
173 | + * @return ISearchOption |
|
174 | + */ |
|
175 | + public function setTitle(string $title): ISearchOption { |
|
176 | + $this->title = $title; |
|
177 | + |
|
178 | + return $this; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Get the title of the option. |
|
183 | + * |
|
184 | + * @since 15.0.0 |
|
185 | + * |
|
186 | + * @return string |
|
187 | + */ |
|
188 | + public function getTitle(): string { |
|
189 | + return $this->title; |
|
190 | + } |
|
191 | + |
|
192 | + |
|
193 | + /** |
|
194 | + * Set the type of the option. |
|
195 | + * $type can be ISearchOption::CHECKBOX or ISearchOption::INPUT |
|
196 | + * |
|
197 | + * @since 15.0.0 |
|
198 | + * |
|
199 | + * @param string $type |
|
200 | + * |
|
201 | + * @return ISearchOption |
|
202 | + */ |
|
203 | + public function setType(string $type): ISearchOption { |
|
204 | + $this->type = $type; |
|
205 | + |
|
206 | + return $this; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Get the type of the option. |
|
211 | + * |
|
212 | + * @since 15.0.0 |
|
213 | + * |
|
214 | + * @return string |
|
215 | + */ |
|
216 | + public function getType(): string { |
|
217 | + return $this->type; |
|
218 | + } |
|
219 | + |
|
220 | + |
|
221 | + /** |
|
222 | + * In case of Type is INPUT, set the size of the input field. |
|
223 | + * Value can be ISearchOption::INPUT_SMALL or not defined. |
|
224 | + * |
|
225 | + * @since 15.0.0 |
|
226 | + * |
|
227 | + * @param string $size |
|
228 | + * |
|
229 | + * @return ISearchOption |
|
230 | + */ |
|
231 | + public function setSize(string $size): ISearchOption { |
|
232 | + $this->size = $size; |
|
233 | + |
|
234 | + return $this; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Get the size of the INPUT. |
|
239 | + * |
|
240 | + * @since 15.0.0 |
|
241 | + * |
|
242 | + * @return string |
|
243 | + */ |
|
244 | + public function getSize(): string { |
|
245 | + return $this->size; |
|
246 | + } |
|
247 | + |
|
248 | + |
|
249 | + /** |
|
250 | + * In case of Type is , set the placeholder to be displayed in the input |
|
251 | + * field. |
|
252 | + * |
|
253 | + * @since 15.0.0 |
|
254 | + * |
|
255 | + * @param string $placeholder |
|
256 | + * |
|
257 | + * @return ISearchOption |
|
258 | + */ |
|
259 | + public function setPlaceholder(string $placeholder): ISearchOption { |
|
260 | + $this->placeholder = $placeholder; |
|
261 | + |
|
262 | + return $this; |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Get the placeholder. |
|
267 | + * |
|
268 | + * @since 15.0.0 |
|
269 | + * |
|
270 | + * @return string |
|
271 | + */ |
|
272 | + public function getPlaceholder(): string { |
|
273 | + return $this->placeholder; |
|
274 | + } |
|
275 | + |
|
276 | + |
|
277 | + /** |
|
278 | + * @since 15.0.0 |
|
279 | + * |
|
280 | + * @return array |
|
281 | + */ |
|
282 | + public function jsonSerialize(): array { |
|
283 | + return [ |
|
284 | + 'name' => $this->getName(), |
|
285 | + 'title' => $this->getTitle(), |
|
286 | + 'type' => $this->getType(), |
|
287 | + 'size' => $this->getSize(), |
|
288 | + 'placeholder' => $this->getPlaceholder() |
|
289 | + ]; |
|
290 | + } |
|
291 | 291 | } |
@@ -389,7 +389,7 @@ |
||
389 | 389 | foreach ($ak as $source) { |
390 | 390 | $tags = $this->subTags[$source]; |
391 | 391 | foreach ($tags as $tag) { |
392 | - $subTags[] = $source . '_' . $tag; |
|
392 | + $subTags[] = $source.'_'.$tag; |
|
393 | 393 | } |
394 | 394 | } |
395 | 395 |
@@ -49,940 +49,940 @@ |
||
49 | 49 | class IndexDocument implements IIndexDocument, JsonSerializable { |
50 | 50 | |
51 | 51 | |
52 | - /** @var string */ |
|
53 | - protected $id = ''; |
|
52 | + /** @var string */ |
|
53 | + protected $id = ''; |
|
54 | 54 | |
55 | - /** @var string */ |
|
56 | - protected $providerId = ''; |
|
55 | + /** @var string */ |
|
56 | + protected $providerId = ''; |
|
57 | 57 | |
58 | - /** @var DocumentAccess */ |
|
59 | - protected $access; |
|
60 | - |
|
61 | - /** @var IIndex */ |
|
62 | - protected $index; |
|
63 | - |
|
64 | - /** @var int */ |
|
65 | - protected $modifiedTime = 0; |
|
66 | - |
|
67 | - /** @var string */ |
|
68 | - protected $source = ''; |
|
69 | - |
|
70 | - /** @var array */ |
|
71 | - protected $tags = []; |
|
72 | - |
|
73 | - /** @var array */ |
|
74 | - protected $metaTags = []; |
|
75 | - |
|
76 | - /** @var array */ |
|
77 | - protected $subTags = []; |
|
78 | - |
|
79 | - /** @var string */ |
|
80 | - protected $title = ''; |
|
81 | - |
|
82 | - /** @var string */ |
|
83 | - protected $content = ''; |
|
84 | - |
|
85 | - /** @var string */ |
|
86 | - protected $hash = ''; |
|
87 | - |
|
88 | - /** @var array */ |
|
89 | - protected $parts = []; |
|
90 | - |
|
91 | - /** @var string */ |
|
92 | - protected $link = ''; |
|
93 | - |
|
94 | - /** @var array */ |
|
95 | - protected $more = []; |
|
96 | - |
|
97 | - /** @var array */ |
|
98 | - protected $excerpts = []; |
|
99 | - |
|
100 | - /** @var string */ |
|
101 | - protected $score = ''; |
|
102 | - |
|
103 | - /** @var array */ |
|
104 | - protected $info = []; |
|
105 | - |
|
106 | - /** @var int */ |
|
107 | - protected $contentEncoded = 0; |
|
108 | - |
|
109 | - |
|
110 | - /** |
|
111 | - * IIndexDocument constructor. |
|
112 | - * |
|
113 | - * On creation, we assure the uniqueness of the object using the providerId |
|
114 | - * and the Id of the original document. |
|
115 | - * |
|
116 | - * @since 15.0.0 |
|
117 | - * |
|
118 | - * @param string $providerId |
|
119 | - * @param string $documentId |
|
120 | - */ |
|
121 | - public function __construct(string $providerId, string $documentId) { |
|
122 | - $this->providerId = $providerId; |
|
123 | - $this->id = $documentId; |
|
124 | - } |
|
125 | - |
|
126 | - |
|
127 | - /** |
|
128 | - * Returns the Id of the original document. |
|
129 | - * |
|
130 | - * @since 15.0.0 |
|
131 | - * |
|
132 | - * @return string |
|
133 | - */ |
|
134 | - final public function getId(): string { |
|
135 | - return $this->id; |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * Returns the Id of the provider. |
|
141 | - * |
|
142 | - * @since 15.0.0 |
|
143 | - * |
|
144 | - * @return string |
|
145 | - */ |
|
146 | - final public function getProviderId(): string { |
|
147 | - return $this->providerId; |
|
148 | - } |
|
149 | - |
|
150 | - |
|
151 | - /** |
|
152 | - * Set the Index related to the IIndexDocument. |
|
153 | - * |
|
154 | - * @see IIndex |
|
155 | - * |
|
156 | - * @since 15.0.0 |
|
157 | - * |
|
158 | - * @param IIndex $index |
|
159 | - * |
|
160 | - * @return IIndexDocument |
|
161 | - */ |
|
162 | - final public function setIndex(IIndex $index): IIndexDocument { |
|
163 | - $this->index = $index; |
|
164 | - |
|
165 | - return $this; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Get the Index. |
|
170 | - * |
|
171 | - * @since 15.0.0 |
|
172 | - * |
|
173 | - * @return IIndex |
|
174 | - */ |
|
175 | - final public function getIndex(): IIndex { |
|
176 | - return $this->index; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * return if Index is defined. |
|
181 | - * |
|
182 | - * @since 16.0.0 |
|
183 | - * |
|
184 | - * @return bool |
|
185 | - */ |
|
186 | - final public function hasIndex(): bool { |
|
187 | - return ($this->index !== null); |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - /** |
|
192 | - * Set the modified time of the original document. |
|
193 | - * |
|
194 | - * @since 15.0.0 |
|
195 | - * |
|
196 | - * @param int $modifiedTime |
|
197 | - * |
|
198 | - * @return IIndexDocument |
|
199 | - */ |
|
200 | - final public function setModifiedTime(int $modifiedTime): IIndexDocument { |
|
201 | - $this->modifiedTime = $modifiedTime; |
|
202 | - |
|
203 | - return $this; |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Get the modified time of the original document. |
|
208 | - * |
|
209 | - * @since 15.0.0 |
|
210 | - * |
|
211 | - * @return int |
|
212 | - */ |
|
213 | - final public function getModifiedTime(): int { |
|
214 | - return $this->modifiedTime; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Check if the original document of the IIndexDocument is older than $time. |
|
219 | - * |
|
220 | - * @since 15.0.0 |
|
221 | - * |
|
222 | - * @param int $time |
|
223 | - * |
|
224 | - * @return bool |
|
225 | - */ |
|
226 | - final public function isOlderThan(int $time): bool { |
|
227 | - return ($this->modifiedTime < $time); |
|
228 | - } |
|
229 | - |
|
230 | - |
|
231 | - /** |
|
232 | - * Set the read rights of the original document using a IDocumentAccess. |
|
233 | - * |
|
234 | - * @see IDocumentAccess |
|
235 | - * |
|
236 | - * @since 15.0.0 |
|
237 | - * |
|
238 | - * @param IDocumentAccess $access |
|
239 | - * |
|
240 | - * @return $this |
|
241 | - */ |
|
242 | - final public function setAccess(IDocumentAccess $access): IIndexDocument { |
|
243 | - $this->access = $access; |
|
244 | - |
|
245 | - return $this; |
|
246 | - } |
|
247 | - |
|
248 | - /** |
|
249 | - * Get the IDocumentAccess related to the original document. |
|
250 | - * |
|
251 | - * @since 15.0.0 |
|
252 | - * |
|
253 | - * @return IDocumentAccess |
|
254 | - */ |
|
255 | - final public function getAccess(): IDocumentAccess { |
|
256 | - return $this->access; |
|
257 | - } |
|
258 | - |
|
259 | - |
|
260 | - /** |
|
261 | - * Add a tag to the list. |
|
262 | - * |
|
263 | - * @since 15.0.0 |
|
264 | - * |
|
265 | - * @param string $tag |
|
266 | - * |
|
267 | - * @return IIndexDocument |
|
268 | - */ |
|
269 | - final public function addTag(string $tag): IIndexDocument { |
|
270 | - $this->tags[] = $tag; |
|
271 | - |
|
272 | - return $this; |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * Set the list of tags assigned to the original document. |
|
277 | - * |
|
278 | - * @since 15.0.0 |
|
279 | - * |
|
280 | - * @param array $tags |
|
281 | - * |
|
282 | - * @return IIndexDocument |
|
283 | - */ |
|
284 | - final public function setTags(array $tags): IIndexDocument { |
|
285 | - $this->tags = $tags; |
|
286 | - |
|
287 | - return $this; |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Get the list of tags assigned to the original document. |
|
292 | - * |
|
293 | - * @since 15.0.0 |
|
294 | - * |
|
295 | - * @return array |
|
296 | - */ |
|
297 | - final public function getTags(): array { |
|
298 | - return $this->tags; |
|
299 | - } |
|
300 | - |
|
301 | - |
|
302 | - /** |
|
303 | - * Add a meta tag to the list. |
|
304 | - * |
|
305 | - * @since 15.0.0 |
|
306 | - * |
|
307 | - * @param string $tag |
|
308 | - * |
|
309 | - * @return IIndexDocument |
|
310 | - */ |
|
311 | - final public function addMetaTag(string $tag): IIndexDocument { |
|
312 | - $this->metaTags[] = $tag; |
|
313 | - |
|
314 | - return $this; |
|
315 | - } |
|
316 | - |
|
317 | - /** |
|
318 | - * Set the list of meta tags assigned to the original document. |
|
319 | - * |
|
320 | - * @since 15.0.0 |
|
321 | - * |
|
322 | - * @param array $tags |
|
323 | - * |
|
324 | - * @return IIndexDocument |
|
325 | - */ |
|
326 | - final public function setMetaTags(array $tags): IIndexDocument { |
|
327 | - $this->metaTags = $tags; |
|
328 | - |
|
329 | - return $this; |
|
330 | - } |
|
331 | - |
|
332 | - /** |
|
333 | - * Get the list of meta tags assigned to the original document. |
|
334 | - * |
|
335 | - * @since 15.0.0 |
|
336 | - * |
|
337 | - * @return array |
|
338 | - */ |
|
339 | - final public function getMetaTags(): array { |
|
340 | - return $this->metaTags; |
|
341 | - } |
|
342 | - |
|
343 | - |
|
344 | - /** |
|
345 | - * Add a sub tag to the list. |
|
346 | - * |
|
347 | - * @since 15.0.0 |
|
348 | - * |
|
349 | - * @param string $sub |
|
350 | - * @param string $tag |
|
351 | - * |
|
352 | - * @return IIndexDocument |
|
353 | - */ |
|
354 | - final public function addSubTag(string $sub, string $tag): IIndexDocument { |
|
355 | - if (!array_key_exists($sub, $this->subTags)) { |
|
356 | - $this->subTags[$sub] = []; |
|
357 | - } |
|
358 | - |
|
359 | - $this->subTags[$sub][] = $tag; |
|
360 | - |
|
361 | - return $this; |
|
362 | - } |
|
363 | - |
|
364 | - |
|
365 | - /** |
|
366 | - * Set the list of sub tags assigned to the original document. |
|
367 | - * |
|
368 | - * @since 15.0.0 |
|
369 | - * |
|
370 | - * @param array $tags |
|
371 | - * |
|
372 | - * @return IIndexDocument |
|
373 | - */ |
|
374 | - final public function setSubTags(array $tags): IIndexDocument { |
|
375 | - $this->subTags = $tags; |
|
376 | - |
|
377 | - return $this; |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * Get the list of sub tags assigned to the original document. |
|
382 | - * If $formatted is true, the result will be formatted in a one |
|
383 | - * dimensional array. |
|
384 | - * |
|
385 | - * @since 15.0.0 |
|
386 | - * |
|
387 | - * @param bool $formatted |
|
388 | - * |
|
389 | - * @return array |
|
390 | - */ |
|
391 | - final public function getSubTags(bool $formatted = false): array { |
|
392 | - if ($formatted === false) { |
|
393 | - return $this->subTags; |
|
394 | - } |
|
395 | - |
|
396 | - $subTags = []; |
|
397 | - $ak = array_keys($this->subTags); |
|
398 | - foreach ($ak as $source) { |
|
399 | - $tags = $this->subTags[$source]; |
|
400 | - foreach ($tags as $tag) { |
|
401 | - $subTags[] = $source . '_' . $tag; |
|
402 | - } |
|
403 | - } |
|
404 | - |
|
405 | - return $subTags; |
|
406 | - } |
|
407 | - |
|
408 | - |
|
409 | - /** |
|
410 | - * Set the source of the original document. |
|
411 | - * |
|
412 | - * @since 15.0.0 |
|
413 | - * |
|
414 | - * @param string $source |
|
415 | - * |
|
416 | - * @return IIndexDocument |
|
417 | - */ |
|
418 | - final public function setSource(string $source): IIndexDocument { |
|
419 | - $this->source = $source; |
|
420 | - |
|
421 | - return $this; |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * Get the source of the original document. |
|
426 | - * |
|
427 | - * @since 15.0.0 |
|
428 | - * |
|
429 | - * @return string |
|
430 | - */ |
|
431 | - final public function getSource(): string { |
|
432 | - return $this->source; |
|
433 | - } |
|
434 | - |
|
435 | - |
|
436 | - /** |
|
437 | - * Set the title of the original document. |
|
438 | - * |
|
439 | - * @since 15.0.0 |
|
440 | - * |
|
441 | - * @param string $title |
|
442 | - * |
|
443 | - * @return IIndexDocument |
|
444 | - */ |
|
445 | - final public function setTitle(string $title): IIndexDocument { |
|
446 | - $this->title = $title; |
|
447 | - |
|
448 | - return $this; |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * Get the title of the original document. |
|
453 | - * |
|
454 | - * @since 15.0.0 |
|
455 | - * |
|
456 | - * @return string |
|
457 | - */ |
|
458 | - final public function getTitle(): string { |
|
459 | - return $this->title; |
|
460 | - } |
|
461 | - |
|
462 | - |
|
463 | - /** |
|
464 | - * Set the content of the document. |
|
465 | - * $encoded can be NOT_ENCODED or ENCODED_BASE64 if the content is raw or |
|
466 | - * encoded in base64. |
|
467 | - * |
|
468 | - * @since 15.0.0 |
|
469 | - * |
|
470 | - * @param string $content |
|
471 | - * @param int $encoded |
|
472 | - * |
|
473 | - * @return IIndexDocument |
|
474 | - */ |
|
475 | - final public function setContent(string $content, int $encoded = 0): IIndexDocument { |
|
476 | - $this->content = $content; |
|
477 | - $this->contentEncoded = $encoded; |
|
478 | - |
|
479 | - return $this; |
|
480 | - } |
|
481 | - |
|
482 | - /** |
|
483 | - * Get the content of the original document. |
|
484 | - * |
|
485 | - * @since 15.0.0 |
|
486 | - * |
|
487 | - * @return string |
|
488 | - */ |
|
489 | - final public function getContent(): string { |
|
490 | - return $this->content; |
|
491 | - } |
|
492 | - |
|
493 | - /** |
|
494 | - * Returns the type of the encoding on the content. |
|
495 | - * |
|
496 | - * @since 15.0.0 |
|
497 | - * |
|
498 | - * @return int |
|
499 | - */ |
|
500 | - final public function isContentEncoded(): int { |
|
501 | - return $this->contentEncoded; |
|
502 | - } |
|
503 | - |
|
504 | - /** |
|
505 | - * Return the size of the content. |
|
506 | - * |
|
507 | - * @since 15.0.0 |
|
508 | - * |
|
509 | - * @return int |
|
510 | - */ |
|
511 | - final public function getContentSize(): int { |
|
512 | - return strlen($this->getContent()); |
|
513 | - } |
|
514 | - |
|
515 | - |
|
516 | - /** |
|
517 | - * Generate an hash, based on the content of the original document. |
|
518 | - * |
|
519 | - * @since 15.0.0 |
|
520 | - * |
|
521 | - * @return IIndexDocument |
|
522 | - */ |
|
523 | - final public function initHash(): IIndexDocument { |
|
524 | - if ($this->getContent() === '' || is_null($this->getContent())) { |
|
525 | - return $this; |
|
526 | - } |
|
527 | - |
|
528 | - $this->hash = hash("md5", $this->getContent()); |
|
529 | - |
|
530 | - return $this; |
|
531 | - } |
|
532 | - |
|
533 | - /** |
|
534 | - * Set the hash of the original document. |
|
535 | - * |
|
536 | - * @since 15.0.0 |
|
537 | - * |
|
538 | - * @param string $hash |
|
539 | - * |
|
540 | - * @return IIndexDocument |
|
541 | - */ |
|
542 | - final public function setHash(string $hash): IIndexDocument { |
|
543 | - $this->hash = $hash; |
|
544 | - |
|
545 | - return $this; |
|
546 | - } |
|
547 | - |
|
548 | - /** |
|
549 | - * Get the hash of the original document. |
|
550 | - * |
|
551 | - * @since 15.0.0 |
|
552 | - * |
|
553 | - * @return string |
|
554 | - */ |
|
555 | - final public function getHash(): string { |
|
556 | - return $this->hash; |
|
557 | - } |
|
558 | - |
|
559 | - |
|
560 | - /** |
|
561 | - * Add a part, identified by a string, and its content. |
|
562 | - * |
|
563 | - * It is strongly advised to use alphanumerical chars with no space in the |
|
564 | - * $part string. |
|
565 | - * |
|
566 | - * @since 15.0.0 |
|
567 | - * |
|
568 | - * @param string $part |
|
569 | - * @param string $content |
|
570 | - * |
|
571 | - * @return IIndexDocument |
|
572 | - */ |
|
573 | - final public function addPart(string $part, string $content): IIndexDocument { |
|
574 | - $this->parts[$part] = $content; |
|
575 | - |
|
576 | - return $this; |
|
577 | - } |
|
578 | - |
|
579 | - /** |
|
580 | - * Set all parts and their content. |
|
581 | - * |
|
582 | - * @since 15.0.0 |
|
583 | - * |
|
584 | - * @param array $parts |
|
585 | - * |
|
586 | - * @return IIndexDocument |
|
587 | - */ |
|
588 | - final public function setParts(array $parts): IIndexDocument { |
|
589 | - $this->parts = $parts; |
|
590 | - |
|
591 | - return $this; |
|
592 | - } |
|
593 | - |
|
594 | - /** |
|
595 | - * Get all parts of the IIndexDocument. |
|
596 | - * |
|
597 | - * @since 15.0.0 |
|
598 | - * |
|
599 | - * @return array |
|
600 | - */ |
|
601 | - final public function getParts(): array { |
|
602 | - return $this->parts; |
|
603 | - } |
|
604 | - |
|
605 | - |
|
606 | - /** |
|
607 | - * Add a link, usable by the frontend. |
|
608 | - * |
|
609 | - * @since 15.0.0 |
|
610 | - * |
|
611 | - * @param string $link |
|
612 | - * |
|
613 | - * @return IIndexDocument |
|
614 | - */ |
|
615 | - final public function setLink(string $link): IIndexDocument { |
|
616 | - $this->link = $link; |
|
617 | - |
|
618 | - return $this; |
|
619 | - } |
|
620 | - |
|
621 | - /** |
|
622 | - * Get the link. |
|
623 | - * |
|
624 | - * @since 15.0.0 |
|
625 | - * |
|
626 | - * @return string |
|
627 | - */ |
|
628 | - final public function getLink(): string { |
|
629 | - return $this->link; |
|
630 | - } |
|
631 | - |
|
632 | - |
|
633 | - /** |
|
634 | - * Set more information that couldn't be set using other method. |
|
635 | - * |
|
636 | - * @since 15.0.0 |
|
637 | - * |
|
638 | - * @param array $more |
|
639 | - * |
|
640 | - * @return IIndexDocument |
|
641 | - */ |
|
642 | - final public function setMore(array $more): IIndexDocument { |
|
643 | - $this->more = $more; |
|
644 | - |
|
645 | - return $this; |
|
646 | - } |
|
647 | - |
|
648 | - /** |
|
649 | - * Get more information. |
|
650 | - * |
|
651 | - * @since 15.0.0 |
|
652 | - * |
|
653 | - * @return array |
|
654 | - */ |
|
655 | - final public function getMore(): array { |
|
656 | - return $this->more; |
|
657 | - } |
|
658 | - |
|
659 | - |
|
660 | - /** |
|
661 | - * Add some excerpt of the content of the original document, usually based |
|
662 | - * on the search request. |
|
663 | - * |
|
664 | - * @since 16.0.0 |
|
665 | - * |
|
666 | - * @param string $source |
|
667 | - * @param string $excerpt |
|
668 | - * |
|
669 | - * @return IIndexDocument |
|
670 | - */ |
|
671 | - final public function addExcerpt(string $source, string $excerpt): IIndexDocument { |
|
672 | - $this->excerpts[] = |
|
673 | - [ |
|
674 | - 'source' => $source, |
|
675 | - 'excerpt' => $this->cleanExcerpt($excerpt) |
|
676 | - ]; |
|
677 | - |
|
678 | - return $this; |
|
679 | - } |
|
680 | - |
|
681 | - |
|
682 | - /** |
|
683 | - * Set all excerpts of the content of the original document. |
|
684 | - * |
|
685 | - * @since 16.0.0 |
|
686 | - * |
|
687 | - * @param array $excerpts |
|
688 | - * |
|
689 | - * @return IIndexDocument |
|
690 | - */ |
|
691 | - final public function setExcerpts(array $excerpts): IIndexDocument { |
|
692 | - $new = []; |
|
693 | - foreach ($excerpts as $entry) { |
|
694 | - $new[] = [ |
|
695 | - 'source' => $entry['source'], |
|
696 | - 'excerpt' => $this->cleanExcerpt($entry['excerpt']) |
|
697 | - ]; |
|
698 | - } |
|
699 | - |
|
700 | - $this->excerpts = $new; |
|
701 | - |
|
702 | - return $this; |
|
703 | - } |
|
704 | - |
|
705 | - /** |
|
706 | - * Get all excerpts of the content of the original document. |
|
707 | - * |
|
708 | - * @since 15.0.0 |
|
709 | - * |
|
710 | - * @return array |
|
711 | - */ |
|
712 | - final public function getExcerpts(): array { |
|
713 | - return $this->excerpts; |
|
714 | - } |
|
715 | - |
|
716 | - /** |
|
717 | - * Clean excerpt. |
|
718 | - * |
|
719 | - * @since 16.0.0 |
|
720 | - * |
|
721 | - * @param string $excerpt |
|
722 | - * @return string |
|
723 | - */ |
|
724 | - private function cleanExcerpt(string $excerpt): string { |
|
725 | - $excerpt = str_replace("\\n", ' ', $excerpt); |
|
726 | - $excerpt = str_replace("\\r", ' ', $excerpt); |
|
727 | - $excerpt = str_replace("\\t", ' ', $excerpt); |
|
728 | - $excerpt = str_replace("\n", ' ', $excerpt); |
|
729 | - $excerpt = str_replace("\r", ' ', $excerpt); |
|
730 | - $excerpt = str_replace("\t", ' ', $excerpt); |
|
731 | - |
|
732 | - return $excerpt; |
|
733 | - } |
|
734 | - |
|
735 | - |
|
736 | - /** |
|
737 | - * Set the score to the result assigned to this document during a search |
|
738 | - * request. |
|
739 | - * |
|
740 | - * @since 15.0.0 |
|
741 | - * |
|
742 | - * @param string $score |
|
743 | - * |
|
744 | - * @return IIndexDocument |
|
745 | - */ |
|
746 | - final public function setScore(string $score): IIndexDocument { |
|
747 | - $this->score = $score; |
|
748 | - |
|
749 | - return $this; |
|
750 | - } |
|
751 | - |
|
752 | - /** |
|
753 | - * Get the score. |
|
754 | - * |
|
755 | - * @since 15.0.0 |
|
756 | - * |
|
757 | - * @return string |
|
758 | - */ |
|
759 | - final public function getScore(): string { |
|
760 | - return $this->score; |
|
761 | - } |
|
762 | - |
|
763 | - |
|
764 | - /** |
|
765 | - * Set some information about the original document that will be available |
|
766 | - * to the front-end when displaying search result. (as string) |
|
767 | - * Because this information will not be indexed, this method can also be |
|
768 | - * used to manage some data while filling the IIndexDocument before its |
|
769 | - * indexing. |
|
770 | - * |
|
771 | - * @since 15.0.0 |
|
772 | - * |
|
773 | - * @param string $info |
|
774 | - * @param string $value |
|
775 | - * |
|
776 | - * @return IIndexDocument |
|
777 | - */ |
|
778 | - final public function setInfo(string $info, string $value): IIndexDocument { |
|
779 | - $this->info[$info] = $value; |
|
780 | - |
|
781 | - return $this; |
|
782 | - } |
|
783 | - |
|
784 | - /** |
|
785 | - * Get an information about a document. (string) |
|
786 | - * |
|
787 | - * @since 15.0.0 |
|
788 | - * |
|
789 | - * @param string $info |
|
790 | - * @param string $default |
|
791 | - * |
|
792 | - * @return string |
|
793 | - */ |
|
794 | - final public function getInfo(string $info, string $default = ''): string { |
|
795 | - if (!key_exists($info, $this->info)) { |
|
796 | - return $default; |
|
797 | - } |
|
798 | - |
|
799 | - return $this->info[$info]; |
|
800 | - } |
|
801 | - |
|
802 | - /** |
|
803 | - * Set some information about the original document that will be available |
|
804 | - * to the front-end when displaying search result. (as array) |
|
805 | - * Because this information will not be indexed, this method can also be |
|
806 | - * used to manage some data while filling the IIndexDocument before its |
|
807 | - * indexing. |
|
808 | - * |
|
809 | - * @since 15.0.0 |
|
810 | - * |
|
811 | - * @param string $info |
|
812 | - * @param array $value |
|
813 | - * |
|
814 | - * @return IIndexDocument |
|
815 | - */ |
|
816 | - final public function setInfoArray(string $info, array $value): IIndexDocument { |
|
817 | - $this->info[$info] = $value; |
|
818 | - |
|
819 | - return $this; |
|
820 | - } |
|
821 | - |
|
822 | - /** |
|
823 | - * Get an information about a document. (array) |
|
824 | - * |
|
825 | - * @since 15.0.0 |
|
826 | - * |
|
827 | - * @param string $info |
|
828 | - * @param array $default |
|
829 | - * |
|
830 | - * @return array |
|
831 | - */ |
|
832 | - final public function getInfoArray(string $info, array $default = []): array { |
|
833 | - if (!key_exists($info, $this->info)) { |
|
834 | - return $default; |
|
835 | - } |
|
836 | - |
|
837 | - return $this->info[$info]; |
|
838 | - } |
|
839 | - |
|
840 | - /** |
|
841 | - * Set some information about the original document that will be available |
|
842 | - * to the front-end when displaying search result. (as int) |
|
843 | - * Because this information will not be indexed, this method can also be |
|
844 | - * used to manage some data while filling the IIndexDocument before its |
|
845 | - * indexing. |
|
846 | - * |
|
847 | - * @since 15.0.0 |
|
848 | - * |
|
849 | - * @param string $info |
|
850 | - * @param int $value |
|
851 | - * |
|
852 | - * @return IIndexDocument |
|
853 | - */ |
|
854 | - final public function setInfoInt(string $info, int $value): IIndexDocument { |
|
855 | - $this->info[$info] = $value; |
|
856 | - |
|
857 | - return $this; |
|
858 | - } |
|
859 | - |
|
860 | - /** |
|
861 | - * Get an information about a document. (int) |
|
862 | - * |
|
863 | - * @since 15.0.0 |
|
864 | - * |
|
865 | - * @param string $info |
|
866 | - * @param int $default |
|
867 | - * |
|
868 | - * @return int |
|
869 | - */ |
|
870 | - final public function getInfoInt(string $info, int $default = 0): int { |
|
871 | - if (!key_exists($info, $this->info)) { |
|
872 | - return $default; |
|
873 | - } |
|
874 | - |
|
875 | - return $this->info[$info]; |
|
876 | - } |
|
877 | - |
|
878 | - /** |
|
879 | - * Set some information about the original document that will be available |
|
880 | - * to the front-end when displaying search result. (as bool) |
|
881 | - * Because this information will not be indexed, this method can also be |
|
882 | - * used to manage some data while filling the IIndexDocument before its |
|
883 | - * indexing. |
|
884 | - * |
|
885 | - * @since 15.0.0 |
|
886 | - * |
|
887 | - * @param string $info |
|
888 | - * @param bool $value |
|
889 | - * |
|
890 | - * @return IIndexDocument |
|
891 | - */ |
|
892 | - final public function setInfoBool(string $info, bool $value): IIndexDocument { |
|
893 | - $this->info[$info] = $value; |
|
894 | - |
|
895 | - return $this; |
|
896 | - } |
|
897 | - |
|
898 | - /** |
|
899 | - * Get an information about a document. (bool) |
|
900 | - * |
|
901 | - * @since 15.0.0 |
|
902 | - * |
|
903 | - * @param string $info |
|
904 | - * @param bool $default |
|
905 | - * |
|
906 | - * @return bool |
|
907 | - */ |
|
908 | - final public function getInfoBool(string $info, bool $default = false): bool { |
|
909 | - if (!key_exists($info, $this->info)) { |
|
910 | - return $default; |
|
911 | - } |
|
912 | - |
|
913 | - return $this->info[$info]; |
|
914 | - } |
|
915 | - |
|
916 | - /** |
|
917 | - * Get all info. |
|
918 | - * |
|
919 | - * @since 15.0.0 |
|
920 | - * |
|
921 | - * @return array |
|
922 | - */ |
|
923 | - final public function getInfoAll(): array { |
|
924 | - $info = []; |
|
925 | - foreach ($this->info as $k => $v) { |
|
926 | - if (substr($k, 0, 1) === '_') { |
|
927 | - continue; |
|
928 | - } |
|
929 | - |
|
930 | - $info[$k] = $v; |
|
931 | - } |
|
932 | - |
|
933 | - return $info; |
|
934 | - } |
|
935 | - |
|
936 | - |
|
937 | - /** |
|
938 | - * @since 15.0.0 |
|
939 | - * |
|
940 | - * On some version of PHP, it is better to force destruct the object. |
|
941 | - * And during the index, the number of generated IIndexDocument can be |
|
942 | - * _huge_. |
|
943 | - */ |
|
944 | - public function __destruct() { |
|
945 | - unset($this->id); |
|
946 | - unset($this->providerId); |
|
947 | - unset($this->access); |
|
948 | - unset($this->modifiedTime); |
|
949 | - unset($this->title); |
|
950 | - unset($this->content); |
|
951 | - unset($this->hash); |
|
952 | - unset($this->link); |
|
953 | - unset($this->source); |
|
954 | - unset($this->tags); |
|
955 | - unset($this->metaTags); |
|
956 | - unset($this->subTags); |
|
957 | - unset($this->more); |
|
958 | - unset($this->excerpts); |
|
959 | - unset($this->score); |
|
960 | - unset($this->info); |
|
961 | - unset($this->contentEncoded); |
|
962 | - } |
|
963 | - |
|
964 | - /** |
|
965 | - * @since 15.0.0 |
|
966 | - */ |
|
967 | - public function jsonSerialize(): array { |
|
968 | - return [ |
|
969 | - 'id' => $this->getId(), |
|
970 | - 'providerId' => $this->getProviderId(), |
|
971 | - 'access' => $this->access, |
|
972 | - 'modifiedTime' => $this->getModifiedTime(), |
|
973 | - 'title' => $this->getTitle(), |
|
974 | - 'link' => $this->getLink(), |
|
975 | - 'index' => $this->index, |
|
976 | - 'source' => $this->getSource(), |
|
977 | - 'info' => $this->getInfoAll(), |
|
978 | - 'hash' => $this->getHash(), |
|
979 | - 'contentSize' => $this->getContentSize(), |
|
980 | - 'tags' => $this->getTags(), |
|
981 | - 'metatags' => $this->getMetaTags(), |
|
982 | - 'subtags' => $this->getSubTags(), |
|
983 | - 'more' => $this->getMore(), |
|
984 | - 'excerpts' => $this->getExcerpts(), |
|
985 | - 'score' => $this->getScore() |
|
986 | - ]; |
|
987 | - } |
|
58 | + /** @var DocumentAccess */ |
|
59 | + protected $access; |
|
60 | + |
|
61 | + /** @var IIndex */ |
|
62 | + protected $index; |
|
63 | + |
|
64 | + /** @var int */ |
|
65 | + protected $modifiedTime = 0; |
|
66 | + |
|
67 | + /** @var string */ |
|
68 | + protected $source = ''; |
|
69 | + |
|
70 | + /** @var array */ |
|
71 | + protected $tags = []; |
|
72 | + |
|
73 | + /** @var array */ |
|
74 | + protected $metaTags = []; |
|
75 | + |
|
76 | + /** @var array */ |
|
77 | + protected $subTags = []; |
|
78 | + |
|
79 | + /** @var string */ |
|
80 | + protected $title = ''; |
|
81 | + |
|
82 | + /** @var string */ |
|
83 | + protected $content = ''; |
|
84 | + |
|
85 | + /** @var string */ |
|
86 | + protected $hash = ''; |
|
87 | + |
|
88 | + /** @var array */ |
|
89 | + protected $parts = []; |
|
90 | + |
|
91 | + /** @var string */ |
|
92 | + protected $link = ''; |
|
93 | + |
|
94 | + /** @var array */ |
|
95 | + protected $more = []; |
|
96 | + |
|
97 | + /** @var array */ |
|
98 | + protected $excerpts = []; |
|
99 | + |
|
100 | + /** @var string */ |
|
101 | + protected $score = ''; |
|
102 | + |
|
103 | + /** @var array */ |
|
104 | + protected $info = []; |
|
105 | + |
|
106 | + /** @var int */ |
|
107 | + protected $contentEncoded = 0; |
|
108 | + |
|
109 | + |
|
110 | + /** |
|
111 | + * IIndexDocument constructor. |
|
112 | + * |
|
113 | + * On creation, we assure the uniqueness of the object using the providerId |
|
114 | + * and the Id of the original document. |
|
115 | + * |
|
116 | + * @since 15.0.0 |
|
117 | + * |
|
118 | + * @param string $providerId |
|
119 | + * @param string $documentId |
|
120 | + */ |
|
121 | + public function __construct(string $providerId, string $documentId) { |
|
122 | + $this->providerId = $providerId; |
|
123 | + $this->id = $documentId; |
|
124 | + } |
|
125 | + |
|
126 | + |
|
127 | + /** |
|
128 | + * Returns the Id of the original document. |
|
129 | + * |
|
130 | + * @since 15.0.0 |
|
131 | + * |
|
132 | + * @return string |
|
133 | + */ |
|
134 | + final public function getId(): string { |
|
135 | + return $this->id; |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * Returns the Id of the provider. |
|
141 | + * |
|
142 | + * @since 15.0.0 |
|
143 | + * |
|
144 | + * @return string |
|
145 | + */ |
|
146 | + final public function getProviderId(): string { |
|
147 | + return $this->providerId; |
|
148 | + } |
|
149 | + |
|
150 | + |
|
151 | + /** |
|
152 | + * Set the Index related to the IIndexDocument. |
|
153 | + * |
|
154 | + * @see IIndex |
|
155 | + * |
|
156 | + * @since 15.0.0 |
|
157 | + * |
|
158 | + * @param IIndex $index |
|
159 | + * |
|
160 | + * @return IIndexDocument |
|
161 | + */ |
|
162 | + final public function setIndex(IIndex $index): IIndexDocument { |
|
163 | + $this->index = $index; |
|
164 | + |
|
165 | + return $this; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Get the Index. |
|
170 | + * |
|
171 | + * @since 15.0.0 |
|
172 | + * |
|
173 | + * @return IIndex |
|
174 | + */ |
|
175 | + final public function getIndex(): IIndex { |
|
176 | + return $this->index; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * return if Index is defined. |
|
181 | + * |
|
182 | + * @since 16.0.0 |
|
183 | + * |
|
184 | + * @return bool |
|
185 | + */ |
|
186 | + final public function hasIndex(): bool { |
|
187 | + return ($this->index !== null); |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + /** |
|
192 | + * Set the modified time of the original document. |
|
193 | + * |
|
194 | + * @since 15.0.0 |
|
195 | + * |
|
196 | + * @param int $modifiedTime |
|
197 | + * |
|
198 | + * @return IIndexDocument |
|
199 | + */ |
|
200 | + final public function setModifiedTime(int $modifiedTime): IIndexDocument { |
|
201 | + $this->modifiedTime = $modifiedTime; |
|
202 | + |
|
203 | + return $this; |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Get the modified time of the original document. |
|
208 | + * |
|
209 | + * @since 15.0.0 |
|
210 | + * |
|
211 | + * @return int |
|
212 | + */ |
|
213 | + final public function getModifiedTime(): int { |
|
214 | + return $this->modifiedTime; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Check if the original document of the IIndexDocument is older than $time. |
|
219 | + * |
|
220 | + * @since 15.0.0 |
|
221 | + * |
|
222 | + * @param int $time |
|
223 | + * |
|
224 | + * @return bool |
|
225 | + */ |
|
226 | + final public function isOlderThan(int $time): bool { |
|
227 | + return ($this->modifiedTime < $time); |
|
228 | + } |
|
229 | + |
|
230 | + |
|
231 | + /** |
|
232 | + * Set the read rights of the original document using a IDocumentAccess. |
|
233 | + * |
|
234 | + * @see IDocumentAccess |
|
235 | + * |
|
236 | + * @since 15.0.0 |
|
237 | + * |
|
238 | + * @param IDocumentAccess $access |
|
239 | + * |
|
240 | + * @return $this |
|
241 | + */ |
|
242 | + final public function setAccess(IDocumentAccess $access): IIndexDocument { |
|
243 | + $this->access = $access; |
|
244 | + |
|
245 | + return $this; |
|
246 | + } |
|
247 | + |
|
248 | + /** |
|
249 | + * Get the IDocumentAccess related to the original document. |
|
250 | + * |
|
251 | + * @since 15.0.0 |
|
252 | + * |
|
253 | + * @return IDocumentAccess |
|
254 | + */ |
|
255 | + final public function getAccess(): IDocumentAccess { |
|
256 | + return $this->access; |
|
257 | + } |
|
258 | + |
|
259 | + |
|
260 | + /** |
|
261 | + * Add a tag to the list. |
|
262 | + * |
|
263 | + * @since 15.0.0 |
|
264 | + * |
|
265 | + * @param string $tag |
|
266 | + * |
|
267 | + * @return IIndexDocument |
|
268 | + */ |
|
269 | + final public function addTag(string $tag): IIndexDocument { |
|
270 | + $this->tags[] = $tag; |
|
271 | + |
|
272 | + return $this; |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * Set the list of tags assigned to the original document. |
|
277 | + * |
|
278 | + * @since 15.0.0 |
|
279 | + * |
|
280 | + * @param array $tags |
|
281 | + * |
|
282 | + * @return IIndexDocument |
|
283 | + */ |
|
284 | + final public function setTags(array $tags): IIndexDocument { |
|
285 | + $this->tags = $tags; |
|
286 | + |
|
287 | + return $this; |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Get the list of tags assigned to the original document. |
|
292 | + * |
|
293 | + * @since 15.0.0 |
|
294 | + * |
|
295 | + * @return array |
|
296 | + */ |
|
297 | + final public function getTags(): array { |
|
298 | + return $this->tags; |
|
299 | + } |
|
300 | + |
|
301 | + |
|
302 | + /** |
|
303 | + * Add a meta tag to the list. |
|
304 | + * |
|
305 | + * @since 15.0.0 |
|
306 | + * |
|
307 | + * @param string $tag |
|
308 | + * |
|
309 | + * @return IIndexDocument |
|
310 | + */ |
|
311 | + final public function addMetaTag(string $tag): IIndexDocument { |
|
312 | + $this->metaTags[] = $tag; |
|
313 | + |
|
314 | + return $this; |
|
315 | + } |
|
316 | + |
|
317 | + /** |
|
318 | + * Set the list of meta tags assigned to the original document. |
|
319 | + * |
|
320 | + * @since 15.0.0 |
|
321 | + * |
|
322 | + * @param array $tags |
|
323 | + * |
|
324 | + * @return IIndexDocument |
|
325 | + */ |
|
326 | + final public function setMetaTags(array $tags): IIndexDocument { |
|
327 | + $this->metaTags = $tags; |
|
328 | + |
|
329 | + return $this; |
|
330 | + } |
|
331 | + |
|
332 | + /** |
|
333 | + * Get the list of meta tags assigned to the original document. |
|
334 | + * |
|
335 | + * @since 15.0.0 |
|
336 | + * |
|
337 | + * @return array |
|
338 | + */ |
|
339 | + final public function getMetaTags(): array { |
|
340 | + return $this->metaTags; |
|
341 | + } |
|
342 | + |
|
343 | + |
|
344 | + /** |
|
345 | + * Add a sub tag to the list. |
|
346 | + * |
|
347 | + * @since 15.0.0 |
|
348 | + * |
|
349 | + * @param string $sub |
|
350 | + * @param string $tag |
|
351 | + * |
|
352 | + * @return IIndexDocument |
|
353 | + */ |
|
354 | + final public function addSubTag(string $sub, string $tag): IIndexDocument { |
|
355 | + if (!array_key_exists($sub, $this->subTags)) { |
|
356 | + $this->subTags[$sub] = []; |
|
357 | + } |
|
358 | + |
|
359 | + $this->subTags[$sub][] = $tag; |
|
360 | + |
|
361 | + return $this; |
|
362 | + } |
|
363 | + |
|
364 | + |
|
365 | + /** |
|
366 | + * Set the list of sub tags assigned to the original document. |
|
367 | + * |
|
368 | + * @since 15.0.0 |
|
369 | + * |
|
370 | + * @param array $tags |
|
371 | + * |
|
372 | + * @return IIndexDocument |
|
373 | + */ |
|
374 | + final public function setSubTags(array $tags): IIndexDocument { |
|
375 | + $this->subTags = $tags; |
|
376 | + |
|
377 | + return $this; |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * Get the list of sub tags assigned to the original document. |
|
382 | + * If $formatted is true, the result will be formatted in a one |
|
383 | + * dimensional array. |
|
384 | + * |
|
385 | + * @since 15.0.0 |
|
386 | + * |
|
387 | + * @param bool $formatted |
|
388 | + * |
|
389 | + * @return array |
|
390 | + */ |
|
391 | + final public function getSubTags(bool $formatted = false): array { |
|
392 | + if ($formatted === false) { |
|
393 | + return $this->subTags; |
|
394 | + } |
|
395 | + |
|
396 | + $subTags = []; |
|
397 | + $ak = array_keys($this->subTags); |
|
398 | + foreach ($ak as $source) { |
|
399 | + $tags = $this->subTags[$source]; |
|
400 | + foreach ($tags as $tag) { |
|
401 | + $subTags[] = $source . '_' . $tag; |
|
402 | + } |
|
403 | + } |
|
404 | + |
|
405 | + return $subTags; |
|
406 | + } |
|
407 | + |
|
408 | + |
|
409 | + /** |
|
410 | + * Set the source of the original document. |
|
411 | + * |
|
412 | + * @since 15.0.0 |
|
413 | + * |
|
414 | + * @param string $source |
|
415 | + * |
|
416 | + * @return IIndexDocument |
|
417 | + */ |
|
418 | + final public function setSource(string $source): IIndexDocument { |
|
419 | + $this->source = $source; |
|
420 | + |
|
421 | + return $this; |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * Get the source of the original document. |
|
426 | + * |
|
427 | + * @since 15.0.0 |
|
428 | + * |
|
429 | + * @return string |
|
430 | + */ |
|
431 | + final public function getSource(): string { |
|
432 | + return $this->source; |
|
433 | + } |
|
434 | + |
|
435 | + |
|
436 | + /** |
|
437 | + * Set the title of the original document. |
|
438 | + * |
|
439 | + * @since 15.0.0 |
|
440 | + * |
|
441 | + * @param string $title |
|
442 | + * |
|
443 | + * @return IIndexDocument |
|
444 | + */ |
|
445 | + final public function setTitle(string $title): IIndexDocument { |
|
446 | + $this->title = $title; |
|
447 | + |
|
448 | + return $this; |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * Get the title of the original document. |
|
453 | + * |
|
454 | + * @since 15.0.0 |
|
455 | + * |
|
456 | + * @return string |
|
457 | + */ |
|
458 | + final public function getTitle(): string { |
|
459 | + return $this->title; |
|
460 | + } |
|
461 | + |
|
462 | + |
|
463 | + /** |
|
464 | + * Set the content of the document. |
|
465 | + * $encoded can be NOT_ENCODED or ENCODED_BASE64 if the content is raw or |
|
466 | + * encoded in base64. |
|
467 | + * |
|
468 | + * @since 15.0.0 |
|
469 | + * |
|
470 | + * @param string $content |
|
471 | + * @param int $encoded |
|
472 | + * |
|
473 | + * @return IIndexDocument |
|
474 | + */ |
|
475 | + final public function setContent(string $content, int $encoded = 0): IIndexDocument { |
|
476 | + $this->content = $content; |
|
477 | + $this->contentEncoded = $encoded; |
|
478 | + |
|
479 | + return $this; |
|
480 | + } |
|
481 | + |
|
482 | + /** |
|
483 | + * Get the content of the original document. |
|
484 | + * |
|
485 | + * @since 15.0.0 |
|
486 | + * |
|
487 | + * @return string |
|
488 | + */ |
|
489 | + final public function getContent(): string { |
|
490 | + return $this->content; |
|
491 | + } |
|
492 | + |
|
493 | + /** |
|
494 | + * Returns the type of the encoding on the content. |
|
495 | + * |
|
496 | + * @since 15.0.0 |
|
497 | + * |
|
498 | + * @return int |
|
499 | + */ |
|
500 | + final public function isContentEncoded(): int { |
|
501 | + return $this->contentEncoded; |
|
502 | + } |
|
503 | + |
|
504 | + /** |
|
505 | + * Return the size of the content. |
|
506 | + * |
|
507 | + * @since 15.0.0 |
|
508 | + * |
|
509 | + * @return int |
|
510 | + */ |
|
511 | + final public function getContentSize(): int { |
|
512 | + return strlen($this->getContent()); |
|
513 | + } |
|
514 | + |
|
515 | + |
|
516 | + /** |
|
517 | + * Generate an hash, based on the content of the original document. |
|
518 | + * |
|
519 | + * @since 15.0.0 |
|
520 | + * |
|
521 | + * @return IIndexDocument |
|
522 | + */ |
|
523 | + final public function initHash(): IIndexDocument { |
|
524 | + if ($this->getContent() === '' || is_null($this->getContent())) { |
|
525 | + return $this; |
|
526 | + } |
|
527 | + |
|
528 | + $this->hash = hash("md5", $this->getContent()); |
|
529 | + |
|
530 | + return $this; |
|
531 | + } |
|
532 | + |
|
533 | + /** |
|
534 | + * Set the hash of the original document. |
|
535 | + * |
|
536 | + * @since 15.0.0 |
|
537 | + * |
|
538 | + * @param string $hash |
|
539 | + * |
|
540 | + * @return IIndexDocument |
|
541 | + */ |
|
542 | + final public function setHash(string $hash): IIndexDocument { |
|
543 | + $this->hash = $hash; |
|
544 | + |
|
545 | + return $this; |
|
546 | + } |
|
547 | + |
|
548 | + /** |
|
549 | + * Get the hash of the original document. |
|
550 | + * |
|
551 | + * @since 15.0.0 |
|
552 | + * |
|
553 | + * @return string |
|
554 | + */ |
|
555 | + final public function getHash(): string { |
|
556 | + return $this->hash; |
|
557 | + } |
|
558 | + |
|
559 | + |
|
560 | + /** |
|
561 | + * Add a part, identified by a string, and its content. |
|
562 | + * |
|
563 | + * It is strongly advised to use alphanumerical chars with no space in the |
|
564 | + * $part string. |
|
565 | + * |
|
566 | + * @since 15.0.0 |
|
567 | + * |
|
568 | + * @param string $part |
|
569 | + * @param string $content |
|
570 | + * |
|
571 | + * @return IIndexDocument |
|
572 | + */ |
|
573 | + final public function addPart(string $part, string $content): IIndexDocument { |
|
574 | + $this->parts[$part] = $content; |
|
575 | + |
|
576 | + return $this; |
|
577 | + } |
|
578 | + |
|
579 | + /** |
|
580 | + * Set all parts and their content. |
|
581 | + * |
|
582 | + * @since 15.0.0 |
|
583 | + * |
|
584 | + * @param array $parts |
|
585 | + * |
|
586 | + * @return IIndexDocument |
|
587 | + */ |
|
588 | + final public function setParts(array $parts): IIndexDocument { |
|
589 | + $this->parts = $parts; |
|
590 | + |
|
591 | + return $this; |
|
592 | + } |
|
593 | + |
|
594 | + /** |
|
595 | + * Get all parts of the IIndexDocument. |
|
596 | + * |
|
597 | + * @since 15.0.0 |
|
598 | + * |
|
599 | + * @return array |
|
600 | + */ |
|
601 | + final public function getParts(): array { |
|
602 | + return $this->parts; |
|
603 | + } |
|
604 | + |
|
605 | + |
|
606 | + /** |
|
607 | + * Add a link, usable by the frontend. |
|
608 | + * |
|
609 | + * @since 15.0.0 |
|
610 | + * |
|
611 | + * @param string $link |
|
612 | + * |
|
613 | + * @return IIndexDocument |
|
614 | + */ |
|
615 | + final public function setLink(string $link): IIndexDocument { |
|
616 | + $this->link = $link; |
|
617 | + |
|
618 | + return $this; |
|
619 | + } |
|
620 | + |
|
621 | + /** |
|
622 | + * Get the link. |
|
623 | + * |
|
624 | + * @since 15.0.0 |
|
625 | + * |
|
626 | + * @return string |
|
627 | + */ |
|
628 | + final public function getLink(): string { |
|
629 | + return $this->link; |
|
630 | + } |
|
631 | + |
|
632 | + |
|
633 | + /** |
|
634 | + * Set more information that couldn't be set using other method. |
|
635 | + * |
|
636 | + * @since 15.0.0 |
|
637 | + * |
|
638 | + * @param array $more |
|
639 | + * |
|
640 | + * @return IIndexDocument |
|
641 | + */ |
|
642 | + final public function setMore(array $more): IIndexDocument { |
|
643 | + $this->more = $more; |
|
644 | + |
|
645 | + return $this; |
|
646 | + } |
|
647 | + |
|
648 | + /** |
|
649 | + * Get more information. |
|
650 | + * |
|
651 | + * @since 15.0.0 |
|
652 | + * |
|
653 | + * @return array |
|
654 | + */ |
|
655 | + final public function getMore(): array { |
|
656 | + return $this->more; |
|
657 | + } |
|
658 | + |
|
659 | + |
|
660 | + /** |
|
661 | + * Add some excerpt of the content of the original document, usually based |
|
662 | + * on the search request. |
|
663 | + * |
|
664 | + * @since 16.0.0 |
|
665 | + * |
|
666 | + * @param string $source |
|
667 | + * @param string $excerpt |
|
668 | + * |
|
669 | + * @return IIndexDocument |
|
670 | + */ |
|
671 | + final public function addExcerpt(string $source, string $excerpt): IIndexDocument { |
|
672 | + $this->excerpts[] = |
|
673 | + [ |
|
674 | + 'source' => $source, |
|
675 | + 'excerpt' => $this->cleanExcerpt($excerpt) |
|
676 | + ]; |
|
677 | + |
|
678 | + return $this; |
|
679 | + } |
|
680 | + |
|
681 | + |
|
682 | + /** |
|
683 | + * Set all excerpts of the content of the original document. |
|
684 | + * |
|
685 | + * @since 16.0.0 |
|
686 | + * |
|
687 | + * @param array $excerpts |
|
688 | + * |
|
689 | + * @return IIndexDocument |
|
690 | + */ |
|
691 | + final public function setExcerpts(array $excerpts): IIndexDocument { |
|
692 | + $new = []; |
|
693 | + foreach ($excerpts as $entry) { |
|
694 | + $new[] = [ |
|
695 | + 'source' => $entry['source'], |
|
696 | + 'excerpt' => $this->cleanExcerpt($entry['excerpt']) |
|
697 | + ]; |
|
698 | + } |
|
699 | + |
|
700 | + $this->excerpts = $new; |
|
701 | + |
|
702 | + return $this; |
|
703 | + } |
|
704 | + |
|
705 | + /** |
|
706 | + * Get all excerpts of the content of the original document. |
|
707 | + * |
|
708 | + * @since 15.0.0 |
|
709 | + * |
|
710 | + * @return array |
|
711 | + */ |
|
712 | + final public function getExcerpts(): array { |
|
713 | + return $this->excerpts; |
|
714 | + } |
|
715 | + |
|
716 | + /** |
|
717 | + * Clean excerpt. |
|
718 | + * |
|
719 | + * @since 16.0.0 |
|
720 | + * |
|
721 | + * @param string $excerpt |
|
722 | + * @return string |
|
723 | + */ |
|
724 | + private function cleanExcerpt(string $excerpt): string { |
|
725 | + $excerpt = str_replace("\\n", ' ', $excerpt); |
|
726 | + $excerpt = str_replace("\\r", ' ', $excerpt); |
|
727 | + $excerpt = str_replace("\\t", ' ', $excerpt); |
|
728 | + $excerpt = str_replace("\n", ' ', $excerpt); |
|
729 | + $excerpt = str_replace("\r", ' ', $excerpt); |
|
730 | + $excerpt = str_replace("\t", ' ', $excerpt); |
|
731 | + |
|
732 | + return $excerpt; |
|
733 | + } |
|
734 | + |
|
735 | + |
|
736 | + /** |
|
737 | + * Set the score to the result assigned to this document during a search |
|
738 | + * request. |
|
739 | + * |
|
740 | + * @since 15.0.0 |
|
741 | + * |
|
742 | + * @param string $score |
|
743 | + * |
|
744 | + * @return IIndexDocument |
|
745 | + */ |
|
746 | + final public function setScore(string $score): IIndexDocument { |
|
747 | + $this->score = $score; |
|
748 | + |
|
749 | + return $this; |
|
750 | + } |
|
751 | + |
|
752 | + /** |
|
753 | + * Get the score. |
|
754 | + * |
|
755 | + * @since 15.0.0 |
|
756 | + * |
|
757 | + * @return string |
|
758 | + */ |
|
759 | + final public function getScore(): string { |
|
760 | + return $this->score; |
|
761 | + } |
|
762 | + |
|
763 | + |
|
764 | + /** |
|
765 | + * Set some information about the original document that will be available |
|
766 | + * to the front-end when displaying search result. (as string) |
|
767 | + * Because this information will not be indexed, this method can also be |
|
768 | + * used to manage some data while filling the IIndexDocument before its |
|
769 | + * indexing. |
|
770 | + * |
|
771 | + * @since 15.0.0 |
|
772 | + * |
|
773 | + * @param string $info |
|
774 | + * @param string $value |
|
775 | + * |
|
776 | + * @return IIndexDocument |
|
777 | + */ |
|
778 | + final public function setInfo(string $info, string $value): IIndexDocument { |
|
779 | + $this->info[$info] = $value; |
|
780 | + |
|
781 | + return $this; |
|
782 | + } |
|
783 | + |
|
784 | + /** |
|
785 | + * Get an information about a document. (string) |
|
786 | + * |
|
787 | + * @since 15.0.0 |
|
788 | + * |
|
789 | + * @param string $info |
|
790 | + * @param string $default |
|
791 | + * |
|
792 | + * @return string |
|
793 | + */ |
|
794 | + final public function getInfo(string $info, string $default = ''): string { |
|
795 | + if (!key_exists($info, $this->info)) { |
|
796 | + return $default; |
|
797 | + } |
|
798 | + |
|
799 | + return $this->info[$info]; |
|
800 | + } |
|
801 | + |
|
802 | + /** |
|
803 | + * Set some information about the original document that will be available |
|
804 | + * to the front-end when displaying search result. (as array) |
|
805 | + * Because this information will not be indexed, this method can also be |
|
806 | + * used to manage some data while filling the IIndexDocument before its |
|
807 | + * indexing. |
|
808 | + * |
|
809 | + * @since 15.0.0 |
|
810 | + * |
|
811 | + * @param string $info |
|
812 | + * @param array $value |
|
813 | + * |
|
814 | + * @return IIndexDocument |
|
815 | + */ |
|
816 | + final public function setInfoArray(string $info, array $value): IIndexDocument { |
|
817 | + $this->info[$info] = $value; |
|
818 | + |
|
819 | + return $this; |
|
820 | + } |
|
821 | + |
|
822 | + /** |
|
823 | + * Get an information about a document. (array) |
|
824 | + * |
|
825 | + * @since 15.0.0 |
|
826 | + * |
|
827 | + * @param string $info |
|
828 | + * @param array $default |
|
829 | + * |
|
830 | + * @return array |
|
831 | + */ |
|
832 | + final public function getInfoArray(string $info, array $default = []): array { |
|
833 | + if (!key_exists($info, $this->info)) { |
|
834 | + return $default; |
|
835 | + } |
|
836 | + |
|
837 | + return $this->info[$info]; |
|
838 | + } |
|
839 | + |
|
840 | + /** |
|
841 | + * Set some information about the original document that will be available |
|
842 | + * to the front-end when displaying search result. (as int) |
|
843 | + * Because this information will not be indexed, this method can also be |
|
844 | + * used to manage some data while filling the IIndexDocument before its |
|
845 | + * indexing. |
|
846 | + * |
|
847 | + * @since 15.0.0 |
|
848 | + * |
|
849 | + * @param string $info |
|
850 | + * @param int $value |
|
851 | + * |
|
852 | + * @return IIndexDocument |
|
853 | + */ |
|
854 | + final public function setInfoInt(string $info, int $value): IIndexDocument { |
|
855 | + $this->info[$info] = $value; |
|
856 | + |
|
857 | + return $this; |
|
858 | + } |
|
859 | + |
|
860 | + /** |
|
861 | + * Get an information about a document. (int) |
|
862 | + * |
|
863 | + * @since 15.0.0 |
|
864 | + * |
|
865 | + * @param string $info |
|
866 | + * @param int $default |
|
867 | + * |
|
868 | + * @return int |
|
869 | + */ |
|
870 | + final public function getInfoInt(string $info, int $default = 0): int { |
|
871 | + if (!key_exists($info, $this->info)) { |
|
872 | + return $default; |
|
873 | + } |
|
874 | + |
|
875 | + return $this->info[$info]; |
|
876 | + } |
|
877 | + |
|
878 | + /** |
|
879 | + * Set some information about the original document that will be available |
|
880 | + * to the front-end when displaying search result. (as bool) |
|
881 | + * Because this information will not be indexed, this method can also be |
|
882 | + * used to manage some data while filling the IIndexDocument before its |
|
883 | + * indexing. |
|
884 | + * |
|
885 | + * @since 15.0.0 |
|
886 | + * |
|
887 | + * @param string $info |
|
888 | + * @param bool $value |
|
889 | + * |
|
890 | + * @return IIndexDocument |
|
891 | + */ |
|
892 | + final public function setInfoBool(string $info, bool $value): IIndexDocument { |
|
893 | + $this->info[$info] = $value; |
|
894 | + |
|
895 | + return $this; |
|
896 | + } |
|
897 | + |
|
898 | + /** |
|
899 | + * Get an information about a document. (bool) |
|
900 | + * |
|
901 | + * @since 15.0.0 |
|
902 | + * |
|
903 | + * @param string $info |
|
904 | + * @param bool $default |
|
905 | + * |
|
906 | + * @return bool |
|
907 | + */ |
|
908 | + final public function getInfoBool(string $info, bool $default = false): bool { |
|
909 | + if (!key_exists($info, $this->info)) { |
|
910 | + return $default; |
|
911 | + } |
|
912 | + |
|
913 | + return $this->info[$info]; |
|
914 | + } |
|
915 | + |
|
916 | + /** |
|
917 | + * Get all info. |
|
918 | + * |
|
919 | + * @since 15.0.0 |
|
920 | + * |
|
921 | + * @return array |
|
922 | + */ |
|
923 | + final public function getInfoAll(): array { |
|
924 | + $info = []; |
|
925 | + foreach ($this->info as $k => $v) { |
|
926 | + if (substr($k, 0, 1) === '_') { |
|
927 | + continue; |
|
928 | + } |
|
929 | + |
|
930 | + $info[$k] = $v; |
|
931 | + } |
|
932 | + |
|
933 | + return $info; |
|
934 | + } |
|
935 | + |
|
936 | + |
|
937 | + /** |
|
938 | + * @since 15.0.0 |
|
939 | + * |
|
940 | + * On some version of PHP, it is better to force destruct the object. |
|
941 | + * And during the index, the number of generated IIndexDocument can be |
|
942 | + * _huge_. |
|
943 | + */ |
|
944 | + public function __destruct() { |
|
945 | + unset($this->id); |
|
946 | + unset($this->providerId); |
|
947 | + unset($this->access); |
|
948 | + unset($this->modifiedTime); |
|
949 | + unset($this->title); |
|
950 | + unset($this->content); |
|
951 | + unset($this->hash); |
|
952 | + unset($this->link); |
|
953 | + unset($this->source); |
|
954 | + unset($this->tags); |
|
955 | + unset($this->metaTags); |
|
956 | + unset($this->subTags); |
|
957 | + unset($this->more); |
|
958 | + unset($this->excerpts); |
|
959 | + unset($this->score); |
|
960 | + unset($this->info); |
|
961 | + unset($this->contentEncoded); |
|
962 | + } |
|
963 | + |
|
964 | + /** |
|
965 | + * @since 15.0.0 |
|
966 | + */ |
|
967 | + public function jsonSerialize(): array { |
|
968 | + return [ |
|
969 | + 'id' => $this->getId(), |
|
970 | + 'providerId' => $this->getProviderId(), |
|
971 | + 'access' => $this->access, |
|
972 | + 'modifiedTime' => $this->getModifiedTime(), |
|
973 | + 'title' => $this->getTitle(), |
|
974 | + 'link' => $this->getLink(), |
|
975 | + 'index' => $this->index, |
|
976 | + 'source' => $this->getSource(), |
|
977 | + 'info' => $this->getInfoAll(), |
|
978 | + 'hash' => $this->getHash(), |
|
979 | + 'contentSize' => $this->getContentSize(), |
|
980 | + 'tags' => $this->getTags(), |
|
981 | + 'metatags' => $this->getMetaTags(), |
|
982 | + 'subtags' => $this->getSubTags(), |
|
983 | + 'more' => $this->getMore(), |
|
984 | + 'excerpts' => $this->getExcerpts(), |
|
985 | + 'score' => $this->getScore() |
|
986 | + ]; |
|
987 | + } |
|
988 | 988 | } |
@@ -65,196 +65,196 @@ |
||
65 | 65 | final class SearchTemplate implements ISearchTemplate, JsonSerializable { |
66 | 66 | |
67 | 67 | |
68 | - /** @var string */ |
|
69 | - private $icon = ''; |
|
70 | - |
|
71 | - /** @var string */ |
|
72 | - private $css = ''; |
|
73 | - |
|
74 | - /** @var string */ |
|
75 | - private $template = ''; |
|
76 | - |
|
77 | - /** @var SearchOption[] */ |
|
78 | - private $panelOptions = []; |
|
79 | - |
|
80 | - /** @var SearchOption[] */ |
|
81 | - private $navigationOptions = []; |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * ISearchTemplate constructor. |
|
86 | - * |
|
87 | - * the class of the icon and the css file to be loaded can be set during the |
|
88 | - * creation of the object. |
|
89 | - * |
|
90 | - * @since 15.0.0 |
|
91 | - * |
|
92 | - * @param string $icon |
|
93 | - * @param string $css |
|
94 | - */ |
|
95 | - public function __construct(string $icon = '', string $css = '') { |
|
96 | - $this->icon = $icon; |
|
97 | - $this->css = $css; |
|
98 | - } |
|
99 | - |
|
100 | - |
|
101 | - /** |
|
102 | - * Set the class of the icon to be displayed in the left panel of the |
|
103 | - * FullTextSearch navigation page, in front of the related Content Provider. |
|
104 | - * |
|
105 | - * @since 15.0.0 |
|
106 | - * |
|
107 | - * @param string $class |
|
108 | - * |
|
109 | - * @return ISearchTemplate |
|
110 | - */ |
|
111 | - public function setIcon(string $class): ISearchTemplate { |
|
112 | - $this->icon = $class; |
|
113 | - |
|
114 | - return $this; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Get the class of the icon. |
|
119 | - * |
|
120 | - * @since 15.0.0 |
|
121 | - * |
|
122 | - * @return string |
|
123 | - */ |
|
124 | - public function getIcon(): string { |
|
125 | - return $this->icon; |
|
126 | - } |
|
127 | - |
|
128 | - |
|
129 | - /** |
|
130 | - * Set the path of a CSS file that will be loaded when needed. |
|
131 | - * |
|
132 | - * @since 15.0.0 |
|
133 | - * |
|
134 | - * @param string $css |
|
135 | - * |
|
136 | - * @return ISearchTemplate |
|
137 | - */ |
|
138 | - public function setCss(string $css): ISearchTemplate { |
|
139 | - $this->css = $css; |
|
140 | - |
|
141 | - return $this; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Get the path of the CSS file. |
|
146 | - * |
|
147 | - * @since 15.0.0 |
|
148 | - * |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public function getCss(): string { |
|
152 | - return $this->css; |
|
153 | - } |
|
154 | - |
|
155 | - |
|
156 | - /** |
|
157 | - * Set the path of the file of a template that the HTML will be displayed |
|
158 | - * below the Options. |
|
159 | - * This should only be used if your Content Provider needs to set options in |
|
160 | - * a way not generated by FullTextSearch |
|
161 | - * |
|
162 | - * @since 15.0.0 |
|
163 | - * |
|
164 | - * @param string $template |
|
165 | - * |
|
166 | - * @return ISearchTemplate |
|
167 | - */ |
|
168 | - public function setTemplate(string $template): ISearchTemplate { |
|
169 | - $this->template = $template; |
|
170 | - |
|
171 | - return $this; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Get the path of the template file. |
|
176 | - * |
|
177 | - * @since 15.0.0 |
|
178 | - * |
|
179 | - * @return string |
|
180 | - */ |
|
181 | - public function getTemplate(): string { |
|
182 | - return $this->template; |
|
183 | - } |
|
184 | - |
|
185 | - |
|
186 | - /** |
|
187 | - * Add an option in the Panel that is displayed when the user start a search |
|
188 | - * within the app that generate the content. |
|
189 | - * |
|
190 | - * @see ISearchOption |
|
191 | - * |
|
192 | - * @since 15.0.0 |
|
193 | - * |
|
194 | - * @param ISearchOption $option |
|
195 | - * |
|
196 | - * @return ISearchTemplate |
|
197 | - */ |
|
198 | - public function addPanelOption(ISearchOption $option): ISearchTemplate { |
|
199 | - $this->panelOptions[] = $option; |
|
200 | - |
|
201 | - return $this; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Get all options to be displayed in the Panel. |
|
206 | - * |
|
207 | - * @since 15.0.0 |
|
208 | - * |
|
209 | - * @return SearchOption[] |
|
210 | - */ |
|
211 | - public function getPanelOptions(): array { |
|
212 | - return $this->panelOptions; |
|
213 | - } |
|
214 | - |
|
215 | - |
|
216 | - /** |
|
217 | - * Add an option in the left panel of the FullTextSearch navigation page. |
|
218 | - * |
|
219 | - * @see ISearchOption |
|
220 | - * |
|
221 | - * @since 15.0.0 |
|
222 | - * |
|
223 | - * @param ISearchOption $option |
|
224 | - * |
|
225 | - * @return ISearchTemplate |
|
226 | - */ |
|
227 | - public function addNavigationOption(ISearchOption $option): ISearchTemplate { |
|
228 | - $this->navigationOptions[] = $option; |
|
229 | - |
|
230 | - return $this; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Get all options to be displayed in the FullTextSearch navigation page. |
|
235 | - * |
|
236 | - * @since 15.0.0 |
|
237 | - * |
|
238 | - * @return array |
|
239 | - */ |
|
240 | - public function getNavigationOptions(): array { |
|
241 | - return $this->navigationOptions; |
|
242 | - } |
|
243 | - |
|
244 | - |
|
245 | - /** |
|
246 | - * @since 15.0.0 |
|
247 | - * |
|
248 | - * @return array |
|
249 | - */ |
|
250 | - public function jsonSerialize(): array { |
|
251 | - return [ |
|
252 | - 'icon' => $this->getIcon(), |
|
253 | - 'css' => $this->getCss(), |
|
254 | - 'template' => $this->getTemplate(), |
|
255 | - 'panel' => $this->getPanelOptions(), |
|
256 | - 'navigation' => $this->getNavigationOptions() |
|
257 | - ]; |
|
258 | - } |
|
68 | + /** @var string */ |
|
69 | + private $icon = ''; |
|
70 | + |
|
71 | + /** @var string */ |
|
72 | + private $css = ''; |
|
73 | + |
|
74 | + /** @var string */ |
|
75 | + private $template = ''; |
|
76 | + |
|
77 | + /** @var SearchOption[] */ |
|
78 | + private $panelOptions = []; |
|
79 | + |
|
80 | + /** @var SearchOption[] */ |
|
81 | + private $navigationOptions = []; |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * ISearchTemplate constructor. |
|
86 | + * |
|
87 | + * the class of the icon and the css file to be loaded can be set during the |
|
88 | + * creation of the object. |
|
89 | + * |
|
90 | + * @since 15.0.0 |
|
91 | + * |
|
92 | + * @param string $icon |
|
93 | + * @param string $css |
|
94 | + */ |
|
95 | + public function __construct(string $icon = '', string $css = '') { |
|
96 | + $this->icon = $icon; |
|
97 | + $this->css = $css; |
|
98 | + } |
|
99 | + |
|
100 | + |
|
101 | + /** |
|
102 | + * Set the class of the icon to be displayed in the left panel of the |
|
103 | + * FullTextSearch navigation page, in front of the related Content Provider. |
|
104 | + * |
|
105 | + * @since 15.0.0 |
|
106 | + * |
|
107 | + * @param string $class |
|
108 | + * |
|
109 | + * @return ISearchTemplate |
|
110 | + */ |
|
111 | + public function setIcon(string $class): ISearchTemplate { |
|
112 | + $this->icon = $class; |
|
113 | + |
|
114 | + return $this; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Get the class of the icon. |
|
119 | + * |
|
120 | + * @since 15.0.0 |
|
121 | + * |
|
122 | + * @return string |
|
123 | + */ |
|
124 | + public function getIcon(): string { |
|
125 | + return $this->icon; |
|
126 | + } |
|
127 | + |
|
128 | + |
|
129 | + /** |
|
130 | + * Set the path of a CSS file that will be loaded when needed. |
|
131 | + * |
|
132 | + * @since 15.0.0 |
|
133 | + * |
|
134 | + * @param string $css |
|
135 | + * |
|
136 | + * @return ISearchTemplate |
|
137 | + */ |
|
138 | + public function setCss(string $css): ISearchTemplate { |
|
139 | + $this->css = $css; |
|
140 | + |
|
141 | + return $this; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Get the path of the CSS file. |
|
146 | + * |
|
147 | + * @since 15.0.0 |
|
148 | + * |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public function getCss(): string { |
|
152 | + return $this->css; |
|
153 | + } |
|
154 | + |
|
155 | + |
|
156 | + /** |
|
157 | + * Set the path of the file of a template that the HTML will be displayed |
|
158 | + * below the Options. |
|
159 | + * This should only be used if your Content Provider needs to set options in |
|
160 | + * a way not generated by FullTextSearch |
|
161 | + * |
|
162 | + * @since 15.0.0 |
|
163 | + * |
|
164 | + * @param string $template |
|
165 | + * |
|
166 | + * @return ISearchTemplate |
|
167 | + */ |
|
168 | + public function setTemplate(string $template): ISearchTemplate { |
|
169 | + $this->template = $template; |
|
170 | + |
|
171 | + return $this; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Get the path of the template file. |
|
176 | + * |
|
177 | + * @since 15.0.0 |
|
178 | + * |
|
179 | + * @return string |
|
180 | + */ |
|
181 | + public function getTemplate(): string { |
|
182 | + return $this->template; |
|
183 | + } |
|
184 | + |
|
185 | + |
|
186 | + /** |
|
187 | + * Add an option in the Panel that is displayed when the user start a search |
|
188 | + * within the app that generate the content. |
|
189 | + * |
|
190 | + * @see ISearchOption |
|
191 | + * |
|
192 | + * @since 15.0.0 |
|
193 | + * |
|
194 | + * @param ISearchOption $option |
|
195 | + * |
|
196 | + * @return ISearchTemplate |
|
197 | + */ |
|
198 | + public function addPanelOption(ISearchOption $option): ISearchTemplate { |
|
199 | + $this->panelOptions[] = $option; |
|
200 | + |
|
201 | + return $this; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Get all options to be displayed in the Panel. |
|
206 | + * |
|
207 | + * @since 15.0.0 |
|
208 | + * |
|
209 | + * @return SearchOption[] |
|
210 | + */ |
|
211 | + public function getPanelOptions(): array { |
|
212 | + return $this->panelOptions; |
|
213 | + } |
|
214 | + |
|
215 | + |
|
216 | + /** |
|
217 | + * Add an option in the left panel of the FullTextSearch navigation page. |
|
218 | + * |
|
219 | + * @see ISearchOption |
|
220 | + * |
|
221 | + * @since 15.0.0 |
|
222 | + * |
|
223 | + * @param ISearchOption $option |
|
224 | + * |
|
225 | + * @return ISearchTemplate |
|
226 | + */ |
|
227 | + public function addNavigationOption(ISearchOption $option): ISearchTemplate { |
|
228 | + $this->navigationOptions[] = $option; |
|
229 | + |
|
230 | + return $this; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Get all options to be displayed in the FullTextSearch navigation page. |
|
235 | + * |
|
236 | + * @since 15.0.0 |
|
237 | + * |
|
238 | + * @return array |
|
239 | + */ |
|
240 | + public function getNavigationOptions(): array { |
|
241 | + return $this->navigationOptions; |
|
242 | + } |
|
243 | + |
|
244 | + |
|
245 | + /** |
|
246 | + * @since 15.0.0 |
|
247 | + * |
|
248 | + * @return array |
|
249 | + */ |
|
250 | + public function jsonSerialize(): array { |
|
251 | + return [ |
|
252 | + 'icon' => $this->getIcon(), |
|
253 | + 'css' => $this->getCss(), |
|
254 | + 'template' => $this->getTemplate(), |
|
255 | + 'panel' => $this->getPanelOptions(), |
|
256 | + 'navigation' => $this->getNavigationOptions() |
|
257 | + ]; |
|
258 | + } |
|
259 | 259 | } |
260 | 260 |
@@ -58,308 +58,308 @@ |
||
58 | 58 | final class DocumentAccess implements IDocumentAccess, JsonSerializable { |
59 | 59 | |
60 | 60 | |
61 | - /** @var string */ |
|
62 | - private $ownerId; |
|
63 | - |
|
64 | - /** @var string */ |
|
65 | - private $viewerId = ''; |
|
66 | - |
|
67 | - /** @var array */ |
|
68 | - private $users = []; |
|
69 | - |
|
70 | - /** @var array */ |
|
71 | - private $groups = []; |
|
72 | - |
|
73 | - /** @var array */ |
|
74 | - private $circles = []; |
|
75 | - |
|
76 | - /** @var array */ |
|
77 | - private $links = []; |
|
78 | - |
|
79 | - |
|
80 | - /** |
|
81 | - * Owner of the document can be set at the init of the object. |
|
82 | - * |
|
83 | - * @since 16.0.0 |
|
84 | - * |
|
85 | - * IDocumentAccess constructor. |
|
86 | - * |
|
87 | - * @param string $ownerId |
|
88 | - */ |
|
89 | - public function __construct(string $ownerId = '') { |
|
90 | - $this->setOwnerId($ownerId); |
|
91 | - } |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * Set the Owner of the document. |
|
96 | - * |
|
97 | - * @since 16.0.0 |
|
98 | - * |
|
99 | - * @param string $ownerId |
|
100 | - * |
|
101 | - * @return IDocumentAccess |
|
102 | - */ |
|
103 | - public function setOwnerId(string $ownerId): IDocumentAccess { |
|
104 | - $this->ownerId = $ownerId; |
|
105 | - |
|
106 | - return $this; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Get the Owner of the document. |
|
111 | - * |
|
112 | - * @since 16.0.0 |
|
113 | - * |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - public function getOwnerId(): string { |
|
117 | - return $this->ownerId; |
|
118 | - } |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * Set the viewer of the document. |
|
123 | - * |
|
124 | - * @since 16.0.0 |
|
125 | - * |
|
126 | - * @param string $viewerId |
|
127 | - * |
|
128 | - * @return IDocumentAccess |
|
129 | - */ |
|
130 | - public function setViewerId(string $viewerId): IDocumentAccess { |
|
131 | - $this->viewerId = $viewerId; |
|
132 | - |
|
133 | - return $this; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Get the viewer of the document. |
|
138 | - * |
|
139 | - * @since 16.0.0 |
|
140 | - * |
|
141 | - * @return string |
|
142 | - */ |
|
143 | - public function getViewerId(): string { |
|
144 | - return $this->viewerId; |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * Set the list of users that have read access to the document. |
|
150 | - * |
|
151 | - * @since 16.0.0 |
|
152 | - * |
|
153 | - * @param array $users |
|
154 | - * |
|
155 | - * @return IDocumentAccess |
|
156 | - */ |
|
157 | - public function setUsers(array $users): IDocumentAccess { |
|
158 | - $this->users = $users; |
|
159 | - |
|
160 | - return $this; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Add an entry to the list of users that have read access to the document. |
|
165 | - * |
|
166 | - * @since 16.0.0 |
|
167 | - * |
|
168 | - * @param string $user |
|
169 | - * |
|
170 | - * @return IDocumentAccess |
|
171 | - */ |
|
172 | - public function addUser(string $user): IDocumentAccess { |
|
173 | - $this->users[] = $user; |
|
174 | - |
|
175 | - return $this; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Add multiple entries to the list of users that have read access to the |
|
180 | - * document. |
|
181 | - * |
|
182 | - * @since 16.0.0 |
|
183 | - * |
|
184 | - * @param array $users |
|
185 | - * |
|
186 | - * @return IDocumentAccess |
|
187 | - */ |
|
188 | - public function addUsers($users): IDocumentAccess { |
|
189 | - $this->users = array_merge($this->users, $users); |
|
190 | - |
|
191 | - return $this; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Get the complete list of users that have read access to the document. |
|
196 | - * |
|
197 | - * @since 16.0.0 |
|
198 | - * |
|
199 | - * @return array |
|
200 | - */ |
|
201 | - public function getUsers(): array { |
|
202 | - return $this->users; |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - /** |
|
207 | - * Set the list of groups that have read access to the document. |
|
208 | - * |
|
209 | - * @since 16.0.0 |
|
210 | - * |
|
211 | - * @param array $groups |
|
212 | - * |
|
213 | - * @return IDocumentAccess |
|
214 | - */ |
|
215 | - public function setGroups(array $groups): IDocumentAccess { |
|
216 | - $this->groups = $groups; |
|
217 | - |
|
218 | - return $this; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Add an entry to the list of groups that have read access to the document. |
|
223 | - * |
|
224 | - * @since 16.0.0 |
|
225 | - * |
|
226 | - * @param string $group |
|
227 | - * |
|
228 | - * @return IDocumentAccess |
|
229 | - */ |
|
230 | - public function addGroup(string $group): IDocumentAccess { |
|
231 | - $this->groups[] = $group; |
|
232 | - |
|
233 | - return $this; |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Add multiple entries to the list of groups that have read access to the |
|
238 | - * document. |
|
239 | - * |
|
240 | - * @since 16.0.0 |
|
241 | - * |
|
242 | - * @param array $groups |
|
243 | - * |
|
244 | - * @return IDocumentAccess |
|
245 | - */ |
|
246 | - public function addGroups(array $groups) { |
|
247 | - $this->groups = array_merge($this->groups, $groups); |
|
248 | - |
|
249 | - return $this; |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Get the complete list of groups that have read access to the document. |
|
254 | - * |
|
255 | - * @since 16.0.0 |
|
256 | - * |
|
257 | - * @return array |
|
258 | - */ |
|
259 | - public function getGroups(): array { |
|
260 | - return $this->groups; |
|
261 | - } |
|
262 | - |
|
263 | - |
|
264 | - /** |
|
265 | - * Set the list of circles that have read access to the document. |
|
266 | - * |
|
267 | - * @since 16.0.0 |
|
268 | - * |
|
269 | - * @param array $circles |
|
270 | - * |
|
271 | - * @return IDocumentAccess |
|
272 | - */ |
|
273 | - public function setCircles(array $circles): IDocumentAccess { |
|
274 | - $this->circles = $circles; |
|
275 | - |
|
276 | - return $this; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Add an entry to the list of circles that have read access to the document. |
|
281 | - * |
|
282 | - * @since 16.0.0 |
|
283 | - * |
|
284 | - * @param string $circle |
|
285 | - * |
|
286 | - * @return IDocumentAccess |
|
287 | - */ |
|
288 | - public function addCircle(string $circle): IDocumentAccess { |
|
289 | - $this->circles[] = $circle; |
|
290 | - |
|
291 | - return $this; |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Add multiple entries to the list of groups that have read access to the |
|
296 | - * document. |
|
297 | - * |
|
298 | - * @since 16.0.0 |
|
299 | - * |
|
300 | - * @param array $circles |
|
301 | - * |
|
302 | - * @return IDocumentAccess |
|
303 | - */ |
|
304 | - public function addCircles(array $circles): IDocumentAccess { |
|
305 | - $this->circles = array_merge($this->circles, $circles); |
|
306 | - |
|
307 | - return $this; |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Get the complete list of circles that have read access to the document. |
|
312 | - * |
|
313 | - * @since 16.0.0 |
|
314 | - * |
|
315 | - * @return array |
|
316 | - */ |
|
317 | - public function getCircles(): array { |
|
318 | - return $this->circles; |
|
319 | - } |
|
320 | - |
|
321 | - |
|
322 | - /** |
|
323 | - * Set the list of links that have read access to the document. |
|
324 | - * |
|
325 | - * @since 16.0.0 |
|
326 | - * |
|
327 | - * @param array $links |
|
328 | - * |
|
329 | - * @return IDocumentAccess |
|
330 | - */ |
|
331 | - public function setLinks(array $links): IDocumentAccess { |
|
332 | - $this->links = $links; |
|
333 | - |
|
334 | - return $this; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * Get the list of links that have read access to the document. |
|
339 | - * |
|
340 | - * @since 16.0.0 |
|
341 | - * |
|
342 | - * @return array |
|
343 | - */ |
|
344 | - public function getLinks(): array { |
|
345 | - return $this->links; |
|
346 | - } |
|
347 | - |
|
348 | - |
|
349 | - /** |
|
350 | - * @since 16.0.0 |
|
351 | - * |
|
352 | - * @return array |
|
353 | - */ |
|
354 | - public function jsonSerialize(): array { |
|
355 | - return [ |
|
356 | - 'ownerId' => $this->getOwnerId(), |
|
357 | - 'viewerId' => $this->getViewerId(), |
|
358 | - 'users' => $this->getUsers(), |
|
359 | - 'groups' => $this->getGroups(), |
|
360 | - 'circles' => $this->getCircles(), |
|
361 | - 'links' => $this->getLinks() |
|
362 | - ]; |
|
363 | - } |
|
61 | + /** @var string */ |
|
62 | + private $ownerId; |
|
63 | + |
|
64 | + /** @var string */ |
|
65 | + private $viewerId = ''; |
|
66 | + |
|
67 | + /** @var array */ |
|
68 | + private $users = []; |
|
69 | + |
|
70 | + /** @var array */ |
|
71 | + private $groups = []; |
|
72 | + |
|
73 | + /** @var array */ |
|
74 | + private $circles = []; |
|
75 | + |
|
76 | + /** @var array */ |
|
77 | + private $links = []; |
|
78 | + |
|
79 | + |
|
80 | + /** |
|
81 | + * Owner of the document can be set at the init of the object. |
|
82 | + * |
|
83 | + * @since 16.0.0 |
|
84 | + * |
|
85 | + * IDocumentAccess constructor. |
|
86 | + * |
|
87 | + * @param string $ownerId |
|
88 | + */ |
|
89 | + public function __construct(string $ownerId = '') { |
|
90 | + $this->setOwnerId($ownerId); |
|
91 | + } |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * Set the Owner of the document. |
|
96 | + * |
|
97 | + * @since 16.0.0 |
|
98 | + * |
|
99 | + * @param string $ownerId |
|
100 | + * |
|
101 | + * @return IDocumentAccess |
|
102 | + */ |
|
103 | + public function setOwnerId(string $ownerId): IDocumentAccess { |
|
104 | + $this->ownerId = $ownerId; |
|
105 | + |
|
106 | + return $this; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Get the Owner of the document. |
|
111 | + * |
|
112 | + * @since 16.0.0 |
|
113 | + * |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + public function getOwnerId(): string { |
|
117 | + return $this->ownerId; |
|
118 | + } |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * Set the viewer of the document. |
|
123 | + * |
|
124 | + * @since 16.0.0 |
|
125 | + * |
|
126 | + * @param string $viewerId |
|
127 | + * |
|
128 | + * @return IDocumentAccess |
|
129 | + */ |
|
130 | + public function setViewerId(string $viewerId): IDocumentAccess { |
|
131 | + $this->viewerId = $viewerId; |
|
132 | + |
|
133 | + return $this; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Get the viewer of the document. |
|
138 | + * |
|
139 | + * @since 16.0.0 |
|
140 | + * |
|
141 | + * @return string |
|
142 | + */ |
|
143 | + public function getViewerId(): string { |
|
144 | + return $this->viewerId; |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * Set the list of users that have read access to the document. |
|
150 | + * |
|
151 | + * @since 16.0.0 |
|
152 | + * |
|
153 | + * @param array $users |
|
154 | + * |
|
155 | + * @return IDocumentAccess |
|
156 | + */ |
|
157 | + public function setUsers(array $users): IDocumentAccess { |
|
158 | + $this->users = $users; |
|
159 | + |
|
160 | + return $this; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Add an entry to the list of users that have read access to the document. |
|
165 | + * |
|
166 | + * @since 16.0.0 |
|
167 | + * |
|
168 | + * @param string $user |
|
169 | + * |
|
170 | + * @return IDocumentAccess |
|
171 | + */ |
|
172 | + public function addUser(string $user): IDocumentAccess { |
|
173 | + $this->users[] = $user; |
|
174 | + |
|
175 | + return $this; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Add multiple entries to the list of users that have read access to the |
|
180 | + * document. |
|
181 | + * |
|
182 | + * @since 16.0.0 |
|
183 | + * |
|
184 | + * @param array $users |
|
185 | + * |
|
186 | + * @return IDocumentAccess |
|
187 | + */ |
|
188 | + public function addUsers($users): IDocumentAccess { |
|
189 | + $this->users = array_merge($this->users, $users); |
|
190 | + |
|
191 | + return $this; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Get the complete list of users that have read access to the document. |
|
196 | + * |
|
197 | + * @since 16.0.0 |
|
198 | + * |
|
199 | + * @return array |
|
200 | + */ |
|
201 | + public function getUsers(): array { |
|
202 | + return $this->users; |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + /** |
|
207 | + * Set the list of groups that have read access to the document. |
|
208 | + * |
|
209 | + * @since 16.0.0 |
|
210 | + * |
|
211 | + * @param array $groups |
|
212 | + * |
|
213 | + * @return IDocumentAccess |
|
214 | + */ |
|
215 | + public function setGroups(array $groups): IDocumentAccess { |
|
216 | + $this->groups = $groups; |
|
217 | + |
|
218 | + return $this; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Add an entry to the list of groups that have read access to the document. |
|
223 | + * |
|
224 | + * @since 16.0.0 |
|
225 | + * |
|
226 | + * @param string $group |
|
227 | + * |
|
228 | + * @return IDocumentAccess |
|
229 | + */ |
|
230 | + public function addGroup(string $group): IDocumentAccess { |
|
231 | + $this->groups[] = $group; |
|
232 | + |
|
233 | + return $this; |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Add multiple entries to the list of groups that have read access to the |
|
238 | + * document. |
|
239 | + * |
|
240 | + * @since 16.0.0 |
|
241 | + * |
|
242 | + * @param array $groups |
|
243 | + * |
|
244 | + * @return IDocumentAccess |
|
245 | + */ |
|
246 | + public function addGroups(array $groups) { |
|
247 | + $this->groups = array_merge($this->groups, $groups); |
|
248 | + |
|
249 | + return $this; |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Get the complete list of groups that have read access to the document. |
|
254 | + * |
|
255 | + * @since 16.0.0 |
|
256 | + * |
|
257 | + * @return array |
|
258 | + */ |
|
259 | + public function getGroups(): array { |
|
260 | + return $this->groups; |
|
261 | + } |
|
262 | + |
|
263 | + |
|
264 | + /** |
|
265 | + * Set the list of circles that have read access to the document. |
|
266 | + * |
|
267 | + * @since 16.0.0 |
|
268 | + * |
|
269 | + * @param array $circles |
|
270 | + * |
|
271 | + * @return IDocumentAccess |
|
272 | + */ |
|
273 | + public function setCircles(array $circles): IDocumentAccess { |
|
274 | + $this->circles = $circles; |
|
275 | + |
|
276 | + return $this; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Add an entry to the list of circles that have read access to the document. |
|
281 | + * |
|
282 | + * @since 16.0.0 |
|
283 | + * |
|
284 | + * @param string $circle |
|
285 | + * |
|
286 | + * @return IDocumentAccess |
|
287 | + */ |
|
288 | + public function addCircle(string $circle): IDocumentAccess { |
|
289 | + $this->circles[] = $circle; |
|
290 | + |
|
291 | + return $this; |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Add multiple entries to the list of groups that have read access to the |
|
296 | + * document. |
|
297 | + * |
|
298 | + * @since 16.0.0 |
|
299 | + * |
|
300 | + * @param array $circles |
|
301 | + * |
|
302 | + * @return IDocumentAccess |
|
303 | + */ |
|
304 | + public function addCircles(array $circles): IDocumentAccess { |
|
305 | + $this->circles = array_merge($this->circles, $circles); |
|
306 | + |
|
307 | + return $this; |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Get the complete list of circles that have read access to the document. |
|
312 | + * |
|
313 | + * @since 16.0.0 |
|
314 | + * |
|
315 | + * @return array |
|
316 | + */ |
|
317 | + public function getCircles(): array { |
|
318 | + return $this->circles; |
|
319 | + } |
|
320 | + |
|
321 | + |
|
322 | + /** |
|
323 | + * Set the list of links that have read access to the document. |
|
324 | + * |
|
325 | + * @since 16.0.0 |
|
326 | + * |
|
327 | + * @param array $links |
|
328 | + * |
|
329 | + * @return IDocumentAccess |
|
330 | + */ |
|
331 | + public function setLinks(array $links): IDocumentAccess { |
|
332 | + $this->links = $links; |
|
333 | + |
|
334 | + return $this; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * Get the list of links that have read access to the document. |
|
339 | + * |
|
340 | + * @since 16.0.0 |
|
341 | + * |
|
342 | + * @return array |
|
343 | + */ |
|
344 | + public function getLinks(): array { |
|
345 | + return $this->links; |
|
346 | + } |
|
347 | + |
|
348 | + |
|
349 | + /** |
|
350 | + * @since 16.0.0 |
|
351 | + * |
|
352 | + * @return array |
|
353 | + */ |
|
354 | + public function jsonSerialize(): array { |
|
355 | + return [ |
|
356 | + 'ownerId' => $this->getOwnerId(), |
|
357 | + 'viewerId' => $this->getViewerId(), |
|
358 | + 'users' => $this->getUsers(), |
|
359 | + 'groups' => $this->getGroups(), |
|
360 | + 'circles' => $this->getCircles(), |
|
361 | + 'links' => $this->getLinks() |
|
362 | + ]; |
|
363 | + } |
|
364 | 364 | } |
365 | 365 |
@@ -53,7 +53,7 @@ |
||
53 | 53 | $providers = $this->registry->getProviderStates($event->getUser()); |
54 | 54 | |
55 | 55 | // Loop over all providers. If all are disabled we remove the job |
56 | - $state = array_reduce($providers, function (bool $carry, bool $enabled) { |
|
56 | + $state = array_reduce($providers, function(bool $carry, bool $enabled) { |
|
57 | 57 | return $carry || $enabled; |
58 | 58 | }, false); |
59 | 59 |
@@ -35,33 +35,33 @@ |
||
35 | 35 | |
36 | 36 | class ProviderDisabled implements IEventListener { |
37 | 37 | |
38 | - /** @var IRegistry */ |
|
39 | - private $registry; |
|
38 | + /** @var IRegistry */ |
|
39 | + private $registry; |
|
40 | 40 | |
41 | - /** @var IJobList */ |
|
42 | - private $jobList; |
|
41 | + /** @var IJobList */ |
|
42 | + private $jobList; |
|
43 | 43 | |
44 | - public function __construct(IRegistry $registry, |
|
45 | - IJobList $jobList) { |
|
46 | - $this->registry = $registry; |
|
47 | - $this->jobList = $jobList; |
|
48 | - } |
|
44 | + public function __construct(IRegistry $registry, |
|
45 | + IJobList $jobList) { |
|
46 | + $this->registry = $registry; |
|
47 | + $this->jobList = $jobList; |
|
48 | + } |
|
49 | 49 | |
50 | - public function handle(Event $event): void { |
|
51 | - if (!($event instanceof RegistryEvent)) { |
|
52 | - return; |
|
53 | - } |
|
50 | + public function handle(Event $event): void { |
|
51 | + if (!($event instanceof RegistryEvent)) { |
|
52 | + return; |
|
53 | + } |
|
54 | 54 | |
55 | - $providers = $this->registry->getProviderStates($event->getUser()); |
|
55 | + $providers = $this->registry->getProviderStates($event->getUser()); |
|
56 | 56 | |
57 | - // Loop over all providers. If all are disabled we remove the job |
|
58 | - $state = array_reduce($providers, function (bool $carry, bool $enabled) { |
|
59 | - return $carry || $enabled; |
|
60 | - }, false); |
|
57 | + // Loop over all providers. If all are disabled we remove the job |
|
58 | + $state = array_reduce($providers, function (bool $carry, bool $enabled) { |
|
59 | + return $carry || $enabled; |
|
60 | + }, false); |
|
61 | 61 | |
62 | - if ($state === false) { |
|
63 | - $this->jobList->remove(RememberBackupCodesJob::class, ['uid' => $event->getUser()->getUID()]); |
|
64 | - } |
|
65 | - } |
|
62 | + if ($state === false) { |
|
63 | + $this->jobList->remove(RememberBackupCodesJob::class, ['uid' => $event->getUser()->getUID()]); |
|
64 | + } |
|
65 | + } |
|
66 | 66 | |
67 | 67 | } |
@@ -31,129 +31,129 @@ |
||
31 | 31 | |
32 | 32 | class Resource implements IResource { |
33 | 33 | |
34 | - /** @var IManager */ |
|
35 | - protected $manager; |
|
36 | - |
|
37 | - /** @var IDBConnection */ |
|
38 | - protected $connection; |
|
39 | - |
|
40 | - /** @var string */ |
|
41 | - protected $type; |
|
42 | - |
|
43 | - /** @var string */ |
|
44 | - protected $id; |
|
45 | - |
|
46 | - /** @var IUser|null */ |
|
47 | - protected $userForAccess; |
|
48 | - |
|
49 | - /** @var bool|null */ |
|
50 | - protected $access; |
|
51 | - |
|
52 | - /** @var array|null */ |
|
53 | - protected $data; |
|
54 | - |
|
55 | - public function __construct( |
|
56 | - IManager $manager, |
|
57 | - IDBConnection $connection, |
|
58 | - string $type, |
|
59 | - string $id, |
|
60 | - ?IUser $userForAccess = null, |
|
61 | - ?bool $access = null |
|
62 | - ) { |
|
63 | - $this->manager = $manager; |
|
64 | - $this->connection = $connection; |
|
65 | - $this->type = $type; |
|
66 | - $this->id = $id; |
|
67 | - $this->userForAccess = $userForAccess; |
|
68 | - $this->access = $access; |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * @return string |
|
73 | - * @since 16.0.0 |
|
74 | - */ |
|
75 | - public function getType(): string { |
|
76 | - return $this->type; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @return string |
|
81 | - * @since 16.0.0 |
|
82 | - */ |
|
83 | - public function getId(): string { |
|
84 | - return $this->id; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @return array |
|
89 | - * @since 16.0.0 |
|
90 | - */ |
|
91 | - public function getRichObject(): array { |
|
92 | - if ($this->data === null) { |
|
93 | - $this->data = $this->manager->getResourceRichObject($this); |
|
94 | - } |
|
95 | - |
|
96 | - return $this->data; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Can a user/guest access the resource |
|
101 | - * |
|
102 | - * @param IUser|null $user |
|
103 | - * @return bool |
|
104 | - * @since 16.0.0 |
|
105 | - */ |
|
106 | - public function canAccess(?IUser $user): bool { |
|
107 | - if ($user instanceof IUser) { |
|
108 | - return $this->canUserAccess($user); |
|
109 | - } |
|
110 | - return $this->canGuestAccess(); |
|
111 | - } |
|
112 | - |
|
113 | - protected function canUserAccess(IUser $user): bool { |
|
114 | - if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
|
115 | - return $this->access; |
|
116 | - } |
|
117 | - |
|
118 | - $access = $this->manager->canAccessResource($this, $user); |
|
119 | - if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
|
120 | - $this->access = $access; |
|
121 | - } |
|
122 | - return $access; |
|
123 | - } |
|
124 | - |
|
125 | - protected function canGuestAccess(): bool { |
|
126 | - if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) { |
|
127 | - return $this->access; |
|
128 | - } |
|
129 | - |
|
130 | - $access = $this->manager->canAccessResource($this, null); |
|
131 | - if (!$this->userForAccess instanceof IUser) { |
|
132 | - $this->access = $access; |
|
133 | - } |
|
134 | - return $access; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @return ICollection[] |
|
139 | - * @since 16.0.0 |
|
140 | - */ |
|
141 | - public function getCollections(): array { |
|
142 | - $collections = []; |
|
143 | - |
|
144 | - $query = $this->connection->getQueryBuilder(); |
|
145 | - |
|
146 | - $query->select('collection_id') |
|
147 | - ->from('collres_resources') |
|
148 | - ->where($query->expr()->eq('resource_type', $query->createNamedParameter($this->getType()))) |
|
149 | - ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($this->getId()))); |
|
150 | - |
|
151 | - $result = $query->execute(); |
|
152 | - while ($row = $result->fetch()) { |
|
153 | - $collections[] = $this->manager->getCollection((int) $row['collection_id']); |
|
154 | - } |
|
155 | - $result->closeCursor(); |
|
156 | - |
|
157 | - return $collections; |
|
158 | - } |
|
34 | + /** @var IManager */ |
|
35 | + protected $manager; |
|
36 | + |
|
37 | + /** @var IDBConnection */ |
|
38 | + protected $connection; |
|
39 | + |
|
40 | + /** @var string */ |
|
41 | + protected $type; |
|
42 | + |
|
43 | + /** @var string */ |
|
44 | + protected $id; |
|
45 | + |
|
46 | + /** @var IUser|null */ |
|
47 | + protected $userForAccess; |
|
48 | + |
|
49 | + /** @var bool|null */ |
|
50 | + protected $access; |
|
51 | + |
|
52 | + /** @var array|null */ |
|
53 | + protected $data; |
|
54 | + |
|
55 | + public function __construct( |
|
56 | + IManager $manager, |
|
57 | + IDBConnection $connection, |
|
58 | + string $type, |
|
59 | + string $id, |
|
60 | + ?IUser $userForAccess = null, |
|
61 | + ?bool $access = null |
|
62 | + ) { |
|
63 | + $this->manager = $manager; |
|
64 | + $this->connection = $connection; |
|
65 | + $this->type = $type; |
|
66 | + $this->id = $id; |
|
67 | + $this->userForAccess = $userForAccess; |
|
68 | + $this->access = $access; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * @return string |
|
73 | + * @since 16.0.0 |
|
74 | + */ |
|
75 | + public function getType(): string { |
|
76 | + return $this->type; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @return string |
|
81 | + * @since 16.0.0 |
|
82 | + */ |
|
83 | + public function getId(): string { |
|
84 | + return $this->id; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @return array |
|
89 | + * @since 16.0.0 |
|
90 | + */ |
|
91 | + public function getRichObject(): array { |
|
92 | + if ($this->data === null) { |
|
93 | + $this->data = $this->manager->getResourceRichObject($this); |
|
94 | + } |
|
95 | + |
|
96 | + return $this->data; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Can a user/guest access the resource |
|
101 | + * |
|
102 | + * @param IUser|null $user |
|
103 | + * @return bool |
|
104 | + * @since 16.0.0 |
|
105 | + */ |
|
106 | + public function canAccess(?IUser $user): bool { |
|
107 | + if ($user instanceof IUser) { |
|
108 | + return $this->canUserAccess($user); |
|
109 | + } |
|
110 | + return $this->canGuestAccess(); |
|
111 | + } |
|
112 | + |
|
113 | + protected function canUserAccess(IUser $user): bool { |
|
114 | + if (\is_bool($this->access) && $this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
|
115 | + return $this->access; |
|
116 | + } |
|
117 | + |
|
118 | + $access = $this->manager->canAccessResource($this, $user); |
|
119 | + if ($this->userForAccess instanceof IUser && $user->getUID() === $this->userForAccess->getUID()) { |
|
120 | + $this->access = $access; |
|
121 | + } |
|
122 | + return $access; |
|
123 | + } |
|
124 | + |
|
125 | + protected function canGuestAccess(): bool { |
|
126 | + if (\is_bool($this->access) && !$this->userForAccess instanceof IUser) { |
|
127 | + return $this->access; |
|
128 | + } |
|
129 | + |
|
130 | + $access = $this->manager->canAccessResource($this, null); |
|
131 | + if (!$this->userForAccess instanceof IUser) { |
|
132 | + $this->access = $access; |
|
133 | + } |
|
134 | + return $access; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @return ICollection[] |
|
139 | + * @since 16.0.0 |
|
140 | + */ |
|
141 | + public function getCollections(): array { |
|
142 | + $collections = []; |
|
143 | + |
|
144 | + $query = $this->connection->getQueryBuilder(); |
|
145 | + |
|
146 | + $query->select('collection_id') |
|
147 | + ->from('collres_resources') |
|
148 | + ->where($query->expr()->eq('resource_type', $query->createNamedParameter($this->getType()))) |
|
149 | + ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($this->getId()))); |
|
150 | + |
|
151 | + $result = $query->execute(); |
|
152 | + while ($row = $result->fetch()) { |
|
153 | + $collections[] = $this->manager->getCollection((int) $row['collection_id']); |
|
154 | + } |
|
155 | + $result->closeCursor(); |
|
156 | + |
|
157 | + return $collections; |
|
158 | + } |
|
159 | 159 | } |
@@ -29,36 +29,36 @@ |
||
29 | 29 | */ |
30 | 30 | interface IResource { |
31 | 31 | |
32 | - /** |
|
33 | - * @return string |
|
34 | - * @since 16.0.0 |
|
35 | - */ |
|
36 | - public function getType(): string; |
|
32 | + /** |
|
33 | + * @return string |
|
34 | + * @since 16.0.0 |
|
35 | + */ |
|
36 | + public function getType(): string; |
|
37 | 37 | |
38 | - /** |
|
39 | - * @return string |
|
40 | - * @since 16.0.0 |
|
41 | - */ |
|
42 | - public function getId(): string; |
|
38 | + /** |
|
39 | + * @return string |
|
40 | + * @since 16.0.0 |
|
41 | + */ |
|
42 | + public function getId(): string; |
|
43 | 43 | |
44 | - /** |
|
45 | - * @return array |
|
46 | - * @since 16.0.0 |
|
47 | - */ |
|
48 | - public function getRichObject(): array; |
|
44 | + /** |
|
45 | + * @return array |
|
46 | + * @since 16.0.0 |
|
47 | + */ |
|
48 | + public function getRichObject(): array; |
|
49 | 49 | |
50 | - /** |
|
51 | - * Can a user/guest access the resource |
|
52 | - * |
|
53 | - * @param IUser|null $user |
|
54 | - * @return bool |
|
55 | - * @since 16.0.0 |
|
56 | - */ |
|
57 | - public function canAccess(?IUser $user): bool; |
|
50 | + /** |
|
51 | + * Can a user/guest access the resource |
|
52 | + * |
|
53 | + * @param IUser|null $user |
|
54 | + * @return bool |
|
55 | + * @since 16.0.0 |
|
56 | + */ |
|
57 | + public function canAccess(?IUser $user): bool; |
|
58 | 58 | |
59 | - /** |
|
60 | - * @return ICollection[] |
|
61 | - * @since 16.0.0 |
|
62 | - */ |
|
63 | - public function getCollections(): array; |
|
59 | + /** |
|
60 | + * @return ICollection[] |
|
61 | + * @since 16.0.0 |
|
62 | + */ |
|
63 | + public function getCollections(): array; |
|
64 | 64 | } |
@@ -29,31 +29,31 @@ |
||
29 | 29 | */ |
30 | 30 | interface IProvider { |
31 | 31 | |
32 | - /** |
|
33 | - * Get the resource type of the provider |
|
34 | - * |
|
35 | - * @return string |
|
36 | - * @since 16.0.0 |
|
37 | - */ |
|
38 | - public function getType(): string; |
|
32 | + /** |
|
33 | + * Get the resource type of the provider |
|
34 | + * |
|
35 | + * @return string |
|
36 | + * @since 16.0.0 |
|
37 | + */ |
|
38 | + public function getType(): string; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Get the rich object data of a resource |
|
42 | - * |
|
43 | - * @param IResource $resource |
|
44 | - * @return array |
|
45 | - * @since 16.0.0 |
|
46 | - */ |
|
47 | - public function getResourceRichObject(IResource $resource): array; |
|
40 | + /** |
|
41 | + * Get the rich object data of a resource |
|
42 | + * |
|
43 | + * @param IResource $resource |
|
44 | + * @return array |
|
45 | + * @since 16.0.0 |
|
46 | + */ |
|
47 | + public function getResourceRichObject(IResource $resource): array; |
|
48 | 48 | |
49 | - /** |
|
50 | - * Can a user/guest access the collection |
|
51 | - * |
|
52 | - * @param IResource $resource |
|
53 | - * @param IUser|null $user |
|
54 | - * @return bool |
|
55 | - * @since 16.0.0 |
|
56 | - */ |
|
57 | - public function canAccessResource(IResource $resource, ?IUser $user): bool; |
|
49 | + /** |
|
50 | + * Can a user/guest access the collection |
|
51 | + * |
|
52 | + * @param IResource $resource |
|
53 | + * @param IUser|null $user |
|
54 | + * @return bool |
|
55 | + * @since 16.0.0 |
|
56 | + */ |
|
57 | + public function canAccessResource(IResource $resource, ?IUser $user): bool; |
|
58 | 58 | |
59 | 59 | } |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | ->setFirstResult($start); |
145 | 145 | |
146 | 146 | if ($filter !== '') { |
147 | - $query->where($query->expr()->iLike('c.name', $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($filter) . '%'))); |
|
147 | + $query->where($query->expr()->iLike('c.name', $query->createNamedParameter('%'.$this->connection->escapeLikeParameter($filter).'%'))); |
|
148 | 148 | } |
149 | 149 | |
150 | 150 | $result = $query->execute(); |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | while ($row = $result->fetch()) { |
155 | 155 | $foundResults++; |
156 | 156 | $access = $row['access'] === null ? null : (bool) $row['access']; |
157 | - $collection = new Collection($this, $this->connection, (int)$row['id'], (string)$row['name'], $user, $access); |
|
157 | + $collection = new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], $user, $access); |
|
158 | 158 | if ($collection->canAccess($user)) { |
159 | 159 | $collections[] = $collection; |
160 | 160 | } |
@@ -41,485 +41,485 @@ |
||
41 | 41 | use Psr\Log\LoggerInterface; |
42 | 42 | |
43 | 43 | class Manager implements IManager { |
44 | - public const TABLE_COLLECTIONS = 'collres_collections'; |
|
45 | - public const TABLE_RESOURCES = 'collres_resources'; |
|
46 | - public const TABLE_ACCESS_CACHE = 'collres_accesscache'; |
|
47 | - |
|
48 | - /** @var IDBConnection */ |
|
49 | - protected $connection; |
|
50 | - /** @var IProviderManager */ |
|
51 | - protected $providerManager; |
|
52 | - /** @var LoggerInterface */ |
|
53 | - protected $logger; |
|
54 | - |
|
55 | - /** @var string[] */ |
|
56 | - protected $providers = []; |
|
57 | - |
|
58 | - |
|
59 | - public function __construct(IDBConnection $connection, IProviderManager $providerManager, LoggerInterface $logger) { |
|
60 | - $this->connection = $connection; |
|
61 | - $this->providerManager = $providerManager; |
|
62 | - $this->logger = $logger; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @param int $id |
|
67 | - * @return ICollection |
|
68 | - * @throws CollectionException when the collection could not be found |
|
69 | - * @since 16.0.0 |
|
70 | - */ |
|
71 | - public function getCollection(int $id): ICollection { |
|
72 | - $query = $this->connection->getQueryBuilder(); |
|
73 | - $query->select('*') |
|
74 | - ->from(self::TABLE_COLLECTIONS) |
|
75 | - ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
76 | - $result = $query->execute(); |
|
77 | - $row = $result->fetch(); |
|
78 | - $result->closeCursor(); |
|
79 | - |
|
80 | - if (!$row) { |
|
81 | - throw new CollectionException('Collection not found'); |
|
82 | - } |
|
83 | - |
|
84 | - return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name']); |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @param int $id |
|
89 | - * @param IUser|null $user |
|
90 | - * @return ICollection |
|
91 | - * @throws CollectionException when the collection could not be found |
|
92 | - * @since 16.0.0 |
|
93 | - */ |
|
94 | - public function getCollectionForUser(int $id, ?IUser $user): ICollection { |
|
95 | - $query = $this->connection->getQueryBuilder(); |
|
96 | - $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
97 | - |
|
98 | - $query->select('*') |
|
99 | - ->from(self::TABLE_COLLECTIONS, 'c') |
|
100 | - ->leftJoin( |
|
101 | - 'c', self::TABLE_ACCESS_CACHE, 'a', |
|
102 | - $query->expr()->andX( |
|
103 | - $query->expr()->eq('c.id', 'a.collection_id'), |
|
104 | - $query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) |
|
105 | - ) |
|
106 | - ) |
|
107 | - ->where($query->expr()->eq('c.id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
108 | - $result = $query->execute(); |
|
109 | - $row = $result->fetch(); |
|
110 | - $result->closeCursor(); |
|
111 | - |
|
112 | - if (!$row) { |
|
113 | - throw new CollectionException('Collection not found'); |
|
114 | - } |
|
115 | - |
|
116 | - $access = $row['access'] === null ? null : (bool) $row['access']; |
|
117 | - if ($user instanceof IUser) { |
|
118 | - return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], $user, $access); |
|
119 | - } |
|
120 | - |
|
121 | - return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], $user, $access); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @param IUser $user |
|
126 | - * @param string $filter |
|
127 | - * @param int $limit |
|
128 | - * @param int $start |
|
129 | - * @return ICollection[] |
|
130 | - * @since 16.0.0 |
|
131 | - */ |
|
132 | - public function searchCollections(IUser $user, string $filter, int $limit = 50, int $start = 0): array { |
|
133 | - $query = $this->connection->getQueryBuilder(); |
|
134 | - $userId = $user->getUID(); |
|
135 | - |
|
136 | - $query->select('c.*', 'a.access') |
|
137 | - ->from(self::TABLE_COLLECTIONS, 'c') |
|
138 | - ->leftJoin( |
|
139 | - 'c', self::TABLE_ACCESS_CACHE, 'a', |
|
140 | - $query->expr()->andX( |
|
141 | - $query->expr()->eq('c.id', 'a.collection_id'), |
|
142 | - $query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) |
|
143 | - ) |
|
144 | - ) |
|
145 | - ->where($query->expr()->eq('a.access', $query->createNamedParameter(1, IQueryBuilder::PARAM_INT))) |
|
146 | - ->orderBy('c.id') |
|
147 | - ->setMaxResults($limit) |
|
148 | - ->setFirstResult($start); |
|
149 | - |
|
150 | - if ($filter !== '') { |
|
151 | - $query->where($query->expr()->iLike('c.name', $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($filter) . '%'))); |
|
152 | - } |
|
153 | - |
|
154 | - $result = $query->execute(); |
|
155 | - $collections = []; |
|
156 | - |
|
157 | - $foundResults = 0; |
|
158 | - while ($row = $result->fetch()) { |
|
159 | - $foundResults++; |
|
160 | - $access = $row['access'] === null ? null : (bool) $row['access']; |
|
161 | - $collection = new Collection($this, $this->connection, (int)$row['id'], (string)$row['name'], $user, $access); |
|
162 | - if ($collection->canAccess($user)) { |
|
163 | - $collections[] = $collection; |
|
164 | - } |
|
165 | - } |
|
166 | - $result->closeCursor(); |
|
167 | - |
|
168 | - if (empty($collections) && $foundResults === $limit) { |
|
169 | - return $this->searchCollections($user, $filter, $limit, $start + $limit); |
|
170 | - } |
|
171 | - |
|
172 | - return $collections; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @param string $name |
|
177 | - * @return ICollection |
|
178 | - * @since 16.0.0 |
|
179 | - */ |
|
180 | - public function newCollection(string $name): ICollection { |
|
181 | - $query = $this->connection->getQueryBuilder(); |
|
182 | - $query->insert(self::TABLE_COLLECTIONS) |
|
183 | - ->values([ |
|
184 | - 'name' => $query->createNamedParameter($name), |
|
185 | - ]); |
|
186 | - $query->execute(); |
|
187 | - |
|
188 | - return new Collection($this, $this->connection, $query->getLastInsertId(), $name); |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * @param string $type |
|
193 | - * @param string $id |
|
194 | - * @return IResource |
|
195 | - * @since 16.0.0 |
|
196 | - */ |
|
197 | - public function createResource(string $type, string $id): IResource { |
|
198 | - return new Resource($this, $this->connection, $type, $id); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * @param string $type |
|
203 | - * @param string $id |
|
204 | - * @param IUser|null $user |
|
205 | - * @return IResource |
|
206 | - * @throws ResourceException |
|
207 | - * @since 16.0.0 |
|
208 | - */ |
|
209 | - public function getResourceForUser(string $type, string $id, ?IUser $user): IResource { |
|
210 | - $query = $this->connection->getQueryBuilder(); |
|
211 | - $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
212 | - |
|
213 | - $query->select('r.*', 'a.access') |
|
214 | - ->from(self::TABLE_RESOURCES, 'r') |
|
215 | - ->leftJoin( |
|
216 | - 'r', self::TABLE_ACCESS_CACHE, 'a', |
|
217 | - $query->expr()->andX( |
|
218 | - $query->expr()->eq('r.resource_id', 'a.resource_id'), |
|
219 | - $query->expr()->eq('r.resource_type', 'a.resource_type'), |
|
220 | - $query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) |
|
221 | - ) |
|
222 | - ) |
|
223 | - ->where($query->expr()->eq('r.resource_type', $query->createNamedParameter($type, IQueryBuilder::PARAM_STR))) |
|
224 | - ->andWhere($query->expr()->eq('r.resource_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_STR))); |
|
225 | - $result = $query->execute(); |
|
226 | - $row = $result->fetch(); |
|
227 | - $result->closeCursor(); |
|
228 | - |
|
229 | - if (!$row) { |
|
230 | - throw new ResourceException('Resource not found'); |
|
231 | - } |
|
232 | - |
|
233 | - $access = $row['access'] === null ? null : (bool) $row['access']; |
|
234 | - if ($user instanceof IUser) { |
|
235 | - return new Resource($this, $this->connection, $type, $id, $user, $access); |
|
236 | - } |
|
237 | - |
|
238 | - return new Resource($this, $this->connection, $type, $id, null, $access); |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * @param ICollection $collection |
|
243 | - * @param IUser|null $user |
|
244 | - * @return IResource[] |
|
245 | - * @since 16.0.0 |
|
246 | - */ |
|
247 | - public function getResourcesByCollectionForUser(ICollection $collection, ?IUser $user): array { |
|
248 | - $query = $this->connection->getQueryBuilder(); |
|
249 | - $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
250 | - |
|
251 | - $query->select('r.*', 'a.access') |
|
252 | - ->from(self::TABLE_RESOURCES, 'r') |
|
253 | - ->leftJoin( |
|
254 | - 'r', self::TABLE_ACCESS_CACHE, 'a', |
|
255 | - $query->expr()->andX( |
|
256 | - $query->expr()->eq('r.resource_id', 'a.resource_id'), |
|
257 | - $query->expr()->eq('r.resource_type', 'a.resource_type'), |
|
258 | - $query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) |
|
259 | - ) |
|
260 | - ) |
|
261 | - ->where($query->expr()->eq('r.collection_id', $query->createNamedParameter($collection->getId(), IQueryBuilder::PARAM_INT))); |
|
262 | - |
|
263 | - $resources = []; |
|
264 | - $result = $query->execute(); |
|
265 | - while ($row = $result->fetch()) { |
|
266 | - $access = $row['access'] === null ? null : (bool) $row['access']; |
|
267 | - $resources[] = new Resource($this, $this->connection, $row['resource_type'], $row['resource_id'], $user, $access); |
|
268 | - } |
|
269 | - $result->closeCursor(); |
|
270 | - |
|
271 | - return $resources; |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Get the rich object data of a resource |
|
276 | - * |
|
277 | - * @param IResource $resource |
|
278 | - * @return array |
|
279 | - * @since 16.0.0 |
|
280 | - */ |
|
281 | - public function getResourceRichObject(IResource $resource): array { |
|
282 | - foreach ($this->providerManager->getResourceProviders() as $provider) { |
|
283 | - if ($provider->getType() === $resource->getType()) { |
|
284 | - try { |
|
285 | - return $provider->getResourceRichObject($resource); |
|
286 | - } catch (ResourceException $e) { |
|
287 | - } |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - return []; |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Can a user/guest access the collection |
|
296 | - * |
|
297 | - * @param IResource $resource |
|
298 | - * @param IUser|null $user |
|
299 | - * @return bool |
|
300 | - * @since 16.0.0 |
|
301 | - */ |
|
302 | - public function canAccessResource(IResource $resource, ?IUser $user): bool { |
|
303 | - $access = $this->checkAccessCacheForUserByResource($resource, $user); |
|
304 | - if (\is_bool($access)) { |
|
305 | - return $access; |
|
306 | - } |
|
307 | - |
|
308 | - $access = false; |
|
309 | - foreach ($this->providerManager->getResourceProviders() as $provider) { |
|
310 | - if ($provider->getType() === $resource->getType()) { |
|
311 | - try { |
|
312 | - if ($provider->canAccessResource($resource, $user)) { |
|
313 | - $access = true; |
|
314 | - break; |
|
315 | - } |
|
316 | - } catch (ResourceException $e) { |
|
317 | - } |
|
318 | - } |
|
319 | - } |
|
320 | - |
|
321 | - $this->cacheAccessForResource($resource, $user, $access); |
|
322 | - return $access; |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * Can a user/guest access the collection |
|
327 | - * |
|
328 | - * @param ICollection $collection |
|
329 | - * @param IUser|null $user |
|
330 | - * @return bool |
|
331 | - * @since 16.0.0 |
|
332 | - */ |
|
333 | - public function canAccessCollection(ICollection $collection, ?IUser $user): bool { |
|
334 | - $access = $this->checkAccessCacheForUserByCollection($collection, $user); |
|
335 | - if (\is_bool($access)) { |
|
336 | - return $access; |
|
337 | - } |
|
338 | - |
|
339 | - $access = null; |
|
340 | - // Access is granted when a user can access all resources |
|
341 | - foreach ($collection->getResources() as $resource) { |
|
342 | - if (!$resource->canAccess($user)) { |
|
343 | - $access = false; |
|
344 | - break; |
|
345 | - } |
|
346 | - |
|
347 | - $access = true; |
|
348 | - } |
|
349 | - |
|
350 | - $this->cacheAccessForCollection($collection, $user, $access); |
|
351 | - return $access; |
|
352 | - } |
|
353 | - |
|
354 | - protected function checkAccessCacheForUserByResource(IResource $resource, ?IUser $user): ?bool { |
|
355 | - $query = $this->connection->getQueryBuilder(); |
|
356 | - $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
357 | - |
|
358 | - $query->select('access') |
|
359 | - ->from(self::TABLE_ACCESS_CACHE) |
|
360 | - ->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId(), IQueryBuilder::PARAM_STR))) |
|
361 | - ->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType(), IQueryBuilder::PARAM_STR))) |
|
362 | - ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR))) |
|
363 | - ->setMaxResults(1); |
|
364 | - |
|
365 | - $hasAccess = null; |
|
366 | - $result = $query->execute(); |
|
367 | - if ($row = $result->fetch()) { |
|
368 | - $hasAccess = (bool) $row['access']; |
|
369 | - } |
|
370 | - $result->closeCursor(); |
|
371 | - |
|
372 | - return $hasAccess; |
|
373 | - } |
|
374 | - |
|
375 | - protected function checkAccessCacheForUserByCollection(ICollection $collection, ?IUser $user): ?bool { |
|
376 | - $query = $this->connection->getQueryBuilder(); |
|
377 | - $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
378 | - |
|
379 | - $query->select('access') |
|
380 | - ->from(self::TABLE_ACCESS_CACHE) |
|
381 | - ->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId(), IQueryBuilder::PARAM_INT))) |
|
382 | - ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR))) |
|
383 | - ->setMaxResults(1); |
|
384 | - |
|
385 | - $hasAccess = null; |
|
386 | - $result = $query->execute(); |
|
387 | - if ($row = $result->fetch()) { |
|
388 | - $hasAccess = (bool) $row['access']; |
|
389 | - } |
|
390 | - $result->closeCursor(); |
|
391 | - |
|
392 | - return $hasAccess; |
|
393 | - } |
|
394 | - |
|
395 | - public function cacheAccessForResource(IResource $resource, ?IUser $user, bool $access): void { |
|
396 | - $query = $this->connection->getQueryBuilder(); |
|
397 | - $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
398 | - |
|
399 | - $query->insert(self::TABLE_ACCESS_CACHE) |
|
400 | - ->values([ |
|
401 | - 'user_id' => $query->createNamedParameter($userId), |
|
402 | - 'resource_id' => $query->createNamedParameter($resource->getId()), |
|
403 | - 'resource_type' => $query->createNamedParameter($resource->getType()), |
|
404 | - 'access' => $query->createNamedParameter($access, IQueryBuilder::PARAM_BOOL), |
|
405 | - ]); |
|
406 | - try { |
|
407 | - $query->execute(); |
|
408 | - } catch (UniqueConstraintViolationException $e) { |
|
409 | - } |
|
410 | - } |
|
411 | - |
|
412 | - public function cacheAccessForCollection(ICollection $collection, ?IUser $user, bool $access): void { |
|
413 | - $query = $this->connection->getQueryBuilder(); |
|
414 | - $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
415 | - |
|
416 | - $query->insert(self::TABLE_ACCESS_CACHE) |
|
417 | - ->values([ |
|
418 | - 'user_id' => $query->createNamedParameter($userId), |
|
419 | - 'collection_id' => $query->createNamedParameter($collection->getId()), |
|
420 | - 'access' => $query->createNamedParameter($access, IQueryBuilder::PARAM_BOOL), |
|
421 | - ]); |
|
422 | - try { |
|
423 | - $query->execute(); |
|
424 | - } catch (UniqueConstraintViolationException $e) { |
|
425 | - } |
|
426 | - } |
|
427 | - |
|
428 | - public function invalidateAccessCacheForUser(?IUser $user): void { |
|
429 | - $query = $this->connection->getQueryBuilder(); |
|
430 | - $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
431 | - |
|
432 | - $query->delete(self::TABLE_ACCESS_CACHE) |
|
433 | - ->where($query->expr()->eq('user_id', $query->createNamedParameter($userId))); |
|
434 | - $query->execute(); |
|
435 | - } |
|
436 | - |
|
437 | - public function invalidateAccessCacheForResource(IResource $resource): void { |
|
438 | - $query = $this->connection->getQueryBuilder(); |
|
439 | - |
|
440 | - $query->delete(self::TABLE_ACCESS_CACHE) |
|
441 | - ->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId()))) |
|
442 | - ->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType(), IQueryBuilder::PARAM_STR))); |
|
443 | - $query->execute(); |
|
444 | - |
|
445 | - foreach ($resource->getCollections() as $collection) { |
|
446 | - $this->invalidateAccessCacheForCollection($collection); |
|
447 | - } |
|
448 | - } |
|
449 | - |
|
450 | - public function invalidateAccessCacheForAllCollections(): void { |
|
451 | - $query = $this->connection->getQueryBuilder(); |
|
452 | - |
|
453 | - $query->delete(self::TABLE_ACCESS_CACHE) |
|
454 | - ->where($query->expr()->neq('collection_id', $query->createNamedParameter(0))); |
|
455 | - $query->execute(); |
|
456 | - } |
|
457 | - |
|
458 | - public function invalidateAccessCacheForCollection(ICollection $collection): void { |
|
459 | - $query = $this->connection->getQueryBuilder(); |
|
460 | - |
|
461 | - $query->delete(self::TABLE_ACCESS_CACHE) |
|
462 | - ->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId()))); |
|
463 | - $query->execute(); |
|
464 | - } |
|
465 | - |
|
466 | - public function invalidateAccessCacheForProvider(IProvider $provider): void { |
|
467 | - $query = $this->connection->getQueryBuilder(); |
|
468 | - |
|
469 | - $query->delete(self::TABLE_ACCESS_CACHE) |
|
470 | - ->where($query->expr()->eq('resource_type', $query->createNamedParameter($provider->getType(), IQueryBuilder::PARAM_STR))); |
|
471 | - $query->execute(); |
|
472 | - } |
|
473 | - |
|
474 | - public function invalidateAccessCacheForResourceByUser(IResource $resource, ?IUser $user): void { |
|
475 | - $query = $this->connection->getQueryBuilder(); |
|
476 | - $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
477 | - |
|
478 | - $query->delete(self::TABLE_ACCESS_CACHE) |
|
479 | - ->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId()))) |
|
480 | - ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId))); |
|
481 | - $query->execute(); |
|
482 | - |
|
483 | - foreach ($resource->getCollections() as $collection) { |
|
484 | - $this->invalidateAccessCacheForCollectionByUser($collection, $user); |
|
485 | - } |
|
486 | - } |
|
487 | - |
|
488 | - protected function invalidateAccessCacheForCollectionByUser(ICollection $collection, ?IUser $user): void { |
|
489 | - $query = $this->connection->getQueryBuilder(); |
|
490 | - $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
491 | - |
|
492 | - $query->delete(self::TABLE_ACCESS_CACHE) |
|
493 | - ->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId()))) |
|
494 | - ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId))); |
|
495 | - $query->execute(); |
|
496 | - } |
|
497 | - |
|
498 | - public function invalidateAccessCacheForProviderByUser(IProvider $provider, ?IUser $user): void { |
|
499 | - $query = $this->connection->getQueryBuilder(); |
|
500 | - $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
501 | - |
|
502 | - $query->delete(self::TABLE_ACCESS_CACHE) |
|
503 | - ->where($query->expr()->eq('resource_type', $query->createNamedParameter($provider->getType(), IQueryBuilder::PARAM_STR))) |
|
504 | - ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId))); |
|
505 | - $query->execute(); |
|
506 | - } |
|
507 | - |
|
508 | - /** |
|
509 | - * @param string $provider |
|
510 | - */ |
|
511 | - public function registerResourceProvider(string $provider): void { |
|
512 | - $this->logger->debug('\OC\Collaboration\Resources\Manager::registerResourceProvider is deprecated', ['provider' => $provider]); |
|
513 | - $this->providerManager->registerResourceProvider($provider); |
|
514 | - } |
|
515 | - |
|
516 | - /** |
|
517 | - * Get the resource type of the provider |
|
518 | - * |
|
519 | - * @return string |
|
520 | - * @since 16.0.0 |
|
521 | - */ |
|
522 | - public function getType(): string { |
|
523 | - return ''; |
|
524 | - } |
|
44 | + public const TABLE_COLLECTIONS = 'collres_collections'; |
|
45 | + public const TABLE_RESOURCES = 'collres_resources'; |
|
46 | + public const TABLE_ACCESS_CACHE = 'collres_accesscache'; |
|
47 | + |
|
48 | + /** @var IDBConnection */ |
|
49 | + protected $connection; |
|
50 | + /** @var IProviderManager */ |
|
51 | + protected $providerManager; |
|
52 | + /** @var LoggerInterface */ |
|
53 | + protected $logger; |
|
54 | + |
|
55 | + /** @var string[] */ |
|
56 | + protected $providers = []; |
|
57 | + |
|
58 | + |
|
59 | + public function __construct(IDBConnection $connection, IProviderManager $providerManager, LoggerInterface $logger) { |
|
60 | + $this->connection = $connection; |
|
61 | + $this->providerManager = $providerManager; |
|
62 | + $this->logger = $logger; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @param int $id |
|
67 | + * @return ICollection |
|
68 | + * @throws CollectionException when the collection could not be found |
|
69 | + * @since 16.0.0 |
|
70 | + */ |
|
71 | + public function getCollection(int $id): ICollection { |
|
72 | + $query = $this->connection->getQueryBuilder(); |
|
73 | + $query->select('*') |
|
74 | + ->from(self::TABLE_COLLECTIONS) |
|
75 | + ->where($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
76 | + $result = $query->execute(); |
|
77 | + $row = $result->fetch(); |
|
78 | + $result->closeCursor(); |
|
79 | + |
|
80 | + if (!$row) { |
|
81 | + throw new CollectionException('Collection not found'); |
|
82 | + } |
|
83 | + |
|
84 | + return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name']); |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @param int $id |
|
89 | + * @param IUser|null $user |
|
90 | + * @return ICollection |
|
91 | + * @throws CollectionException when the collection could not be found |
|
92 | + * @since 16.0.0 |
|
93 | + */ |
|
94 | + public function getCollectionForUser(int $id, ?IUser $user): ICollection { |
|
95 | + $query = $this->connection->getQueryBuilder(); |
|
96 | + $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
97 | + |
|
98 | + $query->select('*') |
|
99 | + ->from(self::TABLE_COLLECTIONS, 'c') |
|
100 | + ->leftJoin( |
|
101 | + 'c', self::TABLE_ACCESS_CACHE, 'a', |
|
102 | + $query->expr()->andX( |
|
103 | + $query->expr()->eq('c.id', 'a.collection_id'), |
|
104 | + $query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) |
|
105 | + ) |
|
106 | + ) |
|
107 | + ->where($query->expr()->eq('c.id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT))); |
|
108 | + $result = $query->execute(); |
|
109 | + $row = $result->fetch(); |
|
110 | + $result->closeCursor(); |
|
111 | + |
|
112 | + if (!$row) { |
|
113 | + throw new CollectionException('Collection not found'); |
|
114 | + } |
|
115 | + |
|
116 | + $access = $row['access'] === null ? null : (bool) $row['access']; |
|
117 | + if ($user instanceof IUser) { |
|
118 | + return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], $user, $access); |
|
119 | + } |
|
120 | + |
|
121 | + return new Collection($this, $this->connection, (int) $row['id'], (string) $row['name'], $user, $access); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @param IUser $user |
|
126 | + * @param string $filter |
|
127 | + * @param int $limit |
|
128 | + * @param int $start |
|
129 | + * @return ICollection[] |
|
130 | + * @since 16.0.0 |
|
131 | + */ |
|
132 | + public function searchCollections(IUser $user, string $filter, int $limit = 50, int $start = 0): array { |
|
133 | + $query = $this->connection->getQueryBuilder(); |
|
134 | + $userId = $user->getUID(); |
|
135 | + |
|
136 | + $query->select('c.*', 'a.access') |
|
137 | + ->from(self::TABLE_COLLECTIONS, 'c') |
|
138 | + ->leftJoin( |
|
139 | + 'c', self::TABLE_ACCESS_CACHE, 'a', |
|
140 | + $query->expr()->andX( |
|
141 | + $query->expr()->eq('c.id', 'a.collection_id'), |
|
142 | + $query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) |
|
143 | + ) |
|
144 | + ) |
|
145 | + ->where($query->expr()->eq('a.access', $query->createNamedParameter(1, IQueryBuilder::PARAM_INT))) |
|
146 | + ->orderBy('c.id') |
|
147 | + ->setMaxResults($limit) |
|
148 | + ->setFirstResult($start); |
|
149 | + |
|
150 | + if ($filter !== '') { |
|
151 | + $query->where($query->expr()->iLike('c.name', $query->createNamedParameter('%' . $this->connection->escapeLikeParameter($filter) . '%'))); |
|
152 | + } |
|
153 | + |
|
154 | + $result = $query->execute(); |
|
155 | + $collections = []; |
|
156 | + |
|
157 | + $foundResults = 0; |
|
158 | + while ($row = $result->fetch()) { |
|
159 | + $foundResults++; |
|
160 | + $access = $row['access'] === null ? null : (bool) $row['access']; |
|
161 | + $collection = new Collection($this, $this->connection, (int)$row['id'], (string)$row['name'], $user, $access); |
|
162 | + if ($collection->canAccess($user)) { |
|
163 | + $collections[] = $collection; |
|
164 | + } |
|
165 | + } |
|
166 | + $result->closeCursor(); |
|
167 | + |
|
168 | + if (empty($collections) && $foundResults === $limit) { |
|
169 | + return $this->searchCollections($user, $filter, $limit, $start + $limit); |
|
170 | + } |
|
171 | + |
|
172 | + return $collections; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @param string $name |
|
177 | + * @return ICollection |
|
178 | + * @since 16.0.0 |
|
179 | + */ |
|
180 | + public function newCollection(string $name): ICollection { |
|
181 | + $query = $this->connection->getQueryBuilder(); |
|
182 | + $query->insert(self::TABLE_COLLECTIONS) |
|
183 | + ->values([ |
|
184 | + 'name' => $query->createNamedParameter($name), |
|
185 | + ]); |
|
186 | + $query->execute(); |
|
187 | + |
|
188 | + return new Collection($this, $this->connection, $query->getLastInsertId(), $name); |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * @param string $type |
|
193 | + * @param string $id |
|
194 | + * @return IResource |
|
195 | + * @since 16.0.0 |
|
196 | + */ |
|
197 | + public function createResource(string $type, string $id): IResource { |
|
198 | + return new Resource($this, $this->connection, $type, $id); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * @param string $type |
|
203 | + * @param string $id |
|
204 | + * @param IUser|null $user |
|
205 | + * @return IResource |
|
206 | + * @throws ResourceException |
|
207 | + * @since 16.0.0 |
|
208 | + */ |
|
209 | + public function getResourceForUser(string $type, string $id, ?IUser $user): IResource { |
|
210 | + $query = $this->connection->getQueryBuilder(); |
|
211 | + $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
212 | + |
|
213 | + $query->select('r.*', 'a.access') |
|
214 | + ->from(self::TABLE_RESOURCES, 'r') |
|
215 | + ->leftJoin( |
|
216 | + 'r', self::TABLE_ACCESS_CACHE, 'a', |
|
217 | + $query->expr()->andX( |
|
218 | + $query->expr()->eq('r.resource_id', 'a.resource_id'), |
|
219 | + $query->expr()->eq('r.resource_type', 'a.resource_type'), |
|
220 | + $query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) |
|
221 | + ) |
|
222 | + ) |
|
223 | + ->where($query->expr()->eq('r.resource_type', $query->createNamedParameter($type, IQueryBuilder::PARAM_STR))) |
|
224 | + ->andWhere($query->expr()->eq('r.resource_id', $query->createNamedParameter($id, IQueryBuilder::PARAM_STR))); |
|
225 | + $result = $query->execute(); |
|
226 | + $row = $result->fetch(); |
|
227 | + $result->closeCursor(); |
|
228 | + |
|
229 | + if (!$row) { |
|
230 | + throw new ResourceException('Resource not found'); |
|
231 | + } |
|
232 | + |
|
233 | + $access = $row['access'] === null ? null : (bool) $row['access']; |
|
234 | + if ($user instanceof IUser) { |
|
235 | + return new Resource($this, $this->connection, $type, $id, $user, $access); |
|
236 | + } |
|
237 | + |
|
238 | + return new Resource($this, $this->connection, $type, $id, null, $access); |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * @param ICollection $collection |
|
243 | + * @param IUser|null $user |
|
244 | + * @return IResource[] |
|
245 | + * @since 16.0.0 |
|
246 | + */ |
|
247 | + public function getResourcesByCollectionForUser(ICollection $collection, ?IUser $user): array { |
|
248 | + $query = $this->connection->getQueryBuilder(); |
|
249 | + $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
250 | + |
|
251 | + $query->select('r.*', 'a.access') |
|
252 | + ->from(self::TABLE_RESOURCES, 'r') |
|
253 | + ->leftJoin( |
|
254 | + 'r', self::TABLE_ACCESS_CACHE, 'a', |
|
255 | + $query->expr()->andX( |
|
256 | + $query->expr()->eq('r.resource_id', 'a.resource_id'), |
|
257 | + $query->expr()->eq('r.resource_type', 'a.resource_type'), |
|
258 | + $query->expr()->eq('a.user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR)) |
|
259 | + ) |
|
260 | + ) |
|
261 | + ->where($query->expr()->eq('r.collection_id', $query->createNamedParameter($collection->getId(), IQueryBuilder::PARAM_INT))); |
|
262 | + |
|
263 | + $resources = []; |
|
264 | + $result = $query->execute(); |
|
265 | + while ($row = $result->fetch()) { |
|
266 | + $access = $row['access'] === null ? null : (bool) $row['access']; |
|
267 | + $resources[] = new Resource($this, $this->connection, $row['resource_type'], $row['resource_id'], $user, $access); |
|
268 | + } |
|
269 | + $result->closeCursor(); |
|
270 | + |
|
271 | + return $resources; |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Get the rich object data of a resource |
|
276 | + * |
|
277 | + * @param IResource $resource |
|
278 | + * @return array |
|
279 | + * @since 16.0.0 |
|
280 | + */ |
|
281 | + public function getResourceRichObject(IResource $resource): array { |
|
282 | + foreach ($this->providerManager->getResourceProviders() as $provider) { |
|
283 | + if ($provider->getType() === $resource->getType()) { |
|
284 | + try { |
|
285 | + return $provider->getResourceRichObject($resource); |
|
286 | + } catch (ResourceException $e) { |
|
287 | + } |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + return []; |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Can a user/guest access the collection |
|
296 | + * |
|
297 | + * @param IResource $resource |
|
298 | + * @param IUser|null $user |
|
299 | + * @return bool |
|
300 | + * @since 16.0.0 |
|
301 | + */ |
|
302 | + public function canAccessResource(IResource $resource, ?IUser $user): bool { |
|
303 | + $access = $this->checkAccessCacheForUserByResource($resource, $user); |
|
304 | + if (\is_bool($access)) { |
|
305 | + return $access; |
|
306 | + } |
|
307 | + |
|
308 | + $access = false; |
|
309 | + foreach ($this->providerManager->getResourceProviders() as $provider) { |
|
310 | + if ($provider->getType() === $resource->getType()) { |
|
311 | + try { |
|
312 | + if ($provider->canAccessResource($resource, $user)) { |
|
313 | + $access = true; |
|
314 | + break; |
|
315 | + } |
|
316 | + } catch (ResourceException $e) { |
|
317 | + } |
|
318 | + } |
|
319 | + } |
|
320 | + |
|
321 | + $this->cacheAccessForResource($resource, $user, $access); |
|
322 | + return $access; |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * Can a user/guest access the collection |
|
327 | + * |
|
328 | + * @param ICollection $collection |
|
329 | + * @param IUser|null $user |
|
330 | + * @return bool |
|
331 | + * @since 16.0.0 |
|
332 | + */ |
|
333 | + public function canAccessCollection(ICollection $collection, ?IUser $user): bool { |
|
334 | + $access = $this->checkAccessCacheForUserByCollection($collection, $user); |
|
335 | + if (\is_bool($access)) { |
|
336 | + return $access; |
|
337 | + } |
|
338 | + |
|
339 | + $access = null; |
|
340 | + // Access is granted when a user can access all resources |
|
341 | + foreach ($collection->getResources() as $resource) { |
|
342 | + if (!$resource->canAccess($user)) { |
|
343 | + $access = false; |
|
344 | + break; |
|
345 | + } |
|
346 | + |
|
347 | + $access = true; |
|
348 | + } |
|
349 | + |
|
350 | + $this->cacheAccessForCollection($collection, $user, $access); |
|
351 | + return $access; |
|
352 | + } |
|
353 | + |
|
354 | + protected function checkAccessCacheForUserByResource(IResource $resource, ?IUser $user): ?bool { |
|
355 | + $query = $this->connection->getQueryBuilder(); |
|
356 | + $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
357 | + |
|
358 | + $query->select('access') |
|
359 | + ->from(self::TABLE_ACCESS_CACHE) |
|
360 | + ->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId(), IQueryBuilder::PARAM_STR))) |
|
361 | + ->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType(), IQueryBuilder::PARAM_STR))) |
|
362 | + ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR))) |
|
363 | + ->setMaxResults(1); |
|
364 | + |
|
365 | + $hasAccess = null; |
|
366 | + $result = $query->execute(); |
|
367 | + if ($row = $result->fetch()) { |
|
368 | + $hasAccess = (bool) $row['access']; |
|
369 | + } |
|
370 | + $result->closeCursor(); |
|
371 | + |
|
372 | + return $hasAccess; |
|
373 | + } |
|
374 | + |
|
375 | + protected function checkAccessCacheForUserByCollection(ICollection $collection, ?IUser $user): ?bool { |
|
376 | + $query = $this->connection->getQueryBuilder(); |
|
377 | + $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
378 | + |
|
379 | + $query->select('access') |
|
380 | + ->from(self::TABLE_ACCESS_CACHE) |
|
381 | + ->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId(), IQueryBuilder::PARAM_INT))) |
|
382 | + ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId, IQueryBuilder::PARAM_STR))) |
|
383 | + ->setMaxResults(1); |
|
384 | + |
|
385 | + $hasAccess = null; |
|
386 | + $result = $query->execute(); |
|
387 | + if ($row = $result->fetch()) { |
|
388 | + $hasAccess = (bool) $row['access']; |
|
389 | + } |
|
390 | + $result->closeCursor(); |
|
391 | + |
|
392 | + return $hasAccess; |
|
393 | + } |
|
394 | + |
|
395 | + public function cacheAccessForResource(IResource $resource, ?IUser $user, bool $access): void { |
|
396 | + $query = $this->connection->getQueryBuilder(); |
|
397 | + $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
398 | + |
|
399 | + $query->insert(self::TABLE_ACCESS_CACHE) |
|
400 | + ->values([ |
|
401 | + 'user_id' => $query->createNamedParameter($userId), |
|
402 | + 'resource_id' => $query->createNamedParameter($resource->getId()), |
|
403 | + 'resource_type' => $query->createNamedParameter($resource->getType()), |
|
404 | + 'access' => $query->createNamedParameter($access, IQueryBuilder::PARAM_BOOL), |
|
405 | + ]); |
|
406 | + try { |
|
407 | + $query->execute(); |
|
408 | + } catch (UniqueConstraintViolationException $e) { |
|
409 | + } |
|
410 | + } |
|
411 | + |
|
412 | + public function cacheAccessForCollection(ICollection $collection, ?IUser $user, bool $access): void { |
|
413 | + $query = $this->connection->getQueryBuilder(); |
|
414 | + $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
415 | + |
|
416 | + $query->insert(self::TABLE_ACCESS_CACHE) |
|
417 | + ->values([ |
|
418 | + 'user_id' => $query->createNamedParameter($userId), |
|
419 | + 'collection_id' => $query->createNamedParameter($collection->getId()), |
|
420 | + 'access' => $query->createNamedParameter($access, IQueryBuilder::PARAM_BOOL), |
|
421 | + ]); |
|
422 | + try { |
|
423 | + $query->execute(); |
|
424 | + } catch (UniqueConstraintViolationException $e) { |
|
425 | + } |
|
426 | + } |
|
427 | + |
|
428 | + public function invalidateAccessCacheForUser(?IUser $user): void { |
|
429 | + $query = $this->connection->getQueryBuilder(); |
|
430 | + $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
431 | + |
|
432 | + $query->delete(self::TABLE_ACCESS_CACHE) |
|
433 | + ->where($query->expr()->eq('user_id', $query->createNamedParameter($userId))); |
|
434 | + $query->execute(); |
|
435 | + } |
|
436 | + |
|
437 | + public function invalidateAccessCacheForResource(IResource $resource): void { |
|
438 | + $query = $this->connection->getQueryBuilder(); |
|
439 | + |
|
440 | + $query->delete(self::TABLE_ACCESS_CACHE) |
|
441 | + ->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId()))) |
|
442 | + ->andWhere($query->expr()->eq('resource_type', $query->createNamedParameter($resource->getType(), IQueryBuilder::PARAM_STR))); |
|
443 | + $query->execute(); |
|
444 | + |
|
445 | + foreach ($resource->getCollections() as $collection) { |
|
446 | + $this->invalidateAccessCacheForCollection($collection); |
|
447 | + } |
|
448 | + } |
|
449 | + |
|
450 | + public function invalidateAccessCacheForAllCollections(): void { |
|
451 | + $query = $this->connection->getQueryBuilder(); |
|
452 | + |
|
453 | + $query->delete(self::TABLE_ACCESS_CACHE) |
|
454 | + ->where($query->expr()->neq('collection_id', $query->createNamedParameter(0))); |
|
455 | + $query->execute(); |
|
456 | + } |
|
457 | + |
|
458 | + public function invalidateAccessCacheForCollection(ICollection $collection): void { |
|
459 | + $query = $this->connection->getQueryBuilder(); |
|
460 | + |
|
461 | + $query->delete(self::TABLE_ACCESS_CACHE) |
|
462 | + ->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId()))); |
|
463 | + $query->execute(); |
|
464 | + } |
|
465 | + |
|
466 | + public function invalidateAccessCacheForProvider(IProvider $provider): void { |
|
467 | + $query = $this->connection->getQueryBuilder(); |
|
468 | + |
|
469 | + $query->delete(self::TABLE_ACCESS_CACHE) |
|
470 | + ->where($query->expr()->eq('resource_type', $query->createNamedParameter($provider->getType(), IQueryBuilder::PARAM_STR))); |
|
471 | + $query->execute(); |
|
472 | + } |
|
473 | + |
|
474 | + public function invalidateAccessCacheForResourceByUser(IResource $resource, ?IUser $user): void { |
|
475 | + $query = $this->connection->getQueryBuilder(); |
|
476 | + $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
477 | + |
|
478 | + $query->delete(self::TABLE_ACCESS_CACHE) |
|
479 | + ->where($query->expr()->eq('resource_id', $query->createNamedParameter($resource->getId()))) |
|
480 | + ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId))); |
|
481 | + $query->execute(); |
|
482 | + |
|
483 | + foreach ($resource->getCollections() as $collection) { |
|
484 | + $this->invalidateAccessCacheForCollectionByUser($collection, $user); |
|
485 | + } |
|
486 | + } |
|
487 | + |
|
488 | + protected function invalidateAccessCacheForCollectionByUser(ICollection $collection, ?IUser $user): void { |
|
489 | + $query = $this->connection->getQueryBuilder(); |
|
490 | + $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
491 | + |
|
492 | + $query->delete(self::TABLE_ACCESS_CACHE) |
|
493 | + ->where($query->expr()->eq('collection_id', $query->createNamedParameter($collection->getId()))) |
|
494 | + ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId))); |
|
495 | + $query->execute(); |
|
496 | + } |
|
497 | + |
|
498 | + public function invalidateAccessCacheForProviderByUser(IProvider $provider, ?IUser $user): void { |
|
499 | + $query = $this->connection->getQueryBuilder(); |
|
500 | + $userId = $user instanceof IUser ? $user->getUID() : ''; |
|
501 | + |
|
502 | + $query->delete(self::TABLE_ACCESS_CACHE) |
|
503 | + ->where($query->expr()->eq('resource_type', $query->createNamedParameter($provider->getType(), IQueryBuilder::PARAM_STR))) |
|
504 | + ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId))); |
|
505 | + $query->execute(); |
|
506 | + } |
|
507 | + |
|
508 | + /** |
|
509 | + * @param string $provider |
|
510 | + */ |
|
511 | + public function registerResourceProvider(string $provider): void { |
|
512 | + $this->logger->debug('\OC\Collaboration\Resources\Manager::registerResourceProvider is deprecated', ['provider' => $provider]); |
|
513 | + $this->providerManager->registerResourceProvider($provider); |
|
514 | + } |
|
515 | + |
|
516 | + /** |
|
517 | + * Get the resource type of the provider |
|
518 | + * |
|
519 | + * @return string |
|
520 | + * @since 16.0.0 |
|
521 | + */ |
|
522 | + public function getType(): string { |
|
523 | + return ''; |
|
524 | + } |
|
525 | 525 | } |