mediawiki-gateway 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.gitignore +3 -0
  2. data/README +12 -0
  3. data/Rakefile +40 -0
  4. data/VERSION +1 -0
  5. data/config/hosts.yml +17 -0
  6. data/doc/classes/MediaWiki.html +189 -0
  7. data/doc/classes/MediaWiki/Config.html +269 -0
  8. data/doc/classes/MediaWiki/Gateway.html +952 -0
  9. data/doc/created.rid +1 -0
  10. data/doc/files/README_txt.html +117 -0
  11. data/doc/files/media_wiki/config_rb.html +108 -0
  12. data/doc/files/media_wiki/gateway_rb.html +113 -0
  13. data/doc/files/media_wiki/utils_rb.html +101 -0
  14. data/doc/files/script/create_page_rb.html +115 -0
  15. data/doc/files/script/delete_book_rb.html +108 -0
  16. data/doc/files/script/export_xml_rb.html +114 -0
  17. data/doc/files/script/get_page_rb.html +114 -0
  18. data/doc/files/script/import_xml_rb.html +114 -0
  19. data/doc/files/script/undelete_page_rb.html +101 -0
  20. data/doc/files/script/upload_commons_rb.html +109 -0
  21. data/doc/files/script/upload_file_rb.html +115 -0
  22. data/doc/fr_class_index.html +29 -0
  23. data/doc/fr_file_index.html +38 -0
  24. data/doc/fr_method_index.html +49 -0
  25. data/doc/index.html +24 -0
  26. data/doc/rdoc-style.css +208 -0
  27. data/lib/media_wiki.rb +3 -0
  28. data/lib/media_wiki/config.rb +69 -0
  29. data/lib/media_wiki/gateway.rb +307 -0
  30. data/lib/media_wiki/utils.rb +18 -0
  31. data/mediawiki-gateway.gemspec +88 -0
  32. data/script/create_page.rb +14 -0
  33. data/script/delete_book.rb +14 -0
  34. data/script/export_xml.rb +14 -0
  35. data/script/get_page.rb +12 -0
  36. data/script/import_xml.rb +14 -0
  37. data/script/run_fake_media_wiki.rb +8 -0
  38. data/script/undelete_page.rb +15 -0
  39. data/script/upload_commons.rb +42 -0
  40. data/script/upload_file.rb +14 -0
  41. data/spec/fake_media_wiki/api_pages.rb +131 -0
  42. data/spec/fake_media_wiki/app.rb +262 -0
  43. data/spec/fake_media_wiki/query_handling.rb +112 -0
  44. data/spec/gateway_spec.old +535 -0
  45. data/spec/gateway_spec.rb +653 -0
  46. data/spec/import-test-data.xml +68 -0
  47. metadata +115 -0
