mediawiki-gateway 0.5.0 → 0.5.1
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.
- data/README +2 -2
- data/lib/media_wiki.rb +1 -1
- data/lib/media_wiki/gateway.rb +101 -6
- data/script/email_user.rb +14 -0
- data/script/semantic_query.rb +17 -0
- metadata +7 -5
data/README
CHANGED
@@ -7,7 +7,7 @@ A Ruby framework for MediaWiki API manipulation. Features out of the box:
|
|
7
7
|
* List, search operations work around API limits to fetch all results
|
8
8
|
* Support for maxlag detection and automated retries on 503
|
9
9
|
* Integrated logging
|
10
|
-
* Tested up to MediaWiki 1.
|
10
|
+
* Tested up to MediaWiki 1.19
|
11
11
|
|
12
12
|
Gem: http://rubygems.org/gems/mediawiki-gateway
|
13
13
|
RDoc: http://rubydoc.info/gems/mediawiki-gateway
|
@@ -28,5 +28,5 @@ Maintained by Jani Patokallio.
|
|
28
28
|
|
29
29
|
Thanks to:
|
30
30
|
* John Carney, Mike Williams, Daniel Heath and the rest of the Lonely Planet Atlas team.
|
31
|
-
* Github users
|
31
|
+
* Github users for code contributions, see https://github.com/jpatokal/mediawiki-gateway/pulls
|
32
32
|
|
data/lib/media_wiki.rb
CHANGED
data/lib/media_wiki/gateway.rb
CHANGED
@@ -38,7 +38,7 @@ module MediaWiki
|
|
38
38
|
@wiki_url = url
|
39
39
|
@log = Logger.new(@options[:logdevice])
|
40
40
|
@log.level = @options[:loglevel]
|
41
|
-
@headers = { "User-Agent" => "MediaWiki::Gateway/#{MediaWiki::VERSION}" }
|
41
|
+
@headers = { "User-Agent" => "MediaWiki::Gateway/#{MediaWiki::VERSION}", "Accept-Encoding" => "gzip" }
|
42
42
|
@cookies = {}
|
43
43
|
end
|
44
44
|
|
@@ -350,6 +350,51 @@ module MediaWiki
|
|
350
350
|
titles
|
351
351
|
end
|
352
352
|
|
353
|
+
# Get a list of users
|
354
|
+
#
|
355
|
+
# [options] Optional hash of options, eg. { 'augroup' => 'sysop' }. See http://www.mediawiki.org/wiki/API:Allusers
|
356
|
+
#
|
357
|
+
# Returns array of user names (empty if no matches)
|
358
|
+
def users(options = {})
|
359
|
+
names = []
|
360
|
+
aufrom = nil
|
361
|
+
begin
|
362
|
+
form_data = options.merge(
|
363
|
+
{'action' => 'query',
|
364
|
+
'list' => 'allusers',
|
365
|
+
'aufrom' => aufrom,
|
366
|
+
'aulimit' => @options[:limit]})
|
367
|
+
res, aufrom = make_api_request(form_data, '//query-continue/allusers/@aufrom')
|
368
|
+
names += REXML::XPath.match(res, "//u").map { |x| x.attributes["name"] }
|
369
|
+
end while aufrom
|
370
|
+
names
|
371
|
+
end
|
372
|
+
|
373
|
+
# Get user contributions
|
374
|
+
#
|
375
|
+
# user: The user name
|
376
|
+
# count: Maximum number of contributions to retreive, or nil for all
|
377
|
+
# [options] Optional hash of options, eg. { 'ucnamespace' => 4 }. See http://www.mediawiki.org/wiki/API:Usercontribs
|
378
|
+
#
|
379
|
+
# Returns array of hashes containing the "item" attributes defined here: http://www.mediawiki.org/wiki/API:Usercontribs
|
380
|
+
def contributions(user, count = nil, options = {})
|
381
|
+
result = []
|
382
|
+
ucstart = nil
|
383
|
+
begin
|
384
|
+
limit = [count, @options[:limit]].compact.min
|
385
|
+
form_data = options.merge(
|
386
|
+
{'action' => 'query',
|
387
|
+
'list' => 'usercontribs',
|
388
|
+
'ucuser' => user,
|
389
|
+
'ucstart' => ucstart,
|
390
|
+
'uclimit' => limit})
|
391
|
+
res, ucstart = make_api_request(form_data, '//query-continue/usercontribs/@ucstart')
|
392
|
+
result += REXML::XPath.match(res, "//item").map { |x| x.attributes.inject({}) { |hash, data| hash[data.first] = data.last; hash } }
|
393
|
+
break if count and result.size >= count
|
394
|
+
end while ucstart
|
395
|
+
result
|
396
|
+
end
|
397
|
+
|
353
398
|
# Upload a file, or get the status of pending uploads. Several
|
354
399
|
# methods are available:
|
355
400
|
#
|
@@ -550,6 +595,19 @@ module MediaWiki
|
|
550
595
|
end
|
551
596
|
end
|
552
597
|
|
598
|
+
# Sends e-mail to a user
|
599
|
+
#
|
600
|
+
# [user] Username to send mail to (name only: eg. 'Bob', not 'User:Bob')
|
601
|
+
# [subject] Subject of message
|
602
|
+
# [content] Content of message
|
603
|
+
#
|
604
|
+
# Will raise a 'noemail' APIError if the target user does not have a confirmed email address, see http://www.mediawiki.org/wiki/API:E-mail for details.
|
605
|
+
def email_user(user, subject, text)
|
606
|
+
form_data = { 'action' => 'emailuser', 'target' => user, 'subject' => subject, 'text' => text, 'token' => get_token('email', "User:" + user) }
|
607
|
+
res, dummy = make_api_request(form_data)
|
608
|
+
res.elements['emailuser'].attributes['result'] == 'Success'
|
609
|
+
end
|
610
|
+
|
553
611
|
# Execute Semantic Mediawiki query
|
554
612
|
#
|
555
613
|
# [query] Semantic Mediawiki query
|
@@ -557,10 +615,21 @@ module MediaWiki
|
|
557
615
|
#
|
558
616
|
# Returns result as an HTML string
|
559
617
|
def semantic_query(query, params = [])
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
618
|
+
if extensions.include? 'Semantic MediaWiki'
|
619
|
+
smw_version = extensions['Semantic MediaWiki'].to_f
|
620
|
+
if smw_version >= 1.7
|
621
|
+
form_data = { 'action' => 'ask', 'query' => "#{query}|#{params.join('|')}"}
|
622
|
+
xml, dummy = make_api_request(form_data)
|
623
|
+
return xml
|
624
|
+
else
|
625
|
+
params << "format=list"
|
626
|
+
form_data = { 'action' => 'parse', 'prop' => 'text', 'text' => "{{#ask:#{query}|#{params.join('|')}}}" }
|
627
|
+
xml, dummy = make_api_request(form_data)
|
628
|
+
return xml.elements["parse/text"].text
|
629
|
+
end
|
630
|
+
else
|
631
|
+
raise MediaWiki::Exception.new "Semantic MediaWiki extension not installed."
|
632
|
+
end
|
564
633
|
end
|
565
634
|
|
566
635
|
# Set groups for a user
|
@@ -588,7 +657,7 @@ module MediaWiki
|
|
588
657
|
|
589
658
|
private
|
590
659
|
|
591
|
-
# Fetch token (type 'delete', 'edit', 'import', 'move', 'protect')
|
660
|
+
# Fetch token (type 'delete', 'edit', 'email', 'import', 'move', 'protect')
|
592
661
|
def get_token(type, page_titles)
|
593
662
|
form_data = {'action' => 'query', 'prop' => 'info', 'intoken' => type, 'titles' => page_titles}
|
594
663
|
res, dummy = make_api_request(form_data)
|
@@ -645,6 +714,32 @@ module MediaWiki
|
|
645
714
|
res
|
646
715
|
end
|
647
716
|
|
717
|
+
|
718
|
+
# Make a custom query
|
719
|
+
#
|
720
|
+
# [options] query options
|
721
|
+
#
|
722
|
+
# Returns the REXML::Element object as result
|
723
|
+
#
|
724
|
+
# Example:
|
725
|
+
# def creation_time(pagename)
|
726
|
+
# res = bot.custom_query(:prop => :revisions,
|
727
|
+
# :titles => pagename,
|
728
|
+
# :rvprop => :timestamp,
|
729
|
+
# :rvdir => :newer,
|
730
|
+
# :rvlimit => 1)
|
731
|
+
# timestr = res.get_elements('*/*/*/rev')[0].attribute('timestamp').to_s
|
732
|
+
# time.parse(timestr)
|
733
|
+
# end
|
734
|
+
#
|
735
|
+
def custom_query(options)
|
736
|
+
form_data = {}
|
737
|
+
options.each {|k,v| form_data[k.to_s] = v.to_s }
|
738
|
+
form_data['action'] = 'query'
|
739
|
+
make_api_request(form_data).first.elements['query']
|
740
|
+
end
|
741
|
+
|
742
|
+
|
648
743
|
# Make generic request to API
|
649
744
|
#
|
650
745
|
# [form_data] hash or string of attributes to post
|
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Sample script for sending e-mail to a registered user
|
4
|
+
#
|
5
|
+
require './lib/media_wiki'
|
6
|
+
|
7
|
+
config = MediaWiki::Config.new ARGV
|
8
|
+
user, subject = ARGV
|
9
|
+
config.abort("Syntax: email_user.rb [username] [subject] <content") unless user and subject
|
10
|
+
|
11
|
+
mw = MediaWiki::Gateway.new(config.url, { :loglevel => Logger::DEBUG } )
|
12
|
+
mw.login(config.user, config.pw)
|
13
|
+
content = STDIN.read
|
14
|
+
mw.email_user(user, subject, content)
|
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Sample script for querying Semantic MediaWiki data
|
4
|
+
#
|
5
|
+
|
6
|
+
require './lib/media_wiki'
|
7
|
+
|
8
|
+
mw = MediaWiki::Gateway.new(ARGV[0])
|
9
|
+
|
10
|
+
params = []
|
11
|
+
i = 2
|
12
|
+
until i == ARGV.length
|
13
|
+
params << ARGV[i]
|
14
|
+
i += 1
|
15
|
+
end
|
16
|
+
|
17
|
+
puts mw.semantic_query(ARGV[1], params)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mediawiki-gateway
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
-
|
10
|
-
version: 0.5.
|
9
|
+
- 1
|
10
|
+
version: 0.5.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jani Patokallio
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
18
|
+
date: 2012-08-08 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rest-client
|
@@ -182,11 +182,13 @@ files:
|
|
182
182
|
- script/create_page.rb
|
183
183
|
- script/delete_batch.rb
|
184
184
|
- script/download_batch.rb
|
185
|
+
- script/email_user.rb
|
185
186
|
- script/export_xml.rb
|
186
187
|
- script/get_page.rb
|
187
188
|
- script/import_xml.rb
|
188
189
|
- script/run_fake_media_wiki.rb
|
189
190
|
- script/search_content.rb
|
191
|
+
- script/semantic_query.rb
|
190
192
|
- script/upload_commons.rb
|
191
193
|
- script/upload_file.rb
|
192
194
|
- spec/fake_media_wiki/api_pages.rb
|
@@ -225,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
225
227
|
requirements: []
|
226
228
|
|
227
229
|
rubyforge_project:
|
228
|
-
rubygems_version: 1.8.
|
230
|
+
rubygems_version: 1.8.10
|
229
231
|
signing_key:
|
230
232
|
specification_version: 3
|
231
233
|
summary: Connect to the mediawiki API
|