Class: MediaWiki::FakeWiki::ApiPages

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

Instance Method Summary collapse

Constructor Details

#initializeApiPages

Returns a new instance of ApiPages.



485
486
487
488
489
# File 'lib/media_wiki/fake_wiki.rb', line 485

def initialize
  @page_id = 0
  @pages = {}
  @namespaces = { "" => 0 }
end

Instance Method Details

#add(title, content, redirect = false) ⇒ Object



503
504
505
506
507
508
509
510
511
512
513
# File 'lib/media_wiki/fake_wiki.rb', line 503

def add(title, content, redirect=false)
  @page_id += 1
  dummy, prefix = title.split(":", 2).reverse
  @pages[title] = {
    :pageid => @page_id,
    :namespace => namespaces_by_prefix[prefix || ""],
    :title => title,
    :content => content,
    :redirect => redirect
  }
end

#add_namespace(id, prefix) ⇒ Object



491
492
493
# File 'lib/media_wiki/fake_wiki.rb', line 491

def add_namespace(id, prefix)
  @namespaces[prefix] = id
end

#delete(title) ⇒ Object



532
533
534
# File 'lib/media_wiki/fake_wiki.rb', line 532

def delete(title)
  @pages.delete(title)
end

#get(title) ⇒ Object



515
516
517
# File 'lib/media_wiki/fake_wiki.rb', line 515

def get(title)
  @pages[title]
end

#list(prefix) ⇒ Object



519
520
521
522
523
# File 'lib/media_wiki/fake_wiki.rb', line 519

def list(prefix)
  @pages.select do |key, page|
    key =~ /^#{prefix}/
  end
end

#namespaces_by_idObject



499
500
501
# File 'lib/media_wiki/fake_wiki.rb', line 499

def namespaces_by_id
  @namespaces.invert
end

#namespaces_by_prefixObject



495
496
497
# File 'lib/media_wiki/fake_wiki.rb', line 495

def namespaces_by_prefix
  @namespaces
end

#search(searchkey, namespaces) ⇒ Object

Raises:



525
526
527
528
529
530
# File 'lib/media_wiki/fake_wiki.rb', line 525

def search(searchkey, namespaces)
  raise ApiError.new("srparam-search", "empty search string is not allowed") if searchkey.empty?
  @pages.select do |key, page|
    page[:content] =~ /#{searchkey}/ and namespaces.include? page[:namespace].to_s
  end
end

#undelete(title) ⇒ Object



536
537
538
539
540
541
542
543
# File 'lib/media_wiki/fake_wiki.rb', line 536

def undelete(title)
  if @pages[title]
    0
  else
    add(title, "Undeleted content")
    1
  end
end