MDL-53709 wiki: Return content size if includecontent=0
[moodle.git] / mod / wiki / classes / external.php
CommitLineData
23cbaefb
DP
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Wiki module external API.
19 *
20 * @package mod_wiki
21 * @category external
22 * @copyright 2015 Dani Palou <dani@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 3.1
25 */
26
27defined('MOODLE_INTERNAL') || die;
28
29require_once($CFG->libdir . '/externallib.php');
30require_once($CFG->dirroot . '/mod/wiki/lib.php');
31require_once($CFG->dirroot . '/mod/wiki/locallib.php');
32
33/**
34 * Wiki module external functions.
35 *
36 * @package mod_wiki
37 * @category external
38 * @copyright 2015 Dani Palou <dani@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 * @since Moodle 3.1
41 */
42class mod_wiki_external extends external_api {
43
44 /**
45 * Describes the parameters for get_wikis_by_courses.
46 *
47 * @return external_function_parameters
48 * @since Moodle 3.1
49 */
50 public static function get_wikis_by_courses_parameters() {
51 return new external_function_parameters (
52 array(
53 'courseids' => new external_multiple_structure(
54 new external_value(PARAM_INT, 'Course ID'), 'Array of course ids.', VALUE_DEFAULT, array()
55 ),
56 )
57 );
58 }
59
60 /**
61 * Returns a list of wikis in a provided list of courses,
62 * if no list is provided all wikis that the user can view will be returned.
63 *
64 * @param array $courseids The courses IDs.
65 * @return array Containing a list of warnings and a list of wikis.
66 * @since Moodle 3.1
67 */
68 public static function get_wikis_by_courses($courseids = array()) {
69
70 $returnedwikis = array();
71 $warnings = array();
72
73 $params = self::validate_parameters(self::get_wikis_by_courses_parameters(), array('courseids' => $courseids));
74
75 $mycourses = array();
76 if (empty($params['courseids'])) {
77 $mycourses = enrol_get_my_courses();
78 $params['courseids'] = array_keys($mycourses);
79 }
80
81 // Ensure there are courseids to loop through.
82 if (!empty($params['courseids'])) {
83
84 list($courses, $warnings) = external_util::validate_courses($params['courseids'], $mycourses);
85
86 // Get the wikis in this course, this function checks users visibility permissions.
87 // We can avoid then additional validate_context calls.
88 $wikis = get_all_instances_in_courses('wiki', $courses);
89
90 foreach ($wikis as $wiki) {
91
92 $context = context_module::instance($wiki->coursemodule);
93
94 // Entry to return.
95 $module = array();
96
97 // First, we return information that any user can see in (or can deduce from) the web interface.
98 $module['id'] = $wiki->id;
99 $module['coursemodule'] = $wiki->coursemodule;
100 $module['course'] = $wiki->course;
101 $module['name'] = external_format_string($wiki->name, $context->id);
102
103 $viewablefields = [];
104 if (has_capability('mod/wiki:viewpage', $context)) {
105 list($module['intro'], $module['introformat']) =
106 external_format_text($wiki->intro, $wiki->introformat, $context->id, 'mod_wiki', 'intro', $wiki->id);
107
108 $viewablefields = array('firstpagetitle', 'wikimode', 'defaultformat', 'forceformat', 'editbegin', 'editend',
109 'section', 'visible', 'groupmode', 'groupingid');
110 }
111
112 // Check additional permissions for returning optional private settings.
113 if (has_capability('moodle/course:manageactivities', $context)) {
114 $additionalfields = array('timecreated', 'timemodified');
115 $viewablefields = array_merge($viewablefields, $additionalfields);
116 }
117
118 foreach ($viewablefields as $field) {
119 $module[$field] = $wiki->{$field};
120 }
121
122 // Check if user can add new pages.
123 $module['cancreatepages'] = wiki_can_create_pages($context);
124
125 $returnedwikis[] = $module;
126 }
127 }
128
129 $result = array();
130 $result['wikis'] = $returnedwikis;
131 $result['warnings'] = $warnings;
132 return $result;
133 }
134
135 /**
136 * Describes the get_wikis_by_courses return value.
137 *
138 * @return external_single_structure
139 * @since Moodle 3.1
140 */
141 public static function get_wikis_by_courses_returns() {
142
143 return new external_single_structure(
144 array(
145 'wikis' => new external_multiple_structure(
146 new external_single_structure(
147 array(
148 'id' => new external_value(PARAM_INT, 'Wiki ID.'),
149 'coursemodule' => new external_value(PARAM_INT, 'Course module ID.'),
150 'course' => new external_value(PARAM_INT, 'Course ID.'),
151 'name' => new external_value(PARAM_RAW, 'Wiki name.'),
152 'intro' => new external_value(PARAM_RAW, 'Wiki intro.', VALUE_OPTIONAL),
153 'introformat' => new external_format_value('Wiki intro format.', VALUE_OPTIONAL),
154 'timecreated' => new external_value(PARAM_INT, 'Time of creation.', VALUE_OPTIONAL),
155 'timemodified' => new external_value(PARAM_INT, 'Time of last modification.', VALUE_OPTIONAL),
156 'firstpagetitle' => new external_value(PARAM_RAW, 'First page title.', VALUE_OPTIONAL),
157 'wikimode' => new external_value(PARAM_TEXT, 'Wiki mode (individual, collaborative).', VALUE_OPTIONAL),
158 'defaultformat' => new external_value(PARAM_TEXT, 'Wiki\'s default format (html, creole, nwiki).',
159 VALUE_OPTIONAL),
160 'forceformat' => new external_value(PARAM_INT, '1 if format is forced, 0 otherwise.',
161 VALUE_OPTIONAL),
162 'editbegin' => new external_value(PARAM_INT, 'Edit begin.', VALUE_OPTIONAL),
163 'editend' => new external_value(PARAM_INT, 'Edit end.', VALUE_OPTIONAL),
164 'section' => new external_value(PARAM_INT, 'Course section ID.', VALUE_OPTIONAL),
165 'visible' => new external_value(PARAM_INT, '1 if visible, 0 otherwise.', VALUE_OPTIONAL),
166 'groupmode' => new external_value(PARAM_INT, 'Group mode.', VALUE_OPTIONAL),
167 'groupingid' => new external_value(PARAM_INT, 'Group ID.', VALUE_OPTIONAL),
168 'cancreatepages' => new external_value(PARAM_BOOL, 'True if user can create pages.'),
169 ), 'Wikis'
170 )
171 ),
172 'warnings' => new external_warnings(),
173 )
174 );
175 }
176
557b58b0
DP
177 /**
178 * Describes the parameters for view_wiki.
179 *
180 * @return external_function_parameters
181 * @since Moodle 3.1
182 */
183 public static function view_wiki_parameters() {
184 return new external_function_parameters (
185 array(
186 'wikiid' => new external_value(PARAM_INT, 'Wiki instance ID.')
187 )
188 );
189 }
190
191 /**
192 * Trigger the course module viewed event and update the module completion status.
193 *
194 * @param int $wikiid The wiki instance ID.
195 * @return array of warnings and status result.
196 * @since Moodle 3.1
197 */
198 public static function view_wiki($wikiid) {
199
200 $params = self::validate_parameters(self::view_wiki_parameters(),
201 array(
202 'wikiid' => $wikiid
203 ));
204 $warnings = array();
205
206 // Get wiki instance.
207 if (!$wiki = wiki_get_wiki($params['wikiid'])) {
208 throw new moodle_exception('incorrectwikiid', 'wiki');
209 }
210
211 // Permission validation.
212 list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki');
213 $context = context_module::instance($cm->id);
214 self::validate_context($context);
215
216 // Check if user can view this wiki.
217 // We don't use wiki_user_can_view because it requires to have a valid subwiki for the user.
218 if (!has_capability('mod/wiki:viewpage', $context)) {
219 throw new moodle_exception('cannotviewpage', 'wiki');
220 }
221
222 // Trigger course_module_viewed event and completion.
223 wiki_view($wiki, $course, $cm, $context);
224
225 $result = array();
226 $result['status'] = true;
227 $result['warnings'] = $warnings;
228 return $result;
229 }
230
231 /**
232 * Describes the view_wiki return value.
233 *
234 * @return external_single_structure
235 * @since Moodle 3.1
236 */
237 public static function view_wiki_returns() {
238 return new external_single_structure(
239 array(
240 'status' => new external_value(PARAM_BOOL, 'Status: true if success.'),
241 'warnings' => new external_warnings()
242 )
243 );
244 }
245
246 /**
247 * Describes the parameters for view_page.
248 *
249 * @return external_function_parameters
250 * @since Moodle 3.1
251 */
252 public static function view_page_parameters() {
253 return new external_function_parameters (
254 array(
255 'pageid' => new external_value(PARAM_INT, 'Wiki page ID.'),
256 )
257 );
258 }
259
260 /**
261 * Trigger the page viewed event and update the module completion status.
262 *
263 * @param int $pageid The page ID.
264 * @return array of warnings and status result.
265 * @since Moodle 3.1
266 * @throws moodle_exception if page is not valid.
267 */
268 public static function view_page($pageid) {
269
270 $params = self::validate_parameters(self::view_page_parameters(),
271 array(
272 'pageid' => $pageid
273 ));
274 $warnings = array();
275
276 // Get wiki page.
277 if (!$page = wiki_get_page($params['pageid'])) {
278 throw new moodle_exception('incorrectpageid', 'wiki');
279 }
280
281 // Get wiki instance.
282 if (!$wiki = wiki_get_wiki_from_pageid($params['pageid'])) {
283 throw new moodle_exception('incorrectwikiid', 'wiki');
284 }
285
286 // Permission validation.
287 list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki');
288 $context = context_module::instance($cm->id);
289 self::validate_context($context);
290
291 // Check if user can view this wiki.
292 if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
293 throw new moodle_exception('incorrectsubwikiid', 'wiki');
294 }
295 if (!wiki_user_can_view($subwiki, $wiki)) {
296 throw new moodle_exception('cannotviewpage', 'wiki');
297 }
298
299 // Trigger page_viewed event and completion.
300 wiki_page_view($wiki, $page, $course, $cm, $context);
301
302 $result = array();
303 $result['status'] = true;
304 $result['warnings'] = $warnings;
305 return $result;
306 }
307
308 /**
309 * Describes the view_page return value.
310 *
311 * @return external_single_structure
312 * @since Moodle 3.1
313 */
314 public static function view_page_returns() {
315 return new external_single_structure(
316 array(
317 'status' => new external_value(PARAM_BOOL, 'Status: true if success.'),
318 'warnings' => new external_warnings()
319 )
320 );
321 }
322
44f1b701
DP
323 /**
324 * Describes the parameters for get_subwikis.
325 *
326 * @return external_function_parameters
327 * @since Moodle 3.1
328 */
329 public static function get_subwikis_parameters() {
330 return new external_function_parameters (
331 array(
332 'wikiid' => new external_value(PARAM_INT, 'Wiki instance ID.')
333 )
334 );
335 }
336
337 /**
338 * Returns the list of subwikis the user can see in a specific wiki.
339 *
340 * @param int $wikiid The wiki instance ID.
341 * @return array Containing a list of warnings and a list of subwikis.
342 * @since Moodle 3.1
343 */
344 public static function get_subwikis($wikiid) {
345 global $USER;
346
347 $warnings = array();
348
349 $params = self::validate_parameters(self::get_subwikis_parameters(), array('wikiid' => $wikiid));
350
351 // Get wiki instance.
352 if (!$wiki = wiki_get_wiki($params['wikiid'])) {
353 throw new moodle_exception('incorrectwikiid', 'wiki');
354 }
355
356 // Validate context and capabilities.
357 list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki');
358 $context = context_module::instance($cm->id);
359 self::validate_context($context);
360 require_capability('mod/wiki:viewpage', $context);
361
362 $returnedsubwikis = wiki_get_visible_subwikis($wiki, $cm, $context);
363 foreach ($returnedsubwikis as $subwiki) {
364 $subwiki->canedit = wiki_user_can_edit($subwiki);
365 }
366
367 $result = array();
368 $result['subwikis'] = $returnedsubwikis;
369 $result['warnings'] = $warnings;
370 return $result;
371 }
372
373 /**
374 * Describes the get_subwikis return value.
375 *
376 * @return external_single_structure
377 * @since Moodle 3.1
378 */
379 public static function get_subwikis_returns() {
380 return new external_single_structure(
381 array(
382 'subwikis' => new external_multiple_structure(
383 new external_single_structure(
384 array(
385 'id' => new external_value(PARAM_INT, 'Subwiki ID.'),
386 'wikiid' => new external_value(PARAM_INT, 'Wiki ID.'),
387 'groupid' => new external_value(PARAM_RAW, 'Group ID.'),
388 'userid' => new external_value(PARAM_INT, 'User ID.'),
389 'canedit' => new external_value(PARAM_BOOL, 'True if user can edit the subwiki.'),
390 ), 'Subwikis'
391 )
392 ),
393 'warnings' => new external_warnings(),
394 )
395 );
396 }
397
96abf9af
DP
398 /**
399 * Describes the parameters for get_subwiki_pages.
400 *
401 * @return external_function_parameters
402 * @since Moodle 3.1
403 */
404 public static function get_subwiki_pages_parameters() {
405 return new external_function_parameters (
406 array(
407 'wikiid' => new external_value(PARAM_INT, 'Wiki instance ID.'),
408 'groupid' => new external_value(PARAM_INT, 'Subwiki\'s group ID, -1 means current group. It will be ignored'
409 . ' if the wiki doesn\'t use groups.', VALUE_DEFAULT, -1),
410 'userid' => new external_value(PARAM_INT, 'Subwiki\'s user ID, 0 means current user. It will be ignored'
411 .' in collaborative wikis.', VALUE_DEFAULT, 0),
412 'options' => new external_single_structure(
413 array(
414 'sortby' => new external_value(PARAM_ALPHA,
415 'Field to sort by (id, title, ...).', VALUE_DEFAULT, 'title'),
416 'sortdirection' => new external_value(PARAM_ALPHA,
417 'Sort direction: ASC or DESC.', VALUE_DEFAULT, 'ASC'),
418 'includecontent' => new external_value(PARAM_INT,
419 'Include each page contents or not.', VALUE_DEFAULT, 1),
420 ), 'Options', VALUE_DEFAULT, array()),
421 )
422 );
423 }
424
425 /**
426 * Returns the list of pages from a specific subwiki.
427 *
428 * @param int $wikiid The wiki instance ID.
429 * @param int $groupid The group ID. If not defined, use current group.
430 * @param int $userid The user ID. If not defined, use current user.
431 * @param array $options Several options like sort by, sort direction, ...
432 * @return array Containing a list of warnings and a list of pages.
433 * @since Moodle 3.1
434 */
435 public static function get_subwiki_pages($wikiid, $groupid = -1, $userid = 0, $options = array()) {
436 global $USER, $DB;
437
438 $returnedpages = array();
439 $warnings = array();
440
441 $params = self::validate_parameters(self::get_subwiki_pages_parameters(),
442 array(
443 'wikiid' => $wikiid,
444 'groupid' => $groupid,
445 'userid' => $userid,
446 'options' => $options
447 )
448 );
449
450 // Get wiki instance.
451 if (!$wiki = wiki_get_wiki($params['wikiid'])) {
452 throw new moodle_exception('incorrectwikiid', 'wiki');
453 }
454 list($course, $cm) = get_course_and_cm_from_instance($wiki, 'wiki');
455 $context = context_module::instance($cm->id);
456 self::validate_context($context);
457
458 // Determine group.
459 $groupmode = groups_get_activity_groupmode($cm);
460 if ($groupmode == NOGROUPS) {
461 $groupid = 0;
462 } else if ($params['groupid'] == -1) {
463 // Use current group.
464 $groupid = groups_get_activity_group($cm);
465 $groupid = !empty($groupid) ? $groupid : 0;
466 } else {
467 $groupid = $params['groupid'];
468 }
469
470 // Determine user.
471 if ($wiki->wikimode == 'collaborative') {
472 // Collaborative wikis don't use userid in subwikis.
473 $userid = 0;
474 } else if (empty($params['userid'])) {
475 // Use current user.
476 $userid = $USER->id;
477 } else {
478 $userid = $params['userid'];
479 }
480
481 // Get subwiki based on group and user.
482 if (!$subwiki = wiki_get_subwiki_by_group($cm->instance, $groupid, $userid)) {
483 // The subwiki doesn't exist.
484 // Validate if user is valid.
485 if ($userid != 0 && $userid != $USER->id && !$user = $DB->get_record('user', array('id' => $userid))) {
486 throw new moodle_exception('invaliduserid', 'error');
487 }
488
489 // Validate that groupid is valid.
490 if ($groupid != 0 && !groups_group_exists($groupid)) {
491 throw new moodle_exception('cannotfindgroup', 'error');
492 }
493
494 // Valid data but subwiki not found. We'll simulate a subwiki object to check if the user would be able to see it
495 // if it existed. If he's able to see it then we'll return an empty array because the subwiki has no pages.
496 $subwiki = new stdClass();
497 $subwiki->wikiid = $wiki->id;
498 $subwiki->userid = $userid;
499 $subwiki->groupid = $groupid;
500
501 // Check that the user can view the subwiki. This function checks capabilities.
502 if (!wiki_user_can_view($subwiki, $wiki)) {
503 throw new moodle_exception('cannotviewpage', 'wiki');
504 }
505 } else {
506 // Check that the user can view the subwiki. This function checks capabilities.
507 if (!wiki_user_can_view($subwiki, $wiki)) {
508 throw new moodle_exception('cannotviewpage', 'wiki');
509 }
510
511 // Set sort param.
512 $options = $params['options'];
513 if (!empty($options['sortby'])) {
514 if ($options['sortdirection'] != 'ASC' && $options['sortdirection'] != 'DESC') {
515 // Invalid sort direction. Use default.
516 $options['sortdirection'] = 'ASC';
517 }
518 $sort = $options['sortby'] . ' ' . $options['sortdirection'];
519 }
520
521 $pages = wiki_get_page_list($subwiki->id, $sort);
522 $caneditpages = wiki_user_can_edit($subwiki);
523 $firstpage = wiki_get_first_page($subwiki->id);
524
525 foreach ($pages as $page) {
526 $retpage = array(
527 'id' => $page->id,
528 'subwikiid' => $page->subwikiid,
529 'title' => external_format_string($page->title, $context->id),
530 'timecreated' => $page->timecreated,
531 'timemodified' => $page->timemodified,
532 'timerendered' => $page->timerendered,
533 'userid' => $page->userid,
534 'pageviews' => $page->pageviews,
535 'readonly' => $page->readonly,
536 'caneditpage' => $caneditpages,
537 'firstpage' => $page->id == $firstpage->id
538 );
539
b1be9b20
DP
540 // Refresh page cached content if needed.
541 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
542 if ($content = wiki_refresh_cachedcontent($page)) {
543 $page = $content['page'];
96abf9af 544 }
b1be9b20
DP
545 }
546 list($cachedcontent, $contentformat) = external_format_text(
547 $page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id);
96abf9af 548
b1be9b20
DP
549 if ($options['includecontent']) {
550 // Return the page content.
551 $retpage['cachedcontent'] = $cachedcontent;
552 $retpage['contentformat'] = $contentformat;
553 } else {
554 // Return the size of the content.
555 if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
556 $retpage['contentsize'] = mb_strlen($cachedcontent, '8bit');
557 } else {
558 $retpage['contentsize'] = strlen($cachedcontent);
559 }
96abf9af
DP
560 }
561
562 $returnedpages[] = $retpage;
563 }
564
565 }
566
567 $result = array();
568 $result['pages'] = $returnedpages;
569 $result['warnings'] = $warnings;
570 return $result;
571 }
572
573 /**
574 * Describes the get_subwiki_pages return value.
575 *
576 * @return external_single_structure
577 * @since Moodle 3.1
578 */
579 public static function get_subwiki_pages_returns() {
580
581 return new external_single_structure(
582 array(
583 'pages' => new external_multiple_structure(
584 new external_single_structure(
585 array(
586 'id' => new external_value(PARAM_INT, 'Page ID.'),
587 'subwikiid' => new external_value(PARAM_INT, 'Page\'s subwiki ID.'),
588 'title' => new external_value(PARAM_RAW, 'Page title.'),
589 'timecreated' => new external_value(PARAM_INT, 'Time of creation.'),
590 'timemodified' => new external_value(PARAM_INT, 'Time of last modification.'),
591 'timerendered' => new external_value(PARAM_INT, 'Time of last renderization.'),
592 'userid' => new external_value(PARAM_INT, 'ID of the user that last modified the page.'),
593 'pageviews' => new external_value(PARAM_INT, 'Number of times the page has been viewed.'),
594 'readonly' => new external_value(PARAM_INT, '1 if readonly, 0 otherwise.'),
595 'caneditpage' => new external_value(PARAM_BOOL, 'True if user can edit the page.'),
596 'firstpage' => new external_value(PARAM_BOOL, 'True if it\'s the first page.'),
597 'cachedcontent' => new external_value(PARAM_RAW, 'Page contents.', VALUE_OPTIONAL),
598 'contentformat' => new external_format_value('cachedcontent', VALUE_OPTIONAL),
b1be9b20
DP
599 'contentsize' => new external_value(PARAM_INT, 'Size of page contents in bytes (doesn\'t include'.
600 ' size of attached files).', VALUE_OPTIONAL),
96abf9af
DP
601 ), 'Pages'
602 )
603 ),
604 'warnings' => new external_warnings(),
605 )
606 );
607 }
608
5d94e343
DP
609 /**
610 * Describes the parameters for get_page_contents.
611 *
612 * @return external_function_parameters
613 * @since Moodle 3.1
614 */
615 public static function get_page_contents_parameters() {
616 return new external_function_parameters (
617 array(
618 'pageid' => new external_value(PARAM_INT, 'Page ID.')
619 )
620 );
621 }
622
623 /**
624 * Get a page contents.
625 *
626 * @param int $pageid The page ID.
627 * @return array of warnings and page data.
628 * @since Moodle 3.1
629 */
630 public static function get_page_contents($pageid) {
631
632 $params = self::validate_parameters(self::get_page_contents_parameters(),
633 array(
634 'pageid' => $pageid
635 )
636 );
637 $warnings = array();
638
639 // Get wiki page.
640 if (!$page = wiki_get_page($params['pageid'])) {
641 throw new moodle_exception('incorrectpageid', 'wiki');
642 }
643
644 // Get wiki instance.
645 if (!$wiki = wiki_get_wiki_from_pageid($params['pageid'])) {
646 throw new moodle_exception('incorrectwikiid', 'wiki');
647 }
648
649 // Permission validation.
650 $cm = get_coursemodule_from_instance('wiki', $wiki->id, $wiki->course);
651 $context = context_module::instance($cm->id);
652 self::validate_context($context);
653
654 // Check if user can view this wiki.
655 if (!$subwiki = wiki_get_subwiki($page->subwikiid)) {
656 throw new moodle_exception('incorrectsubwikiid', 'wiki');
657 }
658 if (!wiki_user_can_view($subwiki, $wiki)) {
659 throw new moodle_exception('cannotviewpage', 'wiki');
660 }
661
662 $returnedpage = array();
663 $returnedpage['id'] = $page->id;
664 $returnedpage['wikiid'] = $wiki->id;
665 $returnedpage['subwikiid'] = $page->subwikiid;
666 $returnedpage['groupid'] = $subwiki->groupid;
667 $returnedpage['userid'] = $subwiki->userid;
668 $returnedpage['title'] = $page->title;
669
670 // Refresh page cached content if needed.
671 if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
672 if ($content = wiki_refresh_cachedcontent($page)) {
673 $page = $content['page'];
674 }
675 }
676
677 list($returnedpage['cachedcontent'], $returnedpage['contentformat']) = external_format_text(
678 $page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id);
679 $returnedpage['caneditpage'] = wiki_user_can_edit($subwiki);
680
681 $result = array();
682 $result['page'] = $returnedpage;
683 $result['warnings'] = $warnings;
684 return $result;
685 }
686
687 /**
688 * Describes the get_page_contents return value.
689 *
690 * @return external_single_structure
691 * @since Moodle 3.1
692 */
693 public static function get_page_contents_returns() {
694 return new external_single_structure(
695 array(
696 'page' => new external_single_structure(
697 array(
698 'id' => new external_value(PARAM_INT, 'Page ID.'),
699 'wikiid' => new external_value(PARAM_INT, 'Page\'s wiki ID.'),
700 'subwikiid' => new external_value(PARAM_INT, 'Page\'s subwiki ID.'),
701 'groupid' => new external_value(PARAM_INT, 'Page\'s group ID.'),
702 'userid' => new external_value(PARAM_INT, 'Page\'s user ID.'),
703 'title' => new external_value(PARAM_RAW, 'Page title.'),
704 'cachedcontent' => new external_value(PARAM_RAW, 'Page contents.'),
705 'contentformat' => new external_format_value('cachedcontent', VALUE_OPTIONAL),
706 'caneditpage' => new external_value(PARAM_BOOL, 'True if user can edit the page.')
707 ), 'Page'
708 ),
709 'warnings' => new external_warnings()
710 )
711 );
712 }
713
23cbaefb 714}