diff --git a/lib/tests/weblib_test.php b/lib/tests/weblib_test.php
index 87185348148..1cdd34b6db0 100644
--- a/lib/tests/weblib_test.php
+++ b/lib/tests/weblib_test.php
@@ -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 = <<
+External link 2:
+Internal link 1:
+Internal link 2:
+Anchor link 1:Link text
+Anchor link 2:Link text
+Anchor + ext. img:
+Ext. anchor + img:
+SOURCE;
+ $expected = <<
+External link 2:
+Internal link 1:
+Internal link 2:
+Anchor link 1:Link text
+Anchor link 2:Link text
+Anchor + ext. img:
+Ext. anchor + img:
+EXPECTED;
+ $this->assertSame($expected, strip_pluginfile_content($source));
+ }
+
}
diff --git a/lib/weblib.php b/lib/weblib.php
index 955526e6b69..4c4499c5aea 100644
--- a/lib/weblib.php
+++ b/lib/weblib.php
@@ -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.
*