Class: WikiBot::Page
- Inherits:
-
Object
- Object
- WikiBot::Page
- Defined in:
- lib/wikibot/core/page.rb
Direct Known Subclasses
Defined Under Namespace
Classes: WriteError
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#wiki_bot ⇒ Object
writeonly
Sets the attribute wiki_bot.
Instance Method Summary collapse
-
#categories(show = :all) ⇒ Object
Get page categories.
- #category_names(show = :all) ⇒ Object
- #content(reload = false) ⇒ Object
-
#exists?(reload = false) ⇒ Boolean
Read from page.
-
#initialize(wiki_bot, name) ⇒ Page
constructor
A new instance of Page.
-
#text(reload = false) ⇒ Object
Parse page content.
-
#writable? ⇒ Boolean
Write to page.
- #write(text, summary, section = nil, minor = false) ⇒ Object
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
#name ⇒ Object (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
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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/wikibot/core/page.rb', line 63 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
101 102 103 |
# File 'lib/wikibot/core/page.rb', line 101 def category_names(show = :all) @category_names ||= categories(show).map{ |c| c.name } end |
#content(reload = false) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/wikibot/core/page.rb', line 30 def content(reload = false) @content = nil if reload @content ||= begin return "" unless exists? data = { :action => :query, :titles => @name, :prop => :revisions, :rvprop => :content } @wiki_bot.query_api(:get, data).query.pages.page.revisions.rev.content end end |
#exists?(reload = false) ⇒ Boolean
Read from page
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/wikibot/core/page.rb', line 18 def exists?(reload = false) @exists = nil if reload @exists ||= begin data = { :action => :query, :titles => @name } !@wiki_bot.query_api(:get, data).query.pages.page.include?("missing") end end |
#text(reload = false) ⇒ Object
Parse page content
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/wikibot/core/page.rb', line 47 def text(reload = false) @text = nil if reload @text ||= begin return "" unless exists? data = { :action => :parse, :page => @name } @wiki_bot.query_api(:get, data).parse.text.content end end |
#writable? ⇒ Boolean
Write to page
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/wikibot/core/page.rb', line 107 def writable? return false if @wiki_bot.debug or @wiki_bot.readonly return true unless content # 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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/wikibot/core/page.rb', line 129 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" true end |