Class: MediaWiki::FakeWiki::App

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/media_wiki/fake_wiki.rb

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



16
17
18
19
# File 'lib/media_wiki/fake_wiki.rb', line 16

def initialize
  reset
  super
end

Instance Method Details

#actionObject



174
175
176
177
178
179
# File 'lib/media_wiki/fake_wiki.rb', line 174

def action
  [:userrights].each do |action_type|
    return send(action_type)
  end
  halt(404, "Page not found")
end

#add_user(username, password, domain, is_admin) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/media_wiki/fake_wiki.rb', line 49

def add_user(username, password, domain, is_admin)
  @users[username] = {
    :userid => next_id,
    :username => username,
    :password => password,
    :domain => domain,
    :is_admin => is_admin
  }
end

#allpagesObject



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/media_wiki/fake_wiki.rb', line 227

def allpages
  api_response do |_|
    _.query do
      _.allpages do
        prefix = params[:apprefix]
        namespace = @pages.namespaces_by_id[params[:apnamespace].to_i]
        prefix = "#{namespace}:#{prefix}" unless namespace.empty?
        @pages.list(prefix).each do |key, page|
          _.p(nil, { :title => page[:title], :ns => page[:namespace], :id => page[:pageid] })
        end
      end
    end
  end
end

#api_error_response(code, info) ⇒ Object



308
309
310
311
312
313
314
# File 'lib/media_wiki/fake_wiki.rb', line 308

def api_error_response(code, info)
  api_response do |_|
    _.error(nil,
            :code => code,
            :info => info)
  end
end

#api_response(api_attr = {}, &block) ⇒ Object



302
303
304
305
306
# File 'lib/media_wiki/fake_wiki.rb', line 302

def api_response(api_attr = {}, &block)
  Nokogiri::XML::Builder.new do |_|
    _.api(api_attr, &block)
  end.to_xml
end

#createaccountObject



436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/media_wiki/fake_wiki.rb', line 436

def createaccount
  api_response do |_|
    @token.request(user)

    if params[:token] && !params[:token].empty?
      @token.validate_admin
      add_user(params[:name], params[:password], 'local', false)
      _.createaccount(:token => @token.createusertoken, :userid => @users.length, :username => params[:name], :result => 'success')
    else
      _.createaccount(:token => @token.createusertoken, :result => 'needtoken')
    end
  end
end

#deleteObject

Raises:



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/media_wiki/fake_wiki.rb', line 125

def delete
  @token.validate_admin

  title = params[:title]
  raise ApiError.new("missingtitle", "The page you requested doesn't exist") unless @pages.get(title)
  @pages.delete(title)

  api_response do |_|
    _.delete(nil, {:title => title, :reason => "Default reason"})
  end
end

#editObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/media_wiki/fake_wiki.rb', line 105

def edit
  @token.validate

  title = params[:title]
  current_page = @pages.get(title)
  validate_page_overwrite(current_page)

  new_page = @pages.add(title, params[:text])
  page_info = {:result => "Success", :pageid => new_page[:pageid], :title => new_page[:title], :newrevid => new_page[:pageid]}
  if current_page
    page_info.merge!(:oldrevid => current_page[:pageid])
  else
    page_info.merge!(:new => "", :oldrevid => 0)
  end

  api_response do |_|
    _.edit(nil, page_info)
  end
end

#exportObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/media_wiki/fake_wiki.rb', line 195

def export
  Nokogiri::XML::Builder.new do |_|
    _.mediawiki do
      requested_page_titles.each do |requested_title|
        page = @pages.get(requested_title)
        _.page do
          _.title(page[:title])
          _.id(page[:pageid])
          _.revision do
            _.id(page[:pageid])
            _.text!(page[:content])
          end
        end
      end
    end
  end.to_xml
end

#extensionsObject



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/media_wiki/fake_wiki.rb', line 288

def extensions
  api_response do |_|
    _.query do
      _.extensions do
        @extensions.each do |name, version|
          attr = { :version => version }
          attr[:name] = name unless name.empty?
          _.ext(name, attr)
        end
      end
    end
  end
end

#get_infoObject



328
329
330
331
332
333
334
335
336
337
338
# File 'lib/media_wiki/fake_wiki.rb', line 328

def get_info
  query_pages do |_, title, page|
    attributes = { :title => title, :ns => '0'}
    if page.nil?
      attributes[:missing] = ""
    else
      attributes[:redirect] = "" if page[:redirect]
    end
    _.page(nil, attributes)
  end
end

#get_revisionsObject



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/media_wiki/fake_wiki.rb', line 340

def get_revisions
  query_pages do |_, title, page|
    if page.nil?
      _.page(nil, { :title => title, :ns => '0', :missing => "" })
    else
      page = page.dup
      content = page.delete(:content)
      _.page(page.merge({ :ns => 0 })) do
        _.revisions do
          _.rev(content)
        end
      end
    end
  end
end

#get_tokenObject



