John Erling Blad has submitted this change and it was merged.
Change subject: Added TermsToClaimsTranslator
......................................................................
Added TermsToClaimsTranslator
Change-Id: I3769b528cc3b21a24525b1b8801ffd27543efc7a
---
M lib/WikibaseLib.hooks.php
M lib/WikibaseLib.php
A lib/includes/TermsToClaimsTranslator.php
A lib/tests/phpunit/TermsToClaimsTranslatorTest.php
4 files changed, 317 insertions(+), 1 deletion(-)
Approvals:
John Erling Blad: Verified; Looks good to me, approved
diff --git a/lib/WikibaseLib.hooks.php b/lib/WikibaseLib.hooks.php
index 28c7436..e6198f4 100644
--- a/lib/WikibaseLib.hooks.php
+++ b/lib/WikibaseLib.hooks.php
@@ -93,8 +93,9 @@
'MapValueHasher',
'SettingsArray',
'SiteLink',
- 'Utils',
+ 'TermsToClaimsTranslator',
'Term',
+ 'Utils',
);
foreach ( $testFiles as $file ) {
diff --git a/lib/WikibaseLib.php b/lib/WikibaseLib.php
index 2955626..10e8cba 100644
--- a/lib/WikibaseLib.php
+++ b/lib/WikibaseLib.php
@@ -117,6 +117,7 @@
$wgAutoloadClasses['Wikibase\SettingsArray'] = $dir .
'includes/SettingsArray.php';
$wgAutoloadClasses['Wikibase\SiteLink'] = $dir
. 'includes/SiteLink.php';
$wgAutoloadClasses['Wikibase\Term'] = $dir
. 'includes/Term.php';
+$wgAutoloadClasses['Wikibase\Lib\TermsToClaimsTranslator'] = $dir .
'includes/TermsToClaimsTranslator.php';
$wgAutoloadClasses['Wikibase\Utils'] = $dir
. 'includes/Utils.php';
$wgAutoloadClasses['Wikibase\WikibaseDiffOpFactory'] = $dir .
'includes/WikibaseDiffOpFactory.php';
diff --git a/lib/includes/TermsToClaimsTranslator.php
b/lib/includes/TermsToClaimsTranslator.php
new file mode 100644
index 0000000..8e0432b
--- /dev/null
+++ b/lib/includes/TermsToClaimsTranslator.php
@@ -0,0 +1,154 @@
+<?php
+
+namespace Wikibase\Lib;
+
+use InvalidArgumentException;
+use Wikibase\Term;
+use Wikibase\Claim;
+use Wikibase\PropertyValueSnak;
+use DataValues\MonolingualTextValue;
+use DataValues\MultilingualTextValue;
+
+/**
+ * Can turn Term objects into Claims.
+ *
+ * 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.4
+ *
+ * @file
+ * @ingroup WikibaseLib
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class TermsToClaimsTranslator {
+
+ /**
+ * Term type => Property id
+ *
+ * @var int[]
+ */
+ private $propertyIds = array();
+
+ /**
+ * Constructor.
+ *
+ * @since 0.4
+ *
+ * @param int[] $propertyIds
+ */
+ public function __construct( array $propertyIds ) {
+ $this->propertyIds = $propertyIds;
+ }
+
+ /**
+ * Turns a set of terms representing the same property though in
different languages into a Claim
+ * with a MultilingualTextValue in its main snak.
+ *
+ * @since 0.4
+ *
+ * @param Term[] $terms
+ *
+ * @return Claim
+ * @throws InvalidArgumentException
+ */
+ public function termsToClaim( array $terms ) {
+ if ( empty( $terms ) ) {
+ throw new InvalidArgumentException( 'Need to have at
least one term to construct a claim' );
+ }
+
+ $term = reset( $terms );
+ $termType = $term->getType();
+
+ $propertyId = $this->getPropertyIdForTermType( $termType );
+
+ $monoTexts = array();
+
+ foreach ( $terms as $term ) {
+ if ( $term->getType() !== $termType ) {
+ throw new InvalidArgumentException( 'Term types
must be the same to construct a claim' );
+ }
+
+ $monoTexts[] = $this->termToMonoText( $term );
+ }
+
+ $multiText = new MultilingualTextValue( $monoTexts );
+
+ $mainSnak = new PropertyValueSnak( $propertyId, $multiText );
+
+ return new Claim( $mainSnak );
+ }
+
+ /**
+ * Turns a term into a Claim with MonolingualTextValue in its main snak.
+ *
+ * @since 0.4
+ *
+ * @param Term $term
+ *
+ * @return Claim
+ */
+ public function termToClaim( Term $term ) {
+ $propertyId = $this->getPropertyIdForTermType( $term->getType()
);
+ $value = $this->termToMonoText( $term );
+
+ $mainSnak = new PropertyValueSnak( $propertyId, $value );
+
+ return new Claim( $mainSnak );
+ }
+
+ /**
+ * Returns the property id for a term type.
+ *
+ * @since 0.4
+ *
+ * @param string $termType
+ *
+ * @return int
+ * @throws InvalidArgumentException
+ */
+ private function getPropertyIdForTermType( $termType ) {
+ if ( $termType === null ) {
+ throw new InvalidArgumentException( 'Term type must be
set to turn it into a claim' );
+ }
+
+ if ( !array_key_exists( $termType, $this->propertyIds ) ) {
+ throw new InvalidArgumentException( 'Term type not
mapped to a property id' );
+ }
+
+ return $this->propertyIds[$termType];
+ }
+
+ /**
+ * Returns a MonolingualTextValue constructed from the provided Term.
+ *
+ * @since 0.4
+ *
+ * @param Term $term
+ *
+ * @return MonolingualTextValue
+ * @throws InvalidArgumentException
+ */
+ private function termToMonoText( Term $term ) {
+ if ( $term->getLanguage() === null ) {
+ throw new InvalidArgumentException( 'Term language
needs to be set in order to turn it into a MonolingualTextValue' );
+ }
+
+ return new MonolingualTextValue( $term->getLanguage(),
$term->getText() );
+ }
+
+}
diff --git a/lib/tests/phpunit/TermsToClaimsTranslatorTest.php
b/lib/tests/phpunit/TermsToClaimsTranslatorTest.php
new file mode 100644
index 0000000..e67789b
--- /dev/null
+++ b/lib/tests/phpunit/TermsToClaimsTranslatorTest.php
@@ -0,0 +1,160 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Wikibase\Term;
+use Wikibase\Lib\TermsToClaimsTranslator;
+
+/**
+ * Tests for the Wikibase\Lib\TermsToClaimsTranslator 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.4
+ *
+ * @ingroup WikibaseLib
+ * @ingroup Test
+ *
+ * @group Wikibase
+ * @group WikibaseLib
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class TermsToClaimsTranslatorTest extends \PHPUnit_Framework_TestCase {
+
+ /**
+ * @return TermsToClaimsTranslator
+ */
+ private function newInstance() {
+ return new TermsToClaimsTranslator( array(
+ Term::TYPE_LABEL => 1000001,
+ Term::TYPE_DESCRIPTION => 1000002,
+ Term::TYPE_ALIAS => 1000003,
+ ) );
+ }
+
+ /**
+ * @dataProvider termsProvider
+ *
+ * @param Term[] $terms
+ */
+ public function testTermsToClaim( array $terms ) {
+ $claim = $this->newInstance()->termsToClaim( $terms );
+
+ $this->assertInstanceOf( 'Wikibase\Claim', $claim );
+ $this->assertInstanceOf( 'Wikibase\PropertyValueSnak',
$claim->getMainSnak() );
+ $this->assertInstanceOf( 'DataValues\MultilingualTextValue',
$claim->getMainSnak()->getDataValue() );
+ $this->assertEquals( count( $terms ), count(
$claim->getMainSnak()->getDataValue()->getTexts() ) );
+ }
+
+ /**
+ * @dataProvider termProvider
+ *
+ * @param Term $term
+ */
+ public function testTermToClaim( Term $term ) {
+ $claim = $this->newInstance()->termToClaim( $term );
+
+ $this->assertInstanceOf( 'Wikibase\Claim', $claim );
+ $this->assertInstanceOf( 'Wikibase\PropertyValueSnak',
$claim->getMainSnak() );
+ $this->assertInstanceOf( 'DataValues\MonolingualTextValue',
$claim->getMainSnak()->getDataValue() );
+ $this->assertEquals( $term->getLanguage(),
$claim->getMainSnak()->getDataValue()->getLanguageCode() );
+ $this->assertEquals( $term->getText(),
$claim->getMainSnak()->getDataValue()->getText() );
+ }
+
+ public function termsProvider() {
+ $argLists = array();
+
+ $argLists[] = array( array(
+ new Term( array(
+ 'termType' => Term::TYPE_LABEL,
+ 'termLanguage' => 'en',
+ 'termText' => 'foo',
+ ) ),
+ ) );
+
+ $argLists[] = array( array(
+ new Term( array(
+ 'termType' => Term::TYPE_LABEL,
+ 'termLanguage' => 'de',
+ 'termText' => 'foo',
+ ) ),
+ new Term( array(
+ 'termType' => Term::TYPE_LABEL,
+ 'termLanguage' => 'nl',
+ 'termText' => 'bar',
+ ) ),
+ new Term( array(
+ 'termType' => Term::TYPE_LABEL,
+ 'termLanguage' => 'en',
+ 'termText' => 'baz',
+ ) ),
+ ) );
+
+ $argLists[] = array( array(
+ new Term( array(
+ 'termType' => Term::TYPE_DESCRIPTION,
+ 'termLanguage' => 'en',
+ 'termText' => 'foo',
+ ) ),
+ ) );
+
+ $argLists[] = array( array(
+ new Term( array(
+ 'termType' => Term::TYPE_ALIAS,
+ 'termLanguage' => 'en',
+ 'termText' => 'foo',
+ ) ),
+ ) );
+
+ $argLists[] = array( array(
+ new Term( array(
+ 'termType' => Term::TYPE_ALIAS,
+ 'termLanguage' => 'de',
+ 'termText' => 'foo',
+ ) ),
+ new Term( array(
+ 'termType' => Term::TYPE_ALIAS,
+ 'termLanguage' => 'en',
+ 'termText' => 'baz',
+ ) ),
+ new Term( array(
+ 'termType' => Term::TYPE_ALIAS,
+ 'termLanguage' => 'nl',
+ 'termText' => 'nyan',
+ ) ),
+ ) );
+
+ return $argLists;
+ }
+
+ public function termProvider() {
+ $terms = array();
+
+ foreach ( $this->termsProvider() as $argList ) {
+ $termList = $argList[0];
+
+ foreach ( $termList as $term ) {
+ $terms[] = array( $term );
+ }
+ }
+
+ return $terms;
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/52607
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I3769b528cc3b21a24525b1b8801ffd27543efc7a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: John Erling Blad <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits