Merge branch 'MDL-71198-master' of https://github.com/junpataleta/moodle

This commit is contained in:
Jun Pataleta 2022-05-26 10:23:27 +08:00
commit 2814436c44
6 changed files with 153 additions and 14 deletions

View File

@ -1019,21 +1019,26 @@ abstract class restore_dbops {
continue;
}
// Updated the times of the new record.
// The file record should reflect when the file entered the system,
// and when this record was created.
$time = time();
// The file record to restore.
$file_record = array(
'contextid' => $newcontextid,
'component' => $component,
'filearea' => $filearea,
'itemid' => $rec->newitemid,
'filepath' => $file->filepath,
'filename' => $file->filename,
'timecreated' => $file->timecreated,
'timemodified'=> $file->timemodified,
'userid' => $mappeduserid,
'source' => $file->source,
'author' => $file->author,
'license' => $file->license,
'sortorder' => $file->sortorder
'contextid' => $newcontextid,
'component' => $component,
'filearea' => $filearea,
'itemid' => $rec->newitemid,
'filepath' => $file->filepath,
'filename' => $file->filename,
'timecreated' => $time,
'timemodified' => $time,
'userid' => $mappeduserid,
'source' => $file->source,
'author' => $file->author,
'license' => $file->license,
'sortorder' => $file->sortorder
);
if (empty($file->repositoryid)) {

View File

@ -0,0 +1,38 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core\task;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/db/upgradelib.php');
/**
* Retroactively fixes file timestamps that are older than the containing folder record.
*
* @package core
* @author Peter Burnett <peterburnett@catalyst-au.net>
* @copyright Catalyst IT, 2021
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class fix_file_timestamps_task extends adhoc_task {
/**
* Run the adhoc task and fix the file timestamps.
*/
public function execute() {
upgrade_fix_file_timestamps();
}
}

View File

@ -4506,5 +4506,14 @@ privatefiles,moodle|/user/files.php';
upgrade_main_savepoint(true, 2022051000.00);
}
if ($oldversion < 2022052500.00) {
// Start an adhoc task to fix the file timestamps of restored files.
$task = new core\task\fix_file_timestamps_task();
\core\task\manager::queue_adhoc_task($task);
// Main savepoint reached.
upgrade_main_savepoint(true, 2022052500.00);
}
return true;
}

View File

@ -1597,3 +1597,38 @@ function upgrade_block_set_my_user_parent_context(
$dbman->drop_table($xmldbtable);
}
/**
* Fix the timestamps for files where their timestamps are older
* than the directory listing that they are contained in.
*/
function upgrade_fix_file_timestamps() {
global $DB;
// Due to incompatability in SQL syntax for updates with joins,
// These will be updated in a select + separate update.
$sql = "SELECT f.id, f2.timecreated
FROM {files} f
JOIN {files} f2
ON f2.contextid = f.contextid
AND f2.filepath = f.filepath
AND f2.component = f.component
AND f2.filearea = f.filearea
AND f2.itemid = f.itemid
AND f2.filename = '.'
WHERE f2.timecreated > f.timecreated";
$recordset = $DB->get_recordset_sql($sql);
if (!$recordset->valid()) {
$recordset->close();
return;
}
foreach ($recordset as $record) {
$record->timemodified = $record->timecreated;
$DB->update_record('files', $record);
}
$recordset->close();
}

View File

@ -1671,4 +1671,56 @@ calendar,core_calendar|/calendar/view.php?view=month',
$this->assertEquals($expectedmenu, $newcustomusermenu);
}
/**
* Test that file timestamps are corrected for copied files.
*/
public function test_upgrade_fix_file_timestamps() {
global $DB;
$this->resetAfterTest();
// Add 2 files for testing, one with edited old timestamps.
$origtime = time();
$new = [
'contextid' => 123,
'component' => 'mod_label',
'filearea' => 'intro',
'itemid' => 0,
'filepath' => '/',
'filename' => 'file.txt',
];
$old = [
'contextid' => 321,
'component' => 'mod_label',
'filearea' => 'intro',
'itemid' => 0,
'filepath' => '/',
'filename' => 'file.txt',
];
// Create the file records. This will create a directory listing with the current time.
$fs = get_file_storage();
$newfile = $fs->create_file_from_string($new, 'new');
$oldfile = $fs->create_file_from_string($old, 'old');
// Manually set the timestamps to use on files.
$DB->set_field('files', 'timecreated', $origtime, ['id' => $newfile->get_id()]);
$DB->set_field('files', 'timemodified', $origtime, ['id' => $newfile->get_id()]);
$DB->set_field('files', 'timecreated', 1, ['id' => $oldfile->get_id()]);
$DB->set_field('files', 'timemodified', 1, ['id' => $oldfile->get_id()]);
upgrade_fix_file_timestamps();
// Check nothing changed on the new file.
$updatednew = $DB->get_record('files', ['id' => $newfile->get_id()]);
$this->assertEquals($origtime, $updatednew->timecreated);
$this->assertEquals($origtime, $updatednew->timemodified);
// Confirm that the file with old timestamps has been fixed.
$updatedold = $DB->get_record('files', ['id' => $oldfile->get_id()]);
$this->assertNotEquals(1, $updatedold->timecreated);
$this->assertNotEquals(1, $updatedold->timemodified);
$this->assertTrue($updatedold->timecreated >= $origtime);
$this->assertTrue($updatedold->timemodified >= $origtime);
}
}

View File

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2022051900.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2022052500.00; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.1dev (Build: 20220519)'; // Human-friendly version name