373
374
375
376
377
378
379
380
# File 'lib/media_wiki/fake_wiki.rb', line 373

def get_token
  token_str = @token.request(user)
  query_pages do |_, title, page|
    page = page ? page.dup : {}
    page[params[:intoken] + "token"] = token_str if token_str
    _.page(nil, page.merge({ :ns => 0 }))
  end
end

#get_undelete_tokenObject



382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/media_wiki/fake_wiki.rb', line 382

def get_undelete_token
  @token.set_type 'undelete'
  token_str = @token.request(user)
  api_response do |_|
    _.query do
      _.deletedrevs do
        requested_page_titles.select {|title| ! @pages.get(title) }.each do |title|
          _.page(nil, { :title => title, :token => token_str })
        end
      end
    end
  end
end

#get_userrights_token(username) ⇒ Object



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/media_wiki/fake_wiki.rb', line 396

def get_userrights_token(username)
  @token.set_type 'userrights'
  token_str = @token.request(user)

  user_to_manage = @users[username]

  if user_to_manage
    api_response do |_|
      _.query do
        _.users do
          _.user(nil, { :name => user_to_manage[:username], :userrightstoken => token_str })
        end
      end
    end
  else
    api_response do |_|
      _.error(nil, { :code => 'nosuchuser', :info => "The user '#{params[:ususer].to_s}' does not exist"} )
    end
  end
end

#handle_requestObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/media_wiki/fake_wiki.rb', line 71

def handle_request
  begin
    halt(503, "Maxlag exceeded") if params[:maxlag].to_i < 0

    @token = ApiToken.new(params)
    action = params[:action]
    if respond_to?(action)
      content_type "application/xml"
      return send(action)
    end

    halt(404, "Page not found")
  rescue ApiError => e
    return api_error_response(e.code, e.message)
  end
end

#importObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/media_wiki/fake_wiki.rb', line 88

def import
  @token.validate_admin

  api_response do |_|
    _.import do
      _.page(nil, :title => "Main Page", :ns => 0, :revisions => 0)
      _.page(nil, :title => "Template:Header", :ns => 10, :revisions => 1)
    end
  end
end

#listObject



213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/media_wiki/fake_wiki.rb', line 213

def list
  list_type = params[:list].to_sym

  # api.php?action=query&list=users&ususers=Bob&ustoken=userrights
  if list_type == :users && params[:ustoken] && params[:ususers]
    # This "list" is actually a request for a user rights token
    return get_userrights_token(params[:ususers])
  end

  # This is a real list
  return send(list_type) if respond_to?(list_type)
  halt(404, "Page not found")
end

#logged_in(username) ⇒ Object



59
60
61
# File 'lib/media_wiki/fake_wiki.rb', line 59

def logged_in(username)
  @logged_in_users.include?(username)
end

#loginObject



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/media_wiki/fake_wiki.rb', line 417

def 
  user = @users[params[:lgname]]
  if user and user[:domain] == params[:lgdomain]
    if params[:lgpassword] == user[:password]
      @logged_in_users << user[:username]
      response.set_cookie('login', user[:username])
      result = { :result => "Success", :lguserid => "1", :lgusername => "Atlasmw"}
    else
      result = { :result => "WrongPass" }
    end
  else
    result = { :result => "NotExists" }
  end

  api_response do |_|
    _.(nil, result)
  end
end

#metaObject



255
256
257
258
259
# File 'lib/media_wiki/fake_wiki.rb', line 255

def meta
  meta_type = params[:meta].to_sym
  return send(meta_type) if respond_to?(meta_type)
  halt(404, "Page not found")
end

#namespacesObject



274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/media_wiki/fake_wiki.rb', line 274

def namespaces
  api_response do |_|
    _.query do
      _.namespaces do
        @pages.namespaces_by_prefix.each do |prefix, id|
          attr = { :id => id }
          attr[:canonical] = prefix unless prefix.empty?
          _.ns(prefix, attr)
        end
      end
    end
  end
end

#next_idObject



45
46
47
# File 'lib/media_wiki/fake_wiki.rb', line 45

def next_id
  @sequence_id += 1
end

#optionsObject



450
451
452
# File 'lib/media_wiki/fake_wiki.rb', line 450

def options
  api_response(:options => 'success')
end

#parseObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/media_wiki/fake_wiki.rb', line 157

def parse
  page = @pages.get(params[:page])
  api_response do |_|
    _.parse({ :revid => page ?  page[:pageid] : 0}) do
      if params[:page] == "Foopage"
        _.text!('Sample <B>HTML</B> content.' \
          '<img width="150" height="150" class="thumbimage" src="http://upload.wikimedia.org/foo/Ruby_logo.svg" alt="Ruby logo.svg"/>' \
          '<span class="editsection">[<a title="Edit section: Nomenclature" href="/w/index.php?title=Seat_of_local_government&amp;action=edit&amp;section=1">edit</a>]</span>' \
          '<a title="Interpreted language" href="/wiki/Interpreted_language">interpreted language</a>'
        )
      else
        _.text!('Sample <B>HTML</B> content.')
      end
    end
  end
