jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/328890 )
Change subject: Introduced form field for content language ...................................................................... Introduced form field for content language Change-Id: I1c432364143a346693f1e6cbc5085e83c7ecd2d6 --- M repo/i18n/en.json M repo/i18n/qqq.json A repo/includes/Specials/HTMLForm/HTMLContentLanguageField.php M repo/includes/Specials/SpecialNewItem.php M repo/includes/Specials/SpecialNewProperty.php A repo/tests/phpunit/includes/Specials/HTMLForm/HTMLContentLanguageFieldTest.php 6 files changed, 172 insertions(+), 28 deletions(-) Approvals: jenkins-bot: Verified Thiemo Mättig (WMDE): Looks good to me, approved diff --git a/repo/i18n/en.json b/repo/i18n/en.json index 727b9f8..1fddc2b 100644 --- a/repo/i18n/en.json +++ b/repo/i18n/en.json @@ -50,6 +50,8 @@ "wikibase-aliases-edit-placeholder": "enter some aliases", "wikibase-aliases-edit-placeholder-language-aware": "enter some aliases in $1", "wikibase-aliases-edit-label": "Aliases, pipe-separated:", + "wikibase-content-language-edit-label": "Language:", + "wikibase-content-language-edit-not-recognized-language": "The supplied language code was not recognized.", "wikibase-diffview-reference": "reference", "wikibase-diffview-rank": "rank", "wikibase-diffview-rank-preferred": "Preferred rank", @@ -182,7 +184,6 @@ "wikibase-newitem-page": "Name of the first linked page", "wikibase-newitem-no-external-page": "A page \"$2\" could not be found on \"$1\".", "wikibase-newitem-not-recognized-siteid": "The provided site identifier was not recognized.", - "wikibase-newitem-not-recognized-language": "The supplied language code was not recognized.", "wikibase-newitem-insufficient-data": "You need to fill at least either label, description or aliases.", "wikibase-newentity-language": "Language:", "wikibase-newentity-label": "Label:", diff --git a/repo/i18n/qqq.json b/repo/i18n/qqq.json index af7c8f1..2bd8d5f 100644 --- a/repo/i18n/qqq.json +++ b/repo/i18n/qqq.json @@ -81,6 +81,8 @@ "wikibase-aliases-edit-placeholder": "This is a generic text used as a placeholder while editing a new list of pipe-separated aliases.", "wikibase-aliases-edit-placeholder-language-aware": "Like {{msg-mw|Wikibase-aliases-edit-placeholder}}, but language ($1) aware.", "wikibase-aliases-edit-label": "Generic label for the \"aliases\" text field holding the pipe-separated aliases.", + "wikibase-content-language-edit-label": "Generic label for the \"language\" field.\n{{Identical|Language}}", + "wikibase-content-language-edit-not-recognized-language": "Error message shown when a user enters an unknown language code into a \"language\" field.\n\nSee also:\n* {{msg-mw|Wikibase-api-not-recognized-language}}", "wikibase-diffview-reference": "Label within the header of a diff-operation on the entity diff view to describe that the diff-operation affects a reference. Will be shown as e.g. \"claim / property q1 / reference\".\n{{Identical|Reference}}", "wikibase-diffview-rank": "Label within the header of a diff-operation on the entity diff view to describe that the diff-operation affects the rank of the statement. Will be shown as e.g. \"claim / property q1 / rank\".\n{{Identical|Rank}}", "wikibase-diffview-rank-preferred": "The [[d:Wikidata:Glossary#Rank-preferred|Preferred Rank]] to be shown in diffs.", @@ -213,7 +215,6 @@ "wikibase-newitem-page": "Label for the page-textfield holding the page name of the first linked page to add to the new item", "wikibase-newitem-no-external-page": "Error message shown when a user tries to add a page that does not exist on the site chosen.\n\nParameters:\n* $1 - global site ID, known to be valid\n* $2 - requested page name that could not be found on the site\n{{Related|Wikibase-no-external-page}}", "wikibase-newitem-not-recognized-siteid": "Error message shown when a user tries to add a link to a site with an unknown identifier.\n\nSee also:\n* {{msg-mw|Wikibase-api-not-recognized-siteid}}", - "wikibase-newitem-not-recognized-language": "Error message shown when a user tries to create an item with an unknown language code.\n\nSee also:\n* {{msg-mw|Wikibase-api-not-recognized-language}}", "wikibase-newitem-insufficient-data": "Error message shown when a user tries to create an item and didn't fill in enough data", "wikibase-newentity-language": "Name for \"language\"", "wikibase-newentity-label": "Label for the \"label\" textfield holding the label of the new item.\n{{Identical|Label}}", diff --git a/repo/includes/Specials/HTMLForm/HTMLContentLanguageField.php b/repo/includes/Specials/HTMLForm/HTMLContentLanguageField.php new file mode 100644 index 0000000..28e5a05 --- /dev/null +++ b/repo/includes/Specials/HTMLForm/HTMLContentLanguageField.php @@ -0,0 +1,79 @@ +<?php + +namespace Wikibase\Repo\Specials\HTMLForm; + +use HTMLComboboxField; +use InvalidArgumentException; +use Wikibase\Lib\LanguageNameLookup; +use Wikibase\Repo\WikibaseRepo; + +/** + * Class representing generic content language selector field + * + * @license GPL-2.0+ + */ +class HTMLContentLanguageField extends HTMLComboboxField { + + /** + * Can be used without label - has some predefined value. + * - Doesn't accept any of options parameters. + * + * @inheritdoc + * + * @see \HTMLForm There is detailed description of the allowed $params (named $info there). + */ + public function __construct( array $params ) { + $defaultParameters = [ + 'label-message' => 'wikibase-content-language-edit-label', + ]; + + if ( isset( $params['parent'] ) && $params['parent'] instanceof \IContextSource ) { + /** @var \IContextSource $form */ + $form = $params['parent']; + $params['default'] = $form->getLanguage()->getCode(); + } + + if ( isset( $params['options'] ) + || isset( $params['options-message'] ) + || isset( $params['options-messages'] ) + ) { + throw new InvalidArgumentException( + "Cannot set options for content language field. It already has it's own options" + ); + } + + $wikibaseRepo = WikibaseRepo::getDefaultInstance(); + $contentLanguages = $wikibaseRepo->getTermsLanguages(); + $params['options'] = $this->constructOptions( + $contentLanguages->getLanguages(), + $wikibaseRepo->getLanguageNameLookup() + ); + + parent::__construct( array_merge( $defaultParameters, $params ) ); + } + + /** + * @param string[] $languageCodes + * @param LanguageNameLookup $lookup + * @return array For details see {@see \HTMLForm} "options" parameter description + */ + private function constructOptions( array $languageCodes, LanguageNameLookup $lookup ) { + $languageOptions = []; + foreach ( $languageCodes as $code ) { + $languageName = $lookup->getName( $code ); + $languageOptions["$languageName ($code)"] = $code; + } + + return $languageOptions; + } + + public function validate( $value, $alldata ) { + $options = $this->getOptions(); + if ( !in_array( $value, $options, true ) ) { + return $this->msg( 'wikibase-content-language-edit-not-recognized-language' ); + } + + return parent::validate( $value, $alldata ); + } + +} diff --git a/repo/includes/Specials/SpecialNewItem.php b/repo/includes/Specials/SpecialNewItem.php index 717e28f..d48aba8 100644 --- a/repo/includes/Specials/SpecialNewItem.php +++ b/repo/includes/Specials/SpecialNewItem.php @@ -10,6 +10,7 @@ use Wikibase\DataModel\Term\Term; use Wikibase\Repo\Specials\HTMLForm\HTMLAliasesField; use Wikibase\Repo\Specials\HTMLForm\HTMLTrimmedTextField; +use Wikibase\Repo\Specials\HTMLForm\HTMLContentLanguageField; use Wikibase\Summary; /** @@ -90,24 +91,11 @@ * @return array[] */ protected function getFormFields() { - $langCode = $this->getLanguage()->getCode(); - $formFields = [ self::FIELD_LANG => [ 'name' => self::FIELD_LANG, - 'options' => $this->getLanguageOptions(), - 'default' => $langCode, - 'type' => 'combobox', + 'class' => HTMLContentLanguageField::class, 'id' => 'wb-newentity-language', - 'filter-callback' => [ $this->stringNormalizer, 'trimToNFC' ], - 'validation-callback' => function ( $language ) { - if ( !in_array( $language, $this->languageCodes ) ) { - return [ $this->msg( 'wikibase-newitem-not-recognized-language' )->text() ]; - } - - return true; - }, - 'label-message' => 'wikibase-newentity-language', ], self::FIELD_LABEL => [ 'name' => self::FIELD_LABEL, diff --git a/repo/includes/Specials/SpecialNewProperty.php b/repo/includes/Specials/SpecialNewProperty.php index 5a0aa21..65d9c9a 100644 --- a/repo/includes/Specials/SpecialNewProperty.php +++ b/repo/includes/Specials/SpecialNewProperty.php @@ -9,6 +9,7 @@ use Wikibase\DataTypeSelector; use Wikibase\Repo\Specials\HTMLForm\HTMLAliasesField; use Wikibase\Repo\Specials\HTMLForm\HTMLTrimmedTextField; +use Wikibase\Repo\Specials\HTMLForm\HTMLContentLanguageField; use Wikibase\Repo\WikibaseRepo; use Wikibase\Summary; @@ -86,19 +87,8 @@ $formFields = [ self::FIELD_LANG => [ 'name' => self::FIELD_LANG, - 'options' => $this->getLanguageOptions(), - 'default' => $langCode, - 'type' => 'combobox', + 'class' => HTMLContentLanguageField::class, 'id' => 'wb-newentity-language', - 'filter-callback' => [ $this->stringNormalizer, 'trimToNFC' ], - 'validation-callback' => function ( $language ) { - if ( !in_array( $language, $this->languageCodes ) ) { - return [ $this->msg( 'wikibase-newitem-not-recognized-language' )->text() ]; - } - - return true; - }, - 'label-message' => 'wikibase-newentity-language' ], self::FIELD_LABEL => [ 'name' => self::FIELD_LABEL, diff --git a/repo/tests/phpunit/includes/Specials/HTMLForm/HTMLContentLanguageFieldTest.php b/repo/tests/phpunit/includes/Specials/HTMLForm/HTMLContentLanguageFieldTest.php new file mode 100644 index 0000000..daf2dff --- /dev/null +++ b/repo/tests/phpunit/includes/Specials/HTMLForm/HTMLContentLanguageFieldTest.php @@ -0,0 +1,85 @@ +<?php + +namespace Wikibase\Repo\Tests\Specials\HTMLForm; + +use Wikibase\Repo\Specials\HTMLForm\HTMLContentLanguageField; + +/** + * @covers Wikibase\Repo\Specials\HTMLForm\HTMLContentLanguageField + * + * @group Wikibase + */ +class HTMLContentLanguageFieldTest extends \MediaWikiTestCase { + + /** + * @dataProvider provideVariantsToDefineOptionsForTheField + */ + public function testDoesNotAllowToSetOptions_WhenCreated( $params ) { + + $this->setExpectedException( \InvalidArgumentException::class ); + $this->createField( $params ); + } + + public function provideVariantsToDefineOptionsForTheField() { + return [ + 'options' => [ + [ + 'options' => [], + ], + ], + 'options-messages' => [ + [ + 'options-messages' => [], + ], + ], + 'options-message' => [ + [ + 'options-message' => [], + ], + ], + + ]; + } + + public function testSetsDefaultValueToLanguageFromParentElement_WhenCreatedAndDefaultIsNotDefined() { + $params = [ + 'parent' => $this->createNewContextSourceWithLanguage( 'some-language' ), + ]; + + $field = $this->createField( $params ); + + self::assertEquals( 'some-language', $field->getDefault() ); + } + + public function testUsesDefaultValue_WhenDefaultIsDefined() { + $params = [ + 'default' => 'default-language', + ]; + + $field = $this->createField( $params ); + + self::assertEquals( 'default-language', $field->getDefault() ); + } + + private function createNewContextSourceWithLanguage( $langCode ) { + $mock = $this->getMock( \IContextSource::class ); + + $language = new \Language(); + $language->setCode( $langCode ); + + $mock->method( 'getLanguage' )->willReturn( $language ); + + return $mock; + } + + /** + * @param $params + * @return HTMLContentLanguageField + */ + private function createField( $params ) { + $requiredByBaseClass = [ 'fieldname' => 'some-name', ]; + + return new HTMLContentLanguageField( array_merge( $requiredByBaseClass, $params ) ); + } + +} -- To view, visit https://gerrit.wikimedia.org/r/328890 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I1c432364143a346693f1e6cbc5085e83c7ecd2d6 Gerrit-PatchSet: 16 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Aleksey Bekh-Ivanov (WMDE) <aleksey.bekh-iva...@wikimedia.de> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Aleksey Bekh-Ivanov (WMDE) <aleksey.bekh-iva...@wikimedia.de> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: WMDE-leszek <leszek.mani...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits