MaxSem has uploaded a new change for review.

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

Change subject: WIP: use SVGs rendered by Hierator service
......................................................................

WIP: use SVGs rendered by Hierator service

Change-Id: I0151a3dd49a799c0d870cdf894c6a9466915112c
---
A includes/FileStore.php
R includes/HtmlRenderer.php
A includes/Renderer.php
A includes/RendererService.php
A includes/RepoFileStore.php
A includes/SimpleFileStore.php
R includes/SpecialHieroglyphs.php
A includes/SvgRenderer.php
R includes/Tokenizer.php
A maintenance/comparisonTables.php
R maintenance/generateTables.php
A tests/TokenizerTest.php
M wikihiero.php
13 files changed, 275 insertions(+), 21 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/wikihiero 
refs/changes/69/178269/1

diff --git a/includes/FileStore.php b/includes/FileStore.php
new file mode 100644
index 0000000..5c8ff6e
--- /dev/null
+++ b/includes/FileStore.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace WikiHiero;
+
+use File;
+
+abstract class FileStore {
+       /**
+        * @param string $hash
+        * @return File
+        */
+       public abstract function getFromCache( $hash );
+
+       /**
+        * @param string $hash
+        * @param string $svgData
+        * @return File
+        */
+       public abstract function storeOriginal( $hash, $svgData );
+       public abstract function getThumbnail( $hash, $size );
+}
diff --git a/wikihiero.body.php b/includes/HtmlRenderer.php
similarity index 96%
rename from wikihiero.body.php
rename to includes/HtmlRenderer.php
index 65d3d6e..2b8108e 100644
--- a/wikihiero.body.php
+++ b/includes/HtmlRenderer.php
@@ -23,7 +23,13 @@
  * http://www.gnu.org/copyleft/gpl.html
  */
 
