Physikerwelt has uploaded a new change for review.

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

Change subject: Display text snippet for evaluation
......................................................................

Display text snippet for evaluation

Change-Id: I63a3bc1ba40338ea954ac2ee15821d3bab3df36a
---
M includes/special/SpecialMlpEval.php
1 file changed, 151 insertions(+), 25 deletions(-)


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

diff --git a/includes/special/SpecialMlpEval.php 
b/includes/special/SpecialMlpEval.php
index bea58c6..a2dc2fe 100644
--- a/includes/special/SpecialMlpEval.php
+++ b/includes/special/SpecialMlpEval.php
@@ -12,6 +12,18 @@
  * @ingroup extensions
  */
 class SpecialMlpEval extends SpecialPage {
+       const MAX_ATTEMPTS = 10;
+       const WINDOW_SIZE = 500;
+       private $step = 1;
+       /**
+        * @var Title
+        */
+       private $title;
+       private $wikitext;
+       private $mathTags;
+       private $revison;
+       private $lastError = false;
+
        function __construct() {
                parent::__construct( 'MlpEval' );
        }
@@ -20,29 +32,9 @@
         * The main function
         */
        public function execute( $par ) {
-               $request = $this->getRequest();
                $this->setHeaders();
-               $this->selectPageForm()->show();
+               $this->displayForm();
        }
-
-       /**
-        * Generates the search input form
-        */
-       private function selectPageForm() {
-               $this->getOutput()->addModules( 'ext.MathSearch.special' );
-               $formDescriptor = array(
-                       'evalPage' => array(
-                               'label' => 'Page to evaluate', # What's the 
label of the field
-                               'class' => 'HTMLTextField' # What's the input 
type
-                       )
-               );
-               $htmlForm =     new HTMLForm( $formDescriptor, 
$this->getContext() );
-               $htmlForm->setSubmitText( 'Select' );
-               $htmlForm->setSubmitCallback( array( $this, 'processInput' ) );
-               $htmlForm->setHeaderText( "<h2>Step 1: Select a page</h2>" );
-               return $htmlForm;
-       }
-
 
 
        /**
@@ -51,15 +43,26 @@
         * @return bool
         */
        public function processInput( $formData ) {
-
-               $this->performSearch();
+               switch ( $this->step ) {
+                       case 1:
+                               $title = Title::newFromText( 
$formData['evalPage'] );
+                               if ( $this->setPage( $title ) ) {
+                                       $this->step ++;
+                                       return false;
+                               } else {
+                                       return $this->lastError;
+                               }
+                               break;
+                       case 2:
+                               return false;
+               }
        }
 
        public function performSearch() {
                $out = $this->getOutput();
                $out->addWikiText( '==Results==' );
                $out->addWikiText( 'You searched for the following terms:' );
-
+               return false;
        }
 
        /**
@@ -75,7 +78,7 @@
        private function enableMathStyles() {
                $out = $this->getOutput();
                $out->addModuleStyles(
-                       array( 'ext.math.styles' , 'ext.math.desktop.styles', 
'ext.math.scripts' )
+                       array( 'ext.math.styles', 'ext.math.desktop.styles', 
'ext.math.scripts' )
                );
        }
 
@@ -83,4 +86,127 @@
        protected function getGroupName() {
                return 'mathsearch';
        }
+
+       private function getRandomPage() {
+               $rp = new RandomPage();
+               for ( $i = 0; $i < self::MAX_ATTEMPTS; $i ++ ) {
+                       $title = $rp->getRandomTitle();
+                       if ( $this->setPage( $title ) ) {
+                               $this->lastError = "";
+                               return $title;
+                       }
+               }
+               $this->log()->warning( "Could not find suitable Page with 
math", $this->lastError );
+               $this->lastError = "";
+               return "";
+       }
+
+       private function displayForm() {
+               $startStep = $this->step;
+               switch ( $this->step ) {
+                       case 1:
+                               $this->getOutput()->addModules( 
'ext.MathSearch.special' );
+                               $formDescriptor = array(
+                                       'evalPage' => array(
+                                               'label'   => 'Page to evaluate',
+                                               'class'   => 'HTMLTextField',
+                                               'default' => 
$this->getRandomPage()
+                                       )
+                               );
+                               break;
+                       case 2:
+                               $this->enableMathStyles();
+                               list( $tagCount, $formDescriptor, $wikiText ) = 
$this->displaySnipped();
+                               break;
+               }
+               $htmlForm = new HTMLForm( $formDescriptor, $this->getContext() 
);
+               $htmlForm->setSubmitText( 'Select' );
+               $htmlForm->setSubmitCallback( array( $this, 'processInput' ) );
+               $htmlForm->setHeaderText( "<h2>Step {$this->step}: Select a 
page</h2>" );
+               $htmlForm->prepareForm();
+               $result = $htmlForm->tryAuthorizedSubmit();
+               if ( $this->step > $startStep ) {
+                       $this->displayForm();
+               } else {
+                       if ( $result === true || ( $result instanceof Status && 
$result->isGood() ) ) {
+                               return $result;
+                       }
+                       $htmlForm->displayForm( $result );
+               }
+       }
+
+       private function setPage( Title $title ) {
+               if ( is_null( $title ) ) {
+                       $this->lastError = "Title was null.";
+                       return false;
+               }
+               if ( $title->isRedirect() ) {
+                       $this->lastError = "Redirects are not supported";
+                       return false;
+               }
+               $revision = $title->getLatestRevID();
+               if ( $revision ) {
+                       $revision = Revision::newFromId( $revision );
+                       if ( $revision->getContentModel() !== 
CONTENT_MODEL_WIKITEXT ) {
+                               $this->lastError = "Has invalid format";
+                               return false;
+                       }
+                       $handler = new WikitextContentHandler();
+                       $wikiText = $handler->getContentText( 
$revision->getContent() );
+                       $wikiText = Sanitizer::removeHTMLcomments( $wikiText );
+                       // Remove <nowiki /> tags to avoid confusion
+                       $wikiText = preg_replace( '#<nowiki>(.*)</nowiki>#', 
'', $wikiText );
+                       $wikiText = Parser::extractTagsAndParams( array( 'math' 
), $wikiText, $mathTags );
+                       $tagCount = count( $mathTags );
+                       if ( $tagCount == 0 ) {
+                               $this->lastError = "has no math tags";
+                               return false;
+                       }
+                       $this->title = $title;
+                       $this->wikitext = $wikiText;
+                       $this->mathTags = $mathTags;
+                       return true;
+               } else {
+                       $this->lastError = "invalid revision";
+                       return false;
+               }
+       }
+
+       /**
+        * @return \Psr\Log\LoggerInterface
+        */
+       private function log() {
+               return LoggerFactory::getInstance( 'MathSearch' );
+       }
+
+       /**
+        * @return array
+        * @throws MWException
+        */
+       private function displaySnipped() {
+               $tagCount = count( $this->mathTags );
+               $this->getOutput()->addWikiText( "Found $tagCount math tags." );
+               $unique = array_rand( $this->mathTags );
+               $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 ) );
+               $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..." );
+               $url = $this->title->getLinkURL();
+               $this->getOutput()
+                       ->addHTML( "<a href=\"$url\" target=\"_blank\">Full 
article (new Window)</a>" );
+               return array( $tagCount, $formDescriptor, $wikiText );
+       }
 }

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

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