jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/380917 )

Change subject: Added WikiText Template builder
......................................................................


Added WikiText Template builder

Change-Id: Ic2164ab2632a22f59493b60d9169858d50d13acf
---
A src/WikiText/Template.php
A tests/phpunit/WikiText/TemplateTest.php
2 files changed, 317 insertions(+), 0 deletions(-)

Approvals:
  Pwirth: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/WikiText/Template.php b/src/WikiText/Template.php
new file mode 100644
index 0000000..e21c71f
--- /dev/null
+++ b/src/WikiText/Template.php
@@ -0,0 +1,172 @@
+<?php
+
+namespace BlueSpice\WikiText;
+
+class Template {
+       /**
+        *
+        * @var string
+        */
+       protected $name = '';
+
+       /**
+        *
+        * @var string
+        */
+       protected $params = [];
+
+       /**
+        *
+        * @var boolean
+        */
+       protected $renderFormatted = true;
+
+       protected $buffer = [];
+
+       /**
+        *
+        * @param string $name
+        * @param array $params
+        */
+       public function __construct( $name, $params ) {
+               $this->name = $name;
+               $this->params = $params;
+       }
+
+       /**
+        *
+        * @param int|string $paramNameorIndex
+        * @param mixed $paramValue
+        * @return Template
+        */
+       public function set( $paramNameorIndex, $paramValue ) {
+               $this->params[$paramNameorIndex] = $paramValue;
+               return $this;
+       }
+
+       /**
+        *
+        * @param int|string $paramNameorIndex
+        * @param mixed $default
+        * @return mixed
+        */
+       public function get( $paramNameorIndex, $default = '' ) {
+               if( isset( $this->params[$paramNameorIndex] ) ) {
+                       return $this->params[$paramNameorIndex];
+               }
+               return $default;
+       }
+
+       /**
+        *
+        * @return string
+        */
+       public function getName() {
+               return $this->name;
+       }
+
+       /**
+        *
+        * @param string $name
+        * @return Template Description
+        */
+       public function setName( $name ) {
+               $this->name = $name;
+               return $this;
+       }
+
+       /**
+        *
+        * @param boolean $renderFormatted
+        * @return Template
+        */
+       public function setRenderFormatted( $renderFormatted = true ) {
+               $this->renderFormatted = $renderFormatted;
+               return $this;
+       }
+
+       /**
+        * @return string
+        */
+       public function render() {
+               $this->clearBuffer();
+               $this->openCurlies();
+               $this->appendName();
+               $this->appendParams();
+               $this->closeCurlies();
+
+               return implode( '', $this->buffer );
+       }
+
+       /**
+        *
+        * @return string
+        */
+       public function __toString() {
+               return $this->render();
+       }
+
+       protected function clearBuffer() {
+               $this->buffer = [];
+       }
+
+       protected function openCurlies() {
+               $this->buffer[] = '{{';
+       }
+
+       protected function closeCurlies() {
+               $this->buffer[] = '}}';
+       }
+
+       protected function appendName() {
+               $this->buffer[] = $this->name;
+               if( $this->renderFormatted ) {
+                       $this->buffer[] = "\n";
+               }
+       }
+
+       protected function appendParams() {
+               foreach( $this->params as $paramNameOrIndex => $paramValue ) {
+                       $this->buffer[] = "|";
+                       $isNamedParameter = false;
+                       if( !is_numeric( $paramNameOrIndex ) ) {
+                               $this->buffer[] = "$paramNameOrIndex =";
+                               $isNamedParameter = true;
+                       }
+
+                       $this->appendParamValue( $paramValue, $isNamedParameter 
);
+
+                       if( $this->renderFormatted ) {
+                               $this->buffer[] = "\n";
+                       }
+               }
+       }
+
+       protected $specialWikiTextMarkupFirstChars = [ '*', '#', ':' ];
+
+       protected function appendParamValue( $paramValue, $isNamedParameter ) {
+               $preparedParamValue = $this->prepareParamValue( $paramValue );
+               $firstChar = substr( $preparedParamValue, 0, 1 );
+
+               if( in_array( $firstChar, 
$this->specialWikiTextMarkupFirstChars ) ) {
+                       $this->buffer[] = "\n";
+               }
+               else if( $isNamedParameter ) {
+                       $this->buffer[] = ' ';
+               }
+
+               $this->buffer[] = $preparedParamValue;
+       }
+
+       protected function prepareParamValue( $paramValue ) {
+               if( is_array(  $paramValue ) ) {
+                       $newParamValue = implode( '', $paramValue );
+               }
+               else {
+                       $newParamValue = $paramValue;
+               }
+
+               return trim( $newParamValue );
+       }
+
+}
diff --git a/tests/phpunit/WikiText/TemplateTest.php 
b/tests/phpunit/WikiText/TemplateTest.php
new file mode 100644
index 0000000..e1ddb48
--- /dev/null
+++ b/tests/phpunit/WikiText/TemplateTest.php
@@ -0,0 +1,145 @@
+<?php
+
+namespace BlueSpice\Tests\WikiText;
+
+class TemplateTest extends \PHPUnit_Framework_TestCase {
+       public function testSimpleIndexedParams() {
+               $template = new \BlueSpice\WikiText\Template( 'TestTemplate', [
+                       'Value 1', 'Value 2', 'Value 3'
+               ] );
+
+               $wikiText = $template->render();
+               $expectedWikiText = <<<HERE
+{{TestTemplate
+|Value 1
+|Value 2
+|Value 3
+}}
+HERE;
+
+               $this->assertEquals( $expectedWikiText, $wikiText );
+
+               $template->setRenderFormatted( false );
+               $wikiText = $template->render();
+               $expectedWikiText = <<<HERE
+{{TestTemplate|Value 1|Value 2|Value 3}}
+HERE;
+
+               $this->assertEquals( $expectedWikiText, $wikiText );
+       }
+
+       public function testSimpleNamedParams() {
+               $template = new \BlueSpice\WikiText\Template( 'TestTemplate', [
+                       'param1' => 'Value 1',
+                       'param2' => 'Value 2',
+                       'param3' => 'Value 3'
+               ] );
+
+               $wikiText = $template->render();
+               $expectedWikiText = <<<HERE
+{{TestTemplate
+|param1 = Value 1
+|param2 = Value 2
+|param3 = Value 3
+}}
+HERE;
+
+               $this->assertEquals( $expectedWikiText, $wikiText );
+
+               $template->setRenderFormatted( false );
+               $wikiText = $template->render();
+               $expectedWikiText = <<<HERE
+{{TestTemplate|param1 = Value 1|param2 = Value 2|param3 = Value 3}}
+HERE;
+
+               $this->assertEquals( $expectedWikiText, $wikiText );
+       }
+
+               public function testSimpleMixedParams() {
+               $template = new \BlueSpice\WikiText\Template( 'TestTemplate', [
+                       'param1' => 'Value 1',
+                       'Value 2',
+                       'param3' => 'Value 3'
+               ] );
+
+               $wikiText = $template->render();
+               $expectedWikiText = <<<HERE
+{{TestTemplate
+|param1 = Value 1
+|Value 2
+|param3 = Value 3
+}}
+HERE;
+
+               $this->assertEquals( $expectedWikiText, $wikiText );
+
+               $template->setRenderFormatted( false );
+               $wikiText = $template->render();
+               $expectedWikiText = <<<HERE
+{{TestTemplate|param1 = Value 1|Value 2|param3 = Value 3}}
+HERE;
+
+               $this->assertEquals( $expectedWikiText, $wikiText );
+       }
+
+       public function testComplexNamedParams() {
+               $template = new \BlueSpice\WikiText\Template( 'TestTemplate', [
+                       'param1' => 'Value 1',
+                       'param2' => [
+                               "Some normal text with a ",
+                               new \BlueSpice\WikiText\Template( 
'NestedTemplate', [
+                                       'innerParam1' => 'Inner value 1'
+                               ]),
+                               " and\n\nsome paragraph"
+                       ],
+                       'param3' => 'Value 3'
+               ] );
+
+               $wikiText = $template->render();
+               $expectedWikiText = <<<HERE
+{{TestTemplate
+|param1 = Value 1
+|param2 = Some normal text with a {{NestedTemplate
+|innerParam1 = Inner value 1
+}} and
+
+some paragraph
+|param3 = Value 3
+}}
+HERE;
+
+               $this->assertEquals( $expectedWikiText, $wikiText );
+
+               $template->setRenderFormatted( false );
+               $wikiText = $template->render();
+               $expectedWikiText = <<<HERE
+{{TestTemplate|param1 = Value 1|param2 = Some normal text with a 
{{NestedTemplate
+|innerParam1 = Inner value 1
+}} and
+
+some paragraph|param3 = Value 3}}
+HERE;
+
+               $this->assertEquals( $expectedWikiText, $wikiText );
+       }
+
+       public function testAutoLinebreakOnCertainWikiTextParamValues() {
+               $template = new \BlueSpice\WikiText\Template( 'TestTemplate', [
+                       'param1' => [
+                               "*Some\n*wikitext\n*list"
+                       ]
+               ] );
+
+               $wikiText = $template->render();
+               $expectedWikiText = <<<HERE
+{{TestTemplate
+|param1 =
+*Some
+*wikitext
+*list
+}}
+HERE;
+
+               $this->assertEquals( $expectedWikiText, $wikiText );
+       }
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic2164ab2632a22f59493b60d9169858d50d13acf
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/BlueSpiceFoundation
Gerrit-Branch: master
Gerrit-Owner: Robert Vogel <[email protected]>
Gerrit-Reviewer: Ljonka <[email protected]>
Gerrit-Reviewer: Mglaser <[email protected]>
Gerrit-Reviewer: Pwirth <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to