diff --git a/files/classes/conversion.php b/files/classes/conversion.php index 9c61530c02c..0a2e8610ed5 100644 --- a/files/classes/conversion.php +++ b/files/classes/conversion.php @@ -128,16 +128,36 @@ class conversion extends \core\persistent { // 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 - )"; + $dbfamily = $DB->get_dbfamily(); + switch ($dbfamily) { + // For certain DB engines, use a more optimised query. + case 'mysql': + case 'postgres': + $sql = "SELECT {$sqlfields} + FROM {" . self::TABLE . "} c + JOIN (SELECT id + FROM {files} + WHERE contenthash = :ccontenthash + LIMIT 1 + ) conversionsourcefile ON conversionsourcefile.id = c.sourcefileid + LEFT JOIN {files} conversiondestfile ON conversiondestfile.id = c.destfileid + WHERE c.targetformat = :cformat + AND (c.destfileid IS NULL + OR conversiondestfile.id IS NOT NULL)"; + break; + + // For everything else, use the standard cross-db compatible query. + default: + $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)"; + break; + } // 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. diff --git a/lib/db/install.xml b/lib/db/install.xml index be6dd424047..e37610320d9 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -2600,6 +2600,7 @@ + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 4beba9a85f3..e9f86bfd94f 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -4530,5 +4530,20 @@ privatefiles,moodle|/user/files.php'; upgrade_main_savepoint(true, 2022052700.01); } + if ($oldversion < 2022052700.02) { + + // Define index filename (not unique) to be added to files. + $table = new xmldb_table('files'); + $index = new xmldb_index('filename', XMLDB_INDEX_NOTUNIQUE, ['filename']); + + // Conditionally launch add index filename. + if (!$dbman->index_exists($table, $index)) { + $dbman->add_index($table, $index); + } + + // Main savepoint reached. + upgrade_main_savepoint(true, 2022052700.02); + } + return true; } diff --git a/version.php b/version.php index 1342998c0a5..d831558599a 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2022052700.01; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2022052700.02; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.1dev (Build: 20220527)'; // Human-friendly version name