MDL-53709 wiki: Return content size if includecontent=0
authorDani Palou <dani@moodle.com>
Tue, 5 Apr 2016 09:50:04 +0000 (11:50 +0200)
committerDani Palou <dani@moodle.com>
Thu, 7 Apr 2016 06:43:31 +0000 (08:43 +0200)
mod/wiki/classes/external.php
mod/wiki/tests/externallib_test.php

index 767a662..a142b85 100644 (file)
@@ -537,16 +537,26 @@ class mod_wiki_external extends external_api {
                         'firstpage' => $page->id == $firstpage->id
                     );
 
-                if ($options['includecontent']) {
-                    // Refresh page cached content if needed.
-                    if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
-                        if ($content = wiki_refresh_cachedcontent($page)) {
-                            $page = $content['page'];
-                        }
+                // Refresh page cached content if needed.
+                if ($page->timerendered + WIKI_REFRESH_CACHE_TIME < time()) {
+                    if ($content = wiki_refresh_cachedcontent($page)) {
+                        $page = $content['page'];
                     }
+                }
+                list($cachedcontent, $contentformat) = external_format_text(
+                            $page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id);
 
-                    list($retpage['cachedcontent'], $retpage['contentformat']) = external_format_text(
-                                $page->cachedcontent, FORMAT_HTML, $context->id, 'mod_wiki', 'attachments', $subwiki->id);
+                if ($options['includecontent']) {
+                    // Return the page content.
+                    $retpage['cachedcontent'] = $cachedcontent;
+                    $retpage['contentformat'] = $contentformat;
+                } else {
+                    // Return the size of the content.
+                    if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
+                        $retpage['contentsize'] = mb_strlen($cachedcontent, '8bit');
+                    } else {
+                        $retpage['contentsize'] = strlen($cachedcontent);
+                    }
                 }
 
                 $returnedpages[] = $retpage;
@@ -586,6 +596,8 @@ class mod_wiki_external extends external_api {
                             'firstpage' => new external_value(PARAM_BOOL, 'True if it\'s the first page.'),
                             'cachedcontent' => new external_value(PARAM_RAW, 'Page contents.', VALUE_OPTIONAL),
                             'contentformat' => new external_format_value('cachedcontent', VALUE_OPTIONAL),
+                            'contentsize' => new external_value(PARAM_INT, 'Size of page contents in bytes (doesn\'t include'.
+                                                                            ' size of attached files).', VALUE_OPTIONAL),
                         ), 'Pages'
                     )
                 ),
index 31e24be..3098ff0 100644 (file)
@@ -629,11 +629,16 @@ class mod_wiki_external_testcase extends externallib_advanced_testcase {
         $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
         $this->assertEquals($expectedpages, $result['pages']);
 
-        // Check that WS doesn't return page content if includecontent is false.
-        unset($expectedpages[0]['cachedcontent']);
-        unset($expectedpages[0]['contentformat']);
-        unset($expectedpages[1]['cachedcontent']);
-        unset($expectedpages[1]['contentformat']);
+        // Check that WS doesn't return page content if includecontent is false, it returns the size instead.
+        foreach ($expectedpages as $i => $expectedpage) {
+            if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
+                $expectedpages[$i]['contentsize'] = mb_strlen($expectedpages[$i]['cachedcontent'], '8bit');
+            } else {
+                $expectedpages[$i]['contentsize'] = strlen($expectedpages[$i]['cachedcontent']);
+            }
+            unset($expectedpages[$i]['cachedcontent']);
+            unset($expectedpages[$i]['contentformat']);
+        }
         $result = mod_wiki_external::get_subwiki_pages($this->wiki->id, 0, 0, array('sortby' => 'id', 'includecontent' => 0));
         $result = external_api::clean_returnvalue(mod_wiki_external::get_subwiki_pages_returns(), $result);
         $this->assertEquals($expectedpages, $result['pages']);