-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCourseEditorOperations.php
430 lines (412 loc) · 20.3 KB
/
CourseEditorOperations.php
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<?php
if ( !defined( 'MEDIAWIKI' ) ){
die( );
}
class CourseEditorOperations {
/**
* Add a category to the course root page to be checked by a bot and a template
* that display a 'Ready to be published' message.
* IDEA: should be implemented an Echo notification
* @param string $operationRequested JSON object with operation type and all
* params used to public the course like the title
* @return string $operation JSON object with all the sended params plus
* a success field
*/
public static function publishCourseOp($operationRequested){
global $wgCourseEditorTemplates, $wgCourseEditorCategories, $wgContLang;
$operation = json_decode($operationRequested);
$title = Title::newFromText($operation->courseName);
$template = "{{". $wgCourseEditorTemplates['ReadyToBePublished'] ."}}";
$category = "<noinclude>[[" . $wgContLang->getNsText( NS_CATEGORY ) . ":". $wgCourseEditorCategories['ReadyToBePublished'] ."]]</noinclude>";
$result = CourseEditorUtils::editWrapper($title, null, $template, $category);
CourseEditorUtils::setSingleOperationSuccess($operation, $result);
return json_encode($operation);
}
/**
* Remove the publish category and templte from the course root page
* @param string $operationRequested JSON object with operation type and all
* params used to public the course like the title
* @return string $operation JSON object with all the sended params plus
* a success field
*/
public static function undoPublishCourseOp($operationRequested){
global $wgCourseEditorTemplates, $wgCourseEditorCategories, $wgContLang;
$operation = json_decode($operationRequested);
$title = Title::newFromText($operation->courseName);
$page = WikiPage::factory($title);
$pageText = $page->getText();
$category = "<noinclude>[[" . $wgContLang->getNsText( NS_CATEGORY ) . ":". $wgCourseEditorCategories['ReadyToBePublished'] ."]]</noinclude>";
$template = "{{". $wgCourseEditorTemplates['ReadyToBePublished'] ."}}";
$replacedText = str_replace($category, "", $pageText);
$newPageText = str_replace($template, "", $replacedText);
$result = CourseEditorUtils::editWrapper($title, $newPageText, null, null);
CourseEditorUtils::setSingleOperationSuccess($operation, $result);
$operation->newPageText = $newPageText;
return json_encode($operation);
}
/**
* Like a Façade. It's an entrypoint for course create process
* independently if the course is private/public etc.
* @param string $operationRequested JSON object with operation type and all
* params used to create the course (name, topic, ...)
* @return string $operation JSON object with all the sended params plus
* a success field and the course complete title(with namespace)
*/
public static function createCourseOp($operationRequested){
$operation = json_decode($operationRequested);
switch ($operation->type) {
case 'fromTopic':
$result = self::createNewCourseFromTopic($operation);
CourseEditorUtils::setComposedOperationSuccess($operation, $result);
break;
case 'fromDepartment':
$result = self::createNewCourseFromDepartment($operation);
CourseEditorUtils::setComposedOperationSuccess($operation, $result);
break;
}
return json_encode($operation);
}
/**
* Update the metadata page with new data submitted by the user.
* Moreover the course root page cache is purged.
* @param string $operationRequested JSON object with new new data
* @return string $operation JSON object with the operation requested plus
* a success field
*/
public static function manageCourseMetadataOp($operationRequested){
$operation = json_decode($operationRequested);
// Get all the params
$params = $operation->params;;
$title = $params[0];
$topic = $params[1];
$description = $params[2];
$bibliography = $params[3];
$exercises = $params[4];
$books = $params[5];
$externalReferences = $params[6];
$isImported = $params[7];
$originalAuthors = $params[8];
$isReviewed = $params[9];
$reviewedOn = $params[10];
// Create the page title prepending the namespace
$pageTitle = MWNamespace::getCanonicalName(NS_COURSEMETADATA) . ':' . $title;
// Add the new metadata submitted
$metadata = "<section begin=topic />" . $topic . "<section end=topic />\r\n";
if($description !== '' && $description !== null){
$metadata .= "<section begin=description />" . $description . "<section end=description />\r\n";
}
if($bibliography !== '' && $bibliography !== null){
$metadata .= "<section begin=bibliography />" . $bibliography . "<section end=bibliography />\r\n";
}
if($exercises !== '' && $exercises !== null){
$metadata .= "<section begin=exercises />" . $exercises . "<section end=exercises />\r\n";
}
if($books !== '' && $books !== null){
$metadata .= "<section begin=books />" . $books . "<section end=books />\r\n";
}
if($externalReferences !== '' && $externalReferences !== null){
$metadata .= "<section begin=externalreferences />" . $externalReferences . "<section end=externalreferences />\r\n";
}
if($isImported !== false || $isReviewed !== false){
$metadata .= "<section begin=hasbadge />" . true . "<section end=hasbadge />\r\n";
if($isImported !== false){
$metadata .= "<section begin=isimported />" . $isImported . "<section end=isimported />\r\n";
$metadata .= "<section begin=originalauthors />" . $originalAuthors . "<section end=originalauthors />\r\n";
}
if($isReviewed !== false){
$metadata .= "<section begin=isreviewed />" . $isReviewed . "<section end=isreviewed />\r\n";
$metadata .= "<section begin=reviewedon />" . $reviewedOn . "<section end=reviewedon />\r\n";
}
}
$resultCreateMetadataPage = CourseEditorUtils::editWrapper($pageTitle, $metadata , null, null);
$resultPurgeCourse = CourseEditorUtils::purgeWrapper($pageTitle);
CourseEditorUtils::setComposedOperationSuccess($operation, [$resultCreateMetadataPage, $resultPurgeCourse]);
return json_encode($operation);
}
/**
* Create the basic metadata page when a new course is created.
* @param string $title the title of the course
* @param string $topic the topic of the course
* @param string $description the description of the course
* @return $apiResult the result of the edit
*/
private function createBasicCourseMetadata($topic, $title, $description){
//Remove username from title (if present) to be used as topic if $topic is null
$explodedString = explode('/', $title, 2);
$titleNoUser = (sizeof($explodedString) === 1) ? $explodedString[0] : $explodedString[1] ;
$topic = ($topic === null ? $titleNoUser : $topic);
$pageTitle = MWNamespace::getCanonicalName(NS_COURSEMETADATA) . ':' . $title;
$metadata = "<section begin=topic />" . $topic . "<section end=topic />\r\n";
if($description !== '' && $description !== null){
$metadata .= "<section begin=description />" . $description . "<section end=description />\r\n";
}
$apiResult = CourseEditorUtils::editWrapper($pageTitle, $metadata , null, null);
return $apiResult;
}
private function createNewCourseFromDepartment(&$operation){
$params = $operation->params;
$department = $params[0];
$title = $params[1];
$description = $params[2];
$namespace = $params[3];
if($department != null && $title != null && $namespace != null){
$compareResult = strcmp($namespace, 'NS_COURSE');
$namespaceCostant = ($compareResult == 0 ? NS_COURSE : NS_USER);
$pageTitle = MWNamespace::getCanonicalName($namespaceCostant) . ':';
if($namespaceCostant == NS_USER){
$result = self::createPrivateCourse($pageTitle, $topic, $title, $description);
$user = CourseEditorUtils::getRequestContext()->getUser();
$userPage = $pageTitle . $user->getName();
$operation->courseTitle = $userPage . '/' . $title;
}else{
$result = self::createPublicCourseFromDepartment($pageTitle, $department, $title, $description);
$operation->courseTitle = $pageTitle . $title;
}
}
return $result;
}
private function createNewCourseFromTopic(&$operation){
$params = $operation->params;
$topic = $params[0];
$title = $params[1];
$description = $params[2];
$namespace = $params[3];
if($topic != null && $title != null && $namespace != null){
$compareResult = strcmp($namespace, 'NS_COURSE');
$namespaceCostant = ($compareResult === 0 ? NS_COURSE : NS_USER);
$pageTitle = MWNamespace::getCanonicalName($namespaceCostant) . ':';
if($namespaceCostant == NS_USER){
$result = self::createPrivateCourse($pageTitle, $topic, $title, $description);
$user = CourseEditorUtils::getRequestContext()->getUser();
$userPage = $pageTitle . $user->getName();
$operation->courseTitle = $userPage . '/' . $title;
}else{
$result = self::createPublicCourseFromTopic($pageTitle, $topic, $title, $description);
$operation->courseTitle = $pageTitle . $title;
}
}
return $result;
}
private function createPrivateCourse($pageTitle, $topic, $title, $description){
global $wgCourseEditorTemplates, $wgCourseEditorCategories, $wgContLang;
$context = CourseEditorUtils::getRequestContext();
$user = $context->getUser();
$userPage = $pageTitle . $user->getName();
$titleWithUser = $user->getName() . '/' . $title;
$pageTitle = $userPage . "/" . $title;
$courseText = "{{". $wgCourseEditorTemplates['CourseRoot'] ."|}}\r\n<noinclude>[["
. $wgContLang->getNsText( NS_CATEGORY ) . ":". $wgCourseEditorCategories['CourseRoot'] ."]]</noinclude>";
$resultCreateCourse = CourseEditorUtils::editWrapper($pageTitle, $courseText, null, null);
$resultCreateMetadataPage = self::createBasicCourseMetadata($topic, $titleWithUser, $description);
$textToPrepend = "{{". $wgCourseEditorTemplates['Course'] ."|" . $title . "|" . $user->getName() . "}}";
$resultPrependToUserPage = CourseEditorUtils::editWrapper($userPage, null, $textToPrepend, null);
$resultPurgeCourse = CourseEditorUtils::purgeWrapper($pageTitle);
return array($resultCreateCourse, $resultCreateMetadataPage, $resultPrependToUserPage, $resultPurgeCourse);
}
private function createPublicCourseFromTopic($pageTitle, $topic, $title, $description){
global $wgCourseEditorTemplates, $wgCourseEditorCategories, $wgContLang;
$pageTitle .= $title;
$courseText = "{{". $wgCourseEditorTemplates['CourseRoot'] ."|}}\r\n<noinclude>[["
. $wgContLang->getNsText( NS_CATEGORY ) . ":". $wgCourseEditorCategories['CourseRoot'] ."]]</noinclude>";
$resultCreateCourse = CourseEditorUtils::editWrapper($pageTitle, $courseText, null, null);
$topicCourses = CourseEditorUtils::getTopicCourses($topic);
$text = $topicCourses . "{{". $wgCourseEditorTemplates['Course'] ."|" . $title . "}}}}";
$resultCreateMetadataPage = self::createBasicCourseMetadata($topic, $title, $description);
$resultAppendToTopic = CourseEditorUtils::editWrapper($topic, $text, null, null);
$resultPurgeCourse = CourseEditorUtils::purgeWrapper($pageTitle);
return array($resultCreateCourse, $resultCreateMetadataPage, $resultAppendToTopic, $resultPurgeCourse);
}
private function createPublicCourseFromDepartment($pageTitle, $department, $title, $description){
global $wgCourseEditorTemplates, $wgCourseEditorCategories, $wgContLang;
$pageTitle .= $title;
$courseText = "{{". $wgCourseEditorTemplates['CourseRoot'] ."|}}\r\n<noinclude>[["
. $wgContLang->getNsText( NS_CATEGORY ) . ":". $wgCourseEditorCategories['CourseRoot'] ."]]</noinclude>";
$resultCreateCourse = CourseEditorUtils::editWrapper($pageTitle, $courseText, null, null);
$text = "{{". $wgCourseEditorTemplates['Topic'] ."|" . "{{". $wgCourseEditorTemplates['Course'] ."|" . $title . "}}}}";
$listElementText = "\r\n* [[" . $title . "]]";
$resultCreateMetadataPage = self::createBasicCourseMetadata(null, $title, $description);
$resultAppendToTopic = CourseEditorUtils::editWrapper($title, $text, null, null);
$resultAppendToDepartment = CourseEditorUtils::editSectionWrapper($department, null, null, $listElementText);
$resultPurgeCourse = CourseEditorUtils::purgeWrapper($pageTitle);
return array($resultCreateCourse, $resultCreateMetadataPage, $resultAppendToTopic, $resultAppendToDepartment, $resultPurgeCourse);
}
/**
* All the possible small tasks to publish a course from a userpage.
* This function is used also for course renaming because "techinally" the
* tasks are the same.
* @param string $operation JSON object with the action requested and the
* params needed
* @return string $operationObj JSON object with the request and the a success
* field
*/
public static function applyPublishCourseOp($operation){
global $wgCourseEditorTemplates, $wgCourseEditorCategories, $wgContLang;
$operationObj = json_decode($operation);
switch ($operationObj->action) {
case 'rename-move-task':
CourseEditorUtils::moveElement($operationObj);
break;
case 'rename-update-task':
CourseEditorUtils::updateLevelTwo($operationObj);
break;
case 'move-root':
CourseEditorUtils::moveElement($operationObj);
break;
case 'remove-ready-texts':
$title = Title::newFromText($operationObj->elementName);
$page = WikiPage::factory($title);
$pageText = $page->getText();
$category = "<noinclude>[[" . $wgContLang->getNsText( NS_CATEGORY ) . ":". $wgCourseEditorCategories['ReadyToBePublished'] ."]]</noinclude>";
$template = "{{". $wgCourseEditorTemplates['ReadyToBePublished'] ."}}";
$replacedText = str_replace($category, "", $pageText);
$newPageText = str_replace($template, "", $replacedText);
$apiResult = CourseEditorUtils::editWrapper($title, $newPageText, null, null);
CourseEditorUtils::setSingleOperationSuccess($operationObj, $apiResult);
break;
case 'move-metadata':
CourseEditorUtils::moveElement($operationObj);
case 'purge':
CourseEditorUtils::purgeCache($operationObj);
break;
case 'update-collection':
CourseEditorUtils::updateCollection($operationObj);
break;
case 'remove-from-topic-page':
$title = Title::newFromText($operationObj->elementName);
$page = WikiPage::factory($title);
$pageText = $page->getText();
$replacedText = str_replace("{{". $wgCourseEditorTemplates['Course'] ."|" . $operationObj->courseName . "}}", '', $pageText);
$apiResult = CourseEditorUtils::editWrapper($title, $replacedText, null, null);
CourseEditorUtils::setSingleOperationSuccess($operationObj, $apiResult);
break;
case 'append-to-topic-page':
$title = Title::newFromText($operationObj->newElementName);
$topicCourses = CourseEditorUtils::getTopicCourses($operationObj->newElementName);
$pageText = $topicCourses . "{{". $wgCourseEditorTemplates['Course'] ."|" . $operationObj->courseName . "}}}}";
$apiResult = CourseEditorUtils::editWrapper($title, $pageText, null, null);
CourseEditorUtils::setSingleOperationSuccess($operationObj, $apiResult);
break;
case 'update-topic-page':
$title = Title::newFromText($operationObj->topicName);
$page = WikiPage::factory($title);
$pageText = $page->getText();
$replacedText = str_replace($operationObj->elementName, $operationObj->newElementName, $pageText);
$apiResult = CourseEditorUtils::editWrapper($title, $replacedText, null, null);
CourseEditorUtils::setSingleOperationSuccess($operationObj, $apiResult);
break;
case 'update-user-page':
$title = Title::newFromText($wgContLang->getNsText( NS_USER ) . ":" . $operationObj->username);
$page = WikiPage::factory($title);
$pageText = $page->getText();
$replacedText = str_replace(
"{{". $wgCourseEditorTemplates['Course'] ."|" . $operationObj->elementName . "|" . $operationObj->username . "}}",
"{{". $wgCourseEditorTemplates['Course'] ."|" . $operationObj->newElementName . "|". $operationObj->username . "}}",
$pageText
);
$apiResult = CourseEditorUtils::editWrapper($title, $replacedText, null, null);
CourseEditorUtils::setSingleOperationSuccess($operationObj, $apiResult);
break;
}
return json_encode($operationObj);
}
/**
* All the possible small tasks that can be performed on a levelTwo.
* @param string $operation JSON object with the action requested and the
* params needed
* @return string $operationObj JSON object with the request and the a success
* field
*/
public static function applyCourseOp($operation){
global $wgCourseEditorTemplates, $wgCourseEditorCategories, $wgContLang;
$operationObj = json_decode($operation);
switch ($operationObj->action) {
case 'rename-move-task':
CourseEditorUtils::moveElement($operationObj);
break;
case 'rename-update-task':
CourseEditorUtils::updateLevelTwo($operationObj);
break;
case 'delete-levelsThree-task':
$levelTwoName = $operationObj->elementName;
$levelsThree = CourseEditorUtils::getLevelsThree($levelTwoName);
if(empty($levelsThree)){
$operationObj->success = true;
}else {
foreach ($levelsThree as $levelThree) {
$operationObj->elementName = $levelTwoName . '/' . $levelThree;
CourseEditorUtils::deleteElement($operationObj);
if ($operationObj->success !== true) {
break;
}
}
}
break;
case 'delete-levelTwo-task':
CourseEditorUtils::deleteElement($operationObj);
break;
case 'add':
$text = "\r\n<noinclude>[["
. $wgContLang->getNsText( NS_CATEGORY )
. ":". $wgCourseEditorCategories['CourseLevelTwo']
."]]</noinclude>";
CourseEditorUtils::addElement($operationObj, $text);
break;
case 'update':
CourseEditorUtils::updateRoot($operationObj);
break;
case 'update-collection':
CourseEditorUtils::updateCollection($operationObj);
break;
}
return json_encode($operationObj);
}
/**
* All the possible small tasks that can be performed on a course.
* @param string $operation JSON object with the action requested and the
* params needed
* @return string $operationObj JSON object with the request and the a success
* field
*/
public static function applyLevelTwoOp($operation){
global $wgCourseEditorTemplates, $wgCourseEditorCategories, $wgContLang;
$context = CourseEditorUtils::getRequestContext();
$operationObj = json_decode($operation);
switch ($operationObj->action) {
// Not yet implemented
/*case 'move':
$chapterName = $value->elementName;
$newSectionName = $value->newElementName;
$from = $sectionName . '/' . $chapterName;
$to = $newSectionName . '/' . $chapterName;
$apiResult = CourseEditorUtils::moveWrapper($from, $to);
$explodedString = explode("/", $sectionName);
$courseName = (sizeof($explodedString) > 2 ? $explodedString[0] . "/" . $explodedString[1] : $explodedString[0]);
$textToAppend = "* [[" .$courseName . "/" . $newSectionName. "/" . $chapterName ."|". $chapterName ."]]\r\n";
CourseEditorUtils::editWrapper($courseName . '/' . $newSectionName, null, null, $textToAppend);
CourseEditorUtils::setSingleOperationSuccess($value, $apiResult);*/
case 'rename':
CourseEditorUtils::moveElement($operationObj);
break;
case 'delete':
CourseEditorUtils::deleteElement($operationObj);
break;
case 'add':
CourseEditorUtils::addElement($operationObj);
break;
case 'update':
CourseEditorUtils::updateLevelTwo($operationObj);
break;
case 'purge':
$explodedString = explode("/", $operationObj->elementName);
$operationObj->elementName = (sizeof($explodedString) > 2 ? $explodedString[0] . "/" . $explodedString[1] : $explodedString[0]);
CourseEditorUtils::purgeCache($operationObj);
break;
case 'update-collection':
$explodedString = explode("/", $operationObj->elementName);
$operationObj->elementName = (sizeof($explodedString) > 2 ? $explodedString[0] . "/" . $explodedString[1] : $explodedString[0]);
CourseEditorUtils::updateCollection($operationObj);
break;
}
return json_encode($operationObj);
}
}