From c13c21b9b5280ff568c604df49756b5cfdb736df Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Wed, 6 Jul 2016 10:09:48 +0200 Subject: [PATCH] MDL-55130 wiki: Reduce data usage when only renew lock is needed. --- mod/wiki/classes/external.php | 35 ++++++++++------- mod/wiki/locallib.php | 3 +- mod/wiki/tests/externallib_test.php | 58 +++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 14 deletions(-) diff --git a/mod/wiki/classes/external.php b/mod/wiki/classes/external.php index ebd08363d04..20f617c92a4 100644 --- a/mod/wiki/classes/external.php +++ b/mod/wiki/classes/external.php @@ -797,7 +797,8 @@ class mod_wiki_external extends external_api { return new external_function_parameters ( array( 'pageid' => new external_value(PARAM_INT, 'Page ID to edit.'), - 'section' => new external_value(PARAM_RAW, 'Section page title.', VALUE_DEFAULT, null) + 'section' => new external_value(PARAM_RAW, 'Section page title.', VALUE_DEFAULT, null), + 'lockonly' => new external_value(PARAM_BOOL, 'Just renew lock and not return content.', VALUE_DEFAULT, false) ) ); } @@ -807,16 +808,18 @@ class mod_wiki_external extends external_api { * * @param int $pageid The page ID. * @param string $section Section page title. + * @param boolean $lockonly If true: Just renew lock and not return content. * @return array of warnings and page data. * @since Moodle 3.1 */ - public static function get_page_for_editing($pageid, $section = null) { + public static function get_page_for_editing($pageid, $section = null, $lockonly = false) { global $USER; $params = self::validate_parameters(self::get_page_for_editing_parameters(), array( 'pageid' => $pageid, - 'section' => $section + 'section' => $section, + 'lockonly' => $lockonly ) ); @@ -855,17 +858,21 @@ class mod_wiki_external extends external_api { throw new moodle_exception('versionerror', 'wiki'); } - if (!is_null($params['section'])) { - $content = wiki_parser_proxy::get_section($version->content, $version->contentformat, $params['section']); - } else { - $content = $version->content; - } - $pagesection = array(); - $pagesection['content'] = $content; - $pagesection['contentformat'] = $version->contentformat; $pagesection['version'] = $version->version; + // Content requested to be returned. + if (!$lockonly) { + if (!is_null($params['section'])) { + $content = wiki_parser_proxy::get_section($version->content, $version->contentformat, $params['section']); + } else { + $content = $version->content; + } + + $pagesection['content'] = $content; + $pagesection['contentformat'] = $version->contentformat; + } + $result = array(); $result['pagesection'] = $pagesection; $result['warnings'] = $warnings; @@ -884,8 +891,10 @@ class mod_wiki_external extends external_api { array( 'pagesection' => new external_single_structure( array( - 'content' => new external_value(PARAM_RAW, 'The contents of the page-section to be edited.'), - 'contentformat' => new external_value(PARAM_TEXT, 'Format of the original content of the page.'), + 'content' => new external_value(PARAM_RAW, 'The contents of the page-section to be edited.', + VALUE_OPTIONAL), + 'contentformat' => new external_value(PARAM_TEXT, 'Format of the original content of the page.', + VALUE_OPTIONAL), 'version' => new external_value(PARAM_INT, 'Latest version of the page.'), 'warnings' => new external_warnings() ) diff --git a/mod/wiki/locallib.php b/mod/wiki/locallib.php index 51a7593500d..b8f0eabc5c7 100644 --- a/mod/wiki/locallib.php +++ b/mod/wiki/locallib.php @@ -1030,7 +1030,8 @@ function wiki_set_lock($pageid, $userid, $section = null, $insert = false) { if (!empty($lock)) { $DB->update_record('wiki_locks', array('id' => $lock->id, 'lockedat' => time() + LOCK_TIMEOUT)); } else if ($insert) { - $DB->insert_record('wiki_locks', array('pageid' => $pageid, 'sectionname' => $section, 'userid' => $userid, 'lockedat' => time() + 30)); + $DB->insert_record('wiki_locks', + array('pageid' => $pageid, 'sectionname' => $section, 'userid' => $userid, 'lockedat' => time() + LOCK_TIMEOUT)); } return true; diff --git a/mod/wiki/tests/externallib_test.php b/mod/wiki/tests/externallib_test.php index 37b64353e2c..f4cdd413f90 100644 --- a/mod/wiki/tests/externallib_test.php +++ b/mod/wiki/tests/externallib_test.php @@ -1187,6 +1187,64 @@ class mod_wiki_external_testcase extends externallib_advanced_testcase { $this->assertEquals($expected, $result['pagesection']); } + /** + * Test test_get_page_locking. + */ + public function test_get_page_locking() { + + $this->create_individual_wikis_with_groups(); + + $pagecontent = '

Title1

Text inside section

Title2

Text inside section'; + $newpage = $this->getDataGenerator()->get_plugin_generator('mod_wiki')->create_page( + $this->wiki, array('content' => $pagecontent)); + + // Test user with full capabilities. + $this->setUser($this->student); + + // Test Section locking. + $expected = array( + 'version' => '1' + ); + + $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title1', true); + $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); + $this->assertEquals($expected, $result['pagesection']); + + // Test the section is locked. + $this->setUser($this->student2); + try { + mod_wiki_external::get_page_for_editing($newpage->id, 'Title1', true); + $this->fail('Exception expected due to not page locking.'); + } catch (moodle_exception $e) { + $this->assertEquals('pageislocked', $e->errorcode); + } + + // Test the page is locked. + try { + mod_wiki_external::get_page_for_editing($newpage->id, null, true); + $this->fail('Exception expected due to not page locking.'); + } catch (moodle_exception $e) { + $this->assertEquals('pageislocked', $e->errorcode); + } + + // Test the other section is not locked. + $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title2', true); + $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); + $this->assertEquals($expected, $result['pagesection']); + + // Back to the original user to test version change when editing. + $this->setUser($this->student); + $newsectioncontent = '

Title2

New test2'; + $result = mod_wiki_external::edit_page($newpage->id, $newsectioncontent, 'Title1'); + + $expected = array( + 'version' => '2' + ); + $result = mod_wiki_external::get_page_for_editing($newpage->id, 'Title1', true); + $result = external_api::clean_returnvalue(mod_wiki_external::get_page_for_editing_returns(), $result); + $this->assertEquals($expected, $result['pagesection']); + } + /** * Test new_page. We won't test all the possible cases because that's already * done in the tests for wiki_create_page. -- 2.43.0