def test__create_claim_json_wikibase_item(self):
        expect = {
            "mainsnak": {
                "snaktype": "value",
                "property": "P31",
                "datatype": "wikibase-item",
                "datavalue": {
                    "value": {
                        "entity-type": "item",
                        "numeric-id": 123
                    },
                    "type": "wikibase-entityid"
                }
            },
            "type": "statement",
            "rank": "normal"
        }

        compare(
            expect,
            ClaimFactory.create_claim_json(
                SnakParameter(property_str="P31",
                              target_type="wikibase-item",
                              target="Q123")))
        compare(
            expect,
            ClaimFactory.create_claim_json(
                SnakParameter(property_str="P31",
                              target_type="wikibase-item",
                              target="123")))
 def threshold_of_originality(self) -> JsonClaimDict:
     qualifier_det = SnakParameter(self._DETERMINATION_METHOD, "wikibase-item",
                                   self._THRESHOLD_OF_ORIGINALITY)
     claim_parameter = SnakParameter(self._COPYRIGHT_STATUS, "wikibase-item", self._PUBLIC_DOMAIN)
     claim = self.create_claim_json(claim_parameter,
                                    qualifiers=[qualifier_det],
                                    references=[[self._IMPORTED_FROM_WIKISOURCE]])
     return claim
 def published_95_years_ago(self) -> JsonClaimDict:
     qualifier_jur = SnakParameter(self._APPLIES_TO_JURISDICTION, "wikibase-item",
                                   self._UNITED_STATES_OF_AMERICA)
     qualifier_det = SnakParameter(self._DETERMINATION_METHOD, "wikibase-item",
                                   self._PUBLISHED_MORE_THAN_THAN_95_YEARS_AGO)
     claim_parameter = SnakParameter(self._COPYRIGHT_STATUS, "wikibase-item", self._PUBLIC_DOMAIN)
     claim = self.create_claim_json(claim_parameter,
                                    qualifiers=[qualifier_jur, qualifier_det],
                                    references=[[self._IMPORTED_FROM_WIKISOURCE]])
     return claim
 def xx_years_after_authors_death(self, years) -> JsonClaimDict:
     qualifier_jur = SnakParameter(self._APPLIES_TO_JURISDICTION, "wikibase-item",
                                   getattr(self, f"_COUNTRIES_WITH_{years}_YEARS_PMA_OR_SHORTER"))
     qualifier_det = SnakParameter(self._DETERMINATION_METHOD, "wikibase-item",
                                   getattr(self, f"_{years}_YEARS_AFTER_AUTHORS_DEATH"))
     claim_parameter = SnakParameter(self._COPYRIGHT_STATUS, "wikibase-item", self._PUBLIC_DOMAIN)
     claim = self.create_claim_json(claim_parameter,
                                    qualifiers=[qualifier_jur, qualifier_det],
                                    references=[[self._IMPORTED_FROM_WIKISOURCE]])
     return claim
 def test__create_claim_json_exception(self):
     with self.assertRaises(BotException):
         ClaimFactory.create_claim_json(
             SnakParameter(property_str="P31",
                           target_type="tada",
                           target="123"))
     with self.assertRaises(ValueError):
         ClaimFactory.create_claim_json(
             SnakParameter(property_str="P31",
                           target_type="time",
                           target="tada"))
    def test__create_claim_json_with_qualifier(self):
        expect = {
            "mainsnak": {
                "snaktype": "value",
                "property": "P31",
                "datatype": "string",
                "datavalue": {
                    "value": "texttexttext",
                    "type": "string"
                }
            },
            "type": "statement",
            "rank": "normal",
            "qualifiers": {
                "P1234": [{
                    "snaktype": "value",
                    "property": "P1234",
                    "datatype": "string",
                    "datavalue": {
                        "value": "text",
                        "type": "string"
                    }
                }],
                "P5678": [{
                    "snaktype": "value",
                    "property": "P5678",
                    "datatype": "wikibase-item",
                    "datavalue": {
                        "value": {
                            "entity-type": "item",
                            "numeric-id": 123456
                        },
                        "type": "wikibase-entityid"
                    }
                }]
            },
            "qualifiers-order": ["P1234", "P5678"]
        }

        main_parameter = SnakParameter(property_str="P31",
                                       target_type="string",
                                       target="texttexttext")
        quali_snak_1 = SnakParameter(property_str="P1234",
                                     target_type="string",
                                     target="text")
        quali_snak_2 = SnakParameter(property_str="P5678",
                                     target_type="wikibase-item",
                                     target="Q123456")
        compare(
            expect,
            ClaimFactory.create_claim_json(
                main_parameter, qualifiers=[quali_snak_1, quali_snak_2]))
