Jeroen De Dauw has uploaded a new change for review.

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


Change subject: Add MediaWikiTitleValue related code which got moved from the 
DataValues repository
......................................................................

Add MediaWikiTitleValue related code which got moved from the DataValues 
repository

Change-Id: Ifa7e628c6327d12fa4fd4f0728ef869cf541643f
---
M ParamProcessor.classes.php
M config/DefaultConfig.php
M includes/ParamProcessor/Hooks.php
A includes/ParamProcessor/MediaWikiTitleValue.php
A includes/ParamProcessor/TitleParser.php
A tests/MediaWikiTitleValueTest.php
A tests/TitleParserTest.php
7 files changed, 343 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Validator 
refs/changes/47/63847/1

diff --git a/ParamProcessor.classes.php b/ParamProcessor.classes.php
index 8db38b9..5236ac9 100644
--- a/ParamProcessor.classes.php
+++ b/ParamProcessor.classes.php
@@ -26,11 +26,13 @@
                'ParamProcessor\ProcessingError',
                'ParamProcessor\ProcessingResult',
                'ParamProcessor\Processor',
+               'ParamProcessor\MediaWikiTitleValue',
                'ParamProcessor\Options',
                'ParamProcessor\TopologicalSort',
                'ParamProcessor\ProcessingErrorHandler',
                'ParamProcessor\IParam',
                'ParamProcessor\IParamDefinition',
+               'ParamProcessor\TitleParser',
 
                'ParamProcessor\Definition\DimensionParam',
                'ParamProcessor\Definition\StringParam',
