Physikerwelt has uploaded a new change for review.

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

Change subject: Modularize Hooks
......................................................................

Modularize Hooks

Change-Id: Ibe4c0aface343b4b7a2175098ac39b2e15c1c8b1
---
M FormulaInfo.php
M MathSearch.hooks.php
M MathSearch.php
3 files changed, 52 insertions(+), 12 deletions(-)


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

diff --git a/FormulaInfo.php b/FormulaInfo.php
index 85556ae..5244cb6 100644
--- a/FormulaInfo.php
+++ b/FormulaInfo.php
@@ -53,6 +53,7 @@
                        $wgOut->addWikiText( "No occurrences found clean up the 
database to remove unused formulae" );
                }
        }
+
        public function DisplayInfo( $pid, $eid ) {
                global $wgMathDebug, $wgOut;
                /* $out Output page find out how to get that variable in a 
static context*/
@@ -66,16 +67,21 @@
                }
 
                $pagename = (string)$article->getTitle();
-               $out->addWikiText( "* Page found: 
[[$pagename#math$eid|$pagename]] (eq $eid)  ", false );
+               $out->addWikiText( "* Page found: [[$pagename#$eid|$pagename]] 
(eq $eid)  ", false );
                $out->addHtml( '<a href="/index.php?title=' . $pagename . 
'&action=purge&mathpurge=true">(force rerendering)</a>' );
 
                /* @var $mo MathObject  */
                $mo = MathObject::constructformpage( $pid, $eid );
+               if ( !$mo ) {
+                       $out->addWikiText( 'Cannot find the equation data in 
the database.' );
+                       return false;
+               }
                $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>' );
                $out->addWikiText( 'MathML (' . self::getlengh( 
$mo->getMathml() ) . ') :', false );
+               // TODO: Add logo
                $out->addHtml( '<a href="/wiki/Special:MathSearch?mathpattern=' 
. urlencode( $mo->getTex() ) . '&searchx=Search"><img 
src="http://wikidemo.formulasearchengine.com/images/FSE-PIC.png"; width="15" 
height="15"></a>' );
                $out->addHtml(  $mo->getMathml() );
                # $log=htmlspecialchars( $res->math_log );
diff --git a/MathSearch.hooks.php b/MathSearch.hooks.php
index c013f04..5db2d6e 100644
--- a/MathSearch.hooks.php
+++ b/MathSearch.hooks.php
@@ -98,6 +98,27 @@
        }
 
        /**
+        * Changes the specified defaultID given as argument ID to
+        * either the manually assignedID from the MathTag or
+        * prefixes it with "math" to increase the probability of
+        * having a unique id that can be referenced via the anchor
+        * #math{$id}.
+        * @param int $id
+        * @param MathRenderer $renderer
+        * @return bool true if an ID has been assigned manually,
+        * false if the automatic fallback math{$id} was used.
+        */
+       private static function setMathId( &$id, MathRenderer $renderer) {
+               if ( $renderer->getID() ){
+                       $id = $renderer->getID();
+                       return true;
+               } else {
+                       $id = "math{$id}";
+                       return false;
+               }
+       }
+
+       /**
         * Callback function that is called after a formula was rendered
         * @param MathRenderer $Renderer
         * @param string|null $Result reference to the rendering result
@@ -105,17 +126,14 @@
         * @param int $eid
         * @return bool
         */
-       static function onMathFormulaRendered( MathRenderer $Renderer, &$Result 
= null, $pid = 0, $eid = 0 ) {
+       static function updateMathIndex( MathRenderer $Renderer, &$Result = 
null, $pid = 0, $eid = 0 ) {
                if ( $pid > 0 ) { // Only store something if a pageid was set.
                        // Use manually assigned IDs whenever possible
                        // and fallback to automatic IDs otherwise.
-                       if ( $Renderer->getID() ){
-                               $id = $Renderer->getID();
-                       } else {
-                               $id = 'math'.$eid;
-                               $Result = preg_replace( 
'/(class="mwe-math-mathml-(inline|display))/', "id=\"$id\" \\1", $Result );
+                       if ( ! self::setMathId( $eid , $Renderer ) ){
+                               $Result = preg_replace( 
'/(class="mwe-math-mathml-(inline|display))/', "id=\"$eid\" \\1", $Result );
                        }
-                       self::updateIndex( $pid , $id , 
$Renderer->getInputHash() , $Renderer->getTex() );
+                       self::updateIndex( $pid , $eid , 
$Renderer->getInputHash() , $Renderer->getTex() );
                }
                return true;
        }
@@ -128,15 +146,30 @@
         * @param int $eid
         * @return bool
         */
-       static function addIdentifierAndSpecialPageLink( MathRenderer 
$Renderer, &$Result = null, $pid = 0, $eid = 0 ) {
-               $url = SpecialPage::getTitleFor( 'FormulaInfo' )->getLocalUrl( 
array( 'pid' => $pid, 'eid' => $eid ) );
+       static function addIdentifierDescription( MathRenderer $Renderer, 
&$Result = null, $pid = 0, $eid = 0 ) {
+               self::setMathId( $eid , $Renderer );
                $mo = MathObject::cloneFromRenderer($Renderer);
                $mo->setPageID($pid);
                $mo->setID($eid);
                $Result = preg_replace_callback("#<(mi|mo)( 
([^>].*?))?>(.*?)</\\1>#u", array( $mo , 'addIdentifierTitle' ), $Result);
-               $Result = '<a href="' . $url . '" id="math' . $eid . '" 
style="color:inherit;">' . $Result . '</a>';
                return true;
        }
+
+       /**
+        * 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 addLinkToFormulaInfoPage( MathRenderer $Renderer, 
&$Result = null, $pid = 0, $eid = 0 ) {
+               self::setMathId( $eid , $Renderer );
+               $url = SpecialPage::getTitleFor( 'FormulaInfo' )->getLocalUrl( 
array( 'pid' => $pid, 'eid' => $eid ) );
+               $Result = "<a href=\"$url\" id=\"$eid\" 
style=\"color:inherit;\">$Result</a>";
+               return true;
+       }
+
        /**
         * Alternative Callback function that is called after a formula was 
rendered
         * used for test corpus generation for NTCIR11 Math-2
diff --git a/MathSearch.php b/MathSearch.php
index 60a46cb..5452183 100644
--- a/MathSearch.php
+++ b/MathSearch.php
@@ -74,7 +74,8 @@
 $wgAPIModules['mathquery'] = 'MathSearchApi';
 
 $wgHooks['LoadExtensionSchemaUpdates'][] = 
'MathSearchHooks::onLoadExtensionSchemaUpdates';
-$wgHooks['MathFormulaRendered'][] = 'MathSearchHooks::onMathFormulaRendered';
+$wgHooks['MathFormulaRendered']['updateIndex'] = 
'MathSearchHooks::updateMathIndex';
+$wgHooks['MathFormulaRendered']['addLink'] = 
'MathSearchHooks::addLinkToFormulaInfoPage';
 $wgHooks['UnitTestsList'][] = 'MathSearchHooks::onRegisterUnitTests';
 
 $wgGroupPermissions['user']['MathDebug'] = true;

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

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