Commit fb639519 authored by Dmitrii Metelkin's avatar Dmitrii Metelkin Committed by Robert Lyon
Browse files

Bug 1647885: Fix updating mime types on install/upgrade

behatnotneeded

Change-Id: I607ec4aaabf8edb6fa5fa9731890a40ac62a0af0
parent f68a3437
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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">
+13 −0
Original line number Diff line number Diff line
@@ -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;
}
+6 −36
Original line number Diff line number Diff line
@@ -219,56 +219,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']++;
            }
        }

        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) {
+2 −2
Original line number Diff line number Diff line
@@ -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';