diff --git a/2018-09-mibact-fixer/bot.php b/2018-09-mibact-fixer/bot.php index 3aa97db..f5c0f1a 100755 --- a/2018-09-mibact-fixer/bot.php +++ b/2018-09-mibact-fixer/bot.php @@ -1,64 +1,79 @@ #!/usr/bin/php login(); foreach( wm\Wikidata::querySPARQL( file_get_contents( 'query.sparql' ) ) as $result ) { - $code = $result->catv->value; $item_id = $result->id ->value; $item_id = str_replace( 'http://www.wikidata.org/entity/', '', $item_id ); - $code = str_replace( 'DBUnico.', '', $code ); // pull existing data $item = wm\Wikidata::getInstance() ->fetchSingleEntity( $item_id, [ 'props' => 'claims' ] ); // data to be pushed $data = $item->cloneEmpty(); - // push the MiBACT ID if not exists - if( ! $item->hasClaimsInProperty( MIBACT_ID ) ) { - $data->addClaim( new wb\StatementExternalID( MIBACT_ID, $code ) ); - } + // MiBACT IDs + $codes = []; - // push for removal of any catalog code claims foreach( $item->getClaimsInProperty( CATALOG_CODE ) as $claim ) { - $code = $claim->getMainsnak()->getDataValue()->getValue(); - if( false !== strpos( $code, 'DBUnico.' ) ) { + $value = $claim->getMainsnak()->getDataValue()->getValue(); + if( false !== strpos( $value, 'DBUnico.' ) ) { + + // push for removal this catalog code $data->addClaim( $claim->cloneForRemoval() ); + + // remember this MiBACT ID + $codes[] = str_replace( 'DBUnico.', '', $value ); } } - // push for removal of any described at URL claims + // check duplicate MiBACT IDs + foreach( $item->getClaimsInProperty( MIBACT_ID ) as $claim ) { + $value = $claim->getMainsnak()->getDataValue()->getValue(); + foreach( $codes as $i => $code ) { + if( $code === $value ) { + unset( $codes[ $i ] ); + } + } + } + + // push MiBACT IDs + foreach( $codes as $code ) { + $data->addClaim( new wb\StatementExternalID( MIBACT_ID, $code ) ); + } + + // push for removal any described at URL claims foreach( $item->getClaimsInProperty( DESCRIBED_AT_URL ) as $claim ) { $code = $claim->getMainsnak()->getDataValue()->getValue(); if( false !== strpos( $code, 'http://dati.beniculturali.it/mibact/luoghi/resource/CulturalInstituteOrSite' ) ) { $data->addClaim( $claim->cloneForRemoval() ); } } if( $data->countClaims() ) { $data->printChanges(); if( 'y' === cli\Input::yesNoQuestion( 'save?' ) ) { $data->editEntity( [ 'bot' => 1, 'summary.pre' => 'Bot: [[Wikidata:Property proposal/DBUnico ID|migrate to new DBUnico ID]]: ' ] ); } } }