Skip to content

Handle long URLs #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions classes/local.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

defined('MOODLE_INTERNAL') || die();

use core_text;
use stored_file;

/**
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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]
);
}

/**
Expand Down
47 changes: 47 additions & 0 deletions classes/task/expirequeue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package filter_imageopt
* @author Andrew Hancox <[email protected]>
* @author Open Source Learning <[email protected]>
* @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]
);
}
}
9 changes: 3 additions & 6 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="filter/imageopt/db" VERSION="20180618" COMMENT="XMLDB file for Moodle filter/imageopt"
<XMLDB PATH="filter/imageopt/db" VERSION="20240131" COMMENT="XMLDB file for Moodle filter/imageopt"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="filter_imageopt" COMMENT="Table for tracking image url processing.">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="urlpath" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="urlpath" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="timeprocessed" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="urlpath" UNIQUE="true" FIELDS="urlpath"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
</XMLDB>
40 changes: 40 additions & 0 deletions db/tasks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* @package filter_imageopt
* @author Andrew Hancox <[email protected]>
* @author Open Source Learning <[email protected]>
* @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
],
];

19 changes: 19 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions lang/en/filter_imageopt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
7 changes: 6 additions & 1 deletion tests/filter_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
defined('MOODLE_INTERNAL') || die();

use context;
use core_text;
use moodle_url;
use stored_file;
use phpunit_util;
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down