public static function get_conversions_for_file(stored_file $file, $format) {
        global $DB;
        $instances = [];

        // Conversion records are intended for tracking a conversion in progress or recently completed.
        // The record is removed periodically, but the destination file is not.
        // We need to fetch all conversion records which match the source file and target, and also all source and
        // destination files which do not have a conversion record.
        $sqlfields = self::get_sql_fields('c', 'conversion');

        // Fetch actual conversions which relate to the specified source file, and have a matching conversion record,
        // and either have a valid destination file which still exists, or do not have a destination file at all.
        $sql = "SELECT {$sqlfields}
                FROM {" . self::TABLE . "} c
                INNER JOIN {files} conversionsourcefile ON conversionsourcefile.id = c.sourcefileid
                LEFT JOIN {files} conversiondestfile ON conversiondestfile.id = c.destfileid
                WHERE
                    conversionsourcefile.contenthash = :ccontenthash
                AND c.targetformat = :cformat
                AND (
                    c.destfileid IS NULL OR conversiondestfile.id IS NOT NULL
                )";

        // Fetch a empty conversion record for each source/destination combination that we find to match where the
        // destination file is in the correct filearea/filepath/filename combination to meet the requirements.
        // This ensures that existing conversions are used where possible, even if there is no 'conversion' record for
        // them.
        $sql .= "
            UNION ALL
                SELECT
                    NULL AS conversionid,
                    orphanedsourcefile.id AS conversionsourcefileid,
                    :oformat AS conversiontargetformat,
                    2 AS conversionstatus,
                    NULL AS conversionstatusmessage,
                    NULL AS conversionconverter,
                    orphaneddestfile.id AS conversiondestfileid,
                    NULL AS conversiondata,
                    0 AS conversiontimecreated,
                    0 AS conversiontimemodified,
                    0 AS conversionusermodified
                FROM {files} orphanedsourcefile
                INNER JOIN {files} orphaneddestfile ON (
                        orphaneddestfile.filename = orphanedsourcefile.contenthash
                    AND orphaneddestfile.component = 'core'
                    AND orphaneddestfile.filearea = 'documentconversion'
                    AND orphaneddestfile.filepath = :ofilepath
                )
                LEFT JOIN {" . self::TABLE . "} orphanedconversion ON orphanedconversion.destfileid = orphaneddestfile.id
                WHERE
                    orphanedconversion.id IS NULL
                AND
                    orphanedsourcefile.id = :osourcefileid
                ";
        $records = $DB->get_records_sql($sql, [
            'ccontenthash' => $file->get_contenthash(),
            'osourcefileid' => $file->get_id(),
            'cfilepath' => "/{$format}/",
            'ofilepath' => "/{$format}/",
            'cformat' => $format,
            'oformat' => $format,
        ]);

        foreach ($records as $record) {
            $data = self::extract_record($record, 'conversion');
            $newrecord = new static(0, $data);
            $instances[] = $newrecord;
        }

        return $instances;
    }