From 214c492447adcd3f9a156a740076a46eb16b228f Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Sun, 29 May 2011 00:18:19 +0200 Subject: [PATCH] Converter file manager's methods take relative path within the backup instead of the absolute path --- backup/converter/moodle1/handlerlib.php | 5 +- backup/converter/moodle1/lib.php | 23 ++++++--- .../converter/moodle1/simpletest/testlib.php | 50 +++++++++++++++++++ 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/backup/converter/moodle1/handlerlib.php b/backup/converter/moodle1/handlerlib.php index e206429afa8..afdeb217637 100644 --- a/backup/converter/moodle1/handlerlib.php +++ b/backup/converter/moodle1/handlerlib.php @@ -501,11 +501,10 @@ class moodle1_files_handler extends moodle1_xml_handler { * Migrates course_files in the converter workdir */ protected function migrate_course_files() { - $path = $this->converter->get_tempdir_path().'/course_files'; $ids = array(); $fileman = $this->converter->get_file_manager($this->converter->get_contextid(CONTEXT_COURSE), 'course', 'legacy'); - if (file_exists($path)) { - $ids = $fileman->migrate_directory($path); + if (file_exists($this->converter->get_tempdir_path().'/course_files')) { + $ids = $fileman->migrate_directory('course_files'); $this->converter->set_stash('course_files_ids', $ids); } } diff --git a/backup/converter/moodle1/lib.php b/backup/converter/moodle1/lib.php index 39a1555ffac..02ea64097f6 100644 --- a/backup/converter/moodle1/lib.php +++ b/backup/converter/moodle1/lib.php @@ -1027,6 +1027,9 @@ class moodle1_file_manager { /** @var int user id */ public $userid; + /** @var string the root of the converter temp directory */ + protected $basepath; + /** @var textlib instance used during the migration */ protected $textlib; @@ -1044,30 +1047,34 @@ class moodle1_file_manager { * @param int $userid initial user id of the files being migrated */ public function __construct(moodle1_converter $converter, $contextid = null, $component = null, $filearea = null, $itemid = 0, $userid = null) { - + // set the initial destination of the migrated files $this->converter = $converter; $this->contextid = $contextid; $this->component = $component; $this->filearea = $filearea; $this->itemid = $itemid; $this->userid = $userid; + // set other useful bits + $this->basepath = $converter->get_tempdir_path(); $this->textlib = textlib_get_instance(); } /** * Migrates one given file stored on disk * - * @param string $sourcefullpath the full path to the source local file + * @param string $sourcepath the path to the source local file within the backup archive {@example 'moddata/foobar/file.ext'} * @param string $filepath the file path of the migrated file, defaults to the root directory '/' * @param string $filename the name of the migrated file, defaults to the same as the source file has * @param int $timecreated override the timestamp of when the migrated file should appear as created * @param int $timemodified override the timestamp of when the migrated file should appear as modified * @return int id of the migrated file */ - public function migrate_file($sourcefullpath, $filepath = '/', $filename = null, $timecreated = null, $timemodified = null) { + public function migrate_file($sourcepath, $filepath = '/', $filename = null, $timecreated = null, $timemodified = null) { + + $sourcefullpath = $this->basepath.'/'.$sourcepath; if (!is_readable($sourcefullpath)) { - throw new moodle1_convert_exception('file_not_readable'); + throw new moodle1_convert_exception('file_not_readable', $sourcefullpath); } $filepath = clean_param($filepath, PARAM_PATH); @@ -1111,7 +1118,7 @@ class moodle1_file_manager { /** * Migrates all files in the given directory * - * @param string $rootpath full path to the root directory containing the files (like course_files) + * @param string $rootpath path within the backup archive to the root directory containing the files {@example 'course_files'} * @param string $relpath relative path used during the recursion - do not provide when calling this! * @return array ids of the migrated files */ @@ -1124,8 +1131,7 @@ class moodle1_file_manager { $this->stash_file($filerecord); $fileids[] = $filerecord['id']; - $fullpath = $rootpath.$relpath; - $items = new DirectoryIterator($fullpath); + $items = new DirectoryIterator($this->basepath.'/'.$rootpath.$relpath); foreach ($items as $item) { @@ -1138,7 +1144,8 @@ class moodle1_file_manager { } if ($item->isFile()) { - $fileids[] = $this->migrate_file($item->getPathname(), $relpath, $item->getFilename(), $item->getCTime(), $item->getMTime()); + $fileids[] = $this->migrate_file(substr($item->getPathname(), strlen($this->basepath.'/')), + $relpath, $item->getFilename(), $item->getCTime(), $item->getMTime()); } else { $dirname = clean_param($item->getFilename(), PARAM_PATH); diff --git a/backup/converter/moodle1/simpletest/testlib.php b/backup/converter/moodle1/simpletest/testlib.php index 7d88b7ba5e0..03e21feb1d0 100644 --- a/backup/converter/moodle1/simpletest/testlib.php +++ b/backup/converter/moodle1/simpletest/testlib.php @@ -40,6 +40,7 @@ class moodle1_converter_test extends UnitTestCase { $this->tempdir = convert_helper::generate_id('simpletest'); check_dir_exists("$CFG->dataroot/temp/backup/$this->tempdir/course_files/sub1"); + check_dir_exists("$CFG->dataroot/temp/backup/$this->tempdir/moddata/unittest/4/7"); copy( "$CFG->dirroot/backup/converter/moodle1/simpletest/files/moodle.xml", "$CFG->dataroot/temp/backup/$this->tempdir/moodle.xml" @@ -52,6 +53,18 @@ class moodle1_converter_test extends UnitTestCase { "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", "$CFG->dataroot/temp/backup/$this->tempdir/course_files/sub1/file2.gif" ); + copy( + "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", + "$CFG->dataroot/temp/backup/$this->tempdir/moddata/unittest/4/file1.gif" + ); + copy( + "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", + "$CFG->dataroot/temp/backup/$this->tempdir/moddata/unittest/4/icon.gif" + ); + copy( + "$CFG->dirroot/backup/converter/moodle1/simpletest/files/icon.gif", + "$CFG->dataroot/temp/backup/$this->tempdir/moddata/unittest/4/7/icon.gif" + ); } public function tearDown() { @@ -182,6 +195,43 @@ class moodle1_converter_test extends UnitTestCase { $this->assertTrue($id2 < $id3); } + public function test_migrate_file() { + // set-up the file manager + $converter = convert_factory::get_converter('moodle1', $this->tempdir); + $converter->create_stash_storage(); + $contextid = $converter->get_contextid(CONTEXT_MODULE, 32); + $fileman = $converter->get_file_manager($contextid, 'mod_unittest', 'testarea'); + // this fileman has not converted anything yet + $fileids = $fileman->get_fileids(); + $this->assertIsA($fileids, 'array'); + $this->assertEqual(0, count($fileids)); + // migrate a single file + $fileman->itemid = 4; + $fileman->migrate_file('moddata/unittest/4/icon.gif'); + $this->assertTrue(is_file($converter->get_workdir_path().'/files/4e/4ea114b0558f53e3af8dd9afc0e0810a95c2a724')); + // get the file id + $fileids = $fileman->get_fileids(); + $this->assertIsA($fileids, 'array'); + $this->assertEqual(1, count($fileids)); + // migrate another single file into another file area + $fileman->filearea = 'anotherarea'; + $fileman->itemid = 7; + $fileman->migrate_file('moddata/unittest/4/7/icon.gif', '/', 'renamed.gif'); + // get the file records + $filerecordids = $converter->get_stash_itemids('files'); + foreach ($filerecordids as $filerecordid) { + $filerecord = $converter->get_stash('files', $filerecordid); + $this->assertEqual('4ea114b0558f53e3af8dd9afc0e0810a95c2a724', $filerecord['contenthash']); + $this->assertEqual($contextid, $filerecord['contextid']); + $this->assertEqual('mod_unittest', $filerecord['component']); + if ($filerecord['filearea'] === 'testarea') { + $this->assertEqual(4, $filerecord['itemid']); + $this->assertEqual('icon.gif', $filerecord['filename']); + } + } + $converter->drop_stash_storage(); + } + public function test_convert_run_convert() { $converter = convert_factory::get_converter('moodle1', $this->tempdir); $converter->convert();