Physikerwelt has uploaded a new change for review.

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

Change subject: Fix problems in MWS search mode
......................................................................

Fix problems in MWS search mode

* improve error handling
* rename pageID -> revisionID
* ignore old revisions

Change-Id: Iba23e6a0f2d00ff4d937957c1ce2ba622ea657c1
---
M MathEngineMws.php
M MathObject.php
M MathQueryObject.php
M MathSearch.hooks.php
M SpecialMathSearch.php
5 files changed, 58 insertions(+), 40 deletions(-)


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

diff --git a/MathEngineMws.php b/MathEngineMws.php
index afcb5ac..228fc17 100644
--- a/MathEngineMws.php
+++ b/MathEngineMws.php
@@ -109,17 +109,17 @@
                wfProfileIn( __METHOD__ );
                foreach ( $xmlRoot->children( "mws", TRUE ) as $page ) {
                        $attrs = $page->attributes();
-                       $uri = explode( "#", $attrs["uri"] );
-                       $pageID = $uri[0];
-                       $AnchorID = $uri[1];
-                       $this->relevanceMap[$pageID] = true;
+                       $uri = explode( ".", $attrs["uri"] );
+                       $revisionID = $uri[1];
+                       $AnchorID = $uri[2];
+                       $this->relevanceMap[$revisionID] = true;
                        $substarr = array();
                        // $this->mathResults[(string) $pageID][(string) 
$AnchorID][]=$page->asXML();
                        foreach ( $page->children( "mws", TRUE ) as $substpair 
) {
                                $substattrs = $substpair->attributes();
                                $substarr[] = array( "qvar" => (string) 
$substattrs["qvar"], "xpath" => (string) $substattrs["xpath"] );
                        }
-                       $this->resultSet[(string) $pageID][(string) 
$AnchorID][] = array( "xpath" => (string) $attrs["xpath"], "mappings" => 
$substarr ); // ,"original"=>$page->asXML()
+                       $this->resultSet[(string) $revisionID][(string) 
$AnchorID][] = array( "xpath" => (string) $attrs["xpath"], "mappings" => 
$substarr ); // ,"original"=>$page->asXML()
                }
                wfProfileOut( __METHOD__ );
        }
diff --git a/MathObject.php b/MathObject.php
index 9030d80..349357d 100644
--- a/MathObject.php
+++ b/MathObject.php
@@ -3,7 +3,7 @@
 class MathObject extends MathMathML {
 
        protected $anchorID = 0;
-       protected $pageID = 0;
+       protected $revisionID = 0;
        protected $index_timestamp = null;
        protected $dbLoadTime= 0;
        protected $mathTableName = null;
@@ -21,12 +21,12 @@
                $this->anchorID = $ID;
        }
 
