From 5b8fa09b00bb01898ed2dc4cdbc1c1c80a7b03b1 Mon Sep 17 00:00:00 2001
From: tjhunt ';
}
- protected function get_table_row($filterinfo, $isfirstrow, $islastactive) {
+ protected function get_table_row($filterinfo, $isfirstrow, $islastactive, $applytostrings) {
global $CFG;
$row = array();
$filter = $filterinfo->filter;
+ // Filter name
$row[] = $this->filternames[$filter];
+ // Disable/off/on
$row[] = popup_form($this->action_url($filter, 'setstate') . '&newstate=', $this->activechoices,
'active' . basename($filter), $filterinfo->active, '', '', '', true, 'self', '', NULL, get_string('save'));
+ // Re-order
$updown = '';
$spacer = '
';
if ($filterinfo->active != TEXTFILTER_DISABLED) {
@@ -3773,9 +3776,12 @@ class admin_setting_managefilters extends admin_setting {
}
$row[] = $updown;
- $row[] = 'TODO Apply to col';
+ // Apply to strings.
+ $row[] = popup_form($this->action_url($filter, 'setapplyto') . '&stringstoo=', $this->applytochoices,
+ 'applyto' . basename($filter), $applytostrings, '', '', '', true, 'self', '', NULL, get_string('save'),
+ $filterinfo->active == TEXTFILTER_DISABLED);
- // settings link (if defined)
+ // Settings link, if required
$settings = '';
if (filter_has_global_settings($filter)) {
$settings = '' . get_string('tablenosave', 'filters') . '
' . get_string('filterallwarning', 'filters') . '
'; $return .= print_box_end(true); return highlight($query, $return); } diff --git a/lib/db/install.php b/lib/db/install.php index d703cadafa..691bf0f9a7 100644 --- a/lib/db/install.php +++ b/lib/db/install.php @@ -41,24 +41,25 @@ function xmldb_main_install() { /// create default course category $cat = get_course_category(); - - $defaults = array('rolesactive' => '0', // marks fully set up system - 'auth' => 'email', - 'auth_pop3mailbox' => 'INBOX', - 'enrol' => 'manual', - 'enrol_plugins_enabled' => 'manual', - 'style' => 'default', - 'template' => 'default', - 'theme' => 'standardwhite', - 'filter_multilang_converted' => 1, - 'siteidentifier' => random_string(32).$_SERVER['HTTP_HOST'], - 'backup_version' => 2008111700, - 'backup_release' => '2.0 dev', - 'blocks_version' => 2007081300, // might be removed soon - 'mnet_dispatcher_mode' => 'off', - 'sessiontimeout' => 7200, // must be present during roles installation - - ); + $defaults = array( + 'rolesactive' => '0', // marks fully set up system + 'auth' => 'email', + 'auth_pop3mailbox' => 'INBOX', + 'enrol' => 'manual', + 'enrol_plugins_enabled' => 'manual', + 'style' => 'default', + 'template' => 'default', + 'theme' => 'standardwhite', + 'filter_multilang_converted' => 1, + 'siteidentifier' => random_string(32).$_SERVER['HTTP_HOST'], + 'backup_version' => 2008111700, + 'backup_release' => '2.0 dev', + 'blocks_version' => 2007081300, // might be removed soon + 'mnet_dispatcher_mode' => 'off', + 'sessiontimeout' => 7200, // must be present during roles installation + 'stringfilters' => '', // These two are managed in a strange way by the filters + 'filterall' => 0, // setting page, so have to be initialised here. + ); foreach($defaults as $key => $value) { set_config($key, $value); } diff --git a/lib/filelib.php b/lib/filelib.php index 4050407a3f..0d83c78645 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -1529,12 +1529,10 @@ function file_modify_html_header($text) { $stylesheetshtml .= ''."\n"; } - $filters = explode(",", $CFG->textfilters); - if (in_array('filter/mediaplugin', $filters)) { + $ufo = ''; + if (filter_is_enabled('filter/mediaplugin')) { // this script is needed by most media filter plugins. $ufo = get_require_js_code(array($CFG->wwwroot . '/lib/ufo.js')); - } else { - $ufo = ''; } preg_match('/\|\/', $text, $matches); diff --git a/lib/filterlib.php b/lib/filterlib.php index 129bfec20b..45aad2fd5e 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -32,10 +32,7 @@ class filter_manager { protected static $singletoninstance; protected function __construct() { - global $CFG; - if (!empty($CFG->filterall) && !empty($CFG->stringfilters)) { - $stringfilternames = explode(',', $CFG->stringfilters); - } + $stringfilternames = filter_get_string_filters(); } /** @@ -441,6 +438,69 @@ function filter_set_global_state($filter, $state, $sortorder = false) { } } +/** + * @param string $filter The filter name, for example 'filter/tex' or 'mod/glossary'. + * @return boolean is this filter allowed to be used on this site. That is, the + * admin has set the global 'active' setting to On, or Off, but available. + */ +function filter_is_enabled($filter) { + return array_key_exists($filter, filter_get_globally_enabled()); +} + +/** + * Return a list of all the filters that may be in use somewhere. + * @return array where the keys and values are both the filter name, like 'filter/tex'. + */ +function filter_get_globally_enabled() { + static $enabledfilters = null; + if (is_null($enabledfilters)) { + $filters = filter_get_global_states(); + $enabledfilters = array(); + foreach ($filters as $filter => $filerinfo) { + if ($filerinfo->active != TEXTFILTER_DISABLED) { + $enabledfilters[$filter] = $filter; + } + } + } + return $enabledfilters; +} + +/** + * Return the names of the filters that should also be applied to strings + * (when they are enabled). + * @return array where the keys and values are both the filter name, like 'filter/tex'. + */ +function filter_get_string_filters() { + global $CFG; + $stringfilters = array(); + if (!empty($CFG->filterall) && !empty($CFG->stringfilters)) { + $stringfilters = explode(',', $CFG->stringfilters); + $stringfilters = array_combine($stringfilters, $stringfilters); + } + return $stringfilters; +} + +/** + * Sets whether a particular active filter should be applied to all strings by + * format_string, or just used by format_text. + * @param string $filter The filter name, for example 'filter/tex' or 'mod/glossary'. + * @param boolean $applytostrings if true, this filter will apply to format_string + * and format_text, when it is enabled. + */ +function filter_set_applies_to_strings($filter, $applytostrings) { + $stringfilters = filter_get_string_filters(); + $numstringfilters = count($stringfilters); + if ($applytostrings) { + $stringfilters[$filter] = $filter; + } else { + unset($stringfilters[$filter]); + } + if (count($stringfilters) != $numstringfilters) { + set_config('stringfilters', implode(',', $stringfilters)); + set_config('filterall', !empty($stringfilters)); + } +} + /** * Set the local activated state for a text filter. * @param string $filter The filter name, for example 'filter/tex' or 'mod/glossary'. diff --git a/lib/simpletest/testfilterconfig.php b/lib/simpletest/testfilterconfig.php index e1ee4a9bc4..e895a45c15 100644 --- a/lib/simpletest/testfilterconfig.php +++ b/lib/simpletest/testfilterconfig.php @@ -44,6 +44,8 @@ class filter_active_global_test extends UnitTestCaseUsingDatabase { private $syscontextid; public function setUp() { + parent::setUp(); + // Make sure accesslib has cached a sensible system context object // before we switch to the test DB. $this->syscontextid = get_context_instance(CONTEXT_SYSTEM)->id; @@ -239,6 +241,8 @@ class filter_active_global_test extends UnitTestCaseUsingDatabase { */ class filter_active_local_test extends UnitTestCaseUsingDatabase { public function setUp() { + parent::setUp(); + // Create the table we need and switch to test DB. $this->create_test_table('filter_active', 'lib'); $this->switch_to_test_db(); @@ -311,6 +315,8 @@ class filter_active_local_test extends UnitTestCaseUsingDatabase { */ class filter_config_test extends UnitTestCaseUsingDatabase { public function setUp() { + parent::setUp(); + // Create the table we need and switch to test DB. $this->create_test_table('filter_config', 'lib'); $this->switch_to_test_db(); @@ -363,6 +369,8 @@ class filter_get_active_in_context_test extends UnitTestCaseUsingDatabase { private $childcontext2; public function setUp() { + parent::setUp(); + // Make sure accesslib has cached a sensible system context object // before we switch to the test DB. $this->syscontext = get_context_instance(CONTEXT_SYSTEM); @@ -493,17 +501,16 @@ class filter_get_active_in_context_test extends UnitTestCaseUsingDatabase { } class filter_delete_all_data_test extends UnitTestCaseUsingDatabase { - private $syscontext; - private $childcontext; - private $childcontext2; - public function setUp() { + parent::setUp(); + // Create the table we need and switch to test DB. $this->create_test_tables(array('filter_active', 'filter_config', 'config', 'config_plugins'), 'lib'); $this->switch_to_test_db(); } public function test_filter_delete_all_data_filter() { + // Setup fixture. $syscontext = get_context_instance(CONTEXT_SYSTEM); filter_set_global_state('filter/name', TEXTFILTER_ON); filter_set_global_state('filter/other', TEXTFILTER_ON); @@ -511,7 +518,9 @@ class filter_delete_all_data_test extends UnitTestCaseUsingDatabase { filter_set_local_config('filter/other', $syscontext->id, 'settingname', 'Other value'); set_config('configname', 'A config value', 'filter_name'); set_config('configname', 'Other config value', 'filter_other'); + // Exercise SUT. filter_delete_all_data('filter/name'); + // Validate. $this->assertEqual(1, $this->testdb->count_records('filter_active')); $this->assertTrue($this->testdb->record_exists('filter_active', array('filter' => 'filter/other'))); $this->assertEqual(1, $this->testdb->count_records('filter_config')); @@ -522,4 +531,65 @@ class filter_delete_all_data_test extends UnitTestCaseUsingDatabase { $this->assertFalse(get_config('filter_name')); } } + +class filter_filter_set_applies_to_strings extends UnitTestCaseUsingDatabase { + protected $origcfgstringfilters; + protected $origcfgfilterall; + + public function setUp() { + global $CFG; + parent::setUp(); + + // Create the table we need and switch to test DB. + $this->create_test_table('config', 'lib'); + $this->switch_to_test_db(); + + // Store original $CFG; + $this->origcfgstringfilters = $CFG->stringfilters; + $this->origcfgfilterall = $CFG->filterall; + } + + public function tearDown() { + $CFG->stringfilters = $this->origcfgstringfilters; + $CFG->filterall = $this->origcfgfilterall; + + parent::tearDown(); + } + + public function test_set() { + global $CFG; + // Setup fixture. + $CFG->filterall = 0; + $CFG->stringfilters = ''; + // Exercise SUT. + filter_set_applies_to_strings('filter/name', true); + // Validate. + $this->assertEqual('filter/name', $CFG->stringfilters); + $this->assertTrue($CFG->filterall); + } + + public function test_unset_to_empty() { + global $CFG; + // Setup fixture. + $CFG->filterall = 1; + $CFG->stringfilters = 'filter/name'; + // Exercise SUT. + filter_set_applies_to_strings('filter/name', false); + // Validate. + $this->assertEqual('', $CFG->stringfilters); + $this->assertFalse($CFG->filterall); + } + + public function test_unset_multi() { + global $CFG; + // Setup fixture. + $CFG->filterall = 1; + $CFG->stringfilters = 'filter/name,filter/other'; + // Exercise SUT. + filter_set_applies_to_strings('filter/name', false); + // Validate. + $this->assertEqual('filter/other', $CFG->stringfilters); + $this->assertTrue($CFG->filterall); + } +} ?> -- 2.39.5