From dcfffe30727d63c159abde49eede04c6680e17ca Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Thu, 7 Oct 2010 08:57:20 +0000 Subject: [PATCH] MDL-24531 Extend filter manager API so that options can be passed to the filters The only option passed at the moment is the original format in which the user inserted the text. Other options can be added later if/when needed. --- filter/activitynames/filter.php | 2 +- filter/algebra/filter.php | 2 +- filter/censor/filter.php | 2 +- filter/emailprotect/filter.php | 2 +- filter/mediaplugin/filter.php | 2 +- filter/multilang/filter.php | 2 +- filter/tex/filter.php | 2 +- filter/tidy/filter.php | 2 +- lib/filterlib.php | 25 +++++++++++++++---------- lib/weblib.php | 6 +++--- 10 files changed, 26 insertions(+), 21 deletions(-) diff --git a/filter/activitynames/filter.php b/filter/activitynames/filter.php index e4bf1f10289..a235092fab7 100644 --- a/filter/activitynames/filter.php +++ b/filter/activitynames/filter.php @@ -35,7 +35,7 @@ class filter_activitynames extends moodle_text_filter { static $activitylist = null; static $cachedcourseid; - function filter($text) { + function filter($text, array $options = array()) { global $CFG, $COURSE, $DB; if (!$courseid = get_courseid_from_context($this->context)) { diff --git a/filter/algebra/filter.php b/filter/algebra/filter.php index c913b245ba1..b196f2de112 100644 --- a/filter/algebra/filter.php +++ b/filter/algebra/filter.php @@ -92,7 +92,7 @@ function filter_algebra_image($imagefile, $tex= "", $height="", $width="", $alig } class filter_algebra extends moodle_text_filter { - function filter($text){ + function filter($text, array $options = array()){ global $CFG, $DB; /// Do a quick check using stripos to avoid unnecessary wor diff --git a/filter/censor/filter.php b/filter/censor/filter.php index 92c9053ca60..67d695ed8a7 100644 --- a/filter/censor/filter.php +++ b/filter/censor/filter.php @@ -52,7 +52,7 @@ class filter_censor extends moodle_text_filter { return $cap; } - function filter($text){ + function filter($text, array $options = array()){ static $words; global $CFG; diff --git a/filter/emailprotect/filter.php b/filter/emailprotect/filter.php index a8d266f0605..2d0dbc480e6 100644 --- a/filter/emailprotect/filter.php +++ b/filter/emailprotect/filter.php @@ -31,7 +31,7 @@ defined('MOODLE_INTERNAL') || die(); * hides them using the Moodle obfuscate_text function. */ class filter_emailprotect extends moodle_text_filter { - function filter($text) { + function filter($text, array $options = array()) { /// Do a quick check using stripos to avoid unnecessary work if (strpos($text, '@') === false) { return $text; diff --git a/filter/mediaplugin/filter.php b/filter/mediaplugin/filter.php index 266d8a90949..838f4cb6ed4 100644 --- a/filter/mediaplugin/filter.php +++ b/filter/mediaplugin/filter.php @@ -33,7 +33,7 @@ require_once($CFG->libdir.'/filelib.php'); class filter_mediaplugin extends moodle_text_filter { private $eolas_fix_applied = false; - function filter($text) { + function filter($text, array $options = array()) { global $CFG, $PAGE; // You should never modify parameters passed to a method or function, it's BAD practice. Create a copy instead. // The reason is that you must always be able to refer to the original parameter that was passed. diff --git a/filter/multilang/filter.php b/filter/multilang/filter.php index d2a237239d8..efb618f705b 100644 --- a/filter/multilang/filter.php +++ b/filter/multilang/filter.php @@ -40,7 +40,7 @@ defined('MOODLE_INTERNAL') || die(); // one langanother language class filter_multilang extends moodle_text_filter { - function filter($text) { + function filter($text, array $options = array()) { global $CFG; // [pj] I don't know about you but I find this new implementation funny :P diff --git a/filter/tex/filter.php b/filter/tex/filter.php index 5287b4bf69e..78a831ec895 100644 --- a/filter/tex/filter.php +++ b/filter/tex/filter.php @@ -105,7 +105,7 @@ function filter_text_image($imagefile, $tex= "", $height="", $width="", $align=" } class filter_tex extends moodle_text_filter { - function filter ($text) { + function filter ($text, array $options = array()) { global $CFG, $DB; diff --git a/filter/tidy/filter.php b/filter/tidy/filter.php index f38d3a19d3d..567f9b683bc 100644 --- a/filter/tidy/filter.php +++ b/filter/tidy/filter.php @@ -38,7 +38,7 @@ defined('MOODLE_INTERNAL') || die(); // values are, see http://php.net/manual/en/function.tidy-get-config.php. class filter_tidy extends moodle_text_filter { - function filter($text) { + function filter($text, array $options = array()) { /// Configuration for tidy. Feel free to tune for your needs, e.g. to allow /// proprietary markup. diff --git a/lib/filterlib.php b/lib/filterlib.php index addb2dd4dd0..e99d558fc19 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -148,11 +148,12 @@ class filter_manager { * @todo Document this function * @param string $text * @param array $filterchain + * @param array $options options passed to the filters * @return string $text */ - protected function apply_filter_chain($text, $filterchain) { + protected function apply_filter_chain($text, $filterchain, array $options = array()) { foreach ($filterchain as $filter) { - $text = $filter->filter($text); + $text = $filter->filter($text, $options); } return $text; } @@ -186,10 +187,11 @@ class filter_manager { * * @param string $text The text to filter * @param object $context + * @param array $options options passed to the filters * @return string resulting text */ - public function filter_text($text, $context) { - $text = $this->apply_filter_chain($text, $this->get_text_filters($context)); + public function filter_text($text, $context, array $options = array()) { + $text = $this->apply_filter_chain($text, $this->get_text_filters($context), $options); /// tags removed for XHTML compatibility $text = str_replace(array('', ''), '', $text); return $text; @@ -236,7 +238,7 @@ class null_filter_manager { /** * @return string */ - public function filter_text($text, $context) { + public function filter_text($text, $context, $options) { return $text; } @@ -285,11 +287,12 @@ class performance_measuring_filter_manager extends filter_manager { /** * @param string $text * @param object $context + * @param array $options options passed to the filters * @return mixed */ - public function filter_text($text, $context) { + public function filter_text($text, $context, array $options = array()) { $this->textsfiltered++; - return parent::filter_text($text, $context); + return parent::filter_text($text, $context, $options); } /** @@ -332,7 +335,7 @@ class performance_measuring_filter_manager extends filter_manager { abstract class moodle_text_filter { /** @var object The context we are in. */ protected $context; - /** @var object Any local configuration for this filter in this context. */ + /** @var array Any local configuration for this filter in this context. */ protected $localconfig; /** @@ -357,9 +360,10 @@ abstract class moodle_text_filter { * Override this function to actually implement the filtering. * * @param $text some HTML content. + * @param array $options options passed to the filters * @return the HTML content after the filtering has been applied. */ - public abstract function filter($text); + public abstract function filter($text, array $options = array()); } /** @@ -391,9 +395,10 @@ class legacy_filter extends moodle_text_filter { /** * @param string $text + * @param array $options options - not supported for legacy filters * @return mixed */ - public function filter($text) { + public function filter($text, array $options = array()) { if ($this->courseid) { // old filters are called only when inside courses return call_user_func($this->filterfunction, $this->courseid, $text); diff --git a/lib/weblib.php b/lib/weblib.php index 04d4567e01f..1f8ab02c1f7 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1036,7 +1036,7 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_ if (!$options['noclean']) { $text = clean_text($text, FORMAT_HTML); } - $text = $filtermanager->filter_text($text, $context); + $text = $filtermanager->filter_text($text, $context, array('originalformat' => FORMAT_HTML)); break; case FORMAT_PLAIN: @@ -1062,7 +1062,7 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_ if (!$options['noclean']) { $text = clean_text($text, FORMAT_HTML); } - $text = $filtermanager->filter_text($text, $context); + $text = $filtermanager->filter_text($text, $context, array('originalformat' => FORMAT_MARKDOWN)); break; default: // FORMAT_MOODLE or anything else @@ -1070,7 +1070,7 @@ function format_text($text, $format = FORMAT_MOODLE, $options = NULL, $courseid_ if (!$options['noclean']) { $text = clean_text($text, FORMAT_HTML); } - $text = $filtermanager->filter_text($text, $context); + $text = $filtermanager->filter_text($text, $context, array('originalformat' => $format)); break; }