/** * string $type type of snak * @param EntityId|string $propertyId id of the property * @param \DataValues\DataValue|null $dataValue value of the property (optional) * @throws Exception */ public function __construct($type, $propertyId, \DataValues\DataValue $dataValue = null) { if (is_string($propertyId)) { $propertyId = EntityId::newFromPrefixedId($propertyId); } if (!$propertyId instanceof EntityId || $propertyId->getEntityType() !== 'property') { throw new Exception('$propertyId must be a valid property id'); } $this->type = $type; $this->propertyId = $propertyId; $this->dataValue = $dataValue; }
/** * @param string $summary summary for the change * @throws Exception * @todo push back changes for all data excepts claims */ public function save($summary = '') { if ($this->changes === array()) { return; // Nothing to do } if ($this->id === null) { $result = $this->api->createEntity($this->getType(), $this->changes, $this->lastRevisionId, $summary); } else { $result = $this->api->editEntity($this->id->getPrefixedId(), $this->changes, $this->lastRevisionId, $summary); } if (isset($result['entity'])) { $this->fillData($result['entity']); } $this->changes = array(); }
<?php /** * Basic example for the use of the libary with some small edits */ include '../include.php'; //Instance the Api object with the base domain of the wiki and the user agent. $api = new WikibaseApi('wikidata-test-repo.wikimedia.de', 'WikibasePhpLibExample/1.0'); //Instance the entity provider that allows to get entites from the wiki $entityProvider = new EntityProvider($api); //login with user:demo and test as password $api->login('demo', 'test'); //Get an entity $entity = $entityProvider->getEntityFromId(EntityId::newFromPrefixedId('Q257')); //Output the description in French echo $entity->getDescription('fr'); //Change the label in French $entity->setLabel('fr', 'Or'); //Add an alias in English $entity->addAlias('en', 'Test'); //Remove an alias $entity->removeAlias('en', 'Test2'); //Delete the sitelink in French $entity->setSitelink('frwiki', ''); //Save changes $entity->save('Test of wikibase-php-lib'); //See the updated aliases in English print_r($entity->getAlias('en')); //Log out $api->logout();
* Basic example for the use of the libary with some small edits */ include '../include.php'; // Instance the Api object with the base domain of the wiki and the user agent. $api = new WikibaseApi('wikidata-test-repo.wikimedia.de', 'WikibasePhpLibExample/0.1'); // Instance the entity provider that allows to get entites from the wiki $entityProvider = new EntityProvider($api); // login with user:demo and test as password $api->login('demo', 'test'); // Get an entity $entity = $entityProvider->getEntityFromId(EntityId::newFromPrefixedId('q82')); // Create a new statement $statement = $entity->createStatementForSnak(new Snak('value', 'p3', EntityId::newFromPrefixedId('Q22'))); // Save it $statement->save('Test of wikibase-php-lib'); // Change the main value of the statement $statement->setMainSnak(new Snak('novalue', 'p3')); // Save the change $statement->save(); // Create a reference for the statement $reference = $statement->createReferenceForSnak(new Snak('value', 'p7', EntityId::newFromPrefixedId('Q36'))); // Save the reference $reference->save('Test of reference'); // See the list of claims print_r($entity->getClaims()); // Delete the reference $reference->deleteAndSave(); // Remove the created statement $statement->deleteAndSave('End of test'); // Log out $api->logout();