mediawiki-gateway 0.6.2 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +22 -0
  3. data/ChangeLog +16 -0
  4. data/README.md +80 -21
  5. data/Rakefile +28 -34
  6. data/bin/mediawiki-gateway +203 -0
  7. data/lib/media_wiki.rb +4 -9
  8. data/lib/media_wiki/exception.rb +11 -8
  9. data/lib/media_wiki/fake_wiki.rb +636 -0
  10. data/lib/media_wiki/gateway.rb +105 -940
  11. data/lib/media_wiki/gateway/files.rb +173 -0
  12. data/lib/media_wiki/gateway/pages.rb +400 -0
  13. data/lib/media_wiki/gateway/query.rb +98 -0
  14. data/lib/media_wiki/gateway/site.rb +101 -0
  15. data/lib/media_wiki/gateway/users.rb +182 -0
  16. data/lib/media_wiki/utils.rb +47 -13
  17. data/lib/media_wiki/version.rb +27 -0
  18. data/lib/mediawiki-gateway.rb +1 -0
  19. data/spec/{import-test-data.xml → data/import.xml} +0 -0
  20. data/spec/media_wiki/gateway/files_spec.rb +34 -0
  21. data/spec/media_wiki/gateway/pages_spec.rb +390 -0
  22. data/spec/media_wiki/gateway/query_spec.rb +84 -0
  23. data/spec/media_wiki/gateway/site_spec.rb +122 -0
  24. data/spec/media_wiki/gateway/users_spec.rb +171 -0
  25. data/spec/media_wiki/gateway_spec.rb +129 -0
  26. data/spec/{live_gateway_spec.rb → media_wiki/live_gateway_spec.rb} +31 -35
  27. data/spec/{utils_spec.rb → media_wiki/utils_spec.rb} +41 -39
  28. data/spec/spec_helper.rb +17 -16
  29. metadata +77 -135
  30. data/.ruby-version +0 -1
  31. data/.rvmrc +0 -34
  32. data/Gemfile +0 -19
  33. data/Gemfile.lock +0 -77
  34. data/LICENSE +0 -21
  35. data/config/hosts.yml +0 -17
  36. data/lib/media_wiki/config.rb +0 -69
  37. data/mediawiki-gateway.gemspec +0 -113
  38. data/samples/README +0 -18
  39. data/samples/create_page.rb +0 -13
  40. data/samples/delete_batch.rb +0 -14
  41. data/samples/download_batch.rb +0 -15
  42. data/samples/email_user.rb +0 -14
  43. data/samples/export_xml.rb +0 -14
  44. data/samples/get_page.rb +0 -11
  45. data/samples/import_xml.rb +0 -14
  46. data/samples/run_fake_media_wiki.rb +0 -8
  47. data/samples/search_content.rb +0 -12
  48. data/samples/semantic_query.rb +0 -17
  49. data/samples/upload_commons.rb +0 -45
  50. data/samples/upload_file.rb +0 -13
  51. data/spec/fake_media_wiki/api_pages.rb +0 -135
  52. data/spec/fake_media_wiki/app.rb +0 -360
  53. data/spec/fake_media_wiki/query_handling.rb +0 -136
  54. data/spec/gateway_spec.rb +0 -888
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2024bd23d67918a2bf7d38216a70da0bf8f60cb4
4
- data.tar.gz: a0a8252820764951697436d9b957126b4f9f3c83
3
+ metadata.gz: 5150a41bc6e40dcb57657afdc997e131fa2d870a
4
+ data.tar.gz: 2de54f26896f811c5deb030ebbbb763d63b567b1
5
5
  SHA512:
