module MediaWiki::Query::Properties::Pages

Public Instance Methods

can_i_read?(title) click to toggle source

Gets whether the current user (can be anonymous) can read the page. @param (see get_categories_in_page) @see (see do_i_watch?) @since 0.8.0 @return [Boolean] Whether the user can read the page. @return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 105
def can_i_read?(title)
  page_info_contains_key(title, 'readable', 'readable')
end
do_i_watch?(title) click to toggle source

Gets whether the current user watches the page. @param (see get_categories_in_page) @see www.mediawiki.org/wiki/API:Info MediaWiki Info API Docs @since 0.8.0 @return [Boolean] Whether the user watches the page. @return [Boolean] False if the user is not logged in. @return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 94
def do_i_watch?(title)
  return false unless @logged_in
  page_info_contains_key(title, 'watched', 'watched')
end
get_categories_in_page(title) click to toggle source

Gets all categories in the page. @param title [String] The page title. @see www.mediawiki.org/wiki/API:Property/Categories MediaWiki Categories Property API Docs @since 0.8.0 @return [Array<String>] All the categories @return [Nil] If the title does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 14
def get_categories_in_page(title)
  params = {
    prop: 'categories',
    titles: title
  }

  query(params) do |return_val, query|
    pageid = query['pages'].keys.find(MediaWiki::Constants::MISSING_PAGEID_PROC) { |id| id != '-1' }
    return if query['pages'][pageid].key?('missing')
    return_val.concat(query['pages'][pageid].fetch('categories', []).collect { |c| c['title'] })
  end
end
get_display_title(title) click to toggle source

Gets the way the title is actually displayed, after any in-page changes to its display, e.g., using a template to make the first letter lowercase, in cases like iPhone. @param (see get_categories_in_page) @see (see do_i_watch?) @since 0.8.0 @return [String] The page's display title. @return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 146
def get_display_title(title)
  page_info_get_val(title, 'displaytitle', 'displaytitle')
end
get_id(title) click to toggle source

Gets the revision ID for the given page. @param (see get_categories_in_page) @see (see get_text) @since 0.8.0 @return [Fixnum] The page's ID @return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 54
def get_id(title)
  params = {
    action: 'query',
    prop: 'revisions',
    rvprop: 'content',
    titles: title
  }

  post(params)['query']['pages'].keys.find { |id| id != '-1' }&.to_i
end
get_images_in_page(title, limit = @query_limit_default) click to toggle source

Gets all of the images in the given page. @param (see get_external_links) @see www.mediawiki.org/wiki/API:Images MediaWiki Images API Docs @since 0.8.0 @return [Array<String>] All of the image titles in the page. @return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 195
def get_images_in_page(title, limit = @query_limit_default)
  params = {
    prop: 'images',
    titles: title,
    imlimit: get_limited(limit)
  }

  query(params) do |return_val, query|
    pageid = query['pages'].keys.find { |id| id != '-1' }
    return unless pageid
    return_val.concat(query['pages'][pageid]['images'].collect { |img| img['title'] })
  end
end
get_number_of_watchers(title) click to toggle source

Gets the number of users that watch the given page. @param (see get_categoeries_in_page) @see (see do_i_watch?) @since 0.8.0 @return [Fixnum] The number of watchers. @return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 135
def get_number_of_watchers(title)
  page_info_get_val(title, 'watchers', 'watchers')
end
get_other_langs_of_page(title, limit = @query_limit_default) click to toggle source

Gets a hash of data for the page in every language that it is available in. This includes url, language name, autonym, and its title. This method does not work with the Translate extension. @param (see get_external_links) @see www.mediawiki.org/wiki/API:Langlinks MediaWiki Langlinks API Docs @since 0.8.0 @return [Hash] The data described previously. @return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 258
def get_other_langs_of_page(title, limit = @query_limit_default)
  params = {
    prop: 'langlinks',
    titles: title,
    lllimit: get_limited(limit),
    llprop: 'url|langname|autonym'
  }

  query(params) do |return_val, query|
    pageid = query['pages'].keys.find { |id| id != '-1' }
    return unless pageid
    langlinks = query['pages'][pageid].fetch('langlinks', [])
    langlinks.each do |l|
      return_val[l['lang'].to_sym] = {
        url: l['url'],
        langname: l['langname'],
        autonym: l['autonym'],
        title: l['*']
      }
    end
  end
end
get_page_size(title) click to toggle source

Gets the size, in bytes, of the page. @param (see get_categories_in_page) @see (see do_i_watch?) @since 0.8.0 @return [Fixnum] The number of bytes. @return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 185
def get_page_size(title)
  page_info_get_val(title, 'length')
end
get_protection_levels(title) click to toggle source

