Module talk:WikidataIB

This is an old revision of this page, as edited by Johnuniq (talk | contribs) at 05:59, 21 January 2017 (Date handling: good). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Latest comment: 8 years ago by Johnuniq in topic Date handling

Testing

Let's see how this would work in Animal farm, d:Q1396889.

Local parameter

# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P50}}<
# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P50 |Fred Bloggs}}<
# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P50 |fetchwikidata=author; number_of_pages |name=author}}<
# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P50 |fetchwikidata=author; number_of_pages; |name=author |Freda Bloggs}}<
# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P50 |fetchwikidata=author; number_of_pages |name=author |suppressfields=author}}<
  1. ><
  2. >Fred Bloggs<
  3. >George Orwell  <
  4. >Freda Bloggs<
  5. ><

Only sourced

# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P50 |fetchwikidata=ALL |name=author |onlysourced=no}}<
# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P50 |fetchwikidata=ALL |name=author |onlysourced=yes}}<
# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P166 |fetchwikidata=ALL |name=awards |onlysourced=}}<
# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P166 |fetchwikidata=ALL |name=awards |onlysourced=no}}<
# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P166 |fetchwikidata=ALL |name=awards |onlysourced=yes}}<
  1. >George Orwell  <
  2. >George Orwell  <
  3. >Retro Hugo Award for Best Novella  <
  4. >Retro Hugo Award for Best Novella, Modern Library's 100 Best Novels, NPR Top 100 Science Fiction and Fantasy Books, Prometheus Award - Hall of Fame  <
  5. >Retro Hugo Award for Best Novella  <

No icon

# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P50 |fetchwikidata=ALL |name=author |noicon=}}<
# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P50 |fetchwikidata=ALL |name=author |noicon=no}}<
# >{{#invoke:WikidataIB |getValue |qid=Q1396889 |P50 |fetchwikidata=ALL |name=author |noicon=yes}}<
  1. >George Orwell  <
  2. >George Orwell  <
  3. >George Orwell<

Coordinates

Just to note that it would be nice to be able to set the coordinates to display both inline and in the title (passing "|display=inline,title" to {{coord}}); this module just seems to allow inline coordinates. Thanks. Mike Peel (talk) 17:37, 18 December 2016 (UTC)Reply

