Skip to content

Instantly share code, notes, and snippets.

@addshore
Last active August 29, 2015 14:03
Show Gist options
  • Save addshore/1c5612b7aa358bb6ac25 to your computer and use it in GitHub Desktop.
Save addshore/1c5612b7aa358bb6ac25 to your computer and use it in GitHub Desktop.
Opencorporates - Claim Creator
<?php
require_once( __DIR__ . "/vendor/autoload.php" );
// Set stuff up
$api = new \Mediawiki\Api\MediawikiApi( 'http://www.wikidata.org/w/api.php' );
$api->login( new \Mediawiki\Api\ApiUser( 'USERNAME', 'PASSWORD' ) );
$services = new \Wikibase\Api\WikibaseFactory( $api );
// Stuff we need for each item!
$identifier = 'en/1234';
$itemId = 'Q123';
// OpenCorporates ID property
$propertyId = \Wikibase\DataModel\Entity\PropertyId::newFromNumber( 1320 );
// Load the item
$revision = $services->newRevisionGetter()->getFromId( $itemId );
/** @var Wikibase\DataModel\Entity\Item $item */
$item = $revision->getContent()->getNativeData();
// Get a nice claims object
$claims = new \Wikibase\DataModel\Claim\Claims( $item->getClaims() );
// If there are no claims for the property
if( $claims->getClaimsForProperty( $propertyId )->isEmpty() ) {
// Create one!
$services->newClaimCreator()->create(
new \Wikibase\DataModel\Snak\PropertyValueSnak(
$propertyId,
new \DataValues\StringValue( $identifier )
),
$itemId
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment