public function execute() { global $CFG, $DB; require_once $CFG->libdir . '/adminlib.php'; // various admin-only functions require_once $CFG->libdir . '/classes/plugin_manager.php'; $filtername = $this->arguments[0]; $newstate = $this->arguments[1]; if ($newstate != 1 && $newstate != -1 && $newstate != -9999) { cli_error("Invalid filter value, use: 1 for on, -1 per course, -9999 for off"); } // Clean up bogus filter states first. $plugininfos = core_plugin_manager::instance()->get_plugins_of_type('filter'); $filters = array(); $states = filter_get_global_states(); foreach ($states as $state) { if (!isset($plugininfos[$state->filter]) and !get_config('filter_' . $state->filter, 'version')) { // Purge messy leftovers after incorrectly uninstalled plugins and unfinished installs. $DB->delete_records('filter_active', array('filter' => $state->filter)); $DB->delete_records('filter_config', array('filter' => $state->filter)); error_log('Deleted bogus "filter_' . $state->filter . '" states and config data.'); } else { $filters[$state->filter] = $state; } } if (!isset($filters[$filtername])) { cli_error("Invalid filter name: '{$filtername}''. Possible values: " . implode(",", array_keys($filters)) . '.'); } filter_set_global_state($filtername, $newstate); if ($newstate == TEXTFILTER_DISABLED) { filter_set_applies_to_strings($filtername, false); } reset_text_filters_cache(); core_plugin_manager::reset_caches(); echo "Updated {$filtername} to state = {$newstate}\n"; }
/** * Return a list of all the filters that may be in use somewhere. * * @staticvar array $enabledfilters * @return array where the keys and values are both the filter name, like '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; }
/** * Provides access to the results of {@link filter_get_global_states()} * but indexed by the normalized filter name * * The legacy filter name is available as ->legacyname property. * * @param bool $disablecache do not attempt to obtain data from the cache * @return array */ protected static function get_global_states($disablecache = false) { global $DB; $cache = cache::make('core', 'plugininfo_filter'); $globalstates = $cache->get('globalstates'); if ($globalstates === false or $disablecache) { if (!$DB->get_manager()->table_exists('filter_active')) { // Not installed yet. $cache->set('globalstates', array()); return array(); } $globalstates = array(); foreach (filter_get_global_states() as $name => $info) { if (strpos($name, '/') !== false) { // Skip existing before upgrade to new names. continue; } $filterinfo = new stdClass(); $filterinfo->active = $info->active; $filterinfo->sortorder = $info->sortorder; $globalstates[$name] = $filterinfo; } $cache->set('globalstates', $globalstates); } return $globalstates; }
/** * Provides access to the results of {@link filter_get_global_states()} * but indexed by the normalized filter name * * The legacy filter name is available as ->legacyname property. * * @param bool $disablecache * @return array */ protected static function get_global_states($disablecache=false) { global $DB; static $globalstatescache = null; if ($disablecache or is_null($globalstatescache)) { if (!$DB->get_manager()->table_exists('filter_active')) { // we're upgrading from 1.9 and the table used by {@link filter_get_global_states()} // does not exist yet $globalstatescache = array(); } else { foreach (filter_get_global_states() as $legacyname => $info) { $name = self::normalize_legacy_name($legacyname); $filterinfo = new stdClass(); $filterinfo->legacyname = $legacyname; $filterinfo->active = $info->active; $filterinfo->sortorder = $info->sortorder; $globalstatescache[$name] = $filterinfo; } } } return $globalstatescache; }
// Add any missing filters to the DB table. foreach ($newfilters as $filter => $notused) { filter_set_global_state($filter, TEXTFILTER_DISABLED); } // Reset caches and return if ($action) { reset_text_filters_cache(); redirect($returnurl); } /// End of process actions ===================================================== /// Print the page heading. admin_externalpage_print_header(); echo $OUTPUT->heading(get_string('filtersettings', 'admin')); $activechoices = array(TEXTFILTER_DISABLED => get_string('disabled', 'filters'), TEXTFILTER_OFF => get_string('offbutavailable', 'filters'), TEXTFILTER_ON => get_string('on', 'filters')); $applytochoices = array(0 => get_string('content', 'filters'), 1 => get_string('contentandheadings', 'filters')); $filters = filter_get_global_states(); // In case any new filters have been installed, but not put in the table yet. $filternames = filter_get_all_installed(); $newfilters = $filternames; foreach ($filters as $filter => $notused) { unset($newfilters[$filter]); } $stringfilters = filter_get_string_filters(); $table = new html_table(); $table->head = array(get_string('filter'), get_string('isactive', 'filters'), get_string('order'), get_string('applyto', 'filters'), get_string('settings'), get_string('delete')); $table->align = array('left', 'left', 'center', 'left', 'left'); $table->width = '100%'; $table->data = array(); $lastactive = null; foreach ($filters as $filter => $filterinfo) { if ($filterinfo->active != TEXTFILTER_DISABLED) {
public function test_filter_get_global_states() { // Setup fixture. filter_set_global_state('filter/1', TEXTFILTER_ON); filter_set_global_state('filter/2', TEXTFILTER_OFF); filter_set_global_state('filter/3', TEXTFILTER_DISABLED); // Exercise SUT. $filters = filter_get_global_states(); // Validate. $this->assertEqual(array('filter/1' => (object) array('filter' => 'filter/1', 'active' => TEXTFILTER_ON, 'sortorder' => 1), 'filter/2' => (object) array('filter' => 'filter/2', 'active' => TEXTFILTER_OFF, 'sortorder' => 2), 'filter/3' => (object) array('filter' => 'filter/3', 'active' => TEXTFILTER_DISABLED, 'sortorder' => 3)), $filters); }
/** * Provides access to the results of {@link filter_get_global_states()} * but indexed by the normalized filter name * * The legacy filter name is available as ->legacyname property. * * @param bool $disablecache * @return array */ protected static function get_global_states($disablecache = false) { global $DB; static $globalstatescache = null; if ($disablecache or is_null($globalstatescache)) { $globalstatescache = array(); if (!$DB->get_manager()->table_exists('filter_active')) { // Not installed yet. return $globalstatescache; } foreach (filter_get_global_states() as $name => $info) { if (strpos($name, '/') !== false) { // Skip existing before upgrade to new names. continue; } $filterinfo = new stdClass(); $filterinfo->active = $info->active; $filterinfo->sortorder = $info->sortorder; $globalstatescache[$name] = $filterinfo; } } return $globalstatescache; }