jenkins-bot has submitted this change and it was merged.

Change subject: SpecialNewLexeme extending SpecialWikibaseRepoPage
......................................................................


SpecialNewLexeme extending SpecialWikibaseRepoPage

Bug: T149687
Change-Id: I745f5d2b15954077aa2dcf2db28ad5dea5b73b51
---
M WikibaseLexeme.i18n.alias.php
M extension.json
M i18n/en.json
M i18n/qqq.json
A src/Specials/SpecialNewLexeme.php
A tests/phpunit/mediawiki/Specials/SpecialNewLexemeTest.php
6 files changed, 383 insertions(+), 3 deletions(-)

Approvals:
  Ladsgroup: Looks good to me, approved
  Jonas Kress (WMDE): Looks good to me, approved
  jenkins-bot: Verified



diff --git a/WikibaseLexeme.i18n.alias.php b/WikibaseLexeme.i18n.alias.php
index ad762ba..ef8ee5e 100644
--- a/WikibaseLexeme.i18n.alias.php
+++ b/WikibaseLexeme.i18n.alias.php
@@ -7,4 +7,6 @@
  */
 $specialPageAliases = [];
 /** English (English) */
-$specialPageAliases['en'] = [];
+$specialPageAliases['en'] = [
+       'NewLexeme' => [ 'Newlexeme' ]
+];
diff --git a/extension.json b/extension.json
index e687440..fe4f4b1 100644
--- a/extension.json
+++ b/extension.json
@@ -16,6 +16,9 @@
                "LexemeNamespace": 146,
                "LexemeTalkNamespace": 147
        },
+       "ExtensionMessagesFiles": {
+               "WikibaseLexemeAlias": "WikibaseLexeme.i18n.alias.php"
+       },
        "Hooks": {
                "WikibaseEntityNamespaces": [
                        
"Wikibase\\Lexeme\\WikibaseLexemeHooks::onWikibaseEntityNamespaces"
@@ -34,5 +37,8 @@
        },
        "ResourceModules": {},
        "ResourceFileModulePaths": {},
+       "SpecialPages": {
+               "NewLexeme": "Wikibase\\Lexeme\\Specials\\SpecialNewLexeme"
+        },
        "manifest_version": 1
 }
diff --git a/i18n/en.json b/i18n/en.json
index 904e019..2f800f8 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -2,5 +2,9 @@
        "@metadata": {
                "authors": []
        },
-       "wikibase-lexeme-desc": "Extension to handle lexeme entity type."
+       "wikibase-lexeme-desc": "Extension to handle lexeme entity type.",
+       "special-newlexeme": "Create a new lexeme",
+       "wikibase-newlexeme-fieldset": "Create a new lexeme",
+       "wikibase-newentity-lemma": "Lemma",
+       "wikibase-lemma-edit-placeholder": "Enter the lemma"
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index ea1937d..cc71c35 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -2,5 +2,9 @@
        "@metadata": {
                "authors": []
        },
-       "wikibase-lexeme-desc": 
"{{desc|name=WikibaseLexeme|url=https://www.mediawiki.org/wiki/Extension:WikibaseLexeme}}";
+       "wikibase-lexeme-desc": 
"{{desc|name=WikibaseLexeme|url=https://www.mediawiki.org/wiki/Extension:WikibaseLexeme}}";,
+       "special-newlexeme": "Title of Special:NewLexeme",
+       "wikibase-newlexeme-fieldset": "Title of the form in Special:NewLexeme",
+       "wikibase-newentity-lemma": "Name for \"Lemma\"",
+       "wikibase-lemma-edit-placeholder": "Placeholder content for lemma"
 }
