2017 © Pedro Peláez
 

library wikibase-api

A Wikibase api lib

image

addwiki/wikibase-api

A Wikibase api lib

  • Friday, July 27, 2018
  • by addshore
  • Repository
  • 8 Watchers
  • 13 Stars
  • 1,458 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 11 Forks
  • 2 Open issues
  • 9 Versions
  • 1 % Grown

The README.md

wikibase-api

GitHub issue custom search in repo Latest Stable Version Download count, (*1)

Issue tracker: https://github.com/addwiki/addwiki/issues, (*2)

Installation

Use composer to install the library and all its dependencies:, (*3)

composer require "addwiki/wikibase-api:~3.0"

Example Usage

The library provides users with a large collection of Services. These services should be retrieved from the WikibaseFactory class., (*4)

Below you will find some more examples using various services. Each example follows on from the previous example (so as not to repeat the first steps). Note: this library uses namespaces so please remember to add the relevant use clauses., (*5)

Load & General Setup

require_once( __DIR__ . '/vendor/autoload.php' );

$api = new MediawikiApi( 'http://localhost/w/api.php', new UserAndPassword( 'username', 'password' ) );

// Create our Factory, All services should be used through this!
// You will need to add more or different datavalues here.
// In the future Wikidata / Wikibase defaults will be provided in a separate library.
$dataValueClasses = array(
    'unknown' => 'DataValues\UnknownValue',
    'string' => 'DataValues\StringValue',
    'boolean' => 'DataValues\BooleanValue',
    'number' => 'DataValues\NumberValue',
    'globecoordinate' => 'DataValues\Geo\Values\GlobeCoordinateValue',
    'monolingualtext' => 'DataValues\MonolingualTextValue',
    'multilingualtext' => 'DataValues\MultilingualTextValue',
    'quantity' => 'DataValues\QuantityValue',
    'time' => 'DataValues\TimeValue',
    'wikibase-entityid' => 'Addwiki\Wikibase\DataModel\Entity\EntityIdValue',
);
$wbFactory = new WikibaseFactory(
    $api,
    new Addwiki\Wikibase\DataModel\DataModelFactory(
        new DataValues\Deserializers\DataValueDeserializer( $dataValueClasses ),
        new DataValues\Serializers\DataValueSerializer()
    )
);

Create an empty entity

Create a new empty item., (*6)

$saver = $wbFactory->newRevisionSaver();

$edit = new Revision(
    new ItemContent( Item::newEmpty() )
);
$resultingItem = $saver->save( $edit );

// You can get the ItemId object of the created item by doing the following
$itemId = $resultingItem->getId()

Set a label

Set an English label on the item Q87 assuming it exists, using a custom summary., (*7)

$getter = $wbFactory->newRevisionGetter();

$entityRevision = $getter->getFromId( 'Q87' );
$entityRevision->getContent()->getData()->setDescription( 'en', 'I am A description' );
$saver->save( $entityRevision, new EditInfo( 'Custom edit summary' ) );

Create a new statement

Create a new string statement on item Q777 if a statement for the property doesn't already exist., (*8)

$statementCreator = $wbFactory->newStatementCreator();
$revision = $getter->getFromId( 'Q777' );
$item = $revision->getContent()->getData();
$statementList = $item->getStatements();
if( $statementList->getByPropertyId( PropertyId::newFromNumber( 1320 ) )->isEmpty() ) {
    $statementCreator->create(
        new PropertyValueSnak(
            PropertyId::newFromNumber( 1320 ),
            new StringValue( 'New String Value' )
        ),
        'Q777'
    );
}

Remove a statement using a GUID

Remove the statement with the given claim (if it exists), (*9)

$statementRemover = $wbFactory->newStatementRemover();

$statementRemover->remove( 'Q123$f12bd80f-415a-c37e-9e18-234b9e19eece' );

Add a reference to a statement

$statementSetter = $wbFactory->newStatementSetter();
$revision = $getter->getFromId( 'Q9956' );
$item = $revision->getContent()->getData();
$statementList = $item->getStatements();
$referenceSnaks = array(
    new PropertyValueSnak( new PropertyId( 'P44' ), new StringValue( 'bar' ) ),
);
foreach( $statementList->getByPropertyId( PropertyId::newFromNumber( 99 ) )->getIterator() as $statement ) {
    if( $statement->getReferences()->isEmpty() ) {
        $statement->addNewReference( $referenceSnaks );
    }
}

Attempt to merge 2 items

Try to merge Q999 and Q888 if possible, catch any errors., (*10)

try{
    $wbFactory->newItemMerger()->merge( 'Q999', 'Q888' );
}
catch( UsageException $e ) {
    echo "Oh no! I failed to merge!";
}

Simple Lookups

Easily lookup an item object, an individual label and redirect sources., (*11)

$itemId = new ItemId( 'Q555' )
$itemLookup = $wbFactory->newItemLookup();
$termLookup = $wbFactory->newTermLookup();
$entityRedirectLookup = $wbFactory->newEntityRedirectLookup();

$item = $itemLookup->getItemForId( $itemId );
$enLabel = $termLookup->getLabel( $itemId, 'en' );
$redirectSources = $entityRedirectLookup->getRedirectIds( $itemId );

The Versions