6
- metadata.gz: c84e686ef4362878482dddb8decd812a3886442388fc243c6fe2a35719a1326691ab1d0cd6b0c94c78a07a5004e013761a083877363c34e45035b093f41612f2
7
- data.tar.gz: e3e8f94661cae3ba07edba0eb5b2a6a67d41e4359fc620cecb6b4688f8f6db8014eea597aeae2bb493e6ca445d8f1536a362386a126b88a79c83b752a41ccbda
6
+ metadata.gz: 4dda2f19dc6b2431f052d59b57d1c36990757f1cb1daf4bf3f9a751c78705c13c4e7c778bdd8106c24ea3a9f45f192f336a360f7a260b43b8099969545c1c2a1
7
+ data.tar.gz: bf221978ac17b5fd4712f5738196f438453bbe5854a26c0e0caa1ec863d9396623aed815677e685ed2e56b29b45a7d778869303b3a1b59928e308e540bb05952
data/COPYING ADDED
@@ -0,0 +1,22 @@
1
+ = License for mediawiki-gateway
2
+
3
+ Copyright (c) Jani Patokallio and Lonely Planet 2010-2013
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ # markup: rd
2
+
3
+ = Revision history for mediawiki-gateway
4
+
5
+ == 1.0.0 [unreleased]
6
+
7
+ * <b>Required Ruby version is now 1.9.3 or higher.</b>
8
+ * For better Unicode support, install the +unicode+ or +activesupport+ gem.
9
+ * API methods are grouped into submodules of MediaWiki::Gateway.
10
+ * MediaWiki::Gateway#send_request allows generic API requests.
11
+ * MediaWiki::Gateway::new learned +user_agent+ option.
12
+ * MediaWiki::Gateway::Query#custom_query is now public.
13
+ * MediaWiki::Gateway#wiki_url attribute is now exposed.
14
+ * Added +mediawiki-gateway+ command-line client.
15
+ * Changed or removed some of the dependencies.
16
+ * Housekeeping and internal refactoring.
data/README.md CHANGED
@@ -1,58 +1,117 @@
1
1
  # MediaWiki::Gateway
2
2
 