@Mike: I've implemented the 'display' parameter now. This is how it would work in Kitt Peak National Observatory (Q592248):
  • {{#invoke:WikidataIB |getCoords |qid=Q592248 |name=coord |fetchwikidata=ALL}}31°57′30″N 111°35′48″W / 31.9583°N 111.5967°W / 31.9583; -111.5967
You should see 31°57′30″N 111°35′48″W in the title position of this page as well.
You can't see multiple examples because the of title coordinates.
If you want to test the "title" or "inline" values, change the above to
  • {{#invoke:WikidataIB |getCoords |qid=Q592248 |display=title |name=coord |fetchwikidata=ALL}}
  • {{#invoke:WikidataIB |getCoords |qid=Q592248 |display=inline |name=coord |fetchwikidata=ALL}}
Of course, you don't need this so much now that the coord templates are Wikidata-aware, but it's useful if you want to implement the white/blacklists. --RexxS (talk) 11:55, 16 January 2017 (UTC)Reply

Format Dates

Some test cases for the function formatDate. Call like {{#invoke:WikidataIB |formatDate | 1 August 30 BCE |bc=BCE |df=dmy}}

Input date: 1 August 30 BCE, 12:39:56
no parameters 1 August 30 BCE
bc=BC 1 August 30 BC
bc=BCE 1 August 30 BCE
bc=bc 1 August 30 BC
bc=bce 1 August 30 BCE
bc=XYZ 1 August 30 BCE
df=dmy 1 August 30 BCE
df=DMY 1 August 30 BCE
df=mdy August 1, 30 BCE
df=MDY August 1, 30 BCE
df=ABC 1 August 30 BCE
df=y 30 BCE
bc=BCE df=dmy 1 August 30 BCE
bc=BCE df=mdy 1 August 30 BCE
bc=BCE df=y 30 BCE
bc=BC df=dmy 1 August 30 BC
bc=BC df=mdy August 1, 30 BC
bc=BC df=y 30 BC
Input date: 20 January 2017, 12:39:56
no parameters 20 January 2017
bc=BC 20 January 2017
bc=BCE 20 January 2017
bc=bc 20 January 2017
bc=bce 20 January 2017
bc=XYZ 20 January 2017
df=dmy 20 January 2017
df=DMY 20 January 2017
df=mdy January 20, 2017
df=MDY January 20, 2017
df=ABC 20 January 2017
df=y 2017
bc=BCE df=dmy 20 January 2017
bc=BCE df=mdy 20 January 2017
bc=BCE df=y 2017
bc=BC df=dmy 20 January 2017
bc=BC df=mdy January 20, 2017
bc=BC df=y 2017

Some test cases for the function formatDate. Call like {{#invoke:WikidataIB/sandbox |formatDate | 1 August 30 BCE |bc=BCE |df=dmy}}

Input date: 1 August 30 BCE, 12:39:56
no parameters 1 August 30 BCE
bc=BC 1 August 30 BC
bc=BCE 1 August 30 BCE
bc=bc 1 August 30 BC
bc=bce 1 August 30 BCE
bc=XYZ 1 August 30 BCE
df=dmy 1 August 30 BCE
df=DMY 1 August 30 BCE
df=mdy August 1, 30 BCE
df=MDY August 1, 30 BCE
df=ABC 1 August 30 BCE
df=y 30 BCE
bc=BCE df=dmy 1 August 30 BCE
bc=BCE df=mdy 1 August 30 BCE
bc=BCE df=y 30 BCE
bc=BC df=dmy 1 August 30 BC
bc=BC df=mdy August 1, 30 BC
bc=BC df=y 30 BC
Input date: 20 January 2017, 12:39:56
no parameters 20 January 2017
bc=BC 20 January 2017
bc=BCE 20 January 2017
bc=bc 20 January 2017
bc=bce 20 January 2017
bc=XYZ 20 January 2017
df=dmy 20 January 2017
df=DMY 20 January 2017
df=mdy January 20, 2017
df=MDY January 20, 2017
df=ABC 20 January 2017
df=y 2017
bc=BCE df=dmy 20 January 2017
bc=BCE df=mdy 20 January 2017
bc=BCE df=y 2017
bc=BC df=dmy 20 January 2017
bc=BC df=mdy January 20, 2017
bc=BC df=y 2017

Calls to getValue for dates

Some calls to getValue that return dates:

Can't get this working

About en:Prussian P 10, item: Prussian P 10 (Q882458). It has P176=Borsig (Company). So Wikidata seems OK.

In this case I test:

-{{#invoke:WikidataIB|getValue|qid=Q882458|P176}}-

Returns empty string: --

Also, when previewing in article page itself (no |qid= used), it returns a blank. Am I missing something? -DePiep (talk) 17:32, 15 January 2017 (UTC)Reply

@DePiep: Remember that this is designed for use in an infobox, therefore there are extra parameters that have to be enabled otherwise it won't return anything. The call matches the name of the field you're using it in with a list of fields that are to enabled ("whitelisted"). What you call the field doesn't matter as long as it's on the whitelist. So you can use:
  • {{#invoke:WikidataIB |getValue |qid=Q882458 |P176 |name=def |fetchwikidata=abd, def, ghi, jkl, etc}}
And it will return manufacturer (P176) for Prussian P 10 (Q882458). I'll miss out the qid in the first part of the following examples as you won't need it other than for testing on pages like this.
Rather than having to list every fieldname on the whitelist, you can use "ALL". So the call you probably want is something like:
  • {{#invoke:WikidataIB |getValue |P176 |name=manufacturer |fetchwikidata=ALL}}
Although if you're designing an infobox, you'll probably want to pass the whitelist (and blacklist) as parameters so the article editor supplies them once and the infobox passes them to each call in the design. Lets say we give them the names |whitelist= and |blacklist=. Then the code in the infobox design for the 'Manufacturer' field would be:
  • {{#invoke:WikidataIB |getValue |P176 |name=manufacturer |fetchwikidata={{{whitelist|}}} |suppressfields={{{blacklist|}}} |{{{manufacturer|}}} }}
The second unnamed parameter allows a local parameter (called 'manufacturer' in this case) to override the Wikidata call as ususal.
The infobox in use in an article could look something as simple as:
{{infobox something
| whitelist = ALL
}}
Does that make sense? Let me know if you're still having problems. Cheers --RexxS (talk) 02:03, 16 January 2017 (UTC)Reply
Thanks, will process this. Could this be in the documentation? Sort of basic "required parameters" list? To me, the description & examples there are already quite abstract (into high end usage). Quite an object that requires studying. -DePiep (talk) 09:16, 16 January 2017 (UTC)Reply

Date handling

If more extensive date handling is ever needed, consider Module:Date which can parse dates in various formats, including a Wikidata date, and perform date arithmetic. However, the module is limited to four-digit years. The template {{extract}} can be used for quick tests although it does not expose options to choose the BC/BCE format.

  • {{extract|+2016-06-21T14:30:00Z}} → 14:30 21 June 2016
  • {{extract|+2016-06-21T14:30:00Z|show=ymd}} → 14:30 2016-06-21

Johnuniq (talk) 23:41, 20 January 2017 (UTC)Reply

Thanks John. I think I had most of that functionality, as well as the BC/BCE and dmy/mdy/y functionality in Module:Wikidata in the getDateValue function, which dealt with any date BC or AD - I needed that for dob of notable Romans, etc. although the code was hard to read because I was using the timestamp and manipulating that directly. For greater flexibility in this module, I decided to use the mw.wikibase.formatPropertyValues() function – which always returns dmy format dates with BCE as appropriate – because that can be used on properties that are qualifiers as well. I'll need that later as fewer properties are defined and more values are stored as properties of qualifiers (like property:creator - qualifier:has role - item:video game artist instead of property:video game artist). Things like marriages have start-date and end-date that are in qualifiers. Anyway, I think I've managed to create a local function that takes the date in dmy+BCE format and outputs it in whatever format is wanted for use in article infoboxes. I've put a wrapper around it so that it's exported and can be invoked. --RexxS (talk) 04:00, 21 January 2017 (UTC)Reply
Good, and I'm sure Wikidata has many quirks which require dedicated code. I just wanted to say that if more stuff such as date differences or adding time deltas were ever needed, Module:Date is available. Johnuniq (talk) 05:59, 21 January 2017 (UTC)Reply