Exemple #7
0
 def _get_claim_json(self) -> List[JsonClaimDict]:
     return [
         self.create_claim_json(
             SnakParameter(property_str=self.get_property_string(),
                           target_type="wikibase-item",
                           target=id)) for id in self._get_author_list()
     ]
    def test__create_claim_json_time_just_year(self):
        expect = {
            "mainsnak": {
                "snaktype": "value",
                "property": "P31",
                "datatype": "time",
                "datavalue": {
                    "value": {
                        "time": "+00000001234-01-01T00:00:00Z",
                        "precision": 9,
                        "after": 0,
                        "before": 0,
                        "timezone": 0,
                        "calendarmodel":
                        "http://www.wikidata.org/entity/Q1985727"
                    },
                    "type": "time"
                }
            },
            "type": "statement",
            "rank": "normal"
        }

        compare(
            expect,
            ClaimFactory.create_claim_json(
                SnakParameter(property_str="P31",
                              target_type="time",
                              target="1234")))
    def _get_claim_json(self) -> List[JsonClaimDict]:
        wp_article = str(self.re_page.first_article["WIKIPEDIA"].value)
        # if no wp_article is present, there is nothing to connect
        if not wp_article:
            return []
        # handle the case of an implicit de:
        if ":" not in wp_article:
            wp_article = f"de:{wp_article}"
        language, wp_page_str = wp_article.split(":")
        wp_site: pywikibot.Site = pywikibot.Site(code=language,
                                                 fam="wikipedia")
        wp_page: pywikibot.Page = pywikibot.Page(wp_site, wp_page_str)
        # try to fetch the data item of the page, if not there is nothing to connect
        try:
            wp_data_item: pywikibot.ItemPage = wp_page.data_item()
        except (pywikibot.exceptions.NoPageError,
                pywikibot.exceptions.InvalidTitleError):
            return []
        # finally create the claim
        snak = SnakParameter(property_str=self.get_property_string(),
                             target_type="wikibase-item",
                             target=wp_data_item.id)

        return [
            self.create_claim_json(
                snak, references=[[self._IMPORTED_FROM_WIKISOURCE]])
        ]
Exemple #10
0
 def _get_claim_json(self) -> List[JsonClaimDict]:
     neighbour_item = self._get_item_of_neighbour_lemma()
     if neighbour_item:
         snak_parameter = SnakParameter(
             property_str=self.get_property_string(),
             target_type="wikibase-item",
             target=neighbour_item.id)
         return [self.create_claim_json(snak_parameter)]
     return []
Exemple #11
0
 def _get_claim_json(self) -> List[JsonClaimDict]:
     start = str(self.re_page.first_article["SPALTE_START"].value)
     end: str = ""
     if self.re_page.first_article["SPALTE_END"].value not in ("", "OFF"):
         end = str(self.re_page.first_article["SPALTE_END"].value)
     columns = start
     if end and start != end:
         columns = f"{start}–{end}"
     snak_parameter = SnakParameter(property_str=self.get_property_string(),
                                    target_type="string",
                                    target=columns)
     return [self.create_claim_json(snak_parameter)]
Exemple #12
0
 def _get_claim_json(self) -> List[JsonClaimDict]:
     article_type = get_article_type(self.re_page)
     if article_type == "index":
         target = self.INDEX
     elif article_type == "prologue":
         target = self.PROLOGUE
     elif article_type == "crossref":
         target = self.CROSS_REFERENCE_ITEM
     else:
         target = self.ENCYCLOPEDIC_ARTICLE_ITEM
     snak_parameter = SnakParameter(property_str=self.get_property_string(),
                                    target_type="wikibase-item",
                                    target=target)
     return [self.create_claim_json(snak_parameter)]
