Physikerwelt has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/252652

Change subject: Add mathoid interface
......................................................................

Add mathoid interface

Change-Id: Id4f84ea65f23bf81dc22544fe5d3a452022bd978
---
M extension.json
A includes/MathoidDriver.php
A tests/MathoidDriverTest.php
3 files changed, 205 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MathSearch 
refs/changes/52/252652/1

diff --git a/extension.json b/extension.json
index d5fa24c..8b3cf6b 100644
--- a/extension.json
+++ b/extension.json
@@ -26,7 +26,8 @@
                "SpecialLaTeXTranslator": 
"includes/special/SpecialLaTeXTranslator.php",
                "LaTeXTranslator": "includes/LaTeXTranslator.php",
                "SpecialUploadResult": "SpecialUploadResult.php",
-               "SpecialMathDownloadResult": "SpecialMathDownloadResult.php"
+               "SpecialMathDownloadResult": "SpecialMathDownloadResult.php",
+               "MathoidDriver": "includes/MathoidDriver.php"
        },
        "AvailableRights": [
                "mathwmcsubmit"
diff --git a/includes/MathoidDriver.php b/includes/MathoidDriver.php
new file mode 100644
index 0000000..f2f3a7e
--- /dev/null
+++ b/includes/MathoidDriver.php
@@ -0,0 +1,154 @@
+<?php
+use MediaWiki\Logger\LoggerFactory;
+
+class MathoidDriver {
+       private $success;
+       private $checked;
+       private $identifiers;
+       private $requiredPackages;
+       private $q;
+       private $version;
+       private $error;
+
+       /**
+        * @return mixed
+        */
+       public function getVersion() {
+               return $this->version;
+       }
+
+       /**
+        * @return mixed
+        */
+       public function getError() {
+               return $this->error;
+       }
+
+       /**
+        * MathoidDriver constructor.
+        * @param $q
+        */
+       function __construct( $q = '' ) {
+               $this->q = $q;
+       }
+
+       /**
+        * @return mixed
+        */
+       public function getSuccess() {
+               return $this->success;
+       }
+
+       /**
+        * @return mixed
+        */
+       public function getChecked() {
+               return $this->checked;
+       }
+
+       /**
+        * @return mixed
+        */
+       public function getIdentifiers() {
+               return $this->identifiers;
+       }
+
+       /**
+        * @return mixed
+        */
+       public function getRequiredPackages() {
+               return $this->requiredPackages;
+       }
+
+       /**
+        *
+        * @return MathQueryObject
+        */
+       public function getQuery() {
+               return $this->query;
+       }
+
+       public function texvcInfo() {
+               return $this->processResults( self::doPost( 
$this->getBackendUrl() . "/texvcinfo",
+                       $this->getPostData() ) );
+       }
+
+       protected function processResults( $res ) {
+               $jsonResult = json_decode( $res );
+               if ( $jsonResult &&
+                       json_last_error() === JSON_ERROR_NONE &&
+                       isset( $jsonResult->texvcinfo )
+               ) {
+                       $texvcinfo = $jsonResult->texvcinfo;
+                       $this->success = $texvcinfo->success;
+                       if ( $this->success ) {
+                               $this->checked = $texvcinfo->checked;
+                               $this->identifiers = $texvcinfo->identifiers;
+                               $this->requiredPackages = 
$texvcinfo->requiredPackages;
+                       } else {
+                               $this->error = $texvcinfo->error;
+                       }
+                       return true;
+               } else {
+                       return false;
+               }
+       }
+
+       protected static function doPost( $url, $postData ) {
+               $options = array(
+                       "postData" => $postData,
+                       "timeout"  => 60,
+                       "method"   => 'POST'
+               );
+               $req = MWHttpRequest::factory( $url, $options, __METHOD__ );
+               $status = $req->execute();
+
+               if ( $status->isOK() ) {
+                       return $req->getContent();
+               } else {
+                       $errors = $status->getErrorsByType( 'error' );
+                       $logger = LoggerFactory::getInstance( 'http' );
+                       $logger->warning( $status->getWikiText(),
+                               array( 'error' => $errors, 'caller' => 
__METHOD__, 'content' => $req->getContent() ) );
+                       return false;
+               }
+       }
+
+       /**
+        * @return string
+        */
+       public function getBackendUrl() {
+               $config = ConfigFactory::getDefaultInstance()->makeConfig( 
'main' );
+               return $config->get( "MathMathMLUrl" );
+       }
+
+       protected function getPostData() {
+               $post = array(
+                       "q" => $this->q
+               );
+               return wfArrayToCgi( $post );
+       }
+
+       public function checkBackend() {
+               $res = HTTP::get( $this->getBackendUrl().'/_info' );
+               if ( $res ) {
+                       $res = json_decode( $res );
+                       if ( $res && json_last_error() === JSON_ERROR_NONE ) {
+                               if ( isset( $res->name ) && $res->name === 
'mathoid' ) {
+                                       $this->version = $res->version;
+                                       // Mathoid 0.2.9 is only version 
currently supported
+                                       if ( $this->version === "0.2.9" ) {
+                                               return true;
+                                       } else {
+                                               return false;
+                                       }
+                               }
+                       }
+               }
+               $logger = LoggerFactory::getInstance( 'MathSearch' );
+               $logger->warning( "Mathoid server backend does not point to 
mathoid.", array(
+                       'detail' => $res ) );
+               return false;
+
+       }
+}
diff --git a/tests/MathoidDriverTest.php b/tests/MathoidDriverTest.php
new file mode 100644
index 0000000..22f6a44
--- /dev/null
+++ b/tests/MathoidDriverTest.php
@@ -0,0 +1,49 @@
+<?php
+
+/**
+ * MediaWiki MathSearch extension
+ *
+ * (c)2015 Moritz Schubotz
+ * GPLv2 license; info in main package.
+ *
+ * @group MathSearch
+ * Class MathSearchHooksTest
+ */
+class MathoidDriverTest extends MediaWikiTestCase {
+
+       private static $hasMathoid;
+
+
+       public static function setUpBeforeClass() {
+               $m = new MathoidDriver();
+               self::$hasMathoid = $m->checkBackend();
+       }
+
+       /**
+        * Sets up the fixture, for example, opens a network connection.
+        * This method is called before a test is executed.
+        */
+       protected function setUp() {
+               parent::setUp();
+
+               if ( !self::$hasMathoid ) {
+                       $this->markTestSkipped( "No compatible Mathoid server 
configured." );
+               }
+       }
+
+       public function testSuccess() {
+               $m = new MathoidDriver( '\\sin(x^2)' );
+               $this->assertTrue( $m->texvcInfo() );
+               $this->assertTrue( $m->getSuccess() );
+               $this->assertEquals( '\\sin(x^{2})', $m->getChecked() );
+               $this->assertEquals( array( 'x' ), $m->getIdentifiers() );
+               $this->assertEquals( array(), $m->getRequiredPackages() );
+       }
+
+       public function testFail() {
+               $m = new MathoidDriver( '\\sin(\\invalid)' );
+               $this->assertTrue( $m->texvcInfo() );
+               $this->assertFalse( $m->getSuccess() );
+               $this->assertObjectHasAttribute( 'message', $m->getError() );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/252652
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id4f84ea65f23bf81dc22544fe5d3a452022bd978
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MathSearch
Gerrit-Branch: master
Gerrit-Owner: Physikerwelt <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to