Skip to content

Commit

Permalink
Merge branch 'MDL-40939-master' of https://github.com/timgus/moodle
Browse files Browse the repository at this point in the history
Conflicts:
	filter/tex/version.php
	lang/en/admin.php
  • Loading branch information
Damyon Wiese committed Dec 3, 2013
2 parents a59a423 + 7a37232 commit 1341058
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 47 deletions.
17 changes: 17 additions & 0 deletions filter/tex/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,22 @@ function xmldb_filter_tex_upgrade($oldversion) {
// Moodle v2.6.0 release upgrade line.
// Put any upgrade step following this.

if ($oldversion < 2013120300) {
$settings = array(
'density', 'latexbackground', 'convertformat', 'pathlatex',
'convertformat', 'pathconvert', 'pathdvips', 'latexpreamble');

// Move tex settings to config_pluins and delete entries from the config table.
foreach ($settings as $setting) {
$existingkey = 'filter_tex_'.$setting;
if (array_key_exists($existingkey, $CFG)) {
set_config($setting, $CFG->{$existingkey}, 'filter_tex');
unset_config($existingkey);
}
}

upgrade_plugin_savepoint(true, 2013120300, 'filter', 'tex');
}

return true;
}
3 changes: 2 additions & 1 deletion filter/tex/filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ function filter($text, array $options = array()) {
$texcache->timemodified = time();
$DB->insert_record("cache_filters", $texcache, false);
}
$filename = $md5 . ".{$CFG->filter_tex_convertformat}";
$convertformat = get_config('filter_tex', 'convertformat');
$filename = $md5.".{$convertformat}";
$text = str_replace( $matches[0][$i], filter_text_image($filename, $texexp, 0, 0, $align, $alt), $text);
}
return $text;
Expand Down
9 changes: 9 additions & 0 deletions filter/tex/lang/en/filter_tex.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,14 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['configconvertformat'] = 'If <i>latex</i>, <i>dvips</i> and <i>convert</i> are available, the images are created using the specified format. If it is not, mimeTeX will be used and it will create GIF images.';
$string['convertformat'] = '<i>Convert</i> output format';
$string['latexpreamble'] = 'LaTeX preamble';
$string['latexsettings'] = 'LaTeX renderer Settings';
$string['filtername'] = 'TeX notation';
$string['pathconvert'] = 'Path of <i>convert</i> binary';
$string['pathdvips'] = 'Path of <i>dvips</i> binary';
$string['pathlatex'] = 'Path of <i>latex</i> binary';
$string['pathmimetex'] = 'Path of <i>mimetex</i> binary';
$string['pathmimetexdesc'] = 'Moodle will use its own mimetex binary unless another valid path is specified.';
$string['source'] = 'TeX source';
19 changes: 12 additions & 7 deletions filter/tex/latex.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function construct_latex_document( $formula, $fontsize=12 ) {

// $fontsize don't affects to formula's size. $density can change size
$doc = "\\documentclass[{$fontsize}pt]{article}\n";
$doc .= $CFG->filter_tex_latexpreamble;
$doc .= get_config('filter_tex', 'latexpreamble');
$doc .= "\\pagestyle{empty}\n";
$doc .= "\\begin{document}\n";
//dlnsk $doc .= "$ {$formula} $\n";
Expand Down Expand Up @@ -90,7 +90,8 @@ function render( $formula, $filename, $fontsize=12, $density=240, $background=''
global $CFG;

// quick check - will this work?
if (empty($CFG->filter_tex_pathlatex)) {
$pathlatex = get_config('filter_tex', 'pathlatex');
if (empty($pathlatex)) {
return false;
}

Expand All @@ -100,22 +101,24 @@ function render( $formula, $filename, $fontsize=12, $density=240, $background=''
$tex = "{$this->temp_dir}/$filename.tex";
$dvi = "{$this->temp_dir}/$filename.dvi";
$ps = "{$this->temp_dir}/$filename.ps";
$img = "{$this->temp_dir}/$filename.{$CFG->filter_tex_convertformat}";
$convertformat = get_config('filter_tex', 'convertformat');
$img = "{$this->temp_dir}/$filename.{$convertformat}";

// turn the latex doc into a .tex file in the temp area
$fh = fopen( $tex, 'w' );
fputs( $fh, $doc );
fclose( $fh );

// run latex on document
$command = "{$CFG->filter_tex_pathlatex} --interaction=nonstopmode --halt-on-error $tex";
$command = "{$pathlatex} --interaction=nonstopmode --halt-on-error $tex";
chdir( $this->temp_dir );
if ($this->execute($command, $log)) { // It allways False on Windows
// return false;
}

// run dvips (.dvi to .ps)
$command = "{$CFG->filter_tex_pathdvips} -E $dvi -o $ps";
$pathdvips = get_config('filter_tex', 'pathdvips');
$command = "{$pathdvips} -E $dvi -o $ps";
if ($this->execute($command, $log )) {
return false;
}
Expand All @@ -126,7 +129,8 @@ function render( $formula, $filename, $fontsize=12, $density=240, $background=''
} else {
$bg_opt = "";
}
$command = "{$CFG->filter_tex_pathconvert} -density $density -trim $bg_opt $ps $img";
$pathconvert = get_config('filter_tex', 'pathconvert');
$command = "{$pathconvert} -density $density -trim $bg_opt $ps $img";
if ($this->execute($command, $log )) {
return false;
}
Expand All @@ -145,7 +149,8 @@ function clean_up( $filename ) {
unlink( "{$this->temp_dir}/$filename.tex" );
unlink( "{$this->temp_dir}/$filename.dvi" );
unlink( "{$this->temp_dir}/$filename.ps" );
unlink( "{$this->temp_dir}/$filename.{$CFG->filter_tex_convertformat}" );
$convertformat = get_config('filter_tex', 'convertformat');
unlink( "{$this->temp_dir}/$filename.{$convertformat}" );
unlink( "{$this->temp_dir}/$filename.aux" );
unlink( "{$this->temp_dir}/$filename.log" );
return;
Expand Down
22 changes: 17 additions & 5 deletions filter/tex/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ function filter_tex_get_executable($debug=false) {
return "$CFG->dirroot/filter/tex/mimetex.exe";
}

if ($pathmimetex = get_config('filter_tex', 'pathmimetex')) {
if (is_executable($pathmimetex)) {
return $pathmimetex;
} else {
print_error('mimetexnotexecutable', 'error');
}
}

$custom_commandpath = "$CFG->dirroot/filter/tex/mimetex";
if (file_exists($custom_commandpath)) {
if (is_executable($custom_commandpath)) {
Expand Down Expand Up @@ -111,16 +119,20 @@ function filter_tex_updatedcallback($name) {
$DB->delete_records('cache_filters', array('filter'=>'tex'));
$DB->delete_records('cache_filters', array('filter'=>'algebra'));

if (!isset($CFG->filter_tex_pathlatex)) {
$pathlatex = get_config('filter_tex', 'pathlatex');
if ($pathlatex === false) {
// detailed settings not present yet
return;
}

if (!(is_file($CFG->filter_tex_pathlatex) && is_executable($CFG->filter_tex_pathlatex) &&
is_file($CFG->filter_tex_pathdvips) && is_executable($CFG->filter_tex_pathdvips) &&
is_file($CFG->filter_tex_pathconvert) && is_executable($CFG->filter_tex_pathconvert))) {
$pathdvips = get_config('filter_tex', 'pathdvips');
$pathconvert = get_config('filter_tex', 'pathconvert');

if (!(is_file($pathlatex) && is_executable($pathlatex) &&
is_file($pathdvips) && is_executable($pathdvips) &&
is_file($pathconvert) && is_executable($pathconvert))) {
// LaTeX, dvips or convert are not available, and mimetex can only produce GIFs so...
set_config('filter_tex_convertformat', 'gif');
set_config('convertformat', 'gif', 'filter_tex');
}
}

Expand Down
7 changes: 4 additions & 3 deletions filter/tex/pix.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@
}

if (!file_exists($pathname)) {
$md5 = str_replace(".{$CFG->filter_tex_convertformat}",'',$image);
$convertformat = get_config('filter_tex', 'convertformat');
$md5 = str_replace(".{$convertformat}", '', $image);
if ($texcache = $DB->get_record('cache_filters', array('filter'=>'tex', 'md5key'=>$md5))) {
if (!file_exists($CFG->dataroot.'/filter/tex')) {
make_upload_directory('filter/tex');
}

// try and render with latex first
$latex = new latex();
$density = $CFG->filter_tex_density;
$background = $CFG->filter_tex_latexbackground;
$density = get_config('filter_tex', 'density');
$background = get_config('filter_tex', 'latexbackground');
$texexp = $texcache->rawtext; // the entities are now decoded before inserting to DB
$latex_path = $latex->render($texexp, $md5, 12, $density, $background);
if ($latex_path) {
Expand Down
19 changes: 10 additions & 9 deletions filter/tex/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
require_once($CFG->dirroot.'/filter/tex/lib.php');

$items = array();
$items[] = new admin_setting_heading('filter_tex_latexheading', get_string('latexsettings', 'admin'), '');
$items[] = new admin_setting_configtextarea('filter_tex_latexpreamble', get_string('latexpreamble','admin'),
$items[] = new admin_setting_heading('filter_tex/latexheading', get_string('latexsettings', 'filter_tex'), '');
$items[] = new admin_setting_configtextarea('filter_tex/latexpreamble', get_string('latexpreamble','filter_tex'),
'', "\\usepackage[latin1]{inputenc}\n\\usepackage{amsmath}\n\\usepackage{amsfonts}\n\\RequirePackage{amsmath,amssymb,latexsym}\n");
$items[] = new admin_setting_configtext('filter_tex_latexbackground', get_string('backgroundcolour', 'admin'), '', '#FFFFFF');
$items[] = new admin_setting_configtext('filter_tex_density', get_string('density', 'admin'), '', '120', PARAM_INT);
$items[] = new admin_setting_configtext('filter_tex/latexbackground', get_string('backgroundcolour', 'admin'), '', '#FFFFFF');
$items[] = new admin_setting_configtext('filter_tex/density', get_string('density', 'admin'), '', '120', PARAM_INT);

if (PHP_OS=='Linux') {
$default_filter_tex_pathlatex = "/usr/bin/latex";
Expand All @@ -60,18 +60,19 @@
$default_filter_tex_pathconvert = '';
}

$items[] = new admin_setting_configexecutable('filter_tex_pathlatex', get_string('pathlatex', 'admin'), '', $default_filter_tex_pathlatex);
$items[] = new admin_setting_configexecutable('filter_tex_pathdvips', get_string('pathdvips', 'admin'), '', $default_filter_tex_pathdvips);
$items[] = new admin_setting_configexecutable('filter_tex_pathconvert', get_string('pathconvert', 'admin'), '', $default_filter_tex_pathconvert);
$items[] = new admin_setting_configexecutable('filter_tex/pathlatex', get_string('pathlatex', 'filter_tex'), '', $default_filter_tex_pathlatex);
$items[] = new admin_setting_configexecutable('filter_tex/pathdvips', get_string('pathdvips', 'filter_tex'), '', $default_filter_tex_pathdvips);
$items[] = new admin_setting_configexecutable('filter_tex/pathconvert', get_string('pathconvert', 'filter_tex'), '', $default_filter_tex_pathconvert);
$items[] = new admin_setting_configexecutable('filter_tex/pathmimetex', get_string('pathmimetex', 'filter_tex'), get_string('pathmimetexdesc', 'filter_tex'), '');

// Even if we offer GIF and PNG formats here, in the update callback we check whether
// all the paths actually point to executables. If they don't, we force the setting
// to GIF, as that's the only format mimeTeX can produce.
$formats = array('gif' => 'GIF', 'png' => 'PNG');
$items[] = new admin_setting_configselect('filter_tex_convertformat', get_string('convertformat', 'admin'), get_string('configconvertformat', 'admin'), 'gif', $formats);
$items[] = new admin_setting_configselect('filter_tex/convertformat', get_string('convertformat', 'filter_tex'), get_string('configconvertformat', 'filter_tex'), 'gif', $formats);

foreach ($items as $item) {
$item->set_updatedcallback('filter_tex_updatedcallback');
$settings->add($item);
}
}
}
32 changes: 18 additions & 14 deletions filter/tex/texdebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,26 +200,29 @@ function TexOutput($expression, $graphic=false) {
// first check if it is likely to work at all
$output .= "<h3>Checking executables</h3>\n";
$executables_exist = true;
if (is_file($CFG->filter_tex_pathlatex)) {
$output .= "latex executable ($CFG->filter_tex_pathlatex) is readable<br />\n";
$pathlatex = get_config('filter_tex', 'pathlatex');
if (is_file($pathlatex)) {
$output .= "latex executable ($pathlatex) is readable<br />\n";
}
else {
$executables_exist = false;
$output .= "<b>Error:</b> latex executable ($CFG->filter_tex_pathlatex) is not readable<br />\n";
$output .= "<b>Error:</b> latex executable ($pathlatex) is not readable<br />\n";
}
if (is_file($CFG->filter_tex_pathdvips)) {
$output .= "dvips executable ($CFG->filter_tex_pathdvips) is readable<br />\n";
$pathdvips = get_config('filter_tex', 'pathdvips');
if (is_file($pathdvips)) {
$output .= "dvips executable ($pathdvips) is readable<br />\n";
}
else {
$executables_exist = false;
$output .= "<b>Error:</b> dvips executable ($CFG->filter_tex_pathdvips) is not readable<br />\n";
$output .= "<b>Error:</b> dvips executable ($pathdvips) is not readable<br />\n";
}
if (is_file($CFG->filter_tex_pathconvert)) {
$output .= "convert executable ($CFG->filter_tex_pathconvert) is readable<br />\n";
$pathconvert = get_config('filter_tex', 'pathconvert');
if (is_file($pathconvert)) {
$output .= "convert executable ($pathconvert) is readable<br />\n";
}
else {
$executables_exist = false;
$output .= "<b>Error:</b> convert executable ($CFG->filter_tex_pathconvert) is not readable<br />\n";
$output .= "<b>Error:</b> convert executable ($pathconvert) is not readable<br />\n";
}

// knowing that it might work..
Expand All @@ -230,7 +233,8 @@ function TexOutput($expression, $graphic=false) {
$tex = "$latex->temp_dir/$md5.tex";
$dvi = "$latex->temp_dir/$md5.dvi";
$ps = "$latex->temp_dir/$md5.ps";
$img = "$latex->temp_dir/$md5.{$CFG->filter_tex_convertformat}";
$convertformat = get_config('filter_tex', 'convertformat');
$img = "$latex->temp_dir/$md5.{$convertformat}";

// put the expression as a file into the temp area
$expression = html_entity_decode($expression);
Expand All @@ -244,21 +248,21 @@ function TexOutput($expression, $graphic=false) {
chdir($latex->temp_dir);

// step 1: latex command
$cmd = "$CFG->filter_tex_pathlatex --interaction=nonstopmode --halt-on-error $tex";
$cmd = "$pathlatex --interaction=nonstopmode --halt-on-error $tex";
$output .= execute($cmd);

// step 2: dvips command
$cmd = "$CFG->filter_tex_pathdvips -E $dvi -o $ps";
$cmd = "$pathdvips -E $dvi -o $ps";
$output .= execute($cmd);

// step 3: convert command
$cmd = "$CFG->filter_tex_pathconvert -density 240 -trim $ps $img ";
$cmd = "$pathconvert -density 240 -trim $ps $img ";
$output .= execute($cmd);

if (!$graphic) {
echo $output;
} else if (file_exists($img)){
send_file($img, "$md5.{$CFG->filter_tex_convertformat}");
send_file($img, "$md5.{$convertformat}");
} else {
echo "Error creating image, see command execution output for more details.";
}
Expand Down
2 changes: 1 addition & 1 deletion filter/tex/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@

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

$plugin->version = 2013110500; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2013120300; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2013110500; // Requires this Moodle version
$plugin->component = 'filter_tex'; // Full name of the plugin (used for diagnostics)
7 changes: 0 additions & 7 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@
$string['configclamactlikevirus'] = 'Treat files like viruses';
$string['configclamdonothing'] = 'Treat files as OK';
$string['configclamfailureonupload'] = 'If you have configured clam to scan uploaded files, but it is configured incorrectly or fails to run for some unknown reason, how should it behave? If you choose \'Treat files like viruses\', they\'ll be moved into the quarantine area, or deleted. If you choose \'Treat files as OK\', the files will be moved to the destination directory like normal. Either way, admins will be alerted that clam has failed. If you choose \'Treat files like viruses\' and for some reason clam fails to run (usually because you have entered an invalid pathtoclam), ALL files that are uploaded will be moved to the given quarantine area, or deleted. Be careful with this setting.';
$string['configconvertformat'] = 'If <i>latex</i>, <i>dvips</i> and <i>convert</i> are available, the images are created using the specified format. If it is not, mimeTeX will be used and it will create GIF images.';
$string['configcookiehttponly'] = 'Enables new PHP 5.2.0 feature - browsers are instructed to send cookie with real http requests only, cookies should not be accessible by scripting languages. This is not supported in all browsers and it may not be fully compatible with current code. It helps to prevent some types of XSS attacks.';
$string['configcookiesecure'] = 'If server is accepting only https connections it is recommended to enable sending of secure cookies. If enabled please make sure that web server is not accepting http:// or set up permanent redirection to https:// address. When <em>wwwroot</em> address does not start with https:// this setting is turned off automatically.';
$string['configcountry'] = 'If you set a country here, then this country will be selected by default on new user accounts. To force users to choose a country, just leave this unset.';
Expand Down Expand Up @@ -355,7 +354,6 @@
$string['confirmation'] = 'Confirmation';
$string['confirmdeletecomments'] = 'You are about to delete comments, are you sure?';
$string['confirmed'] = 'Confirmed';
$string['convertformat'] = '<i>convert</i> output format';
$string['cookiehttponly'] = 'Only http cookies';
$string['cookiesecure'] = 'Secure cookies only';
$string['country'] = 'Default country';
Expand Down Expand Up @@ -622,8 +620,6 @@
$string['langpackwillbeupdated'] = 'NOTE: Moodle will try to download updates for your language packs during the upgrade.';
$string['langstringcache'] = 'Cache all language strings';
$string['languagesettings'] = 'Language settings';
$string['latexpreamble'] = 'LaTeX preamble';
$string['latexsettings'] = 'LaTeX renderer Settings';
$string['latinexcelexport'] = 'Excel encoding';
$string['legacyfilesaddallowed'] = 'Allow adding to legacy course files';
$string['legacyfilesaddallowed_help'] = 'If a course has legacy course files, allow new files and folders to be added to it.';
Expand Down Expand Up @@ -787,9 +783,6 @@
$string['order4'] = 'Fourth';
$string['passwordpolicy'] = 'Password policy';
$string['passwordresettime'] = 'Maximum time to validate password reset request';
$string['pathconvert'] = 'Path of <i>convert</i> binary';
$string['pathdvips'] = 'Path of <i>dvips</i> binary';
$string['pathlatex'] = 'Path of <i>latex</i> binary';
$string['pathtoclam'] = 'clam AV path';
$string['pathtodot'] = 'Path to dot';
$string['pathtodot_help'] = 'Path to dot. Probably something like /usr/bin/dot. To be able to generate graphics from DOT files, you must have installed the dot executable and point to it here. Note that, for now, this only used by the profiling features (Development->Profiling) built into Moodle.';
Expand Down

0 comments on commit 1341058

Please sign in to comment.