Module: MediaWiki::Query::Properties::Pages

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

Instance Method Summary collapse

Instance Method Details

#can_i_read?(title) ⇒ Boolean, Nil

Gets whether the current user (can be anonymous) can read the page.

Parameters:

  • title (String)

    The page title.

Returns:

  • (Boolean)

    Whether the user can read the page.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



105
106
107
# 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) ⇒ Boolean, Nil

Gets whether the current user watches the page.

Parameters:

  • title (String)

    The page title.

Returns:

  • (Boolean)

    Whether the user watches the page.

  • (Boolean)

    False if the user is not logged in.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



94
95
96
97
# 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

Gets every single link in a page.

Parameters:

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

    The maximum number of members to get. Defaults to 500, and cannot be greater than that unless the user is a bot. If the user is a bot, the limit cannot be greater than 5000.

  • title (String)

    The page title.

Returns:

  • (Array<String>)

    All link titles.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/mediawiki/query/properties/pages.rb', line 287

def get_all_links_in_page(title, limit = @query_limit_default)
  params = {
    prop: 'links',
    titles: title,
    pllimit: get_limited(limit)
  }

  query(params) do |return_val, query|
    pageid = query['pages'].keys.find { |id| id != '-1' }
    return unless pageid
    links = query['pages'][pageid].fetch('links', [])
    return_val.concat(links.collect { |l| l['title'] })
  end
end

#get_basic_page_info(title, inprop = nil) ⇒ Object (private)

Performs a query for the page info with the provided property.

Parameters:

  • title (String)

    The page name

  • inprop (String) (defaults to: nil)

    The inprop to use. See the MediaWiki API documentation.



308
309
310
311
312
313
314
315
316
317
# 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

#get_categories_in_page(title) ⇒ Array<String>, Nil

Gets all categories in the page.

Parameters:

  • title (String)

    The page title.

Returns:

  • (Array<String>)

    All the categories

  • (Nil)

    If the title does not exist.

See Also:

Since:

  • 0.8.0



14
15
16
17
18
19
20
21
22
23
24
25
# 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) ⇒ String, Nil

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.

Parameters:

  • title (String)

    The page title.

Returns:

  • (String)

    The page’s display title.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



146
147
148
# File 'lib/mediawiki/query/properties/pages.rb', line 146

def get_display_title(title)
  page_info_get_val(title, 'displaytitle', 'displaytitle')
end

Gets all the external links on a given page.

Parameters:

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

    The maximum number of members to get. Defaults to 500, and cannot be greater than that unless the user is a bot. If the user is a bot, the limit cannot be greater than 5000.

  • title (String)

    The page title.

Returns:

  • (Array<String>)

    All external link URLs.

See Also:

Since:

  • 0.8.0



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mediawiki/query/properties/pages.rb', line 72

def get_external_links(title, limit = @query_limit_default)
  params = {
    action: 'query',
    titles: title,
    prop: 'extlinks',
    ellimit: 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]['extlinks'].collect { |l| l['*'] })
  end
end

#get_id(title) ⇒ Fixnum, Nil

Gets the revision ID for the given page.

Parameters:

  • title (String)

    The page title.

Returns:

  • (Fixnum)

    The page’s ID

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Array<String>, Nil

Gets all of the images in the given page.

Parameters:

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

    The maximum number of members to get. Defaults to 500, and cannot be greater than that unless the user is a bot. If the user is a bot, the limit cannot be greater than 5000.

  • title (String)

    The page title.

Returns:

  • (Array<String>)

    All of the image titles in the page.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



195
196
197
198
199
200
201
202
203
204
205
206
207
# 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

Gets all of the interwiki links on the given page.

Parameters:

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

    The maximum number of members to get. Defaults to 500, and cannot be greater than that unless the user is a bot. If the user is a bot, the limit cannot be greater than 5000.

  • title (String)

    The page title.

Returns:

  • (Array<Hash<Symbol, String>>)

    All interwiki links. Each hash has a :prefix and :title

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/mediawiki/query/properties/pages.rb', line 236

def get_interwiki_links_in_page(title, limit = @query_limit_default)
  params = {
    prop: 'iwlinks',
    titles: title,
    tllimit: get_limited(limit)
  }

  query(params) do |return_val, query|
    pageid = query['pages'].keys.find { |id| id != '-1' }
    return unless pageid
    iwlinks = query['pages'][pageid].fetch('iwlinks', [])
    return_val.concat(iwlinks.collect { |l| { prefix: l['prefix'], title: l['*']} })
  end
end

#get_number_of_watchers(title) ⇒ Fixnum, Nil

Gets the number of users that watch the given page.

Returns:

  • (Fixnum)

    The number of watchers.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



135
136
137
# 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) ⇒ Hash, Nil

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.

Parameters:

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

    The maximum number of members to get. Defaults to 500, and cannot be greater than that unless the user is a bot. If the user is a bot, the limit cannot be greater than 5000.

  • title (String)

    The page title.

Returns:

  • (Hash)

    The data described previously.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# 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) ⇒ Fixnum, Nil

Gets the size, in bytes, of the page.

Parameters:

  • title (String)

    The page title.

Returns:

  • (Fixnum)

    The number of bytes.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



185
186
187
# 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) ⇒ Array<Hash<Symbol, String>>, Nil

Gets the levels of protection on the page.

Parameters:

  • title (String)

    The page title.

Returns:

  • (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.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# 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) ⇒ Array<String>, Nil

Gets all of the templates in the given page.

Parameters:

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

    The maximum number of members to get. Defaults to 500, and cannot be greater than that unless the user is a bot. If the user is a bot, the limit cannot be greater than 5000.

  • title (String)

    The page title.

Returns:

  • (Array<String>)

    All of the template titles in the page.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# 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) ⇒ String, Nil

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.

Parameters:

  • title (String)

    The page title.

Returns:

  • (String)

    String containing page contents.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



34
35
36
37
38
39
40
41
42
43
44
45
46
# 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_info_contains_key(title, key, inprop = nil) ⇒ Nil, Boolean (private)

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

Parameters:

  • title (String)

    The page name

  • key (String)

    The key to check for

  • inprop (String) (defaults to: nil)

    The inprop to use. See the MediaWiki API documentation.

Returns:

  • (Nil)

    If the page is not found.

  • (Boolean)

    If the page object contains the provided key.



326
327
328
329
330
331
# 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) ⇒ Nil, Any (private)

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.

Parameters:

  • title (String)

    The page name

  • key (String)

    The key to check for

  • inprop (String) (defaults to: nil)

    The inprop to use. See the MediaWiki API documentation.

Returns:

  • (Nil)

    If the page is not found.

  • (Any)

    The returned value for the key.



338
339
340
341
342
343
# 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

#page_new?(title) ⇒ Boolean, Nil

Gets whether the given page only has one edit.

Parameters:

  • title (String)

    The page title.

Returns:

  • (Boolean)

    Whether the page only has one edit.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



125
126
127
# File 'lib/mediawiki/query/properties/pages.rb', line 125

def page_new?(title)
  page_info_contains_key(title, 'new')
end

#page_redirect?(title) ⇒ Boolean, Nil

Gets whether the given page is a redirect.

Parameters:

  • title (String)

    The page title.

Returns:

  • (Boolean)

    Whether the page is a redirect.

  • (Nil)

    If the page does not exist.

See Also:

Since:

  • 0.8.0



115
116
117
# File 'lib/mediawiki/query/properties/pages.rb', line 115

def page_redirect?(title)
  page_info_contains_key(title, 'redirect')
end