Jeroen De Dauw has uploaded a new change for review. https://gerrit.wikimedia.org/r/61581
Change subject: Added MW title value - DO NOT MERGE ...................................................................... Added MW title value - DO NOT MERGE Change-Id: I4cf5150ce43bbd84f3bccf0d80550ba694ac3d78 --- A includes/utils/MediaWikiTitleValue.php A includes/utils/MediaWikiTitleValueTest.php A includes/utils/TitleParser.php A includes/utils/TitleParserTest.php 4 files changed, 334 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Validator refs/changes/81/61581/1 diff --git a/includes/utils/MediaWikiTitleValue.php b/includes/utils/MediaWikiTitleValue.php new file mode 100644 index 0000000..df06917 --- /dev/null +++ b/includes/utils/MediaWikiTitleValue.php @@ -0,0 +1,126 @@ +<?php + +namespace DataValues; + +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 StringValue + */ + 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 ); + } + +} diff --git a/includes/utils/MediaWikiTitleValueTest.php b/includes/utils/MediaWikiTitleValueTest.php new file mode 100644 index 0000000..239e481 --- /dev/null +++ b/includes/utils/MediaWikiTitleValueTest.php @@ -0,0 +1,84 @@ +<?php + +namespace DataValues\Test; + +use DataValues\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 'DataValues\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 \DataValues\MediaWikiTitleValue $titleValue + * @param array $arguments + */ + public function testGetValue( MediaWikiTitleValue $titleValue, array $arguments ) { + $this->assertEquals( $arguments[0]->getFullText(), $titleValue->getValue()->getFullText() ); + } + +} diff --git a/includes/utils/TitleParser.php b/includes/utils/TitleParser.php new file mode 100644 index 0000000..5af9ca3 --- /dev/null +++ b/includes/utils/TitleParser.php @@ -0,0 +1,55 @@ +<?php + +namespace ValueParsers; + +use DataValues\MediaWikiTitleValue; + +/** + * 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 ); + } + +} diff --git a/includes/utils/TitleParserTest.php b/includes/utils/TitleParserTest.php new file mode 100644 index 0000000..997afdb --- /dev/null +++ b/includes/utils/TitleParserTest.php @@ -0,0 +1,69 @@ +<?php + +namespace ValueParsers\Test; + +use DataValues\MediaWikiTitleValue; + +/** + * 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 'ValueParsers\TitleParser'; + } + +} -- To view, visit https://gerrit.wikimedia.org/r/61581 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I4cf5150ce43bbd84f3bccf0d80550ba694ac3d78 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
