Module: MediaWiki::Query::Properties::Pages
- Included in:
- MediaWiki::Query::Properties
- Defined in:
- lib/mediawiki/query/properties/pages.rb
Instance Method Summary collapse
-
#can_i_read?(title) ⇒ Boolean, Nil
Gets whether the current user (can be anonymous) can read the page.
-
#do_i_watch?(title) ⇒ Boolean, Nil
Gets whether the current user watches the page.
-
#get_all_links_in_page(title, limit = @query_limit_default) ⇒ Array<String>, Nil
Gets every single link in a page.
-
#get_basic_page_info(title, inprop = nil) ⇒ Object
private
Performs a query for the page info with the provided property.
-
#get_categories_in_page(title) ⇒ Array<String>, Nil
Gets all categories in the page.
-
#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.
-
#get_external_links(title, limit = @query_limit_default) ⇒ Array<String>
Gets all the external links on a given page.
-
#get_id(title) ⇒ Fixnum, Nil
Gets the revision ID for the given page.
-
#get_images_in_page(title, limit = @query_limit_default) ⇒ Array<String>, Nil
Gets all of the images in the given page.
-
#get_interwiki_links_in_page(title, limit = @query_limit_default) ⇒ Array<Hash<Symbol, String>>, Nil
Gets all of the interwiki links on the given page.
-
#get_number_of_watchers(title) ⇒ Fixnum, Nil
Gets the number of users that watch the given page.
-
#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.
-
#get_page_size(title) ⇒ Fixnum, Nil
Gets the size, in bytes, of the page.
-
#get_protection_levels(title) ⇒ Array<Hash<Symbol, String>>, Nil
Gets the levels of protection on the page.
-
#get_templates_in_page(title, limit = @query_limit_default) ⇒ Array<String>, Nil
Gets all of the templates in the given page.
-
#get_text(title) ⇒ String, Nil
Gets the wiki text for the given page.
-
#page_info_contains_key(title, key, inprop = nil) ⇒ Nil, Boolean
private
Performs a query for the page info with the provided property.
-
#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.
-
#page_new?(title) ⇒ Boolean, Nil
Gets whether the given page only has one edit.
-
#page_redirect?(title) ⇒ Boolean, Nil
Gets whether the given page is a redirect.
Instance Method Details
#can_i_read?(title) ⇒ Boolean, Nil
Gets whether the current user (can be anonymous) can read the page.
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.
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 |
#get_all_links_in_page(title, limit = @query_limit_default) ⇒ Array<String>, Nil
Gets every single link in a page.
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.
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.
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.
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 |
#get_external_links(title, limit = @query_limit_default) ⇒ Array<String>
Gets all the external links on a given page.
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.
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.
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 |
#get_interwiki_links_in_page(title, limit = @query_limit_default) ⇒ Array<Hash<Symbol, String>>, Nil
Gets all of the interwiki links on the given page.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
115 116 117 |
# File 'lib/mediawiki/query/properties/pages.rb', line 115 def page_redirect?(title) page_info_contains_key(title, 'redirect') end |