diff --git a/backup/converter/moodle1/lib.php b/backup/converter/moodle1/lib.php
index fdc2529b121..afd3e35ce6a 100644
--- a/backup/converter/moodle1/lib.php
+++ b/backup/converter/moodle1/lib.php
@@ -536,6 +536,54 @@ class moodle1_converter extends base_converter {
return new moodle1_file_manager($this, $contextid, $component, $filearea, $itemid, $userid);
}
+ /**
+ * Detects all links to file.php encoded via $@FILEPHP@$ and returns the files to migrate
+ *
+ * @param string $text
+ * @return array
+ */
+ public static function find_referenced_files($text) {
+
+ $files = array();
+
+ if (empty($text) or is_numeric($text)) {
+ return $files;
+ }
+
+ $matches = array();
+ $pattern = '|(["\'])(\$@FILEPHP@\$.+?)\1|';
+ $result = preg_match_all($pattern, $text, $matches);
+ if ($result === false) {
+ throw new moodle1_convert_exception('error_while_searching_for_referenced_files');
+ }
+ if ($result == 0) {
+ return $files;
+ }
+ foreach ($matches[2] as $match) {
+ $files[] = str_replace(array('$@FILEPHP@$', '$@SLASH@$', '$@FORCEDOWNLOAD@$'), array('', '/', ''), $match);
+ }
+
+ return array_unique($files);
+ }
+
+ /**
+ * Given the list of migrated files, rewrites references to them from $@FILEPHP@$ form to the @@PLUGINFILE@@ one
+ *
+ * @param string $text
+ * @param array $files
+ * @return string
+ */
+ public static function rewrite_filephp_usage($text, array $files) {
+
+ foreach ($files as $file) {
+ $fileref = '$@FILEPHP@$'.str_replace('/', '$@SLASH@$', $file);
+ $text = str_replace($fileref.'$@FORCEDOWNLOAD@$', '@@PLUGINFILE@@'.$file.'?forcedownload=1', $text);
+ $text = str_replace($fileref, '@@PLUGINFILE@@'.$file, $text);
+ }
+
+ return $text;
+ }
+
/**
* @see parent::description()
*/
diff --git a/backup/converter/moodle1/simpletest/testlib.php b/backup/converter/moodle1/simpletest/testlib.php
index 434389134cc..7b00ea2f67c 100644
--- a/backup/converter/moodle1/simpletest/testlib.php
+++ b/backup/converter/moodle1/simpletest/testlib.php
@@ -381,6 +381,24 @@ class moodle1_converter_test extends UnitTestCase {
), true);
}
+ public function test_referenced_course_files() {
+
+ $text = 'This is a text containing links to file.php
+as it is parsed from the backup file.
download image
+
download manual
';
+
+ $files = moodle1_converter::find_referenced_files($text);
+ $this->assertIsA($files, 'array');
+ $this->assertEqual(2, count($files));
+ $this->assertTrue(in_array('/pics/news.gif', $files));
+ $this->assertTrue(in_array('/MANUAL.DOC', $files));
+
+ $text = moodle1_converter::rewrite_filephp_usage($text, array('/pics/news.gif', '/another/file/notused.txt'), $files);
+ $this->assertEqual($text, 'This is a text containing links to file.php
+as it is parsed from the backup file.
download image
+
download manual
');
+ }
+
public function test_question_bank_conversion() {
global $CFG;