Aleksey Bekh-Ivanov (WMDE) has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/330405 )
Change subject: Added item reference field type
......................................................................
Added item reference field type
Will be used in WikibaseLexeme SpecialNewLexeme page
Bug: T150205
Change-Id: I575d350eebef3e1f47291f4efd65940a66a3e04f
---
M repo/i18n/en.json
M repo/i18n/qqq.json
A repo/includes/Specials/HTMLForm/HTMLItemReferenceField.php
A repo/tests/phpunit/includes/Specials/HTMLForm/HTMLItemReferenceFieldTest.php
4 files changed, 172 insertions(+), 0 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/05/330405/1
diff --git a/repo/i18n/en.json b/repo/i18n/en.json
index 1fddc2b..eb9c72d 100644
--- a/repo/i18n/en.json
+++ b/repo/i18n/en.json
@@ -134,6 +134,9 @@
"wikibase-itemlink-id-wrapper": "($1)",
"wikibase-itemlink-userlang-wrapper": " ($1: $2)",
"wikibase-itemlink-title": "$1 | $2",
+ "wikibase-item-reference-edit-placeholder": "Enter item id e.g. Q10",
+ "wikibase-item-reference-edit-invalid-format": "Entered item id has
invalid format",
+ "wikibase-item-reference-edit-nonexistent-item": "Item with this id
does not exist",
"wikibase-wikibaserepopage-not-itemid": "\"$1\" is not a valid item
ID.",
"wikibase-wikibaserepopage-invalid-langcode": "The language code \"$1\"
is unknown. Please use a language code known to the system, such as \"en\".",
"wikibase-wikibaserepopage-invalid-id": "The ID \"$1\" is unknown to
the system. Please use a valid entity ID.",
diff --git a/repo/i18n/qqq.json b/repo/i18n/qqq.json
index 2bd8d5f..5d00644 100644
--- a/repo/i18n/qqq.json
+++ b/repo/i18n/qqq.json
@@ -165,6 +165,9 @@
"wikibase-itemlink-id-wrapper": "{{optional}}\nUsed to wrap the items
id for a link to an item in parentheses or similar. Params:\n* $1 is the
prefixed id of the item",
"wikibase-itemlink-userlang-wrapper": "{{optional}}\nUsed to wrap the
language and label for an additional entry when the language are different from
the user's own. Parameters:\n* $1 is the label;s language name\n* $2 is the
label text",
"wikibase-itemlink-title": "{{optional}}\nTitle attribute set in links
to an item. $1 is the items label or (if not available) the prefixed page title
of the item. $2 is the items description. Parameters:\n* $1 label as a
directional string\n* $2 description as a directional string",
+ "wikibase-item-reference-edit-placeholder": "This is a generic text
used as a placeholder while editing an item reference field.",
+ "wikibase-item-reference-edit-invalid-format": "Error message displayed
when text entered in the form field that supposed to contain item id does not
match expected format.",
+ "wikibase-item-reference-edit-nonexistent-item": "Error message
displayed when item id entered in the form field that supposed to contain item
id contains id of nonexistent item.",
"wikibase-wikibaserepopage-not-itemid": "Error message when an entity
id, other than item id, is entered. The message advises users that the id is
invalid.\n\nParameters:\n* $1 - the invalid id",
"wikibase-wikibaserepopage-invalid-langcode": "Response informing that
the language code is not valid. Could give an example of a valid language code.
Feel free to change \"en\" to your language code(s).\n\nParameters:\n* $1 - the
invalid code\n\n\"language identifier\" is the same as \"language code\".\nSee
also:\n* {{msg-mw|wikibase-validator-not-a-language}}",
"wikibase-wikibaserepopage-invalid-id": "Response informing that the
selected entity ID is not valid.\n\nParameters:\n* $1 - the invalid ID",
diff --git a/repo/includes/Specials/HTMLForm/HTMLItemReferenceField.php
b/repo/includes/Specials/HTMLForm/HTMLItemReferenceField.php
new file mode 100644
index 0000000..355bc04
--- /dev/null
+++ b/repo/includes/Specials/HTMLForm/HTMLItemReferenceField.php
@@ -0,0 +1,63 @@
+<?php
+
+namespace Wikibase\Repo\Specials\HTMLForm;
+
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Services\Lookup\EntityLookup;
+use Wikibase\Repo\WikibaseRepo;
+
+/**
+ * Class representing generic form field referencing item by its id
+ *
+ * @license GPL-2.0+
+ */
+class HTMLItemReferenceField extends \HTMLTextField {
+ /**
+ * @var EntityLookup
+ */
+ private $entityLookup;
+
+ /**
+ * - Can be used without placeholder - has some predefined value.
+ * - Doesn't accept `type` parameter.
+ *
+ * @inheritdoc
+ *
+ * @see \HTMLForm There is detailed description of the allowed $params
(named $info there).
+ */
+ public function __construct( array $params, EntityLookup $entityLookup
= null ) {
+ if ( isset( $params['type'] ) ) {
+ throw new \InvalidArgumentException( "Cannot use `type`
for item reference field" );
+ }
+
+ $defaultValues = [ 'placeholder-message' =>
'wikibase-item-reference-edit-placeholder' ];
+
+ // TODO Placeholder message
+
+ $params['type'] = 'text';
+ parent::__construct( array_merge( $defaultValues, $params ) );
+
+ $this->entityLookup = $entityLookup ?:
WikibaseRepo::getDefaultInstance()->getEntityLookup();
+ }
+
+ public function validate( $value, $alldata ) {
+ $required = isset( $this->mParams['required'] ) &&
$this->mParams['required'] !== false;
+
+ if ( !$required && $value === '' ) {
+ return true;
+ }
+
+ if ( !preg_match( ItemId::PATTERN, $value ) ) {
+ // FIXME add text in language files
+ return $this->msg(
'wikibase-item-reference-edit-invalid-format' );
+ }
+
+ if ( !$this->entityLookup->hasEntity( new ItemId( $value ) ) ) {
+ // FIXME add text in language files
+ return $this->msg(
'wikibase-item-reference-edit-nonexistent-item' );
+ }
+
+ return parent::validate( $value, $alldata );
+ }
+
+}
diff --git
a/repo/tests/phpunit/includes/Specials/HTMLForm/HTMLItemReferenceFieldTest.php
b/repo/tests/phpunit/includes/Specials/HTMLForm/HTMLItemReferenceFieldTest.php
new file mode 100644
index 0000000..04dc374
--- /dev/null
+++
b/repo/tests/phpunit/includes/Specials/HTMLForm/HTMLItemReferenceFieldTest.php
@@ -0,0 +1,103 @@
+<?php
+
+namespace Wikibase\Repo\Tests\Specials\HTMLForm;
+
+use Wikibase\DataModel\Entity\Item;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup;
+use Wikibase\Repo\Specials\HTMLForm\HTMLItemReferenceField;
+
+/**
+ * @covers Wikibase\Repo\Specials\HTMLForm\HTMLItemReferenceField
+ *
+ * @group Wikibase
+ *
+ * @license GPL-2.0+
+ */
+class HTMLItemReferenceFieldTest extends \PHPUnit_Framework_TestCase {
+ /**
+ * @var InMemoryEntityLookup
+ */
+ private $entityLookup;
+
+ protected function setUp() {
+ $this->entityLookup = new InMemoryEntityLookup();
+ }
+
+ /**
+ * Test ensures that client won't be able to set type of input field,
because it will not work
+ * with any type except "text" which it sets internally {@see
testSetsTypeToText_WhenCreated}
+ */
+ public function testThrowsExceptionIfTypeParameterIsSet_WhenCreated() {
+ $this->setExpectedException( \Exception::class );
+
+ $this->createField( [ 'type' => 'some-type', ] );
+ }
+
+ public function testSetsTypeToText_WhenCreated() {
+ $field = $this->createField();
+
+ self::assertEquals( 'text', $field->mParams['type'] );
+ }
+
+ public function
testValidationFailsWithInvalidFormatMessage_WhenEnteredTextDoesNotMatchItemIdFormat()
{
+ $field = $this->createField();
+
+ /** @var \Message $failureMessage */
+ $failureMessage = $field->validate( 'x', [] );
+
+ self::assertEquals(
'wikibase-item-reference-edit-invalid-format', $failureMessage->getKey() );
+ }
+
+ public function
testValidationFailsWithNonexistentItemMessage_WhenItemHavingEnteredIdDoesNotExist()
{
+ $field = $this->createField();
+
+ /** @var \Message $failureMessage */
+ $failureMessage = $field->validate( 'Q2', [] );
+
+ self::assertEquals(
'wikibase-item-reference-edit-nonexistent-item', $failureMessage->getKey() );
+ }
+
+ public function
testValidationPasses_WhenEmptyStringGivenAndFieldIsNotRequired() {
+ $field = $this->createField();
+
+ $result = $field->validate( '', [] );
+
+ self::assertTrue( $result );
+ }
+
+ public function
testValidationCallbackExecuted_WhenReferencedItemExists() {
+ $this->givenItemExists( $existingItemId = 'Q1' );
+ $field = $this->createField(
+ [
+ 'validation-callback' => function () {
+ return wfMessage( 'some-message' );
+ },
+ ]
+ );
+
+ /** @var \Message $failureMessage */
+ $failureMessage = $field->validate( $existingItemId, [] );
+
+ self::assertEquals( 'some-message', $failureMessage->getKey() );
+ }
+
+ /**
+ * @return HTMLItemReferenceField
+ */
+ protected function createField( $params = [] ) {
+ $paramsRequiredByParentClass = [
+ 'fieldname' => 'some-name',
+ ];
+
+ return new HTMLItemReferenceField( array_merge(
$paramsRequiredByParentClass, $params ), $this->entityLookup );
+ }
+
+ /**
+ * @param string $itemId
+ */
+ private function givenItemExists( $itemId ) {
+ $this->entityLookup->addEntity( new Item( new ItemId( $itemId )
) );
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/330405
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I575d350eebef3e1f47291f4efd65940a66a3e04f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aleksey Bekh-Ivanov (WMDE) <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits