From aa97e0dd528427123076e6f1f426fa7e9ab26f8e Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Sat, 4 Jun 2011 19:58:22 +0200 Subject: [PATCH] MDL-22414 File manager allows to set explicit sortorder and accepts filepaths not ending with a slash We want to set migrated content file in the File module as a main file. Main files are files with sortorder set to 1. Therefore the file manager must provide a way to set the sortorder property of the new file record. The second change allowing filepaths not ending with slash is a helper as we can easily use dirname() as a value (see the usage in the next commits). Not providing the dirname() was a bug as the reference to a file in the HTML text did not respect the migrated file path. --- backup/converter/moodle1/lib.php | 15 ++++++++++++--- mod/folder/backup/moodle1/lib.php | 2 +- mod/page/backup/moodle1/lib.php | 4 ++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/backup/converter/moodle1/lib.php b/backup/converter/moodle1/lib.php index 09259f16abb..bc3247b579f 100644 --- a/backup/converter/moodle1/lib.php +++ b/backup/converter/moodle1/lib.php @@ -1113,13 +1113,14 @@ class moodle1_file_manager { * Migrates one given file stored on disk * * @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 $filepath the file path of the migrated file, defaults to the root directory '/' {@example '/sub/dir/'} * @param string $filename the name of the migrated file, defaults to the same as the source file has + * @param int $sortorder the sortorder of the file (main files have sortorder set to 1) * @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($sourcepath, $filepath = '/', $filename = null, $timecreated = null, $timemodified = null) { + public function migrate_file($sourcepath, $filepath = '/', $filename = null, $sortorder = 0, $timecreated = null, $timemodified = null) { $sourcefullpath = $this->basepath.'/'.$sourcepath; @@ -1127,6 +1128,13 @@ class moodle1_file_manager { throw new moodle1_convert_exception('file_not_readable', $sourcefullpath); } + // sanitize filepath + if (empty($filepath)) { + $filepath = '/'; + } + if (substr($filepath, -1) !== '/') { + $filepath .= '/'; + } $filepath = clean_param($filepath, PARAM_PATH); if ($this->textlib->strlen($filepath) > 255) { @@ -1154,6 +1162,7 @@ class moodle1_file_manager { $filerecord = $this->make_file_record(array( 'filepath' => $filepath, 'filename' => $filename, + 'sortorder' => $sortorder, 'mimetype' => mimeinfo('type', $sourcefullpath), 'timecreated' => $timecreated, 'timemodified' => $timemodified, @@ -1199,7 +1208,7 @@ class moodle1_file_manager { if ($item->isFile()) { $fileids[] = $this->migrate_file(substr($item->getPathname(), strlen($this->basepath.'/')), - $relpath, $item->getFilename(), $item->getCTime(), $item->getMTime()); + $relpath, $item->getFilename(), 0, $item->getCTime(), $item->getMTime()); } else { $dirname = clean_param($item->getFilename(), PARAM_PATH); diff --git a/mod/folder/backup/moodle1/lib.php b/mod/folder/backup/moodle1/lib.php index 5f7d38adb78..4ca4db4b482 100644 --- a/mod/folder/backup/moodle1/lib.php +++ b/mod/folder/backup/moodle1/lib.php @@ -63,7 +63,7 @@ class moodle1_mod_folder_handler extends moodle1_resource_successor_handler { $this->fileman->filearea = 'intro'; $this->fileman->itemid = 0; foreach ($files as $file) { - $this->fileman->migrate_file('course_files'.$file); + $this->fileman->migrate_file('course_files'.$file, dirname($file)); } $folder['intro'] = moodle1_converter::rewrite_filephp_usage($folder['intro'], $files); } diff --git a/mod/page/backup/moodle1/lib.php b/mod/page/backup/moodle1/lib.php index 5d4ea701426..9951057397c 100644 --- a/mod/page/backup/moodle1/lib.php +++ b/mod/page/backup/moodle1/lib.php @@ -91,7 +91,7 @@ class moodle1_mod_page_handler extends moodle1_resource_successor_handler { $this->fileman->filearea = 'intro'; $this->fileman->itemid = 0; foreach ($files as $file) { - $this->fileman->migrate_file('course_files'.$file); + $this->fileman->migrate_file('course_files'.$file, dirname($file)); } $page['intro'] = moodle1_converter::rewrite_filephp_usage($page['intro'], $files); } @@ -102,7 +102,7 @@ class moodle1_mod_page_handler extends moodle1_resource_successor_handler { $this->fileman->filearea = 'content'; $this->fileman->itemid = 0; foreach ($files as $file) { - $this->fileman->migrate_file('course_files'.$file); + $this->fileman->migrate_file('course_files'.$file, dirname($file)); } $page['content'] = moodle1_converter::rewrite_filephp_usage($page['content'], $files); }