diff --git a/src/Specials/SpecialNewLexeme.php 
b/src/Specials/SpecialNewLexeme.php
new file mode 100644
index 0000000..57425c7
--- /dev/null
+++ b/src/Specials/SpecialNewLexeme.php
@@ -0,0 +1,311 @@
+<?php
+
+namespace Wikibase\Lexeme\Specials;
+
+use Html;
+use HTMLForm;
+use InvalidArgumentException;
+use Status;
+use Wikibase\CopyrightMessageBuilder;
+use Wikibase\DataModel\Entity\EntityDocument;
+use Wikibase\DataModel\Term\FingerprintProvider;
+use Wikibase\Lexeme\DataModel\Lexeme;
+use Wikibase\Lib\LanguageNameLookup;
+use Wikibase\Repo\WikibaseRepo;
+use Wikibase\Repo\Specials\SpecialWikibaseRepoPage;
+use Wikibase\Repo\Specials\SpecialPageCopyrightView;
+use Wikibase\Summary;
+use Wikibase\View\LanguageDirectionalityLookup;
+
+/**
+ * Page for creating new Lexeme entities that contain a Fingerprint.
+ * Mostly copied from SpecialNewEntity
+ *
+ * @license GPL-2.0+
+ */
+class SpecialNewLexeme extends SpecialWikibaseRepoPage {
+
+       /**
+        * Contains pieces of the sub-page name of this special page if a 
subpage was called.
+        * E.g. [ 'a', 'b' ] in case of 'Special:NewLexeme/a/b'
+        * @var string[]|null
+        */
+       protected $parts = null;
+
+       /**
+        * @var string|null
+        */
+       private $lemma;
+
+       /**
+        * @var string
+        */
+       private $contentLanguageCode;
+
+       /**
+        * @var string[]
+        */
+       private $languageCodes;
+
+       /**
+        * @var SpecialPageCopyrightView
+        */
+       private $copyrightView;
+
+       /**
+        * @var LanguageDirectionalityLookup
+        */
+       private $languageDirectionalityLookup;
+
+       /**
+        * @var LanguageNameLookup
+        */
+       private $languageNameLookup;
+
+       public function __construct() {
+               parent::__construct( 'NewLexeme', 'createpage' );
+               $wikibaseRepo = WikibaseRepo::getDefaultInstance();
+
+               $settings = $wikibaseRepo->getSettings();
+               $this->copyrightView = new SpecialPageCopyrightView(
+                       new CopyrightMessageBuilder(),
+                       $settings->getSetting( 'dataRightsUrl' ),
+                       $settings->getSetting( 'dataRightsText' )
+               );
+               $this->languageCodes = 
$wikibaseRepo->getTermsLanguages()->getLanguages();
+               $this->languageDirectionalityLookup = 
$wikibaseRepo->getLanguageDirectionalityLookup();
+               $this->languageNameLookup = 
$wikibaseRepo->getLanguageNameLookup();
+       }
+
+       public function doesWrites() {
+               return true;
+       }
+
+       /**
+        * @see SpecialWikibasePage::execute
+        *
+        * @param string|null $subPage
+        */
+       public function execute( $subPage ) {
+               parent::execute( $subPage );
+
+               $this->checkPermissions();
+               $this->checkBlocked();
+               $this->checkReadOnly();
+
+               $this->parts = ( $subPage === '' ? [] : explode( '/', $subPage 
) );
+               $this->prepareArguments();
+
+               $out = $this->getOutput();
+
+               $uiLanguageCode = $this->getLanguage()->getCode();
+
+               if ( $this->getRequest()->wasPosted()
+                    && $this->getUser()->matchEditToken( 
$this->getRequest()->getVal( 'wpEditToken' ) )
+               ) {
+                       if ( $this->hasSufficientArguments() ) {
+                               $entity = $this->createEntity();
+
+                               $status = $this->modifyEntity( $entity );
+
+                               if ( $status->isGood() ) {
+                                       $summary = new Summary( 'wbeditentity', 
'create' );
+                                       $summary->setLanguage( $uiLanguageCode 
);
+                                       $summary->addAutoSummaryArgs( 
$this->lemma );
+
+                                       $status = $this->saveEntity(
+                                               $entity,
+                                               $summary,
+                                               $this->getRequest()->getVal( 
'wpEditToken' ),
+                                               EDIT_NEW
+                                       );
+                                       var_dump( 'can be saved' );
+                                       $out = $this->getOutput();
+
+                                       if ( !$status->isOK() ) {
+                                               var_dump( 'here' );
+                                               $out->addHTML( '<div 
class="error">' );
+                                               $out->addWikiText( 
$status->getWikiText() );
+                                               $out->addHTML( '</div>' );
+                                       } elseif ( $entity !== null ) {
+                                               $title = $this->getEntityTitle( 
$entity->getId() );
+                                               $entityUrl = 
$title->getFullURL();
+                                               $this->getOutput()->redirect( 
$entityUrl );
+                                       }
+                               } else {
+                                       $out->addHTML( '<div class="error">' );
+                                       $out->addHTML( $status->getHTML() );
+                                       $out->addHTML( '</div>' );
+                               }
+                       }
+               }
+
+               $this->getOutput()->addModuleStyles( [ 'wikibase.special' ] );
+
+               foreach ( $this->getWarnings() as $warning ) {
+                       $out->addHTML( Html::element( 'div', [ 'class' => 
'warning' ], $warning ) );
+               }
+
+               $this->createForm( $this->getLegend(), 
$this->additionalFormElements() );
+       }
+
+       /**
+        * Tries to extract argument values from web request or of the page's 
sub-page parts
+        *
+        * Trimming argument values from web request.
+        */
+       protected function prepareArguments() {
+               $lemma = $this->getRequest()->getVal(
+                       'lemma',
+                       isset( $this->parts[0] ) ? $this->parts[0] : ''
+               );
+               $this->lemma = $this->stringNormalizer->trimToNFC( $lemma );
+
+               $this->contentLanguageCode = $this->getRequest()->getVal(
+                       'lang', $this->getLanguage()->getCode()
+               );
+       }
+
+       /**
+        * Checks whether required arguments are set sufficiently
+        *
+        * @return bool
+        */
+       protected function hasSufficientArguments() {
+               return $this->lemma !== '';
+       }
+
+       /**
+        * @see SpecialNewEntity::createEntity
+        */
+       protected function createEntity() {
+               return new Lexeme();
+       }
+
+       /**
+        * Attempt to modify entity
+        *
+        * @param EntityDocument &$entity
+        *
+        * @throws InvalidArgumentException
+        * @return Status
+        */
+       protected function modifyEntity( EntityDocument &$entity ) {
+               if ( !( $entity instanceof FingerprintProvider ) ) {
+                       throw new InvalidArgumentException( '$entity must be a 
FingerprintProvider' );
+               }
+
+               $fingerprint = $entity->getFingerprint();
+               $status = Status::newGood();
+
+               $languageCode = $this->contentLanguageCode;
+               if ( !in_array( $languageCode, $this->languageCodes ) ) {
+                       $status->error( 
'wikibase-newitem-not-recognized-language' );
+                       return $status;
+               }
+
+               $fingerprint->setLabel( $languageCode, $this->lemma );
+
+               return $status;
+       }
+
+       /**
+        * Get options for language selector
+        *
+        * @return string[]
+        */
+       private function getLanguageOptions() {
+               $languageOptions = [];
+               foreach ( $this->languageCodes as $code ) {
+                       $languageName = $this->languageNameLookup->getName( 
$code );
+                       $languageOptions["$languageName ($code)"] = $code;
+               }
+               return $languageOptions;
+       }
+
+       /**
+        * @return array[]
+        */
+       protected function additionalFormElements() {
+               $this->getOutput()->addModules( 
'wikibase.special.languageLabelDescriptionAliases' );
+
+               $langCode = $this->contentLanguageCode;
+               $langDir = 
$this->languageDirectionalityLookup->getDirectionality( 
$this->contentLanguageCode );
+               return [
+                       'lang' => [
+                               'name' => 'lang',
+                               'options' => $this->getLanguageOptions(),
+                               'default' => $langCode,
+                               'type' => 'combobox',
+                               'id' => 'wb-newentity-language',
+                               'label-message' => 'wikibase-newentity-language'
+                       ],
+                       'lemma' => [
+                               'name' => 'lemma',
+                               'default' => $this->lemma ?: '',
+                               'type' => 'text',
+                               'id' => 'wb-newentity-label',
+                               'lang' => $langCode,
+                               'dir' => $langDir,
+                               'placeholder' => $this->msg(
+                                       'wikibase-lemma-edit-placeholder'
+                               )->text(),
+                               'label-message' => 'wikibase-newentity-lemma'
+                       ]
+               ];
+       }
+
+       /**
+        * Building the HTML form for creating a new item.
+        *
+        * @param string|null $legend initial value for the label input box
+        * @param array[] $additionalFormElements initial value for the 
description input box
+        */
+       private function createForm( $legend = null, array 
$additionalFormElements ) {
+               $this->addCopyrightText();
+
+               HTMLForm::factory( 'ooui', $additionalFormElements, 
$this->getContext() )
+                       ->setId( 'mw-newentity-form1' )
+                       ->setSubmitID( 'wb-newentity-submit' )
+                       ->setSubmitName( 'submit' )
+                       ->setSubmitTextMsg( 'wikibase-newentity-submit' )
+                       ->setWrapperLegendMsg( $legend )
+                       ->setSubmitCallback( function () {// no-op
+                       } )->show();
+       }
+
+       /**
+        * @todo could factor this out into a special page form builder and 
renderer
+        */
+       private function addCopyrightText() {
+               $html = $this->copyrightView->getHtml( $this->getLanguage(), 
'wikibase-newentity-submit' );
+
+               $this->getOutput()->addHTML( $html );
+       }
+
+       /**
+        * @see SpecialNewEntity::getLegend()
+        */
+       protected function getLegend() {
+               return $this->msg( 'wikibase-newlexeme-fieldset' );
+       }
+
+       /**
+        * @see SpecialCreateEntity::getWarnings
+        *
+        * @return string[]
+        */
+       protected function getWarnings() {
+               if ( $this->getUser()->isAnon() ) {
+                       return [
+                               $this->msg(
+                                       'wikibase-anonymouseditwarning',
+                                       $this->msg( 'wikibase-entity-lexeme' )
+                               ),
+                       ];
+               }
+
+               return [];
+       }
+
+}
diff --git a/tests/phpunit/mediawiki/Specials/SpecialNewLexemeTest.php 
b/tests/phpunit/mediawiki/Specials/SpecialNewLexemeTest.php
new file mode 100644
index 0000000..34c5fad
--- /dev/null
+++ b/tests/phpunit/mediawiki/Specials/SpecialNewLexemeTest.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace Wikibase\Lexeme\Tests\Specials;
+
+use SpecialPageTestBase;
+use Wikibase\Lexeme\Specials\SpecialNewLexeme;
+use Wikibase\Repo\Specials\SpecialNewProperty;
+
+/**
+ * @covers Wikibase\Lexeme\Specials\SpecialNewLexeme
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ * @group SpecialPage
+ * @group WikibaseSpecialPage
+ *
+ * @license GPL-2.0+
+ * @author Amir Sarabadani <[email protected]>
+ */
+class SpecialNewLemexeTest extends SpecialPageTestBase {
+
+       protected function newSpecialPage() {
+               return new SpecialNewLexeme();
+       }
+
+       public function testExecute() {
+
+               $matchers['lemma'] = [
+                       'tag' => 'div',
+                       'attributes' => [
+                               'id' => 'wb-newentity-label',
+                       ],
+                       'child' => [
+                               'tag' => 'input',
+                               'attributes' => [
+                                       'name' => 'lemma',
+                               ]
+                       ] ];
+
+               list( $output, ) = $this->executeSpecialPage( '' );
+               foreach ( $matchers as $key => $matcher ) {
+                       $this->assertTag( $matcher, $output, "Failed to match 
html output with tag '{$key}''" );
+               }
+
+               list( $output, ) = $this->executeSpecialPage( 'LemmaText' );
+               $matchers['lemma']['child'][0]['attributes']['value'] = 
'LemmaText';
+
+               foreach ( $matchers as $key => $matcher ) {
+                       $this->assertTag( $matcher, $output, "Failed to match 
html output with tag '{$key}''" );
+               }
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I745f5d2b15954077aa2dcf2db28ad5dea5b73b51
Gerrit-PatchSet: 10
Gerrit-Project: mediawiki/extensions/WikibaseLexeme
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: WMDE-leszek <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to