MDL-22414 Added helper methods to migrate files referenced by $@FILEPHP@$

This commit is contained in:
David Mudrak
2011-06-04 13:22:27 +02:00
parent d73c82e791
commit c818e2df49
2 changed files with 66 additions and 0 deletions
+48
View File
@@ -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()
*/
@@ -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. <br /><br /><img border="0" width="110" vspace="0" hspace="0" height="92" title="News" alt="News" src="$@FILEPHP@$$@SLASH@$pics$@SLASH@$news.gif" /><a href="$@FILEPHP@$$@SLASH@$pics$@SLASH@$news.gif$@FORCEDOWNLOAD@$">download image</a><br />
<br /><a href=\'$@FILEPHP@$$@SLASH@$MANUAL.DOC$@FORCEDOWNLOAD@$\'>download manual</a><br />';
$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. <br /><br /><img border="0" width="110" vspace="0" hspace="0" height="92" title="News" alt="News" src="@@PLUGINFILE@@/pics/news.gif" /><a href="@@PLUGINFILE@@/pics/news.gif?forcedownload=1">download image</a><br />
<br /><a href=\'$@FILEPHP@$$@SLASH@$MANUAL.DOC$@FORCEDOWNLOAD@$\'>download manual</a><br />');
}
public function test_question_bank_conversion() {
global $CFG;