Physikerwelt has uploaded a new change for review.

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

Change subject: WIP: highlight
......................................................................

WIP: highlight

Change-Id: I0b496b0298c212c20a301437444b4ce1c01cac19
---
M MathQueryObject.php
M MathSearch.php
M includes/engines/MathEngineBaseX.php
M includes/engines/MathEngineMws.php
M includes/engines/MathEngineRest.php
M includes/special/SpecialMathSearch.php
6 files changed, 141 insertions(+), 59 deletions(-)


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

diff --git a/MathQueryObject.php b/MathQueryObject.php
index d234fd3..0fec99e 100644
--- a/MathQueryObject.php
+++ b/MathQueryObject.php
@@ -230,7 +230,7 @@
                        $this->cquery = $renderer->getMathml();
                        return $this->cquery;
                } else {
-                       wfDebugLog( 'math', 'error during geration of query 
string' . $renderer->getLastError() );
+                       MWLoggerFactory::getInstance( 'Math' )->debug( 'error 
during geration of query string' . $renderer->getLastError() );
                }
        }
 
diff --git a/MathSearch.php b/MathSearch.php
index 493d23c..555a219 100644
--- a/MathSearch.php
+++ b/MathSearch.php
@@ -74,7 +74,7 @@
 $wgHooks['UnitTestsList'][] = 'MathSearchHooks::onRegisterUnitTests';
 $wgHooks['ParserFirstCallInit'][] = 'MathSearchHooks::onParserFirstCallInit';
 
-$wgMathSearchBaseXBackendUrl = 'http://localhost:10043/mwsquery';
+$wgMathSearchBaseXBackendUrl = 'http://localhost:10043/';
 
 /* Optional stuff for math search competetion server */
 $wgMathWmcServer = false;
diff --git a/includes/engines/MathEngineBaseX.php 
b/includes/engines/MathEngineBaseX.php
index 28b454e..7ffe00b 100644
--- a/includes/engines/MathEngineBaseX.php
+++ b/includes/engines/MathEngineBaseX.php
@@ -15,7 +15,6 @@
                parent::__construct( $query, $wgMathSearchBaseXBackendUrl );
        }
 
-
        /**
         * @param SimpleXMLElement $xmlRoot
         */
@@ -32,4 +31,12 @@
                }
                $this->relevanceMap = array_unique( $this->relevanceMap );
        }
+
+       /**
+        *
+        *
+        */
+       function getPostData( $numProcess ){
+               return json_encode( array( "type" => $this->type, "query" => 
$this->query ) );
+       }
 }
\ No newline at end of file
diff --git a/includes/engines/MathEngineMws.php 
b/includes/engines/MathEngineMws.php
index 2cad67f..17a763e 100644
--- a/includes/engines/MathEngineMws.php
+++ b/includes/engines/MathEngineMws.php
@@ -34,7 +34,7 @@
                        }
                        $this->resultSet[(string) $revisionID][(string) 
$AnchorID][] = array( "xpath" => (string) $attrs["xpath"], "mappings" => 
$substarr ); // ,"original"=>$page->asXML()
                }
-               $this->relevanceMap = array_unique( $this->relevanceMap );
+
        }
 
 }
