diff --git a/filter/mathjaxloader/db/upgrade.php b/filter/mathjaxloader/db/upgrade.php index cfc9ea79e47b5..a16cbf1d068d4 100644 --- a/filter/mathjaxloader/db/upgrade.php +++ b/filter/mathjaxloader/db/upgrade.php @@ -31,6 +31,8 @@ function xmldb_filter_mathjaxloader_upgrade($oldversion) { global $CFG, $DB; + require_once($CFG->dirroot . '/filter/mathjaxloader/db/upgradelib.php'); + $dbman = $DB->get_manager(); if ($oldversion < 2014081100) { @@ -157,6 +159,14 @@ function xmldb_filter_mathjaxloader_upgrade($oldversion) { } // Automatically generated Moodle v3.2.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2017040300) { + + $httpsurl = get_config('filter_mathjaxloader', 'httpsurl'); + $newcdnurl = filter_mathjaxloader_upgrade_cdn_cloudflare($httpsurl, false); + + set_config('httpsurl', $newcdnurl, 'filter_mathjaxloader'); + upgrade_plugin_savepoint(true, 2017040300, 'filter', 'mathjaxloader'); + } return true; } diff --git a/filter/mathjaxloader/db/upgradelib.php b/filter/mathjaxloader/db/upgradelib.php new file mode 100644 index 0000000000000..295d4b3d9a854 --- /dev/null +++ b/filter/mathjaxloader/db/upgradelib.php @@ -0,0 +1,66 @@ +. + +/** + * Random functions for mathjax upgrades. + * + * @package filter_mathjaxloader + * @copyright 2017 Damyon Wiese (damyon@moodle.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * This function takes an existing mathjax url and, if it was using the standard mathjax cdn, + * upgrades it to use the cloudflare matchjax cdn (because the standard one is shutting down). + * @param string $mathjaxurl - The current url. + * @param boolean $httponly - Use http instead of https - really only for 3.1 upgrades. + * @return string The new url. + */ +function filter_mathjaxloader_upgrade_cdn_cloudflare($mathjaxurl, $httponly = false) { + $newcdnurl = $mathjaxurl; + $cdnroot = 'https://cdn.mathjax.org/mathjax/'; + if ($httponly) { + $cdnroot = 'http://cdn.mathjax.org/mathjax/'; + } + $usingcdn = strpos($mathjaxurl, $cdnroot) === 0; + if ($usingcdn) { + $majorversion = substr($mathjaxurl, strlen($cdnroot), 3); + $latestversion = '2.7.0'; + if ($majorversion == '2.6') { + $latestversion = '2.6.1'; + } else if ($majorversion == '2.5') { + $latestversion = '2.5.3'; + } + + $offset = strpos($mathjaxurl, '/', strlen($cdnroot)); + if ($offset === false) { + return $newcdnurl; + } + + $endofurl = substr($mathjaxurl, $offset + 1); + + $newcdnbase = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/'; + if ($httponly) { + $newcdnbase = 'http://cdnjs.cloudflare.com/ajax/libs/mathjax/'; + } + + $newcdnurl = $newcdnbase . $latestversion . '/' . $endofurl; + } + + return $newcdnurl; +} diff --git a/filter/mathjaxloader/tests/upgradelib_test.php b/filter/mathjaxloader/tests/upgradelib_test.php new file mode 100644 index 0000000000000..01d99088671a6 --- /dev/null +++ b/filter/mathjaxloader/tests/upgradelib_test.php @@ -0,0 +1,91 @@ +. + +/** + * Unit test for the upgrade MathJax code. + * + * @package filter_mathjaxloader + * @copyright 2017 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/filter/mathjaxloader/db/upgradelib.php'); + +/** + * Unit test for the upgrade MathJax code. + * + * Test the functions in upgradelib.php + * + * @copyright 2017 Damyon Wiese + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class filter_mathjax_upgradelib_testcase extends advanced_testcase { + + public function test_upgradelib() { + $current = 'https://cdn.mathjax.org/mathjax/2.7-latest/MathJax.js?...'; + $expected = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?...'; + $this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current)); + + $current = 'http://cdn.mathjax.org/mathjax/2.7-latest/MathJax.js?...'; + $expected = 'http://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?...'; + $this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current, true)); + + $current = 'http://cdn.mathjax.org/mathjax/2.7-latest/MathJax.js?...'; + $expected = 'http://cdn.mathjax.org/mathjax/2.7-latest/MathJax.js?...'; + $this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current, false)); + + $current = 'https://cdn.mathjax.org/mathjax/2.7-latest/MathJax.js?...'; + $expected = 'https://cdn.mathjax.org/mathjax/2.7-latest/MathJax.js?...'; + $this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current, true)); + + $current = 'https://cdn.mathjax.org/mathjax/2.6-latest/MathJax.js?...'; + $expected = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.6.1/MathJax.js?...'; + $this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current)); + + $current = 'https://cdn.mathjax.org/mathjax/2.5-latest/MathJax.js?...'; + $expected = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.5.3/MathJax.js?...'; + $this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current)); + + // Dont touch non https links. + $current = 'http://cdn.mathjax.org/mathjax/2.5-latest/MathJax.js?...'; + $expected = 'http://cdn.mathjax.org/mathjax/2.5-latest/MathJax.js?...'; + $this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current)); + + // Dont touch non local links. + $current = 'https://mylocalmirror/mathjax/2.7-latest/MathJax.js?...'; + $expected = 'https://mylocalmirror/mathjax/2.7-latest/MathJax.js?...'; + $this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current)); + + // Try some unexpected things. + $current = ''; + $expected = ''; + $this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current)); + + // Try some unexpected things. + $current = 'https://cdn.mathjax.org/mathjax/2.7-latest/MathJax.js'; + $expected = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js'; + $this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current)); + + // Try some unexpected things. + $current = 'https://cdn.mathjax.org/mathjax/2.7-latest'; + $expected = 'https://cdn.mathjax.org/mathjax/2.7-latest'; + $this->assertEquals($expected, filter_mathjaxloader_upgrade_cdn_cloudflare($current)); + } +} + diff --git a/filter/mathjaxloader/version.php b/filter/mathjaxloader/version.php index 69b63b3e32c55..062f3244dc438 100644 --- a/filter/mathjaxloader/version.php +++ b/filter/mathjaxloader/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016120500; +$plugin->version = 2017040300; $plugin->requires = 2016112900; // Requires this Moodle version. $plugin->component= 'filter_mathjaxloader';