addwiki/wikibase-api

A Wikibase api lib

Installs: 1 081

Dependents: 0

Stars: 6

Watches: 5

Forks: 6

Open Issues: 1

Language: PHP

0.5 2015-06-29 08:32 UTC

README

Build Status Code Coverage Scrutinizer Quality Score Dependency Status

On Packagist: Latest Stable Version Download count

Installation

Use one of the below methods:

1 - Use composer to install the library and all its dependencies using the master branch:

composer require "addwiki/wikibase-api:dev-master"

2 - Create a composer.json file that just defines a dependency on version 0.2 of this package, and run 'composer install' in the directory:

{
    "require": {
        "addwiki/wikibase-api": "~0.2.0"
    }
}

The tests can be run as follows:

phpunit -c tests/unit/

Example Usage

Below you will find some smore examples using various parts of the code.

Please also take a look at our integration tests that might be able to help you!

// Load all of the things
require_once( __DIR__ . "/vendor/autoload.php" );

// Use the mediawiki api and Login
$api = new MediawikiApi( "http://localhost/w/api.php" );
$api->login( new ApiUser( 'username', 'password' ) );

// Create our Factory, All services should be used through this!
$services = new WikibaseFactory( $api );

// Get 2 specific services
$getter = $services->newRevisionGetter();
$saver = $services->newRevisionSaver();

// Create a new Entity
$edit = new Revision(
    new ItemContent( Item::newEmpty() )
);
$saver->save( $edit );

// Set a label in the language en on the item Q87
$entityRevision = $getter->getFromId( 'Q87' );
$entityRevision->getContent()->getData()->setDescription( 'en', 'I am A description' );
$saver->save( $entityRevision );

// Create a new string claim on item Q777 if a claim for the property doesn't already exist
$revision = $services->newRevisionGetter()->getFromId( 'Q777' );
$item = $revision->getContent()->getData();
$claims = new Claims( $item->getClaims() );
if( $claims->getClaimsForProperty( PropertyId::newFromNumber( 1320 ) )->isEmpty() ) {
    $services->newClaimCreator()->create(
        new PropertyValueSnak(
            PropertyId::newFromNumber( 1320 ),
            new StringValue( 'New String Value' )
        ),
        'Q777'
    );
}

// Try to merge Q999 and Q888 if possible, catch any errors
try{
    $services->newItemMerger()->merge( 'Q999', 'Q888' );
}
catch( UsageException $e ) {
    echo "Oh no! I failed to merge!";
}