Exemplo n.º 1
0
 /// <summary>
 /// Get the entity from the given entity id with data in the languages provided.
 /// </summary>
 /// <param name="id">The entity id</param>
 /// <param name="languages">The languages</param>
 /// <returns>The entity</returns>
 public Entity getEntityFromId(EntityId id, string[] languages)
 {
     Entity[] entities = this.getEntitiesFromIds(new EntityId[] { id }, languages);
     foreach (Entity entity in entities)
     {
         return entity;
     }
     return null;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Get the entities from the given entity ids with data in the languages provided.
 /// </summary>
 /// <param name="ids">The entity ids</param>
 /// <param name="languages">The languages</param>
 /// <returns>The entities</returns>
 public Entity[] getEntitiesFromIds(EntityId[] ids, string[] languages)
 {
     string[] prefixedIds = new string[ids.Length];
     for (int i = 0; i < ids.Length; i++)
     {
         prefixedIds[i] = ids[i].getPrefixedId();
     }
     return this.api.getEntitiesFromIds(prefixedIds, languages);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="type">The type</param>
 /// <param name="propertyId">The property id</param>
 /// <param name="dataValue">The data value</param>
 public Snak(string type, EntityId propertyId, DataValue dataValue)
 {
     if (propertyId.prefix != "p")
     {
          throw new ArgumentException("propertyId must be a valid property id", "propertyId");
     }
     this.type = type;
     this.propertyId = propertyId;
     this.dataValue = dataValue;
 }
Exemplo n.º 4
0
        private WikiDataState CheckCoordinateValue(Item item, String propertyId, Point expected, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
        {
            if ( item == null )
                throw new ArgumentNullException("item");

            WikiDataState result = WikiDataState.Unknown;

            // Statement claim = item.Claims.FirstOrDefault(x => x.IsAboutProperty(WikiBase.PropertyIdCountry)) as Statement;
            var property = new EntityId(propertyId);
            Statement claim = item.Claims.FirstOrDefault(x => property.Equals(x.mainSnak.PropertyId)) as Statement;

            var dataValue = new GlobeCoordinateValue(Convert.ToDouble(expected.lat), Convert.ToDouble(expected.@long), 0.0001, Globe.Earth);
            var snak = new Snak(SnakType.Value, new EntityId(propertyId), dataValue);
            if ( claim == null )
            {
                result = WikiDataState.NotSet;
                if ( createStatement )
                {
                    claim = item.createStatementForSnak(snak);
                }
            }
            else
            {
                Snak oldSnak = claim.mainSnak;
                var oldDataValue = oldSnak.DataValue as GlobeCoordinateValue;
                if ( (Math.Abs(oldDataValue.Latitude - dataValue.Latitude) < dataValue.Precision) &&
                    (Math.Abs(oldDataValue.Longitude - dataValue.Longitude) < dataValue.Precision) )
                {
                    result = WikiDataState.Valid;
                }
                else
                {
                    result = WikiDataState.WrongValue;
                    if ( overrideWrongData )
                    {
                        claim.mainSnak = snak;
                    }
                }
            }

            statement = claim as Statement;
            return result;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the location as a geopoint.
        /// </summary>
        /// <param name="item">Item to check.</param>
        /// <param name="propertyId">Id of the property.</param>
        /// <returns>Location as geopoint, or <c>null</c> if none is set.</returns>
        public GeoCoordinate GetCoordinateValue(Item item, String propertyId)
        {
            if ( item == null )
                throw new ArgumentNullException("item");

            // Statement claim = item.Claims.FirstOrDefault(x => x.IsAboutProperty(WikiBase.PropertyIdCountry)) as Statement;
            var property = new EntityId(propertyId);
            Statement claim = item.Claims.FirstOrDefault(x => property.Equals(x.mainSnak.PropertyId)) as Statement;
            if ( claim != null )
            {
                Snak oldSnak = claim.mainSnak;
                if ( oldSnak != null )
                {
                    var oldDataValue = oldSnak.DataValue as GlobeCoordinateValue;
                    if ( oldDataValue != null )
                    {
                        return new GeoCoordinate(oldDataValue.Latitude, oldDataValue.Longitude);
                    }
                }
            }
            return null;
        }
Exemplo n.º 6
0
        public EntityId GetCategoryOfItem(Entity entity)
        {
            if ( entity == null )
                throw new ArgumentNullException("entity");

            EntityId result = null;
            var item = GetWikiDataItemForEntity(entity);
            if ( item != null )
            {
                var categoryClaim = item.Claims.FirstOrDefault(x => x.IsAboutProperty(WikiBase.PropertyIdCategoryForTopic));
                if ( categoryClaim != null )
                {
                    var value = categoryClaim.mainSnak.DataValue as EntityIdValue;
                    if ( value != null )
                    {
                        result = new EntityId("P", value.NumericId);
                    }
                }
            }
            return result;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Checks and evetually sets a link property.
        /// </summary>
        /// <param name="item">Wikidata item.</param>
        /// <param name="propertyId">Id of the property.</param>
        /// <param name="expectedItemId">Expected item id.</param>
        /// <param name="createStatement"><c>true</c> to create the statement if not existing yet.</param>
        /// <param name="overrideWrongData"><c>true</c> to overwrite wrong data.</param>
        /// <param name="statement">Newly created statement.</param>
        /// <returns>Status of the link property.</returns>
        public WikiDataState CheckPropertyValue(Item item, String propertyId, String expectedItemId, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
        {
            if ( item == null )
                throw new ArgumentNullException("item");

            WikiDataState result = WikiDataState.Unknown;

            // Statement claim = item.Claims.FirstOrDefault(x => x.IsAboutProperty(WikiBase.PropertyIdCountry)) as Statement;
            var property = new EntityId(propertyId);
            Statement claim = item.Claims.FirstOrDefault(x => property.Equals(x.mainSnak.PropertyId)) as Statement;

            var entity = new EntityId(expectedItemId);
            var dataValue = new EntityIdValue(entity);

            var snak = new Snak(SnakType.Value, new EntityId(propertyId), dataValue);
            if ( claim == null )
            {
                if ( String.IsNullOrEmpty(expectedItemId) )
                {
                    result = WikiDataState.Valid;
                }
                else
                {
                    result = WikiDataState.NotSet;
                    if ( createStatement )
                    {
                        claim = item.createStatementForSnak(snak);
                    }
                }
            }
            else
            {
                Snak oldSnak = claim.mainSnak;
                var oldDataValue = oldSnak.DataValue as EntityIdValue;
                if ( oldDataValue.NumericId == dataValue.NumericId )
                {
                    result = WikiDataState.Valid;
                }
                else
                {
                    if ( !String.IsNullOrEmpty(expectedItemId) )
                    {
                        result = WikiDataState.WrongValue;
                        if ( overrideWrongData )
                        {
                            claim.mainSnak = snak;
                        }
                    }
                    else
                    {
                        result = WikiDataState.DataMissing;
                    }
                }
            }

            statement = claim as Statement;
            return result;
        }
Exemplo n.º 8
0
        private WikiDataState PopulationData(Item item, PopulationData data, Boolean createStatement, Boolean overrideWrongData, out Statement statement)
        {
            var total = data.data.FirstOrDefault(y => y.type == PopulationDataType.total);
            var propertyName = WikiBase.PropertyIdPopulation;

            WikiDataState result = WikiDataState.Unknown;

            // Statement claim = item.Claims.FirstOrDefault(x => x.IsAboutProperty(WikiBase.PropertyIdCountry)) as Statement;
            var property = new EntityId(propertyName);
            var propertyPointInTime = new EntityId(WikiBase.PropertyIdPointInTime);
            var claimsForProperty = item.Claims.Where(x => property.Equals(x.mainSnak.PropertyId));
            Statement claim = claimsForProperty.FirstOrDefault(
                x => x.Qualifiers.Any(
                    y => y.PropertyId.Equals(propertyPointInTime) &&
                         y.DataValue is TimeValue &&
                         (y.DataValue as TimeValue).DateTime.Year == data.Year)) as Statement;

            var dataValue = new QuantityValue(total.total);
            var snak = new Snak(SnakType.Value, new EntityId(propertyName), dataValue);
            if ( claim == null )
            {
                result = WikiDataState.NotSet;
                if ( createStatement )
                {
                    claim = item.createStatementForSnak(snak);
                }
            }
            else
            {
                Snak oldSnak = claim.mainSnak;
                var oldDataValue = snak.DataValue as QuantityValue;
                if ( oldDataValue.Equals(dataValue) )
                {
                    result = WikiDataState.Valid;
                }
                else
                {
                    result = WikiDataState.WrongValue;
                    if ( overrideWrongData )
                    {
                        claim.mainSnak = snak;
                    }
                }
            }

            statement = claim as Statement;
            return result;
        }
Exemplo n.º 9
0
        private WikiDataState CheckStringMultiValue(Item item, String propertyId, String expectedValue, Boolean createStatement, out Statement statement)
        {
            if ( item == null )
                throw new ArgumentNullException("item");

            WikiDataState result = WikiDataState.Unknown;

            if ( String.IsNullOrEmpty(expectedValue) )
            {
                statement = null;
                return result;  // TODO better handling!
            }
            var dataValue = new StringValue(expectedValue);
            var snak = new Snak(SnakType.Value, new EntityId(propertyId), dataValue);

            var property = new EntityId(propertyId);
            Statement foundStatement = null;
            foreach ( var claim in item.Claims.Where(x => property.Equals(x.mainSnak.PropertyId)) )
            {
                Snak oldSnak = claim.mainSnak;
                var oldDataValue = oldSnak.DataValue as StringValue;
                if ( oldDataValue.Value == dataValue.Value )
                {
                    foundStatement = claim as Statement;
                    result = WikiDataState.Valid;
                }
            }

            if ( foundStatement == null )
            {
                if ( String.IsNullOrEmpty(expectedValue) )
                {
                    result = WikiDataState.Valid;
                }
                else
                {
                    result = WikiDataState.Incomplete;
                    if ( createStatement )
                    {
                        foundStatement = item.createStatementForSnak(snak);
                    }
                }
            }

            statement = foundStatement;
            return result;
        }
Exemplo n.º 10
0
 /// <summary>
 /// Get the entity from the given entity id.
 /// </summary>
 /// <param name="id">The entity id</param>
 /// <returns>The entity</returns>
 public Entity getEntityFromId(EntityId id)
 {
     return this.getEntityFromId(id, null);
 }
Exemplo n.º 11
0
 /// <summary>
 /// Get the entities from the given entity ids.
 /// </summary>
 /// <param name="ids">The entity ids</param>
 /// <returns>The entities</returns>
 public Entity[] getEntitiesFromIds(EntityId[] ids)
 {
     return this.getEntitiesFromIds(ids, null);
 }