wikibase-cli 15.16.8 → 16.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-search CHANGED
@@ -5,6 +5,7 @@ program
5
5
  .option('-p, --properties <properties>', 'request additional properties (separated by a comma) (implies verbose mode)')
6
6
  .option('-t, --type <type>', 'customize type: i|item, p|property, l|lexeme, f|form, s|sense (Default: item)')
7
7
  .option('-n, --limit <num>', 'set a custom limit (defaults to 10)')
8
+ .option('--cirrus', 'use Cirrus search')
8
9
  .process('search')
9
10
 
10
11
  // Accept several words without requiring those to be joined by quotes
@@ -12,7 +13,7 @@ const search = program.args.join(' ')
12
13
 
13
14
  if (!(search && search.length > 0)) program.helpAndExit(0)
14
15
 
15
- let { lang: language, json, verbose, properties, limit, type } = program
16
+ let { lang: language, json, verbose, properties, limit, type, cirrus } = program
16
17
  limit = limit || 20
17
18
 
18
19
  if (properties) program.verbose = verbose = true
@@ -25,40 +26,63 @@ if (!valide.lang(language)) {
25
26
  }
26
27
 
27
28
  require('../lib/exit_on_missing').instance(program.instance)
28
- const { searchEntities } = require('../lib/wbk')(program)
29
+ const { searchEntities, cirrusSearchPages } = require('../lib/wbk')(program)
29
30
  const { get } = require('../lib/request')
30
31
  const parseEntityType = require('../lib/parse_entity_type')
31
32
  const { padEnd } = require('lodash')
32
33
 
33
34
  type = parseEntityType(type)
34
35
 
35
- const url = searchEntities({
36
- search,
37
- language,
38
- // Fetch more in case we need to filter-out some results
39
- limit: limit + 10,
40
- type
41
- })
36
+ let url
37
+ if (cirrus) {
38
+ url = cirrusSearchPages({
39
+ search,
40
+ srprop: 'snippet|titlesnippet|sectiontitle',
41
+ limit,
42
+ })
43
+ } else {
44
+ url = searchEntities({
45
+ search,
46
+ language,
47
+ // Fetch more in case we need to filter-out some results
48
+ limit: limit + 10,
49
+ type
50
+ })
51
+ }
52
+
53
+ const dropMarkup = str => {
54
+ return str
55
+ .replace(/<span class="searchmatch">/g, '')
56
+ .replace(/<\/span>/g, '')
57
+ }
42
58
 
43
59
  get(url)
