Exemplo n.º 1
0
 protected void fillData(JsonObject data)
 {
     if (data.get("snaks") != null)
     {
         foreach (JsonObject.Member member in data.get("snaks").asObject())
         {
             Dictionary <string, Snak> list = new Dictionary <string, Snak>();
             foreach (JsonValue value in member.value.asArray())
             {
                 Snak snak = Snak.newFromArray(value.asObject());
                 list.Add(snak.dataValue.getHash(), snak);
             }
             this.snaks.Add(member.name, list);
         }
     }
     if (data.get("hash") != null)
     {
         this.hash = data.get("hash").asString();
     }
     if (this.internalId == null)
     {
         if (this.hash != null)
         {
             this.internalId = this.hash;
         }
         else
         {
             this.internalId = "" + Environment.TickCount + this.statement.internalId;
         }
     }
 }
Exemplo n.º 2
0
 public static Reference newFromSnaks(Statement statement, Snak[] snaks)
 {
     Reference reference = new Reference(statement, new JsonObject());
     foreach (Snak snak in snaks)
     {
         reference.addSnak(snak);
     }
     statement.addReference(reference);
     return reference;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Add a snak.
        /// </summary>
        /// <param name="snak">The snak</param>
        public void addSnak(Snak snak)
        {
            string property = snak.propertyId.getPrefixedId();

            if (!this.snaks.ContainsKey(property))
            {
                this.snaks[property] = new Dictionary <string, Snak>();
            }
            this.snaks[property][snak.dataValue.getHash()] = snak;
        }
Exemplo n.º 4
0
        public static Claim newFromSnak(Entity entity, Snak snak, string type)
        {
            Claim claim = newFromArray(
                entity,
                new JsonObject()
                .add("mainsnak", snak.toArray())
                .add("type", type)
                );

            claim.changes = new JsonObject()
                            .add("mainsnak", snak.toArray());
            entity.addClaim(claim);
            return(claim);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Remove the snak.
        /// </summary>
        /// <param name="snak">The snak</param>
        /// <returns>If the snak was removed successfully</returns>
        public bool removeSnak(Snak snak)
        {
            string property = snak.propertyId.getPrefixedId();

            if (!this.snaks.ContainsKey(property))
            {
                return(false);
            }
            if (this.snaks[property].Remove(snak.dataValue.getHash()))
            {
                if (this.snaks[property].Count == 0)
                {
                    this.snaks.Remove(property);
                }
                return(true);
            }
            return(false);
        }
Exemplo n.º 6
0
 protected virtual void fillData(JsonObject data)
 {
     if (data.get("mainsnak") != null)
     {
         this.mMainSnak = Snak.newFromArray(data.get("mainsnak").asObject());
     }
     if (data.get("id") != null)
     {
         this.id = data.get("id").asString();
     }
     if (this.internalId == null)
     {
         if (this.id != null)
         {
             this.internalId = this.id;
         }
         else
         {
             this.internalId = "" + Environment.TickCount + this.mMainSnak.propertyId + this.mMainSnak.dataValue;
         }
     }
 }
Exemplo n.º 7
0
 protected virtual void fillData(JsonObject data)
 {
     if (data.get("mainsnak") != null)
     {
         this.mMainSnak = Snak.newFromArray(data.get("mainsnak").asObject());
     }
     if (data.get("id") != null)
     {
         this.id = data.get("id").asString();
     }
     if (this.internalId == null)
     {
         if (this.id != null)
         {
             this.internalId = this.id;
         }
         else
         {
             this.internalId = "" + Environment.TickCount + this.mMainSnak.propertyId + this.mMainSnak.dataValue;
         }
     }
 }
Exemplo n.º 8
0
        public void AddPopulationDataReferences(Statement statement, PopulationData data, Entity entity)
        {
            Reference reference = null;
            Snak snak;
            switch ( data.source )
            {
                case PopulationDataSourceType.Census:
                    var statedInItem = String.Empty;
                    if ( WikiBase.ItemCensus.Keys.Contains(data.Year) )
                    {
                        statedInItem = WikiBase.ItemCensus[data.Year];
                    }
                    snak = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdStatedIn), new EntityIdValue(new EntityId(statedInItem)));
                    reference = statement.CreateReferenceForSnak(snak);
                    foreach ( var refItem in data.reference )
                    {
                        var urlReference = refItem as MyUri;
                        if ( urlReference != null )
                        {
                            reference.AddSnak(new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdReferenceUrl), new StringValue(urlReference.Value)));
                        }
                    }
                    break;

                case PopulationDataSourceType.DOPA:
                    Uri source = PopulationDataDownloader.GetDisplayUrl(data.Year, entity.geocode);
                    snak = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdReferenceUrl), new StringValue(source.AbsoluteUri));
                    reference = statement.CreateReferenceForSnak(snak);
                    reference.AddSnak(new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdPublisher), new EntityIdValue(new EntityId(WikiBase.ItemDopa))));
                    break;
            }

            if ( reference != null )
            {
                statement.AddReference(reference);
            }
        }
