Commit bb71f2ed authored by Robert Lyon's avatar Robert Lyon Committed by Gerrit Code Review

Merge "Bug 1647885: Fix updating mime types on install/upgrade" into 15.10_STABLE

parents 986f34fb b34acbbe
......@@ -34,7 +34,7 @@
<FIELD NAME="description" TYPE="char" LENGTH="32" NOTNULL="true"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="mimetype"/>
<KEY NAME="primary" TYPE="primary" FIELDS="mimetype, description" />
</KEYS>
</TABLE>
<TABLE NAME="artefact_file_embedded">
......
......@@ -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;
}
......@@ -218,56 +218,26 @@ class PluginArtefactFile extends PluginArtefact {
* This can be called on install (and is, in the postinst method above),
* 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');
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'));
$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) {
$description = $filetype['#']['description'][0]['#'];
foreach ($filetype['#']['mimetypes'][0]['#']['mimetype'] as $type) {
$mimetype = $type['#'];
if (!isset($currentlist[$mimetype])) {
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']++;
execute_sql("INSERT INTO {artefact_file_mime_types} (mimetype, description) VALUES (?,?)", array($mimetype, $description));
}
}
db_commit();
$changes = array();
foreach (array_filter($count) as $k => $v) {
$changes[] = "$v $k";
}
if ($changes) {
log_info('Updated filetype list: ' . join(', ', $changes) . '.');
}
log_info('Synced filetype list with filetypes.xml');
}
public static function get_mimetypes_from_description($description=null, $getrecords=false) {
......
......@@ -12,6 +12,6 @@
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2015101900;
$config->release = '1.2.5';
$config->version = 2015101901;
$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