Physikerwelt has uploaded a new change for review.

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

Change subject: Improve snippet generation
......................................................................

Improve snippet generation

Change-Id: Ib201ba5a2693f516ac377ac28df6cb100bb6a93e
---
M includes/special/SpecialMlpEval.php
1 file changed, 48 insertions(+), 7 deletions(-)


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

diff --git a/includes/special/SpecialMlpEval.php 
b/includes/special/SpecialMlpEval.php
index a2dc2fe..7b1eaba 100644
--- a/includes/special/SpecialMlpEval.php
+++ b/includes/special/SpecialMlpEval.php
@@ -13,13 +13,14 @@
  */
 class SpecialMlpEval extends SpecialPage {
        const MAX_ATTEMPTS = 10;
-       const WINDOW_SIZE = 500;
+       const WINDOW_SIZE = 1200;
        private $step = 1;
        /**
         * @var Title
         */
        private $title;
        private $wikitext;
+       private $snippet;
        private $mathTags;
        private $revison;
        private $lastError = false;
@@ -179,6 +180,7 @@
                return LoggerFactory::getInstance( 'MathSearch' );
        }
 
+
        /**
         * @return array
         * @throws MWException
@@ -190,23 +192,62 @@
                $tag = $this->mathTags[$unique];
                $formDescriptor = array();
                $this->getOutput()->addWikiText( $tag[3] );
-
                $tagPos = strpos( $this->wikitext, $unique );
                $wikiText = $this->wikitext;
-               $wikiText = substr( $wikiText,
-                       max( $tagPos - self::WINDOW_SIZE, 0 ),
-                       min( 2 * self::WINDOW_SIZE, strlen( $wikiText ) - 
$tagPos ) );
+               $startPos = $this->getStartPos( $tagPos, $wikiText );
+               $length = $this->getEndPos( $tagPos, $wikiText ) - $startPos;
+               $wikiText = substr( $wikiText, $startPos, $length );
                $wikiText = str_replace( $unique,
                        '<span id="theelement" style="background-color: 
yellow">' . $tag[3] . '</span>',
                        $wikiText );
                foreach ( $this->mathTags as $key => $content ) {
                        $wikiText = str_replace( $key, $content[3], $wikiText );
                }
-
-               $this->getOutput()->addWikiText( "== Extract 
==\n...\n$wikiText\n..." );
+               $this->snippet = "== Extract ==\nStart of the 
extract...\n\n$wikiText\n\n...end of the extract";
+               $this->getOutput()->addWikiText( $this->snippet );
                $url = $this->title->getLinkURL();
                $this->getOutput()
                        ->addHTML( "<a href=\"$url\" target=\"_blank\">Full 
article (new Window)</a>" );
                return array( $tagCount, $formDescriptor, $wikiText );
        }
+
+       /**
+        * @param $tagPos
+        * @param $wikiText
+        * @return array
+        */
+       private function getStartPos( $tagPos, $wikiText ) {
+               $startPos = max( $tagPos - round( self::WINDOW_SIZE / 2 ), 0 );
+               if ( $startPos > 0 ) {
+                       // Heuristics to find a reasonable cutting point
+                       $newPos = strpos( $wikiText, "\n", $startPos );
+                       if ( $newPos !== false && ( $newPos - $startPos ) < 
round( self::WINDOW_SIZE / 4 ) ) {
+                               // only change startPos, if it seems reasonable
+                               $startPos = $newPos;
+                       }
+               }
+               return $startPos;
+       }
+
+       /**
+        * @param $wikiText
+        * @param $tagPos
+        * @return bool|int|mixed
+        */
+       private function getEndPos( $tagPos, $wikiText ) {
+               $halfWindow = round( self::WINDOW_SIZE / 2 );
+               $distance2End = strlen( $wikiText ) - $tagPos;
+               if ( $distance2End > $halfWindow ) {
+                       $newPos = strpos( $wikiText, "\n", $tagPos + 
$halfWindow );
+                       if ( $newPos !== false && ( $newPos - $tagPos ) < 
round( 3 / 4 * self::WINDOW_SIZE ) ) {
+                               // only change startPos, if it seems reasonable
+                               return $newPos;
+                       } else {
+                               return $tagPos + $halfWindow;
+                       }
+               } else {
+                       return strlen( $wikiText );
+               }
+       }
+
 }

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

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