Exemplo n.º 9
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.º 10
0
 private Boolean CreateDateQualifierAndReference(Statement statement, String propertyId, HistoryEntryBase entityHistory)
 {
     var result = false;
     var startDateQualifier = statement.Qualifiers.FirstOrDefault(x => x.PropertyId.PrefixedId == propertyId);
     if ( startDateQualifier == null )
     {
         startDateQualifier = new Qualifier(statement, SnakType.Value, new EntityId(propertyId), TimeValue.DateValue(entityHistory.effective));
         result = true;
     }
     var gazetteReference = entityHistory.Items.FirstOrDefault(x => x is GazetteRelated) as GazetteRelated;
     if ( gazetteReference != null )
     {
         GazetteEntry gazetteAnnouncement = GlobalData.AllGazetteAnnouncements.FindAnnouncement(gazetteReference);
         if ( gazetteAnnouncement != null )
         {
             var snak = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdReferenceUrl), new StringValue(gazetteAnnouncement.DownloadUrl.AbsoluteUri));
             var startDateReference = statement.CreateReferenceForSnak(snak);
             result = true;
         }
     }
     return result;
 }
Exemplo n.º 11
0
        public void AddCategoryListOf(Item item, Entity entity, EntityType entityType)
        {
            if ( !item.Claims.Any(x => x.IsAboutProperty(WikiBase.PropertyIdIsListOf)) )
            {
                var dataValue = new EntityIdValue(new EntityId(WikiBase.WikiDataItems[entityType]));
                var snak = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdIsListOf), dataValue);
                var claim = item.createStatementForSnak(snak);
                claim.save(GetClaimSaveEditSummary(claim));

                var dataValue2 = new EntityIdValue(new EntityId(entity.wiki.wikidata));
                var qualifier = new Qualifier(claim, SnakType.Value, new EntityId(WikiBase.PropertyIdIsInAdministrativeUnit), dataValue2);
                qualifier.Save(GetQualifierSaveEditSummary(qualifier));
            }
        }
Exemplo n.º 12
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.º 13
0
        public void AddCategoryCombinesTopic(Item item, Entity entity, EntityType entityType)
        {
            if ( !item.Claims.Any(x => x.IsAboutProperty(WikiBase.PropertyIdCategoryCombinesTopic)) )
            {
                var dataValue1 = new EntityIdValue(new EntityId(WikiBase.WikiDataItems[entityType]));
                var snak1 = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdCategoryCombinesTopic), dataValue1);
                var claim1 = item.createStatementForSnak(snak1);
                claim1.save(GetClaimSaveEditSummary(claim1));

                var dataValue2 = new EntityIdValue(new EntityId(entity.wiki.wikidata));
                var snak2 = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdCategoryCombinesTopic), dataValue2);
                var claim2 = item.createStatementForSnak(snak2);
                claim2.save(GetClaimSaveEditSummary(claim2));
            }
        }
Exemplo n.º 14
0
 /// <summary>
 /// Create a new reference in this statement for the provided snak.
 /// </summary>
 /// <param name="snak">The snak</param>
 /// <returns>The reference</returns>
 public Reference createReferenceForSnak(Snak snak)
 {
     return Reference.newFromSnaks(this, new Snak[] { snak });
 }