diff --git a/config/DefaultConfig.php b/config/DefaultConfig.php
index 9a6426a..97b9fe3 100644
--- a/config/DefaultConfig.php
+++ b/config/DefaultConfig.php
@@ -59,7 +59,7 @@
                'definition' => '\ParamProcessor\Definition\StringParam',
        ),
        'title' => array(
-               'string-parser' => '\ValueParsers\TitleParser',
+               'string-parser' => '\ParamProcessor\TitleParser',
                'validator' => '\ValueValidators\TitleValidator',
        ),
        'dimension' => array(
diff --git a/includes/ParamProcessor/Hooks.php 
b/includes/ParamProcessor/Hooks.php
index eb4bb8b..d75bdbe 100644
--- a/includes/ParamProcessor/Hooks.php
+++ b/includes/ParamProcessor/Hooks.php
@@ -33,9 +33,11 @@
                        'definitions/StringParam',
                        'definitions/TitleParam',
 
+                       'MediaWikiTitleValue',
                        'ParamDefinitionFactory',
                        'Options',
                        'Processor',
+                       'TitleParser',
                );
 
                foreach ( $testFiles as $file ) {
diff --git a/includes/ParamProcessor/MediaWikiTitleValue.php 
b/includes/ParamProcessor/MediaWikiTitleValue.php
new file mode 100644
index 0000000..30ce5e0
--- /dev/null
+++ b/includes/ParamProcessor/MediaWikiTitleValue.php
@@ -0,0 +1,127 @@
+<?php
+
+namespace ParamProcessor;
+
+use DataValues\DataValueObject;
+use InvalidArgumentException;
+use Title;
+
+/**
+ * Class representing a MediaWiki title value.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.1
+ *
+ * @file
+ * @ingroup DataValue
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class MediaWikiTitleValue extends DataValueObject {
+
+       /**
+        * @since 0.1
+        *
+        * @var Title
+        */
+       protected $title;
+
+       /**
+        * @since 0.1
+        *
+        * @param Title $title
+        *
+        * @throws InvalidArgumentException
+        */
+       public function __construct( Title $title ) {
+               $this->title = $title;
+       }
+
+       /**
+        * @see Serializable::serialize
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       public function serialize() {
+               return $this->title->getFullText();
+       }
+
+       /**
+        * @see Serializable::unserialize
+        *
+        * @since 0.1
+        *
+        * @param string $value
+        *
+        * @return MediaWikiTitleValue
+        */
+       public function unserialize( $value ) {
+               $this->__construct( Title::newFromText( $value ) );
+       }
+
+       /**
+        * @see DataValue::getType
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       public function getType() {
+               return 'mediawikititle';
+       }
+
+       /**
+        * @see DataValue::getSortKey
+        *
+        * @since 0.1
+        *
+        * @return string|float|int
+        */
+       public function getSortKey() {
+               return $this->title->getCategorySortkey();
+       }
+
+       /**
+        * Returns the Title object.
+        * @see DataValue::getValue
+        *
+        * @since 0.1
+        *
+        * @return Title
+        */
+       public function getValue() {
+               return $this->title;
+       }
+
+       /**
+        * Constructs a new instance of the DataValue from the provided data.
+        * This can round-trip with @see getArrayValue
+        *
+        * @since 0.1
+        *
+        * @param mixed $data
+        *
+        * @return MediaWikiTitleValue
+        */
+       public static function newFromArray( $data ) {
+               return new static( $data );
+       }
+
+}
\ No newline at end of file
diff --git a/includes/ParamProcessor/TitleParser.php 
b/includes/ParamProcessor/TitleParser.php
new file mode 100644
index 0000000..775a6e5
--- /dev/null
+++ b/includes/ParamProcessor/TitleParser.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace ParamProcessor;
+
+use ValueParsers\ParseException;
+use ValueParsers\StringValueParser;
+
+/**
+ * ValueParser that parses the string representation of a Title object.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.1
+ *
+ * @file
+ * @ingroup ValueParsers
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class TitleParser extends StringValueParser {
+
+       /**
+        * @see StringValueParser::stringParse
+        *
+        * @since 0.1
+        *
+        * @param string $value
+        *
+        * @return MediaWikiTitleValue
+        * @throws ParseException
+        */
+       protected function stringParse( $value ) {
+               $value = \Title::newFromText( $value );
+
+               if ( is_null( $value ) ) {
+                       throw new ParseException( 'Not a title' );
+               }
+
+               return new MediaWikiTitleValue( $value );
+       }
+
+}
\ No newline at end of file
diff --git a/tests/MediaWikiTitleValueTest.php 
b/tests/MediaWikiTitleValueTest.php
new file mode 100644
index 0000000..f31f44f
--- /dev/null
+++ b/tests/MediaWikiTitleValueTest.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace ParamProcessor\Tests;
+
+use DataValues\Test\DataValueTest;
+use ParamProcessor\MediaWikiTitleValue;
+
+/**
+ * Tests for the DataValues\MediaWikiTitleValue class.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since 0.1
+ *
+ * @ingroup DataValue
+ *
+ * @group DataValue
+ * @group DataValueExtensions
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class MediaWikiTitleValueTest extends DataValueTest {
+
+       /**
+        * @see DataValueTest::getClass
+        *
+        * @since 0.1
+        *
+        * @return string
+        */
+       public function getClass() {
+               return 'ParamProcessor\MediaWikiTitleValue';
+       }
+
+       /**
+        * @see DataValueTest::constructorProvider
+        *
+        * @since 0.1
+        *
+        * @return array
+        */
+       public function constructorProvider() {
+               $argLists = array();
+
+               $argLists[] = array( false );
+               $argLists[] = array( false, 42 );
+               $argLists[] = array( false, array() );
+               $argLists[] = array( false, false );
+               $argLists[] = array( false, true );
+               $argLists[] = array( false, null );
+               $argLists[] = array( false, 'foo' );
+               $argLists[] = array( false, '' );
+               $argLists[] = array( false, ' foo bar baz foo bar baz foo bar 
baz foo bar baz foo bar baz foo bar baz ' );
+
+               $argLists[] = array( true, \Title::newMainPage() );
+               $argLists[] = array( true, \Title::newFromText( 'Foobar' ) );
+
+               return $argLists;
+       }
+
+       /**
+        * @dataProvider instanceProvider
+        * @param MediaWikiTitleValue $titleValue
+        * @param array $arguments
+        */
+       public function testGetValue( MediaWikiTitleValue $titleValue, array 
$arguments ) {
+               $this->assertEquals( $arguments[0]->getFullText(), 
$titleValue->getValue()->getFullText() );
+       }
+
+}
\ No newline at end of file
diff --git a/tests/TitleParserTest.php b/tests/TitleParserTest.php
new file mode 100644
index 0000000..9616b12
--- /dev/null
+++ b/tests/TitleParserTest.php
@@ -0,0 +1,70 @@
+<?php
+
+namespace ParamProcessor\Tests;
+
+use ParamProcessor\MediaWikiTitleValue;
+use ValueParsers\Test\StringValueParserTest;
+
+/**
+ * Unit test TitleParser class.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since 0.1
+ *
+ * @ingroup ValueParsersTest
+ *
+ * @group ValueParsers
+ * @group DataValueExtensions
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class TitleParserTest extends StringValueParserTest {
+
+       /**
+        * @see ValueParserTestBase::validInputProvider
+        *
+        * @since 0.1
+        *
+        * @return array
+        */
+       public function validInputProvider() {
+               $argLists = array();
+
+               $valid = array(
+                       'Foo bar',
+                       'Ohi there!',
+               );
+
+               foreach ( $valid as $value ) {
+                       $argLists[] = array( $value, new MediaWikiTitleValue( 
\Title::newFromText( $value ) ) );
+               }
+
+               return $argLists;
+       }
+
+       /**
+        * @see ValueParserTestBase::getParserClass
+        * @since 0.1
+        * @return string
+        */
+       protected function getParserClass() {
+               return 'ParamProcessor\TitleParser';
+       }
+
+}
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifa7e628c6327d12fa4fd4f0728ef869cf541643f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Validator
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>

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

Reply via email to