SimpleMathJax extension for MediaWiki and Mathoid issues

Updated on January 12, 2022

I had set up a Math extension in Mediawiki version 1.31.1 for one of my client’s websites. It was working fine for a year and all of a sudden it broke with an error: Failed to parse (MathML with SVG or PNG fallback (recommended for modern browsers and accessibility tools): Invalid response (“Math extension cannot connect to Restbase.”) from server “https://wikimedia.org/api/rest_v1/”) {displaystyle y= ax^2+bx+c}. So how did I solve this? In this tutorial, I’ll explain the various attempts to fix the error and why I finally ended up using the alternate extension called SimpleMathJax.

Attempt 1: Verifying the $wgMathFullRestbaseURL

To start with, I checked the $wgMathFullRestbaseURL variable in the LocalSettings.php file. I tried switching between possible values as shown below.

$wgMathFullRestbaseURL= 'https://en.wikipedia.org/api/rest_';

$wgMathFullRestbaseURL= 'https://wikimedia.org/api/rest_v1/';

wgMathFullRestbaseURL= 'https://api.formulasearchengine.com/v1/';

However, the error still persisted with the change of Restbase URLs in the error message.

Attempt 2: Testing the Restbase URLs with cURL

There were a few suggestions from various forums that the PHP’s cURL might be refusing the SSL connection and suggested setting CURLOPT_SSL_VERIFYHOST to 0 in ‘includes/libs/http/MultiHttpClient.php‘.

$ch = curl_init();

+                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
+                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt( $ch, CURLOPT_PROXY, $req['proxy'] ?? $this->proxy );

Though I was hesitant to do that, I still tried it out and it didn’t work.

Attempt 3: Making MathRestbaseInterface.php to accept code 0 response

And this link suggested patching the MathRestbaseInterface.php file so that it can accept code 0 response. I tried the following changes.

Opened the MathRestbaseInterface.php file and added the following line to the top.

const WikiMediaServersReturnInvalidErrorCodes = true;

Looked out for the evaluateRestbaseCheckResponse and evaluateContentResponse functions and changed the ‘if‘ condition as below:

if ( $response['code'] === 200 || WikiMediaServersReturnInvalidErrorCodes ) {

The complete patch is here.

--- MathRestbaseInterface.php 2019-08-09 19:10:54.000000000 -0500
+++ MathRestbaseInterface.php 2019-08-09 19:23:45.000000000 -0500
@@ -8,6 +8,8 @@

use MediaWiki\Logger\LoggerFactory;

+const WikiMediaServersReturnInvalidErrorCodes = true;
+
class MathRestbaseInterface {
private $hash = false;
private $tex;
@@ -382,7 +384,7 @@
*/
public function evaluateRestbaseCheckResponse( $response ) {
$json = json_decode( $response['body'] );
- if ( $response['code'] === 200 ) {
+ if ( $response['code'] === 200 || WikiMediaServersReturnInvalidErrorCodes ) {
$headers = $response['headers'];
$this->hash = $headers['x-resource-location'];
$this->success = $json->success;
@@ -439,7 +441,7 @@
* @throws MWException
*/
private function evaluateContentResponse( $type, array $response, array $request ) {
- if ( $response['code'] === 200 ) {
+ if ( $response['code'] === 200 || WikiMediaServersReturnInvalidErrorCodes ) {
if ( array_key_exists( 'x-mathoid-style', $response['headers'] ) ) {
$this->mathoidStyle = $response['headers']['x-mathoid-style'];
}
@@ -474,3 +476,4 @@
throw new MWException( "Cannot get $type. $detail" );
}
}
+

It didn’t help either. But the same thread suggested making a change in the MultiHttpClient.php file as below.

Opened the <mediawiki_install_path>/includes/libs/MultiHttpClient.php and looked out for the preg_match() inside the method getCurlHandle and changed the argument from "/^(HTTP\/1\.[01]) (\d{3}) (.*)/" to "/^(HTTP\/[12](?:\.[01])?) (\d{3}) (.*)/"

So the change looks as below.

diff MultiHttpClient_orig.php MultiHttpClient.php

392c392
< 				if ( preg_match( "/^(HTTP\/1\.[01]) (\d{3}) (.*)/", $header, $matches ) ) {
---
> 				if ( preg_match( "/^(HTTP\/[12](?:\.[01])?) (\d{3}) (.*)/", $header, $matches ) ) {

Well, the above changes didn’t work for me, though it has worked for a few. Note, the patch was suggested for the Mediawiki version 1.32 and for the HTTP servers receiving HTTP/2 responses.

Attempt 4: Installing Mathoid

Finally, someone suggested that Mathoid needs to run as a service that uses MathJax on the server-side to convert texvc input to MathML+SVG rendering and that’s the most recommended option. Note, Mathoid is used in Wikipedia for Math rendering.

However, my understanding is that if Mathoid is not used as RESTBase, then we can rely on Wikipedia’s RESTBase or Wikimedia’s RESTBase and that’s what I had done in my first attempt. Also, the MediaWiki version 1.31.1 did work all these days without the Mathoid server.

But I decided to try installing the Mathoid server on Debian version 4.8 (I know this is a pretty old version, but that’s what the client has been using for years). Mathoid is dependant on Node, but I couldn’t install it due to various other dependency errors.

So what’s the way out? I finally used the alternate Mediawiki extension called SimpleMathJax.

Attempt 5: Using SimpleMathJax with MediaWiki version 1.31.1

Followed the below steps

Step 1: Downloaded the SimpleMathJax extension and copied it to the extensions folder inside the Mediawiki installation.

Step 2: Unzipped the extension.

Step 3: Loaded the extension as shown below in LocalSettings.php file.

$ vim <mediawiki_install_path>/LocalSettings.php
wfLoadExtension( 'SimpleMathJax' );

Step 4: Tested if the extension is working properly by adding a sample <math> tag as below

<math>E=mc^2</math>

The equation and other formulas are rendered properly in the Mediawiki pages and that meant, I was finally out of the rut that troubled me for the entire day.

Was this article helpful?

Related Articles

Leave a Comment