MDL-55130 wiki: Reduce data usage when only renew lock is needed.
authorPau Ferrer Ocaña <crazyserver@gmail.com>
Wed, 6 Jul 2016 08:09:48 +0000 (10:09 +0200)
committerPau Ferrer Ocaña <crazyserver@gmail.com>
Mon, 15 Aug 2016 12:40:38 +0000 (14:40 +0200)
mod/wiki/classes/external.php
mod/wiki/locallib.php
mod/wiki/tests/externallib_test.php

index ebd0836..20f617c 100644 (file)
@@ -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()
                     )
index 51a7593..b8f0eab 100644 (file)
@@ -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;
index 37b6435..f4cdd41 100644 (file)
@@ -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 = '<h1>Title1</h1>Text inside section<h1>Title2</h1>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 = '<h1>Title2</h1>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.