diff --git a/classes/local.php b/classes/local.php index 9b74d4b..94b9e6a 100644 --- a/classes/local.php +++ b/classes/local.php @@ -26,6 +26,7 @@ defined('MOODLE_INTERNAL') || die(); +use core_text; use stored_file; /** @@ -132,7 +133,13 @@ public static function get_optimised_src(\stored_file $file, $originalsrc, $opti public static function add_url_path_to_queue($path) { global $DB; - $existing = $DB->get_record('filter_imageopt', ['urlpath' => $path]); + $existing = $DB->get_record_select( + 'filter_imageopt', + $DB->sql_compare_text('urlpath', core_text::strlen($path)) . '= :path', + ['path' => $path], + '*', + IGNORE_MULTIPLE // It's very unlikely but there is a possible race condition where multiple records exist. + ); if ($existing) { return $existing->id; } @@ -164,7 +171,11 @@ public static function get_url_path_by_id($id) { public static function delete_queue_item_by_path($urlpath) { global $DB; - $DB->delete_records('filter_imageopt', ['urlpath' => $urlpath]); + $DB->delete_records_select( + 'filter_imageopt', + $DB->sql_compare_text('urlpath', core_text::strlen($urlpath)) . '= :path', + ['path' => $urlpath] + ); } /** diff --git a/classes/task/expirequeue.php b/classes/task/expirequeue.php new file mode 100644 index 0000000..13e50b5 --- /dev/null +++ b/classes/task/expirequeue.php @@ -0,0 +1,47 @@ +. + +/** + * @package filter_imageopt + * @author Andrew Hancox + * @author Open Source Learning + * @link https://opensourcelearning.co.uk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @copyright 2024, Andrew Hancox + */ + +namespace filter_imageopt\task; + +defined('MOODLE_INTERNAL') || die(); + +class expirequeue extends \core\task\scheduled_task { + /** + * @return string + */ + public function get_name() { + return get_string('expirequeuetask', 'filter_imageopt'); + } + + public function execute() { + global $DB; + + $DB->delete_records_select( + 'filter_imageopt', + 'timecreated < :time', + ['time' => time() - YEARSECS] + ); + } +} diff --git a/db/install.xml b/db/install.xml index 65c2276..8760fb3 100644 --- a/db/install.xml +++ b/db/install.xml @@ -1,5 +1,5 @@ - @@ -7,16 +7,13 @@ - + - - -
-
\ No newline at end of file + diff --git a/db/tasks.php b/db/tasks.php new file mode 100755 index 0000000..738fbbe --- /dev/null +++ b/db/tasks.php @@ -0,0 +1,40 @@ +. + +/** + * @package filter_imageopt + * @author Andrew Hancox + * @author Open Source Learning + * @link https://opensourcelearning.co.uk + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @copyright 2024, Andrew Hancox + */ + +defined('MOODLE_INTERNAL') || die(); + +$tasks = [ + [ + 'classname' => '\filter_imageopt\task\expirequeue', + 'blocking' => 0, + 'minute' => '0', + 'hour' => '0', + 'day' => '*', + 'month' => '*', + 'dayofweek' => '*', + 'disabled' => 0 + ], +]; + diff --git a/db/upgrade.php b/db/upgrade.php index 729f274..8d66ded 100644 --- a/db/upgrade.php +++ b/db/upgrade.php @@ -60,6 +60,25 @@ function xmldb_filter_imageopt_upgrade($oldversion) { // Imageopt savepoint reached. upgrade_plugin_savepoint(true, 2018061803, 'filter', 'imageopt'); } + if ($oldversion < 2024013100) { + + // Define index urlpath (unique) to be dropped form filter_imageopt. + $table = new xmldb_table('filter_imageopt'); + $index = new xmldb_index('urlpath', XMLDB_INDEX_UNIQUE, ['urlpath']); + + // Conditionally launch drop index urlpath. + if ($dbman->index_exists($table, $index)) { + $dbman->drop_index($table, $index); + } + + $field = new xmldb_field('urlpath', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null, 'id'); + + // Launch change of type for field urlpath. + $dbman->change_field_type($table, $field); + + // Imageopt savepoint reached. + upgrade_plugin_savepoint(true, 2024013100, 'filter', 'imageopt'); + } return true; } diff --git a/lang/en/filter_imageopt.php b/lang/en/filter_imageopt.php index f17db77..3562ac2 100644 --- a/lang/en/filter_imageopt.php +++ b/lang/en/filter_imageopt.php @@ -21,6 +21,7 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ $string['cachedef_public_files'] = 'Cache of public file contenthashes'; +$string['expirequeuetask'] = 'Expire queued images'; $string['filtername'] = 'Image optimiser'; $string['maxwidth'] = 'Maximum image width'; $string['maxwidthdesc'] = 'Maximum image width in pixels'; diff --git a/tests/filter_test.php b/tests/filter_test.php index f1ce1cb..6173883 100644 --- a/tests/filter_test.php +++ b/tests/filter_test.php @@ -27,6 +27,7 @@ defined('MOODLE_INTERNAL') || die(); use context; +use core_text; use moodle_url; use stored_file; use phpunit_util; @@ -121,7 +122,11 @@ public function test_image_opt_url() { $url = phpunit_util::call_internal_method($filter, 'image_opt_url', [$file, $originalurl], get_class($filter)); - $row = $DB->get_record('filter_imageopt', ['urlpath' => 'pluginfile.php/somefile.jpg']); + $path = "pluginfile.php/somefile.jpg"; + $row = $DB->get_record_select( + 'filter_imageopt', + $DB->sql_compare_text('urlpath', core_text::strlen($path)) . "= '{$path}'" + ); $urlpathid = $row->id; $expected = new moodle_url( diff --git a/version.php b/version.php index cd42b05..47b0d8c 100644 --- a/version.php +++ b/version.php @@ -22,7 +22,7 @@ */ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2022020800; +$plugin->version = 2024013101; $plugin->requires = 2011111500; $plugin->component = 'filter_imageopt'; $plugin->maturity = MATURITY_BETA;