Module: MediaWiki::Query::Properties::Files

Included in:
MediaWiki::Query::Properties
Defined in:
lib/mediawiki/query/properties/files.rb

Instance Method Summary collapse

Instance Method Details

#get_all_duplicated_files(limit = @query_limit_default) ⇒ Array<String>

Gets all duplicated files on the wiki.

Parameters:

  • limit (Fixnum) (defaults to: @query_limit_default)

    The maximum number of files to get.

Returns:

  • (Array<String>)

    All duplicate file titles on the wiki.

Since:

  • 0.8.0



34
35
36
37
38
39
40
41
42
# File 'lib/mediawiki/query/properties/files.rb', line 34

def get_all_duplicated_files(limit = @query_limit_default)
  params = {
    generator: 'allimages',
    prop: 'duplicatedfiles',
    dflimit: get_limited(limit)
  }

  query_ary_irrelevant_keys(params, 'pages', 'title')
end

#get_duplicated_files_of(title, limit = @query_limit_default) ⇒ Array<String>

Gets the duplicated files of the title.

Parameters:

  • title (String)

    The title to get duplicated files of.

  • limit (Fixnum) (defaults to: @query_limit_default)

    The maximum number of files to get.

Returns:

  • (Array<String>)

    Array of all the duplicated file names.

See Also:

Since:

  • 0.8.0



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mediawiki/query/properties/files.rb', line 14

def get_duplicated_files_of(title, limit = @query_limit_default)
  params = {
    prop: 'duplicatefiles',
    titles: title,
    dflimit: get_limited(limit)
  }

  query(params) do |return_val, query|
    query['pages'].each do |_, c|
      next unless c['duplicatefies']
      c['duplicatefiles'].each { |f| return_val << f['name'] }
    end
  end
end

#get_image_bytes(image) ⇒ Fixnum, Nil

Gets the size of an image in bytes.

Parameters:

  • image (String)

    The image to get info for.

Returns:

  • (Fixnum)

    The number of bytes.

  • (Nil)

    If the image does not exist.

See Also:

Since:

  • 0.8.0



50
51
52
53
54
# File 'lib/mediawiki/query/properties/files.rb', line 50

def get_image_bytes(image)
  response = get_image_sizes(image)
  return nil if response.nil?
  response['size']
end

#get_image_dimensions(image) ⇒ Array<Fixnum>, Nil

TODO:

Use data_types library to store the pair of width, height.

Gets the dimensions of an image as width, height.

Parameters:

  • image (String)

    The image to get info for.

Returns:

  • (Array<Fixnum>)

    The dimensions as width, height.

  • (Nil)

    If the image does not exist.

See Also:

Since:

  • 0.8.0



63
64
65
66
67
# File 'lib/mediawiki/query/properties/files.rb', line 63

def get_image_dimensions(image)
  response = get_image_sizes(image)
  return nil if response.nil?
  [response['width'], response['height']]
end

#get_image_sizes(image) ⇒ Hash<String, Fixnum>, Nil (private)

Gets the imageinfo property ‘size’ for the image.

Parameters:

  • image (String)

    The image to get info for.

Returns:

  • (Hash<String, Fixnum>)

    A hash of the size, width, and height.

  • (Nil)

    If the image does not exist.

See Also:

Since:

  • 0.8.0



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mediawiki/query/properties/files.rb', line 77

def get_image_sizes(image)
  params = {
    action: 'query',
    prop: 'imageinfo',
    iiprop: 'size',
    titles: image
  }

  response = post(params)
  pageid = response['query']['pages'].keys.find(MediaWiki::Constants::MISSING_PAGEID_PROC) { |id| id != '-1'}
  return nil if pageid == '-1'
  response['query']['pages'][pageid]['imageinfo'].reduce({}, :merge)
end