Commit b34acbbe authored by Dmitrii Metelkin's avatar Dmitrii Metelkin Committed by Robert Lyon

Bug 1647885: Fix updating mime types on install/upgrade

behatnotneeded Change-Id: I607ec4aaabf8edb6fa5fa9731890a40ac62a0af0
parent 52abb658
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
<FIELD NAME="description" TYPE="char" LENGTH="32" NOTNULL="true"/> <FIELD NAME="description" TYPE="char" LENGTH="32" NOTNULL="true"/>
</FIELDS> </FIELDS>
<KEYS> <KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="mimetype"/> <KEY NAME="primary" TYPE="primary" FIELDS="mimetype, description" />
</KEYS> </KEYS>
</TABLE> </TABLE>
<TABLE NAME="artefact_file_embedded"> <TABLE NAME="artefact_file_embedded">
......
...@@ -442,5 +442,18 @@ function xmldb_artefact_file_upgrade($oldversion=0) { ...@@ -442,5 +442,18 @@ function xmldb_artefact_file_upgrade($oldversion=0) {
} }
} }
if ($oldversion < 2015101901) {
log_debug('Recreate artefact_file_mime_types table');
$table = new XMLDBTable('artefact_file_mime_types');
drop_table($table);
$table = new XMLDBTable('artefact_file_mime_types');
$table->addFieldInfo('mimetype', XMLDB_TYPE_CHAR, 128, XMLDB_UNSIGNED, XMLDB_NOTNULL);
$table->addFieldInfo('description', XMLDB_TYPE_CHAR, 32, XMLDB_UNSIGNED, XMLDB_NOTNULL);
$table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('mimetype, description'));
create_table($table);
}
return $status; return $status;
} }
...@@ -218,56 +218,26 @@ class PluginArtefactFile extends PluginArtefact { ...@@ -218,56 +218,26 @@ class PluginArtefactFile extends PluginArtefact {
* This can be called on install (and is, in the postinst method above), * This can be called on install (and is, in the postinst method above),
* and every time an upgrade is made that changes the file. * and every time an upgrade is made that changes the file.
*/ */
function resync_filetype_list() { public static function resync_filetype_list() {
require_once('xmlize.php'); require_once('xmlize.php');
db_begin(); db_begin();
$currentlist = get_records_assoc('artefact_file_mime_types'); delete_records('artefact_file_mime_types');
$newlist = xmlize(file_get_contents(get_config('docroot') . 'artefact/file/filetypes.xml')); $newlist = xmlize(file_get_contents(get_config('docroot') . 'artefact/file/filetypes.xml'));
$filetypes = $newlist['filetypes']['#']['filetype']; $filetypes = $newlist['filetypes']['#']['filetype'];
$newtypes = array();
$count = array('added' => 0, 'updated' => 0, 'removed' => 0);
// Step one: if a mimetype is in the new list that is not in the current
// list, add it to the current list.
foreach ($filetypes as $filetype) { foreach ($filetypes as $filetype) {
$description = $filetype['#']['description'][0]['#']; $description = $filetype['#']['description'][0]['#'];
foreach ($filetype['#']['mimetypes'][0]['#']['mimetype'] as $type) { foreach ($filetype['#']['mimetypes'][0]['#']['mimetype'] as $type) {
$mimetype = $type['#']; $mimetype = $type['#'];
if (!isset($currentlist[$mimetype])) {
execute_sql("INSERT INTO {artefact_file_mime_types} (mimetype, description) VALUES (?,?)", array($mimetype, $description)); execute_sql("INSERT INTO {artefact_file_mime_types} (mimetype, description) VALUES (?,?)", array($mimetype, $description));
$count['added']++;
}
else if ($currentlist[$mimetype]->description != $description) {
execute_sql("UPDATE {artefact_file_mime_types} SET description = ? WHERE mimetype = ?", array($description, $mimetype));
$count['updated']++;
}
$newtypes[$mimetype] = true;
$currentlist[$mimetype] = (object) array(
'mimetype' => $mimetype,
'description' => $description,
);
}
}
// Step two: If a mimetype is in the current list that is not in the
// new list, remove it from the current list.
foreach ($currentlist as $mimetype => $type) {
if (!isset($newtypes[$mimetype])) {
delete_records('artefact_file_mime_types', 'mimetype', $mimetype);
$count['removed']++;
} }
} }
db_commit(); db_commit();
$changes = array();
foreach (array_filter($count) as $k => $v) { log_info('Synced filetype list with filetypes.xml');
$changes[] = "$v $k";
}
if ($changes) {
log_info('Updated filetype list: ' . join(', ', $changes) . '.');
}
} }
public static function get_mimetypes_from_description($description=null, $getrecords=false) { public static function get_mimetypes_from_description($description=null, $getrecords=false) {
......
...@@ -12,6 +12,6 @@ ...@@ -12,6 +12,6 @@
defined('INTERNAL') || die(); defined('INTERNAL') || die();
$config = new StdClass; $config = new StdClass;
$config->version = 2015101900; $config->version = 2015101901;
$config->release = '1.2.5'; $config->release = '1.2.7';
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