-class WikiHiero {
+namespace WikiHiero;
+
+use Html;
+use MWException;
+use ProfileSection;
+
+class HtmlRenderer extends Renderer {
        const IMAGE_EXT = 'png';
        const IMAGE_PREFIX = 'hiero_';
 
@@ -60,15 +66,6 @@
                self::$phonemes = $data['wh_phonemes'];
                self::$prefabs = $data['wh_prefabs'];
                self::$files = $data['wh_files'];
-       }
-
-       /**
-        *
-        */
-       public static function parserHook( $input ) {
-               $hiero = new WikiHiero();
-               // Strip newlines to avoid breakage in the wiki parser block 
pass
-               return str_replace( "\n", " ", $hiero->render( $input ) );
        }
 
        public function getScale() {
@@ -231,7 +228,7 @@
                        $html .= "<hr />\n";
                }
 
-               $tokenizer = new HieroTokenizer( $hiero );
+               $tokenizer = new Tokenizer( $hiero );
                $blocks = $tokenizer->tokenize();
                $contentHtml = $tableHtml = $tableContentHtml = "";
                $is_cartouche = false;
diff --git a/includes/Renderer.php b/includes/Renderer.php
new file mode 100644
index 0000000..708a294
--- /dev/null
+++ b/includes/Renderer.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace WikiHiero;
+
+abstract class Renderer {
+       public abstract function render( $text, $scale = 100 );
+
+       /**
+        *
+        */
+       public static function parserHook( $input ) {
+               $hiero = new HtmlRenderer();
+               // Strip newlines to avoid breakage in the wiki parser block 
pass
+               return str_replace( "\n", " ", $hiero->render( $input ) );
+       }
+}
diff --git a/includes/RendererService.php b/includes/RendererService.php
new file mode 100644
index 0000000..473848f
--- /dev/null
+++ b/includes/RendererService.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace WikiHiero;
+
+use MWHttpRequest;
+use ProfileSection;
+
+class RendererService {
+       const DEFAULT_SIZE = 38;
+
+       private $url;
+       private $size;
+
+       public function __construct( $url, $size = self::DEFAULT_SIZE ) {
+               $this->url = $url;
+               $this->size = $size;
+       }
+
+       /**
+        * @param $text
+        * @returns string
+        */
+       public function render( $text ) {
+               $profileSection = new ProfileSection( __METHOD__ );
+
+               $url = "{$this->url}/{$this->size}px/";
+               $options = array( 'method' => 'POST', 'postData' => array( 
'text' => $text ) );
+               $request = MWHttpRequest::factory( $url, $options );
+               $status = $request->execute();
+       }
+} 
\ No newline at end of file
diff --git a/includes/RepoFileStore.php b/includes/RepoFileStore.php
new file mode 100644
index 0000000..c0b0588
--- /dev/null
+++ b/includes/RepoFileStore.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace WikiHiero;
+
+use FileBackend;
+use FSFileBackend;
+use NullLockManager;
+
+class RepoFileStore extends FileStore {
+
+       public function getFromCache( $hash ) {
+               $profileSection = new \ProfileSection( __METHOD__ );
+
+               $backend = $this->getBackend();
+               $file = $backend->fileExists();
+               wfFindFile()
+       }
+
+       public function storeOriginal( $hash, $svgData ) {
+               // TODO: Implement storeOriginal() method.
+       }
+
+       public function getThumbnail( $hash, $size ) {
+               // TODO: Implement getThumbnail() method.
+       }
+
+       /**
+        * @return FileBackend
+        */
+       private function getBackend() {
+               return new FSFileBackend( array(
+                       'name'           => 'hiero-backend',
+                       'wikiId'         => wfWikiId(),
+                       'lockManager'    => new NullLockManager( array() ),
+                       'containerPaths' => array( 'hiero-render' => 'hiero' ),
+                       'fileMode'       => 0777
+               ) );
+       }
+}
diff --git a/includes/SimpleFileStore.php b/includes/SimpleFileStore.php
new file mode 100644
index 0000000..7281bc5
--- /dev/null
+++ b/includes/SimpleFileStore.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace WikiHiero;
+
+use UnregisteredLocalFile;
+
+class SimpleFileStore extends FileStore {
+       private $localPath;
+       private $baseUrl;
+
+       public function __construct( $localPath, $baseUrl ) {
+               $this->localPath = $localPath;
+               $this->baseUrl = $baseUrl;
+       }
+
+       public function getFromCache( $hash ) {
+               return null;
+       }
+
+       public function storeOriginal( $hash, $svgData ) {
+               $name = "{$this->localPath}/{$hash}.svg";
+               file_put_contents( $name, $svgData );
+               return UnregisteredLocalFile::newFromPath( $name, false );
+               //return "{$this->baseUrl}/{$hash}.svg";
+       }
+
+       public function getThumbnail( $hash, $size ) {
+               throw new \MWException( __CLASS__ . ' does not support 
thumbnails' );
+       }
+}
diff --git a/SpecialHieroglyphs.php b/includes/SpecialHieroglyphs.php
similarity index 96%
rename from SpecialHieroglyphs.php
rename to includes/SpecialHieroglyphs.php
index 4d14e56..2b0b697 100644
--- a/SpecialHieroglyphs.php
+++ b/includes/SpecialHieroglyphs.php
@@ -17,12 +17,16 @@
  * http://www.gnu.org/copyleft/gpl.html
  */
 
-class SpecialHieroglyphs extends SpecialPage {
+namespace WikiHiero;
+
+use Html;
+
+class SpecialHieroglyphs extends \SpecialPage {
        const HIEROGLYPHS_PER_ROW = 10;
        const CACHE_EXPIRY = 86400; // 1 day
 
        /**
-        * @var WikiHiero $hiero
+        * @var HtmlRenderer $hiero
         */
        private $hiero;
        private $syntaxHelp = array(
@@ -57,7 +61,7 @@
 
                $text = trim( $this->getContext()->getRequest()->getVal( 
'text', '' ) );
                if ( $text !== '' ) {
-                       $hiero = new WikiHiero();
+                       $hiero = new HtmlRenderer();
                        $out->addHTML( '<table class="wikitable">'
                                . '<tr><th>' . $this->msg( 'wikihiero-input' 
)->escaped() . '</th><th>'
                                . $this->msg( 'wikihiero-result' )->escaped() . 
'</th></tr>'
@@ -85,7 +89,7 @@
                        . Html::closeElement( 'form' )
                );
 
-               $this->hiero = new WikiHiero();
+               $this->hiero = new HtmlRenderer();
 
                $out->addHTML( '<table><tr><td>' );
                $out->addHTML( '<div class="mw-hiero-list">' );
@@ -103,7 +107,7 @@
 
                $key = wfMemcKey( 'hiero-list',
                        
$this->getContext()->getLanguage()->getExtraHashOptions(),
-                       WikiHiero::getImagePath(),
+                       HtmlRenderer::getImagePath(),
                        WIKIHIERO_VERSION
                );
                $html = $wgMemc->get( $key );
diff --git a/includes/SvgRenderer.php b/includes/SvgRenderer.php
new file mode 100644
index 0000000..5f6fd43
--- /dev/null
+++ b/includes/SvgRenderer.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace WikiHiero;
+
+use Html;
+use ProfileSection;
+
+class SvgRenderer extends Renderer {
+       private $pngFallbacks = true;
+       private $store;
+       private $renderer;
+
+       public function __construct( RendererService $renderer, FileStore 
$store ) {
+               $this->renderer = $renderer;
+               $this->store = $store;
+       }
+
+       public function usePngFallbacks( $value ) {
+               $this->pngFallbacks = $value;
+       }
+
+       public function render( $text, $scale = 100 ) {
+               $profileSection = new ProfileSection( __METHOD__ );
+
+               $tokenizer = new Tokenizer( $text );
+               $normalizedText = $tokenizer->normalize();
+               $hash = sha1( $normalizedText );
+               $file = $this->store->getFromCache( $hash );
+               if ( !$file ) {
+                       $contents = $this->renderer->render( $normalizedText );
+                       $file = $this->store->storeOriginal( $hash, $contents );
+               }
+               $svgUrl = $file->getUrl();
+               $width = $file->getWidth();
+               $height = $file->getHeight();
+               if ( $this->pngFallbacks ) {
+                       $fallback = $this->store->getThumbnail( $hash, $width );
+               } else {
+                       return Html::element(
+                               'img',
+                               array( 'src' => $svgUrl, 'width' => $width, 
'height' => $height, 'alt' => $normalizedText )
+                       );
+               }
+               return 'poop!';
+       }
+}
diff --git a/HieroTokenizer.php b/includes/Tokenizer.php
similarity index 88%
rename from HieroTokenizer.php
rename to includes/Tokenizer.php
index 8e5f1dd..eaec8be 100644
--- a/HieroTokenizer.php
+++ b/includes/Tokenizer.php
@@ -18,10 +18,14 @@
  * http://www.gnu.org/copyleft/gpl.html
  */
 
+namespace WikiHiero;
+
+use ProfileSection;
+
 /**
  * Hieroglyphs tokenizer
  */
-class HieroTokenizer {
+class Tokenizer {
        private static $delimiters = false;
        private static $tokenDelimiters;
        private static $singleChars;
@@ -89,6 +93,20 @@
                return $this->blocks;
        }
 
+       public function normalize() {
+               $profileSection = new ProfileSection( __METHOD__ );
+
+               $blocks = $this->tokenize();
+               return implode( '-', array_map( array( $this, 
'normalizeInnerBlock' ), $blocks ) );
+       }
+
+       private function normalizeInnerBlock( array $block ) {
+               if ( $block && $block[0] == ':' ) {
+                       array_shift( $block );
+               }
+               return implode( '', $block );
+       }
+
        /**
         * Handles a block delimiter
         */
diff --git a/maintenance/comparisonTables.php b/maintenance/comparisonTables.php
new file mode 100644
index 0000000..a3085a7
--- /dev/null
+++ b/maintenance/comparisonTables.php
@@ -0,0 +1,11 @@
+<?php
+
+$IP = getenv( 'MW_INSTALL_PATH' );
+if ( $IP === false ) {
+       $IP = dirname( __FILE__ ) . '/../..';
+}
+require_once( "$IP/maintenance/Maintenance.php" );
+
+class MakeComparisonTables extends Maintenance {
+       public function execute() {}
+}
diff --git a/generateTables.php b/maintenance/generateTables.php
similarity index 100%
rename from generateTables.php
rename to maintenance/generateTables.php
diff --git a/tests/TokenizerTest.php b/tests/TokenizerTest.php
new file mode 100644
index 0000000..0b12290
--- /dev/null
+++ b/tests/TokenizerTest.php
@@ -0,0 +1,32 @@
+<?php
+
+/**
+ * @group WikiHiero
+ */
+class TokenizerTest extends MediaWikiTestCase {
+       /**
+        * @dataProvider provideNormalize
+        * @param $input
+        * @param $expected
+        */
+       public function testNormalize( $input, $expected ) {
+               $tokenizer = new HieroTokenizer( $input );
+               $text = $tokenizer->normalize();
+               $this->assertEquals( $expected, $text );
+       }
+
+       public function provideNormalize() {
+               return array(
+                       array( 'A1-A2', 'A1-A2' ),
+                       array( "\tA1   A2\nA3   ", 'A1-A2-A3' ),
+                       array( "A<!-- A2 \n -->1", 'A1' ),
+                       // Lots of existing WP texts have blocks starting with 
a ":" which is handled well
+                       // by WikiHiero but not JSesh
+                       array( 'A1  <!-- boo -->   B1-C1 :D1:E1', 
'A1-B1-C1-D1:E1' ),
+                       array( '<h1 A1 A2 >', '<h1-A1-A2->' ),
+                       array( '<-A1-A2-h2>', '<-A1-A2-h2>' ),
+                       array( 'a*b:t', 'a:b*t' ),
+                       array( '(a:b)*t', '(a:b)*t' ),
+               );
+       }
+}
diff --git a/wikihiero.php b/wikihiero.php
index 6840b5a..df6186e 100644
--- a/wikihiero.php
+++ b/wikihiero.php
@@ -25,6 +25,10 @@
 
 $wgHooks['ParserFirstCallInit'][] = 'wfRegisterWikiHiero';
 $wgHooks['BeforePageDisplay'][] = 'wfHieroBeforePageDisplay';
+$wgHooks['UnitTestsList'][] = function( &$paths ) {
+       $paths[] = __DIR__ . '/tests';
+       return true;
+};
 
 // Register MediaWiki extension
 $wgExtensionCredits['parserhook'][] = array(
@@ -43,9 +47,10 @@
 $wgExtensionMessagesFiles['Wikihiero'] = "$dir/wikihiero.i18n.php";
 $wgExtensionMessagesFiles['HieroglyphsAlias'] = "$dir/wikihiero.alias.php";
 
-$wgAutoloadClasses['WikiHiero'] = "$dir/wikihiero.body.php";
-$wgAutoloadClasses['SpecialHieroglyphs'] = "$dir/SpecialHieroglyphs.php";
-$wgAutoloadClasses['HieroTokenizer'] = "$dir/HieroTokenizer.php";
+$wgAutoloadClasses['WikiHiero/Renderer'] = "$dir/includes/Renderer.php";
+$wgAutoloadClasses['WikiHiero/HtmlRenderer'] = 
"$dir/includes/HtmlRenderer.php";
+$wgAutoloadClasses['WikiHiero/SpecialHieroglyphs'] = 
"$dir/includes/SpecialHieroglyphs.php";
+$wgAutoloadClasses['WikiHiero/Tokenizer'] = "$dir/includes/Tokenizer.php";
 
 $wgParserTestFiles[] = "$dir/tests.txt";
 
@@ -101,7 +106,7 @@
  * @return bool
  */
 function wfRegisterWikiHiero( &$parser ) {
-       $parser->setHook( 'hiero', 'WikiHiero::parserHook' );
+       $parser->setHook( 'hiero', 'WikiHiero/Renderer::parserHook' );
        return true;
 }
 
@@ -113,3 +118,7 @@
        $out->addModuleStyles( 'ext.wikihiero' );
        return true;
 }
+
+$wgWikiHieroService = null;
+
+$wgWikiHieroDefaultSize = 38;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0151a3dd49a799c0d870cdf894c6a9466915112c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/wikihiero
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to