end

#propObject



188
189
190
191
192
193
# File 'lib/media_wiki/fake_wiki.rb', line 188

def prop
  return get_revisions if params[:prop] == "revisions"
  return get_undelete_token if params[:drprop] == 'token'
  return get_token if params[:intoken]
  return get_info if params[:prop] == "info"
end

#queryObject



181
182
183
184
185
186
# File 'lib/media_wiki/fake_wiki.rb', line 181

def query
  [:prop, :export, :list, :meta].each do |query_type|
    return send(query_type) if params[query_type]
  end
  halt(404, "Page not found")
end

#query_pagesObject



316
317
318
319
320
321
322
323
324
325
326
# File 'lib/media_wiki/fake_wiki.rb', line 316

def query_pages
  api_response do |_|
    _.query do
      _.pages do
        requested_page_titles.each do |title|
          yield(_, title, @pages.get(title))
        end
      end
    end
  end
end

#requested_page_titlesObject



361
362
363
# File 'lib/media_wiki/fake_wiki.rb', line 361

def requested_page_titles
  params[:titles].split("|")
end

#resetObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/media_wiki/fake_wiki.rb', line 21

def reset
  @sequence_id = 0

  @users = {}
  add_user('atlasmw', 'wombat', 'local', true)
  add_user('nonadmin', 'sekrit', 'local', false)
  add_user('ldapuser', 'ldappass', 'ldapdomain', false)

  @pages = ApiPages.new
  @pages.add('Main Page', 'Content')
  @pages.add('Main 2', 'Content')
  @pages.add('Empty', '')
  @pages.add('Level/Level/Index', '{{#include:Foo}} {{#include:Bar}}')
  @pages.add_namespace(100, "Book")
  @pages.add('Book:Italy', 'Introduction')
  @pages.add_namespace(200, "Sandbox")
  @pages.add('Foopage', 'Content')
  @pages.add('Redirect', '#REDIRECT', true)

  @extensions = { 'FooExtension' => 'r1', 'BarExtension' => 'r2', 'Semantic MediaWiki' => '1.5' }

  @logged_in_users = []
end

#searchObject



242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/media_wiki/fake_wiki.rb', line 242

def search
  api_response do |_|
    _.query do
      _.search do
        namespaces = params[:srnamespace] ? params[:srnamespace].split('|') : [ "0" ]
        @pages.search(params[:srsearch], namespaces).first(params[:srlimit].to_i).each do |key, page|
          _.p(nil, { :title => page[:title], :ns => page[:namespace], :id => page[:pageid] })
        end
      end
    end
  end
end

#siteinfoObject



261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/media_wiki/fake_wiki.rb', line 261

def siteinfo
  if siteinfo_type = params[:siprop]
    return send(siteinfo_type) if respond_to?(siteinfo_type)
    halt(404, "Page not found")
  else
    api_response do |_|
      _.query do
        _.general(generator: "MediaWiki #{MediaWiki::VERSION}")
      end
    end
  end
end

#tokensObject



365
366
367
368
369
370
371
# File 'lib/media_wiki/fake_wiki.rb', line 365

def tokens
  @token.request(user)

  api_response do |_|
    _.tokens(:optionstoken => @token.optionstoken)
  end
end

#undeleteObject



137
138
139
140
141
142
143
144
145
# File 'lib/media_wiki/fake_wiki.rb', line 137

def undelete
  @token.validate_admin

  title = params[:title]
  revisions = @pages.undelete(title)
  api_response do |_|
    _.undelete(nil, {:title => title, :revisions => revisions})
  end
end

#uploadObject



147
148
149
150
151
152
153
154
155
# File 'lib/media_wiki/fake_wiki.rb', line 147

def upload
  @token.validate

  filename = params[:filename]
  @pages.add(filename, params[:file])
  api_response do |_|
    _.upload(nil, {:filename => filename, :result => "Success"})
  end
end

#userObject



356
357
358
359
# File 'lib/media_wiki/fake_wiki.rb', line 356

def user
  username = request.cookies['login']
  @users[username] if logged_in(username)
end

#userrightsObject



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/media_wiki/fake_wiki.rb', line 454

def userrights
  api_response do |_|
    _.userrights({:user => params[:user]}) do
      _.removed do
        params[:remove].split('|').each do |removed_group|
          _.group(removed_group)
        end
      end
      _.added do
        params[:add].split('|').each do |added_group|
          _.group(added_group)
        end
      end
    end
  end
end

#validate_page_overwrite(current_page) ⇒ Object



99
100
101
102
103
# File 'lib/media_wiki/fake_wiki.rb', line 99

def validate_page_overwrite(current_page)
  if current_page && params[:createonly]
    raise ApiError.new("articleexists", "The article you tried to create has been created already")
  end
end