From e3c0211879509f87593a5b6c91907d4ae0019321 Mon Sep 17 00:00:00 2001 From: Dongsheng Cai Date: Tue, 22 May 2012 13:51:04 +0800 Subject: [PATCH] MDL-33147 Added transaction support for some Files APIs --- lib/filestorage/file_storage.php | 4 ++++ lib/filestorage/stored_file.php | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/filestorage/file_storage.php b/lib/filestorage/file_storage.php index 149936b8079..d7124a863cb 100644 --- a/lib/filestorage/file_storage.php +++ b/lib/filestorage/file_storage.php @@ -1260,6 +1260,8 @@ class file_storage { $filerecord->timemodified = $now; } + $transaction = $DB->start_delegated_transaction(); + // Insert file reference record. try { $referencerecord = new stdClass; @@ -1292,6 +1294,8 @@ class file_storage { $this->create_directory($filerecord->contextid, $filerecord->component, $filerecord->filearea, $filerecord->itemid, $filerecord->filepath, $filerecord->userid); + $transaction->allow_commit(); + // Adding repositoryid and reference to file record to create stored_file instance $filerecord->repositoryid = $repositoryid; $filerecord->reference = $reference; diff --git a/lib/filestorage/stored_file.php b/lib/filestorage/stored_file.php index 6423d6b3e45..fb5fa2304c9 100644 --- a/lib/filestorage/stored_file.php +++ b/lib/filestorage/stored_file.php @@ -206,6 +206,8 @@ class stored_file { // Remove repository info. $this->repository = null; + $transaction = $DB->start_delegated_transaction(); + // Remove reference info from DB. $DB->delete_records('files_reference', array('id'=>$this->file_record->referencefileid)); @@ -217,6 +219,8 @@ class stored_file { $filerecord->referencefileid = null; $this->update($filerecord); + $transaction->allow_commit(); + // unset object variable unset($this->file_record->repositoryid); unset($this->file_record->reference); @@ -248,6 +252,9 @@ class stored_file { */ public function delete() { global $DB; + + $transaction = $DB->start_delegated_transaction(); + // If other files referring to this file, we need convert them if ($files = $this->fs->get_references_by_storedfile($this)) { foreach ($files as $file) { @@ -257,6 +264,9 @@ class stored_file { // Now delete file records in DB $DB->delete_records('files', array('id'=>$this->file_record->id)); $DB->delete_records('files_reference', array('id'=>$this->file_record->referencefileid)); + + $transaction->allow_commit(); + // moves pool file to trash if content not needed any more $this->fs->deleted_file_cleanup($this->file_record->contenthash); return true; // BC only