MDL-51213 externallib: external_format_text/external_format_string

API functions do not know if they are being called from a web page, or a
web-service. They need a single function to call to correctly apply
filters, strip tags etc that knows the correct way to do this based on
the way it was called (e.g. webservices allow parameters for skipping
filters etc).

So here we are fixing the default for the filter argument used in
external_format_text and adding a matching external_format_string.
This commit is contained in:
Damyon Wiese 2015-08-27 14:21:56 +08:00
parent f5d23fc162
commit 9764aab9da
2 changed files with 88 additions and 1 deletions

View File

@ -718,6 +718,37 @@ function external_validate_format($format) {
return $format;
}
/**
* Format the string to be returned properly as requested by the either the web service server,
* either by an internally call.
* The caller can change the format (raw) with the external_settings singleton
* All web service servers must set this singleton when parsing the $_GET and $_POST.
*
* @param string $str The string to be filtered. Should be plain text, expect
* possibly for multilang tags.
* @param boolean $striplinks To strip any link in the result text. Moodle 1.8 default changed from false to true! MDL-8713
* @param int $contextid The id of the context for the string (affects filters).
* @param array $options options array/object or courseid
* @return string text
* @since Moodle 3.0
*/
function external_format_string($str, $contextid, $striplinks = true, $options = array()) {
// Get settings (singleton).
$settings = external_settings::get_instance();
if (empty($contextid)) {
throw new coding_exception('contextid is required');
}
if (!$settings->get_raw()) {
$context = context::instance_by_id($contextid);
$options['context'] = $context;
$str = format_string($str, $striplinks, $options);
}
return $str;
}
/**
* Format the text to be returned properly as requested by the either the web service server,
* either by an internally call.
@ -745,7 +776,8 @@ function external_format_text($text, $textformat, $contextid, $component, $filea
}
if (!$settings->get_raw()) {
$text = format_text($text, $textformat, array('para' => false, 'filter' => $settings->get_filter()));
$context = context::instance_by_id($contextid);
$text = format_text($text, $textformat, array('para' => false, 'filter' => $settings->get_filter(), 'context' => $context));
$textformat = FORMAT_HTML; // Once converted to html (from markdown, plain... lets inform consumer this is already HTML).
}
@ -783,6 +815,10 @@ class external_settings {
* Constructor - protected - can not be instanciated
*/
protected function __construct() {
if (!defined('AJAX_SCRIPT') && !defined('CLI_SCRIPT') && !defined('WS_SERVER')) {
// For normal pages, the default should match the default for format_text.
$this->filter = true;
}
}
/**

View File

@ -72,6 +72,57 @@ class core_externallib_testcase extends advanced_testcase {
$this->assertSame('aaa', $result['text']);
}
public function test_external_format_text() {
$settings = external_settings::get_instance();
$currentraw = $settings->get_raw();
$currentfilter = $settings->get_filter();
$settings->set_raw(true);
$settings->set_filter(false);
$context = context_system::instance();
$test = '$$ \pi $$';
$testformat = FORMAT_MARKDOWN;
$correct = array($test, $testformat);
$this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
$settings->set_raw(false);
$settings->set_filter(true);
$test = '$$ \pi $$';
$testformat = FORMAT_MARKDOWN;
$correct = array('<span class="nolink"><span class="filter_mathjaxloader_equation"><p>$$ \pi $$</p>
</span></span>', FORMAT_HTML);
$this->assertSame(external_format_text($test, $testformat, $context->id, 'core', '', 0), $correct);
$settings->set_raw($currentraw);
$settings->set_filter($currentfilter);
}
public function test_external_format_string() {
$settings = external_settings::get_instance();
$currentraw = $settings->get_raw();
$currentfilter = $settings->get_filter();
$settings->set_raw(true);
$context = context_system::instance();
$test = '$$ \pi $$ <script>hi</script> <h3>there</h3>';
$correct = $test;
$this->assertSame(external_format_string($test, $context->id), $correct);
$settings->set_raw(false);
$test = '$$ \pi $$<script>hi</script> <h3>there</h3>';
$correct = '$$ \pi $$hi there';
$this->assertSame(external_format_string($test, $context->id), $correct);
$settings->set_raw($currentraw);
$settings->set_filter($currentfilter);
}
/**
* Test for clean_returnvalue().
*/