protected void addLabelForNumbers ( ItemIdValue itemIdValue ) { String qid = itemIdValue . getId ( ) ; try { // Fetch the online version of the item to make sure we edit the // current version: ItemDocument currentItemDocument = ( ItemDocument ) dataFetcher . getEntityDocument ( qid ) ; if ( currentItemDocument == null ) { System . out . println ( "*** " + qid + " could not be fetched. Maybe it has been deleted." ) ; return ; } // Check if we still have exactly one numeric value: QuantityValue number = currentItemDocument . findStatementQuantityValue ( "P1181" ) ; if ( number == null ) { System . out . println ( "*** No unique numeric value for " + qid ) ; return ; } // Check if the item is in a known numeric class: if ( ! currentItemDocument . hasStatementValue ( "P31" , numberClasses ) ) { System . out . println ( "*** " + qid + " is not in a known class of integer numbers. Skipping." ) ; return ; } // Check if the value is integer and build label string: String numberString ; try { BigInteger intValue = number . getNumericValue ( ) . toBigIntegerExact ( ) ; numberString = intValue . toString ( ) ; } catch ( ArithmeticException e ) { System . out . println ( "*** Numeric value for " + qid + " is not an integer: " + number . getNumericValue ( ) ) ; return ; } // Construct data to write: ItemDocumentBuilder itemDocumentBuilder = ItemDocumentBuilder . forItemId ( itemIdValue ) . withRevisionId ( currentItemDocument . getRevisionId ( ) ) ; ArrayList < String > languages = new ArrayList <> ( arabicNumeralLanguages . length ) ; for ( int i = 0 ; i < arabicNumeralLanguages . length ; i ++ ) { if ( ! currentItemDocument . getLabels ( ) . containsKey ( arabicNumeralLanguages [ i ] ) ) { itemDocumentBuilder . withLabel ( numberString , arabicNumeralLanguages [ i ] ) ; languages . add ( arabicNumeralLanguages [ i ] ) ; } } if ( languages . size ( ) == 0 ) { System . out . println ( "*** Labels already complete for " + qid ) ; return ; } logEntityModification ( currentItemDocument . getEntityId ( ) , numberString , languages ) ; dataEditor . editItemDocument ( itemDocumentBuilder . build ( ) , false , "Set labels to numeric value (Task MB1)" ) ; } catch ( MediaWikiApiErrorException e ) { e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } }
Fetches the current online data for the given item and adds numerical labels if necessary .