Skip to content

Commit

Permalink
MDL-45616 repositories: more clearly distinguish when we use source a…
Browse files Browse the repository at this point in the history
…nd when reference

Function repository::get_moodle_file() should always be called on packed reference and not on the source received from user.
Also added phpdocs to some other methods that were confusing source and reference
  • Loading branch information
marinaglancy authored and Damyon Wiese committed Jul 7, 2014
1 parent a5abafc commit cb2b42a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion repository/filepicker.php
Expand Up @@ -293,7 +293,7 @@
// note that in this case user may not have permission to access the source file directly
// so no file_browser/file_info can be used below
if ($repo->has_moodle_files()) {
$file = repository::get_moodle_file($fileurl);
$file = repository::get_moodle_file($reference);
if ($file && $file->is_external_file()) {
$sourcefield = $file->get_source(); // remember the original source
$record->source = $repo::build_source_field($sourcefield);
Expand Down
23 changes: 13 additions & 10 deletions repository/lib.php
Expand Up @@ -717,13 +717,14 @@ public static function draftfile_exists($itemid, $filepath, $filename) {
}

/**
* Parses the 'source' returned by moodle repositories and returns an instance of stored_file
* Parses the moodle file reference and returns an instance of stored_file
*
* @param string $source
* @param string $reference reference to the moodle internal file as retruned by
* {@link repository::get_file_reference()} or {@link file_storage::pack_reference()}
* @return stored_file|null
*/
public static function get_moodle_file($source) {
$params = file_storage::unpack_reference($source, true);
public static function get_moodle_file($reference) {
$params = file_storage::unpack_reference($reference, true);
$fs = get_file_storage();
return $fs->get_file($params['contextid'], $params['component'], $params['filearea'],
$params['itemid'], $params['filepath'], $params['filename']);
Expand All @@ -735,13 +736,14 @@ public static function get_moodle_file($source) {
* This is checked when user tries to pick the file from repository to deal with
* potential parameter substitutions is request
*
* @param string $source
* @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
* @return bool whether the file is accessible by current user
*/
public function file_is_accessible($source) {
if ($this->has_moodle_files()) {
$reference = $this->get_file_reference($source);
try {
$params = file_storage::unpack_reference($source, true);
$params = file_storage::unpack_reference($reference, true);
} catch (file_reference_exception $e) {
return false;
}
Expand Down Expand Up @@ -1336,12 +1338,13 @@ public function get_file_by_reference($reference) {
* again to another file area (also as a copy or as a reference), the value of
* files.source is copied.
*
* @param string $source the value that repository returned in listing as 'source'
* @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
* @return string|null
*/
public function get_file_source_info($source) {
if ($this->has_moodle_files()) {
return $this->get_reference_details($source, 0);
$reference = $this->get_file_reference($source);
return $this->get_reference_details($reference, 0);
}
return $source;
}
Expand Down Expand Up @@ -1621,11 +1624,11 @@ public static function display_instances_list($context, $typename = null) {
/**
* Prepare file reference information
*
* @param string $source
* @param string $source source of the file, returned by repository as 'source' and received back from user (not cleaned)
* @return string file referece
*/
public function get_file_reference($source) {
if ($this->has_moodle_files() && ($this->supported_returntypes() & FILE_REFERENCE)) {
if ($source && $this->has_moodle_files()) {
$params = file_storage::unpack_reference($source);
if (!is_array($params)) {
throw new repository_exception('invalidparams', 'repository');
Expand Down
2 changes: 1 addition & 1 deletion repository/repository_ajax.php
Expand Up @@ -239,7 +239,7 @@
// note that in this case user may not have permission to access the source file directly
// so no file_browser/file_info can be used below
if ($repo->has_moodle_files()) {
$file = repository::get_moodle_file($source);
$file = repository::get_moodle_file($reference);
if ($file && $file->is_external_file()) {
$sourcefield = $file->get_source(); // remember the original source
$record->source = $repo::build_source_field($sourcefield);
Expand Down

0 comments on commit cb2b42a

Please sign in to comment.