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.
This commit is contained in:
David Mudrak 2011-06-04 19:58:22 +02:00
parent 93dd33b405
commit aa97e0dd52
3 changed files with 15 additions and 6 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}