mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 00:46:50 +02:00
MDL-31244, MDL-25063 algebra filter: fix common false positives.
There are two well-known cases where the algebra filter messes up input that is obviously not meant for the algebra filter: 1. Copy and paste of unified diffs. 2. @@PLUGINFILE@@ tokens in the HTML that are due to be replaced by the files API. This fix detects these two cases, and just stops the algebra filter from replacing them.
This commit is contained in:
parent
bac15e5782
commit
35a95edba4
2 changed files with 104 additions and 5 deletions
|
@ -89,7 +89,7 @@ function filter_algebra_image($imagefile, $tex= "", $height="", $width="", $alig
|
|||
}
|
||||
|
||||
class filter_algebra extends moodle_text_filter {
|
||||
function filter($text, array $options = array()){
|
||||
public function filter($text, array $options = array()){
|
||||
global $CFG, $DB;
|
||||
|
||||
/// Do a quick check using stripos to avoid unnecessary wor
|
||||
|
@ -114,9 +114,6 @@ class filter_algebra extends moodle_text_filter {
|
|||
# return $text;
|
||||
# }
|
||||
|
||||
|
||||
$text .= ' ';
|
||||
|
||||
preg_match_all('/@(@@+)([^@])/',$text,$matches);
|
||||
for ($i=0;$i<count($matches[0]);$i++) {
|
||||
$replacement = str_replace('@','@',$matches[1][$i]).$matches[2][$i];
|
||||
|
@ -129,6 +126,17 @@ class filter_algebra extends moodle_text_filter {
|
|||
preg_match_all('/<algebra>(.+?)<\/algebra>|@@(.+?)@@/is', $text, $matches);
|
||||
for ($i=0; $i<count($matches[0]); $i++) {
|
||||
$algebra = $matches[1][$i] . $matches[2][$i];
|
||||
|
||||
// Look for some common false positives, and skip processing them.
|
||||
if ($algebra == 'PLUGINFILE' || $algebra == 'DRAFTFILE') {
|
||||
// Raw pluginfile URL.
|
||||
continue;
|
||||
}
|
||||
if (preg_match('/^ -\d+(,\d+)? \+\d+(,\d+)? $/', $algebra)) {
|
||||
// Part of a unified diff.
|
||||
continue;
|
||||
}
|
||||
|
||||
$algebra = str_replace('<nolink>','',$algebra);
|
||||
$algebra = str_replace('</nolink>','',$algebra);
|
||||
$algebra = str_replace('<span class="nolink">','',$algebra);
|
||||
|
@ -159,7 +167,7 @@ class filter_algebra extends moodle_text_filter {
|
|||
$algebra = str_replace('upsilon','zupslon',$algebra);
|
||||
$algebra = preg_replace('!\r\n?!',' ',$algebra);
|
||||
$algebra = escapeshellarg($algebra);
|
||||
if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows") ) {
|
||||
if ( (PHP_OS == "WINNT") || (PHP_OS == "WIN32") || (PHP_OS == "Windows")) {
|
||||
$cmd = "cd $CFG->dirroot\\filter\\algebra & algebra2tex.pl $algebra";
|
||||
} else {
|
||||
$cmd = "cd $CFG->dirroot/filter/algebra; ./algebra2tex.pl $algebra";
|
||||
|
@ -220,6 +228,7 @@ class filter_algebra extends moodle_text_filter {
|
|||
$texexp = preg_replace('/\\\int\\\left\((.+?d[a-z])\\\right\)/s','\int '. "\$1 ",$texexp);
|
||||
$texexp = preg_replace('/\\\lim\\\left\((.+?),(.+?),(.+?)\\\right\)/s','\lim_'. "{\$2\\to \$3}\$1 ",$texexp);
|
||||
$texexp = str_replace('\mbox', '', $texexp); // now blacklisted in tex, sorry
|
||||
$texcache = new stdClass();
|
||||
$texcache->filter = 'algebra';
|
||||
$texcache->version = 1;
|
||||
$texcache->md5key = $md5;
|
||||
|
|
90
filter/algebra/tests/filter_test.php
Normal file
90
filter/algebra/tests/filter_test.php
Normal file
|
@ -0,0 +1,90 @@
|
|||
<?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 filter_algebra
|
||||
*
|
||||
* @package filter_algebra
|
||||
* @category phpunit
|
||||
* @copyright 2012 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->dirroot . '/filter/algebra/filter.php');
|
||||
|
||||
|
||||
/**
|
||||
* Unit tests for filter_algebra.
|
||||
*
|
||||
* Note that this only tests some of the filter logic. It does not acutally test
|
||||
* the normal case of the filter working, because I cannot make it work on my
|
||||
* test server, and if it does not work here, it probably does not also work
|
||||
* for other people. A failing test will be irritating noise.
|
||||
*
|
||||
* @copyright 2012 Tim Hunt
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class filter_algebra_testcase extends basic_testcase {
|
||||
|
||||
protected $filter;
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->filter = new filter_algebra(context_system::instance(), array());
|
||||
}
|
||||
|
||||
function test_algebra_filter_no_algebra() {
|
||||
$this->assertEquals('<p>Look no algebra!</p>',
|
||||
$this->filter->filter('<p>Look no algebra!</p>'));
|
||||
}
|
||||
|
||||
|
||||
function test_algebra_filter_pluginfile() {
|
||||
$this->assertEquals('<img src="@@PLUGINFILE@@/photo.jpg">',
|
||||
$this->filter->filter('<img src="@@PLUGINFILE@@/photo.jpg">'));
|
||||
}
|
||||
|
||||
function test_algebra_filter_draftfile() {
|
||||
$this->assertEquals('<img src="@@DRAFTFILE@@/photo.jpg">',
|
||||
$this->filter->filter('<img src="@@DRAFTFILE@@/photo.jpg">'));
|
||||
}
|
||||
|
||||
function test_algebra_filter_unified_diff() {
|
||||
$diff = '
|
||||
diff -u -r1.1 Worksheet.php
|
||||
--- Worksheet.php 26 Sep 2003 04:18:02 -0000 1.1
|
||||
+++ Worksheet.php 18 Nov 2009 03:58:50 -0000
|
||||
@@ -1264,10 +1264,10 @@
|
||||
}
|
||||
|
||||
// Strip the = or @ sign at the beginning of the formula string
|
||||
- if (ereg("^=",$formula)) {
|
||||
+ if (preg_match("/^=/",$formula)) {
|
||||
$formula = preg_replace("/(^=)/","",$formula);
|
||||
}
|
||||
- elseif(ereg("^@",$formula)) {
|
||||
+ elseif(preg_match("/^@/",$formula)) {
|
||||
$formula = preg_replace("/(^@)/","",$formula);
|
||||
}
|
||||
else {
|
||||
';
|
||||
$this->assertEquals('<pre>' . $diff . '</pre>',
|
||||
$this->filter->filter('<pre>' . $diff . '</pre>'));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue