public static function update_type($extension, $newextension, $mimetype, $coreicon,
            array $groups = array(), $corestring = '', $customdescription = '',
            $defaulticon = false) {

        // Extension must exist.
        $extension = (string)$extension;
        $mimetypes = get_mimetypes_array();
        if (!array_key_exists($extension, $mimetypes)) {
            throw new coding_exception('Extension ' . $extension . ' not found');
        }

        // If there's a new extension then this must not exist.
        $newextension = (string)$newextension;
        if ($newextension !== $extension) {
            if ($newextension === '' || $newextension[0] === '.') {
                throw new coding_exception('Invalid extension .' . $newextension);
            }
            if (array_key_exists($newextension, $mimetypes)) {
                throw new coding_exception('Extension ' . $newextension . ' already exists');
            }
        }

        // For default icon, check there isn't already something with default icon
        // set for that MIME type (unless it's this).
        if ($defaulticon) {
            foreach ($mimetypes as $ext => $type) {
                if ($ext !== $extension && $type['type'] === $mimetype &&
                        !empty($type['defaulticon'])) {
                    throw new coding_exception('MIME type ' . $mimetype .
                            ' already has a default icon set');
                }
            }
        }

        // Delete the old extension and then add the new one (may be same). This
        // will correctly handle cases when a default type is involved.
        self::delete_type($extension);
        self::add_type($newextension, $mimetype, $coreicon, $groups, $corestring,
                $customdescription, $defaulticon);
    }