PHP code example of addwiki / wikibase-api

1. Go to this page and download the library: Download addwiki/wikibase-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

addwiki / wikibase-api example snippets




$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()
	)
);

$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()

$getter = $wbFactory->newRevisionGetter();

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

$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'
    );
}

$statementRemover = $wbFactory->newStatementRemover();

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

$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 );
    }
}

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

$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 );