Class: WikiBot::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/wikibot/core/page.rb

Direct Known Subclasses

Category

Defined Under Namespace

Classes: WriteError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wiki_bot, name) ⇒ Page

Returns a new instance of Page.



11
12
13
14
# File 'lib/wikibot/core/page.rb', line 11

def initialize(wiki_bot, name)
  @wiki_bot = wiki_bot
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/wikibot/core/page.rb', line 9

def name
  @name
end

#wiki_bot=(value) ⇒ Object (writeonly)

Sets the attribute wiki_bot

Parameters:

  • value

    the value to set the attribute wiki_bot to.



8
9
10
# File 'lib/wikibot/core/page.rb', line 8

def wiki_bot=(value)
  @wiki_bot = value
end

Instance Method Details

#categories(show = :all) ⇒ Object

Get page categories



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/wikibot/core/page.rb', line 47

def categories(show = :all)
  # Cache hidden and non-hidden categories separately
  @categories ||= begin
    @category_names = nil # Reset @category_names

    data = {
      :action => :query,
      :titles => @name,
      :prop => :categories,
      :clshow => "!hidden"
    }

    categories = @wiki_bot.query_api(:get, data).query.pages.page.categories.andand.cl || []

    categories = categories.inject([]) do |memo, category|
      memo.push(WikiBot::Category.new(@wiki_bot, category["title"]))
    end
    
    data = {
      :action => :query,
      :titles => @name,
      :prop => :categories,
      :clshow => "hidden"
    }

    hidden_categories = @wiki_bot.query_api(:get, data).query.pages.page.categories.andand.cl || []
    hidden_categories = hidden_categories.inject([]) do |memo, category|
      memo.push(WikiBot::Category.new(@wiki_bot, category["title"]))
    end

    {:nonhidden => categories, :hidden => hidden_categories}
  end

  show = :all unless [:all, :hidden, :nonhidden].include? show
  return @categories[:nonhidden] + @categories[:hidden] if show == :all
  @categories[show]
end

#category_names(show = :all) ⇒ Object



85
86
87
# File 'lib/wikibot/core/page.rb', line 85

def category_names(show = :all)
  @category_names ||= categories(show).map{ |c| c.name }
end

#content(reload = false) ⇒ Object

Read from page



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/wikibot/core/page.rb', line 18

def content(reload = false)
  @content = nil if reload
  @content ||= begin
    data = {
      :action => :query,
      :titles => @name,
      :prop => :revisions,
      :rvprop => :content
    }

    @wiki_bot.query_api(:get, data).query.pages.page.revisions.rev.content
  end
end

#text(reload = false) ⇒ Object

Parse page content



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wikibot/core/page.rb', line 33

def text(reload = false)
  @text = nil if reload
  @text ||= begin
    data = {
      :action => :parse,
      :page => @name
    }

    @wiki_bot.query_api(:get, data).parse.text.content
  end
end

#writable?Boolean

Write to page

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/wikibot/core/page.rb', line 91

def writable?
  return false if @wiki_bot.debug or @wiki_bot.readonly
  
  # Deny all bots
  return false if content.include?("{{nobots}}") or content.include?("{{bots|deny=all}}") or content.include?("{{bots|allow=none}}")

  # Allow all bots
  return true if content.include?("{{bots}}") or content.include?("{{bots|allow=all}}") or content.include?("{{bots|deny=none}}")
  
  # Username specific white/blacklist
  if content =~ /\{\{bots\|(allow|deny)=([^\}]+)\}\}/
    allow = Regexp.last_match(1)
    usernames = Regexp.last_match(2).split(",")

    return (allow == "allow") if usernames.include?(@wiki_bot.config.username)
    return allow != "allow"
  end

  return true
end

#write(text, summary, section = nil, minor = false) ⇒ Object

Raises:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/wikibot/core/page.rb', line 112

def write(text, summary, section = nil, minor = false)
  return false unless @wiki_bot.logged_in?
  return false unless writable? 

  data = {
    :action => :edit,
    :title => @name,

    :text => text,
    :token => @wiki_bot.edit_token,
    :summary => summary,
    :recreate => 1,
    :bot => 1
  }

  data[:section] = section if !section.nil?
  data[:minor] = 1 if minor
  data[:notminor] = 1 if !minor

  result = @wiki_bot.query_api(:post, data)
  status = result.edit.result
  @wiki_bot.page_writes += 1
  raise WriteError, status unless status == "Success"
end