@@ -0,0 +1,112 @@
1
+
2
+ module FakeMediaWiki
3
+
4
+ module QueryHandling
5
+
6
+ def query
7
+ [:prop, :export, :list, :meta].each do |query_type|
8
+ return send(query_type) if params[query_type]
9
+ end
10
+ halt(404, "Page not found")
11
+ end
12
+
13
+ def prop
14
+ return get_revisions if params[:prop] == "revisions"
15
+ return get_undelete_token if params[:drprop] == 'token'
16
+ return get_token if params[:prop] == "info"
17
+ end
18
+
19
+ def export
20
+ builder do |_|
21
+ _.mediawiki do
22
+ requested_page_titles.each do |requested_title|
23
+ page = @pages.get(requested_title)
24
+ _.page do
25
+ _.title(page[:title])
26
+ _.id(page[:pageid])
27
+ _.revision do
28
+ _.id(page[:pageid])
29
+ _.text(page[:content])
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ def list
38
+ list_type = params[:list].to_sym
39
+ return send(list_type) if respond_to?(list_type)
40
+ halt(404, "Page not found")
41
+ end
42
+
43
+ def allpages
44
+ api_response do |_|
45
+ _.query do
46
+ _.allpages do
47
+ prefix = params[:apprefix]
48
+ namespace = @pages.namespaces_by_id[params[:apnamespace].to_i]
49
+ prefix = "#{namespace}:#{prefix}" unless namespace.empty?
50
+ @pages.list(prefix).each do |key, page|
51
+ _.p(nil, { :title => page[:title], :ns => page[:namespace], :id => page[:pageid] })
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ def search
59
+ api_response do |_|
60
+ _.query do
61
+ _.search do
62
+ namespaces = params[:srnamespace] ? params[:srnamespace].split('|') : [ "0" ]
63
+ @pages.search(params[:srsearch], namespaces).each do |key, page|
64
+ _.p(nil, { :title => page[:title], :ns => page[:namespace], :id => page[:pageid] })
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ def meta
72
+ meta_type = params[:meta].to_sym
73
+ return send(meta_type) if respond_to?(meta_type)
74
+ halt(404, "Page not found")
75
+ end
76
+
77
+ def siteinfo
78
+ siteinfo_type = params[:siprop].to_sym
79
+ return send(siteinfo_type) if respond_to?(siteinfo_type)
80
+ halt(404, "Page not found")
81
+ end
82
+
83
+ def namespaces
84
+ api_response do |_|
85
+ _.query do
86
+ _.namespaces do
87
+ @pages.namespaces_by_prefix.each do |prefix, id|
88
+ attr = { :id => id }
89
+ attr[:canonical] = prefix unless prefix.empty?
90
+ _.ns(prefix, attr)
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+
97
+ def extensions
98
+ api_response do |_|
99
+ _.query do
100
+ _.extensions do
101
+ @extensions.each do |name, version|
102
+ attr = { :version => version }
103
+ attr[:name] = name unless name.empty?
104
+ _.ext(name, attr)
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ end
@@ -0,0 +1,535 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe MediaWiki::Gateway do
4
+
5
+ def media_wiki_url
6
+ "#{LonelyPlanet::OnRails.environment.services[:atlas_mediawiki]}"
7
+ end
8
+
9
+ before do
10
+ @gateway = MediaWiki::Gateway.new({:url => media_wiki_url, :username => 'atlasmw', :password => 'wombat'})
11
+ $fake_media_wiki.reset
12
+ end
13
+
14
+ describe '.wiki_to_uri' do
15
+
16
+ it "should underscore spaces" do
17
+ MediaWiki.wiki_to_uri('getting there').should == 'getting_there'
18
+ end
19
+
20
+ it "should escape ampersands" do
21
+ MediaWiki.wiki_to_uri('getting there & away').should == 'getting_there_%26_away'
22
+ end
23
+
24
+ it "should escape UTF-8" do
25
+ MediaWiki.wiki_to_uri('Phở').should == 'Ph%E1%BB%9F'
26
+ end
27
+
28
+ it "should escape each path component but leave slashes untouched" do
29
+ MediaWiki.wiki_to_uri('Phở/B&r/B z').should == 'Ph%E1%BB%9F/B%26r/B_z'
30
+ end
31
+
32
+ it "should pass through nil" do
33
+ MediaWiki.wiki_to_uri(nil).should == nil
34
+ end
35
+
36
+ end
37
+
38
+ describe '#login' do
39
+
40
+ describe "with a valid username & password" do
41
+
42
+ before do
43
+ @gateway.login('atlasmw', 'wombat')
44
+ end
45
+
46
+ it "should login successfully" do
47
+ $fake_media_wiki.logged_in('atlasmw').should == true
48
+ end
49
+
50
+ end
51
+
52
+ describe "with an non-existent username" do
53
+
54
+ it "should raise an error" do
55
+ lambda do
56
+ @gateway.login('bogususer', 'sekrit')
57
+ end.should raise_error(StandardError)
58
+ end
59
+
60
+ end
61
+
62
+ describe "with an incorrect password" do
63
+
64
+ it "should raise an error" do
65
+ lambda do
66
+ @gateway.login('atlasmw', 'sekrit')
67
+ end.should raise_error(StandardError)
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+ describe "#get_token" do
75
+
76
+ describe "when not logged in" do
77
+
78
+ describe "requesting an edit token" do
79
+
80
+ before do
81
+ @token = @gateway.get_token('edit', 'Main Page')
82
+ end
83
+
84
+ it "should return a blank token" do
85
+ @token.should_not == nil
86
+ @token.should == "+\\"
87
+ end
88
+
89
+ end
90
+
91
+ describe "requesting an import token" do
92
+
93
+ it "should raise an error" do
94
+ lambda do
95
+ @gateway.get_token('import', 'Main Page')
96
+ end.should raise_error(StandardError)
97
+ end
98
+
99
+ end
100
+
101
+ end
102
+
103
+ describe "when logged in as admin user" do
104
+
105
+ before do
106
+ @gateway.login('atlasmw', 'wombat')
107
+ end
108
+
109
+ describe "requesting an edit token for a single page" do
110
+
111
+ before do
112
+ @token = @gateway.get_token('edit', 'Main Page')
113
+ end
114
+
115
+ it "should return a token" do
116
+ @token.should_not == nil
117
+ @token.should_not == "+\\"
118
+ end
119
+
120
+ end
121
+
122
+ describe "requesting an edit token for multiple pages" do
123
+
124
+ before do
125
+ @token = @gateway.get_token('edit', "Main Page|Another Page")
126
+ end
127
+
128
+ it "should return a token" do
129
+ @token.should_not == nil
130
+ @token.should_not == "+\\"
131
+ end
132
+
133
+ end
134
+
135
+ describe "requesting an import token" do
136
+
137
+ before do
138
+ @token = @gateway.get_token('import', 'Main Page')
139
+ end
140
+
141
+ it "should return a token" do
142
+ @token.should_not == nil
143
+ @token.should_not == "+\\"
144
+ end
145
+
146
+ end
147
+
148
+ end
149
+
150
+ end
151
+
152
+ describe "#get" do
153
+
154
+ describe "for an existing wiki page" do
155
+
156
+ before do
157
+ $fake_media_wiki.add_page("page/title", "Wiki-text content")
158
+ end
159
+
160
+ it "returns raw page content" do
161
+ @gateway.get("page/title").should == "Wiki-text content"
162
+ end
163
+
164
+ end
165
+
166
+ describe "for a missing wiki page" do
167
+
168
+ it "returns nil" do
169
+ @gateway.get("page/missing").should be_nil
170
+ end
171
+
172
+ end
173
+
174
+ describe "for root (/)" do
175
+
176
+ it "returns nil" do
177
+ @gateway.get("").should be_nil
178
+ end
179
+
180
+ end
181
+
182
+ end
183
+
184
+ describe "#render" do
185
+
186
+ describe "for an existing wiki page" do
187
+
188
+ before do
189
+ @pages = @gateway.render('Main Page')
190
+ end
191
+
192
+ it "should return the page content" do
193
+ expected = "Sample <B>HTML</B> content"
194
+ @pages.to_s.should == expected
195
+ end
196
+ end
197
+
198
+ describe "for a missing wiki page" do
199
+
200
+ before do
201
+ @pages = @gateway.render('Invalidpage')
202
+ end
203
+
204
+ it "should return nil" do
205
+ @pages.should == nil
206
+ end
207
+
208
+ end
209
+
210
+ end
211
+
212
+ describe "#semantic_query" do
213
+
214
+ before do
215
+ @response = @gateway.semantic_query('[[place-id::123]]', ['mainlabel=Page'])
216
+ end
217
+
218
+ it "should return an HTML string" do
219
+ @response.should == "Sample <B>HTML</B> content"
220
+ end
221
+
222
+ end
223
+
224
+ describe "#create" do
225
+
226
+ before do
227
+ @gateway.login('atlasmw', 'wombat')
228
+ end
229
+
230
+ describe "when creating a new page" do
231
+
232
+ before do
233
+ @page = @gateway.create("A New Page", "Some content")
234
+ end
235
+
236
+ it "should create the page" do
237
+ expected = <<-XML
238
+ <api>
239
+ <edit new='' result='Success' pageid='5' title='A New Page' oldrevid='0' newrevid='5'/>
240
+ </api>
241
+ XML
242
+ Hash.from_xml(@page.to_s).should == Hash.from_xml(expected)
243
+ end
244
+
245
+ end
246
+
247
+ describe "when creating a page that already exists" do
248
+
249
+ before do
250
+ @existing_page = $fake_media_wiki.add_page("The Page", "Some content")
251
+ end
252
+
253
+ describe "and the 'overwrite' option is set" do
254
+
255
+ before do
256
+ @new_page = @gateway.create("The Page", "Some new content", :summary => "The summary", :overwrite => true)
257
+ end
258
+
259
+ it "should overwrite the existing page" do
260
+ expected = <<-XML
261
+ <api>
262
+ <edit result='Success' pageid='#{@existing_page[:pageid] + 1}' title='The Page' oldrevid='#{@existing_page[:pageid]}' newrevid='#{@existing_page[:pageid] + 1}'/>
263
+ </api>
264
+ XML
265
+ Hash.from_xml(@new_page.to_s).should == Hash.from_xml(expected)
266
+ end
267
+
268
+ end
269
+
270
+ describe "and the 'overwrite' option is not set" do
271
+
272
+ it "should raise an error" do
273
+ lambda do
274
+ @gateway.create("The Page", "Some new content")
275
+ end.should raise_error(StandardError)
276
+ end
277
+
278
+ end
279
+
280
+ end
281
+
282
+ end
283
+
284
+ describe "#delete" do
285
+
286
+ describe "when logged in as admin" do
287
+
288
+ describe "and the page exists" do
289
+ def delete_response
290
+ <<-XML
291
+ <api>
292
+ <delete title='Deletable Page' reason='Default reason'/>
293
+ </api>
294
+ XML
295
+ end
296
+
297
+ before do
298
+ @gateway.login("atlasmw", "wombat")
299
+ @gateway.create("Deletable Page", "Some content")
300
+ @page = @gateway.delete("Deletable Page")
301
+ end
302
+
303
+ it "should delete the page" do
304
+ Hash.from_xml(@page.to_s) == Hash.from_xml(delete_response)
305
+ end
306
+ end
307
+
308
+ describe "and the page does not exist" do
309
+
310
+ before do
311
+ @gateway.login("atlasmw", "wombat")
312
+ end
313
+
314
+ it "should raise an error" do
315
+ lambda do
316
+ @gateway.delete("Missing Page")
317
+ end.should raise_error(StandardError)
318
+ end
319
+ end
320
+ end
321
+
322
+ describe "when not logged in" do
323
+
324
+ before do
325
+ @gateway.create("Deletable Page", "Some content")
326
+ end
327
+
328
+ it "should raise an error" do
329
+ lambda do
330
+ @gateway.delete("Deletable Page")
331
+ end.should raise_error(StandardError)
332
+ end
333
+
334
+ end
335
+
336
+ end
337
+
338
+ describe "#import" do
339
+
340
+ def import_file
341
+ File.dirname(__FILE__) + "/import-test-data.xml"
342
+ end
343
+
344
+ describe "when not logged in" do
345
+
346
+ it "should raise an error" do
347
+ lambda do
348
+ @gateway.import(import_file)
349
+ end.should raise_error(StandardError)
350
+ end
351
+
352
+ end
353
+
354
+ describe "when logged in as admin" do
355
+
356
+ def import_response
357
+ <<-XML
358
+ <api>
359
+ <import>
360
+ <page title='Main Page' ns='0' revisions='0'/>
361
+ <page title='Template:Header' ns='10' revisions='1'/>
362
+ </import>
363
+ </api>
364
+ XML
365
+ end
366
+
367
+ before do
368
+ @gateway.login("atlasmw", "wombat")
369
+ @page = @gateway.import(import_file)
370
+ end
371
+
372
+ it "should import content" do
373
+ Hash.from_xml(@page.to_s) == Hash.from_xml(import_response)
374
+ end
375
+
376
+ end
377
+
378
+ end
379
+
380
+ describe "#export" do
381
+
382
+ def export_data
383
+ page = $fake_media_wiki.page_by_title("Main Page")
384
+ <<-XML
385
+ <mediawiki>
386
+ <page>
387
+ <title>#{page[:title]}</title>
388
+ <id>#{page[:pageid]}</id>
389
+ <revision>
390
+ <id>#{page[:pageid]}</id>
391
+ <text>#{page[:content]}</text>
392
+ </revision>
393
+ </page>
394
+ </mediawiki>
395
+ XML
396
+ end
397
+
398
+ before do
399
+ @page = @gateway.export("Main Page")
400
+ end
401
+
402
+ it "should return export data for the page" do
403
+ Hash.from_xml(@page.to_s).should == Hash.from_xml(export_data)
404
+ end
405
+
406
+ end
407
+
408
+ describe "#list" do
409
+
410
+ before do
411
+ $fake_media_wiki.reset
412
+ end
413
+
414
+ describe "with an empty key" do
415
+
416
+ before do
417
+ @list = @gateway.list("")
418
+ end
419
+
420
+ it "should list all pages" do
421
+ @list.sort.should == [ "Book:Italy", "Level/Level/Index", "Main 2", "Main Page" ]
422
+ end
423
+
424
+ end
425
+
426
+ describe "with a namespace as the key" do
427
+
428
+ before do
429
+ @list = @gateway.list("Book:")
430
+ end
431
+
432
+ it "should list all pages in the namespace" do
433
+ @list.should == [ "Book:Italy" ]
434
+ end
435
+
436
+ end
437
+
438
+ describe "with a partial title as the key" do
439
+
440
+ before do
441
+ @list = @gateway.list("Main")
442
+ end
443
+
444
+ it "should list all pages in the main namespace that start with key" do
445
+ @list.sort.should == [ "Main 2", "Main Page" ]
446
+ end
447
+
448
+ end
449
+
450
+ end
451
+
452
+ describe "#search" do
453
+
454
+ before do
455
+ $fake_media_wiki.reset
456
+ @gateway.create("Search Test", "Foo KEY Blah")
457
+ @gateway.create("Search Test 2", "Zomp KEY Zorg")
458
+ @gateway.create("Book:Search Test", "Bar KEY Baz")
459
+ @gateway.create("Sandbox:Search Test", "Evil KEY Evil")
460
+ end
461
+
462
+ describe "with an empty key" do
463
+
464
+ it "should raise an error" do
465
+ lambda do
466
+ @gateway.search("")
467
+ end.should raise_error(StandardError)
468
+ end
469
+
470
+ end
471
+
472
+ describe "with a valid key and no namespaces" do
473
+
474
+ before do
475
+ @search = @gateway.search("KEY")
476
+ end
477
+
478
+ it "should list all matching pages in the main namespace" do
479
+ @search.should == [ "Search Test", "Search Test 2" ]
480
+ end
481
+
482
+ end
483
+
484
+ describe "with a valid key and a namespace string" do
485
+
486
+ before do
487
+ @search = @gateway.search("KEY", "Book")
488
+ end
489
+
490
+ it "should list all matching pages in the specified namespaces" do
491
+ @search.should == [ "Book:Search Test" ]
492
+ end
493
+
494
+ end
495
+
496
+ describe "with a valid key and a namespace array" do
497
+
498
+ before do
499
+ @search = @gateway.search("KEY", ["Book", "Sandbox"])
500
+ end
501
+
502
+ it "should list all matching pages in the specified namespaces" do
503
+ @search.should == [ "Sandbox:Search Test", "Book:Search Test" ]
504
+ end
505
+
506
+ end
507
+
508
+ end
509
+
510
+ describe "#namespaces_by_prefix" do
511
+
512
+ before do
513
+ $fake_media_wiki.reset
514
+ @namespaces = @gateway.namespaces_by_prefix
515
+ end
516
+
517
+ it "should list all namespaces" do
518
+ @namespaces.should == { "" => 0, "Book" => 100, "Sandbox" => 200}
519
+ end
520
+
521
+ end
522
+
523
+ describe "#extensions" do
524
+
525
+ before do
526
+ $fake_media_wiki.reset
527
+ @extensions = @gateway.extensions
528
+ end
529
+
530
+ it "should list all namespaces" do
531
+ @extensions.should == { "FooExtension" => "r1", "BarExtension" => "r2" }
532
+ end
533
+
534
+ end
535
+ end