Exemple #13
0
class ClaimFactory:
    _authors = Authors()
    _volumes = Volumes()
    _IMPORTED_FROM_WIKISOURCE = SnakParameter(property_str="P143",
                                              target_type="wikibase-item",
                                              target="Q15522295")

    def __init__(self, re_page: RePage, logger: WikiLogger):
        self.wikisource: pywikibot.Site = pywikibot.Site(code="de", fam="wikisource", user="******")
        self.wikipedia: pywikibot.Site = pywikibot.Site(code="de", fam="wikipedia", user="******")
        self.wikidata: pywikibot.site.DataSite = self.wikisource.data_repository()
        self.re_page = re_page
        self.logger = logger
        self._current_year = datetime.now().year

    def _get_claim_json(self) -> List[JsonClaimDict]:
        raise NotImplementedError

    def execute_pre_action(self):
        pass

    def get_claims_to_update(self, data_item: pywikibot.ItemPage) -> ChangedClaimsDict:
        """
        Every claim that is updated can possible add new claims, but can also remove existing claims at the item.
        Which claims is removed or added depends of the specific implementation of the property factory. The standart
        implementation will update all claims as expected by the factory (this include removing possible existing
        claims).

        :param: data_item: item where the specific property is going to be altered

        :returns: A dictionary with claims to add and to remove is returned
        """

        claim_list = [pywikibot.Claim.fromJSON(self.wikidata, claim_json)
                      for claim_json in self._get_claim_json()]
        return self.get_diff_claims_for_replacement(claim_list, data_item)

    @classmethod
    def get_property_string(cls) -> str:
        """
        Returns the property string for this class
        """
        if regex_hit := re.search(r"^P\d{1,6}", cls.__name__):
            return regex_hit.group(0)
        return ""
    def test__create_claim_json_string(self):
        expect = {
            "mainsnak": {
                "snaktype": "value",
                "property": "P31",
                "datatype": "string",
                "datavalue": {
                    "value": "texttexttext",
                    "type": "string"
                }
            },
            "type": "statement",
            "rank": "normal"
        }

        compare(
            expect,
            ClaimFactory.create_claim_json(
                SnakParameter(property_str="P31",
                              target_type="string",
                              target="texttexttext")))
Exemple #15
0
 def _get_claim_json(self) -> List[JsonClaimDict]:
     snak_parameter = SnakParameter(property_str=self.get_property_string(),
                                    target_type="monolingualtext",
                                    target=self.re_page.lemma_without_prefix)
     return [self.create_claim_json(snak_parameter)]
Exemple #16
0
 def _get_claim_json(self) -> List[JsonClaimDict]:
     snak = SnakParameter(property_str=self.get_property_string(),
                          target_type="time",
                          target=self._volume_of_first_article.year)
     return [self.create_claim_json(snak)]
    def test__create_claim_json_with_reference(self):
        expect = {
            "mainsnak": {
                "snaktype": "value",
                "property": "P1234",
                "datatype": "string",
                "datavalue": {
                    "value": "value",
                    "type": "string"
                }
            },
            "type":
            "statement",
            "rank":
            "normal",
            "references": [{
                "snaks": {
                    "P123": [{
                        "snaktype": "value",
                        "property": "P123",
                        "datatype": "string",
                        "datavalue": {
                            "value": "ref1",
                            "type": "string"
                        }
                    }],
                    "P234": [{
                        "snaktype": "value",
                        "property": "P234",
                        "datatype": "string",
                        "datavalue": {
                            "value": "ref2",
                            "type": "string"
                        }
                    }]
                },
                "snaks-order": ["P123", "P234"]
            }, {
                "snaks": {
                    "P345": [{
                        "snaktype": "value",
                        "property": "P345",
                        "datatype": "string",
                        "datavalue": {
                            "value": "ref3",
                            "type": "string"
                        }
                    }],
                    "P456": [{
                        "snaktype": "value",
                        "property": "P456",
                        "datatype": "string",
                        "datavalue": {
                            "value": "ref4",
                            "type": "string"
                        }
                    }]
                },
                "snaks-order": ["P345", "P456"]
            }]
        }

        main_parameter = SnakParameter(property_str="P1234",
                                       target_type="string",
                                       target="value")
        ref_snak_1 = SnakParameter(property_str="P123",
                                   target_type="string",
                                   target="ref1")
        ref_snak_2 = SnakParameter(property_str="P234",
                                   target_type="string",
                                   target="ref2")
        ref_snak_3 = SnakParameter(property_str="P345",
                                   target_type="string",
                                   target="ref3")
        ref_snak_4 = SnakParameter(property_str="P456",
                                   target_type="string",
                                   target="ref4")
        compare(
            expect,
            ClaimFactory.create_claim_json(
                snak_parameter=main_parameter,
                references=[[ref_snak_1, ref_snak_2], [ref_snak_3,
                                                       ref_snak_4]]))
Exemple #18
0
 def _get_claim_json(self) -> List[JsonClaimDict]:
     snak_parameter = SnakParameter(
         property_str=self.get_property_string(),
         target_type="wikibase-item",
         target=self._volume_of_first_article.data_item)
     return [self.create_claim_json(snak_parameter)]
Exemple #19
0
 def _get_claim_json(self) -> List[JsonClaimDict]:
     snak_parameter = SnakParameter(property_str=self.get_property_string(),
                                    target_type="wikibase-item",
                                    target=self.GERMAN)
     return [self.create_claim_json(snak_parameter)]