From 437f5dc4cf0e3c4a4614aeaf298aac4ab0f8284a Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Thu, 9 Aug 2012 13:50:42 +0800 Subject: [PATCH] MDL-34290 Auto synchronise newly created references when possible --- lib/filestorage/file_storage.php | 29 +++++++++++++++++++++++------ repository/repository_ajax.php | 9 +++++++-- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/filestorage/file_storage.php b/lib/filestorage/file_storage.php index 71c8fc8389a13..b18105b18debb 100644 --- a/lib/filestorage/file_storage.php +++ b/lib/filestorage/file_storage.php @@ -1266,22 +1266,39 @@ public function create_file_from_reference($filerecord, $repositoryid, $referenc $transaction = $DB->start_delegated_transaction(); try { - $filerecord->referencefileid = $this->get_or_create_referencefileid($repositoryid, $reference, - $filerecord->referencelastsync, $filerecord->referencelifetime); + $filerecord->referencefileid = $this->get_or_create_referencefileid($repositoryid, $reference); } catch (Exception $e) { throw new file_reference_exception($repositoryid, $reference, null, null, $e->getMessage()); } - // External file doesn't have content in moodle. - // So we create an empty file for it. - list($filerecord->contenthash, $filerecord->filesize, $newfile) = $this->add_string_to_pool(null); + if (isset($filerecord->contenthash) && $this->content_exists($filerecord->contenthash)) { + // there was specified the contenthash for a file already stored in moodle filepool + if (empty($filerecord->filesize)) { + $filepathname = $this->path_from_hash($filerecord->contenthash) . '/' . $filerecord->contenthash; + $filerecord->filesize = filesize($filepathname); + } else { + $filerecord->filesize = clean_param($filerecord->filesize, PARAM_INT); + } + } else { + // atempt to get the result of last synchronisation for this reference + $lastcontent = $DB->get_record('files', array('referencefileid' => $filerecord->referencefileid), + 'id, contenthash, filesize', IGNORE_MULTIPLE); + if ($lastcontent) { + $filerecord->contenthash = $lastcontent->contenthash; + $filerecord->filesize = $lastcontent->filesize; + } else { + // External file doesn't have content in moodle. + // So we create an empty file for it. + list($filerecord->contenthash, $filerecord->filesize, $newfile) = $this->add_string_to_pool(null); + } + } $filerecord->pathnamehash = $this->get_pathname_hash($filerecord->contextid, $filerecord->component, $filerecord->filearea, $filerecord->itemid, $filerecord->filepath, $filerecord->filename); try { $filerecord->id = $DB->insert_record('files', $filerecord); } catch (dml_exception $e) { - if ($newfile) { + if (!empty($newfile)) { $this->deleted_file_cleanup($filerecord->contenthash); } throw new stored_file_creation_exception($filerecord->contextid, $filerecord->component, $filerecord->filearea, $filerecord->itemid, diff --git a/repository/repository_ajax.php b/repository/repository_ajax.php index 8c9fc7faf6d6e..17d679c00348d 100644 --- a/repository/repository_ajax.php +++ b/repository/repository_ajax.php @@ -234,6 +234,8 @@ if ($file && $file->is_external_file()) { $sourcefield = $file->get_source(); // remember the original source $record->source = $repo::build_source_field($sourcefield); + $record->contenthash = $file->get_contenthash(); + $record->filesize = $file->get_filesize(); $reference = $file->get_reference(); $repo_id = $file->get_repository_id(); $repo = repository::get_repository_by_id($repo_id, $contextid, $repooptions); @@ -241,8 +243,11 @@ } if ($usefilereference) { - // get reference life time from repo - $record->referencelifetime = $repo->get_reference_file_lifetime($reference); + if ($repo->has_moodle_files()) { + $sourcefile = repository::get_moodle_file($reference); + $record->contenthash = $sourcefile->get_contenthash(); + $record->filesize = $sourcefile->get_filesize(); + } // Check if file exists. if (repository::draftfile_exists($itemid, $saveas_path, $saveas_filename)) { // File name being used, rename it.