44
60
  .then(body => {
45
- const { error, search } = body
61
+ const { error, search, query } = body
46
62
 
47
63
  if (error) {
48
64
  console.error('API error response: ' + error.info)
49
65
  process.exit(1)
50
66
  }
51
67
 
52
- let results = search
68
+ let results = search || query.search
53
69
 
54
70
  results = results.slice(0, limit)
55
71
 
72
+ if (cirrus) {
73
+ results.forEach(result => {
74
+ result.id = result.title
75
+ result.label = dropMarkup(result.titlesnippet)
76
+ result.description = dropMarkup(result.snippet.split('\n')[0])
77
+ })
78
+ }
79
+
56
80
  if (json) {
57
81
  console.log(JSON.stringify(results, null, 2))
58
82
  return
59
83
  }
60
84
 
61
- const ids = results.map(result => result.id)
85
+ const ids = results.map(result => result.id || result.title)
62
86
 
63
87
  if (ids.length === 0) {
64
88
  console.error('no result')
@@ -74,9 +98,22 @@ get(url)
74
98
  // in the summary verbose format, only in the normal, short, summary format
75
99
  program.verbose = false
76
100
  entityDataParser({ ids, parser: summaryParser })
101
+ } else if (cirrus) {
102
+ results
103
+ .forEach(result => {
104
+ let { id, label, description } = result
105
+ // Known case: when search with haswbstatement
106
+ if (!label) {
107
+ label = description
108
+ description = ''
109
+ }
110
+ let text = padEnd(id, 11) + label
111
+ if (description && description.length > 0) text += ' ' + grey(description)
112
+ console.log(text)
113
+ })
77
114
  } else {
78
115
  results
79
- .forEach((result, index) => {
116
+ .forEach(result => {
80
117
  const { id, label, description } = result
81
118
  let text = padEnd(id, 11) + label
82
119
  if (description && description.length > 0) text += ' ' + grey(description)
@@ -5,7 +5,7 @@ const errors_ = require('../lib/errors')
5
5
  module.exports = program => {
6
6
  exitOnMissing.instance(program.instance)
7
7
  const { getEntities } = require('../lib/wbk')(program)
8
- return (id, lang, sitelinkSuffix) => {
8
+ return async (id, lang, sitelinkSuffix) => {
9
9
  if (id[0] === 'P') {
10
10
  console.error("Wikibase properties don't have sitelinks")
11
11
  process.exit(1)
@@ -13,18 +13,30 @@ module.exports = program => {
13
13
 
14
14
  const url = getEntities({ ids: id, props: [ 'sitelinks' ] })
15
15
 
16
- return get(url)
17
- .then(res => {
18
- const entity = res.entities[id]
19
- if (!entity || entity.missing === '') throw errors_.new('entity not found', { id, lang, sitelinkSuffix })
20
- const sitelink = entity.sitelinks[`${lang}${sitelinkSuffix}`]
21
- if (!sitelink) throw errors_.new('sitelink not found', { id, lang, sitelinkSuffix })
22
- const domain = sitelinkDomain[sitelinkSuffix]
23
- return `https://${lang}.${domain}/wiki/${sitelink.title}`
24
- })
16
+ const { entities } = await get(url)
17
+ const entity = entities[id]
18
+ if (!entity || entity.missing === '') throw errors_.new('entity not found', { id, lang, sitelinkSuffix })
19
+ const sitelink = entity.sitelinks[`${lang}${sitelinkSuffix}`]
20
+ if (!sitelink) throw errors_.new('sitelink not found', { id, lang, sitelinkSuffix })
21
+ const domain = sitelinkDomain[sitelinkSuffix]
22
+ const formattedTitle = formatTitle(sitelink.title)
23
+ return `https://${lang}.${domain}/wiki/${formattedTitle}`
25
24
  }
26
25
  }
27
26
 
28
27
  const sitelinkDomain = {
29
28
  wiki: 'wikipedia.org'
30
29
  }
30
+
31
+ const formatTitle = title => {
32
+ title = title.replace(/\s/g, '_')
33
+ return fixedEncodeURIComponent(title)
34
+ }
35
+
36
+ // encodeURIComponent ignores !, ', (, ), and *
37
+ // cf https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent#Description
38
+ const fixedEncodeURIComponent = str => {
39
+ return encodeURIComponent(str).replace(/[!'()*]/g, encodeCharacter)
40
+ }
41
+
42
+ const encodeCharacter = c => '%' + c.charCodeAt(0).toString(16)
@@ -15,9 +15,10 @@ module.exports = {
15
15
  { args: 'Harry Potter --limit 25', comment: 'displays up to 25 results matching "Harry Potter"' },
16
16
  { args: 'Harry Potter --verbose', comment: 'display rich results (aka summaries)' },
17
17
  { args: 'Harry Potter --properties P577,P935', comment: 'request additional properties (separated by a comma) to be added to the results summaries' },
18
- { args: 'date --type property', comment: "Search properties (but `wb props` might be doing a better job)" },
19
- { args: 'date --type lexeme', comment: "Search lexemes" },
20
- { args: 'code --type form', comment: "Search forms" },
21
- { args: 'test --type sense', comment: "Searching senses doesn't seem to work currently (2020-04-17)" }
18
+ { args: 'date --type property', comment: 'Search properties (but `wb props` might be doing a better job)' },
19
+ { args: 'date --type lexeme', comment: 'Search lexemes' },
20
+ { args: 'code --type form', comment: 'Search forms' },
21
+ { args: 'test --type sense', comment: "Searching senses doesn't seem to work currently (2020-04-17)" },
22
+ { args: '--cirrus "porte haswbstatement:P31=Q5"', comment: 'Use Cirrus search to find humans (Q5) matching "porte"' },
22
23
  ]
23
24
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wikibase-cli",
3
- "version": "15.16.8",
3
+ "version": "16.0.0",
4
4
  "description": "A command-line interface to Wikibase",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -50,8 +50,8 @@
50
50
  "shell-quote": "^1.7.3",
51
51
  "split": "^1.0.1",
52
52
  "through": "^2.3.8",
53
- "wikibase-edit": "^4.16.1",
54
- "wikibase-sdk": "^7.14.4",
53
+ "wikibase-edit": "^5.0.0",
54
+ "wikibase-sdk": "^8.0.0",
55
55
  "wikidata-lang": "^2.0.11"
56
56
  },
57
57
  "devDependencies": {
@@ -67,6 +67,6 @@
67
67
  "should": "^13.2.3"
68
68
  },
69
69
  "engines": {
70
- "node": ">= 7.6.0"
70
+ "node": ">= 10.0.0"
71
71
  }
72
72
  }