Module: MediaWiki::Watch

Included in:
Butt
Defined in:
lib/mediawiki/watch.rb

Instance Method Summary collapse

Instance Method Details

#unwatch(titles) ⇒ Hash{String => Nil, Boolean}

Removes a page or an array of pages from the current user’s watchlist.

Parameters:

  • titles (Array<String>, String)

    An array of page titles, or a page title as a string.

Returns:

  • (Hash{String => Nil, Boolean})

    Keys are page titles. Nil value means the page was missing, but it was watched anyway. True means the page was watched and it exists. False means the page was not watched.

See Also:



16
17
18
# File 'lib/mediawiki/watch.rb', line 16

def unwatch(titles)
  watch_request(titles, true)
end

#watch(titles) ⇒ Hash{String => Nil, Boolean}

Adds a page or an array of pages to the current user’s watchlist.

Parameters:

  • titles (Array<String>, String)

    An array of page titles, or a page title as a string.

Returns:

  • (Hash{String => Nil, Boolean})

    Keys are page titles. Nil value means the page was missing, but it was watched anyway. True means the page was watched and it exists. False means the page was not watched.

See Also:



8
9
10
# File 'lib/mediawiki/watch.rb', line 8

def watch(titles)
  watch_request(titles)
end

#watch_request(titles, unwatch = false) ⇒ Hash{String => Nil, Boolean} (private)

Submits a watch action request.

Parameters:

  • unwatch (Boolean) (defaults to: false)

    Whether the request should unwatch the pages or not.

  • titles (Array<String>, String)

    An array of page titles, or a page title as a string.

Returns:

  • (Hash{String => Nil, Boolean})

    Keys are page titles. Nil value means the page was missing, but it was watched anyway. True means the page was watched and it exists. False means the page was not watched.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mediawiki/watch.rb', line 26

def watch_request(titles, unwatch = false)
  titles = titles.is_a?(Array) ? titles : [titles]
  params = {
    action: 'watch',
    titles: titles.shift(get_limited(titles.length, 50, 500)).join('|'),
    token: get_token('watch')
  }
  success_key = 'watched'
  if unwatch
    params[:unwatch] = 1
    success_key = 'unwatched'
  end

  post(params)['watch'].inject({}) do |result, entry|
    title = entry['title']
    if entry.key?(success_key)
      result[title] = entry.key?('missing') ? nil : true
    else
      result[title] = false
    end

    result
  end
end