forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-58461 filter_mathjaxloader: Change CDN url
The MathJax CDN url is going away - we need to switch to another mirror.
- Loading branch information
Damyon Wiese
committed
Apr 11, 2017
1 parent
bd99cb9
commit 7a0f141
Showing
4 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?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/>. | ||
|
||
/** | ||
* Random functions for mathjax upgrades. | ||
* | ||
* @package filter_mathjaxloader | ||
* @copyright 2017 Damyon Wiese ([email protected]) | ||
* @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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?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/>. | ||
|
||
/** | ||
* 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)); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters