Skip to content

Commit

Permalink
Merge branch 'MDL-62251-master' of git://github.com/rezaies/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed May 13, 2018
2 parents 7bdb9d8 + 8614257 commit 28ec28e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 12 deletions.
8 changes: 5 additions & 3 deletions backup/converter/convertlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,17 @@ protected function create_workdir() {
protected function replace_tempdir() {
global $CFG;

$tempdir = $this->get_tempdir_path();

if (empty($CFG->keeptempdirectoriesonbackup)) {
fulldelete($this->get_tempdir_path());
fulldelete($tempdir);
} else {
if (!rename($this->get_tempdir_path(), $this->get_tempdir_path() . '_' . $this->get_name() . '_' . $this->id . '_source')) {
if (!rename($tempdir, $tempdir . '_' . $this->get_name() . '_' . $this->id . '_source')) {
throw new convert_exception('failed_rename_source_tempdir');
}
}

if (!rename($this->get_workdir_path(), $this->get_tempdir_path())) {
if (!rename($this->get_workdir_path(), $tempdir)) {
throw new convert_exception('failed_move_converted_into_place');
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/classes/task/file_temp_cleanup_task.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function execute() {
$this->execute_on($CFG->tempdir);

// Run on $CFG->backuptempdir too, if different from the default one, '$CFG->tempdir/backup'.
if (realpath(dirname($CFG->backuptempdir)) !== $CFG->tempdir) {
if (realpath(dirname($CFG->backuptempdir)) !== realpath($CFG->tempdir)) {
// The $CFG->backuptempdir setting is different from the default '$CFG->tempdir/backup'.
$this->execute_on($CFG->backuptempdir);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/tests/cronlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ public function test_cron_delete_from_temp($nodes, $expected) {
$isvalid = true;
$isvalid = $isvalid && !$iter->isDot();
// Remove the default $CFG->tempdir/backup directory and $CFG->tempdir/.htaccess file from this comparison.
$isvalid = $isvalid && !($iter->isDir() && ($iter->getRealpath() === "{$tmpdir}/backup"));
$isvalid = $isvalid && !($iter->isFile() && ($iter->getRealpath() === "{$tmpdir}/.htaccess"));
$isvalid = $isvalid && !($iter->isDir() && ($iter->getRealpath() === $tmpdir . DIRECTORY_SEPARATOR . 'backup'));
$isvalid = $isvalid && !($iter->isFile() && ($iter->getRealpath() === $tmpdir . DIRECTORY_SEPARATOR . '.htaccess'));
if ($isvalid) {
$actual[] = $iter->getRealPath();
}
Expand Down
29 changes: 26 additions & 3 deletions privacy/classes/local/request/moodle_content_writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function export_custom_file(array $subcontext, $filename, $filecontent) :
* @return string The processed string
*/
public function rewrite_pluginfile_urls(array $subcontext, $component, $filearea, $itemid, $text) : string {
return str_replace('@@PLUGINFILE@@/', $this->get_files_target_path($component, $filearea, $itemid).'/', $text);
return str_replace('@@PLUGINFILE@@/', $this->get_files_target_url($component, $filearea, $itemid).'/', $text);
}

/**
Expand Down Expand Up @@ -272,7 +272,9 @@ protected function get_path(array $subcontext, string $name) : string {
// Join the directory together with the name.
$filepath = implode(DIRECTORY_SEPARATOR, $path) . DIRECTORY_SEPARATOR . $name;

return preg_replace('@' . DIRECTORY_SEPARATOR . '+@', DIRECTORY_SEPARATOR, $filepath);
// To use backslash, it must be doubled ("\\\\" PHP string).
$separator = str_replace('\\', '\\\\', DIRECTORY_SEPARATOR);
return preg_replace('@(' . $separator . '|/)+@', $separator, $filepath);
}

/**
Expand All @@ -291,7 +293,9 @@ protected function get_full_path(array $subcontext, string $name) : string {
// Join the directory together with the name.
$filepath = implode(DIRECTORY_SEPARATOR, $path);

return preg_replace('@' . DIRECTORY_SEPARATOR . '+@', DIRECTORY_SEPARATOR, $filepath);
// To use backslash, it must be doubled ("\\\\" PHP string).
$separator = str_replace('\\', '\\\\', DIRECTORY_SEPARATOR);
return preg_replace('@(' . $separator . '|/)+@', $separator, $filepath);
}

/**
Expand All @@ -314,6 +318,25 @@ protected function get_files_target_path($component, $filearea, $itemid) : strin
return implode(DIRECTORY_SEPARATOR, $parts);
}

/**
* Get a relative url to the directory of the exported files within a subcontext.
*
* @param string $component The name of the component that the files belong to.
* @param string $filearea The filearea within that component.
* @param string $itemid Which item those files belong to.
* @return string The url
*/
protected function get_files_target_url($component, $filearea, $itemid) : string {
// We do not need to include the component because we organise things by context.
$parts = ['_files', $filearea];

if (!empty($itemid)) {
$parts[] = $itemid;
}

return implode('/', $parts);
}

/**
* Write the data to the specified path.
*
Expand Down
5 changes: 4 additions & 1 deletion privacy/classes/tests/request/content_writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,10 @@ public function export_area_files(array $subcontext, $component, $filearea, $ite
*/
public function export_file(array $subcontext, \stored_file $file) : \core_privacy\local\request\content_writer {
if (!$file->is_directory()) {
$filepath = explode(DIRECTORY_SEPARATOR, $file->get_filepath());
$filepath = $file->get_filepath();
// Directory separator in the stored_file class should always be '/'. The following line is just a fail safe.
$filepath = str_replace(DIRECTORY_SEPARATOR, '/', $filepath);
$filepath = explode('/', $filepath);
$filepath[] = $file->get_filename();
$filepath = array_filter($filepath);
$filepath = implode('/', $filepath);
Expand Down
10 changes: 8 additions & 2 deletions privacy/tests/moodle_content_writer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -1162,12 +1162,18 @@ protected function get_context_path($context, $subcontext = null, $name = '') {
if (null === $subcontext) {
$rcm = $rc->getMethod('get_context_path');
$rcm->setAccessible(true);
return $rcm->invoke($writer);
$path = $rcm->invoke($writer);
} else {
$rcm = $rc->getMethod('get_path');
$rcm->setAccessible(true);
return $rcm->invoke($writer, $subcontext, $name);
$path = $rcm->invoke($writer, $subcontext, $name);
}

// PHPUnit uses mikey179/vfsStream which is a stream wrapper for a virtual file system that uses '/'
// as the directory separator.
$path = str_replace(DIRECTORY_SEPARATOR, '/', $path);

return $path;
}

/**
Expand Down

0 comments on commit 28ec28e

Please sign in to comment.