Skip to content

Commit

Permalink
MDL-24531 Extend filter manager API so that options can be passed to …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
mudrd8mz committed Oct 7, 2010
1 parent 695719f commit dcfffe3
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion filter/activitynames/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion filter/algebra/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion filter/censor/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function hash(){
return $cap;
}

function filter($text){
function filter($text, array $options = array()){
static $words;
global $CFG;

Expand Down
2 changes: 1 addition & 1 deletion filter/emailprotect/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* 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;
Expand Down
2 changes: 1 addition & 1 deletion filter/mediaplugin/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

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.
Expand Down
2 changes: 1 addition & 1 deletion filter/multilang/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
// <span lang="XX" class="multilang">one lang</span><span lang="YY" class="multilang">another language</span>

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
Expand Down
2 changes: 1 addition & 1 deletion filter/tex/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion filter/tidy/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
// 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.
Expand Down
25 changes: 15 additions & 10 deletions lib/filterlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@ protected function make_filter_object($filtername, $context, $localconfig) {
* @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;
}
Expand Down Expand Up @@ -186,10 +187,11 @@ protected function get_string_filters($context) {
*
* @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);
/// <nolink> tags removed for XHTML compatibility
$text = str_replace(array('<nolink>', '</nolink>'), '', $text);
return $text;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -285,11 +287,12 @@ protected function make_filter_object($filtername, $context, $localconfig) {
/**
* @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);
}

/**
Expand Down Expand Up @@ -332,7 +335,7 @@ public function get_performance_summary() {
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;

/**
Expand All @@ -357,9 +360,10 @@ public function hash() {
* 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());
}

/**
Expand Down Expand Up @@ -391,9 +395,10 @@ public function __construct($filterfunction, $context, array $localconfig) {

/**
* @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);
Expand Down
6 changes: 3 additions & 3 deletions lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -1062,15 +1062,15 @@ 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
$text = text_to_html($text, $options['smiley'], $options['para'], $options['newlines']);
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;
}

Expand Down

0 comments on commit dcfffe3

Please sign in to comment.