Skip to content

Commit

Permalink
MDL-34290, MDL-33416 prepare to deprecate fields files.referencelasts…
Browse files Browse the repository at this point in the history
…ync and referencelifetime
  • Loading branch information
marinaglancy committed Aug 29, 2012
1 parent 437f5dc commit 42aa6e1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
32 changes: 13 additions & 19 deletions lib/filestorage/file_storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -976,10 +976,6 @@ public function create_file_from_pathname($filerecord, $pathname) {
$filerecord->sortorder = 0;
}

$filerecord->referencefileid = !isset($filerecord->referencefileid) ? 0 : $filerecord->referencefileid;
$filerecord->referencelastsync = !isset($filerecord->referencelastsync) ? 0 : $filerecord->referencelastsync;
$filerecord->referencelifetime = !isset($filerecord->referencelifetime) ? 0 : $filerecord->referencelifetime;

$filerecord->filepath = clean_param($filerecord->filepath, PARAM_PATH);
if (strpos($filerecord->filepath, '/') !== 0 or strrpos($filerecord->filepath, '/') !== strlen($filerecord->filepath)-1) {
// path must start and end with '/'
Expand Down Expand Up @@ -1094,9 +1090,6 @@ public function create_file_from_string($filerecord, $content) {
} else {
$filerecord->sortorder = 0;
}
$filerecord->referencefileid = !isset($filerecord->referencefileid) ? 0 : $filerecord->referencefileid;
$filerecord->referencelastsync = !isset($filerecord->referencelastsync) ? 0 : $filerecord->referencelastsync;
$filerecord->referencelifetime = !isset($filerecord->referencelifetime) ? 0 : $filerecord->referencelifetime;

$filerecord->filepath = clean_param($filerecord->filepath, PARAM_PATH);
if (strpos($filerecord->filepath, '/') !== 0 or strrpos($filerecord->filepath, '/') !== strlen($filerecord->filepath)-1) {
Expand Down Expand Up @@ -1217,9 +1210,10 @@ public function create_file_from_reference($filerecord, $repositoryid, $referenc
$filerecord->sortorder = 0;
}

$filerecord->referencefileid = empty($filerecord->referencefileid) ? 0 : $filerecord->referencefileid;
$filerecord->referencelastsync = empty($filerecord->referencelastsync) ? 0 : $filerecord->referencelastsync;
$filerecord->referencelifetime = empty($filerecord->referencelifetime) ? 0 : $filerecord->referencelifetime;
// TODO MDL-33416 [2.4] fields referencelastsync and referencelifetime to be removed from {files} table completely
unset($filerecord->referencelastsync);
unset($filerecord->referencelifetime);

$filerecord->mimetype = empty($filerecord->mimetype) ? $this->mimetype($filerecord->filename) : $filerecord->mimetype;
$filerecord->userid = empty($filerecord->userid) ? null : $filerecord->userid;
$filerecord->source = empty($filerecord->source) ? null : $filerecord->source;
Expand Down Expand Up @@ -1309,10 +1303,8 @@ public function create_file_from_reference($filerecord, $repositoryid, $referenc

$transaction->allow_commit();

// Adding repositoryid and reference to file record to create stored_file instance
$filerecord->repositoryid = $repositoryid;
$filerecord->reference = $reference;
return $this->get_file_instance($filerecord);
// this will retrieve all reference information from DB as well
return $this->get_file_by_id($filerecord->id);
}

/**
Expand Down Expand Up @@ -1973,10 +1965,12 @@ private static function instance_sql_fields($filesprefix, $filesreferenceprefix)
// else problems like MDL-33172 occur.
$filefields = array('contenthash', 'pathnamehash', 'contextid', 'component', 'filearea',
'itemid', 'filepath', 'filename', 'userid', 'filesize', 'mimetype', 'status', 'source',
'author', 'license', 'timecreated', 'timemodified', 'sortorder', 'referencefileid',
'referencelastsync', 'referencelifetime');
'author', 'license', 'timecreated', 'timemodified', 'sortorder', 'referencefileid');

$referencefields = array('repositoryid', 'reference');
$referencefields = array('repositoryid' => 'repositoryid',
'reference' => 'reference',
'lastsync' => 'referencelastsync',
'lifetime' => 'referencelifetime');

// id is specifically named to prevent overlaping between the two tables.
$fields = array();
Expand All @@ -1985,8 +1979,8 @@ private static function instance_sql_fields($filesprefix, $filesreferenceprefix)
$fields[] = "{$filesprefix}.{$field}";
}

foreach ($referencefields as $field) {
$fields[] = "{$filesreferenceprefix}.{$field}";
foreach ($referencefields as $field => $alias) {
$fields[] = "{$filesreferenceprefix}.{$field} AS {$alias}";
}

return implode(', ', $fields);
Expand Down
16 changes: 13 additions & 3 deletions lib/filestorage/stored_file.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ public function __construct(file_storage $fs, stdClass $file_record, $filedir) {
} else {
$this->repository = null;
}
// make sure all reference fields exist in file_record even when it is not a reference
foreach (array('referencelastsync', 'referencelifetime', 'referencefileid', 'reference', 'repositoryid') as $key) {
if (empty($this->file_record->$key)) {
$this->file_record->$key = null;
}
}
}

/**
Expand Down Expand Up @@ -142,12 +148,18 @@ protected function update($dataobject) {
}
}

if ($field === 'referencefileid' or $field === 'referencelastsync' or $field === 'referencelifetime') {
if ($field === 'referencefileid') {
if (!is_null($value) and !is_number($value)) {
throw new file_exception('storedfileproblem', 'Invalid reference info');
}
}

if ($field === 'referencelastsync' or $field === 'referencelifetime') {
// do not update those fields
// TODO MDL-33416 [2.4] fields referencelastsync and referencelifetime to be removed from {files} table completely
continue;
}

// adding the field
$this->file_record->$field = $value;
} else {
Expand Down Expand Up @@ -226,8 +238,6 @@ public function delete_reference() {
// Update the underlying record in the database.
$update = new stdClass();
$update->referencefileid = null;
$update->referencelastsync = null;
$update->referencelifetime = null;
$this->update($update);

$transaction->allow_commit();
Expand Down

0 comments on commit 42aa6e1

Please sign in to comment.