MDL-42995 libraries: Global function to strip pluginfile content

This commit is contained in:
Frederic Massart 2013-12-19 10:19:42 +01:00
parent bbb291b7b7
commit 0a8b3583ca
2 changed files with 58 additions and 0 deletions

View File

@ -492,4 +492,43 @@ class core_weblib_testcase extends advanced_testcase {
$this->assertEquals(DEBUG_NONE, $CFG->debug);
$this->assertFalse($CFG->debugdeveloper);
}
public function test_strip_pluginfile_content() {
$source = <<<SOURCE
Hello!
I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
URL outside a tag: https://moodle.org/logo/logo-240x60.gif
Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
External link 1:<img src='https://moodle.org/logo/logo-240x60.gif' alt='Moodle'/>
External link 2:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif"/>
Internal link 1:<img src='@@PLUGINFILE@@/logo-240x60.gif' alt='Moodle'/>
Internal link 2:<img alt="Moodle" src="@@PLUGINFILE@@logo-240x60.gif"/>
Anchor link 1:<a href="@@PLUGINFILE@@logo-240x60.gif" alt="bananas">Link text</a>
Anchor link 2:<a title="bananas" href="../logo-240x60.gif">Link text</a>
Anchor + ext. img:<a title="bananas" href="../logo-240x60.gif"><img alt="Moodle" src="@@PLUGINFILE@@logo-240x60.gif"/></a>
Ext. anchor + img:<a href="@@PLUGINFILE@@logo-240x60.gif"><img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif"/></a>
SOURCE;
$expected = <<<EXPECTED
Hello!
I'm writing to you from the Moodle Majlis in Muscat, Oman, where we just had several days of Moodle community goodness.
URL outside a tag: https://moodle.org/logo/logo-240x60.gif
Plugin url outside a tag: @@PLUGINFILE@@/logo-240x60.gif
External link 1:<img src="https://moodle.org/logo/logo-240x60.gif" alt="Moodle" />
External link 2:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif" />
Internal link 1:
Internal link 2:
Anchor link 1:Link text
Anchor link 2:<a title="bananas" href="../logo-240x60.gif">Link text</a>
Anchor + ext. img:<a title="bananas" href="../logo-240x60.gif"></a>
Ext. anchor + img:<img alt="Moodle" src="https://moodle.org/logo/logo-240x60.gif" />
EXPECTED;
$this->assertSame($expected, strip_pluginfile_content($source));
}
}

View File

@ -1523,6 +1523,25 @@ function format_module_intro($module, $activity, $cmid, $filter=true) {
return trim(format_text($intro, $activity->introformat, $options, null));
}
/**
* Removes the usage of Moodle files from a text.
*
* In some rare cases we need to re-use a text that already has embedded links
* to some files hosted within Moodle. But the new area in which we will push
* this content does not support files... therefore we need to remove those files.
*
* @param string $source The text
* @return string The stripped text
*/
function strip_pluginfile_content($source) {
$baseurl = '@@PLUGINFILE@@';
// Looking for something like < .* "@@pluginfile@@.*" .* >
$pattern = '$<[^<>]+["\']' . $baseurl . '[^"\']*["\'][^<>]*>$';
$stripped = preg_replace($pattern, '', $source);
// Use purify html to rebalence potentially mismatched tags and generally cleanup.
return purify_html($stripped);
}
/**
* Legacy function, used for cleaning of old forum and glossary text only.
*