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
|
<?php
/**
* WikiPage.class.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 array $id alias for pk
* @property string $range_id database column
* @property string|null $user_id database column
* @property string $keyword database column
* @property string $body database column
* @property string|null $ancestor database column
* @property int|null $chdate database column
* @property int $version database column
* @property int|null $mkdate database column
* @property User|null $author belongs_to User
* @property Course $course belongs_to Course
* @property-read mixed $parent additional field
* @property-read mixed $children additional field
* @property-read mixed $config additional field
*/
class WikiPage extends SimpleORMap implements PrivacyObject
{
/**
* Configures the model
* @param array $config Configuration
*/
protected static function configure($config = [])
{
$config['db_table'] = 'wiki';
$config['belongs_to']['author'] = [
'class_name' => User::class,
'foreign_key' => 'user_id'
];
$config['belongs_to']['course'] = [
'class_name' => Course::class,
'foreign_key' => 'range_id',
];
$config['additional_fields']['parent'] = [
'get' => function ($page) {
return \WikiPage::findLatestPage($page->range_id, $page->ancestor);
}
];
$config['additional_fields']['children'] = [
'get' => function ($page) {
$query = "SELECT range_id, keyword, MAX(version) as version FROM wiki
WHERE range_id = ? AND ancestor = ? GROUP BY keyword ORDER BY keyword ASC";
$stmt = \DBManager::get()->prepare($query);
$stmt->execute([$page->range_id, $page->keyword]);
$pageIds = $stmt->fetchAll(\PDO::FETCH_NUM);
return array_map(
function ($pageId) {
return self::find($pageId);
},
$pageIds
);
}
];
$config['additional_fields']['config']['get'] = function ($page) {
return new WikiPageConfig([$page->range_id, $page->keyword]);
};
$config['registered_callbacks']['before_delete'][] = function ($page) {
if ($page->version == 1 && $page->config) {
$page->config->delete();
}
};
$config['default_values']['user_id'] = 'nobody';
parent::configure($config);
}
/**
* Finds all latest versions of all pages for the given course.
* @param string $course_id Course id
* @return SimpleCollection of all pages
*/
public static function findLatestPages($course_id)
{
$query = "SELECT
range_id,
keyword,
MAX(version) as version
FROM wiki
WHERE range_id = ?
GROUP BY keyword
ORDER BY keyword ASC";
$st = DBManager::get()->prepare($query);
$st->execute([$course_id]);
$ids = $st->fetchAll(PDO::FETCH_NUM);
$pages = new SimpleORMapCollection();
$pages->setClassName(__CLASS__);
foreach ($ids as $id) {
$pages[] = self::find($id);
}
return $pages;
}
/**
* Finds the latest version for the given course and keyword
* @param string $course_id Course id
* @param string $keyword Keyword
* @return WikiPage or null
*/
public static function findLatestPage($course_id, $keyword)
{
$results = self::findBySQL(
"range_id = ? AND keyword = ? ORDER BY version DESC LIMIT 1",
[$course_id, $keyword]
);
if (count($results) === 0) {
return null;
}
return $results[0];
}
/**
* Returns whether this page is visible to the given user.
* @param mixed $user User object or id
* @return boolean indicating whether the page is visible
*/
public function isVisibleTo($user)
{
// anyone can see this page if it belongs to a free course
if (!$this->config->read_restricted
&& Config::get()->ENABLE_FREE_ACCESS
&& $this->course && $this->course->lesezugriff == 0)
{
return true;
}
return $GLOBALS['perm']->have_studip_perm(
$this->config->read_restricted ? 'tutor' : 'user',
$this->range_id,
is_object($user) ? $user->id : $user
);
}
/**
* Returns whether this page is editable to the given user.
* @param mixed $user User object or id
* @return boolean indicating whether the page is editable
*/
public function isEditableBy($user)
{
return $GLOBALS['perm']->have_studip_perm(
$this->config->edit_restricted ? 'tutor' : 'autor',
$this->range_id,
is_object($user) ? $user->id : $user
);
}
/**
* Returns whether this page is creatable to the given user.
* @param mixed $user User object or id
* @return boolean indicating whether the page is creatable
* @todo this method is kinda bogus as an instance method
*/
public function isCreatableBy($user)
{
return $this->isEditableBy($user);
}
/**
* Returns whether this version of this page is the latest version availabe.
* @return boolean
*/
public function isLatestVersion()
{
return self::countBySQL(
'range_id = ? AND keyword = ? AND version > ?',
[$this->range_id, $this->keyword, $this->version]
) === 0;
}
/**
* Returns the start page of a wiki for a given course. The start page has
* the keyword 'WikiWikiWeb'.
*
* @param string $course_id Course id
* @return WikiPage
*/
public static function getStartPage($course_id)
{
$start = self::findLatestPage($course_id, '');
if (!$start) {
$start = new self([$course_id, 'WikiWikiWeb', 0]);
$start->body = _('Dieses Wiki ist noch leer.');
if ($start->isEditableBy($GLOBALS['user'])) {
$start->body .= ' ' . _("Bearbeiten Sie es!\nNeue Seiten oder Links werden einfach durch Eingeben von [nop][[Wikinamen]][/nop] in doppelten eckigen Klammern angelegt.");
}
}
return $start;
}
/**
* 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);
}
}
}
/**
* Sets the parent page for all versions of a Wikipage.
*
* @param string ancestor Wikipage name to be set as the parent
*/
public function setAncestorForAllVersions($ancestor) {
$query = "UPDATE
wiki
SET
ancestor = ?
WHERE
range_id = ? AND
keyword = ?";
$st = DBManager::get()->prepare($query);
$st->execute([$ancestor, $this->range_id, $this->keyword]);
}
/**
* 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)
{
if ($this->keyword === 'WikiWikiWeb' || $this->keyword === $ancestor) {
return false;
}
$keywords = array_map(
function ($descendant) {
return $descendant['keyword'];
},
$this->getDescendants()
);
return !in_array($ancestor, $keywords);
}
/**
* Retrieve an array of all descending WikiPages (recursive).
*
* @return array Array of all descendant WikiPages
*
*/
public function getDescendants()
{
$descendants = [];
foreach ($this->children as $child) {
array_push($descendants, $child, ...$child->getDescendants());
}
return $descendants;
}
/**
* @param string $other_keyword
* @return bool
*/
public function isDescendantOf(string $other_keyword): bool
{
if ($other_keyword === $this->keyword) {
return false;
}
$other_page = WikiPage::findLatestPage($this->range_id, $other_keyword);
if ($other_page) {
foreach ($other_page->getDescendants() as $descendant) {
if ($descendant->keyword === $this->keyword) {
return true;
}
}
}
return false;
}
}
|