diff --git a/filter/glossary/filter.php b/filter/glossary/filter.php index 5c4ba448122..1f2591836a1 100644 --- a/filter/glossary/filter.php +++ b/filter/glossary/filter.php @@ -33,12 +33,8 @@ defined('MOODLE_INTERNAL') || die(); * NOTE: multilang glossary entries are not compatible with this filter. */ class filter_glossary extends moodle_text_filter { - /** @var int $cachecourseid cache invalidation flag in case content from multiple courses displayed. */ - protected $cachecourseid = null; - /** @var int $cacheuserid cache invalidation flag in case user is switched. */ - protected $cacheuserid = null; - /** @var array $cacheconceptlist page level filter cache, this should be always faster than MUC */ - protected $cacheconceptlist = null; + /** @var null|cache_store cache used to store the terms for this course. */ + protected $cache = null; public function setup($page, $context) { if ($page->requires->should_create_one_time_item_now('filter_glossary_autolinker')) { @@ -50,8 +46,16 @@ class filter_glossary extends moodle_text_filter { } } - public function filter($text, array $options = array()) { - global $CFG, $USER, $GLOSSARY_EXCLUDEENTRY; + /** + * Get all the concepts for this context. + * @return filterobject[] the concepts, and filterobjects. + */ + protected function get_all_concepts() { + global $USER; + + if ($this->cache === null) { + $this->cache = cache::make_from_params(cache_store::MODE_REQUEST, 'filter', 'glossary'); + } // Try to get current course. $coursectx = $this->context->get_course_context(false); @@ -62,96 +66,131 @@ class filter_glossary extends moodle_text_filter { $courseid = $coursectx->instanceid; } - if ($this->cachecourseid != $courseid or $this->cacheuserid != $USER->id) { + $cached = $this->cache->get('concepts'); + if ($cached !== false && ($cached->cachecourseid != $courseid || $cached->cacheuserid != $USER->id)) { // Invalidate the page cache. - $this->cacheconceptlist = null; + $cached = false; } - if (is_array($this->cacheconceptlist) and empty($GLOSSARY_EXCLUDEENTRY)) { - if (empty($this->cacheconceptlist)) { - return $text; - } - return filter_phrases($text, $this->cacheconceptlist); + if ($cached !== false && is_array($cached->cacheconceptlist)) { + return $cached->cacheconceptlist; } list($glossaries, $allconcepts) = \mod_glossary\local\concept_cache::get_concepts($courseid); if (!$allconcepts) { - $this->cacheuserid = $USER->id; - $this->cachecourseid = $courseid; - $this->cacheconcepts = array(); - return $text; + $tocache = new stdClass(); + $tocache->cacheuserid = $USER->id; + $tocache->cachecourseid = $courseid; + $tocache->cacheconceptlist = []; + $this->cache->set('concepts', $tocache); + return []; } - $strcategory = get_string('category', 'glossary'); - $conceptlist = array(); - $excluded = false; foreach ($allconcepts as $concepts) { foreach ($concepts as $concept) { - if (!empty($GLOSSARY_EXCLUDEENTRY) and $concept->id == $GLOSSARY_EXCLUDEENTRY) { - $excluded = true; - continue; - } - if ($concept->category) { // Link to a category. - // TODO: Fix this string usage. - $title = $glossaries[$concept->glossaryid] . ': ' . $strcategory . ' ' . $concept->concept; - $link = new moodle_url('/mod/glossary/view.php', array('g' => $concept->glossaryid, 'mode' => 'cat', 'hook' => $concept->id)); - $attributes = array( - 'href' => $link, - 'title' => $title, - 'class' => 'glossary autolink category glossaryid' . $concept->glossaryid); - - } else { // Link to entry or alias - $title = $glossaries[$concept->glossaryid] . ': ' . $concept->concept; - // Hardcoding dictionary format in the URL rather than defaulting - // to the current glossary format which may not work in a popup. - // for example "entry list" means the popup would only contain - // a link that opens another popup. - $link = new moodle_url('/mod/glossary/showentry.php', array('eid' => $concept->id, 'displayformat' => 'dictionary')); - $attributes = array( - 'href' => $link, - 'title' => str_replace('&', '&', $title), // Undo the s() mangling. - 'class' => 'glossary autolink concept glossaryid' . $concept->glossaryid); - } - // This flag is optionally set by resource_pluginfile() - // if processing an embedded file use target to prevent getting nested Moodles. - if (!empty($CFG->embeddedsoforcelinktarget)) { - $attributes['target'] = '_top'; - } - $href_tag_begin = html_writer::start_tag('a', $attributes); - - $conceptlist[] = new filterobject($concept->concept, $href_tag_begin, '', - $concept->casesensitive, $concept->fullmatch); + $conceptlist[] = new filterobject($concept->concept, null, null, + $concept->casesensitive, $concept->fullmatch, null, + [$this, 'filterobject_prepare_replacement_callback'], [$concept, $glossaries]); } } - usort($conceptlist, 'filter_glossary::sort_entries_by_length'); + // We sort longest first, so that when we replace the terms, + // the longest ones are replaced first. This does the right thing + // when you have two terms like 'Moodle' and 'Moodle 3.5'. You want the longest match. + usort($conceptlist, [$this, 'sort_entries_by_length']); - if (!$excluded) { - // Do not cache the excluded list here, it is used once per page only. - $this->cacheuserid = $USER->id; - $this->cachecourseid = $courseid; - $this->cacheconceptlist = $conceptlist; + $conceptlist = filter_prepare_phrases_for_filtering($conceptlist); + + $tocache = new stdClass(); + $tocache->cacheuserid = $USER->id; + $tocache->cachecourseid = $courseid; + $tocache->cacheconceptlist = $conceptlist; + $this->cache->set('concepts', $tocache); + + return $conceptlist; + } + + /** + * Callback used by filterobject / filter_phrases. + * + * @param object $concept the concept that is being replaced (from get_all_concepts). + * @param array $glossaries the list of glossary titles (from get_all_concepts). + * @return array [$hreftagbegin, $hreftagend, $replacementphrase] for filterobject. + */ + public function filterobject_prepare_replacement_callback($concept, $glossaries) { + global $CFG; + + if ($concept->category) { // Link to a category. + $title = get_string('glossarycategory', 'filter_glossary', + ['glossary' => $glossaries[$concept->glossaryid], 'category' => $concept->concept]); + $link = new moodle_url('/mod/glossary/view.php', + ['g' => $concept->glossaryid, 'mode' => 'cat', 'hook' => $concept->id]); + $attributes = array( + 'href' => $link, + 'title' => $title, + 'class' => 'glossary autolink category glossaryid' . $concept->glossaryid); + + } else { // Link to entry or alias. + $title = get_string('glossaryconcept', 'filter_glossary', + ['glossary' => $glossaries[$concept->glossaryid], 'concept' => $concept->concept]); + // Hardcoding dictionary format in the URL rather than defaulting + // to the current glossary format which may not work in a popup. + // for example "entry list" means the popup would only contain + // a link that opens another popup. + $link = new moodle_url('/mod/glossary/showentry.php', + ['eid' => $concept->id, 'displayformat' => 'dictionary']); + $attributes = array( + 'href' => $link, + 'title' => str_replace('&', '&', $title), // Undo the s() mangling. + 'class' => 'glossary autolink concept glossaryid' . $concept->glossaryid); + } + + // This flag is optionally set by resource_pluginfile() + // if processing an embedded file use target to prevent getting nested Moodles. + if (!empty($CFG->embeddedsoforcelinktarget)) { + $attributes['target'] = '_top'; + } + + return [html_writer::start_tag('a', $attributes), '', null]; + } + + public function filter($text, array $options = array()) { + global $GLOSSARY_EXCLUDEENTRY; + + $conceptlist = $this->get_all_concepts(); + + if (empty($conceptlist)) { + return $text; + } + + if (!empty($GLOSSARY_EXCLUDEENTRY)) { + foreach ($conceptlist as $key => $filterobj) { + // The original concept object was stored here in when $filterobj was constructed in + // get_all_concepts(). Get it back out now so we can check to see if it is excluded. + $concept = $filterobj->replacementcallbackdata[0]; + if (!$concept->category && $concept->id == $GLOSSARY_EXCLUDEENTRY) { + unset($conceptlist[$key]); + } + } } if (empty($conceptlist)) { return $text; } - return filter_phrases($text, $conceptlist); // Actually search for concepts! + + return filter_phrases($text, $conceptlist, null, null, false, true); } - private static function sort_entries_by_length($entry0, $entry1) { - $len0 = strlen($entry0->phrase); - $len1 = strlen($entry1->phrase); - - if ($len0 < $len1) { - return 1; - } else if ($len0 > $len1) { - return -1; - } else { - return 0; - } + /** + * usort helper used in get_all_concepts above. + * @param filterobject $filterobject0 first item to compare. + * @param filterobject $filterobject1 second item to compare. + * @return int -1, 0 or 1. + */ + private function sort_entries_by_length($filterobject0, $filterobject1) { + return strlen($filterobject1->phrase) <=> strlen($filterobject0->phrase); } } diff --git a/filter/glossary/lang/en/filter_glossary.php b/filter/glossary/lang/en/filter_glossary.php index 0f14c51d5fc..d7917afb25c 100644 --- a/filter/glossary/lang/en/filter_glossary.php +++ b/filter/glossary/lang/en/filter_glossary.php @@ -25,5 +25,7 @@ defined('MOODLE_INTERNAL') || die(); +$string['glossarycategory'] = '{$a->glossary}: Category {$a->category}'; +$string['glossaryconcept'] = '{$a->glossary}: {$a->concept}'; $string['filtername'] = 'Glossary auto-linking'; $string['privacy:metadata'] = 'The Glossary auto-linking plugin does not store any personal data.'; diff --git a/filter/glossary/tests/filter_test.php b/filter/glossary/tests/filter_test.php index ccf54eda2ad..f22822cf031 100644 --- a/filter/glossary/tests/filter_test.php +++ b/filter/glossary/tests/filter_test.php @@ -33,6 +33,119 @@ require_once($CFG->dirroot . '/filter/glossary/filter.php'); // Include the code */ class filter_glossary_filter_testcase extends advanced_testcase { + public function test_link_to_entry_with_alias() { + global $CFG; + $this->resetAfterTest(true); + + // Enable glossary filter at top level. + filter_set_global_state('glossary', TEXTFILTER_ON); + $CFG->glossary_linkentries = 1; + + // Create a test course. + $course = $this->getDataGenerator()->create_course(); + $context = context_course::instance($course->id); + + // Create a glossary. + $glossary = $this->getDataGenerator()->create_module('glossary', + array('course' => $course->id, 'mainglossary' => 1)); + + // Create two entries with ampersands and one normal entry. + $generator = $this->getDataGenerator()->get_plugin_generator('mod_glossary'); + $normal = $generator->create_content($glossary, array('concept' => 'entry name'), + array('first alias', 'second alias')); + + // Format text with all three entries in HTML. + $html = '

First we have entry name, then we have it twp aliases first alias and second alias.

'; + $filtered = format_text($html, FORMAT_HTML, array('context' => $context)); + + // Find all the glossary links in the result. + $matches = array(); + preg_match_all('~eid=([0-9]+).*?title="(.*?)"~', $filtered, $matches); + + // There should be 3 glossary links. + $this->assertEquals(3, count($matches[1])); + $this->assertEquals($normal->id, $matches[1][0]); + $this->assertEquals($normal->id, $matches[1][1]); + $this->assertEquals($normal->id, $matches[1][2]); + + // Check text of title attribute. + $this->assertEquals($glossary->name . ': entry name', $matches[2][0]); + $this->assertEquals($glossary->name . ': first alias', $matches[2][1]); + $this->assertEquals($glossary->name . ': second alias', $matches[2][2]); + } + + public function test_longest_link_used() { + global $CFG; + $this->resetAfterTest(true); + + // Enable glossary filter at top level. + filter_set_global_state('glossary', TEXTFILTER_ON); + $CFG->glossary_linkentries = 1; + + // Create a test course. + $course = $this->getDataGenerator()->create_course(); + $context = context_course::instance($course->id); + + // Create a glossary. + $glossary = $this->getDataGenerator()->create_module('glossary', + array('course' => $course->id, 'mainglossary' => 1)); + + // Create two entries with ampersands and one normal entry. + $generator = $this->getDataGenerator()->get_plugin_generator('mod_glossary'); + $shorter = $generator->create_content($glossary, array('concept' => 'Tim')); + $longer = $generator->create_content($glossary, array('concept' => 'Time')); + + // Format text with all three entries in HTML. + $html = '

Time will tell

'; + $filtered = format_text($html, FORMAT_HTML, array('context' => $context)); + + // Find all the glossary links in the result. + $matches = array(); + preg_match_all('~eid=([0-9]+).*?title="(.*?)"~', $filtered, $matches); + + // There should be 1 glossary link to Time, not Tim. + $this->assertEquals(1, count($matches[1])); + $this->assertEquals($longer->id, $matches[1][0]); + + // Check text of title attribute. + $this->assertEquals($glossary->name . ': Time', $matches[2][0]); + } + + public function test_link_to_category() { + global $CFG; + $this->resetAfterTest(true); + + // Enable glossary filter at top level. + filter_set_global_state('glossary', TEXTFILTER_ON); + $CFG->glossary_linkentries = 1; + + // Create a test course. + $course = $this->getDataGenerator()->create_course(); + $context = context_course::instance($course->id); + + // Create a glossary. + $glossary = $this->getDataGenerator()->create_module('glossary', + array('course' => $course->id, 'mainglossary' => 1)); + + // Create two entries with ampersands and one normal entry. + /** @var mod_glossary_generator $generator */ + $generator = $this->getDataGenerator()->get_plugin_generator('mod_glossary'); + $category = $generator->create_category($glossary, array('name' => 'My category', 'usedynalink' => 1)); + + // Format text with all three entries in HTML. + $html = '

This is My category you know.

'; + $filtered = format_text($html, FORMAT_HTML, array('context' => $context)); + + // Find all the glossary links in the result. + $matches = array(); + preg_match_all('~hook=([0-9]+).*?title="(.*?)"~', $filtered, $matches); + + // There should be 1 glossary link. + $this->assertEquals(1, count($matches[1])); + $this->assertEquals($category->id, $matches[1][0]); + $this->assertEquals($glossary->name . ': Category My category', $matches[2][0]); + } + /** * Test ampersands. */ @@ -59,9 +172,6 @@ class filter_glossary_filter_testcase extends advanced_testcase { $amp1 = $generator->create_content($glossary, array('concept' => 'A&B')); $amp2 = $generator->create_content($glossary, array('concept' => 'C&D')); - filter_manager::reset_caches(); - \mod_glossary\local\concept_cache::reset_caches(); - // Format text with all three entries in HTML. $html = '

A&B C&D normal

'; $filtered = format_text($html, FORMAT_HTML, array('context' => $context)); @@ -81,4 +191,81 @@ class filter_glossary_filter_testcase extends advanced_testcase { $this->assertEquals($glossary->name . ': C&D', $matches[2][1]); $this->assertEquals($glossary->name . ': normal', $matches[2][2]); } + + public function test_exclude_excludes_link_to_entry_with_alias() { + global $CFG, $GLOSSARY_EXCLUDEENTRY; + + $this->resetAfterTest(true); + + // Enable glossary filter at top level. + filter_set_global_state('glossary', TEXTFILTER_ON); + $CFG->glossary_linkentries = 1; + + // Create a test course. + $course = $this->getDataGenerator()->create_course(); + $context = context_course::instance($course->id); + + // Create a glossary. + $glossary = $this->getDataGenerator()->create_module('glossary', + array('course' => $course->id, 'mainglossary' => 1)); + + // Create two entries with ampersands and one normal entry. + $generator = $this->getDataGenerator()->get_plugin_generator('mod_glossary'); + $tobeexcluded = $generator->create_content($glossary, array('concept' => 'entry name'), + array('first alias', 'second alias')); + $normal = $generator->create_content($glossary, array('concept' => 'other entry')); + + // Format text with all three entries in HTML. + $html = '

First we have entry name, then we have it twp aliases first alias and second alias. ' . + 'In this case, those should not be linked, but this other entry should be.

'; + $GLOSSARY_EXCLUDEENTRY = $tobeexcluded->id; + $filtered = format_text($html, FORMAT_HTML, array('context' => $context)); + $GLOSSARY_EXCLUDEENTRY = null; + + // Find all the glossary links in the result. + $matches = array(); + preg_match_all('~eid=([0-9]+).*?title="(.*?)"~', $filtered, $matches); + + // There should be 1 glossary links. + $this->assertEquals(1, count($matches[1])); + $this->assertEquals($normal->id, $matches[1][0]); + $this->assertEquals($glossary->name . ': other entry', $matches[2][0]); + } + + public function test_exclude_does_not_exclude_categories() { + global $CFG, $GLOSSARY_EXCLUDEENTRY; + $this->resetAfterTest(true); + + // Enable glossary filter at top level. + filter_set_global_state('glossary', TEXTFILTER_ON); + $CFG->glossary_linkentries = 1; + + // Create a test course. + $course = $this->getDataGenerator()->create_course(); + $context = context_course::instance($course->id); + + // Create a glossary. + $glossary = $this->getDataGenerator()->create_module('glossary', + array('course' => $course->id, 'mainglossary' => 1)); + + // Create two entries with ampersands and one normal entry. + /** @var mod_glossary_generator $generator */ + $generator = $this->getDataGenerator()->get_plugin_generator('mod_glossary'); + $category = $generator->create_category($glossary, array('name' => 'My category', 'usedynalink' => 1)); + + // Format text with all three entries in HTML. + $html = '

This is My category you know.

'; + $GLOSSARY_EXCLUDEENTRY = $category->id; + $filtered = format_text($html, FORMAT_HTML, array('context' => $context)); + $GLOSSARY_EXCLUDEENTRY = null; + + // Find all the glossary links in the result. + $matches = array(); + preg_match_all('~hook=([0-9]+).*?title="(.*?)"~', $filtered, $matches); + + // There should be 1 glossary link. + $this->assertEquals(1, count($matches[1])); + $this->assertEquals($category->id, $matches[1][0]); + $this->assertEquals($glossary->name . ': Category My category', $matches[2][0]); + } } diff --git a/filter/upgrade.txt b/filter/upgrade.txt index ddfbd82c4bc..1dd24941a0a 100644 --- a/filter/upgrade.txt +++ b/filter/upgrade.txt @@ -1,6 +1,20 @@ This file describes API changes in core filter API and plugins, information provided here is intended especially for developers. +=== 3.6 === + +* Although there is no API change that require you to update your filter, + if you use the filter_phrases() helper method, you may wish to take + advantage of the changes that were made in MDL-47962 to improve performance. + + Now, instead of having to compute the replacement HTML for each phrase before + you construct the filterobject for it. You can instead pass a callback to + the filterobject constructor which is only called if the phrase is used. + + To understand how to use this, see the comment on filterobject::__construct and + look at the filter_glossary changes as an example: + https://github.com/timhunt/moodle/commit/0b9220b527b1d70d2a2fad46c71f1cf2928f8759 + === 3.0 === * New argument $skipfilters to filter_manager::filter_text to allow applying diff --git a/lib/filterlib.php b/lib/filterlib.php index bbb928947c7..d991075af96 100644 --- a/lib/filterlib.php +++ b/lib/filterlib.php @@ -216,7 +216,7 @@ class filter_manager { public function filter_text($text, $context, array $options = array(), array $skipfilters = null) { $text = $this->apply_filter_chain($text, $this->get_text_filters($context), $options, $skipfilters); - // tags removed for XHTML compatibility + // Remove tags for XHTML compatibility. $text = str_replace(array('', ''), '', $text); return $text; } @@ -461,54 +461,75 @@ abstract class moodle_text_filter { /** * This is just a little object to define a phrase and some instructions * for how to process it. Filters can create an array of these to pass - * to the filter_phrases function below. + * to the @{link filter_phrases()} function below. + * + * Note that although the fields here are public, you almost certainly should + * never use that. All that is supported is contructing new instances of this + * class, and then passing an array of them to filter_phrases. * * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - **/ + */ class filterobject { - /** @var string */ - var $phrase; - var $hreftagbegin; - var $hreftagend; - /** @var bool */ - var $casesensitive; - var $fullmatch; - /** @var mixed */ - var $replacementphrase; - var $work_phrase; - var $work_hreftagbegin; - var $work_hreftagend; - var $work_casesensitive; - var $work_fullmatch; - var $work_replacementphrase; - /** @var bool */ - var $work_calculated; + /** @var string this is the phrase that should be matched. */ + public $phrase; + + /** @var bool whether to match complete words. If true, 'T' won't be matched in 'Tim'. */ + public $fullmatch; + + /** @var bool whether the match needs to be case sensitive. */ + public $casesensitive; + + /** @var string HTML to insert before any match. */ + public $hreftagbegin; + /** @var string HTML to insert after any match. */ + public $hreftagend; + + /** @var null|string replacement text to go inside begin and end. If not set, + * the body of the replacement will be the original phrase. + */ + public $replacementphrase; + + /** @var null|string once initialised, holds the regexp for matching this phrase. */ + public $workregexp = null; + + /** @var null|string once initialised, holds the mangled HTML to replace the regexp with. */ + public $workreplacementphrase = null; /** - * A constructor just because I like constructing + * Constructor. * - * @param string $phrase - * @param string $hreftagbegin - * @param string $hreftagend - * @param bool $casesensitive - * @param bool $fullmatch - * @param mixed $replacementphrase + * @param string $phrase this is the phrase that should be matched. + * @param string $hreftagbegin HTML to insert before any match. Default ''. + * @param string $hreftagend HTML to insert after any match. Default ''. + * @param bool $casesensitive whether the match needs to be case sensitive + * @param bool $fullmatch whether to match complete words. If true, 'T' won't be matched in 'Tim'. + * @param mixed $replacementphrase replacement text to go inside begin and end. If not set, + * the body of the replacement will be the original phrase. + * @param callback $replacementcallback if set, then this will be called just before + * $hreftagbegin, $hreftagend and $replacementphrase are needed, so they can be computed only if required. + * The call made is + * list($linkobject->hreftagbegin, $linkobject->hreftagend, $linkobject->replacementphrase) = + * call_user_func_array($linkobject->replacementcallback, $linkobject->replacementcallbackdata); + * so the return should be an array [$hreftagbegin, $hreftagend, $replacementphrase], the last of which may be null. + * @param array $replacementcallbackdata data to be passed to $replacementcallback (optional). */ public function __construct($phrase, $hreftagbegin = '', - $hreftagend = '', - $casesensitive = false, - $fullmatch = false, - $replacementphrase = NULL) { - - $this->phrase = $phrase; - $this->hreftagbegin = $hreftagbegin; - $this->hreftagend = $hreftagend; - $this->casesensitive = $casesensitive; - $this->fullmatch = $fullmatch; - $this->replacementphrase= $replacementphrase; - $this->work_calculated = false; + $hreftagend = '', + $casesensitive = false, + $fullmatch = false, + $replacementphrase = null, + $replacementcallback = null, + array $replacementcallbackdata = null) { + $this->phrase = $phrase; + $this->hreftagbegin = $hreftagbegin; + $this->hreftagend = $hreftagend; + $this->casesensitive = !empty($casesensitive); + $this->fullmatch = !empty($fullmatch); + $this->replacementphrase = $replacementphrase; + $this->replacementcallback = $replacementcallback; + $this->replacementcallbackdata = $replacementcallbackdata; } } @@ -574,7 +595,6 @@ function filter_set_global_state($filtername, $state, $move = 0) { } if (strpos($filtername, 'filter/') === 0) { - //debugging("Old filtername '$filtername' parameter used in filter_set_global_state()", DEBUG_DEVELOPER); $filtername = substr($filtername, 7); } else if (strpos($filtername, '/') !== false) { throw new coding_exception("Invalid filter name '$filtername' used in filter_set_global_state()"); @@ -588,7 +608,7 @@ function filter_set_global_state($filtername, $state, $move = 0) { $on = array(); $off = array(); - foreach($filters as $f) { + foreach ($filters as $f) { if ($f->active == TEXTFILTER_DISABLED) { $off[$f->filter] = $f; } else { @@ -675,13 +695,13 @@ function filter_set_global_state($filtername, $state, $move = 0) { $i = 1; foreach ($on as $f) { if ($f->sortorder != $i) { - $DB->set_field('filter_active', 'sortorder', $i, array('id'=>$f->id)); + $DB->set_field('filter_active', 'sortorder', $i, array('id' => $f->id)); } $i++; } foreach ($off as $f) { if ($f->sortorder != $i) { - $DB->set_field('filter_active', 'sortorder', $i, array('id'=>$f->id)); + $DB->set_field('filter_active', 'sortorder', $i, array('id' => $f->id)); } $i++; } @@ -696,7 +716,6 @@ function filter_set_global_state($filtername, $state, $move = 0) { */ function filter_is_enabled($filtername) { if (strpos($filtername, 'filter/') === 0) { - //debugging("Old filtername '$filtername' parameter used in filter_is_enabled()", DEBUG_DEVELOPER); $filtername = substr($filtername, 7); } else if (strpos($filtername, '/') !== false) { throw new coding_exception("Invalid filter name '$filtername' used in filter_is_enabled()"); @@ -707,7 +726,6 @@ function filter_is_enabled($filtername) { /** * 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() { @@ -965,7 +983,7 @@ function filter_get_active_in_context($context) { $contextids = str_replace('/', ',', trim($context->path, '/')); // The following SQL is tricky. It is explained on - // http://docs.moodle.org/dev/Filter_enable/disable_by_context + // http://docs.moodle.org/dev/Filter_enable/disable_by_context. $sql = "SELECT active.filter, fc.name, fc.value FROM (SELECT f.filter, MAX(f.sortorder) AS sortorder FROM {filter_active} f @@ -1007,7 +1025,7 @@ function filter_preload_activities(course_modinfo $modinfo) { $FILTERLIB_PRIVATE = new stdClass(); } - // Don't repeat preload + // Don't repeat preload. if (!isset($FILTERLIB_PRIVATE->preloaded)) { $FILTERLIB_PRIVATE->preloaded = array(); } @@ -1016,7 +1034,7 @@ function filter_preload_activities(course_modinfo $modinfo) { } $FILTERLIB_PRIVATE->preloaded[$modinfo->get_course_id()] = true; - // Get contexts for all CMs + // Get contexts for all CMs. $cmcontexts = array(); $cmcontextids = array(); foreach ($modinfo->get_cms() as $cm) { @@ -1025,16 +1043,16 @@ function filter_preload_activities(course_modinfo $modinfo) { $cmcontexts[] = $modulecontext; } - // Get course context and all other parents... + // Get course context and all other parents. $coursecontext = context_course::instance($modinfo->get_course_id()); $parentcontextids = explode('/', substr($coursecontext->path, 1)); $allcontextids = array_merge($cmcontextids, $parentcontextids); - // Get all filter_active rows relating to all these contexts + // Get all filter_active rows relating to all these contexts. list ($sql, $params) = $DB->get_in_or_equal($allcontextids); $filteractives = $DB->get_records_select('filter_active', "contextid $sql", $params, 'sortorder'); - // Get all filter_config only for the cm contexts + // Get all filter_config only for the cm contexts. list ($sql, $params) = $DB->get_in_or_equal($cmcontextids); $filterconfigs = $DB->get_records_select('filter_config', "contextid $sql", $params); @@ -1044,20 +1062,20 @@ function filter_preload_activities(course_modinfo $modinfo) { // filter_get_active_in_context, this does seem to be correct. // Build course default active list. Initially this will be an array of - // filter name => active score (where an active score >0 means it's active) + // filter name => active score (where an active score >0 means it's active). $courseactive = array(); - // Also build list of filter_active rows below course level, by contextid + // Also build list of filter_active rows below course level, by contextid. $remainingactives = array(); - // Array lists filters that are banned at top level + // Array lists filters that are banned at top level. $banned = array(); - // Add any active filters in parent contexts to the array + // Add any active filters in parent contexts to the array. foreach ($filteractives as $row) { $depth = array_search($row->contextid, $parentcontextids); if ($depth !== false) { - // Find entry + // Find entry. if (!array_key_exists($row->filter, $courseactive)) { $courseactive[$row->filter] = 0; } @@ -1072,7 +1090,7 @@ function filter_preload_activities(course_modinfo $modinfo) { $banned[$row->filter] = true; } } else { - // Build list of other rows indexed by contextid + // Build list of other rows indexed by contextid. if (!array_key_exists($row->contextid, $remainingactives)) { $remainingactives[$row->contextid] = array(); } @@ -1081,7 +1099,7 @@ function filter_preload_activities(course_modinfo $modinfo) { } // Chuck away the ones that aren't active. - foreach ($courseactive as $filter=>$score) { + foreach ($courseactive as $filter => $score) { if ($score <= 0) { unset($courseactive[$filter]); } else { @@ -1095,7 +1113,7 @@ function filter_preload_activities(course_modinfo $modinfo) { $FILTERLIB_PRIVATE->active = array(); } foreach ($cmcontextids as $contextid) { - // Copy course list + // Copy course list. $FILTERLIB_PRIVATE->active[$contextid] = $courseactive; // Are there any changes to the active list? @@ -1242,33 +1260,40 @@ function filter_context_may_have_filter_settings($context) { /** * Process phrases intelligently found within a HTML text (such as adding links). * - * @staticvar array $usedpharses - * @param string $text the text that we are filtering - * @param array $link_array an array of filterobjects + * @param string $text the text that we are filtering + * @param filterobject[] $linkarray an array of filterobjects * @param array $ignoretagsopen an array of opening tags that we should ignore while filtering * @param array $ignoretagsclose an array of corresponding closing tags * @param bool $overridedefaultignore True to only use tags provided by arguments + * @param bool $linkarrayalreadyprepared True to say that filter_prepare_phrases_for_filtering + * has already been called for $linkarray. Default false. * @return string - **/ -function filter_phrases($text, &$link_array, $ignoretagsopen=NULL, $ignoretagsclose=NULL, - $overridedefaultignore=false) { + */ +function filter_phrases($text, $linkarray, $ignoretagsopen = null, $ignoretagsclose = null, + $overridedefaultignore = false, $linkarrayalreadyprepared = false) { global $CFG; - static $usedphrases; + // Used if $CFG->filtermatchoneperpage is on. Array with keys being the workregexp + // for things that have already been matched on this page. + static $usedphrases = []; $ignoretags = array(); // To store all the enclosig tags to be completely ignored. $tags = array(); // To store all the simple tags to be ignored. + if (!$linkarrayalreadyprepared) { + $linkarray = filter_prepare_phrases_for_filtering($linkarray); + } + if (!$overridedefaultignore) { - // A list of open/close tags that we should not replace within - // Extended to include ', '', '',''); + '', '', '', ''); } else { // Set an empty default list. $filterignoretagsopen = array(); @@ -1285,29 +1310,90 @@ function filter_phrases($text, &$link_array, $ignoretagsopen=NULL, $ignoretagscl } } - // Invalid prefixes and suffixes for the fullmatch searches - // Every "word" character, but the underscore, is a invalid suffix or prefix. - // (nice to use this because it includes national characters (accents...) as word characters. - $filterinvalidprefixes = '([^\W_])'; - $filterinvalidsuffixes = '([^\W_])'; + // Double up some magic chars to avoid "accidental matches". + $text = preg_replace('/([#*%])/', '\1\1', $text); - // Double up some magic chars to avoid "accidental matches" - $text = preg_replace('/([#*%])/','\1\1',$text); + // Remove everything enclosed by the ignore tags from $text. + filter_save_ignore_tags($text, $filterignoretagsopen, $filterignoretagsclose, $ignoretags); + // Remove tags from $text. + filter_save_tags($text, $tags); - //Remove everything enclosed by the ignore tags from $text - filter_save_ignore_tags($text,$filterignoretagsopen,$filterignoretagsclose,$ignoretags); + // Prepare the limit for preg_match calls. + if (!empty($CFG->filtermatchonepertext) || !empty($CFG->filtermatchoneperpage)) { + $pregreplacelimit = 1; + } else { + $pregreplacelimit = -1; // No limit. + } - // Remove tags from $text - filter_save_tags($text,$tags); + // Time to cycle through each phrase to be linked. + foreach ($linkarray as $key => $linkobject) { + if ($linkobject->workregexp === null) { + // This is the case if, when preparing the phrases for filtering, + // we decided that this was not a suitable phrase to match. + continue; + } - // Time to cycle through each phrase to be linked - $size = sizeof($link_array); - for ($n=0; $n < $size; $n++) { - $linkobject =& $link_array[$n]; + // If $CFG->filtermatchoneperpage, avoid previously matched linked phrases. + if (!empty($CFG->filtermatchoneperpage) && isset($usedphrases[$linkobject->workregexp])) { + continue; + } - // Set some defaults if certain properties are missing - // Properties may be missing if the filterobject class has not been used to construct the object + // Do our highlighting. + $resulttext = preg_replace_callback($linkobject->workregexp, + function ($matches) use ($linkobject) { + if ($linkobject->workreplacementphrase === null) { + filter_prepare_phrase_for_replacement($linkobject); + } + + return str_replace('$1', $matches[1], $linkobject->workreplacementphrase); + }, $text, $pregreplacelimit); + + // If the text has changed we have to look for links again. + if ($resulttext != $text) { + $text = $resulttext; + // Remove everything enclosed by the ignore tags from $text. + filter_save_ignore_tags($text, $filterignoretagsopen, $filterignoretagsclose, $ignoretags); + // Remove tags from $text. + filter_save_tags($text, $tags); + // If $CFG->filtermatchoneperpage, save linked phrases to request. + if (!empty($CFG->filtermatchoneperpage)) { + $usedphrases[$linkobject->workregexp] = 1; + } + } + } + + // Rebuild the text with all the excluded areas. + if (!empty($tags)) { + $text = str_replace(array_keys($tags), $tags, $text); + } + + if (!empty($ignoretags)) { + $ignoretags = array_reverse($ignoretags); // Reversed so "progressive" str_replace() will solve some nesting problems. + $text = str_replace(array_keys($ignoretags), $ignoretags, $text); + } + + // Remove the protective doubleups. + $text = preg_replace('/([#*%])(\1)/', '\1', $text); + + // Add missing javascript for popus. + $text = filter_add_javascript($text); + + return $text; +} + +/** + * Prepare a list of link for processing with {@link filter_phrases()}. + * + * @param filterobject[] $linkarray the links that will be passed to filter_phrases(). + * @return filterobject[] the updated list of links with necessary pre-processing done. + */ +function filter_prepare_phrases_for_filtering(array $linkarray) { + // Time to cycle through each phrase to be linked. + foreach ($linkarray as $linkobject) { + + // Set some defaults if certain properties are missing. + // Properties may be missing if the filterobject class has not been used to construct the object. if (empty($linkobject->phrase)) { continue; } @@ -1318,137 +1404,63 @@ function filter_phrases($text, &$link_array, $ignoretagsopen=NULL, $ignoretagscl continue; } - // All this work has to be done ONLY it it hasn't been done before - if (!$linkobject->work_calculated) { - if (!isset($linkobject->hreftagbegin) or !isset($linkobject->hreftagend)) { - $linkobject->work_hreftagbegin = 'work_hreftagend = ''; - } else { - $linkobject->work_hreftagbegin = $linkobject->hreftagbegin; - $linkobject->work_hreftagend = $linkobject->hreftagend; - } - - // Double up chars to protect true duplicates - // be cleared up before returning to the user. - $linkobject->work_hreftagbegin = preg_replace('/([#*%])/','\1\1',$linkobject->work_hreftagbegin); - - if (empty($linkobject->casesensitive)) { - $linkobject->work_casesensitive = false; - } else { - $linkobject->work_casesensitive = true; - } - if (empty($linkobject->fullmatch)) { - $linkobject->work_fullmatch = false; - } else { - $linkobject->work_fullmatch = true; - } - - // Strip tags out of the phrase - $linkobject->work_phrase = strip_tags($linkobject->phrase); - - // Double up chars that might cause a false match -- the duplicates will - // be cleared up before returning to the user. - $linkobject->work_phrase = preg_replace('/([#*%])/','\1\1',$linkobject->work_phrase); - - // Set the replacement phrase properly - if ($linkobject->replacementphrase) { //We have specified a replacement phrase - // Strip tags - $linkobject->work_replacementphrase = strip_tags($linkobject->replacementphrase); - } else { //The replacement is the original phrase as matched below - $linkobject->work_replacementphrase = '$1'; - } - - // Quote any regular expression characters and the delimiter in the work phrase to be searched - $linkobject->work_phrase = preg_quote($linkobject->work_phrase, '/'); - - // Work calculated - $linkobject->work_calculated = true; + // Strip tags out of the phrase. + $linkobject->workregexp = strip_tags($linkobject->phrase); + if (!$linkobject->casesensitive) { + $linkobject->workregexp = core_text::strtolower($linkobject->workregexp); } - // If $CFG->filtermatchoneperpage, avoid previously (request) linked phrases - if (!empty($CFG->filtermatchoneperpage)) { - if (!empty($usedphrases) && in_array($linkobject->work_phrase,$usedphrases)) { - continue; - } + // Double up chars that might cause a false match -- the duplicates will + // be cleared up before returning to the user. + $linkobject->workregexp = preg_replace('/([#*%])/', '\1\1', $linkobject->workregexp); + + // Quote any regular expression characters and the delimiter in the work phrase to be searched. + $linkobject->workregexp = preg_quote($linkobject->workregexp, '/'); + + if ($linkobject->fullmatch) { + $linkobject->workregexp = '\b' . $linkobject->workregexp . '\b'; } - // Regular expression modifiers - $modifiers = ($linkobject->work_casesensitive) ? 's' : 'isu'; // works in unicode mode! + $linkobject->workregexp = '/(' . $linkobject->workregexp . ')/s'; - // Do we need to do a fullmatch? - // If yes then go through and remove any non full matching entries - if ($linkobject->work_fullmatch) { - $notfullmatches = array(); - $regexp = '/'.$filterinvalidprefixes.'('.$linkobject->work_phrase.')|('.$linkobject->work_phrase.')'.$filterinvalidsuffixes.'/'.$modifiers; - - preg_match_all($regexp,$text,$list_of_notfullmatches); - - if ($list_of_notfullmatches) { - foreach (array_unique($list_of_notfullmatches[0]) as $key=>$value) { - $notfullmatches['<*'.$key.'*>'] = $value; - } - if (!empty($notfullmatches)) { - $text = str_replace($notfullmatches,array_keys($notfullmatches),$text); - } - } - } - - // Finally we do our highlighting - if (!empty($CFG->filtermatchonepertext) || !empty($CFG->filtermatchoneperpage)) { - $resulttext = preg_replace('/('.$linkobject->work_phrase.')/'.$modifiers, - $linkobject->work_hreftagbegin. - $linkobject->work_replacementphrase. - $linkobject->work_hreftagend, $text, 1); - } else { - $resulttext = preg_replace('/('.$linkobject->work_phrase.')/'.$modifiers, - $linkobject->work_hreftagbegin. - $linkobject->work_replacementphrase. - $linkobject->work_hreftagend, $text); - } - - - // If the text has changed we have to look for links again - if ($resulttext != $text) { - // Set $text to $resulttext - $text = $resulttext; - // Remove everything enclosed by the ignore tags from $text - filter_save_ignore_tags($text,$filterignoretagsopen,$filterignoretagsclose,$ignoretags); - // Remove tags from $text - filter_save_tags($text,$tags); - // If $CFG->filtermatchoneperpage, save linked phrases to request - if (!empty($CFG->filtermatchoneperpage)) { - $usedphrases[] = $linkobject->work_phrase; - } - } - - - // Replace the not full matches before cycling to next link object - if (!empty($notfullmatches)) { - $text = str_replace(array_keys($notfullmatches),$notfullmatches,$text); - unset($notfullmatches); + if (!$linkobject->casesensitive) { + $linkobject->workregexp .= 'iu'; } } - // Rebuild the text with all the excluded areas + return $linkarray; +} - if (!empty($tags)) { - $text = str_replace(array_keys($tags), $tags, $text); +/** + * Fill in the remaining ->work... fields, that would be needed to replace the phrase. + * + * @param filterobject $linkobject the link object on which to set additional fields. + */ +function filter_prepare_phrase_for_replacement(filterobject $linkobject) { + if ($linkobject->replacementcallback !== null) { + list($linkobject->hreftagbegin, $linkobject->hreftagend, $linkobject->replacementphrase) = + call_user_func_array($linkobject->replacementcallback, $linkobject->replacementcallbackdata); } - if (!empty($ignoretags)) { - $ignoretags = array_reverse($ignoretags); // Reversed so "progressive" str_replace() will solve some nesting problems. - $text = str_replace(array_keys($ignoretags),$ignoretags,$text); + if (!isset($linkobject->hreftagbegin) or !isset($linkobject->hreftagend)) { + $linkobject->hreftagbegin = 'hreftagend = ''; } - // Remove the protective doubleups - $text = preg_replace('/([#*%])(\1)/','\1',$text); + // Double up chars to protect true duplicates + // be cleared up before returning to the user. + $hreftagbeginmangled = preg_replace('/([#*%])/', '\1\1', $linkobject->hreftagbegin); - // Add missing javascript for popus - $text = filter_add_javascript($text); + // Set the replacement phrase properly. + if ($linkobject->replacementphrase) { // We have specified a replacement phrase. + $linkobject->workreplacementphrase = strip_tags($linkobject->replacementphrase); + } else { // The replacement is the original phrase as matched below. + $linkobject->workreplacementphrase = '$1'; + } - - return $text; + $linkobject->workreplacementphrase = $hreftagbeginmangled . + $linkobject->workreplacementphrase . $linkobject->hreftagend; } /** @@ -1459,12 +1471,12 @@ function filter_phrases($text, &$link_array, $ignoretagsopen=NULL, $ignoretagscl */ function filter_remove_duplicates($linkarray) { - $concepts = array(); // keep a record of concepts as we cycle through - $lconcepts = array(); // a lower case version for case insensitive + $concepts = array(); // Keep a record of concepts as we cycle through. + $lconcepts = array(); // A lower case version for case insensitive. $cleanlinks = array(); - foreach ($linkarray as $key=>$filterobject) { + foreach ($linkarray as $key => $filterobject) { if ($filterobject->casesensitive) { $exists = in_array($filterobject->phrase, $concepts); } else { @@ -1494,21 +1506,21 @@ function filter_remove_duplicates($linkarray) { **/ function filter_save_ignore_tags(&$text, $filterignoretagsopen, $filterignoretagsclose, &$ignoretags) { - // Remove everything enclosed by the ignore tags from $text - foreach ($filterignoretagsopen as $ikey=>$opentag) { + // Remove everything enclosed by the ignore tags from $text. + foreach ($filterignoretagsopen as $ikey => $opentag) { $closetag = $filterignoretagsclose[$ikey]; - // form regular expression - $opentag = str_replace('/','\/',$opentag); // delimit forward slashes - $closetag = str_replace('/','\/',$closetag); // delimit forward slashes + // Form regular expression. + $opentag = str_replace('/', '\/', $opentag); // Delimit forward slashes. + $closetag = str_replace('/', '\/', $closetag); // Delimit forward slashes. $pregexp = '/'.$opentag.'(.*?)'.$closetag.'/is'; - preg_match_all($pregexp, $text, $list_of_ignores); - foreach (array_unique($list_of_ignores[0]) as $key=>$value) { - $prefix = (string)(count($ignoretags) + 1); + preg_match_all($pregexp, $text, $listofignores); + foreach (array_unique($listofignores[0]) as $key => $value) { + $prefix = (string) (count($ignoretags) + 1); $ignoretags['<#'.$prefix.TEXTFILTER_EXCL_SEPARATOR.$key.'#>'] = $value; } if (!empty($ignoretags)) { - $text = str_replace($ignoretags,array_keys($ignoretags),$text); + $text = str_replace($ignoretags, array_keys($ignoretags), $text); } } } @@ -1523,13 +1535,13 @@ function filter_save_ignore_tags(&$text, $filterignoretagsopen, $filterignoretag **/ function filter_save_tags(&$text, &$tags) { - preg_match_all('/<([^#%*].*?)>/is',$text,$list_of_newtags); - foreach (array_unique($list_of_newtags[0]) as $ntkey=>$value) { + preg_match_all('/<([^#%*].*?)>/is', $text, $listofnewtags); + foreach (array_unique($listofnewtags[0]) as $ntkey => $value) { $prefix = (string)(count($tags) + 1); $tags['<%'.$prefix.TEXTFILTER_EXCL_SEPARATOR.$ntkey.'%>'] = $value; } if (!empty($tags)) { - $text = str_replace($tags,array_keys($tags),$text); + $text = str_replace($tags, array_keys($tags), $text); } } @@ -1542,13 +1554,13 @@ function filter_save_tags(&$text, &$tags) { function filter_add_javascript($text) { global $CFG; - if (stripos($text, '') === FALSE) { + if (stripos($text, '') === false) { return $text; // This is not a html file. } - if (strpos($text, 'onclick="return openpopup') === FALSE) { + if (strpos($text, 'onclick="return openpopup') === false) { return $text; // No popup - no need to add javascript. } - $js =" + $js = " "; - if (stripos($text, '') !== FALSE) { + if (stripos($text, '') !== false) { // Try to add it into the head element. $text = str_ireplace('', $js.'', $text); return $text;