Page MenuHomeGitPull.it

D103.1782838608.diff
No OneTemporary

Authored By
Unknown
Size
12 KB
Referenced Files
None
Subscribers
None

D103.1782838608.diff

diff --git a/include/network/HTTPRequest.php b/include/network/HTTPRequest.php
--- a/include/network/HTTPRequest.php
+++ b/include/network/HTTPRequest.php
@@ -328,7 +328,7 @@
if( !$http_status ) {
// oh nose!
- Log::error( "Huston, we have not valid headers" );
+ Log::error( "Houston, we have not valid headers" );
// retry but without DOSsing
$args = array_replace( $args, [
@@ -344,7 +344,7 @@
if( $http_status->isServerError() ) {
// oh nose!
- Log::error( sprintf( "Huston, we have the code %s: %s",
+ Log::error( sprintf( "Houston, we have the code %s: %s",
$http_status->getCode(),
$http_status->getMessage()
) );
diff --git a/include/wb/DataModel.php b/include/wb/DataModel.php
--- a/include/wb/DataModel.php
+++ b/include/wb/DataModel.php
@@ -1,6 +1,6 @@
<?php
# Boz-MW - Another MediaWiki API handler in PHP
-# Copyright (C) 2017, 2018, 2019, 2020 Valerio Bozzolan
+# Copyright (C) 2017-2023 Valerio Bozzolan, contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
@@ -19,6 +19,8 @@
namespace wb;
use \mw\WikibaseSite;
+use \wb\Label;
+use \wb\Description;
/**
* Wikibase data container
@@ -269,8 +271,35 @@
return $this->labels->have( $language );
}
+ /**
+ * Get a label in a specific language
+ *
+ * @param $language Language code, as accepted by Wikidata
+ * @return string|null
+ */
+ public function getLabelValue( $language ) {
+ return $this->labels->getLanguageValue( $language );
+ }
+
+ /**
+ * Set a label in a specific language
+ *
+ * @param $language Language code, as accepted by Wikidata
+ * @param $value Language value
+ * @return self
+ */
+ public function setLabelValue( $language, $value ) {
+ $this->labels->setLanguageValue( $language, $value );
+ return $this;
+ }
+
/**
* Set, delete, preserve if it exists, a label.
+ *
+ * You may want to use setLabelValue() instead that is more user-friendly.
+ *
+ * @param $label Label object
+ * @return self
*/
public function setLabel( $label ) {
$this->labels->set( $label );
@@ -278,15 +307,37 @@
}
/**
- * Check if a label of a certain language exists
+ * Check if a description exists in a certain language
*
- * @param $language string
+ * @param $language string Language code, as accepted by Wikidata
* @return bool
*/
public function hasDescriptionInLanguage( $language ) {
return $this->descriptions->have( $language );
}
+ /**
+ * Get a description in a specific language
+ *
+ * @param $language Language code, as accepted by Wikidata
+ * @return string|null
+ */
+ public function getDescriptionValue( $language ) {
+ return $this->descriptions->getLanguageValue( $language );
+ }
+
+ /**
+ * Set a label in a specific language
+ *
+ * @param $language Language code, as accepted by Wikidata
+ * @param $value Language value
+ * @return self
+ */
+ public function setDescriptionValue( $language, $value ) {
+ $this->descriptions->setLanguageValue( $language, $value );
+ return $this;
+ }
+
/**
* Set, delete, preserve if it exists, a description.
*
diff --git a/include/wb/Descriptions.php b/include/wb/Descriptions.php
--- a/include/wb/Descriptions.php
+++ b/include/wb/Descriptions.php
@@ -1,6 +1,6 @@
<?php
# Boz-MW - Another MediaWiki API handler in PHP
-# Copyright (C) 2017, 2018 Valerio Bozzolan
+# Copyright (C) 2017-2023 Valerio Bozzolan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
@@ -23,6 +23,17 @@
*/
class Descriptions extends Labels {
+ /**
+ * Create a single element from a language and its value
+ *
+ * @param $language string Language code
+ * @param $value string Label value
+ * @return Description
+ */
+ protected function createSingleFromLanguageValue( $language, $value ) {
+ return new Description( $language, $value );
+ }
+
/**
* String rappresentation
*
diff --git a/include/wb/Label.php b/include/wb/Label.php
--- a/include/wb/Label.php
+++ b/include/wb/Label.php
@@ -1,6 +1,6 @@
<?php
# Boz-MW - Another MediaWiki API handler in PHP
-# Copyright (C) 2017 Valerio Bozzolan
+# Copyright (C) 2017-2023 Valerio Bozzolan, contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
@@ -19,38 +19,81 @@
namespace wb;
/**
- * An entity Label.
+ * A Wikibase Label
*
- * @see https://www.wikidata.org/wiki/Wikidata:Glossary#Label
+ * https://www.wikidata.org/wiki/Wikidata:Glossary#Label
*/
class Label {
+ /**
+ * @var string
+ */
public $language;
+
+ /**
+ * @var string
+ */
public $value;
+ /**
+ * Constructor
+ *
+ * @var $language string Language code
+ * @var $value string Label value
+ */
public function __construct( $language, $value ) {
$this->setLanguage( $language )
->setValue( $value );
}
+ /**
+ * Get the language code
+ *
+ * @return string Language code
+ */
public function getLanguage() {
return $this->language;
}
+ /**
+ * Get the language value
+ *
+ * @return string
+ */
public function getValue() {
return $this->value;
}
- public function setLanguage( $language ) {
+ /**
+ * Change the language
+ *
+ * This is more an internal method and should be avoided.
+ *
+ * @param $language string Language code
+ * @return self
+ */
+ protected function setLanguage( $language ) {
$this->language = $language;
return $this;
}
+ /**
+ * Set the label value
+ *
+ * @param $value Label value
+ * @return self
+ */
public function setValue( $value ) {
$this->value = $value;
return $this;
}
+ /**
+ * Create a Label object from raw array data
+ *
+ * @param $data array
+ * @return self
+ */
public static function createFromData( $data ) {
if( ! isset( $data['language'], $data['value'] ) ) {
throw new WrongDataException( self::class );
diff --git a/include/wb/Labels.php b/include/wb/Labels.php
--- a/include/wb/Labels.php
+++ b/include/wb/Labels.php
@@ -1,6 +1,6 @@
<?php
# Boz-MW - Another MediaWiki API handler in PHP
-# Copyright (C) 2017, 2018 Valerio Bozzolan
+# Copyright (C) 2017-2023 Valerio Bozzolan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
@@ -24,6 +24,8 @@
class Labels {
/**
+ * Labels indexed by language code
+ *
* @var array
*/
private $labels = [];
@@ -35,7 +37,7 @@
*/
public function __construct( $labels = [] ) {
foreach( $labels as $label ) {
- $this->add( $label );
+ $this->set( $label );
}
}
@@ -46,37 +48,55 @@
* @return Label
*/
public function get( $language ) {
- foreach( $this->labels as $label ) {
- if( $label->getLanguage() === $language ) {
- return $label;
- }
+ return $this->labels[ $language ] ?? false;
+ }
+
+ /**
+ * Get a certain Label value by its language
+ *
+ * @param $language string
+ * @return string The language value or NULL
+ */
+ public function getLanguageValue( $language ) {
+ $value = null;
+ $label = $this->get( $language );
+ if( $label ) {
+ $value = $label->getValue();
}
- return false;
+ return $value;
}
/**
- * Does it have a certain label?
+ * Get a certain Label value by its language
+ *
+ * @param $language string
+ * @param string The language value or NULL
+ * @return self
+ */
+ public function setLanguageValue( $language, $value ) {
+ $label = $this->createSingleFromLanguageValue( $language, $value );
+ return $this->set( $label );
+ }
+
+ /**
+ * Do our labels have this one?
*
* @param $language string
* @return bool
*/
public function have( $language ) {
- return false !== $this->get( $language );
+ return $this->get( $language ) !== false;
}
/**
- * Set/add a certain label
+ * Set/add a certain Label
*
* @param $label Label
* @return self
*/
public function set( Label $label ) {
- $existing = $this->get( $label->getLanguage() );
- if( $existing ) {
- $existing = $language;
- } else {
- $this->labels[] = $label;
- }
+ $lang_code = $label->getLanguage();
+ $this->labels[ $lang_code ] = $label;
return $this;
}
@@ -104,6 +124,17 @@
return implode( $glue, $codes );
}
+ /**
+ * Create a single element from a language and its value
+ *
+ * @param $language string Language code
+ * @param $value string Label value
+ * @return Label
+ */
+ protected function createSingleFromLanguageValue( $language, $value ) {
+ return new Label( $language, $value );
+ }
+
/**
* String rappresentation
*
diff --git a/phpunit/WikibaseDataModelTest.php b/phpunit/WikibaseDataModelTest.php
new file mode 100644
--- /dev/null
+++ b/phpunit/WikibaseDataModelTest.php
@@ -0,0 +1,26 @@
+<?php
+// use the phpunit framework
+use PHPUnit\Framework\TestCase;
+
+use wb\DataModel;
+
+/**
+ * Test Query class
+ */
+final class WikibaseDataModelTest extends TestCase {
+
+ public function testSetLabel() {
+ $data = new DataModel();
+ $data->setLabelValue( 'it', 'pizza' );
+ $asd = $data->getLabelValue( 'it' );
+ $this->assertEquals( $asd, 'pizza' );
+ }
+
+ public function testSetDescription() {
+ $data = new DataModel();
+ $data->setDescriptionValue( 'it', 'pizza' );
+ $asd = $data->getDescriptionValue( 'it' );
+ $this->assertEquals( $asd, 'pizza' );
+ }
+
+}
diff --git a/phpunit/WikibaseDescriptionsTest.php b/phpunit/WikibaseDescriptionsTest.php
new file mode 100644
--- /dev/null
+++ b/phpunit/WikibaseDescriptionsTest.php
@@ -0,0 +1,48 @@
+<?php
+// use the phpunit framework
+use PHPUnit\Framework\TestCase;
+
+/**
+ * Test Query class
+ */
+final class WikibaseDescriptionsTest extends TestCase {
+
+ public function testCreateEmpty() {
+ $labels = new \wb\Descriptions();
+ $this->assertEquals( count( $labels->getAll() ), 0 );
+ }
+
+ public function testGetLanguageValueEmpty() {
+ $labels = new \wb\Descriptions();
+ $this->assertEquals( $labels->getLanguageValue( 'it' ), null );
+ }
+
+ public function testSetGetLanguageValue() {
+ $labels = new \wb\Descriptions();
+ $labels->setLanguageValue( 'it', 'Pizza' );
+ $this->assertEquals( $labels->getLanguageValue( 'it' ), 'Pizza' );
+ }
+
+ public function testSetOne() {
+ $labels = new \wb\Descriptions();
+ $label = new \wb\Description( 'it', 'Asd' );
+ $this->assertEquals( $labels->have( 'it' ), false );
+ $this->assertEquals( count( $labels->getAll() ), 0 );
+
+ $labels->set( $label );
+ $this->assertEquals( $labels->have( 'it' ), true );
+ $this->assertEquals( count( $labels->getAll() ), 1 );
+ }
+
+ public function testSetOverride() {
+ $labels = new \wb\Descriptions();
+ $label1 = new \wb\Description( 'it', 'Asd1' );
+ $label2 = new \wb\Description( 'it', 'Asd2' );
+ $label3 = new \wb\Description( 'it', 'Asd3' );
+ $labels->set( $label1 );
+ $labels->set( $label2 );
+ $labels->set( $label3 );
+ $this->assertEquals( count( $labels->getAll() ), 1 );
+ }
+
+}
diff --git a/phpunit/WikibaseLabelsTest.php b/phpunit/WikibaseLabelsTest.php
new file mode 100644
--- /dev/null
+++ b/phpunit/WikibaseLabelsTest.php
@@ -0,0 +1,48 @@
+<?php
+// use the phpunit framework
+use PHPUnit\Framework\TestCase;
+
+/**
+ * Test Query class
+ */
+final class WikibaseLabelsTest extends TestCase {
+
+ public function testCreateEmpty() {
+ $labels = new \wb\Labels();
+ $this->assertEquals( count( $labels->getAll() ), 0 );
+ }
+
+ public function testGetLanguageValueEmpty() {
+ $labels = new \wb\Labels();
+ $this->assertEquals( $labels->getLanguageValue( 'it' ), null );
+ }
+
+ public function testSetGetLanguageValue() {
+ $labels = new \wb\Labels();
+ $labels->setLanguageValue( 'it', 'Pizza' );
+ $this->assertEquals( $labels->getLanguageValue( 'it' ), 'Pizza' );
+ }
+
+ public function testSetOne() {
+ $labels = new \wb\Labels();
+ $label = new \wb\Label( 'it', 'Asd' );
+ $this->assertEquals( $labels->have( 'it' ), false );
+ $this->assertEquals( count( $labels->getAll() ), 0 );
+
+ $labels->set( $label );
+ $this->assertEquals( $labels->have( 'it' ), true );
+ $this->assertEquals( count( $labels->getAll() ), 1 );
+ }
+
+ public function testSetOverride() {
+ $labels = new \wb\Labels();
+ $label1 = new \wb\Label( 'it', 'Asd1' );
+ $label2 = new \wb\Label( 'it', 'Asd2' );
+ $label3 = new \wb\Label( 'it', 'Asd3' );
+ $labels->set( $label1 );
+ $labels->set( $label2 );
+ $labels->set( $label3 );
+ $this->assertEquals( count( $labels->getAll() ), 1 );
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Tue, Jun 30, 18:56 (18 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1984355
Default Alt Text
D103.1782838608.diff (12 KB)

Event Timeline