1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
<?php
/**
* WikiPage.php
* model class for table wiki
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author mlunzena
* @copyright (c) Authors
*
* @property int $id alias column for page_id
* @property int $page_id database column
* @property string $range_id database column
* @property string $name database column
* @property string|null $content database column
* @property int|null $parent_id database column
* @property string $read_permission database column
* @property string $write_permission database column
* @property string $user_id database column
* @property int|null $locked_since database column
* @property string|null $locked_by_user_id database column
* @property int $chdate database column
* @property int $mkdate database column
* @property SimpleORMapCollection<WikiVersion> $versions
* @property SimpleORMapCollection<WikiOnlineEditingUser> $onlineeditingusers
* @property User $user belongs_to User
* @property Course $course belongs_to Course
* @property-read (WikiPage | null) $parent additional field
* @property-read WikiPage[] $children additional field
* @property-read (WikiVersion | null) $predecessor additional field
* @property-read int $versionnumber additional field
*/
class WikiPage extends SimpleORMap implements PrivacyObject
{
/**
* Configures the model
* @param array $config Configuration
*/
protected static function configure($config = [])
{
$config['db_table'] = 'wiki_pages';
$config['belongs_to']['user'] = [
'class_name' => User::class,
'foreign_key' => 'user_id'
];
$config['belongs_to']['course'] = [
'class_name' => Course::class,
'foreign_key' => 'range_id',
];
$config['has_many']['versions'] = [
'class_name' => WikiVersion::class,
'foreign_key' => 'page_id',
'order_by' => 'ORDER BY mkdate DESC',
'on_delete' => 'delete',
];
$config['has_many']['onlineeditingusers'] = [
'class_name' => WikiOnlineEditingUser::class,
'foreign_key' => 'page_id',
'on_delete' => 'delete',
];
$config['additional_fields']['parent'] = [
'get' => function (WikiPage $page): ?WikiPage {
return self::find($page->parent_id);
}
];
$config['additional_fields']['children'] = [
'get' => function (WikiPage $page): array {
return self::findBySQL('parent_id = ?', [
$page->id
]);
}
];
$config['additional_fields']['predecessor'] = [
'get' => function (WikiPage $page): ?WikiVersion {
return $page->versions ? $page->versions[0] : null;
}
];
$config['additional_fields']['versionnumber'] = [
'get' => function (WikiPage $page): int {
return count($page->versions) + 1;
}
];
$config['registered_callbacks']['before_store'][] = 'createVersion';
$config['registered_callbacks']['after_delete'][] = function (WikiPage $page) {
$query = "UPDATE `wiki_pages` SET `parent_id` = NULL WHERE `parent_id` = ?";
DBManager::get()->execute($query, [$page->id]);
};
$config['default_values']['last_author'] = 'nobody';
parent::configure($config);
}
protected function createVersion()
{
$last_version = $this->versions[0];
if (
!$this->isNew()
&& $this->content['content'] !== $this->content_db['content']
&& (
$this->content_db['user_id'] !== $this->content['user_id']
|| $this->content_db['chdate'] < time() - 60 * 30
)
&& (!$last_version || $last_version['content'] !== $this['content'])
) {
//Neue Version anlegen:
WikiVersion::create([
'page_id' => $this->id,
'name' => $this->content_db['name'],
'content' => $this->content_db['content'],
'user_id' => $this->content_db['user_id'],
'mkdate' => $this->content_db['chdate'],
]);
}
return true;
}
public static function findByName($range_id, $name)
{
return self::findOneBySQL('BINARY name = :name AND range_id = :range_id', [
'range_id' => $range_id,
'name' => $name
]);
}
/**
* Returns whether this page is visible to the given user.
* @param string|null $user_id User id
* @return boolean indicating whether the page is visible
*/
public function isReadable(?string $user_id = null): bool
{
if ($this->isNew()) {
return true;
}
// anyone can see this page if it belongs to a free course
if (
$this->read_permission === 'all'
&& Config::get()->ENABLE_FREE_ACCESS
&& $this->course
&& !$this->course->lesezugriff
) {
return true;
}
if ($user_id === null && User::findCurrent()) {
$user_id = User::findCurrent()->id;
}
if ($this->read_permission === 'all') {
if ($GLOBALS['perm']->have_studip_perm('user', $this->range_id, $user_id)) {
return true;
}
$inst = Institute::find($this->range_id);
if ($inst) {
//in institutes anyone can read the wiki
return true;
}
}
if ($GLOBALS['perm']->have_studip_perm(
'dozent',
$this->range_id,
$user_id
)) {
return true;
}
if (in_array($this->read_permission, ['tutor', 'dozent'])) {
return $GLOBALS['perm']->have_studip_perm($this->read_permission, $this->range_id, $user_id);
} else {
return StatusgruppeUser::exists([$this->read_permission, $user_id]);
}
}
/**
* Returns whether this page is editable to the given user.
* @param string|null $user_id the ID of the user
* @return boolean indicating whether the page is editable
*/
public function isEditable(?string $user_id = null): bool
{
if ($user_id === null && User::findCurrent()) {
$user_id = User::findCurrent()->id;
}
if (
!$user_id
|| !$GLOBALS['perm']->have_studip_perm('user', $this->range_id, $user_id)
) {
return false;
}
// Check create permission if page is new
if ($this->isNew()) {
$range = RangeFactory::find($this->range_id, ['sem', 'inst']);
$permission = $range->getConfiguration()->getValue('WIKI_CREATE_PERMISSION');
return $permission === 'all'
|| $GLOBALS['perm']->have_studip_perm($permission, $this->range_id, $user_id);
}
// Otherwise check write permissions
if ($this->write_permission === 'all') {
return true;
}
if (in_array($this->write_permission, ['tutor', 'dozent'])) {
return $GLOBALS['perm']->have_studip_perm(
$this->write_permission,
$this->range_id,
$user_id
);
} else {
return StatusgruppeUser::exists([$this->write_permission, $user_id]);
}
}
public function isDeletable(?string $user_id = null): bool
{
if ($user_id === null && User::findCurrent()) {
$user_id = User::findCurrent()->id;
}
if (!$user_id) {
return false;
}
if ($this->id == RangeConfig::get($this->range_id)->WIKI_STARTPAGE_ID) {
if (self::countBySQL('range_id = ?', [$this->range_id]) > 1) {
return false;
}
}
$permission = $this->write_permission;
if ($permission === 'all') {
$permission = 'tutor';
}
if (
in_array($permission, ['tutor', 'dozent'])
&& $GLOBALS['perm']->have_studip_perm(
$this->write_permission,
$this->range_id,
$user_id
)
) {
return true;
}
return $this->user_id === $user_id
&& $this->versions->every(function (WikiVersion $version) use ($user_id): bool {
return $version->user_id === $user_id;
});
}
/**
* Export available data of a given user into a storage object
* (an instance of the StoredUserData class) for that user.
*
* @param StoredUserData $storage object to store data into
*/
public static function exportUserData(StoredUserData $storage)
{
$sorm = self::findBySQL("user_id = ?", [$storage->user_id]);
if ($sorm) {
$field_data = [];
foreach ($sorm as $row) {
$field_data[] = $row->toRawArray();
}
if ($field_data) {
$storage->addTabularData(_('Wiki Einträge'), 'wiki', $field_data);
}
}
}
/**
* Tests if a given Wikipage name (keyword) is a valid ancestor for this page.
*
* @param string $ancestor Wikipage name to be tested to be an ancestor
* @return boolean true if ok, false if not
*
*/
public function isValidAncestor($ancestor): bool
{
if ($this->name === 'WikiWikiWeb' || $this->name === $ancestor) {
return false;
}
$keywords = array_map(
function ($descendant) {
return $descendant->name;
},
$this->getDescendants()
);
return !in_array($ancestor, $keywords);
}
/**
* Retrieve an array of all descending WikiPages (recursive).
*
* @return WikiPage[] Array of all descendant WikiPages
*
*/
public function getDescendants(): array
{
$descendants = [];
foreach ($this->children as $child) {
array_push($descendants, $child, ...$child->getDescendants());
}
return $descendants;
}
/**
* @return array
*/
public function getOnlineUsers(): array
{
WikiOnlineEditingUser::purge($this);
$this->resetRelation('onlineeditingusers');
return $this->onlineeditingusers->map(function (WikiOnlineEditingUser $editing_user) {
return [
'user_id' => $editing_user->user_id,
'username' => $editing_user->user->username,
'fullname' => $editing_user->user->getFullName(),
'avatar' => Avatar::getAvatar($editing_user->user_id)->getURL(Avatar::SMALL),
'editing' => (bool) $editing_user->editing,
'editing_request' => (bool) $editing_user->editing_request,
];
});
}
}
|