-       public function getPageID() {
-               return $this->pageID;
+       public function getRevisionID() {
+               return $this->revisionID;
        }
 
-       public function setPageID( $ID ) {
-               $this->pageID = $ID;
+       public function setRevisionID( $ID ) {
+               $this->revisionID = $ID;
        }
 
        public function getIndexTimestamp() {
@@ -57,10 +57,11 @@
         */
        public static function constructformpagerow( $res ) {
                global $wgMathDebug;
-               if ( $res && $res->mathindex_page_id > 0 ) {
+               if ( $res && $res->mathindex_revision_id > 0 ) {
                        $class = get_called_class();
+                       /** @type MathObject $instance */
                        $instance = new $class;
-                       $instance->setPageID( $res->mathindex_page_id );
+                       $instance->setRevisionID( $res->mathindex_revision_id );
                        $instance->setAnchorID( $res->mathindex_anchor );
                        if ( $wgMathDebug && isset($res->mathindex_timestamp) ) 
{
                                $instance->index_timestamp = 
$res->mathindex_timestamp;
@@ -106,7 +107,7 @@
                                        'pagestat_featurecount', "count(*) as 
localcnt" ), array( "mathobservation_inputhash" => $this->getInputHash(),
                                        'varstat_featurename = 
mathobservation_featurename',
                                        'varstat_featuretype = 
mathobservation_featuretype',
-                                       'pagestat_pageid' => $this->getPageID(),
+                                       'pagestat_pageid' => 
$this->getRevisionID(),
                                        'pagestat_featureid = varstat_id'
                                )
                                , __METHOD__, array( 'GROUP BY' => 
'mathobservation_featurename',
@@ -229,7 +230,7 @@
        }
 
        public function getPageTitle() {
-               $revision = Revision::newFromId( $this->getPageID() );
+               $revision = Revision::newFromId( $this->getRevisionID() );
                if ( $revision ) {
                        return (string) $revision->getTitle();
                } else {
diff --git a/MathQueryObject.php b/MathQueryObject.php
index dddccd9..d48ec3b 100644
--- a/MathQueryObject.php
+++ b/MathQueryObject.php
@@ -70,7 +70,7 @@
        public function saveToDatabase( $overwrite = false ){
                $fields = array(
                        'qId' => $this->queryID,
-                       'oldId' => $this->getPageID(),
+                       'oldId' => $this->getRevisionID(),
                        'fId' => $this->getAnchorID(),
                        'texQuery' => $this->getTeXQuery(),
                        'qVarCount' => $this->qVarCount,
@@ -88,7 +88,7 @@
        public function exportTexDocument() {
                $texInput = htmlspecialchars( $this->getUserInputTex() );
                $texInputComment = preg_replace( "/[\n\r]/", "\n%", $texInput );
-               $title = Title::newFromId( $this->getPageID() );
+               $title = Title::newFromId( $this->getRevisionID() );
                $absUrl =
                        $title->getFullURL( array( "oldid" => 
$title->getLatestRevID() ) ) .
                        MathSearchHooks::generateMathAnchorString( 
$title->getLatestRevID(), $this->getAnchorID(), '' );
diff --git a/MathSearch.hooks.php b/MathSearch.hooks.php
index 9892e4a..fb80e08 100644
--- a/MathSearch.hooks.php
+++ b/MathSearch.hooks.php
@@ -165,7 +165,7 @@
        static function addIdentifierDescription( MathRenderer $Renderer, 
&$Result = null, $pid = 0, $eid = 0 ) {
                self::setMathId( $eid , $Renderer );
                $mo = MathObject::cloneFromRenderer($Renderer);
-               $mo->setPageID($pid);
+               $mo->setRevisionID($pid);
                $mo->setID($eid);
                $Result = preg_replace_callback("#<(mi|mo)( 
([^>].*?))?>(.*?)</\\1>#u", array( $mo , 'addIdentifierTitle' ), $Result);
                return true;
diff --git a/SpecialMathSearch.php b/SpecialMathSearch.php
index 3f6c7f2..e2a7ef9 100644
--- a/SpecialMathSearch.php
+++ b/SpecialMathSearch.php
@@ -25,11 +25,18 @@
                private $resultID = 0;
                private $xQueryEngines = array( 'db2', 'basex' );
 
+               public static function exception_error_handler($errno, $errstr, 
$errfile, $errline ) {
+                       if (!(error_reporting() & $errno)) {
+                               // This error code is not included in 
error_reporting
+                               return;
+                       }
+                       throw new ErrorException($errstr, 0, $errno, $errfile, 
$errline);
+               }
+
                /**
                 *
                 */
-               function __construct()
-               {
+               function __construct() {
                        parent::__construct( 'MathSearch' );
                }
 
@@ -51,6 +58,7 @@
                 * The main function
                 */
                public function execute( $par ) {
+                       
set_error_handler("SpecialMathSearch::exception_error_handler");
                        global $wgExtensionAssetsPath;
                        $request = $this->getRequest();
                        $this->setHeaders();
@@ -68,6 +76,7 @@
                        if ( $this->mathpattern || $this->textpattern ) {
                                $this->performSearch();
                        }
+                       restore_error_handler();
                }
 
                /**
@@ -168,15 +177,18 @@
                        if ( $this->mathBackend && $this->textpattern == "" ) {
                                $results = $this->mathBackend->getResultSet();
                                if ( $results ) {
-                                       foreach ( $results as $pageID => $page 
) {
-                                               $revision = 
Revision::newFromId( $pageID );
+                                       foreach ( $results as $revisionID => 
$page ) {
+                                               $revision = 
Revision::newFromId( $revisionID );
                                                if ( $revision ) {
-                                                       wfDebugLog( 
"MathSearch", "Found revision " . $revision->getTitle()->getText() );
-                                                       $pagename = 
(string)$revision->getTitle();
-                                                       $out->addWikiText( 
"==[[$pagename]]==" );
-                                                       $this->DisplayMath( 
$pageID );
+                                                       if ( 
$revision->isCurrent() ) {
+                                                               wfDebugLog( 
"MathSearch",
+                                                                       "Found 
revision " . $revision->getTitle()->getText() );
+                                                               $pagename = 
(string)$revision->getTitle();
+                                                               
$out->addWikiText( "==[[$pagename]]==" );
+                                                               
$this->DisplayMath( $revisionID );
+                                                       }
                                                } else
-                                                       $out->addWikiText( 
"Error with Page (ID=$pageID) update math index.\n" );
+                                                       $out->addWikiText( 
"Error with Revision (ID=$revisionID) update math index.\n" );
                                        }
                                }
                        }
@@ -198,14 +210,14 @@
                                                wfDebugLog( 'mathsearch', 'BOF' 
);
                                                $pageList = "";
                                                while ( $tres = $sres->next() ) 
{
-                                                       $pageID = 
$tres->getTitle()->getLatestRevID();
+                                                       $revisionID = 
$tres->getTitle()->getLatestRevID();
                                                        $rMap = 
$this->mathBackend->getRelevanceMap();
-                                                       if ( isset( $rMap[ 
$pageID ] ) ) {
+                                                       if ( isset( $rMap[ 
$revisionID ] ) ) {
                                                                
$out->addWikiText( "[[" . $tres->getTitle() . "]]" );
                                                                $out->addHtml( 
$tres->getTextSnippet( $textpattern ) );
-                                                               $pageList .= 
"OR [[" . $pageID . "]]";
+                                                               $pageList .= 
"OR [[" . $revisionID . "]]";
                                                                // 
$out->addHtml($this->showHit($tres),$textpattern);
-                                                               
$this->DisplayMath( $pageID );
+                                                               
$this->DisplayMath( $revisionID );
                                                        } /* else {
                                          $out->addWikiText(":NO MATH");
                                          }// */
@@ -239,16 +251,17 @@
 
                /**
                 * Displays the equations for one page
-                * @param unknown $pageID
+                *
+                * @param int $revisionID
+                *
                 * @return boolean
                 */
-               function DisplayMath( $pageID )
-               {
+               function DisplayMath( $revisionID ) {
                        global $wgMathDebug;
                        $out = $this->getOutput();
-                       $resultes = $this->mathBackend->getResultSet();
-                       $page = $resultes[ (string)$pageID ];
-                       $revision = Revision::newFromId( $pageID );
+                       $results = $this->mathBackend->getResultSet();
+                       $page = $results[ (string)$revisionID ];
+                       $revision = Revision::newFromId( $revisionID );
                        if ( $revision === false ) {
                                wfDebugLog( "MathSearch", "invalid revision 
number" );
                                return false;
@@ -256,7 +269,7 @@
                        $pagename = (string)$revision->getTitle();
                        wfDebugLog( "MathSearch", "Processing results for 
$pagename" );
                        foreach ( $page as $anchorID => $answ ) {
-                               $res = MathObject::constructformpage( $pageID, 
$anchorID );
+                               $res = MathObject::constructformpage( 
$revisionID, $anchorID );
                                if( $res ){
                                        $mml = $res->getMathml();
                                        $out->addWikiText( 
"====[[$pagename#$anchorID|Eq: $anchorID (Result " . $this->resultID++ . 
")]]====", false );
@@ -276,7 +289,12 @@
                                                foreach ( $hits as $node ) {
                                                        /* @var DOMDocument 
$node */
                                                        if ( 
$node->hasAttributes() ) {
-                                                               $domRes = 
$dom->getElementById( $node->attributes->getNamedItem( 'xref' )->nodeValue );
+                                                               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() );
@@ -290,9 +308,8 @@
                                                        }
                                                }
                                        }
-                                       wfDebugLog( "MathSearch", 
"PositionInfo:" . var_export( $this->mathResults[ $pageID ][ $anchorID ], true 
) );
                                } else
-                                       wfDebugLog( "MathSearch", "Failure: 
Could not get entry $anchorID for page $pagename (id $pageID) :" . var_export( 
$this->mathResults, true ) );
+                                       wfDebugLog( "MathSearch", "Failure: 
Could not get entry $anchorID for page $pagename (id $revisionID) :" . 
var_export( $this->mathResults, true ) );
                        }
                        return true;
                }

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

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