Code Monkey home page Code Monkey logo

wikidatum's Introduction

Wikidatum

This gem supports making requests to the new Wikidata/Wikibase REST API.

The Wikimedia Docs on the Wikibase REST API data format differences are also very useful for interacting with/contributing to this gem.

The gem is currently in development. It's ready to be used, but you should be careful when making edits with it to ensure it's working correctly. It's missing some key features, namely authentication support, but the core of the library works.

I reserve the right to make breaking changes while the library is still in the 0.x release series, although I'll avoid them unless I believe them to be significantly better for the library's usability/maintainability (or are necessary due to breaking changes in the REST API itself). If they do happen, I'll make them clear in the Changelog.

Installation

Add this line to your application's Gemfile:

gem 'wikidatum'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install wikidatum

Usage

You can view the YARD docs on GitHub Pages here. Generally, you'll want to look at the docs for {Wikidatum::Client} to get started.

Currently, the gem is able to hit a few of the basic endpoints, and currently has no way to provide authentication. The additional features will be added later.

require 'wikidatum'

wikidatum_client = Wikidatum::Client.new(
  user_agent: 'REPLACE ME WITH THE NAME OF YOUR BOT!',
  wikibase_url: 'https://www.wikidata.org',
  # NOTE: To edit as a bot, you need to authenticate as a user with the Bot
  # flag. If you don't have that flag on your Wikibase User, you'll get a
  # 403 error.
  bot: true
)

# Get an item from the Wikibase instance.
item = wikidatum_client.item(id: 'Q2') #=> Wikidatum::Item

# Get the statements from the item.
item.statements #=> Array<Wikidatum::Statement>

# Get the statments for property P123 on the item.
item.statements(properties: ['P123']) #=> Array<Wikidatum::Statement>

# Get all the labels for the item.
item.labels #=> Array<Wikidatum::Term>

# Get the English label for the item.
item.label(lang: :en) #=> Wikidatum::Term

# Get the actual value for the label.
item.label(lang: :en).value #=> "Earth"

# Get the values for all English aliases on this item.
item.aliases(langs: [:en]).map(&:value) #=> ["Planet Earth", "Pale Blue Dot"]

# Get all labels for a given item.
wikidatum_client.labels(id: 'Q2') #=> Array<Wikidatum::Term>

# Get a specific statement from its ID.
statement_id = 'Q123$4543523c-1d1d-1111-1e1e-11b11111b1f1'
statement = wikidatum_client.statement(id: statement_id) #=> Wikidatum::Statement

# Add a statement to Q193581 for P577 (publication date) that has a time value of November 16, 2004.
wikidatum_client.add_statement(
  id: 'Q193581',
  property: 'P577',
  value: Wikidatum::DataType::Time.new(
    time: '+2004-11-16T00:00:00Z',
    precision: 11,
    calendar_model: 'https://www.wikidata.org/entity/Q12138'
  )
)

# Delete a statement and include an edit summary.
wikidatum_client.delete_statement(
  id: 'Q123$4543523c-1d1d-1111-1e1e-11b11111b1f1',
  comment: 'Deleting this statement because it is inaccurate.'
)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/connorshea/wikidatum.

License

The gem is available as open source under the terms of the MIT License.

wikidatum's People

Contributors

connorshea avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

wikidatum's Issues

Allow automatically generating/assigning an edit group when initializing the Client

EditGroups are a useful tool for managing mass edits: https://editgroups.toolforge.org/

They allow you to define an identifier that can be used to group a series of edits, e.g. which is good for being able to view and even mass-revert instances of imports.

See also https://www.wikidata.org/wiki/Wikidata:Edit_groups

All that's necessary is generating a random valid EditGroups ID and then adding the editgroups link in the edit summary for every edit in the given script? See https://www.wikidata.org/wiki/Wikidata:Edit_groups/Adding_a_tool

We could allow passing a specific EditGroup ID that'd be used, or just a boolean to enable auto-generating an EditGroups ID for the given instance of Wikidatum::Client.

Handle redirected items

e.g. Q116169761 is a redirected item, and right now the gem just fails.

irb(main):015:0> wikidatum_client.item(id: 'Q116169761')
Something went wrong with this request!
Status Code: 308
""
/Users/connorshea/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/json-2.6.3/lib/json/common.rb:216:in `parse': unexpected token at '' (JSON::ParserError)
	from /Users/connorshea/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/json-2.6.3/lib/json/common.rb:216:in `parse'
	from /Users/connorshea/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/wikidatum-0.3.3/lib/wikidatum/client.rb:416:in `get_request'
	from /Users/connorshea/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/wikidatum-0.3.3/lib/wikidatum/client.rb:108:in `item'
	from (irb):15:in `<main>'
	from /Users/connorshea/.rbenv/versions/3.1.0/lib/ruby/gems/3.1.0/gems/irb-1.7.4/exe/irb:9:in `<top (required)>'
	from /Users/connorshea/.rbenv/versions/3.1.0/bin/irb:25:in `load'
	from /Users/connorshea/.rbenv/versions/3.1.0/bin/irb:25:in `<main>'

Add a parameter on Client.new to prevent IP edits

Right now, it's very easy to make anonymous edits which expose your IP address. This isn't ideal for the user if they forgot to authenticate their request (which they can't actually do yet, but y'know...).

By default, this parameter should prevent IP edits and you should have to opt-in to them. Otherwise, when it tries to make a non-GET request it should error out if auth isn't provided.

client = Wikidatum::Client.new(
  user_agent: 'Foobar Bot',
  wikibase_url: 'https://wikidata.beta.wmflabs.org',
  bot: true,
  allow_ip_edits: true # false by default
)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.