Physikerwelt has uploaded a new change for review.

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

Change subject: Alternative Hook for Math Postprocessing
......................................................................

Alternative Hook for Math Postprocessing

* add an alternative Hook for postprocessing

Change-Id: Idb60f03af51f291198585e82a3b68fb2475cd387
---
M FormulaInfo.php
M MathSearch.hooks.php
M tests/MathDB2ConnectionTest.php
A tests/MathSearchHooksTest.php
M tests/MathXQueryTest.php
5 files changed, 189 insertions(+), 55 deletions(-)


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

diff --git a/FormulaInfo.php b/FormulaInfo.php
index c3ccf0a..85556ae 100644
--- a/FormulaInfo.php
+++ b/FormulaInfo.php
@@ -50,7 +50,7 @@
                if ( $allPages ) {
                        $this->DisplayInfo( $allPages[0]->getPageID(), 
$allPages[0]->getAnchorID() );
                } else {
-                       $wgOut->addWikiText( "No occurences found clean up the 
database to remove unused formulae" );
+                       $wgOut->addWikiText( "No occurrences found clean up the 
database to remove unused formulae" );
                }
        }
        public function DisplayInfo( $pid, $eid ) {
@@ -71,7 +71,7 @@
 
                /* @var $mo MathObject  */
                $mo = MathObject::constructformpage( $pid, $eid );
-               $out->addWikiText( "Occurences on the following pages:" );
+               $out->addWikiText( "Occurrences on the following pages:" );
                wfDebugLog( "MathSearch", var_export( $mo->getAllOccurences(), 
true ) );
                // $wgOut->addWikiText('<b>:'.var_export($res,true).'</b>');
                $out->addWikiText( 'TeX (as stored in database): 
<syntaxhighlight lang="latex">' . $mo->getTex() . '</syntaxhighlight>' );
@@ -89,7 +89,7 @@
                $out->addWikiText( 'Hash : ' . $mo->getMd5(), false );
                $out->addHtml( "<br />" );
                $out->addWikiText( '==Similar pages==' );
-               $out->addWikiText( 'Calculataed based on the variables occuring 
on the entire ' . $pagename . ' page' );
+               $out->addWikiText( 'Calculated based on the variables occurring 
on the entire ' . $pagename . ' page' );
                $mo->findSimilarPages( $pid );
                $out->addWikiText( '==Variables==' );
                $mo->getObservations();
diff --git a/MathSearch.hooks.php b/MathSearch.hooks.php
index 41284ac..d6a93f1 100644
--- a/MathSearch.hooks.php
+++ b/MathSearch.hooks.php
@@ -55,48 +55,59 @@
        }
 
        /**
-        * Callback function that is called after a formula was rendered
+        * Updates the formula index in the database
         *
-        * @param $content
-        * @param $attributes
-        * @param $parser Parser
-        * @return boolean (true)
+        * @param int $pid Page-ID
+        * @param int $eid Equation-ID (get updated incrementally for every 
math element on the page)
+        * @param string $inputHash hash of tex string (used as database entry)
+        * @param string $tex the user input hash
         */
-       static function onMathFormulaRendered( MathRenderer $Renderer, &$Result 
= null, $pid = 0, $eid = 0 ) {
-               if ( $pid > 0 ) { // Only store something if a pageid was set.
-                       try {
+       private static function updateIndex($pid, $eid, $inputHash, $tex){
+               try {
                        $dbr = wfGetDB( DB_SLAVE );
                        $exists = $dbr->selectRow( 'mathindex',
                                array( 'mathindex_page_id', 'mathindex_anchor', 
'mathindex_inputhash' ),
                                array(
                                        'mathindex_page_id' => $pid,
                                        'mathindex_anchor' => $eid,
-                                       'mathindex_inputhash' => 
$Renderer->getInputHash() )
-                                       ) ;
+                                       'mathindex_inputhash' => $inputHash)
+                       ) ;
                        if ( $exists ) {
-                               wfDebugLog( "MathSearch", 'Index $' . 
$Renderer->getTex() . '$ already in database.' );
+                               wfDebugLog( "MathSearch", 'Index $' . $tex . '$ 
already in database.' );
                        } else {
-                               wfDebugLog( "MathSearch", 'Store index for $' . 
$Renderer->getTex() . '$ in database' );
+                               wfDebugLog( "MathSearch", 'Store index for $' . 
$tex . '$ in database' );
                                $dbw = wfGetDB( DB_MASTER );
-                               $inputhash = $Renderer->getInputHash();
                                $dbw->onTransactionIdle(
-                                               function () use ( $pid, $eid, 
$inputhash, $dbw ) {
-                                                       $dbw->replace( 
'mathindex',
+                                       function () use ( $pid, $eid, 
$inputHash, $dbw ) {
+                                               $dbw->replace( 'mathindex',
                                                        array( 
'mathindex_page_id', 'mathindex_anchor' ),
                                                        array(
                                                                
'mathindex_page_id' => $pid,
                                                                
'mathindex_anchor' =>  $eid ,
-                                                               
'mathindex_inputhash' => $inputhash
+                                                               
'mathindex_inputhash' => $inputHash
                                                        ) );
-                                               }
+                                       }
                                );
-                               }
-                       } catch ( Exception $e ) {
-                               wfDebugLog( "MathSearch", 'Problem writing to 
math index!'
-                                       . ' You might want the rebuild the 
index by running:'
-                                       . '"php 
extensions/MathSearch/ReRenderMath.php". The error is'
-                                       . $e->getMessage() );
                        }
+               } catch ( Exception $e ) {
+                       wfDebugLog( "MathSearch", 'Problem writing to math 
index!'
+                               . ' You might want the rebuild the index by 
running:'
+                               . '"php 
extensions/MathSearch/ReRenderMath.php". The error is'
+                               . $e->getMessage() );
+               }
+       }
+
+       /**
+        * Callback function that is called after a formula was rendered
+        * @param MathRenderer $Renderer
+        * @param string|null $Result reference to the rendering result
+        * @param int $pid
+        * @param int $eid
+        * @return bool
+        */
+       static function onMathFormulaRendered( MathRenderer $Renderer, &$Result 
= null, $pid = 0, $eid = 0 ) {
+               if ( $pid > 0 ) { // Only store something if a pageid was set.
+                       self::updateIndex( $pid , $eid , 
$Renderer->getInputHash() , $Renderer->getTex() );
                }
                $url = SpecialPage::getTitleFor( 'FormulaInfo' )->getLocalUrl( 
array( 'pid' => $pid, 'eid' => $eid ) );
                $mo = MathObject::cloneFromRenderer($Renderer);
@@ -108,6 +119,34 @@
        }
 
        /**
+        * Alternative Callback function that is called after a formula was 
rendered
+        * used for test corpus generation for NTCIR11 Math-2
+        * You can enable this alternative hook via setting
+        * <code>$wgHooks['MathFormulaRendered'] = 
array('MathSearchHooks::onMathFormulaRenderedNoLink');</code>
+        * in your local settings
+        *
+        * @param MathRenderer $Renderer
+        * @param null $Result
+        * @param int $pid
+        * @param int $eid
+        * @internal param $content
+        * @internal param $attributes
+        * @internal param \Parser $parser
+        * @return boolean (true)
+        */
+       static function onMathFormulaRenderedNoLink( MathRenderer $Renderer, 
&$Result = null, $pid = 0, $eid = 0 ) {
+               if ( $pid > 0 ) { // Only store something if a pageid was set.
+                       self::updateIndex( $pid, $eid, 
$Renderer->getInputHash(), $Renderer->getTex() );
+               }
+               if ( preg_match( '#<math(.*)?\sid="(?P<id>[\w\.]+)"#', $Result, 
$matches ) ) {
+                       $oldId = $matches['id'];
+                       $newID = "math.$pid.$eid";
+                       $Result = str_replace( $oldId, $newID, $Result );
+               }
+               return true;
+       }
+
+       /**
         * Links to the unit test files for the test cases.
         *
         * @param string $files
diff --git a/tests/MathDB2ConnectionTest.php b/tests/MathDB2ConnectionTest.php
index 4008ea9..45c17f9 100644
--- a/tests/MathDB2ConnectionTest.php
+++ b/tests/MathDB2ConnectionTest.php
@@ -1,10 +1,11 @@
 <?php
+
 /**
  * Test the db2 access of  MathSearch.
-*
-* @group MathSearch
-* @group Database
-*/
+ *
+ * @group MathSearch
+ * @group Database
+ */
 class MathD2ConnectionTest extends MediaWikiTestCase {
        public function testConnect() {
                global $wgMathSearchDB2ConnStr;
@@ -29,18 +30,19 @@
                        echo $message;
                }
        }
-       public  function testBasicXQuery(){
+
+       public function testBasicXQuery() {
                global $wgMathSearchDB2ConnStr;
                if ( ! MathSearchHooks::isDB2Supported() || true ) {
                        $this->markTestSkipped( 'DB2 php client is not 
installed.' );
                }
-               $conn = db2_connect($wgMathSearchDB2ConnStr, '', '');
-               $stmt = db2_exec($conn,'xquery declare default element 
namespace "http://www.w3.org/1998/Math/MathML";;
+               $conn = db2_connect( $wgMathSearchDB2ConnStr, '', '' );
+               $stmt = db2_exec( $conn, 'xquery declare default element 
namespace "http://www.w3.org/1998/Math/MathML";;
  for $m in db2-fn:xmlcolumn("math.math_mathml") return
 for $x in $m//*:\'apply\'[*[1]/name() = \'eq\'] return
-data($m/*[1]/@alttext)');
-               while($row = db2_fetch_row($stmt)){
-                       echo db2_result($stmt,0);
+data($m/*[1]/@alttext)' );
+               while ( $row = db2_fetch_row( $stmt ) ) {
+                       echo db2_result( $stmt, 0 );
 
                }
        }
diff --git a/tests/MathSearchHooksTest.php b/tests/MathSearchHooksTest.php
new file mode 100644
index 0000000..c161e63
--- /dev/null
+++ b/tests/MathSearchHooksTest.php
@@ -0,0 +1,90 @@
+<?php
+
+/**
+ * MediaWiki MathSearch extension
+ *
+ * (c)2014 Moritz Schubotz
+ * GPLv2 license; info in main package.
+ *
+ * @group MathSearch
+ * Class MathSearchHooksTest
+ */
+class MathSearchHooksTest extends MediaWikiTestCase {
+
+       private $mathMLSample = <<<EOT
+<math xref="p1.1.m1.1.cmml" xml:id="p1.1.m1.1" display="inline" 
alttext="{\displaystyle\min\left(1;\exp\left(-\beta\cdot\Delta 
E\right)\right)}" class="ltx_Math" id="p1.1.m1.1" 
xmlns="http://www.w3.org/1998/Math/MathML";>
+  <semantics xref="p1.1.m1.1.cmml" xml:id="p1.1.m1.1a">
+    <mrow xref="p1.1.m1.1.14.cmml" xml:id="p1.1.m1.1.14">
+      <mo xref="p1.1.m1.1.1.cmml" xml:id="p1.1.m1.1.1" movablelimits="false" 
title="not set">min</mo>
+      <mo xref="p1.1.m1.1.14.cmml" xml:id="p1.1.m1.1.14a" title="not 
set">&ApplyFunction;</mo>
+      <mrow xref="p1.1.m1.1.14.1.cmml" xml:id="p1.1.m1.1.14.1">
+        <mo xref="p1.1.m1.1.14.1.cmml" xml:id="p1.1.m1.1.14.1a" title="not 
set">(</mo>
+        <mrow xref="p1.1.m1.1.14.1.cmml" xml:id="p1.1.m1.1.14.1b">
+          <mn xref="p1.1.m1.1.3.cmml" xml:id="p1.1.m1.1.3">1</mn>
+          <mo xref="p1.1.m1.1.14.1.cmml" xml:id="p1.1.m1.1.14.1c" title="not 
set">;</mo>
+          <mrow xref="p1.1.m1.1.14.1.2.cmml" xml:id="p1.1.m1.1.14.1.2">
+            <mi xref="p1.1.m1.1.5.cmml" xml:id="p1.1.m1.1.5" title="not 
set">exp</mi>
+            <mo xref="p1.1.m1.1.14.1.2.cmml" xml:id="p1.1.m1.1.14.1.2a" 
title="not set">&ApplyFunction;</mo>
+            <mrow xref="p1.1.m1.1.14.1.2.cmml" xml:id="p1.1.m1.1.14.1.2b">
+              <mo xref="p1.1.m1.1.14.1.2.cmml" xml:id="p1.1.m1.1.14.1.2c" 
title="not set">(</mo>
+              <mrow xref="p1.1.m1.1.14.1.2.1.cmml" xml:id="p1.1.m1.1.14.1.2.1">
+
+                <mo xref="p1.1.m1.1.7.cmml" xml:id="p1.1.m1.1.7" title="not 
set">-</mo>
+
+                <mrow xref="p1.1.m1.1.14.1.2.1.1.cmml" 
xml:id="p1.1.m1.1.14.1.2.1.1">
+                  <mrow xref="p1.1.m1.1.14.1.2.1.1.2.cmml" 
xml:id="p1.1.m1.1.14.1.2.1.1.2">
+                    <mi xref="p1.1.m1.1.8.cmml" xml:id="p1.1.m1.1.8" 
title="not set">&beta;</mi>
+                    <mo xref="p1.1.m1.1.9.cmml" xml:id="p1.1.m1.1.9" 
title="not set">&sdot;</mo>
+                    <mi xref="p1.1.m1.1.10.cmml" xml:id="p1.1.m1.1.10" 
mathvariant="normal" title="not set">&Delta;</mi>
+                  </mrow>
+                  <mo xref="p1.1.m1.1.14.1.2.1.1.1.cmml" 
xml:id="p1.1.m1.1.14.1.2.1.1.1" title="not set">&InvisibleTimes;</mo>
+                  <mi xref="p1.1.m1.1.11.cmml" xml:id="p1.1.m1.1.11" 
title="not set">E</mi>
+                </mrow>
+              </mrow>
+              <mo xref="p1.1.m1.1.14.1.2.cmml" xml:id="p1.1.m1.1.14.1.2d" 
title="not set">)</mo>
+            </mrow>
+          </mrow>
+        </mrow>
+        <mo xref="p1.1.m1.1.14.1.cmml" xml:id="p1.1.m1.1.14.1d" title="not 
set">)</mo>
+      </mrow>
+    </mrow>
+    <annotation-xml xref="p1.1.m1.1" xml:id="p1.1.m1.1.cmml" 
encoding="MathML-Content">
+      <apply xref="p1.1.m1.1.14" xml:id="p1.1.m1.1.14.cmml">
+        <min xref="p1.1.m1.1.1" xml:id="p1.1.m1.1.1.cmml"/>
+        <apply xref="p1.1.m1.1.14.1" xml:id="p1.1.m1.1.14.1.cmml">
+          <list xml:id="p1.1.m1.1.14.1.1.cmml"/>
+          <cn xref="p1.1.m1.1.3" xml:id="p1.1.m1.1.3.cmml" 
type="integer">1</cn>
+          <apply xref="p1.1.m1.1.14.1.2" xml:id="p1.1.m1.1.14.1.2.cmml">
+            <exp xref="p1.1.m1.1.5" xml:id="p1.1.m1.1.5.cmml"/>
+            <apply xref="p1.1.m1.1.14.1.2.1" xml:id="p1.1.m1.1.14.1.2.1.cmml">
+              <minus xref="p1.1.m1.1.7" xml:id="p1.1.m1.1.7.cmml"/>
+              <apply xref="p1.1.m1.1.14.1.2.1.1" 
xml:id="p1.1.m1.1.14.1.2.1.1.cmml">
+                <times xref="p1.1.m1.1.14.1.2.1.1.1" 
xml:id="p1.1.m1.1.14.1.2.1.1.1.cmml"/>
+                <apply xref="p1.1.m1.1.14.1.2.1.1.2" 
xml:id="p1.1.m1.1.14.1.2.1.1.2.cmml">
+                  <ci xref="p1.1.m1.1.9" 
xml:id="p1.1.m1.1.9.cmml">normal-&sdot;</ci>
+                  <ci xref="p1.1.m1.1.8" xml:id="p1.1.m1.1.8.cmml">&beta;</ci>
+                  <ci xref="p1.1.m1.1.10" 
xml:id="p1.1.m1.1.10.cmml">normal-&Delta;</ci>
+                </apply>
+                <ci xref="p1.1.m1.1.11" xml:id="p1.1.m1.1.11.cmml">E</ci>
+              </apply>
+            </apply>
+          </apply>
+        </apply>
+      </apply>
+    </annotation-xml>
+    <annotation xref="p1.1.m1.1.cmml" xml:id="p1.1.m1.1b" 
encoding="application/x-tex">{\displaystyle\min\left(1;\exp\left(-\beta\cdot\Delta
 E\right)\right)}</annotation>
+  </semantics>
+</math>
+EOT;
+
+       /**
+        * Tests if ID's for math elements are replaced correctly
+        */
+       public function testNTCIRHook() {
+               $dummyRenderer = new MathLaTeXML();
+               //use Page-ID = 0 to avoid that the test tries to save 
something to the DB
+               $sample = $this->mathMLSample;
+               $this->assertTrue( 
MathSearchHooks::onMathFormulaRenderedNoLink( $dummyRenderer, $sample, 0, 6 ), 
'Hook did not return true' );
+               $this->assertContains( "math.0.6.14.1.cmml", $sample, "expected 
replaced id not found" );
+       }
+} 
\ No newline at end of file
diff --git a/tests/MathXQueryTest.php b/tests/MathXQueryTest.php
index 9609217..c6635d7 100644
--- a/tests/MathXQueryTest.php
+++ b/tests/MathXQueryTest.php
@@ -1,10 +1,11 @@
 <?php
+
 /**
  * Test the db2 access of  MathSearch.
-*
-* @group MathSearch
-* @group Database
-*/
+ *
+ * @group MathSearch
+ * @group Database
+ */
 class MathXQueryTest extends MediaWikiTestCase {
 
        private $q1 = <<<'EOT'
@@ -136,43 +137,45 @@
  return
 data($m/*[1]/@alttext)
 EOT;
+
        /**
         * Searches for $E$
         */
-       public function testE(){
-               $xQuery = new XQueryGeneratorDB2($this->q1);
-               $this->assertEquals($this->r1,$xQuery->getXQuery());
+       public function testE() {
+               $xQuery = new XQueryGeneratorDB2( $this->q1 );
+               $this->assertEquals( $this->r1, $xQuery->getXQuery() );
        }
 
        /*
         * Searches for $c^2$
         */
-       public function testc2(){
-               $xQuery = new XQueryGeneratorDB2($this->q2);
-               $this->assertEquals($this->r2,$xQuery->getXQuery());
+       public function testc2() {
+               $xQuery = new XQueryGeneratorDB2( $this->q2 );
+               $this->assertEquals( $this->r2, $xQuery->getXQuery() );
        }
 
        /*
         * Searches for $\sin x$
         */
-       public function testsinx(){
-               $xQuery = new XQueryGeneratorDB2($this->q3);
-               $this->assertEquals($this->r3,$xQuery->getXQuery());
+       public function testsinx() {
+               $xQuery = new XQueryGeneratorDB2( $this->q3 );
+               $this->assertEquals( $this->r3, $xQuery->getXQuery() );
        }
 
        /*
         * Searches for $?x^2$
         */
-       public function testx2(){
-               $xQuery = new XQueryGeneratorDB2($this->qqx2);
-               $this->assertEquals($this->rqx2,$xQuery->getXQuery());
+       public function testx2() {
+               $xQuery = new XQueryGeneratorDB2( $this->qqx2 );
+               $this->assertEquals( $this->rqx2, $xQuery->getXQuery() );
        }
+
        /*
         * Searches for $?x^2+?x$
         */
-       public function testx2x(){
-               $xQuery = new XQueryGeneratorDB2($this->qqx2x);
-               $this->assertEquals($this->rqx2x,$xQuery->getXQuery());
+       public function testx2x() {
+               $xQuery = new XQueryGeneratorDB2( $this->qqx2x );
+               $this->assertEquals( $this->rqx2x, $xQuery->getXQuery() );
        }
        /*public function testBasicSQL(){
                global $wgMathSearchDB2ConnStr;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idb60f03af51f291198585e82a3b68fb2475cd387
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MathSearch
Gerrit-Branch: master
Gerrit-Owner: Physikerwelt <w...@physikerwelt.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to