Exemplo n.º 15
0
 /// <summary>
 /// Create a new statement in this item for the provided snak.
 /// </summary>
 /// <param name="snak">The snak</param>
 /// <returns>The statement</returns>
 public Statement createStatementForSnak(Snak snak)
 {
     return((Statement)Claim.newFromSnak(this, snak, "statement"));
 }
Exemplo n.º 16
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.º 17
0
 /// <summary>
 /// Add a snak.
 /// </summary>
 /// <param name="snak">The snak</param>
 public void addSnak(Snak snak)
 {
     string property = snak.propertyId.getPrefixedId();
     if (!this.snaks.ContainsKey(property))
     {
         this.snaks[property] = new Dictionary<string, Snak>();
     }
     this.snaks[property][snak.dataValue.getHash()] = snak;
 }
Exemplo n.º 18
0
 /// <summary>
 /// Create a new statement in this item for the provided snak.
 /// </summary>
 /// <param name="snak">The snak</param>
 /// <returns>The statement</returns>
 public Statement createStatementForSnak(Snak snak)
 {
     return (Statement) Claim.newFromSnak(this, snak, "statement");
 }
Exemplo n.º 19
0
 /// <summary>
 /// Remove the snak.
 /// </summary>
 /// <param name="snak">The snak</param>
 /// <returns>If the snak was removed successfully</returns>
 public bool removeSnak(Snak snak)
 {
     string property = snak.propertyId.getPrefixedId();
     if (!this.snaks.ContainsKey(property))
     {
         return false;
     }
     if (this.snaks[property].Remove(snak.dataValue.getHash()))
     {
         if (this.snaks[property].Count == 0)
         {
             this.snaks.Remove(property);
         }
         return true;
     }
     return false;
 }
Exemplo n.º 20
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.º 21
0
 public static Claim newFromSnak(Entity entity, Snak snak, string type)
 {
     Claim claim = newFromArray(
         entity,
         new JsonObject()
             .add("mainsnak", snak.toArray())
             .add("type", type)
     );
     claim.changes = new JsonObject()
         .add("mainsnak", snak.toArray());
     entity.addClaim(claim);
     return claim;
 }
Exemplo n.º 22
0
 private void SetSlogan(IEnumerable<Entity> entities, StringBuilder collisionInfo, Boolean overrideData)
 {
     if ( entities == null )
     {
         throw new ArgumentNullException("entities");
     }
     ClearRunInfo();
     foreach ( var entity in entities )
     {
         var item = _helper.GetWikiDataItemForEntity(entity);
         if ( item == null )
         {
             _runInfo[WikiDataState.ItemNotFound]++;
             collisionInfo.AppendFormat("{0}: {1} was deleted!", entity.wiki.wikidata, entity.english);
         }
         else
         {
             var state = _helper.SloganCorrect(item, entity);
             _runInfo[state]++;
             if ( state == WikiDataState.WrongValue )
             {
                 collisionInfo.AppendFormat("{0}: {1} has wrong slogan", item.id, entity.english);
                 collisionInfo.AppendLine();
             }
             if ( state != WikiDataState.Valid )
             {
                 var statement = _helper.SetSlogan(item, entity);
                 if ( statement != null )
                 {
                     statement.save(_helper.GetClaimSaveEditSummary(statement));
                     if ( (statement != null) && entity.type.IsCompatibleEntityType(EntityType.Amphoe) )
                     {
                         var source = AmphoeComHelper.AmphoeWebsite(entity.geocode);
                         if ( source != null )
                         {
                             var snak = new Snak(SnakType.Value, new EntityId(WikiBase.PropertyIdReferenceUrl), new StringValue(source.AbsoluteUri));
                             var sloganReference = statement.CreateReferenceForSnak(snak);
                             statement.AddReference(sloganReference);
                             foreach ( var reference in statement.References )
                             {
                                 reference.Save(_helper.GetReferenceSaveEditSummary(reference));
                             }
                         }
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 23
0
 /// <summary>
 /// Create a new reference in this statement for the provided snak.
 /// </summary>
 /// <param name="snak">The snak</param>
 /// <returns>The reference</returns>
 public Reference createReferenceForSnak(Snak snak)
 {
     return(Reference.newFromSnaks(this, new Snak[] { snak }));
 }