Commit d5595674 authored by Robert Lyon's avatar Robert Lyon

Bug 1597536: cleaning up old import_entry_requests rows

And avoiding storing full path to dataroot temp dir in db for imported
entries

behatnotneeded

Change-Id: I18fb3a76accd57517e340ce98a19f7b10bd4814c
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 9ca160df
......@@ -377,11 +377,13 @@ class LeapImportFile extends LeapImportArtefactPlugin {
$data = self::get_file_entry_data($entry, $importer, $parent);
$pathname = $data->pathname;
if (file_exists($pathname)) {
$filesize = filesize($pathname);
}
// Don't save full pathname to db, only the relative path to dataroot
$pathname = preg_replace('#^' . get_config('dataroot') . '#', '', $pathname);
// Work around that save_file doesn't let us set the mtime
return PluginImportLeap::add_import_entry_request($importer->get('importertransport')->get('importid'), (string)$entry->id, self::STRATEGY_IMPORT_AS_FILE, 'file', array(
'owner' => $data->owner,
......@@ -417,7 +419,7 @@ class LeapImportFile extends LeapImportArtefactPlugin {
$data->parent = $parent;
}
$data->owner = $entry_request->ownerid;
$data->pathname = get_config('dataroot') . $data->pathname;
if ($artefact = self::create_file_from_entry_data($data, $importer, $entry_request->entryid)) {
$importer->add_artefactmapping($entry_request->entryid, $artefact->get('id'), true);
return $artefact;
......
......@@ -295,6 +295,7 @@ abstract class PluginImport extends Plugin implements IPluginImport {
'duplicateditemids' => serialize($duplicatedartefactids),
'existingitemids' => serialize($existingartefactids),
'decision' => $decision,
'ctime' => db_format_timestamp(time()),
));
}
return false;
......@@ -674,22 +675,27 @@ class MnetImporterTransport extends ImporterTransport {
/**
* Looks in the import staging area in dataroot and deletes old, unneeded
* import.
* Also cleans up old import entries in the 'import_entry_requests' table
*/
function import_cleanup_old_imports() {
require_once('file.php');
$basedir = get_config('dataroot') . 'import/';
if (!check_dir_exists($basedir, false)) {
return;
}
$importdir = new DirectoryIterator($basedir);
$mintime = time() - (12 * 60 * 60); // delete imports older than 12 hours
// The import dir contains one directory for each attempted import, named
// after their username and the import timestamp
foreach ($importdir as $attemptdir) {
if ($attemptdir->isDot()) continue;
if ($attemptdir->getCTime() < $mintime) {
rmdirr($basedir . $attemptdir->getFilename());
// remove entries from db older than 12 hours
delete_records_select('import_entry_requests', 'ctime < ?', array(db_format_timestamp($mintime)));
// Delete old files from the import staging area in dataroot
$basedir = get_config('dataroot') . 'import/';
if (check_dir_exists($basedir, false)) {
$importdir = new DirectoryIterator($basedir);
// The import dir contains one directory for each attempted import, named
// after their username and the import timestamp
foreach ($importdir as $attemptdir) {
if ($attemptdir->isDot()) continue;
if ($attemptdir->getCTime() < $mintime) {
rmdirr($basedir . $attemptdir->getFilename());
}
}
}
}
......@@ -1047,6 +1047,7 @@
<FIELD NAME="existingitemids" TYPE="text" LENGTH="big" NOTNULL="false" />
<FIELD NAME="artefactmapping" TYPE="text" LENGTH="big" NOTNULL="false" />
<FIELD NAME="decision" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" />
<FIELD NAME="ctime" TYPE="datetime" NOTNULL="false" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
......
......@@ -4676,5 +4676,17 @@ function xmldb_core_upgrade($oldversion=0) {
set_field('usr', 'suspendedcusr', $suspendinguserid, 'suspendedcusr', 0);
}
if ($oldversion < 2016082400) {
log_debug('Add a "ctime" column to import_entry_requests table.');
$table = new XMLDBTable('import_entry_requests');
$field = new XMLDBField('ctime');
$field->setAttributes(XMLDB_TYPE_DATETIME);
if (!field_exists($table, $field)) {
add_field($table, $field);
// Fill in starting value for existing rows.
execute_sql('UPDATE {import_entry_requests} SET ctime = ?', array(db_format_timestamp(time())));
}
}
return $status;
}
......@@ -16,7 +16,7 @@ $config = new stdClass();
// See https://wiki.mahara.org/wiki/Developer_Area/Version_Numbering_Policy
// For upgrades on stable branches, increment the version by one. On master, use the date.
$config->version = 2016072500;
$config->version = 2016082400;
$config->series = '16.10';
$config->release = '16.10dev';
$config->minupgradefrom = 2012080604;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment