Physikerwelt has uploaded a new change for review.

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

Change subject: WIP: Chemistry datatype for Wikidata
......................................................................

WIP: Chemistry datatype for Wikidata

* Just writing down the idea I had in mind.

Bug: T126862
Change-Id: Iedd9f95d50eba96f1dc83d2b44a1c91c75f13ed2
---
A ChemFormatter.php
M MathValidator.php
M MathWikidataHook.php
M extension.json
4 files changed, 143 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Math 
refs/changes/76/270476/1

diff --git a/ChemFormatter.php b/ChemFormatter.php
new file mode 100644
index 0000000..0f7b9f2
--- /dev/null
+++ b/ChemFormatter.php
@@ -0,0 +1,81 @@
+<?php
+
+use DataValues\StringValue;
+use ValueFormatters\Exceptions\MismatchingDataValueTypeException;
+use ValueFormatters\ValueFormatter;
+use Wikibase\Lib\SnakFormatter;
+
+/*
+* Formats the tex string based on the known formats
+* * text/plain: used in the value input field of Wikidata
+* * text/x-wiki: wikitext
+* * text/html: used in Wikidata to display the value of properties
+* Formats can look like this: "text/html; disposition=widget"
+* or just "text/plain"
+*/
+
+class ChemFormatter implements ValueFormatter {
+
+       /**
+        * @var string One of the SnakFormatter::FORMAT_... constants.
+        */
+       private $format;
+
+       /**
+        * Loads format to distinguish the type of formatting
+        *
+        * @param string $format One of the SnakFormatter::FORMAT_... constants.
+        *
+        * @throws InvalidArgumentException
+        */
+       public function __construct( $format ) {
+               switch ( $format ) {
+                       case SnakFormatter::FORMAT_PLAIN:
+                       case SnakFormatter::FORMAT_WIKI:
+                       case SnakFormatter::FORMAT_HTML:
+                       case SnakFormatter::FORMAT_HTML_DIFF:
+                       case SnakFormatter::FORMAT_HTML_WIDGET:
+                               $this->format = $format;
+                               break;
+                       default:
+                               throw new InvalidArgumentException( 
'Unsupported output format: ' . $format );
+               }
+       }
+
+       /**
+        * @param StringValue $value
+        *
+        * @throws MismatchingDataValueTypeException
+        * @return string
+        */
+       public function format( $value ) {
+               if ( !( $value instanceof StringValue ) ) {
+                       throw new MismatchingDataValueTypeException( 
'StringValue', get_class( $value ) );
+               }
+
+               $tex = $value->getValue();
+
+               switch ( $this->format ) {
+                       case SnakFormatter::FORMAT_PLAIN:
+                               return $tex;
+                       case SnakFormatter::FORMAT_WIKI:
+                               return "<ce>$tex</ce>";
+                       default:
+                               $renderer = new MathMathML( '\ce{' . $tex . 
'}', array( 'type' => 'chem' ) );
+                               if ( $renderer->checkTex() && 
$renderer->render() ) {
+                                       return $renderer->getHtmlOutput();
+                               }
+
+                               // TeX string is not valid or rendering failed
+                               return $renderer->getLastError();
+               }
+       }
+
+       /**
+        * @return string One of the SnakFormatter::FORMAT_... constants.
+        */
+       public function getFormat() {
+               return $this->format;
+       }
+
+}
diff --git a/MathValidator.php b/MathValidator.php
index ad3bf2d..844a2b1 100644
--- a/MathValidator.php
+++ b/MathValidator.php
@@ -9,6 +9,13 @@
 // @author Duc Linh Tran, Julian Hilbig, Moritz Schubotz
 
 class MathValidator implements ValueValidator {
+       private $inputType;
+       /**
+        * MathValidator constructor.
+        */
+       public function __construct( $type = 'tex' ) {
+               $this->inputType = $type;
+       }
 
        /**
         * Validates a value with MathInputCheckRestbase
@@ -26,7 +33,7 @@
                // get input String from value
                $tex = $value->getValue();
 
-               $checker = new MathInputCheckRestbase( $tex );
+               $checker = new MathInputCheckRestbase( $tex, $this->inputType );
                if ( $checker->isValid() )  {
                        return Result::newSuccess();
                }
diff --git a/MathWikidataHook.php b/MathWikidataHook.php
index 145dd1b..41ea0a9 100644
--- a/MathWikidataHook.php
+++ b/MathWikidataHook.php
@@ -17,10 +17,6 @@
        public static function onWikibaseRepoDataTypes( array 
&$dataTypeDefinitions ) {
                global $wgMathEnableWikibaseDataType;
 
-               if ( !$wgMathEnableWikibaseDataType ) {
-                       return;
-               }
-
                $dataTypeDefinitions['PT:math'] = array(
                        'value-type'                 => 'string',
                        'validator-factory-callback' => function() {
@@ -55,6 +51,45 @@
                                return new MathMLRdfBuilder();
                        },
                );
+
+               if ( !$wgMathEnableWikibaseDataType ) {
+                       return;
+               }
+
+               $dataTypeDefinitions['PT:ce'] = array(
+                       'value-type'                 => 'string',
+                       'validator-factory-callback' => function() {
+                               // load validator builders
+                               $factory = 
WikibaseRepo::getDefaultValidatorBuilders();
+
+                               // initialize an array with string validators
+                               // returns an array of validators
+                               // that add basic string validation such as 
preventing empty strings
+                               $validators = $factory->buildStringValidators();
+                               $validators[] = new MathValidator( 'chem' );
+                               return $validators;
+                       },
+                       'parser-factory-callback' => function( ParserOptions 
$options ) {
+                               $repo = WikibaseRepo::getDefaultInstance();
+                               $normalizer = new 
WikibaseStringValueNormalizer( $repo->getStringNormalizer() );
+                               return new StringParser( $normalizer );
+                       },
+                       'formatter-factory-callback' => function( $format, 
FormatterOptions $options ) {
+                               global $wgOut;
+                               $styles = array( 'ext.math.desktop.styles', 
'ext.math.scripts', 'ext.math.styles' );
+                               $wgOut->addModuleStyles( $styles );
+                               return new ChemFormatter( $format );
+                       },
+                       'rdf-builder-factory-callback' => function (
+                               $mode,
+                               RdfVocabulary $vocab,
+                               RdfWriter $writer,
+                               EntityMentionListener $tracker,
+                               DedupeBag $dedupe
+                       ) {
+                               return new MathMLRdfBuilder();
+                       },
+               );
        }
 
        /*
@@ -63,10 +98,6 @@
        public static function onWikibaseClientDataTypes( array 
&$dataTypeDefinitions ) {
                global $wgMathEnableWikibaseDataType;
 
-               if ( !$wgMathEnableWikibaseDataType ) {
-                       return;
-               }
-
                $dataTypeDefinitions['PT:math'] = array(
                        'value-type'                 => 'string',
                        'formatter-factory-callback' => function( $format, 
FormatterOptions $options ) {
@@ -76,6 +107,20 @@
                                return new MathFormatter( $format );
                        },
                );
+
+               if ( !$wgMathEnableWikibaseDataType ) {
+                       return;
+               }
+
+               $dataTypeDefinitions['PT:chem'] = array(
+                       'value-type'                 => 'string',
+                       'formatter-factory-callback' => function( $format, 
FormatterOptions $options ) {
+                               global $wgOut;
+                               $styles = array( 'ext.math.desktop.styles', 
'ext.math.scripts', 'ext.math.styles' );
+                               $wgOut->addModuleStyles( $styles );
+                               return new ChemFormatter( $format );
+                       },
+               );
        }
 
 }
diff --git a/extension.json b/extension.json
index edc8732..ab99bb2 100644
--- a/extension.json
+++ b/extension.json
@@ -28,6 +28,7 @@
                "SpecialMathStatus": "SpecialMathStatus.php",
                "MathValidator": "MathValidator.php",
                "MathFormatter": "MathFormatter.php",
+               "ChemFormatter": "ChemFormatter.php",
                "MathWikidataHook": "MathWikidataHook.php",
                "MathMLRdfBuilder": "MathMLRdfBuilder.php"
        },

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iedd9f95d50eba96f1dc83d2b44a1c91c75f13ed2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Math
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