3
- A Ruby framework for MediaWiki API manipulation. Features out of the box:
3
+ A Ruby framework for [MediaWiki API](http://www.mediawiki.org/wiki/API:Main_page) manipulation.
4
+
5
+ ## Features
4
6
 
5
7
  * Simple, elegant syntax for common operations
6
- * Handles login, edit, move etc tokens for you
8
+ * Handles login, edit, move etc. tokens for you
7
9
  * List, search operations work around API limits to fetch all results
8
10
  * Support for maxlag detection and automated retries on 503
9
11
  * Integrated logging
10
12
  * Tested up to MediaWiki 1.22
11
- * Should work with both Ruby 1.8 and 1.9
13
+ * Should work with Ruby 1.9.3 and higher
14
+
15
+ ## Links
16
+
17
+ RubyGem
18
+ : http://rubygems.org/gems/mediawiki-gateway
19
+
20
+ Documentation
21
+ : http://rubydoc.info/gems/mediawiki-gateway
22
+
23
+ Source
24
+ : https://github.com/jpatokal/mediawiki-gateway
25
+
26
+ CI
27
+ : https://travis-ci.org/jpatokal/mediawiki-gateway
12
28
 
13
- Gem: http://rubygems.org/gems/mediawiki-gateway
29
+ ## Installation
14
30
 
15
- RDoc: http://rubydoc.info/gems/mediawiki-gateway
31
+ To install MediaWiki::Gateway, execute the command:
16
32
 
17
- Git: https://github.com/jpatokal/mediawiki-gateway
33
+ ```shell
34
+ $ gem install mediawiki-gateway
35
+ ```
18
36
 
19
- ## Example
37
+ Or add it to your application's `Gemfile`:
38
+
39
+ ```ruby
40
+ gem 'mediawiki-gateway'
41
+ ```
42
+
43
+ and then execute the command:
44
+
45
+ ```shell
46
+ $ bundle
47
+ ```
48
+
49
+ ## Usage
20
50
 
21
51
  Simple page creation script:
22
52
 
23
- require 'media_wiki'
24
- mw = MediaWiki::Gateway.new('http://my-wiki.example/w/api.php')
25
- mw.login('RubyBot', 'pa$$w0rd')
26
- mw.create('PageTitle', 'Hello world!', :summary => 'My first page')
53
+ ```ruby
54
+ require 'media_wiki'
55
+ mw = MediaWiki::Gateway.new('http://my-wiki.example/w/api.php')
56
+ mw.login('RubyBot', 'pa$$w0rd')
57
+ mw.create('PageTitle', 'Hello world!', summary: 'My first page')
58
+ ```
59
+
60
+ ## Changing the default User-Agent
61
+
62
+ In order to comply with Wikimedia's [User-Agent policy](https://meta.wikimedia.org/wiki/User-Agent_policy), you are strongly advised to provide your own User-Agent header when accessing Wikimedia websites. The User-Agent information should include the name and version of your bot as well as a URL (homepage, repository) and contact e-mail.
63
+
64
+ You can set the default User-Agent globally:
65
+
66
+ ```ruby
67
+ MediaWiki::Gateway.default_user_agent = 'MyCoolTool/1.1 (http://example.com/MyCoolTool/; MyCoolTool@example.com)'
68
+ ```
69
+
70
+ You can also set it on an instance by instance basis, overriding the global default:
71
+
72
+ ```ruby
73
+ mw = MediaWiki::Gateway.new('http://my-wiki.example/w/api.php', user_agent: 'MyCoolTool/1.1 (http://example.com/MyCoolTool/; MyCoolTool@example.com)')
74
+ ```
75
+
76
+ You only need to provide the part that identifies your own bot, an additional part denoting that your bot is based on MediaWiki::Gateway is appended automatically.
27
77
 
28
78
  ## Development environment
29
79
 
30
- To compile and test MediaWiki::Gateway locally, Bundler and Ruby 1.9+ are expected.
80
+ To compile and test MediaWiki::Gateway locally, install its development dependencies:
31
81
 
32
- rvm install 1.9.3-p194
33
- bundle install
82
+ ```shell
83
+ gem install --development mediawiki-gateway
84
+ ```
34
85
 
35
- This will list the available options:
86
+ Then this will list the available options:
36
87
 
37
- bundle exec rake -T
88
+ ```shell
89
+ rake -T
90
+ ```
38
91
 
39
92
  To build and install the gem use:
40
93
 
41
- bundle exec rake install
94
+ ```shell
95
+ rake gem:install
96
+ ```
42
97
 
43
98
  ### Testing against a live MediaWiki instance
44
99
 
45
100
  You need to have [Docker](https://docker.com) and [mediawiki-testwiki](https://rubygems.org/gems/mediawiki-testwiki) installed.
46
101
 
102
+ ## Versioning
103
+
104
+ This project adheres to the [Semantic Versioning Specification](http://semver.org/).
105
+
47
106
  ## Status
48
107
 
49
- This gem is no longer in active development. Pull requests that fix bugs or add new features are more than welcome, but asking for new features is unlikely to make them materialize out of thin air.
108
+ This gem is no longer in active development. Pull requests that fix bugs or add new features are more than welcome, but asking for new features is unlikely to make them materialize out of thin air.
50
109
 
51
110
  ## Credits
52
111
 
53
- Loosely maintained by Jani Patokallio and [Jens Wille](https://github.com/blackwinter). If you'd be seriously interested in joining as an active maintainer, drop us a line!
112
+ Loosely maintained by Jani Patokallio and [Jens Wille](https://github.com/blackwinter). If you'd be seriously interested in joining as an active maintainer, drop us a line!
54
113
 
55
114
  Thanks to:
56
- * John Carney, Mike Williams, Daniel Heath and the rest of the Lonely Planet Atlas team.
57
- * Github users for code contributions, see https://github.com/jpatokal/mediawiki-gateway/pulls
58
115
 
116
+ * John Carney, Mike Williams, Daniel Heath and the rest of the Lonely Planet Atlas team.
117
+ * GitHub users for code contributions, see https://github.com/jpatokal/mediawiki-gateway/pulls
data/Rakefile CHANGED
@@ -1,40 +1,34 @@
1
- require 'thread'
2
- require 'rake'
3
- require "rubygems/package_task"
4
- require 'rdoc/task'
5
- require 'rspec/core/rake_task'
6
- require_relative 'lib/media_wiki'
1
+ require_relative 'lib/media_wiki/version'
7
2
 
8
- task :default => ['spec']
3
+ begin
4
+ require 'hen'
9
5
 
10
- desc 'generate API documentation to doc/index.html'
6
+ Hen.lay! {{
7
+ gem: {
8
+ name: %q{mediawiki-gateway},
9
+ version: MediaWiki::VERSION,
10
+ summary: %q{Connect to the MediaWiki API.},
11
+ description: %q{A Ruby framework for MediaWiki API manipulation.},
12
+ authors: ['Jani Patokallio', 'Jens Wille'],
13
+ email: ['jpatokal@iki.fi', 'jens.wille@gmail.com'],
14
+ license: %q{MIT},
15
+ homepage: :jpatokal,
16
+ dependencies: { 'rest-client' => '~> 1.7' },
11
17
 
12
- RDoc::Task.new do |rd|
13
- rd.rdoc_dir = 'doc'
14
- rd.main = 'README'
15
- rd.rdoc_files.include "README", "lib/media_wiki/**/*\.rb", "script/**/*\.rb"
16
- rd.options << '--inline-source'
17
- rd.options << '--line-numbers'
18
- rd.options << '--all'
19
- end
18
+ development_dependencies: %w[
19
+ equivalent-xml
20
+ nokogiri
21
+ sham_rack
22
+ sinatra
23
+ ],
20
24
 
21
- desc "Run all specs"
22
- RSpec::Core::RakeTask.new('spec') do |t|
23
- t.pattern = FileList['spec/**/*.rb']
24
- end
25
+ required_ruby_version: '>= 1.9.3'
26
+ },
25
27
 
26
-
27
- begin
28
- require 'jeweler'
29
- Jeweler::Tasks.new do |gemspec|
30
- gemspec.name = "mediawiki-gateway"
31
- gemspec.summary = "Connect to the mediawiki API"
32
- gemspec.description = ""
33
- gemspec.email = "jpatokal@iki.fi"
34
- gemspec.homepage = "http://github.com/jpatokal/mediawiki-gateway"
35
- gemspec.authors = ["Jani Patokallio"]
36
- gemspec.version = MediaWiki::VERSION
37
- end
38
- rescue LoadError
39
- puts "Jeweler not available. Install it with: gem install jeweler"
28
+ rdoc: {
29
+ extra_files: %w[README.md]
30
+ }
31
+ }}
32
+ rescue LoadError => err
33
+ warn "Please install the `hen' gem. (#{err})"
40
34
  end
@@ -0,0 +1,203 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'media_wiki'
4
+
5
+ require 'optparse'
6
+ require 'yaml'
7
+
8
+ def mw(options)
9
+ MediaWiki::Gateway.new(options[:url], loglevel: options[:loglevel]).tap { |mw|
10
+ mw.login(options[:user], options[:pw]) if options[:user]
11
+ }
12
+ end
13
+
14
+ commands = {
15
+ create_page: ['Update a MediaWiki article\'s content', lambda { |options|
16
+ abort 'Article name is mandatory.' unless article = ARGV.shift
17
+
18
+ puts mw(options).create(article, ARGF.read, overwrite: true, summary: options[:summary] || 'Automated edit via MediaWiki::Gateway')
19
+ }],
20
+ delete_batch: ['Delete articles in batch', lambda { |options|
21
+ abort 'Article prefix is mandatory.' unless key = ARGV.shift
22
+
23
+ mw = mw(options)
24
+
25
+ mw.list(key).each { |title|
26
+ puts "Deleting #{title}..."
27
+ mw.delete(title)
28
+ }
29
+ }],
30
+ download_batch: ['Download images in batch', lambda { |options|
31
+ mw = mw(options)
32
+
33
+ ARGF.each { |image|
34
+ image.strip!
35
+ File.write(image, mw.download(image)) unless File.exist?(image)
36
+ }
37
+ }],
38
+ email_user: ['Send e-mail to a registered user', lambda { |options|
39
+ user, subject = ARGV.shift, ARGV.shift
40
+ abort 'User and subject are mandatory.' unless user && subject
41
+
42
+ mw(options).email_user(user, subject, ARGF.read)
43
+ }],
44
+ export_xml: ['Export MediaWiki pages as XML', lambda { |options|
45
+ abort 'User is mandatory.' unless options[:user]
46
+
47
+ puts mw(options).export(ARGV)
48
+ }],
49
+ get_page: ['Fetch a page\'s current contents in Wiki markup', lambda { |options|
50
+ abort 'Article name is mandatory.' unless article = ARGV.shift
51
+
52
+ puts mw(options).get(article)
53
+ }],
54
+ import_xml: ['Import a MediaWiki XML dump', lambda { |options|
55
+ abort 'File name is mandatory.' unless file = ARGV.shift
56
+
57
+ mw(options).import(file)
58
+ }],
59
+ fake_media_wiki: ['Run a live FakeWiki instance instead of just shamracking it', lambda {
60
+ require 'media_wiki/fake_wiki'
61
+
62
+ MediaWiki::FakeWiki::App.run!(host: 'localhost', port: 9090)
63
+ }],
64
+ search_content: ['Search page contents in a Wiki', lambda { |options|
65
+ abort 'Search key is mandatory.' unless key = ARGV.shift
66
+
67
+ puts mw(options).search(key, nil, 50)
68
+ }],
69
+ semantic_query: ['Query Semantic MediaWiki data', lambda { |options|
70
+ puts mw(options).semantic_query(ARGV.shift, ARGV)
71
+ }],
72
+ upload_commons: ['Upload files to Mediawiki Commons (interactive)', lambda { |options|
73
+ abort 'File name is mandatory.' unless file = ARGV.shift
74
+
75
+ mw = mw(options)
76
+
77
+ puts 'Description of file:'
78
+ desc = STDIN.gets.chomp
79
+
80
+ puts 'Date of file:'
81
+ date = STDIN.gets.chomp
82
+
83
+ puts 'Target filename (leave blank to use existing name):'
84
+ target = STDIN.gets.chomp
85
+ target = options[:target] if target.empty?
86
+
87
+ puts 'Categories, separated by commas:'
88
+ cats = STDIN.gets.chomp.split(',')
89
+
90
+ puts "Uploading #{file} to #{target || file}..."
91
+ mw.upload(file, target: target, description: <<-EOT, summary: options[:summary] || 'Uploaded by MediaWiki::Gateway')
92
+ == Summary ==
93
+ {{Information
94
+ |Description={{en|1=#{desc}}}
95
+ |Source={{own}}
96
+ |Author=[[User:#{options[:user]}|#{options[:user]}]]
97
+ |Date=#{date}
98
+ |Permission=
99
+ |other_versions=
100
+ }}
101
+
102
+ == Licensing ==
103
+ {{self|cc-by-sa-3.0|GFDL}}
104
+
105
+ #{cats.map { |cat| "[[Category:#{cat}]]" }.join("\n")}
106
+ EOT
107
+ }],
108
+ upload_file: ['Upload a file to a Wiki', lambda { |options|
109
+ abort 'File name is mandatory.' unless file = ARGV.shift
110
+
111
+ mw(options).upload(file, target: options[:target], description: options[:description], summary: options[:summary] || 'Uploaded by MediaWiki::Gateway')
112
+ }]
113
+ }
114
+
115
+ def commands.list
116
+ l = keys.map(&:length).max
117
+ map { |c, (d, _)| " #{c.to_s.ljust(l)} - #{d}" }.unshift('Available commands:')
118
+ end
119
+
120
+ usage = <<-EOT
121
+ Usage: #{$0} <command> [options]
122
+ #{$0} [-h|--help] [--version]
123
+ EOT
124
+
125
+ options = { loglevel: Logger::WARN }
126
+
127
+ OptionParser.new { |opts|
128
+ opts.banner = usage
129
+
130
+ opts.separator ''
131
+
132
+ opts.on('-w', '--url URL', 'MediaWiki API URL') { |url|
133
+ options[:url] = url
134
+ }
135
+
136
+ opts.on('-u', '--username USERNAME', 'Username for login') { |user|
137
+ options[:user] = user
138
+ }
139
+
140
+ opts.on('-p', '--password PASSWORD', 'Password for login') { |pw|
141
+ options[:pw] = pw
142
+ }
143
+
144
+ opts.separator ''
145
+
146
+ opts.on('-H', '--host HOST', 'Use preconfigured HOST in hosts.yml') { |host_id|
147
+ host = YAML.load_file('hosts.yml').fetch(host_id) {
148
+ abort "Host #{host_id} not found in hosts.yml"
149
+ }
150
+
151
+ options[:url] = host['url']
152
+ options[:pw] = host['pw']
153
+ options[:user] = host['user']
154
+ }
155
+
156
+ opts.separator ''
157
+ opts.separator 'Edit options:'
158
+
159
+ opts.on('-s', '--summary SUMMARY', 'Edit summary for this change') { |summary|
160
+ options[:summary] = summary
161
+ }
162
+
163
+ opts.separator ''
164
+ opts.separator 'Upload options:'
165
+
166
+ opts.on('-d', '--description DESCRIPTION', 'Description of file to upload') { |description|
167
+ options[:description] = description
168
+ }
169
+
170
+ opts.on('-t', '--target-file TARGET-FILE', 'Target file name to upload to') { |target|
171
+ options[:target] = target
172
+ }
173
+
174
+ opts.separator ''
175
+ opts.separator 'Generic options:'
176
+
177
+ opts.on('-D', '--debug', 'Set log level to debug') {
178
+ options[:loglevel] = Logger::DEBUG
179
+ }
180
+
181
+ opts.on('-h', '--help', 'Print this help message and exit') {
182
+ puts opts
183
+ exit
184
+ }
185
+
186
+ opts.on('--version', 'Print program version and exit') {
187
+ puts "#{File.basename($0)} v#{MediaWiki::VERSION}"
188
+ exit
189
+ }
190
+
191
+ opts.separator ''
192
+ opts.separator commands.list
193
+ }.parse!
194
+
195
+ abort usage if ARGV.empty?
196
+
197
+ command = commands.fetch(key = ARGV.shift.tr('-', '_').to_sym) {
198
+ abort "Invalid command: #{key}\n\n#{commands.list.join("\n")}"
199
+ }.last
200
+
201
+ command.arity.zero? ? command.call :
202
+ options[:url] ? command[options] :
203
+ abort('URL (-u) or valid host (-H) is mandatory.')