mediawiki-gateway 0.6.2 → 1.0.0.rc1
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.
- checksums.yaml +4 -4
- data/COPYING +22 -0
- data/ChangeLog +16 -0
- data/README.md +80 -21
- data/Rakefile +28 -34
- data/bin/mediawiki-gateway +203 -0
- data/lib/media_wiki.rb +4 -9
- data/lib/media_wiki/exception.rb +11 -8
- data/lib/media_wiki/fake_wiki.rb +636 -0
- data/lib/media_wiki/gateway.rb +105 -940
- data/lib/media_wiki/gateway/files.rb +173 -0
- data/lib/media_wiki/gateway/pages.rb +400 -0
- data/lib/media_wiki/gateway/query.rb +98 -0
- data/lib/media_wiki/gateway/site.rb +101 -0
- data/lib/media_wiki/gateway/users.rb +182 -0
- data/lib/media_wiki/utils.rb +47 -13
- data/lib/media_wiki/version.rb +27 -0
- data/lib/mediawiki-gateway.rb +1 -0
- data/spec/{import-test-data.xml → data/import.xml} +0 -0
- data/spec/media_wiki/gateway/files_spec.rb +34 -0
- data/spec/media_wiki/gateway/pages_spec.rb +390 -0
- data/spec/media_wiki/gateway/query_spec.rb +84 -0
- data/spec/media_wiki/gateway/site_spec.rb +122 -0
- data/spec/media_wiki/gateway/users_spec.rb +171 -0
- data/spec/media_wiki/gateway_spec.rb +129 -0
- data/spec/{live_gateway_spec.rb → media_wiki/live_gateway_spec.rb} +31 -35
- data/spec/{utils_spec.rb → media_wiki/utils_spec.rb} +41 -39
- data/spec/spec_helper.rb +17 -16
- metadata +77 -135
- data/.ruby-version +0 -1
- data/.rvmrc +0 -34
- data/Gemfile +0 -19
- data/Gemfile.lock +0 -77
- data/LICENSE +0 -21
- data/config/hosts.yml +0 -17
- data/lib/media_wiki/config.rb +0 -69
- data/mediawiki-gateway.gemspec +0 -113
- data/samples/README +0 -18
- data/samples/create_page.rb +0 -13
- data/samples/delete_batch.rb +0 -14
- data/samples/download_batch.rb +0 -15
- data/samples/email_user.rb +0 -14
- data/samples/export_xml.rb +0 -14
- data/samples/get_page.rb +0 -11
- data/samples/import_xml.rb +0 -14
- data/samples/run_fake_media_wiki.rb +0 -8
- data/samples/search_content.rb +0 -12
- data/samples/semantic_query.rb +0 -17
- data/samples/upload_commons.rb +0 -45
- data/samples/upload_file.rb +0 -13
- data/spec/fake_media_wiki/api_pages.rb +0 -135
- data/spec/fake_media_wiki/app.rb +0 -360
- data/spec/fake_media_wiki/query_handling.rb +0 -136
- data/spec/gateway_spec.rb +0 -888
@@ -1,136 +0,0 @@
|
|
1
|
-
|
2
|
-
module FakeMediaWiki
|
3
|
-
|
4
|
-
module QueryHandling
|
5
|
-
|
6
|
-
def action
|
7
|
-
[:userrights].each do |action_type|
|
8
|
-
return send(action_type)
|
9
|
-
end
|
10
|
-
halt(404, "Page not found")
|
11
|
-
end
|
12
|
-
|
13
|
-
def query
|
14
|
-
[:prop, :export, :list, :meta].each do |query_type|
|
15
|
-
return send(query_type) if params[query_type]
|
16
|
-
end
|
17
|
-
halt(404, "Page not found")
|
18
|
-
end
|
19
|
-
|
20
|
-
def prop
|
21
|
-
return get_revisions if params[:prop] == "revisions"
|
22
|
-
return get_undelete_token if params[:drprop] == 'token'
|
23
|
-
return get_token if params[:intoken]
|
24
|
-
return get_info if params[:prop] == "info"
|
25
|
-
end
|
26
|
-
|
27
|
-
def export
|
28
|
-
builder do |_|
|
29
|
-
_.mediawiki do
|
30
|
-
requested_page_titles.each do |requested_title|
|
31
|
-
page = @pages.get(requested_title)
|
32
|
-
_.page do
|
33
|
-
_.title(page[:title])
|
34
|
-
_.id(page[:pageid])
|
35
|
-
_.revision do
|
36
|
-
_.id(page[:pageid])
|
37
|
-
_.text(page[:content])
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def list
|
46
|
-
list_type = params[:list].to_sym
|
47
|
-
|
48
|
-
# api.php?action=query&list=users&ususers=Bob&ustoken=userrights
|
49
|
-
if list_type == :users && params[:ustoken] && params[:ususers]
|
50
|
-
# This "list" is actually a request for a user rights token
|
51
|
-
return get_userrights_token(params[:ususers])
|
52
|
-
end
|
53
|
-
|
54
|
-
# This is a real list
|
55
|
-
return send(list_type) if respond_to?(list_type)
|
56
|
-
halt(404, "Page not found")
|
57
|
-
end
|
58
|
-
|
59
|
-
def allpages
|
60
|
-
api_response do |_|
|
61
|
-
_.query do
|
62
|
-
_.allpages do
|
63
|
-
prefix = params[:apprefix]
|
64
|
-
namespace = @pages.namespaces_by_id[params[:apnamespace].to_i]
|
65
|
-
prefix = "#{namespace}:#{prefix}" unless namespace.empty?
|
66
|
-
@pages.list(prefix).each do |key, page|
|
67
|
-
_.p(nil, { :title => page[:title], :ns => page[:namespace], :id => page[:pageid] })
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def search
|
75
|
-
api_response do |_|
|
76
|
-
_.query do
|
77
|
-
_.search do
|
78
|
-
namespaces = params[:srnamespace] ? params[:srnamespace].split('|') : [ "0" ]
|
79
|
-
@pages.search(params[:srsearch], namespaces).first(params[:srlimit].to_i).each do |key, page|
|
80
|
-
_.p(nil, { :title => page[:title], :ns => page[:namespace], :id => page[:pageid] })
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def meta
|
88
|
-
meta_type = params[:meta].to_sym
|
89
|
-
return send(meta_type) if respond_to?(meta_type)
|
90
|
-
halt(404, "Page not found")
|
91
|
-
end
|
92
|
-
|
93
|
-
def siteinfo
|
94
|
-
if siteinfo_type = params[:siprop]
|
95
|
-
return send(siteinfo_type) if respond_to?(siteinfo_type)
|
96
|
-
halt(404, "Page not found")
|
97
|
-
else
|
98
|
-
api_response do |_|
|
99
|
-
_.query do
|
100
|
-
_.general(generator: "MediaWiki #{MediaWiki::VERSION}")
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
def namespaces
|
107
|
-
api_response do |_|
|
108
|
-
_.query do
|
109
|
-
_.namespaces do
|
110
|
-
@pages.namespaces_by_prefix.each do |prefix, id|
|
111
|
-
attr = { :id => id }
|
112
|
-
attr[:canonical] = prefix unless prefix.empty?
|
113
|
-
_.ns(prefix, attr)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
def extensions
|
121
|
-
api_response do |_|
|
122
|
-
_.query do
|
123
|
-
_.extensions do
|
124
|
-
@extensions.each do |name, version|
|
125
|
-
attr = { :version => version }
|
126
|
-
attr[:name] = name unless name.empty?
|
127
|
-
_.ext(name, attr)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
end
|
135
|
-
|
136
|
-
end
|
data/spec/gateway_spec.rb
DELETED
@@ -1,888 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
# Kickstart fake media wiki app
|
4
|
-
require 'sham_rack'
|
5
|
-
require_relative 'fake_media_wiki/app'
|
6
|
-
|
7
|
-
$fake_media_wiki = FakeMediaWiki::App.new!
|
8
|
-
ShamRack.mount($fake_media_wiki, 'dummy-wiki.example')
|
9
|
-
|
10
|
-
describe MediaWiki::Gateway do
|
11
|
-
|
12
|
-
before do
|
13
|
-
@gateway = MediaWiki::Gateway.new('http://dummy-wiki.example/w/api.php')
|
14
|
-
$fake_media_wiki.reset
|
15
|
-
end
|
16
|
-
|
17
|
-
describe '#login' do
|
18
|
-
|
19
|
-
describe "with a valid username & password" do
|
20
|
-
|
21
|
-
before do
|
22
|
-
@gateway.login('atlasmw', 'wombat')
|
23
|
-
end
|
24
|
-
|
25
|
-
it "should login successfully with the default domain" do
|
26
|
-
$fake_media_wiki.logged_in('atlasmw').should == true
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
31
|
-
describe "with a valid username, password and domain" do
|
32
|
-
|
33
|
-
before do
|
34
|
-
@gateway.login('ldapuser', 'ldappass', 'ldapdomain')
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should login successfully" do
|
38
|
-
$fake_media_wiki.logged_in('ldapuser').should == true
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
42
|
-
|
43
|
-
describe "with an non-existent username" do
|
44
|
-
|
45
|
-
it "should raise an error" do
|
46
|
-
lambda do
|
47
|
-
@gateway.login('bogususer', 'sekrit')
|
48
|
-
end.should raise_error(MediaWiki::Unauthorized)
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
describe "with an incorrect password" do
|
54
|
-
|
55
|
-
it "should raise an error" do
|
56
|
-
lambda do
|
57
|
-
@gateway.login('atlasmw', 'sekrit')
|
58
|
-
end.should raise_error(MediaWiki::Unauthorized)
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "with an incorrect domain" do
|
64
|
-
|
65
|
-
it "should raise an error" do
|
66
|
-
lambda do
|
67
|
-
@gateway.login('atlasmw', 'wombat', 'bogusdomain')
|
68
|
-
end.should raise_error(MediaWiki::Unauthorized)
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
describe "#get_token" do
|
76
|
-
|
77
|
-
describe "when not logged in" do
|
78
|
-
|
79
|
-
describe "requesting an edit token" do
|
80
|
-
|
81
|
-
before do
|
82
|
-
@token = @gateway.send(:get_token, 'edit', 'Main Page')
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should return a blank token" do
|
86
|
-
@token.should_not == nil
|
87
|
-
@token.should == "+\\"
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
91
|
-
|
92
|
-
describe "requesting an import token" do
|
93
|
-
|
94
|
-
it "should raise an error" do
|
95
|
-
lambda do
|
96
|
-
@gateway.send(:get_token, 'import', 'Main Page')
|
97
|
-
end.should raise_error(MediaWiki::Unauthorized)
|
98
|
-
end
|
99
|
-
|
100
|
-
end
|
101
|
-
|
102
|
-
end
|
103
|
-
|
104
|
-
describe "when logged in as admin user" do
|
105
|
-
|
106
|
-
before do
|
107
|
-
@gateway.login('atlasmw', 'wombat')
|
108
|
-
end
|
109
|
-
|
110
|
-
describe "requesting an edit token for a single page" do
|
111
|
-
|
112
|
-
before do
|
113
|
-
@token = @gateway.send(:get_token, 'edit', 'Main Page')
|
114
|
-
end
|
115
|
-
|
116
|
-
it "should return a token" do
|
117
|
-
@token.should_not == nil
|
118
|
-
@token.should_not == "+\\"
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|
122
|
-
|
123
|
-
describe "requesting an edit token for multiple pages" do
|
124
|
-
|
125
|
-
before do
|
126
|
-
@token = @gateway.send(:get_token, 'edit', "Main Page|Another Page")
|
127
|
-
end
|
128
|
-
|
129
|
-
it "should return a token" do
|
130
|
-
@token.should_not == nil
|
131
|
-
@token.should_not == "+\\"
|
132
|
-
end
|
133
|
-
|
134
|
-
end
|
135
|
-
|
136
|
-
describe "requesting an import token" do
|
137
|
-
|
138
|
-
before do
|
139
|
-
@token = @gateway.send(:get_token, 'import', 'Main Page')
|
140
|
-
end
|
141
|
-
|
142
|
-
it "should return a token" do
|
143
|
-
@token.should_not == nil
|
144
|
-
@token.should_not == "+\\"
|
145
|
-
end
|
146
|
-
|
147
|
-
end
|
148
|
-
|
149
|
-
end
|
150
|
-
|
151
|
-
end
|
152
|
-
|
153
|
-
describe "#get" do
|
154
|
-
|
155
|
-
describe "for an existing wiki page" do
|
156
|
-
|
157
|
-
it "returns raw page content" do
|
158
|
-
@gateway.get("Main Page").should == "Content"
|
159
|
-
end
|
160
|
-
|
161
|
-
end
|
162
|
-
|
163
|
-
describe "for an existing empty wiki page" do
|
164
|
-
|
165
|
-
it "returns an empty string" do
|
166
|
-
@gateway.get("Empty").should == ""
|
167
|
-
end
|
168
|
-
|
169
|
-
end
|
170
|
-
|
171
|
-
describe "for a missing wiki page" do
|
172
|
-
|
173
|
-
it "returns nil" do
|
174
|
-
@gateway.get("page/missing").should be_nil
|
175
|
-
end
|
176
|
-
|
177
|
-
end
|
178
|
-
|
179
|
-
describe "for root (/)" do
|
180
|
-
|
181
|
-
it "returns nil" do
|
182
|
-
@gateway.get("").should be_nil
|
183
|
-
end
|
184
|
-
|
185
|
-
end
|
186
|
-
|
187
|
-
describe "when wiki returns 503" do
|
188
|
-
|
189
|
-
before do
|
190
|
-
@log = double(:debug => nil, :warn => nil)
|
191
|
-
@fail_gateway = MediaWiki::Gateway.new('http://dummy-wiki.example/w/api.php', {:maxlag => -1, :retry_delay => 0})
|
192
|
-
allow(@fail_gateway).to receive(:log) { @log }
|
193
|
-
end
|
194
|
-
|
195
|
-
it "should retry twice and fail" do
|
196
|
-
lambda {
|
197
|
-
@fail_gateway.get("")
|
198
|
-
}.should raise_error
|
199
|
-
@log.should have_received(:warn).with("503 Service Unavailable: Maxlag exceeded. Retry in 0 seconds.").twice
|
200
|
-
end
|
201
|
-
|
202
|
-
end
|
203
|
-
|
204
|
-
it "should pass options to RestClient::Request" do
|
205
|
-
gateway = MediaWiki::Gateway.new('http://dummy-wiki.example/w/api.php', {}, :verify_ssl => false)
|
206
|
-
RestClient::Request.should receive(:execute).with(hash_including(:verify_ssl => false)).and_return([double(:elements => {})])
|
207
|
-
gateway.get("").should be_nil
|
208
|
-
end
|
209
|
-
|
210
|
-
end
|
211
|
-
|
212
|
-
describe "#redirect?" do
|
213
|
-
|
214
|
-
describe "for an existing redirect page" do
|
215
|
-
|
216
|
-
it "returns true" do
|
217
|
-
@gateway.redirect?("Redirect").should == true
|
218
|
-
end
|
219
|
-
|
220
|
-
end
|
221
|
-
|
222
|
-
describe "for an existing non-redirect page" do
|
223
|
-
|
224
|
-
it "returns false" do
|
225
|
-
@gateway.redirect?("Main Page").should == false
|
226
|
-
end
|
227
|
-
|
228
|
-
end
|
229
|
-
|
230
|
-
describe "for a missing wiki page" do
|
231
|
-
|
232
|
-
it "returns false" do
|
233
|
-
@gateway.redirect?("page/missing").should == false
|
234
|
-
end
|
235
|
-
|
236
|
-
end
|
237
|
-
|
238
|
-
end
|
239
|
-
|
240
|
-
describe "#render" do
|
241
|
-
|
242
|
-
describe "for an existing wiki page" do
|
243
|
-
|
244
|
-
before do
|
245
|
-
@pages = @gateway.render('Main Page')
|
246
|
-
end
|
247
|
-
|
248
|
-
it "should return the page content" do
|
249
|
-
expected = 'Sample <B>HTML</B> content.'
|
250
|
-
@pages.to_s.should == expected
|
251
|
-
end
|
252
|
-
|
253
|
-
it "should raise an ArgumentError on illegal options" do
|
254
|
-
lambda do
|
255
|
-
@gateway.render("Main Page", :doesnotexist => :at_all)
|
256
|
-
end.should raise_error(ArgumentError)
|
257
|
-
end
|
258
|
-
|
259
|
-
describe "with option" do
|
260
|
-
|
261
|
-
it "should strip img tags" do
|
262
|
-
@pages = @gateway.render('Foopage', :noimages => true)
|
263
|
-
|
264
|
-
expected = 'Sample <B>HTML</B> content.'\
|
265
|
-
'<span class="editsection">[<a title="Edit section: Nomenclature" href="/w/index.php?title=Seat_of_local_government&action=edit&section=1">edit</a>]</span>'\
|
266
|
-
'<a title="Interpreted language" href="/wiki/Interpreted_language">interpreted language</a>'
|
267
|
-
@pages.to_s.should == expected
|
268
|
-
end
|
269
|
-
|
270
|
-
it "should strip edit sections" do
|
271
|
-
@pages = @gateway.render('Foopage', :noeditsections => true)
|
272
|
-
|
273
|
-
expected = 'Sample <B>HTML</B> content.' \
|
274
|
-
'<img width="150" height="150" class="thumbimage" src="http://upload.wikimedia.org/foo/Ruby_logo.svg" alt="Ruby logo.svg"/>' \
|
275
|
-
'<a title="Interpreted language" href="/wiki/Interpreted_language">interpreted language</a>'
|
276
|
-
@pages.to_s.should == expected
|
277
|
-
end
|
278
|
-
|
279
|
-
it "should make all links absolute" do
|
280
|
-
@pages = @gateway.render('Foopage', :linkbase => "http://en.wikipedia.org")
|
281
|
-
|
282
|
-
expected = 'Sample <B>HTML</B> content.' \
|
283
|
-
'<img width="150" height="150" class="thumbimage" src="http://upload.wikimedia.org/foo/Ruby_logo.svg" alt="Ruby logo.svg"/>' \
|
284
|
-
'<span class="editsection">[<a title="Edit section: Nomenclature" href="/w/index.php?title=Seat_of_local_government&action=edit&section=1">edit</a>]</span>'\
|
285
|
-
'<a title="Interpreted language" href="http://en.wikipedia.org/wiki/Interpreted_language">interpreted language</a>'
|
286
|
-
@pages.to_s.should == expected
|
287
|
-
end
|
288
|
-
|
289
|
-
end
|
290
|
-
|
291
|
-
end
|
292
|
-
|
293
|
-
describe "for a missing wiki page" do
|
294
|
-
|
295
|
-
before do
|
296
|
-
@pages = @gateway.render('Invalidpage')
|
297
|
-
end
|
298
|
-
|
299
|
-
it "should return nil" do
|
300
|
-
@pages.should == nil
|
301
|
-
end
|
302
|
-
|
303
|
-
end
|
304
|
-
|
305
|
-
end
|
306
|
-
|
307
|
-
describe "#create" do
|
308
|
-
|
309
|
-
before do
|
310
|
-
@gateway.login('atlasmw', 'wombat')
|
311
|
-
end
|
312
|
-
|
313
|
-
describe "when creating a new page" do
|
314
|
-
|
315
|
-
before do
|
316
|
-
@page = @gateway.create("A New Page", "Some content")
|
317
|
-
end
|
318
|
-
|
319
|
-
it "should create the page" do
|
320
|
-
expected = <<-XML
|
321
|
-
<api>
|
322
|
-
<edit new='' result='Success' pageid='8' title='A New Page' oldrevid='0' newrevid='8'/>
|
323
|
-
</api>
|
324
|
-
XML
|
325
|
-
Hash.from_xml(@page.first.to_s).should == Hash.from_xml(expected)
|
326
|
-
end
|
327
|
-
|
328
|
-
end
|
329
|
-
|
330
|
-
describe "when creating a page that already exists" do
|
331
|
-
|
332
|
-
before do
|
333
|
-
$fake_media_wiki.reset
|
334
|
-
end
|
335
|
-
|
336
|
-
describe "and the 'overwrite' option is set" do
|
337
|
-
|
338
|
-
before do
|
339
|
-
@new_page = @gateway.create("Main Page", "Some new content", :summary => "The summary", :overwrite => true)
|
340
|
-
end
|
341
|
-
|
342
|
-
it "should overwrite the existing page" do
|
343
|
-
expected = <<-XML
|
344
|
-
<api>
|
345
|
-
<edit result='Success' pageid='8' title='Main Page' oldrevid='1' newrevid='8'/>
|
346
|
-
</api>
|
347
|
-
XML
|
348
|
-
Hash.from_xml(@new_page.first.to_s).should == Hash.from_xml(expected)
|
349
|
-
end
|
350
|
-
|
351
|
-
end
|
352
|
-
|
353
|
-
describe "and the 'overwrite' option is not set" do
|
354
|
-
|
355
|
-
it "should raise an error" do
|
356
|
-
lambda do
|
357
|
-
@gateway.create("Main Page", "Some new content")
|
358
|
-
end.should raise_error(MediaWiki::APIError)
|
359
|
-
end
|
360
|
-
|
361
|
-
end
|
362
|
-
|
363
|
-
end
|
364
|
-
|
365
|
-
end
|
366
|
-
|
367
|
-
describe "#edit" do
|
368
|
-
before do
|
369
|
-
$fake_media_wiki.reset
|
370
|
-
@edit_page = @gateway.edit("Main Page", "Some new content")
|
371
|
-
end
|
372
|
-
|
373
|
-
it "should overwrite the existing page" do
|
374
|
-
expected = <<-XML
|
375
|
-
<api>
|
376
|
-
<edit result='Success' pageid='8' title='Main Page' oldrevid='1' newrevid='8'/>
|
377
|
-
</api>
|
378
|
-
XML
|
379
|
-
Hash.from_xml(@edit_page.first.to_s).should == Hash.from_xml(expected)
|
380
|
-
end
|
381
|
-
|
382
|
-
end
|
383
|
-
|
384
|
-
describe "#upload" do
|
385
|
-
|
386
|
-
before do
|
387
|
-
@gateway.login('atlasmw', 'wombat')
|
388
|
-
end
|
389
|
-
|
390
|
-
describe "when uploading a new file" do
|
391
|
-
|
392
|
-
before do
|
393
|
-
@path = 'some/path/sample_image.jpg'
|
394
|
-
allow(File).to receive(:new).with(@path).and_return('SAMPLEIMAGEDATA')
|
395
|
-
@page = @gateway.upload(@path)
|
396
|
-
end
|
397
|
-
|
398
|
-
it "should open the file" do
|
399
|
-
File.should have_received(:new).with(@path)
|
400
|
-
end
|
401
|
-
|
402
|
-
it "should upload the file" do
|
403
|
-
expected = <<-XML
|
404
|
-
<api>
|
405
|
-
<upload result="Success" filename="sample_image.jpg"/>
|
406
|
-
</api>
|
407
|
-
XML
|
408
|
-
Hash.from_xml(@page.first.to_s).should == Hash.from_xml(expected)
|
409
|
-
end
|
410
|
-
|
411
|
-
end
|
412
|
-
|
413
|
-
end
|
414
|
-
|
415
|
-
describe "#delete" do
|
416
|
-
|
417
|
-
describe "when logged in as admin" do
|
418
|
-
|
419
|
-
describe "and the page exists" do
|
420
|
-
def delete_response
|
421
|
-
<<-XML
|
422
|
-
<api>
|
423
|
-
<delete title='Deletable Page' reason='Default reason'/>
|
424
|
-
</api>
|
425
|
-
XML
|
426
|
-
end
|
427
|
-
|
428
|
-
before do
|
429
|
-
@gateway.login("atlasmw", "wombat")
|
430
|
-
|
431
|
-
create("Deletable Page", "Some content")
|
432
|
-
@page = @gateway.delete("Deletable Page")
|
433
|
-
end
|
434
|
-
|
435
|
-
it "should delete the page" do
|
436
|
-
Hash.from_xml(@page.first.to_s) == Hash.from_xml(delete_response)
|
437
|
-
end
|
438
|
-
end
|
439
|
-
|
440
|
-
describe "and the page does not exist" do
|
441
|
-
|
442
|
-
before do
|
443
|
-
@gateway.login("atlasmw", "wombat")
|
444
|
-
end
|
445
|
-
|
446
|
-
it "should raise an error" do
|
447
|
-
lambda do
|
448
|
-
@gateway.delete("Missing Page")
|
449
|
-
end.should raise_error(MediaWiki::APIError)
|
450
|
-
end
|
451
|
-
end
|
452
|
-
end
|
453
|
-
|
454
|
-
describe "when not logged in" do
|
455
|
-
|
456
|
-
before do
|
457
|
-
create("Deletable Page", "Some content")
|
458
|
-
end
|
459
|
-
|
460
|
-
it "should raise an error" do
|
461
|
-
lambda do
|
462
|
-
@gateway.delete("Deletable Page")
|
463
|
-
end.should raise_error(MediaWiki::Unauthorized)
|
464
|
-
end
|
465
|
-
|
466
|
-
end
|
467
|
-
|
468
|
-
end
|
469
|
-
|
470
|
-
describe "#undelete" do
|
471
|
-
|
472
|
-
describe "when logged in as admin" do
|
473
|
-
before do
|
474
|
-
$fake_media_wiki.reset
|
475
|
-
@gateway.login("atlasmw", "wombat")
|
476
|
-
end
|
477
|
-
|
478
|
-
describe "and the page no longer exists" do
|
479
|
-
before do
|
480
|
-
@revs = @gateway.undelete("Sandbox:Undeleted")
|
481
|
-
end
|
482
|
-
|
483
|
-
it "should recreate the given page" do
|
484
|
-
@gateway.list("Sandbox:Undeleted").should == [ "Sandbox:Undeleted" ]
|
485
|
-
end
|
486
|
-
|
487
|
-
it "should report one undeleted revision" do
|
488
|
-
@revs.should == 1
|
489
|
-
end
|
490
|
-
end
|
491
|
-
|
492
|
-
describe "but the page exists" do
|
493
|
-
before do
|
494
|
-
@revs = @gateway.undelete("Main Page")
|
495
|
-
end
|
496
|
-
|
497
|
-
it "should report zero undeleted revisions" do
|
498
|
-
@revs.should == 0
|
499
|
-
end
|
500
|
-
end
|
501
|
-
end
|
502
|
-
|
503
|
-
describe "when not logged in" do
|
504
|
-
|
505
|
-
it "should raise an error" do
|
506
|
-
lambda do
|
507
|
-
@gateway.undelete("Undeletable Page")
|
508
|
-
end.should raise_error(MediaWiki::APIError)
|
509
|
-
end
|
510
|
-
|
511
|
-
end
|
512
|
-
|
513
|
-
end
|
514
|
-
|
515
|
-
describe "#list" do
|
516
|
-
|
517
|
-
before do
|
518
|
-
$fake_media_wiki.reset
|
519
|
-
end
|
520
|
-
|
521
|
-
describe "with an empty key" do
|
522
|
-
|
523
|
-
before do
|
524
|
-
@list = @gateway.list("")
|
525
|
-
end
|
526
|
-
|
527
|
-
it "should list all pages" do
|
528
|
-
@list.sort.should == [ "Book:Italy", "Empty", "Foopage", "Level/Level/Index", "Main 2", "Main Page", "Redirect" ]
|
529
|
-
end
|
530
|
-
|
531
|
-
end
|
532
|
-
|
533
|
-
describe "with a namespace as the key" do
|
534
|
-
|
535
|
-
before do
|
536
|
-
@list = @gateway.list("Book:")
|
537
|
-
end
|
538
|
-
|
539
|
-
it "should list all pages in the namespace" do
|
540
|
-
@list.should == [ "Book:Italy" ]
|
541
|
-
end
|
542
|
-
|
543
|
-
end
|
544
|
-
|
545
|
-
describe "with a partial title as the key" do
|
546
|
-
|
547
|
-
before do
|
548
|
-
@list = @gateway.list("Main")
|
549
|
-
end
|
550
|
-
|
551
|
-
it "should list all pages in the main namespace that start with key" do
|
552
|
-
@list.sort.should == [ "Main 2", "Main Page" ]
|
553
|
-
end
|
554
|
-
|
555
|
-
end
|
556
|
-
|
557
|
-
end
|
558
|
-
|
559
|
-
describe "#search" do
|
560
|
-
|
561
|
-
before do
|
562
|
-
$fake_media_wiki.reset
|
563
|
-
@gateway.create("Search Test", "Foo KEY Blah")
|
564
|
-
@gateway.create("Search Test 2", "Zomp KEY Zorg")
|
565
|
-
@gateway.create("Book:Search Test", "Bar KEY Baz")
|
566
|
-
@gateway.create("Sandbox:Search Test", "Evil KEY Evil")
|
567
|
-
end
|
568
|
-
|
569
|
-
describe "with an empty key" do
|
570
|
-
|
571
|
-
it "should raise an error" do
|
572
|
-
lambda do
|
573
|
-
@gateway.search("")
|
574
|
-
end.should raise_error(MediaWiki::APIError)
|
575
|
-
end
|
576
|
-
|
577
|
-
end
|
578
|
-
|
579
|
-
describe "with a valid key and no namespaces" do
|
580
|
-
|
581
|
-
before do
|
582
|
-
@search = @gateway.search("KEY")
|
583
|
-
end
|
584
|
-
|
585
|
-
it "should list all matching pages in the main namespace" do
|
586
|
-
@search.should =~ [ "Search Test", "Search Test 2" ]
|
587
|
-
end
|
588
|
-
|
589
|
-
end
|
590
|
-
|
591
|
-
describe "with a valid key and a namespace string" do
|
592
|
-
|
593
|
-
before do
|
594
|
-
@search = @gateway.search("KEY", "Book")
|
595
|
-
end
|
596
|
-
|
597
|
-
it "should list all matching pages in the specified namespaces" do
|
598
|
-
@search.should == [ "Book:Search Test" ]
|
599
|
-
end
|
600
|
-
|
601
|
-
end
|
602
|
-
|
603
|
-
describe "with a valid key and a namespace array" do
|
604
|
-
|
605
|
-
before do
|
606
|
-
@search = @gateway.search("KEY", ["Book", "Sandbox"])
|
607
|
-
end
|
608
|
-
|
609
|
-
it "should list all matching pages in the specified namespaces" do
|
610
|
-
@search.should =~ [ "Sandbox:Search Test", "Book:Search Test" ]
|
611
|
-
end
|
612
|
-
|
613
|
-
end
|
614
|
-
|
615
|
-
describe "with maximum number of results" do
|
616
|
-
|
617
|
-
before do
|
618
|
-
@search = @gateway.search("KEY", nil, 2, 1)
|
619
|
-
end
|
620
|
-
|
621
|
-
it "should return at most the maximum number of results asked" do
|
622
|
-
@search.size.should == 1
|
623
|
-
end
|
624
|
-
end
|
625
|
-
|
626
|
-
end
|
627
|
-
|
628
|
-
describe "#namespaces_by_prefix" do
|
629
|
-
|
630
|
-
before do
|
631
|
-
$fake_media_wiki.reset
|
632
|
-
@namespaces = @gateway.namespaces_by_prefix
|
633
|
-
end
|
634
|
-
|
635
|
-
it "should list all namespaces" do
|
636
|
-
@namespaces.should == { "" => 0, "Book" => 100, "Sandbox" => 200}
|
637
|
-
end
|
638
|
-
|
639
|
-
end
|
640
|
-
|
641
|
-
describe "#semantic_query" do
|
642
|
-
|
643
|
-
before do
|
644
|
-
@response = @gateway.semantic_query('[[place::123]]', ['mainlabel=Page'])
|
645
|
-
end
|
646
|
-
|
647
|
-
it "should return an HTML string" do
|
648
|
-
@response.should == 'Sample <B>HTML</B> content.'
|
649
|
-
end
|
650
|
-
|
651
|
-
end
|
652
|
-
|
653
|
-
describe "#import" do
|
654
|
-
|
655
|
-
def import_file
|
656
|
-
File.dirname(__FILE__) + "/import-test-data.xml"
|
657
|
-
end
|
658
|
-
|
659
|
-
describe "when not logged in" do
|
660
|
-
|
661
|
-
it "should raise an error" do
|
662
|
-
lambda do
|
663
|
-
@gateway.import(import_file)
|
664
|
-
end.should raise_error(MediaWiki::Unauthorized)
|
665
|
-
end
|
666
|
-
|
667
|
-
end
|
668
|
-
|
669
|
-
describe "when logged in as admin" do
|
670
|
-
|
671
|
-
def import_response
|
672
|
-
<<-XML
|
673
|
-
<api>
|
674
|
-
<import>
|
675
|
-
<page title='Main Page' ns='0' revisions='0'/>
|
676
|
-
<page title='Template:Header' ns='10' revisions='1'/>
|
677
|
-
</import>
|
678
|
-
</api>
|
679
|
-
XML
|
680
|
-
end
|
681
|
-
|
682
|
-
before do
|
683
|
-
@gateway.login("atlasmw", "wombat")
|
684
|
-
@page = @gateway.import(import_file)
|
685
|
-
end
|
686
|
-
|
687
|
-
it "should import content" do
|
688
|
-
Hash.from_xml(@page.first.to_s) == Hash.from_xml(import_response)
|
689
|
-
end
|
690
|
-
|
691
|
-
end
|
692
|
-
|
693
|
-
end
|
694
|
-
|
695
|
-
describe "#export" do
|
696
|
-
|
697
|
-
def export_response
|
698
|
-
<<-XML
|
699
|
-
<mediawiki>
|
700
|
-
<page>
|
701
|
-
<title>Main Page</title>
|
702
|
-
<id>1</id>
|
703
|
-
<revision>
|
704
|
-
<id>1</id>
|
705
|
-
<text>Content</text>
|
706
|
-
</revision>
|
707
|
-
</page>
|
708
|
-
</mediawiki>
|
709
|
-
XML
|
710
|
-
end
|
711
|
-
|
712
|
-
before do
|
713
|
-
@page = @gateway.export("Main Page")
|
714
|
-
end
|
715
|
-
|
716
|
-
it "should return export data for the page" do
|
717
|
-
Hash.from_xml(@page.to_s).should == Hash.from_xml(export_response)
|
718
|
-
end
|
719
|
-
|
720
|
-
end
|
721
|
-
|
722
|
-
describe "#siteinfo" do
|
723
|
-
|
724
|
-
before do
|
725
|
-
$fake_media_wiki.reset
|
726
|
-
@siteinfo = @gateway.siteinfo
|
727
|
-
end
|
728
|
-
|
729
|
-
it "should get the siteinfo" do
|
730
|
-
@siteinfo.should == { 'generator' => "MediaWiki #{MediaWiki::VERSION}" }
|
731
|
-
end
|
732
|
-
|
733
|
-
end
|
734
|
-
|
735
|
-
describe "#version" do
|
736
|
-
|
737
|
-
before do
|
738
|
-
$fake_media_wiki.reset
|
739
|
-
@version = @gateway.version
|
740
|
-
end
|
741
|
-
|
742
|
-
it "should get the version" do
|
743
|
-
@version.should == MediaWiki::VERSION
|
744
|
-
end
|
745
|
-
|
746
|
-
end
|
747
|
-
|
748
|
-
describe "#namespaces_by_prefix" do
|
749
|
-
|
750
|
-
before do
|
751
|
-
$fake_media_wiki.reset
|
752
|
-
@namespaces = @gateway.send :namespaces_by_prefix
|
753
|
-
end
|
754
|
-
|
755
|
-
it "should list all namespaces" do
|
756
|
-
@namespaces.should == { "" => 0, "Book" => 100, "Sandbox" => 200}
|
757
|
-
end
|
758
|
-
|
759
|
-
end
|
760
|
-
|
761
|
-
describe "#extensions" do
|
762
|
-
|
763
|
-
before do
|
764
|
-
$fake_media_wiki.reset
|
765
|
-
@extensions = @gateway.extensions
|
766
|
-
end
|
767
|
-
|
768
|
-
it "should list all extensions" do
|
769
|
-
@extensions.should == { "FooExtension" => "r1", "BarExtension" => "r2", 'Semantic MediaWiki' => '1.5' }
|
770
|
-
end
|
771
|
-
|
772
|
-
end
|
773
|
-
|
774
|
-
def create(title, content, options={})
|
775
|
-
form_data = {'action' => 'edit', 'title' => title, 'text' => content, 'summary' => (options[:summary] || ""), 'token' => @gateway.send(:get_token, 'edit', title)}
|
776
|
-
form_data['createonly'] = "" unless options[:overwrite]
|
777
|
-
@gateway.send(:make_api_request, form_data)
|
778
|
-
end
|
779
|
-
|
780
|
-
describe "#create_account" do
|
781
|
-
|
782
|
-
describe "when logged in as admin" do
|
783
|
-
before do
|
784
|
-
@gateway.login("atlasmw", "wombat")
|
785
|
-
end
|
786
|
-
|
787
|
-
it 'should get expected result' do
|
788
|
-
expected = <<-XML
|
789
|
-
<api>
|
790
|
-
<createaccount result='success' token='admin_token+\\' userid='4' username='FooBar'/>
|
791
|
-
</api>
|
792
|
-
XML
|
793
|
-
|
794
|
-
Hash.from_xml(@gateway.create_account({ 'name' => 'FooBar', 'password' => 'BarBaz' }).to_s).should == Hash.from_xml(expected)
|
795
|
-
end
|
796
|
-
|
797
|
-
end
|
798
|
-
|
799
|
-
end
|
800
|
-
|
801
|
-
describe '#options' do
|
802
|
-
|
803
|
-
describe 'when logged in' do
|
804
|
-
before do
|
805
|
-
@gateway.login("atlasmw", "wombat")
|
806
|
-
end
|
807
|
-
|
808
|
-
describe 'requesting an options token' do
|
809
|
-
before do
|
810
|
-
@token = @gateway.send(:get_options_token)
|
811
|
-
end
|
812
|
-
|
813
|
-
it "should return a token" do
|
814
|
-
@token.should_not == nil
|
815
|
-
@token.should_not == "+\\"
|
816
|
-
end
|
817
|
-
|
818
|
-
end
|
819
|
-
|
820
|
-
it 'should return the expected response' do
|
821
|
-
expected = '<api options="success" />'
|
822
|
-
Hash.from_xml(@gateway.options({ :realname => 'Bar Baz' }).to_s).should == Hash.from_xml(expected)
|
823
|
-
end
|
824
|
-
|
825
|
-
end
|
826
|
-
|
827
|
-
end
|
828
|
-
|
829
|
-
describe "#user_rights" do
|
830
|
-
|
831
|
-
describe "when logged in as admin" do
|
832
|
-
before do
|
833
|
-
@gateway.login("atlasmw", "wombat")
|
834
|
-
end
|
835
|
-
|
836
|
-
describe "requesting a userrights token for an existing user" do
|
837
|
-
|
838
|
-
before do
|
839
|
-
@token = @gateway.send(:get_userrights_token, 'nonadmin')
|
840
|
-
end
|
841
|
-
|
842
|
-
it "should return a token" do
|
843
|
-
@token.should_not == nil
|
844
|
-
@token.should_not == "+\\"
|
845
|
-
end
|
846
|
-
end
|
847
|
-
|
848
|
-
describe "requesting a userrights token for an nonexistant user" do
|
849
|
-
|
850
|
-
it "should raise an error" do
|
851
|
-
lambda do
|
852
|
-
@gateway.send(:get_userrights_token, 'nosuchuser')
|
853
|
-
end.should raise_error(MediaWiki::APIError)
|
854
|
-
end
|
855
|
-
end
|
856
|
-
|
857
|
-
describe "changing a user's groups with a valid token" do
|
858
|
-
|
859
|
-
def userrights_response
|
860
|
-
<<-XML
|
861
|
-
<api>
|
862
|
-
<userrights user="nonadmin">
|
863
|
-
<removed>
|
864
|
-
<group>oldgroup</group>
|
865
|
-
</removed>
|
866
|
-
<added>
|
867
|
-
<group>newgroup</group>
|
868
|
-
</added>
|
869
|
-
</userrights>
|
870
|
-
</api>
|
871
|
-
XML
|
872
|
-
end
|
873
|
-
|
874
|
-
before do
|
875
|
-
@token = @gateway.send(:get_userrights_token, 'nonadmin')
|
876
|
-
@result = @gateway.send(:userrights, 'nonadmin', @token, 'newgroup', 'oldgroup', 'because I can')
|
877
|
-
end
|
878
|
-
|
879
|
-
it "should return a result matching the input" do
|
880
|
-
Hash.from_xml(@result.to_s).should == Hash.from_xml(userrights_response)
|
881
|
-
end
|
882
|
-
|
883
|
-
end
|
884
|
-
|
885
|
-
end
|
886
|
-
end
|
887
|
-
|
888
|
-
end
|