From: tjhunt Date: Mon, 30 Mar 2009 08:33:13 +0000 (+0000) Subject: filters: MDL-17684 Look for the filter name in filter_myfilter.php first. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=b810a4d3c9099b4a22eee2fe4872ec78255850f4;p=moodle.git filters: MDL-17684 Look for the filter name in filter_myfilter.php first. This makes filters more plugginable, becuase with this lang file name, get_string will look for the filter name in filter/myfilter/lang/en_utf8/filter_myfilter.php. To do this, there is a new function filter_get_name in filterlib that contains the logic. Also, a new function filter_get_all_installed to replace the logic for getting all filters that was duplicated in three places. filter_get_name no longer does such a nice fall-back if the name is missing, to encourage people to supply the right string. The fallback now looks like '[[filtername]] (filter/tidy)'. --- diff --git a/admin/settings/plugins.php b/admin/settings/plugins.php index 8391a61892..ed32575d14 100644 --- a/admin/settings/plugins.php +++ b/admin/settings/plugins.php @@ -119,35 +119,19 @@ if ($hassiteconfig || has_capability('moodle/question:config', $systemcontext)) } else { $activefilters = explode(',', $CFG->textfilters); } - $filterlocations = array('mod','filter'); - foreach ($filterlocations as $filterlocation) { - $filters = get_list_of_plugins($filterlocation); - - $filterbyname = array(); - - foreach ($filters as $filter) { - $strfiltername = get_string('filtername', $filter); - // Deal with filters which are lacking the language string - if ($strfiltername == '[[filtername]]') { - $textlib = textlib_get_instance(); - $strfiltername = $textlib->strtotitle($filter); - } - $filterbyname[$strfiltername] = "$filterlocation/$filter"; - } - ksort($filterbyname); - - foreach ($filterbyname as $strfiltername=>$filterfull) { - if (file_exists("$CFG->dirroot/$filterfull/filtersettings.php")) { - $settings = new admin_settingpage('filtersetting'.str_replace('/', '', $filterfull), $strfiltername, 'moodle/site:config', !in_array($filterfull, $activefilters)); - if ($ADMIN->fulltree) { - include("$CFG->dirroot/$filterfull/filtersettings.php"); - } - $ADMIN->add('filtersettings', $settings); + $filternames = filter_get_all_installed(); + foreach ($filternames as $filterpath => $strfiltername) { + if (file_exists("$CFG->dirroot/$filterpath/filtersettings.php")) { + $settings = new admin_settingpage('filtersetting'.str_replace('/', '', $filterpath), + $strfiltername, 'moodle/site:config', !in_array($filterpath, $activefilters)); + if ($ADMIN->fulltree) { + include("$CFG->dirroot/$filterpath/filtersettings.php"); } + $ADMIN->add('filtersettings', $settings); } } - $catname =get_string('portfolios', 'portfolio'); + $catname = get_string('portfolios', 'portfolio'); $manage = get_string('manageportfolios', 'portfolio'); $url = "$CFG->wwwroot/$CFG->admin/portfolio.php"; diff --git a/lang/en_utf8/filter_tidy.php b/lang/en_utf8/filter_tidy.php new file mode 100644 index 0000000000..cbbdcf32e8 --- /dev/null +++ b/lang/en_utf8/filter_tidy.php @@ -0,0 +1,4 @@ +strtolower($name), $query) !== false) { - return true; - } + foreach ($filternames as $path => $strfiltername) { + if (strpos($textlib->strtolower($strfiltername), $query) !== false) { + return true; + } + list($type, $filter) = explode('/', $path); + if (strpos($filter, $query) !== false) { + return true; } } return false; @@ -3752,25 +3749,12 @@ class admin_setting_managefilters extends admin_setting { // get a list of possible filters (and translate name if possible) // note filters can be in the dedicated filters area OR in their // associated modules - $installedfilters = array(); + $installedfilters = filter_get_all_installed(); $filtersettings_new = array(); - $filterlocations = array('mod','filter'); - foreach ($filterlocations as $filterlocation) { - $plugins = get_list_of_plugins($filterlocation); - foreach ($plugins as $plugin) { - $pluginpath = "$CFG->dirroot/$filterlocation/$plugin/filter.php"; - $settingspath_new = "$CFG->dirroot/$filterlocation/$plugin/filtersettings.php"; - if (is_readable($pluginpath)) { - $name = trim(get_string("filtername", $plugin)); - if (empty($name) or ($name == '[[filtername]]')) { - $textlib = textlib_get_instance(); - $name = $textlib->strtotitle($plugin); - } - $installedfilters["$filterlocation/$plugin"] = $name; - if (is_readable($settingspath_new)) { - $filtersettings_new[] = "$filterlocation/$plugin"; - } - } + foreach ($installedfilters as $path => $strfiltername) { + $settingspath_new = $CFG->dirroot . '/' . $path . '/filtersettings.php'; + if (is_readable($settingspath_new)) { + $filtersettings_new[] = $path; } } @@ -3790,8 +3774,8 @@ class admin_setting_managefilters extends admin_setting { } } - // construct the display array with installed filters - // at the top in the right order + // Get the list of all filters, and pull the active filters + // to the top. $displayfilters = array(); foreach ($activefilters as $activefilter) { $name = $installedfilters[$activefilter]; diff --git a/lib/filterlib.php b/lib/filterlib.php index db32633f18..74f203fed8 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -56,7 +56,7 @@ abstract class filter_base { /// Define one exclusive separator that we'll use in the temp saved tags /// keys. It must be something rare enough to avoid having matches with /// filterobjects. MDL-18165 -define ('EXCL_SEPARATOR', '-%-'); +define('EXCL_SEPARATOR', '-%-'); /** * This is just a little object to define a phrase and some instructions @@ -96,6 +96,62 @@ class filterobject { } } +/** + * Look up the name of this filter in the most appropriate location. + * If $filterlocation = 'mod' then does get_string('filtername', $filter); + * else if $filterlocation = 'filter' then does get_string('filtername', 'filter_' . $filter); + * with a fallback to get_string('filtername', $filter) for backwards compatibility. + * These are the only two options supported at the moment. + * @param string $filterlocation 'filter' or 'mod'. + * @param string $filter the folder name where the filter lives. + * @return string the human-readable name for this filter. + */ +function filter_get_name($filterlocation, $filter) { + switch ($filterlocation) { + case 'filter': + $strfiltername = get_string('filtername', 'filter_' . $filter); + if (substr($strfiltername, 0, 2) != '[[') { + // found a valid string. + return $strfiltername; + } + // Fall through to try the legacy location. + + case 'mod': + $strfiltername = get_string('filtername', $filter); + if (substr($strfiltername, 0, 2) == '[[') { + $strfiltername .= ' (' . $filterlocation . '/' . $filter . ')'; + } + return $strfiltername; + + default: + throw new coding_exception('Unknown filter location ' . $filterlocation); + } +} + +/** + * Get the names of all the filters installed in this Moodle. + * @return array path => filter name from the appropriate lang file. e.g. + * array('mod/glossary' => 'Glossary Auto-linking', 'filter/tex' => 'TeX Notation'); + * sorted in alphabetical order of name. + */ +function filter_get_all_installed() { + global $CFG; + $filternames = array(); + $filterlocations = array('mod', 'filter'); + foreach ($filterlocations as $filterlocation) { + $filters = get_list_of_plugins($filterlocation); + foreach ($filters as $filter) { + $path = $filterlocation . '/' . $filter; + if (is_readable($CFG->dirroot . '/' . $path . '/filter.php')) { + $strfiltername = filter_get_name($filterlocation, $filter); + $filternames[$path] = $strfiltername; + } + } + } + asort($filternames, SORT_LOCALE_STRING); + return $filternames; +} + /** * Process phrases intelligently found within a HTML text (such as adding links) * @@ -104,7 +160,7 @@ class filterobject { * param ignoretagsopen an array of opening tags that we should ignore while filtering * param ignoretagsclose an array of corresponding closing tags **/ -function filter_phrases ($text, &$link_array, $ignoretagsopen=NULL, $ignoretagsclose=NULL) { +function filter_phrases($text, &$link_array, $ignoretagsopen=NULL, $ignoretagsclose=NULL) { global $CFG; @@ -296,11 +352,8 @@ function filter_phrases ($text, &$link_array, $ignoretagsopen=NULL, $ignoretagsc return $text; - } - - function filter_remove_duplicates($linkarray) { $concepts = array(); // keep a record of concepts as we cycle through diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 5839210af9..0869278036 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -8342,11 +8342,7 @@ function get_plugin_name($plugin, $type='mod') { } break; case 'filter': - $plugin_name = trim(get_string('filtername', $plugin)); - if (empty($plugin_name) or ($plugin_name == '[[filtername]]')) { - $textlib = textlib_get_instance(); - $plugin_name = $textlib->strtotitle($plugin); - } + $plugin_name = filter_get_name('filter', $plugin); break; default: $plugin_name = $plugin;