Class: WikiBot::Category

Inherits:
Page
  • Object
show all
Defined in:
lib/category.rb

Instance Attribute Summary

Attributes inherited from Page

#name, #wiki_bot

Instance Method Summary collapse

Methods inherited from Page

#categories, #category_names, #content, #initialize, #text, #write

Constructor Details

This class inherits a constructor from WikiBot::Page

Instance Method Details

#category_infoObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/category.rb', line 3

def category_info
  @category_info ||= begin
    data = {
      :action => :query,
      :titles => @name,
      :prop => :categoryinfo
    }

    # The query API returns nothing for an empty cat, so we'll return a hash with all the normal
    # properties set to 0 instead
    empty_cat = { :pages => 0, :size => 0, :files => 0, :subcats => 0, :hidden => "" }.to_openhash
    @wiki_bot.query_api(:get, data).query.pages.page.categoryinfo || empty_cat
  end
end

#count(include_subcats = false) ⇒ Object

Returns a hash of how many pages live in a category



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/category.rb', line 34

def count(include_subcats = false)
  @count ||= begin
    out = {}
    ci = category_info

    out[@name] = {
      :pages => ci.pages.to_i
    }

    if include_subcats and ci.subcats.to_i > 0
      out[@name][:subcats] = {}
      members.each do |m|
        out[@name][:subcats].merge! self.class.new(@wiki_bot, m["title"]).count(include_subcats)
      end
    end

    out
  end
end

#members(sort = :sortkey, dir = :desc, namespace = nil) ⇒ Object



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

def members(sort = :sortkey, dir = :desc, namespace = nil)
  @members ||= begin
    data = {
      :action => :query,
      :list => :categorymembers,
      :cmtitle => @name,
      :cmsort => sort,
      :cmdir => dir,
      :cmnamespace => namespace
    }

    @wiki_bot.query_api(:get, data).query.categorymembers.cm.map { |m| Page.new(@wiki_bot, m["title"]) }
  end
end