diff --git a/includes/engines/MathEngineRest.php 
b/includes/engines/MathEngineRest.php
index 531e196..6ab8452 100644
--- a/includes/engines/MathEngineRest.php
+++ b/includes/engines/MathEngineRest.php
@@ -12,6 +12,7 @@
 abstract class MathEngineRest {
        /** @var MathQueryObject the query to be answered*/
        protected $query;
+       protected $type = "mws";
        protected $size = false;
        protected $resultSet = array();
        protected $relevanceMap = array();
@@ -78,13 +79,7 @@
        function postQuery() {
                global $wgMathDebug;
                $numProcess = 30000;
-               if ( $this->query->getXQuery() ){
-                       $postData = $this->query->getXQuery();
-               } else {
-                       $tmp = str_replace( "answsize=\"30\"", 
"answsize=\"$numProcess\" totalreq=\"yes\"", $this->getQuery()->getCQuery() );
-                       $postData = str_replace( "m:", "", $tmp );
-                       if ( $wgMathDebug ) { wfDebugLog( 'MathSearch', 'MWS 
query:' . $postData ); }
-               }
+               $postData = $this->getPostData( $numProcess );
                $res = Http::post( $this->backendUrl, array( "postData" => 
$postData, "timeout" => 60 ) );
                if ( $res == false ) {
                        if ( function_exists( 'curl_init' ) ) {
@@ -103,16 +98,53 @@
                                        var_export( $details, true ) );
                        return false;
                }
-               try{
+               return $this->processResults( $res, $numProcess );
+       }
+
+       /**
+        * @param SimpleXMLElement $xmlRoot
+        */
+       abstract function processMathResults( $xmlRoot ) ;
+
+       /**
+        * @param $numProcess
+        * @return mixed|string
+        */
+       protected function getPostData( $numProcess ) {
+               global $wgMathDebug;
+               if ( $this->query->getXQuery() ) {
+                       $postData = $this->query->getXQuery();
+                       return $postData;
+               } else {
+                       $tmp =
+                               str_replace( "answsize=\"30\"", 
"answsize=\"$numProcess\" totalreq=\"yes\"",
+                                       $this->getQuery()->getCQuery() );
+                       $postData = str_replace( "m:", "", $tmp );
+                       if ( $wgMathDebug ) {
+                               wfDebugLog( 'MathSearch', 'MWS query:' . 
$postData );
+                               return $postData;
+                       }
+                       return $postData;
+               }
+       }
+
+       /**
+        * @param $res
+        * @param $numProcess
+        * @return bool
+        */
+       protected function processResults( $res, $numProcess ) {
+               try {
                        $xres = new SimpleXMLElement( $res );
-               } catch (Exception $e){
-                       wfDebugLog( 'MathSearch', "No valid XMLRESUSLT" . $res);
+               }
+               catch ( Exception $e ) {
+                       wfDebugLog( 'MathSearch', "No valid XMLRESUSLT" . $res 
);
                        return false;
                }
 
-               $this->size = (int) $xres["total"];
+               $this->size = (int)$xres["total"];
                wfDebugLog( "MathSearch", $this->size . " results retreived 
from $this->backendUrl." );
-               if ($this->size == 0) {
+               if ( $this->size == 0 ) {
                        return true;
                }
                $this->relevanceMap = array();
@@ -121,11 +153,13 @@
                if ( $this->size >= $numProcess ) {
                        ini_set( 'memory_limit', '256M' );
                        for ( $i = $numProcess; $i <= $this->size; $i += 
$numProcess ) {
-                               $query = str_replace( "limitmin=\"0\" ", 
"limitmin=\"$i\" ", $postData );
-                               $res = Http::post( $this->backendUrl, array( 
"postData" => $query, "timeout" => 60 ) );
+                               $query = str_replace( "limitmin=\"0\" ", 
"limitmin=\"$i\" ", $this->postData );
+                               $res =
+                                       Http::post( $this->backendUrl, array( 
"postData" => $query, "timeout" => 60 ) );
                                wfDebugLog( 'mathsearch', 'MWS query:' . $query 
);
                                if ( $res == false ) {
-                                       wfDebugLog( "MathSearch", "Nothing 
retreived from $this->backendUrl. check if mwsd is running there" );
+                                       wfDebugLog( "MathSearch",
+                                               "Nothing retreived from 
$this->backendUrl. check if mwsd is running there" );
                                        return false;
                                }
                                $xres = new SimpleXMLElement( $res );
@@ -136,8 +170,17 @@
        }
 
        /**
-        * @param SimpleXMLElement $xmlRoot
+        * @return mixed
         */
-       abstract function processMathResults( $xmlRoot ) ;
+       public function getType() {
+               return $this->type;
+       }
+
+       /**
+        * @param string $type
+        */
+       public function setType( $type ) {
+               $this->type = $type;
+       }
 
 }
diff --git a/includes/special/SpecialMathSearch.php 
b/includes/special/SpecialMathSearch.php
index 35c9a7a..fcc2657 100644
--- a/includes/special/SpecialMathSearch.php
+++ b/includes/special/SpecialMathSearch.php
@@ -216,14 +216,20 @@
                /**
                 * @param $revisionID
                 * @param $mathElements
-                * @param $out
                 * @param $pagename
+                * @internal param $out
                 */
-               public function displayMathElements( $revisionID, 
$mathElements, $out, $pagename ) {
+               public function displayMathElements( $revisionID, 
$mathElements, $pagename ) {
+                       $out = $this->getOutput();
                        global $wgMathDebug;
                        foreach ( $mathElements as $anchorID => $answ ) {
                                $res = MathObject::constructformpage( 
$revisionID, $anchorID );
-                               if ( $res ) {
+                               if ( !$res ) {
+                                       wfDebugLog( "MathSearch",
+                                               "Failure: Could not get entry 
$anchorID for page $pagename (id $revisionID) :" .
+                                               var_export( $this->mathResults, 
TRUE ) );
+                                       return;
+                               }
                                        $mml = $res->getMathml();
                                        $out->addWikiText( 
"====[[$pagename#$anchorID|Eq: $anchorID (Result " .
                                                                           
$this->resultID ++ . ")]]====", false );
@@ -235,6 +241,8 @@
                                                str_replace( 
'/m:semantics/m:annotation-xml[@encoding="MathML-Content"]',
                                                        '', $xpath );
                                        $dom = new DOMDocument;
+                                       $dom->preserveWhiteSpace = false;
+                                       $dom->validateOnParse = true;
                                        $dom->loadXML( $mml );
                                        $DOMx = new DOMXpath( $dom );
                                        $hits = $DOMx->query( $xpath );
@@ -243,52 +251,77 @@
                                        }
                                        if ( !is_null( $hits ) && $hits ) {
                                                foreach ( $hits as $node ) {
-                                                       /* @var DOMDocument 
$node */
-                                                       if ( 
$node->hasAttributes() ) {
-                                                               try {
-                                                                       $domRes 
=
-                                                                               
$dom->getElementById( $node->attributes->getNamedItem( 'xref' )->nodeValue );
-                                                               }
-                                                               catch ( 
Exception $e ) {
-                                                                       
wfDebugLog( 'MathSearch',
-                                                                               
'Problem getting references ' . $e->getMessage() );
-                                                                       $domRes 
= false;
-                                                               }
-                                                               if ( $domRes ) {
-                                                                       
$domRes->setAttribute( 'mathcolor', '#cc0000' );
-                                                                       
$out->addHtml( $domRes->ownerDocument->saveXML() );
-                                                               } else {
-                                                                       
$out->addHTML( $node->ownerDocument->saveXML() );
-                                                               }
-                                                       } else {
-                                                               $renderer = new 
MathMathML();
-                                                               
$renderer->setMathml( $mml );
-                                                               $out->addHtml( 
$renderer->getHtmlOutput() );
-                                                       }
+                                                       $this->highlightHit( 
$node, $dom, $mml );
                                                }
-                                       } else {
-                                               $renderer = new MathMathML();#
-                                               $renderer->setMathml( $mml );
-                                               $out->addHtml( 
$renderer->getHtmlOutput() );
                                        }
-                               } else {
-                                       wfDebugLog( "MathSearch",
-                                               "Failure: Could not get entry 
$anchorID for page $pagename (id $revisionID) :" .
-                                               var_export( $this->mathResults, 
TRUE ) );
+                               $renderer = new MathMathML( $mml, array( 'type' 
=> 'pmml' ) );
+                               $renderer->setMathml( $mml );
+                               $renderer->render();
+                               $out->addHtml( $renderer->getHtmlOutput() );
                                }
                        }
+
+               /**
+                * Note that the default getElementById function
+                * <code>
+                *  $dom->getElementById( $id );
+                * </code>
+                * works for "xml:id" only,  but not for "id" which is extended 
to "math:id"
+                *      TODO: could be fixed with
+                * @link 
http://php.net/manual/de/domdocument.getelementbyid.php#86056
+                * @param String $id
+                * @param DOMDocument $doc
+                * @return DOMElement
+                */
+               private function getElementById($id,$doc){
+                               $xpath = new DOMXPath($doc);
+                               return $xpath->query("//*[@id='$id']")->item(0);
                }
 
                /**
                 * @param MathSearchTerm $term
                 */
                public function printTerm( $term ) {
+                       if ( $term->getType() == MathSearchTerm::TYPE_MATH ){
+                               $expr = "<math>{$term->getExpr()}</math>";
+                       } else {
+                               $expr = "<code>{$term->getExpr()}</code>";
+                       }
                        $this->getOutput()->addWikiMsg( 'math-search-term',
                                $term->getKey(),
-                               $term->getExpr(),
+                               $expr,
                                wfMessage( 
"math-search-type-{$term->getType()}")->text(),
                                $term->getRel() == '' ? '' : wfMessage( 
"math-search-relation-{$term->getRel()}")->text(),
                                sizeof( $term->getRelevanceMap() ) );
+                       //TODO: Load math syles.
+                       $this->getOutput()->addWikiText($expr);
+               }
+
+               /**
+                * @param DOMNode $node
+                * @param DOMDocument $dom
+                * @param string $mml
+                */
+               protected function highlightHit( $node, $dom, &$mml ) {
+                       if( $node == null || !$node->hasAttributes() ){
+                               return;
+                       }
+                       try {
+                               $xRef = $node->attributes->getNamedItem( 'xref' 
);
+                               if ( $xRef ) {
+                                       $domRes = $this->getElementById( 
$xRef->nodeValue, $dom );
+                                       if ( $domRes ) {
+                                               $domRes->setAttribute( 
'mathcolor', '#cc0000' );
+                                               $mml = 
$domRes->ownerDocument->saveXML();
+                                       }
+                               } else {
+                                       // CMML node has no corresponding PMML 
element
+                                       $fallback = $node->parentNode;
+                                       $this->highlightHit( $fallback, $dom, 
$mml );
+                               }
+                       } catch ( Exception $e ) {
+                               wfDebugLog( 'MathSearch', 'Problem highlighting 
hit ' . $e->getMessage() );
+                       }
                }
 
                /**
@@ -312,7 +345,7 @@
                function displayRevisionResults( $revisionID ) {
                        $out = $this->getOutput();
                        $revision = Revision::newFromId( $revisionID );
-                       if ( $revision === false ) {
+                       if ( !$revision ) {
                                wfDebugLog( "MathSearch", "invalid revision 
number" );
                                return false;
                        }
@@ -336,7 +369,7 @@
                                $out->addWikiText( $textResult );
                        }
                        wfDebugLog( "MathSearch", "Processing results for 
$pagename" );
-                       $this->displayMathElements( $revisionID, $mathElements, 
$out, $pagename );
+                       $this->displayMathElements( $revisionID, $mathElements, 
$pagename );
                        return true;
                }
 
@@ -344,8 +377,7 @@
                 * Renders the math search input to mathml
                 * @return boolean
                 */
-               function render()
-               {
+               function render() {
                        $renderer = new MathLaTeXML( $this->mathpattern );
                        $renderer->setLaTeXMLSettings( 'profile=mwsquery' );
                        $renderer->setAllowedRootElments( array( 'query' ) );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0b496b0298c212c20a301437444b4ce1c01cac19
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