wikibase-cli 16.3.2 → 17.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/wb-add-alias.js +12 -0
- package/bin/wb-add-badge.js +12 -0
- package/bin/wb-add-claim.js +14 -0
- package/bin/wb-add-qualifier.js +14 -0
- package/bin/wb-add-reference.js +14 -0
- package/bin/wb-aliases.js +4 -0
- package/bin/wb-badges.js +18 -0
- package/bin/{wb-claims → wb-claims.js} +19 -14
- package/bin/{wb-config → wb-config.js} +8 -6
- package/bin/{wb-convert → wb-convert.js} +11 -12
- package/bin/wb-create-entity.js +10 -0
- package/bin/{wb-data → wb-data.js} +15 -11
- package/bin/wb-delete-entity.js +10 -0
- package/bin/wb-description.js +4 -0
- package/bin/wb-edit-entity.js +10 -0
- package/bin/{wb-generate-template → wb-generate-template.js} +24 -13
- package/bin/{wb-hub → wb-hub.js} +10 -8
- package/bin/{wb-id → wb-id.js} +14 -11
- package/bin/wb-label.js +4 -0
- package/bin/{wb-lang → wb-lang.js} +8 -8
- package/bin/wb-lemma.js +4 -0
- package/bin/{wb-merge-entity → wb-merge-entity.js} +6 -4
- package/bin/wb-move-claim.js +20 -0
- package/bin/wb-move-qualifier.js +21 -0
- package/bin/{wb-open → wb-open.js} +23 -22
- package/bin/{wb-props → wb-props.js} +8 -9
- package/bin/{wb-query → wb-query.js} +13 -9
- package/bin/wb-remove-alias.js +12 -0
- package/bin/wb-remove-badge.js +12 -0
- package/bin/wb-remove-claim.js +11 -0
- package/bin/wb-remove-description.js +16 -0
- package/bin/wb-remove-label.js +16 -0
- package/bin/wb-remove-qualifier.js +15 -0
- package/bin/wb-remove-reference.js +15 -0
- package/bin/wb-remove-sitelink.js +16 -0
- package/bin/{wb-revisions → wb-revisions.js} +15 -11
- package/bin/{wb-search → wb-search.js} +17 -15
- package/bin/wb-set-alias.js +12 -0
- package/bin/wb-set-description.js +12 -0
- package/bin/wb-set-label.js +12 -0
- package/bin/wb-set-sitelink.js +17 -0
- package/bin/{wb-sparql → wb-sparql.js} +16 -12
- package/bin/{wb-summary → wb-summary.js} +3 -3
- package/bin/{wb-update-claim → wb-update-claim.js} +9 -6
- package/bin/wb-update-qualifier.js +14 -0
- package/bin/wb.js +27 -0
- package/bin/{wd → wd.js} +2 -3
- package/index.js +7 -1
- package/lib/apply_env_and_config_default.js +7 -5
- package/lib/chalk.js +12 -0
- package/lib/command_with_template_custom_help.js +7 -7
- package/lib/common_command_args_parser.js +7 -10
- package/lib/common_errors.js +5 -7
- package/lib/common_options.js +23 -24
- package/lib/config/config.js +10 -5
- package/lib/config/credentials_config.js +7 -6
- package/lib/config/file_operations.js +9 -8
- package/lib/config/file_path.js +11 -8
- package/lib/config/parameters.js +11 -10
- package/lib/convert_ids.js +10 -8
- package/lib/copy.js +2 -2
- package/lib/drop_non_selected_subprops.js +3 -3
- package/lib/edit/assert_credentials.js +3 -3
- package/lib/edit/edit_command.js +19 -19
- package/lib/edit/init_credentials.js +11 -11
- package/lib/edit/parse_batch_line.js +2 -2
- package/lib/edit/parse_object_value.js +6 -5
- package/lib/edit/prompt.js +7 -5
- package/lib/edit/validate_instance_credentials.js +2 -2
- package/lib/entity_attribute_command.js +10 -10
- package/lib/entity_attribute_parser.js +8 -8
- package/lib/entity_data_parser.js +14 -11
- package/lib/errors.js +3 -3
- package/lib/execute_function.js +4 -4
- package/lib/exit_on_missing.js +11 -12
- package/lib/fetch_and_log_entity_revision.js +18 -15
- package/lib/fetch_and_log_guid_data.js +19 -11
- package/lib/fetch_and_log_ids_data.js +17 -16
- package/lib/fetch_and_log_property_claims_data.js +18 -10
- package/lib/fetch_lang_props.js +13 -12
- package/lib/filter_claims_properties.js +4 -4
- package/lib/format_properties_data.js +2 -2
- package/lib/format_statement_element_value.js +3 -3
- package/lib/fs.js +27 -25
- package/lib/generate_describe_sparql_query.js +3 -3
- package/lib/generate_sample_sparql_query.js +1 -1
- package/lib/generate_select_sparql_query.js +9 -6
- package/lib/get_cache_folder_path.js +5 -5
- package/lib/get_claims_texts.js +8 -7
- package/lib/get_entities.js +6 -5
- package/lib/get_entities_by_batches.js +12 -7
- package/lib/get_entities_labels.js +3 -5
- package/lib/get_entities_schemas.js +7 -7
- package/lib/get_folder_path.js +8 -6
- package/lib/get_lang_props.js +17 -16
- package/lib/get_pattern_filter.js +18 -9
- package/lib/get_properties_data.js +5 -5
- package/lib/get_redirected_sitelink_title.js +4 -4
- package/lib/get_sitelink_url.js +9 -8
- package/lib/get_stdin_input.js +1 -1
- package/lib/get_ttl_entities.js +8 -8
- package/lib/global_options_help.js +1 -1
- package/lib/hash_string.js +4 -2
- package/lib/json.js +5 -0
- package/lib/local_lang.js +2 -1
- package/lib/log_claims.js +12 -12
- package/lib/log_command_examples.js +3 -3
- package/lib/log_ndjson.js +1 -1
- package/lib/make_sparql_query.js +29 -25
- package/lib/matching_langs.js +4 -6
- package/lib/minimize_claims.js +3 -5
- package/lib/object_arg_parser.js +12 -17
- package/lib/open.js +7 -7
- package/lib/output.js +37 -35
- package/lib/output_templates.js +41 -31
- package/lib/parse_command_utils.js +14 -15
- package/lib/parse_entity_type.js +2 -2
- package/lib/parse_props.js +7 -4
- package/lib/parse_simplify_options.js +1 -1
- package/lib/path.js +3 -2
- package/lib/pluralize.js +2 -2
- package/lib/polymorphic_command_args_parser.js +11 -9
- package/lib/program.js +9 -9
- package/lib/properties.js +22 -23
- package/lib/queries/all_instances.js +2 -2
- package/lib/queries/all_properties.js +1 -1
- package/lib/read_ids_from_stdin.js +3 -3
- package/lib/request.js +22 -23
- package/lib/request_headers.js +8 -2
- package/lib/reset_properties.js +4 -4
- package/lib/sparql_query_command.js +10 -8
- package/lib/stringify_as_js_function.js +3 -3
- package/lib/summary_parser.js +43 -13
- package/lib/tabularize.js +7 -7
- package/lib/tolerant_id_parser.js +21 -18
- package/lib/types.js +4 -6
- package/lib/utils.js +11 -13
- package/lib/validate.js +2 -0
- package/lib/validate_function_args.js +2 -2
- package/lib/value_parsers.js +2 -2
- package/lib/wbk.js +4 -3
- package/lib/wellknown_queries.js +6 -5
- package/metadata/add-alias.js +6 -4
- package/metadata/add-badge.js +5 -3
- package/metadata/add-claim.js +5 -5
- package/metadata/add-qualifier.js +6 -4
- package/metadata/add-reference.js +6 -4
- package/metadata/aliases.js +6 -4
- package/metadata/badges.js +3 -3
- package/metadata/claims.js +4 -4
- package/metadata/config.js +5 -5
- package/metadata/convert.js +4 -4
- package/metadata/create-entity.js +8 -8
- package/metadata/data.js +4 -4
- package/metadata/delete-entity.js +7 -7
- package/metadata/description.js +6 -4
- package/metadata/edit-entity.js +10 -10
- package/metadata/generate-template.js +4 -4
- package/metadata/hub.js +3 -3
- package/metadata/id.js +4 -4
- package/metadata/label.js +5 -3
- package/metadata/lang.js +4 -4
- package/metadata/lemma.js +6 -4
- package/metadata/merge-entity.js +6 -6
- package/metadata/move-claim.js +6 -4
- package/metadata/move-qualifier.js +6 -4
- package/metadata/open.js +4 -4
- package/metadata/props.js +4 -4
- package/metadata/query.js +3 -3
- package/metadata/remove-alias.js +6 -4
- package/metadata/remove-badge.js +5 -3
- package/metadata/remove-claim.js +6 -4
- package/metadata/remove-description.js +5 -3
- package/metadata/remove-label.js +5 -3
- package/metadata/remove-qualifier.js +6 -4
- package/metadata/remove-reference.js +6 -4
- package/metadata/remove-sitelink.js +5 -3
- package/metadata/revisions.js +4 -4
- package/metadata/search.js +3 -3
- package/metadata/set-alias.js +6 -4
- package/metadata/set-description.js +6 -4
- package/metadata/set-label.js +6 -4
- package/metadata/set-sitelink.js +5 -3
- package/metadata/sparql.js +4 -4
- package/metadata/summary.js +5 -5
- package/metadata/update-claim.js +5 -5
- package/metadata/update-qualifier.js +6 -4
- package/package.json +24 -16
- package/bin/wb +0 -22
- package/bin/wb-add-alias +0 -9
- package/bin/wb-add-badge +0 -9
- package/bin/wb-add-claim +0 -11
- package/bin/wb-add-qualifier +0 -11
- package/bin/wb-add-reference +0 -11
- package/bin/wb-aliases +0 -2
- package/bin/wb-badges +0 -18
- package/bin/wb-create-entity +0 -5
- package/bin/wb-delete-entity +0 -7
- package/bin/wb-description +0 -2
- package/bin/wb-edit-entity +0 -5
- package/bin/wb-label +0 -2
- package/bin/wb-lemma +0 -2
- package/bin/wb-move-claim +0 -17
- package/bin/wb-move-qualifier +0 -18
- package/bin/wb-remove-alias +0 -9
- package/bin/wb-remove-badge +0 -9
- package/bin/wb-remove-claim +0 -8
- package/bin/wb-remove-description +0 -13
- package/bin/wb-remove-label +0 -13
- package/bin/wb-remove-qualifier +0 -12
- package/bin/wb-remove-reference +0 -12
- package/bin/wb-remove-sitelink +0 -13
- package/bin/wb-set-alias +0 -9
- package/bin/wb-set-description +0 -9
- package/bin/wb-set-label +0 -9
- package/bin/wb-set-sitelink +0 -14
- package/bin/wb-update-qualifier +0 -11
- package/lib/valide.js +0 -4
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { isEntityId } from 'wikibase-sdk'
|
|
2
|
+
import { exitOnMissingInstance } from '#lib/exit_on_missing'
|
|
3
|
+
import errors_ from './errors.js'
|
|
4
|
+
import program from './program.js'
|
|
5
|
+
import { get } from './request.js'
|
|
6
|
+
import { wait } from './utils.js'
|
|
7
|
+
import wbk from './wbk.js'
|
|
8
|
+
|
|
7
9
|
const noop = () => {}
|
|
8
10
|
|
|
9
|
-
|
|
11
|
+
export async function getEntitiesByBatches ({ ids, props, languages, urls, onResponse, onDone = noop }) {
|
|
12
|
+
exitOnMissingInstance(program.instance)
|
|
13
|
+
const { getManyEntities } = wbk(program)
|
|
14
|
+
|
|
10
15
|
if (ids != null) {
|
|
11
16
|
ids.forEach(id => {
|
|
12
17
|
if (!isEntityId(id)) throw new Error(`invalid entity id: ${id}`)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import getEntities from './get_entities.js'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export async function getEntitiesLabels (ids, lang = 'en') {
|
|
4
4
|
const entities = await getEntities({ ids, languages: lang, props: 'labels' })
|
|
5
5
|
return parseEntitiesLabels(entities, lang)
|
|
6
6
|
}
|
|
@@ -17,11 +17,9 @@ const parseEntitiesLabels = (entities, lang) => {
|
|
|
17
17
|
|
|
18
18
|
const parseLangLabel = (labels, lang) => labels && labels[lang] && labels[lang].value
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
export async function getEntityLabel (id, lang) {
|
|
21
21
|
const labels = await getEntitiesLabels([ id ], lang)
|
|
22
22
|
const label = labels[id]
|
|
23
23
|
if (!label) throw new Error(`label not found: ${id}`)
|
|
24
24
|
return label
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
module.exports = { getEntitiesLabels, getEntityLabel }
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import fetch from 'node-fetch'
|
|
2
|
+
import { isEntitySchemaId } from 'wikibase-sdk'
|
|
3
|
+
import { exitOnMissingInstance } from '#lib/exit_on_missing'
|
|
4
|
+
import program from './program.js'
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const instanceDomain = instance.replace('/w/api.php', '')
|
|
6
|
+
export function getEntitiesSchemas (ids) {
|
|
7
|
+
exitOnMissingInstance(program.instance)
|
|
8
|
+
const instanceDomain = program.instance.replace('/w/api.php', '')
|
|
8
9
|
|
|
9
|
-
module.exports = ids => {
|
|
10
10
|
ids.forEach(id => {
|
|
11
11
|
if (!isEntitySchemaId(id)) throw new Error(`invalid entity schema id: ${id}`)
|
|
12
12
|
})
|
package/lib/get_folder_path.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import os from 'node:os'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import mkdirp from 'mkdirp'
|
|
4
|
+
import { writeAccessSync } from './fs.js'
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
const homePath = os.homedir()
|
|
7
|
+
|
|
8
|
+
export default (category, subfolderName) => {
|
|
7
9
|
let folderPath
|
|
8
10
|
if (process.platform === 'win32') {
|
|
9
11
|
folderPath = path.win32.join(homePath, 'wikibase-cli', category || subfolderName)
|
|
@@ -15,7 +17,7 @@ module.exports = (category, subfolderName) => {
|
|
|
15
17
|
}
|
|
16
18
|
mkdirp.sync(folderPath)
|
|
17
19
|
// Make sure we have write access right in case the folder already existed
|
|
18
|
-
|
|
20
|
+
writeAccessSync(folderPath)
|
|
19
21
|
|
|
20
22
|
return folderPath
|
|
21
23
|
}
|
package/lib/get_lang_props.js
CHANGED
|
@@ -1,38 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { URL } from 'node:url'
|
|
2
|
+
import getCacheFolderPath from '#lib/get_cache_folder_path'
|
|
3
|
+
import { hashString } from '#lib/hash_string'
|
|
4
|
+
import { readJsonFile } from '#lib/json'
|
|
5
|
+
import fetchLangProps from './fetch_lang_props.js'
|
|
6
|
+
import { writeFile } from './fs.js'
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
export default async program => {
|
|
7
9
|
const { lang, sparqlEndpoint, noCache } = program
|
|
8
10
|
const sparqlEndpointName = getSparqlEndpointPart(sparqlEndpoint)
|
|
9
11
|
|
|
10
12
|
const propsDir = await getCacheFolderPath('props')
|
|
11
|
-
const
|
|
13
|
+
const langJsonFilePath = `${propsDir}/${lang}${sparqlEndpointName}.v2.json`
|
|
12
14
|
|
|
13
|
-
if (noCache) return refreshProps(program,
|
|
15
|
+
if (noCache) return refreshProps(program, langJsonFilePath)
|
|
14
16
|
|
|
15
17
|
try {
|
|
16
|
-
|
|
17
|
-
return propsSync
|
|
18
|
+
return readJsonFile(langJsonFilePath)
|
|
18
19
|
} catch (err) {
|
|
19
|
-
return refreshProps(program,
|
|
20
|
+
return refreshProps(program, langJsonFilePath)
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
const refreshProps = async (program,
|
|
24
|
-
await fetchAndSaveProps(program,
|
|
25
|
-
return
|
|
24
|
+
const refreshProps = async (program, langJsonFilePath) => {
|
|
25
|
+
await fetchAndSaveProps(program, langJsonFilePath)
|
|
26
|
+
return readJsonFile(langJsonFilePath)
|
|
26
27
|
}
|
|
27
28
|
|
|
28
|
-
const fetchAndSaveProps = async (program,
|
|
29
|
+
const fetchAndSaveProps = async (program, langJsonFilePath) => {
|
|
29
30
|
const props = await fetchLangProps(program)
|
|
30
|
-
return
|
|
31
|
+
return writeFile(langJsonFilePath, JSON.stringify(props))
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
const getSparqlEndpointPart = sparqlEndpoint => {
|
|
34
35
|
if (!sparqlEndpoint) return ''
|
|
35
36
|
const { host } = new URL(sparqlEndpoint)
|
|
36
|
-
const hash =
|
|
37
|
+
const hash = hashString(sparqlEndpoint)
|
|
37
38
|
return `-${host}-${hash}`
|
|
38
39
|
}
|
|
@@ -1,19 +1,28 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import program from './program.js'
|
|
2
|
+
import { propTypes } from './properties.js'
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
const normalizeTypeName = name => name.replace(/-/g, '').toLowerCase()
|
|
5
|
+
const normalizedTypesNames = Object.keys(propTypes).map(normalizeTypeName)
|
|
6
|
+
|
|
7
|
+
export default (pattern, includeTypes) => {
|
|
5
8
|
const { type } = program
|
|
9
|
+
let normalizedType, isKnownType
|
|
10
|
+
// If --type is called without argument, `type` will be a boolean
|
|
11
|
+
if (typeof type === 'string') {
|
|
12
|
+
normalizedType = normalizeTypeName(type)
|
|
13
|
+
isKnownType = normalizedTypesNames.includes(normalizedType)
|
|
14
|
+
}
|
|
6
15
|
if (!(pattern && pattern.length > 0)) {
|
|
7
|
-
if (
|
|
16
|
+
if (normalizedType) {
|
|
8
17
|
// Consider that what was passed as a type argument was actually a pattern
|
|
9
|
-
if (!
|
|
18
|
+
if (!isKnownType) pattern = type
|
|
10
19
|
} else {
|
|
11
20
|
return
|
|
12
21
|
}
|
|
13
22
|
}
|
|
14
23
|
|
|
15
|
-
if (includeTypes &&
|
|
16
|
-
return propData => propData.type ===
|
|
24
|
+
if (includeTypes && isKnownType) {
|
|
25
|
+
return propData => normalizeTypeName(propData.type) === normalizedType
|
|
17
26
|
}
|
|
18
27
|
|
|
19
28
|
// Set the insensitive case flag only if only lowercased was used
|
|
@@ -23,8 +32,8 @@ module.exports = (pattern, includeTypes) => {
|
|
|
23
32
|
const filterRegex = new RegExp(pattern, flag)
|
|
24
33
|
|
|
25
34
|
let typeFilter
|
|
26
|
-
if (
|
|
27
|
-
typeFilter = propData => propData.type ===
|
|
35
|
+
if (normalizedType && isKnownType) {
|
|
36
|
+
typeFilter = propData => normalizeTypeName(propData.type) === normalizedType
|
|
28
37
|
} else {
|
|
29
38
|
typeFilter = () => true
|
|
30
39
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { simplify } from 'wikibase-sdk'
|
|
2
|
+
import getEntities from './get_entities.js'
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
export default async (ids, lang = 'en') => {
|
|
5
5
|
const properties = await getEntities({
|
|
6
6
|
ids,
|
|
7
7
|
languages: [ lang, 'en' ],
|
|
8
|
-
props: [ 'info', 'labels' ]
|
|
8
|
+
props: [ 'info', 'labels' ],
|
|
9
9
|
})
|
|
10
10
|
const propertiesData = {}
|
|
11
11
|
Object.keys(properties).forEach(id => {
|
|
@@ -13,7 +13,7 @@ module.exports = async (ids, lang = 'en') => {
|
|
|
13
13
|
const labels = simplify.labels(property.labels)
|
|
14
14
|
propertiesData[id] = {
|
|
15
15
|
type: property.datatype,
|
|
16
|
-
label: labels[lang] || labels.en
|
|
16
|
+
label: labels[lang] || labels.en,
|
|
17
17
|
}
|
|
18
18
|
})
|
|
19
19
|
return propertiesData
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { values } from 'lodash-es'
|
|
2
|
+
import { get } from '#lib/request'
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
export default (lang, site, title) => {
|
|
5
5
|
title = encodeURIComponent(title)
|
|
6
6
|
const url = `https://${lang}.${site}.org/w/api.php?format=json&action=query&redirects&titles=${title}`
|
|
7
7
|
return get(url)
|
|
8
|
-
.then(body =>
|
|
8
|
+
.then(body => values(body.query.pages)[0].title)
|
|
9
9
|
}
|
package/lib/get_sitelink_url.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import errors_ from '#lib/errors'
|
|
2
|
+
import { exitOnMissingInstance } from '#lib/exit_on_missing'
|
|
3
|
+
import { get } from '#lib/request'
|
|
4
|
+
import wbk from '#lib/wbk'
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const { getEntities } =
|
|
8
|
-
return async (id, lang, sitelinkSuffix)
|
|
6
|
+
export function getSitelinkUrlFactory (program) {
|
|
7
|
+
exitOnMissingInstance(program.instance)
|
|
8
|
+
const { getEntities } = wbk(program)
|
|
9
|
+
return async function (id, lang, sitelinkSuffix) {
|
|
9
10
|
if (id[0] === 'P') {
|
|
10
11
|
console.error("Wikibase properties don't have sitelinks")
|
|
11
12
|
process.exit(1)
|
|
@@ -25,7 +26,7 @@ module.exports = program => {
|
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
const sitelinkDomain = {
|
|
28
|
-
wiki: 'wikipedia.org'
|
|
29
|
+
wiki: 'wikipedia.org',
|
|
29
30
|
}
|
|
30
31
|
|
|
31
32
|
const formatTitle = title => {
|
package/lib/get_stdin_input.js
CHANGED
package/lib/get_ttl_entities.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import crypto from 'node:crypto'
|
|
2
|
+
import fetch from 'node-fetch'
|
|
3
|
+
import { isEntityId } from 'wikibase-sdk'
|
|
4
|
+
import { exitOnMissingInstance } from '#lib/exit_on_missing'
|
|
5
|
+
import program from '#lib/program'
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const instanceDomain = instance.replace('/w/api.php', '')
|
|
7
|
+
export default ids => {
|
|
8
|
+
exitOnMissingInstance(program.instance)
|
|
9
|
+
const instanceDomain = program.instance.replace('/w/api.php', '')
|
|
9
10
|
|
|
10
|
-
module.exports = ids => {
|
|
11
11
|
ids.forEach(id => {
|
|
12
12
|
if (!isEntityId(id)) throw new Error(`invalid entity id: ${id}`)
|
|
13
13
|
})
|
package/lib/hash_string.js
CHANGED
package/lib/json.js
ADDED
package/lib/local_lang.js
CHANGED
package/lib/log_claims.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { chain, max, padEnd } from 'lodash-es'
|
|
2
|
+
import { isEntityId } from 'wikibase-sdk'
|
|
3
|
+
import { bgMagenta, grey } from '#lib/chalk'
|
|
4
|
+
import filterClaimsProperties from '#lib/filter_claims_properties'
|
|
5
|
+
import getClaimsTexts from '#lib/get_claims_texts'
|
|
6
|
+
import { getEntitiesLabels } from './get_entities_labels.js'
|
|
7
|
+
import getPropertiesData from './get_properties_data.js'
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
export default async ({ program, simplifiedClaims, extraData = {}, pattern, resort }) => {
|
|
10
10
|
const { lang } = program
|
|
11
11
|
const ids = collectEntityIds(simplifiedClaims, extraData)
|
|
12
12
|
const labels = await getEntitiesLabels(ids, lang)
|
|
@@ -24,11 +24,11 @@ module.exports = async ({ program, simplifiedClaims, extraData = {}, pattern, re
|
|
|
24
24
|
labels,
|
|
25
25
|
simplifiedClaims,
|
|
26
26
|
resort,
|
|
27
|
-
lang
|
|
27
|
+
lang,
|
|
28
28
|
})
|
|
29
29
|
|
|
30
30
|
const propTextLength = claimsTexts.map(data => data.propText.length)
|
|
31
|
-
const longestPropText =
|
|
31
|
+
const longestPropText = max(propTextLength) || 0
|
|
32
32
|
|
|
33
33
|
const claimsText = claimsTexts.map(propClaimsText(longestPropText))
|
|
34
34
|
const lineBreaks = claimsTexts.length > 3 ? '\n\n' : '\n'
|
|
@@ -36,7 +36,7 @@ module.exports = async ({ program, simplifiedClaims, extraData = {}, pattern, re
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
const collectEntityIds = (claims, extraData) => {
|
|
39
|
-
const claimIds =
|
|
39
|
+
const claimIds = chain(claims)
|
|
40
40
|
.values()
|
|
41
41
|
.flatten()
|
|
42
42
|
.filter(isEntityId)
|
|
@@ -53,7 +53,7 @@ const propClaimsText = targetLength => claimsTextData => {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
const harmonizedLength = (str, minLength) => {
|
|
56
|
-
if (minLength && str.length < minLength) str =
|
|
56
|
+
if (minLength && str.length < minLength) str = padEnd(str, minLength)
|
|
57
57
|
return str
|
|
58
58
|
}
|
|
59
59
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { execSync } from 'node:child_process'
|
|
2
|
+
import { grey } from '#lib/chalk'
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
export default (command, examples, doc, dryRun = false) => {
|
|
5
5
|
if (examples && examples.length > 0) {
|
|
6
6
|
console.log('\nExamples:')
|
|
7
7
|
for (const example of examples) {
|
package/lib/log_ndjson.js
CHANGED
package/lib/make_sparql_query.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { simplify } from 'wikibase-sdk'
|
|
2
|
+
import { grey } from '#lib/chalk'
|
|
3
|
+
import errors_ from '#lib/errors'
|
|
4
|
+
import { exitOnMissingSparqlEndpoint } from '#lib/exit_on_missing'
|
|
5
|
+
import { outputFactory } from '#lib/output'
|
|
6
|
+
import program from '#lib/program'
|
|
7
|
+
import { customGet, post } from '#lib/request'
|
|
8
|
+
import wbk from '#lib/wbk'
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
const { index: indexAttribute } = program
|
|
10
|
-
|
|
11
|
-
module.exports = (sparql, format = 'json') => {
|
|
10
|
+
export default (sparql, format = 'json') => {
|
|
12
11
|
// JIT require to be sure program.process was already called
|
|
13
12
|
// and program.sparqlEndpoint is set
|
|
14
|
-
|
|
15
|
-
const { sparqlQuery } =
|
|
13
|
+
exitOnMissingSparqlEndpoint(program.sparqlEndpoint)
|
|
14
|
+
const { sparqlQuery } = wbk(program)
|
|
16
15
|
|
|
17
16
|
if (localFormatsDerivedFromJSON.includes(format)) format = 'json'
|
|
18
17
|
|
|
@@ -22,7 +21,8 @@ module.exports = (sparql, format = 'json') => {
|
|
|
22
21
|
.catch(errors_.exit)
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
|
|
24
|
+
function makeRequest (url, format) {
|
|
25
|
+
const output = outputFactory(program)
|
|
26
26
|
const acceptHeader = formatAcceptHeader[format]
|
|
27
27
|
if (!acceptHeader) throw new Error(`unknown format: ${format}`)
|
|
28
28
|
// Avoid making a POST request when not necessary as those aren't cached
|
|
@@ -32,8 +32,8 @@ const makeRequest = (url, format) => {
|
|
|
32
32
|
return customGet({
|
|
33
33
|
url,
|
|
34
34
|
headers: {
|
|
35
|
-
Accept: formatAcceptHeader[format]
|
|
36
|
-
}
|
|
35
|
+
Accept: formatAcceptHeader[format],
|
|
36
|
+
},
|
|
37
37
|
})
|
|
38
38
|
} else {
|
|
39
39
|
const [ postUrl, urlencodedSparql ] = url.split('?')
|
|
@@ -44,7 +44,7 @@ const makeRequest = (url, format) => {
|
|
|
44
44
|
headers: {
|
|
45
45
|
'Content-type': 'application/sparql-query',
|
|
46
46
|
Accept: formatAcceptHeader[format],
|
|
47
|
-
}
|
|
47
|
+
},
|
|
48
48
|
})
|
|
49
49
|
}
|
|
50
50
|
}
|
|
@@ -63,16 +63,20 @@ const formatAcceptHeader = {
|
|
|
63
63
|
binrdf: 'application/x-binary-rdf-results-table',
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
66
|
+
function parseResult (format) {
|
|
67
|
+
const simplifyOption = !program.raw
|
|
68
|
+
const { index: indexAttribute } = program
|
|
69
|
+
return results => {
|
|
70
|
+
try {
|
|
71
|
+
if (format === 'json' && simplifyOption) {
|
|
72
|
+
results = simplify.sparqlResults(results, { minimize: true })
|
|
73
|
+
if (indexAttribute) results = indexBy(results, indexAttribute)
|
|
74
|
+
}
|
|
75
|
+
return results
|
|
76
|
+
} catch (err) {
|
|
77
|
+
err.context = { results }
|
|
78
|
+
throw err
|
|
71
79
|
}
|
|
72
|
-
return results
|
|
73
|
-
} catch (err) {
|
|
74
|
-
err.context = { results }
|
|
75
|
-
throw err
|
|
76
80
|
}
|
|
77
81
|
}
|
|
78
82
|
|
package/lib/matching_langs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { values, padEnd } from 'lodash-es'
|
|
2
|
+
import wdLang from 'wikidata-lang'
|
|
3
3
|
|
|
4
|
-
const
|
|
4
|
+
export const findMatchingLang = input => {
|
|
5
5
|
const regex = new RegExp(input, 'i')
|
|
6
6
|
return values(wdLang.byCode)
|
|
7
7
|
.filter(matches(regex))
|
|
@@ -12,11 +12,9 @@ const matches = regex => langData => {
|
|
|
12
12
|
return label.match(regex) || native.match(regex)
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
const
|
|
15
|
+
export const formatLangs = langsData => langsData.map(formatLangData).join('\n')
|
|
16
16
|
|
|
17
17
|
const formatLangData = langData => {
|
|
18
18
|
const { code, label, native, wd } = langData
|
|
19
19
|
return `${padEnd(code, 5)} ${padEnd(wd, 10)} ${padEnd(label, 10)} ${padEnd(native, 10)} `
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
module.exports = { find, format }
|
package/lib/minimize_claims.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
export function minimizeClaimsOrSnaks (snaks) {
|
|
2
2
|
if (!snaks) return
|
|
3
3
|
// Support references nested snaks
|
|
4
4
|
if (snaks.snaks) snaks = snaks.snaks
|
|
@@ -11,11 +11,11 @@ const minimizeSnaks = snaks => {
|
|
|
11
11
|
if (snak.snaktype === 'value') delete snak.snaktype
|
|
12
12
|
if (snak.rank === 'normal') delete snak.rank
|
|
13
13
|
if (snak.qualifiers) {
|
|
14
|
-
|
|
14
|
+
minimizeClaimsOrSnaks(snak.qualifiers)
|
|
15
15
|
if (Object.keys(snak.qualifiers).length === 0) delete snak.qualifiers
|
|
16
16
|
}
|
|
17
17
|
if (snak.references) {
|
|
18
|
-
snak.references.forEach(
|
|
18
|
+
snak.references.forEach(minimizeClaimsOrSnaks)
|
|
19
19
|
if (snak.references.length === 0) delete snak.references
|
|
20
20
|
}
|
|
21
21
|
const keys = Object.keys(snak)
|
|
@@ -25,5 +25,3 @@ const minimizeSnaks = snaks => {
|
|
|
25
25
|
if (snaks[property].length === 1) snaks[property] = snaks[property][0]
|
|
26
26
|
})
|
|
27
27
|
}
|
|
28
|
-
|
|
29
|
-
module.exports = minimizeSnaks
|
package/lib/object_arg_parser.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { red } from '#lib/chalk'
|
|
2
|
+
import { readJsonFile } from '#lib/json'
|
|
3
|
+
import { isFilePathSync, isJsonString, getAbsoluteFilePath, validateTemplateCommand } from '#lib/utils'
|
|
4
|
+
import { parseGuid } from './parse_command_utils.js'
|
|
5
|
+
import program from './program.js'
|
|
6
|
+
import validateFunctionArgs from './validate_function_args.js'
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
const data = getData(args)
|
|
8
|
+
export async function objectArgParser (args) {
|
|
9
|
+
const data = await getData(args)
|
|
10
10
|
if (data == null) return []
|
|
11
11
|
if (data.guid != null) data.guid = parseGuid(data.guid)
|
|
12
12
|
return [ data ]
|
|
@@ -31,7 +31,7 @@ const getData = args => {
|
|
|
31
31
|
|
|
32
32
|
try {
|
|
33
33
|
// Try to parse it as a JSON file
|
|
34
|
-
return
|
|
34
|
+
return readJsonFile(filepath)
|
|
35
35
|
} catch (err1) {
|
|
36
36
|
// Try to parse it as a JS module
|
|
37
37
|
try {
|
|
@@ -50,13 +50,8 @@ const getData = args => {
|
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
-
|
|
54
|
-
const
|
|
55
|
-
return JSON.parse(file)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const getDataFromJsModule = (filepath, args) => {
|
|
59
|
-
const jsModule = require(filepath)
|
|
53
|
+
async function getDataFromJsModule (filepath, args) {
|
|
54
|
+
const { default: jsModule } = await import(filepath)
|
|
60
55
|
if (typeof jsModule === 'function') {
|
|
61
56
|
const inputArgs = args.slice(1)
|
|
62
57
|
validateFunctionArgs(jsModule, inputArgs, jsModule)
|
|
@@ -64,7 +59,7 @@ const getDataFromJsModule = (filepath, args) => {
|
|
|
64
59
|
} else if (jsModule && typeof jsModule.template === 'function') {
|
|
65
60
|
validateTemplateCommand({
|
|
66
61
|
commandName: program._name.replace('wb-', ''),
|
|
67
|
-
validCommands: jsModule.commands
|
|
62
|
+
validCommands: jsModule.commands,
|
|
68
63
|
})
|
|
69
64
|
const inputArgs = args.slice(1)
|
|
70
65
|
validateFunctionArgs(jsModule.template, inputArgs, jsModule)
|
package/lib/open.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const { url: urlOnly, clipboard } = program
|
|
1
|
+
import open from 'open'
|
|
2
|
+
import { copyAndExit } from './copy.js'
|
|
3
|
+
import program from './program.js'
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
export function openUrl (url) {
|
|
6
|
+
const { url: urlOnly, clipboard } = program
|
|
7
7
|
if (urlOnly) {
|
|
8
|
-
clipboard ?
|
|
8
|
+
clipboard ? copyAndExit(url) : console.log(url)
|
|
9
9
|
} else {
|
|
10
|
-
if (clipboard)
|
|
10
|
+
if (clipboard) copyAndExit(url)
|
|
11
11
|
open(url, { url: true })
|
|
12
12
|
}
|
|
13
13
|
}
|