Page MenuHomePhabricator

Describe in the documentation how to add a basic statement
Closed, InvalidPublic

Description

In Wikidata an important data type is to connect to another qid.
The documentation currently only describes setting strings.
It could have a basic example how to declare an item as the value of a statement.

Git Repo: https://github.com/addwiki/wikibase-api
Documentation directory: https://github.com/addwiki/wikibase-api/tree/master/docs

Example patch in another repo which adds functionality but also updated the docs: https://github.com/addwiki/mediawiki-api-base/commit/2eb635079cc69e0b84ea1d51a5052358845bee01#diff-e3e2a9bfd88566b05001b02a3f51d286
The files changed in the above commit that update the docs are docs/index.rst and the addition of docs/multipart.rst

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript
Hannolans renamed this task from Describe in the documentation hoe to add a statement 'is a human' to Describe in the documentation how to add a statement 'is a human'.Mar 17 2017, 8:28 AM
Hannolans updated the task description. (Show Details)

@Hannolans

So, there is currently an example in the README that shows how to create a statement (for a string value)

$statementCreator->create(
    new PropertyValueSnak(
        PropertyId::newFromNumber( 1320 ),
        new StringValue( 'New String Value' )
    ),
    'Q777'
);

The StringValue object can be replaced by any object that extends DataValueObject.
So for linking to another entity it needs to be an EntityIdValue.
This needs to be changed to something like:

$statementCreator->create(
    new PropertyValueSnak(
        PropertyId::newFromNumber( 1320 ),
        new EntityIdValue( new ItemId( 'Q99499' ) )
    ),
    'Q777'
);

I'll try and work on documentation soon, it would be great if others could also make PRs with their learnings though!

Addshore moved this task from Incoming to Ready on the Addwiki board.
Addshore subscribed.

Thanks. In the documentation there is also described to create a new item. in the above example an existing item Q777 is used. What if you create a new item and no qid is available?

Below is the code I have. Adding a string works for new items, but adding a qid not. I used getStatements()->addNewStatement to be able to add it to the new item without a qid.

//Create a new empty item.
$saver = $wbFactory->newRevisionSaver();
$statementCreator = $wbFactory->newStatementCreator();
$edit = new Revision(    new ItemContent( Item::newEmpty() ));
$edit->getContent()->getData()->setDescription( 'nl', 'Kunstenaar');
$edit->getContent()->getData()->setLabel( 'nl', $author->title);

//set RKD id
$edit->getContent()->getData()->getStatements()->addNewStatement(
			 new PropertyValueSnak(PropertyId::newFromNumber( '650'), new StringValue( $rkd_id ))
     );

//human
$edit->getContent()->getData()->getStatements()->addNewStatement(
			 new PropertyValueSnak(PropertyId::newFromNumber( '31' ), new EntityIdValue( new ItemId( 'Q5' )) )
     );

$saver->save( $edit);

Hmmm, I believe that should work.
To make it cleaner to read you can also do.

$saver = $wbFactory->newRevisionSaver();
$statementCreator = $wbFactory->newStatementCreator();

$newItem = Item::newEmpty();
$newItem->getStatements()->addNewStatement( new PropertyValueSnak(PropertyId::newFromNumber( '650'), new StringValue( $rkd_id )));
//etc.

$edit = new Revision(    new ItemContent( $item ));
$saver->save( $edit);

What actually happens with the code you posted above?

I had to add 'use Wikibase\DataModel\Entity\ItemId;' now it works.

Hi

I am a newbie, and I wanted to work on this, can u assign it to me.

Hi and welcome @Dheerajmalisetty! Thanks for your interest! If you have any questions, please ask!

Hi,

Can i actually know where the documentation folder was present?

Hi,

Can i actually know where the documentation folder was present?

https://github.com/addwiki/wikibase-api/blob/master/README.md

Thank you,
above link provides how to create a statement, but according to question we need to describe in the documentation 'how to declare "is human" is item'.

Declaring that an item is a human is a statement.

Maybe @Addshore or @Hannolans can explain where this needs to be documented. I linked the readme for the library this ticket is about.

Addshore renamed this task from Describe in the documentation how to add a statement 'is a human' to Describe in the documentation how to add a basic statement.Dec 27 2017, 1:01 PM
Addshore updated the task description. (Show Details)
Addshore updated the task description. (Show Details)

So the example in the README currently uses a String value

new StringValue( 'New String Value' )

For a statement, for example, instance of, an EntityIdValue will need to be used.

The constructor for EntityIdValue is as follows:

	public function __construct( EntityId $entityId ) {
		$this->entityId = $entityId;
	}

Do it will be something like:

new EntityIdValue( $itemId )

Should I clone the wikidata seperately or is it to work in wikibase?