Gets the levels of protection on the page. @param (see get_categories_in_page) @see (see do_i_watch?) @since 0.8.0 @return [Array<Hash<Symbol, String>>] Hashes of all the protection levels. Each has includes a 'type', a

'level', and an 'expiry'. Type refers to the type of change protected against, like 'edit'. Level refers to
the usergroup that is needed to perform that type of edit, like 'sysop'. Expiry refers to when the
protection will expire, if never, it will be 'infinity.

@return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 159
def get_protection_levels(title)
  params = {
    action: 'query',
    titles: title,
    prop: 'info',
    inprop: 'protection'
  }

  response = post(params)

  pageid = response['query']['pages'].keys.find { |id| id != '-1' }
  return unless pageid
  protection = response['query']['pages'][pageid]['protection']
  protection.each do |p|
    p.keys.each { |k| p[k.to_sym] = p.delete(k) }
  end

  protection
end
get_templates_in_page(title, limit = @query_limit_default) click to toggle source

Gets all of the templates in the given page. @param (see get_external_links) @see www.mediawiki.org/wiki/API:Templates MediaWiki Templates API Docs @since 0.8.0 @return [Array<String>] All of the template titles in the page. @return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 215
def get_templates_in_page(title, limit = @query_limit_default)
  params = {
    prop: 'templates',
    titles: title,
    tllimit: get_limited(limit)
  }

  query(params) do |return_val, query|
    pageid = query['pages'].keys.find { |id| id != '-1' }
    return unless pageid
    templates = query['pages'][pageid].fetch('templates', [])
    return_val.concat(templates.collect { |template| template['title'] })
  end
end
get_text(title) click to toggle source

Gets the wiki text for the given page. Returns nil if it for some reason cannot get the text, for example, if the page does not exist. @param (see get_categories_in_page) @see www.mediawiki.org/wiki/API:Revisions MediaWiki Revisions API Docs @since 0.8.0 @return [String] String containing page contents. @return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 34
def get_text(title)
  params = {
    action: 'query',
    prop: 'revisions',
    rvprop: 'content',
    titles: title
  }

  response = post(params)
  revid = response['query']['pages'].keys.find(MediaWiki::Constants::MISSING_PAGEID_PROC) { |id| id != '-1' }
  revision = response['query']['pages'][revid]
  revision['missing'] == '' ? nil : revision['revisions'][0]['*']
end
page_new?(title) click to toggle source

Gets whether the given page only has one edit. @param (see get_categories_in_page) @see (see do_i_watch?) @since 0.8.0 @return [Boolean] Whether the page only has one edit. @return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 125
def page_new?(title)
  page_info_contains_key(title, 'new')
end
page_redirect?(title) click to toggle source

Gets whether the given page is a redirect. @param (see get_categories_in_page) @see (see do_i_watch?) @since 0.8.0 @return [Boolean] Whether the page is a redirect. @return [Nil] If the page does not exist.

# File lib/mediawiki/query/properties/pages.rb, line 115
def page_redirect?(title)
  page_info_contains_key(title, 'redirect')
end

Private Instance Methods

get_basic_page_info(title, inprop = nil) click to toggle source

Performs a query for the page info with the provided property. @param title [String] The page name @param inprop [String] The inprop to use. See the MediaWiki API documentation. @return (see post)

# File lib/mediawiki/query/properties/pages.rb, line 308
def get_basic_page_info(title, inprop = nil)
  params = {
    action: 'query',
    titles: title,
    prop: 'info'
  }
  params[:inprop] = inprop if inprop

  post(params)
end
page_info_contains_key(title, key, inprop = nil) click to toggle source

Performs a query for the page info with the provided property. Checks if the first non-missing page returned

contains the provided key.

@param title [String] The page name @param key [String] The key to check for @param inprop [String] The inprop to use. See the MediaWiki API documentation. @return [Nil] If the page is not found. @return [Boolean] If the page object contains the provided key.

# File lib/mediawiki/query/properties/pages.rb, line 326
def page_info_contains_key(title, key, inprop = nil)
  response = get_basic_page_info(title, inprop)
  pageid = response['query']['pages'].keys.find { |id| id != '-1' }
  return unless pageid
  response['query']['pages'][pageid].key?(key)
end
page_info_get_val(title, key, inprop = nil) click to toggle source

Performs a query for the page info with the provided property, and returns a value by the provided key in

the page's returned object.

@param (see page_info_contains_key) @return [Nil] If the page is not found. @return [Any] The returned value for the key.

# File lib/mediawiki/query/properties/pages.rb, line 338
def page_info_get_val(title, key, inprop = nil)
  response = get_basic_page_info(title, inprop)
  pageid = response['query']['pages'].keys.find { |id| id != '-1' }
  return unless pageid
  response['query']['pages'][pageid][key]
end